1 #include "qemu/osdep.h"
2 #include "ui/console.h"
4 #include "cursor_hidden.xpm"
5 #include "cursor_left_ptr.xpm"
7 /* for creating built-in cursors */
8 static QEMUCursor
*cursor_parse_xpm(const char *xpm
[])
12 unsigned int width
, height
, colors
, chars
;
13 unsigned int line
= 0, i
, r
, g
, b
, x
, y
, pixel
;
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",
25 fprintf(stderr
, "%s: chars != 1 not supported\n", __func__
);
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
;
37 if (strcmp(name
, "None") == 0) {
38 ctab
[idx
] = 0x00000000;
42 fprintf(stderr
, "%s: color parse error: \"%s\"\n",
47 /* parse pixel data */
48 c
= cursor_alloc(width
, height
);
51 for (pixel
= 0, y
= 0; y
< height
; y
++, line
++) {
52 for (x
= 0; x
< height
; x
++, pixel
++) {
54 c
->data
[pixel
] = ctab
[idx
];
60 /* nice for debugging */
61 void cursor_print_ascii_art(QEMUCursor
*c
, const char *prefix
)
63 uint32_t *data
= c
->data
;
66 for (y
= 0; y
< c
->height
; y
++) {
67 fprintf(stderr
, "%s: %2d: |", prefix
, y
);
68 for (x
= 0; x
< c
->width
; x
++, data
++) {
69 if ((*data
& 0xff000000) != 0xff000000) {
70 fprintf(stderr
, " "); /* transparent */
71 } else if ((*data
& 0x00ffffff) == 0x00ffffff) {
72 fprintf(stderr
, "."); /* white */
73 } else if ((*data
& 0x00ffffff) == 0x00000000) {
74 fprintf(stderr
, "X"); /* black */
76 fprintf(stderr
, "o"); /* other */
79 fprintf(stderr
, "|\n");
83 QEMUCursor
*cursor_builtin_hidden(void)
85 return cursor_parse_xpm(cursor_hidden_xpm
);
88 QEMUCursor
*cursor_builtin_left_ptr(void)
90 return cursor_parse_xpm(cursor_left_ptr_xpm
);
93 QEMUCursor
*cursor_alloc(uint16_t width
, uint16_t height
)
96 size_t datasize
= width
* height
* sizeof(uint32_t);
98 /* Modern physical hardware typically uses 512x512 sprites */
99 if (width
> 512 || height
> 512) {
103 c
= g_malloc0(sizeof(QEMUCursor
) + datasize
);
110 QEMUCursor
*cursor_ref(QEMUCursor
*c
)
116 void cursor_unref(QEMUCursor
*c
)
126 int cursor_get_mono_bpl(QEMUCursor
*c
)
128 return DIV_ROUND_UP(c
->width
, 8);
131 void cursor_set_mono(QEMUCursor
*c
,
132 uint32_t foreground
, uint32_t background
, uint8_t *image
,
133 int transparent
, uint8_t *mask
)
135 uint32_t *data
= c
->data
;
138 bool expand_bitmap_only
= image
== mask
;
139 bool has_inverted_colors
= false;
140 const uint32_t inverted
= 0x80000000;
143 * Converts a monochrome bitmap with XOR mask 'image' and AND mask 'mask':
144 * https://docs.microsoft.com/en-us/windows-hardware/drivers/display/drawing-monochrome-pointers
146 bpl
= cursor_get_mono_bpl(c
);
147 for (y
= 0; y
< c
->height
; y
++) {
149 for (x
= 0; x
< c
->width
; x
++, data
++) {
150 if (transparent
&& mask
[x
/8] & bit
) {
151 if (!expand_bitmap_only
&& image
[x
/ 8] & bit
) {
153 has_inverted_colors
= true;
157 } else if (!transparent
&& !(mask
[x
/8] & bit
)) {
159 } else if (image
[x
/8] & bit
) {
160 *data
= 0xff000000 | foreground
;
162 *data
= 0xff000000 | background
;
174 * If there are any pixels with inverted colors, create an outline (fill
175 * transparent neighbors with the background color) and use the foreground
176 * color as "inverted" color.
178 if (has_inverted_colors
) {
180 for (y
= 0; y
< c
->height
; y
++) {
181 for (x
= 0; x
< c
->width
; x
++, data
++) {
182 if (*data
== 0 /* transparent */ &&
183 ((x
> 0 && data
[-1] == inverted
) ||
184 (x
+ 1 < c
->width
&& data
[1] == inverted
) ||
185 (y
> 0 && data
[-c
->width
] == inverted
) ||
186 (y
+ 1 < c
->height
&& data
[c
->width
] == inverted
))) {
187 *data
= 0xff000000 | background
;
192 for (x
= 0; x
< c
->width
* c
->height
; x
++, data
++) {
193 if (*data
== inverted
) {
194 *data
= 0xff000000 | foreground
;
200 void cursor_get_mono_image(QEMUCursor
*c
, int foreground
, uint8_t *image
)
202 uint32_t *data
= c
->data
;
206 bpl
= cursor_get_mono_bpl(c
);
207 memset(image
, 0, bpl
* c
->height
);
208 for (y
= 0; y
< c
->height
; y
++) {
210 for (x
= 0; x
< c
->width
; x
++, data
++) {
211 if (((*data
& 0xff000000) == 0xff000000) &&
212 ((*data
& 0x00ffffff) == foreground
)) {
224 void cursor_get_mono_mask(QEMUCursor
*c
, int transparent
, uint8_t *mask
)
226 uint32_t *data
= c
->data
;
230 bpl
= cursor_get_mono_bpl(c
);
231 memset(mask
, 0, bpl
* c
->height
);
232 for (y
= 0; y
< c
->height
; y
++) {
234 for (x
= 0; x
< c
->width
; x
++, data
++) {
235 if ((*data
& 0xff000000) != 0xff000000) {
236 if (transparent
!= 0) {
240 if (transparent
== 0) {