2f8ca636462a32d79957f7573d183deb03fb74b1
[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 SMALL_FONT FrutigerLT_Roman_12_font
42
43 #define VALUE_Y         BIG_FONT.ascent
44 #define LABEL_Y         BIG_FONT.ascent + SMALL_FONT.ascent + 2
45 #define BOX_X           2
46 #define PAD_X           90
47 #define BOX_LABEL_X     30
48 #define PAD_LABEL_X     95
49 #define SEP_X           (PAD_X - 8)
50
51 static int      box_number = 1;
52 static int      pad_number = 1;
53 static bool     do_polys = false;
54
55 static const struct ao_coord trek[] = {
56         { .x = 90, .y = 0 },
57         { .x = 60, .y = 40 },
58         { .x = 90, .y = 20 },
59         { .x = 120, .y = 40 },
60 };
61
62 #define NCOORD_TREK (sizeof(trek)/sizeof(trek[0]))
63
64 static const struct ao_coord donut[] = {
65         { .x = 30, .y = 0 },
66         { .x = 0, .y = 30 },
67         { .x = 30, .y = 60 },
68         { .x = 60, .y = 30 },
69         { .x = 30, .y = 0 },
70         { .x = 30, .y = 10 },
71         { .x = 50, .y = 30 },
72         { .x = 30, .y = 50 },
73         { .x = 10, .y = 30 },
74         { .x = 30, .y = 10 },
75 };
76
77 #define NCOORD_DONUT (sizeof(donut)/sizeof(donut[0]))
78
79 void HandleExpose(Display *dpy, Window win, GC gc)
80 {
81         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, 0xffffffff, AO_COPY);
82
83         if (do_polys) {
84                 ao_poly(&fb, trek, NCOORD_TREK, 0x00000000, AO_COPY);
85                 ao_poly(&fb, donut, NCOORD_DONUT, 0x00000000, AO_COPY);
86         } else {
87                 char    str[64];
88
89                 sprintf(str, "%02d", box_number);
90                 ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
91                 ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "box", 0x00000000, AO_COPY);
92
93                 sprintf(str, "%d", pad_number);
94                 ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, 0x00000000, AO_COPY);
95                 ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "pad", 0x00000000, AO_COPY);
96
97                 ao_line(&fb, SEP_X, 0, SEP_X, HEIGHT, 0x00000000, AO_COPY);
98         }
99
100         XImage *image = XCreateImage(dpy, visual, 1, XYBitmap, 0, (char *) bits, WIDTH, HEIGHT, 32, STRIDE*4);
101         XSetForeground(dpy, gc, WhitePixel(dpy, screen));
102         XSetBackground(dpy, gc, BlackPixel(dpy, screen));
103         XPutImage(dpy, win, gc, image, 0, 0, 0, 0, WIDTH, HEIGHT);
104         free(image);
105 }
106
107 void
108 HandleKeyPress(Display *dpy, Window win, GC gc, XEvent *ev)
109 {
110         char    string[10];
111         if (XLookupString ((XKeyEvent *) ev, string, sizeof (string), 0, 0) >= 1) {
112                 printf("key %s\n", string);
113                 switch (string[0]) {
114                 case 'q':
115                         exit (0);
116                 case 'p':
117                         pad_number++;
118                         if (pad_number > 8)
119                                 pad_number = 1;
120                         break;
121                 case 'P':
122                         pad_number--;
123                         if (pad_number < 1)
124                                 pad_number = 8;
125                         break;
126                 case 'b':
127                         box_number++;
128                         if (box_number > 99)
129                                 box_number = 1;
130                         break;
131                 case 'B':
132                         box_number--;
133                         if (box_number < 1)
134                                 box_number = 99;
135                         break;
136                 case 's':
137                         do_polys = !do_polys;
138                         break;
139                 case 'c':
140                         break;
141                 }
142                 HandleExpose(dpy, win, gc);
143         }
144 }
145
146 void
147 HandleKeyRelease(Display *dpy, Window win, GC gc, XEvent *ev)
148 {
149 }