altos/draw: Use bitstream fonts for LCO demo
[fw/altos] / src / draw / lco-test.c
1 /*
2  * Copyright © 2023 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define TIMEOUT 50
20
21 #define PASS_KEYS
22 #include "test-frame.c"
23
24 #define BIG_FONT BitstreamVeraSans_Roman_58_font
25 #define VOLT_FONT BitstreamVeraSans_Roman_58_font
26 #define SMALL_FONT BitstreamVeraSans_Roman_12_font
27 #define TINY_FONT BitstreamVeraSans_Roman_12_font
28 #define LOGO_FONT BenguiatGothicStd_Bold_26_font
29
30 #define LABEL_Y         (int16_t) (SMALL_FONT.ascent)
31 #define VALUE_Y         (int16_t) (LABEL_Y + BIG_FONT.ascent + 3)
32 #define BOX_X           2
33 #define PAD_X           90
34 #define BOX_LABEL_X     30
35 #define VOLT_LABEL_X    25
36 #define RSSI_LABEL_X    15
37 #define PAD_LABEL_X     95
38 #define SEP_X           (PAD_X - 10)
39 #define SCAN_X          (WIDTH - 100) / 2
40 #define SCAN_Y          49
41 #define SCAN_HEIGHT     4
42 #define FOUND_Y         63
43 #define FOUND_WIDTH     21
44 #define MAX_VALID       (WIDTH / FOUND_WIDTH)
45
46 static int      box_number = 1;
47 static int      pad_number = 1;
48 static int      do_polys = 1;
49 static int      scan_number = 0;
50
51 static const struct ao_coord trek[] = {
52         { .x = 90, .y = 0 },
53         { .x = 60, .y = 40 },
54         { .x = 90, .y = 20 },
55         { .x = 120, .y = 40 },
56 };
57
58 #define NCOORD_TREK (sizeof(trek)/sizeof(trek[0]))
59
60 static const struct ao_coord donut[] = {
61         { .x = 30, .y = 0 },
62         { .x = 0, .y = 30 },
63         { .x = 30, .y = 60 },
64         { .x = 60, .y = 30 },
65         { .x = 30, .y = 0 },
66         { .x = 30, .y = 10 },
67         { .x = 50, .y = 30 },
68         { .x = 30, .y = 50 },
69         { .x = 10, .y = 30 },
70         { .x = 30, .y = 10 },
71 };
72
73 #define NCOORD_DONUT (sizeof(donut)/sizeof(donut[0]))
74
75 static const struct ao_coord bowtie[] = {
76         { .x = 0, .y = 0 },
77         { .x = 32, .y = 32 },
78         { .x = 0, .y = 32 },
79         { .x = 32, .y = 0 },
80 };
81
82 #define NCOORD_BOWTIE (sizeof(bowtie)/sizeof(bowtie[0]))
83
84 static const struct ao_transform logo_transform = {
85         .x_scale = 48, .x_off = 0,
86         .y_scale = 48, .y_off = 0,
87 };
88
89 static const struct ao_transform bowtie_transform = {
90         .x_scale = 1, .x_off = 50,
91         .y_scale = 1, .y_off = 20,
92 };
93
94 static const float pad_volts = 12.3f;
95 static const float lco_volts = 4.1f;
96 static const int rssi = -30;
97
98 static int      boxes[] = { 1, 2, 3, 5, 8, 11, 13, 17, 19, 23, 29, 31, 37, 62, 97 };
99
100 static int      max_box = 97;
101
102 #define ARRAYSIZE(a)    (sizeof(a) / sizeof((a)[0]))
103
104 static bool
105 valid_box(int box)
106 {
107         size_t i;
108         if (box == 0)
109                 return true;
110         for (i = 0; i < ARRAYSIZE(boxes); i++)
111                 if (boxes[i] == box)
112                         return true;
113         return false;
114 }
115
116 static void
117 next_box(void)
118 {
119         for (int n = box_number + 1; n <= max_box; n++)
120                 if (valid_box(n)) {
121                         box_number = n;
122                         return;
123                 }
124         box_number = 0;
125 }
126
127 static void
128 prev_box(void)
129 {
130         for (int n = box_number - 1; n >= 0; n--)
131                 if (valid_box(n)) {
132                         box_number = n;
133                         return;
134                 }
135         box_number = max_box;
136 }
137
138 void HandleExpose(Display *dpy, Window win, GC gc)
139 {
140         char    str[64];
141         int     i;
142         int     v;
143         int16_t b;
144
145         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, 0xffffffff, AO_COPY);
146
147         if (do_polys == 1)
148                 current_timeout = TIMEOUT;
149         else
150                 current_timeout = 0;
151         switch (do_polys) {
152         case 1:
153                 ao_logo(&fb, &logo_transform, &LOGO_FONT, 0x00000000, AO_COPY);
154                 if (scan_number) {
155                         ao_rect(&fb, SCAN_X, SCAN_Y, (int16_t) scan_number, SCAN_HEIGHT, 0x00000000, AO_COPY);
156                         b = 0;
157                         v = 0;
158                         for (i = scan_number; i > 1; i--) {
159                                 if (valid_box(i)) {
160                                         v++;
161                                         if (v == MAX_VALID)
162                                                 break;
163                                 }
164                         }
165                         for (; i <= scan_number; i++) {
166                                 if (valid_box(i)) {
167                                         sprintf(str, "%02d", i);
168                                         ao_text(&fb, &TINY_FONT, 0 + FOUND_WIDTH * b, FOUND_Y, str, 0x00000000, AO_COPY);
169                                         b++;
170                                 }
171                         }
172                 }
173                 break;
174         case 2:
175                 ao_poly(&fb, trek, NCOORD_TREK, NULL, 0x00000000, AO_COPY);
176                 ao_poly(&fb, donut, NCOORD_DONUT, NULL, 0x00000000, AO_COPY);
177                 break;
178         case 3:
179                 ao_poly(&fb, bowtie, NCOORD_BOWTIE, &bowtie_transform, 0x00000000, AO_COPY);
180                 break;
181         default:
182         case 0:
183                 switch (box_number) {
184                 case 0:
185                         sprintf(str, "%4.1f", lco_volts);
186                         ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
187                         ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, "LCO Battery", 0x00000000, AO_COPY);
188                         break;
189                 default:
190                         switch (pad_number) {
191                         case -1:
192                                 sprintf(str, "%4.1f", pad_volts);
193                                 ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
194                                 ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, "Pad Battery", 0x00000000, AO_COPY);
195                                 break;
196                         case 0:
197                                 sprintf(str, "%4d", rssi);
198                                 ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
199                                 ao_text(&fb, &SMALL_FONT, RSSI_LABEL_X, LABEL_Y, "Signal Strength", 0x00000000, AO_COPY);
200                                 break;
201                         default:
202                                 sprintf(str, "%02d", box_number);
203                                 ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
204                                 ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "Box", 0x00000000, AO_COPY);
205
206                                 sprintf(str, "%d", pad_number);
207                                 ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, 0x00000000, AO_COPY);
208                                 ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "Pad", 0x00000000, AO_COPY);
209
210                                 ao_rect(&fb, SEP_X, 0, 2, HEIGHT, 0x00000000, AO_COPY);
211                         }
212                         break;
213                 }
214                 break;
215         }
216
217         DoDisplay(dpy, win, gc);
218 }
219
220 void
221 HandleKeyPress(Display *dpy, Window win, GC gc, XEvent *ev)
222 {
223         char    string[10];
224         if (XLookupString ((XKeyEvent *) ev, string, sizeof (string), 0, 0) >= 1) {
225                 switch (string[0]) {
226                 case 'q':
227                         exit (0);
228                 case 'p':
229                         if (box_number != 0) {
230                                 pad_number++;
231                                 if (pad_number > 8)
232                                         pad_number = -1;
233                         }
234                         break;
235                 case 'P':
236                         if (box_number != 0) {
237                                 pad_number--;
238                                 if (pad_number < -1)
239                                         pad_number = 8;
240                         }
241                         break;
242                 case 'b':
243                         next_box();
244                         break;
245                 case 'B':
246                         prev_box();
247                         break;
248                 case 'i':
249                         do_polys++;
250                         if (do_polys == 4)
251                                 do_polys = 0;
252                         if (do_polys == 1)
253                                 scan_number = 0;
254                         break;
255                 case 'I':
256                         do_polys--;
257                         if (do_polys < 0)
258                                 do_polys = 4;
259                         if (do_polys == 1)
260                                 scan_number = 0;
261                         break;
262                 case 's':
263                         if (scan_number < 99)
264                                 scan_number++;
265                         break;
266                 case 'S':
267                         if (scan_number > 0)
268                                 scan_number--;
269                         break;
270                 case 'c':
271                         break;
272                 }
273                 HandleExpose(dpy, win, gc);
274         }
275 }
276
277 void
278 HandleKeyRelease(Display *dpy, Window win, GC gc, XEvent *ev)
279 {
280         (void) dpy;
281         (void) win;
282         (void) gc;
283         (void) ev;
284 }
285
286 void
287 HandleTimeout(Display *dpy, Window win, GC gc)
288 {
289         if (do_polys == 1) {
290                 if (scan_number < 99)
291                         scan_number++;
292                 else {
293                         box_number = boxes[0];
294                         pad_number = 1;
295                         do_polys = 0;
296                 }
297                 HandleExpose(dpy, win, gc);
298         }
299 }