64080dfae59ae620745e78d6d7dedaf7d95838ff
[fw/altos] / src / draw / draw-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 WIDTH   128
20 #define HEIGHT  64
21
22 #define DEFAULT_WIDTH   WIDTH
23 #define DEFAULT_HEIGHT  HEIGHT
24
25 #define PASS_KEYS
26 #include "frame.c"
27 #include "ao_draw.h"
28
29 #define STRIDE  ((WIDTH + 31) / 32)
30
31 static uint32_t bits[STRIDE * HEIGHT];
32
33 static struct ao_bitmap fb = {
34         .base = bits,
35         .stride = STRIDE,
36         .width = WIDTH,
37         .height = HEIGHT
38 };
39
40 #define BIG_FONT FrutigerLT_Roman_64_font
41 #define VOLT_FONT FrutigerLT_Roman_64_font
42 #define SMALL_FONT NotoMono_12_font
43 #define LOGO_FONT BenguiatGothicStd_Bold_26_font
44
45 #define LABEL_Y         (int16_t) (SMALL_FONT.ascent)
46 #define VALUE_Y         (int16_t) (LABEL_Y + BIG_FONT.ascent)
47 #define BOX_X           2
48 #define PAD_X           90
49 #define BOX_LABEL_X     30
50 #define VOLT_LABEL_X    35
51 #define RSSI_LABEL_X    15
52 #define PAD_LABEL_X     95
53 #define SEP_X           (PAD_X - 8)
54
55 static int      box_number = 1;
56 static int      pad_number = 1;
57 static int      do_polys = 0;
58
59 static const struct ao_coord trek[] = {
60         { .x = 90, .y = 0 },
61         { .x = 60, .y = 40 },
62         { .x = 90, .y = 20 },
63         { .x = 120, .y = 40 },
64 };
65
66 #define NCOORD_TREK (sizeof(trek)/sizeof(trek[0]))
67
68 static const struct ao_coord donut[] = {
69         { .x = 30, .y = 0 },
70         { .x = 0, .y = 30 },
71         { .x = 30, .y = 60 },
72         { .x = 60, .y = 30 },
73         { .x = 30, .y = 0 },
74         { .x = 30, .y = 10 },
75         { .x = 50, .y = 30 },
76         { .x = 30, .y = 50 },
77         { .x = 10, .y = 30 },
78         { .x = 30, .y = 10 },
79 };
80
81 #define NCOORD_DONUT (sizeof(donut)/sizeof(donut[0]))
82
83 static const struct ao_coord bowtie[] = {
84         { .x = 0, .y = 0 },
85         { .x = 32, .y = 32 },
86         { .x = 0, .y = 32 },
87         { .x = 32, .y = 0 },
88 };
89
90 #define NCOORD_BOWTIE (sizeof(bowtie)/sizeof(bowtie[0]))
91
92 static const struct ao_transform logo_transform = {
93         .x_scale = 48, .x_off = 0,
94         .y_scale = 48, .y_off = 10,
95 };
96
97 static const struct ao_transform bowtie_transform = {
98         .x_scale = 1, .x_off = 50,
99         .y_scale = 1, .y_off = 20,
100 };
101
102 static const float pad_volts = 12.3f;
103 static const float lco_volts = 4.1f;
104 static const int rssi = -30;
105
106 void HandleExpose(Display *dpy, Window win, GC gc)
107 {
108         char    str[64];
109
110         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, 0xffffffff, AO_COPY);
111
112         switch (do_polys) {
113         case 1:
114                 ao_logo(&fb, &logo_transform, &LOGO_FONT, 0x00000000, AO_COPY);
115                 break;
116         case 2:
117                 ao_poly(&fb, trek, NCOORD_TREK, NULL, 0x00000000, AO_COPY);
118                 ao_poly(&fb, donut, NCOORD_DONUT, NULL, 0x00000000, AO_COPY);
119                 break;
120         case 3:
121                 ao_poly(&fb, bowtie, NCOORD_BOWTIE, &bowtie_transform, 0x00000000, AO_COPY);
122                 break;
123         default:
124         case 0:
125                 switch (box_number) {
126                 case 0:
127                         sprintf(str, "%4.1f", lco_volts);
128                         ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
129                         ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, "LCO Battery", 0x00000000, AO_COPY);
130                         break;
131                 default:
132                         switch (pad_number) {
133                         case -1:
134                                 sprintf(str, "%4.1f", pad_volts);
135                                 ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
136                                 ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, "Pad Battery", 0x00000000, AO_COPY);
137                                 break;
138                         case 0:
139                                 sprintf(str, "%4d", rssi);
140                                 ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
141                                 ao_text(&fb, &SMALL_FONT, RSSI_LABEL_X, LABEL_Y, "Signal Strength", 0x00000000, AO_COPY);
142                                 break;
143                         default:
144                                 sprintf(str, "%02d", box_number);
145                                 ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
146                                 ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "Box", 0x00000000, AO_COPY);
147
148                                 sprintf(str, "%d", pad_number);
149                                 ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, 0x00000000, AO_COPY);
150                                 ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "Pad", 0x00000000, AO_COPY);
151
152                                 ao_line(&fb, SEP_X, 0, SEP_X, HEIGHT, 0x00000000, AO_COPY);
153                         }
154                         break;
155                 }
156                 break;
157         }
158
159         XImage *image = XCreateImage(dpy, visual, 1, XYBitmap, 0, (char *) bits, WIDTH, HEIGHT, 32, STRIDE*4);
160         XSetForeground(dpy, gc, WhitePixel(dpy, screen));
161         XSetBackground(dpy, gc, BlackPixel(dpy, screen));
162         XPutImage(dpy, win, gc, image, 0, 0, 0, 0, WIDTH, HEIGHT);
163         free(image);
164 }
165
166 void
167 HandleKeyPress(Display *dpy, Window win, GC gc, XEvent *ev)
168 {
169         char    string[10];
170         if (XLookupString ((XKeyEvent *) ev, string, sizeof (string), 0, 0) >= 1) {
171                 switch (string[0]) {
172                 case 'q':
173                         exit (0);
174                 case 'p':
175                         if (box_number != 0) {
176                                 pad_number++;
177                                 if (pad_number > 8)
178                                         pad_number = -1;
179                         }
180                         break;
181                 case 'P':
182                         if (box_number != 0) {
183                                 pad_number--;
184                                 if (pad_number < -1)
185                                         pad_number = 8;
186                         }
187                         break;
188                 case 'b':
189                         box_number++;
190                         if (box_number > 99)
191                                 box_number = 0;
192                         break;
193                 case 'B':
194                         box_number--;
195                         if (box_number < 0)
196                                 box_number = 99;
197                         break;
198                 case 's':
199                         do_polys++;
200                         if (do_polys == 4)
201                                 do_polys = 0;
202                         break;
203                 case 'c':
204                         break;
205                 }
206                 HandleExpose(dpy, win, gc);
207         }
208 }
209
210 void
211 HandleKeyRelease(Display *dpy, Window win, GC gc, XEvent *ev)
212 {
213         (void) dpy;
214         (void) win;
215         (void) gc;
216         (void) ev;
217 }