xref: /openbmc/qemu/ui/cursor.c (revision f363d039)
1 #include "qemu/osdep.h"
2 #include "ui/console.h"
3 
4 #include "cursor_hidden.xpm"
5 #include "cursor_left_ptr.xpm"
6 
7 /* for creating built-in cursors */
8 static QEMUCursor *cursor_parse_xpm(const char *xpm[])
9 {
10     QEMUCursor *c;
11     uint32_t ctab[128];
12     unsigned int width, height, colors, chars;
13     unsigned int line = 0, i, r, g, b, x, y, pixel;
14     char name[16];
15     uint8_t idx;
16 
17     /* parse header line: width, height, #colors, #chars */
18     if (sscanf(xpm[line], "%u %u %u %u",
19                &width, &height, &colors, &chars) != 4) {
20         fprintf(stderr, "%s: header parse error: \"%s\"\n",
21                 __func__, xpm[line]);
22         return NULL;
23     }
24     if (chars != 1) {
25         fprintf(stderr, "%s: chars != 1 not supported\n", __func__);
26         return NULL;
27     }
28     line++;
29 
30     /* parse color table */
31     for (i = 0; i < colors; i++, line++) {
32         if (sscanf(xpm[line], "%c c %15s", &idx, name) == 2) {
33             if (sscanf(name, "#%02x%02x%02x", &r, &g, &b) == 3) {
34                 ctab[idx] = (0xff << 24) | (b << 16) | (g << 8) | r;
35                 continue;
36             }
37             if (strcmp(name, "None") == 0) {
38                 ctab[idx] = 0x00000000;
39                 continue;
40             }
41         }
42         fprintf(stderr, "%s: color parse error: \"%s\"\n",
43                 __func__, xpm[line]);
44         return NULL;
45     }
46 
47     /* parse pixel data */
48     c = cursor_alloc(width, height);
49     for (pixel = 0, y = 0; y < height; y++, line++) {
50         for (x = 0; x < height; x++, pixel++) {
51             idx = xpm[line][x];
52             c->data[pixel] = ctab[idx];
53         }
54     }
55     return c;
56 }
57 
58 /* nice for debugging */
59 void cursor_print_ascii_art(QEMUCursor *c, const char *prefix)
60 {
61     uint32_t *data = c->data;
62     int x,y;
63 
64     for (y = 0; y < c->height; y++) {
65         fprintf(stderr, "%s: %2d: |", prefix, y);
66         for (x = 0; x < c->width; x++, data++) {
67             if ((*data & 0xff000000) != 0xff000000) {
68                 fprintf(stderr, " "); /* transparent */
69             } else if ((*data & 0x00ffffff) == 0x00ffffff) {
70                 fprintf(stderr, "."); /* white */
71             } else if ((*data & 0x00ffffff) == 0x00000000) {
72                 fprintf(stderr, "X"); /* black */
73             } else {
74                 fprintf(stderr, "o"); /* other */
75             }
76         }
77         fprintf(stderr, "|\n");
78     }
79 }
80 
81 QEMUCursor *cursor_builtin_hidden(void)
82 {
83     return cursor_parse_xpm(cursor_hidden_xpm);
84 }
85 
86 QEMUCursor *cursor_builtin_left_ptr(void)
87 {
88     return cursor_parse_xpm(cursor_left_ptr_xpm);
89 }
90 
91 QEMUCursor *cursor_alloc(int width, int height)
92 {
93     QEMUCursor *c;
94     int datasize = width * height * sizeof(uint32_t);
95 
96     c = g_malloc0(sizeof(QEMUCursor) + datasize);
97     c->width  = width;
98     c->height = height;
99     c->refcount = 1;
100     return c;
101 }
102 
103 void cursor_get(QEMUCursor *c)
104 {
105     c->refcount++;
106 }
107 
108 void cursor_put(QEMUCursor *c)
109 {
110     if (c == NULL)
111         return;
112     c->refcount--;
113     if (c->refcount)
114         return;
115     g_free(c);
116 }
117 
118 int cursor_get_mono_bpl(QEMUCursor *c)
119 {
120     return DIV_ROUND_UP(c->width, 8);
121 }
122 
123 void cursor_set_mono(QEMUCursor *c,
124                      uint32_t foreground, uint32_t background, uint8_t *image,
125                      int transparent, uint8_t *mask)
126 {
127     uint32_t *data = c->data;
128     uint8_t bit;
129     int x,y,bpl;
130     bool expand_bitmap_only = image == mask;
131     bool has_inverted_colors = false;
132     const uint32_t inverted = 0x80000000;
133 
134     /*
135      * Converts a monochrome bitmap with XOR mask 'image' and AND mask 'mask':
136      * https://docs.microsoft.com/en-us/windows-hardware/drivers/display/drawing-monochrome-pointers
137      */
138     bpl = cursor_get_mono_bpl(c);
139     for (y = 0; y < c->height; y++) {
140         bit = 0x80;
141         for (x = 0; x < c->width; x++, data++) {
142             if (transparent && mask[x/8] & bit) {
143                 if (!expand_bitmap_only && image[x / 8] & bit) {
144                     *data = inverted;
145                     has_inverted_colors = true;
146                 } else {
147                     *data = 0x00000000;
148                 }
149             } else if (!transparent && !(mask[x/8] & bit)) {
150                 *data = 0x00000000;
151             } else if (image[x/8] & bit) {
152                 *data = 0xff000000 | foreground;
153             } else {
154                 *data = 0xff000000 | background;
155             }
156             bit >>= 1;
157             if (bit == 0) {
158                 bit = 0x80;
159             }
160         }
161         mask  += bpl;
162         image += bpl;
163     }
164 
165     /*
166      * If there are any pixels with inverted colors, create an outline (fill
167      * transparent neighbors with the background color) and use the foreground
168      * color as "inverted" color.
169      */
170     if (has_inverted_colors) {
171         data = c->data;
172         for (y = 0; y < c->height; y++) {
173             for (x = 0; x < c->width; x++, data++) {
174                 if (*data == 0 /* transparent */ &&
175                         ((x > 0 && data[-1] == inverted) ||
176                          (x + 1 < c->width && data[1] == inverted) ||
177                          (y > 0 && data[-c->width] == inverted) ||
178                          (y + 1 < c->height && data[c->width] == inverted))) {
179                     *data = 0xff000000 | background;
180                 }
181             }
182         }
183         data = c->data;
184         for (x = 0; x < c->width * c->height; x++, data++) {
185             if (*data == inverted) {
186                 *data = 0xff000000 | foreground;
187             }
188         }
189     }
190 }
191 
192 void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *image)
193 {
194     uint32_t *data = c->data;
195     uint8_t bit;
196     int x,y,bpl;
197 
198     bpl = cursor_get_mono_bpl(c);
199     memset(image, 0, bpl * c->height);
200     for (y = 0; y < c->height; y++) {
201         bit = 0x80;
202         for (x = 0; x < c->width; x++, data++) {
203             if (((*data & 0xff000000) == 0xff000000) &&
204                 ((*data & 0x00ffffff) == foreground)) {
205                 image[x/8] |= bit;
206             }
207             bit >>= 1;
208             if (bit == 0) {
209                 bit = 0x80;
210             }
211         }
212         image += bpl;
213     }
214 }
215 
216 void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask)
217 {
218     uint32_t *data = c->data;
219     uint8_t bit;
220     int x,y,bpl;
221 
222     bpl = cursor_get_mono_bpl(c);
223     memset(mask, 0, bpl * c->height);
224     for (y = 0; y < c->height; y++) {
225         bit = 0x80;
226         for (x = 0; x < c->width; x++, data++) {
227             if ((*data & 0xff000000) != 0xff000000) {
228                 if (transparent != 0) {
229                     mask[x/8] |= bit;
230                 }
231             } else {
232                 if (transparent == 0) {
233                     mask[x/8] |= bit;
234                 }
235             }
236             bit >>= 1;
237             if (bit == 0) {
238                 bit = 0x80;
239             }
240         }
241         mask += bpl;
242     }
243 }
244