1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "ui/console.h"
5 #include "cursor_hidden.xpm"
6 #include "cursor_left_ptr.xpm"
8 /* for creating built-in cursors */
9 static QEMUCursor
*cursor_parse_xpm(const char *xpm
[])
13 unsigned int width
, height
, colors
, chars
;
14 unsigned int line
= 0, i
, r
, g
, b
, x
, y
, pixel
;
18 /* parse header line: width, height, #colors, #chars */
19 if (sscanf(xpm
[line
], "%u %u %u %u",
20 &width
, &height
, &colors
, &chars
) != 4) {
21 fprintf(stderr
, "%s: header parse error: \"%s\"\n",
22 __FUNCTION__
, xpm
[line
]);
26 fprintf(stderr
, "%s: chars != 1 not supported\n", __FUNCTION__
);
31 /* parse color table */
32 for (i
= 0; i
< colors
; i
++, line
++) {
33 if (sscanf(xpm
[line
], "%c c %15s", &idx
, name
) == 2) {
34 if (sscanf(name
, "#%02x%02x%02x", &r
, &g
, &b
) == 3) {
35 ctab
[idx
] = (0xff << 24) | (b
<< 16) | (g
<< 8) | r
;
38 if (strcmp(name
, "None") == 0) {
39 ctab
[idx
] = 0x00000000;
43 fprintf(stderr
, "%s: color parse error: \"%s\"\n",
44 __FUNCTION__
, xpm
[line
]);
48 /* parse pixel data */
49 c
= cursor_alloc(width
, height
);
50 for (pixel
= 0, y
= 0; y
< height
; y
++, line
++) {
51 for (x
= 0; x
< height
; x
++, pixel
++) {
53 c
->data
[pixel
] = ctab
[idx
];
59 /* nice for debugging */
60 void cursor_print_ascii_art(QEMUCursor
*c
, const char *prefix
)
62 uint32_t *data
= c
->data
;
65 for (y
= 0; y
< c
->height
; y
++) {
66 fprintf(stderr
, "%s: %2d: |", prefix
, y
);
67 for (x
= 0; x
< c
->width
; x
++, data
++) {
68 if ((*data
& 0xff000000) != 0xff000000) {
69 fprintf(stderr
, " "); /* transparent */
70 } else if ((*data
& 0x00ffffff) == 0x00ffffff) {
71 fprintf(stderr
, "."); /* white */
72 } else if ((*data
& 0x00ffffff) == 0x00000000) {
73 fprintf(stderr
, "X"); /* black */
75 fprintf(stderr
, "o"); /* other */
78 fprintf(stderr
, "|\n");
82 QEMUCursor
*cursor_builtin_hidden(void)
84 return cursor_parse_xpm(cursor_hidden_xpm
);
87 QEMUCursor
*cursor_builtin_left_ptr(void)
89 return cursor_parse_xpm(cursor_left_ptr_xpm
);
92 QEMUCursor
*cursor_alloc(int width
, int height
)
95 int datasize
= width
* height
* sizeof(uint32_t);
97 c
= g_malloc0(sizeof(QEMUCursor
) + datasize
);
104 void cursor_get(QEMUCursor
*c
)
109 void cursor_put(QEMUCursor
*c
)
119 int cursor_get_mono_bpl(QEMUCursor
*c
)
121 return (c
->width
+ 7) / 8;
124 void cursor_set_mono(QEMUCursor
*c
,
125 uint32_t foreground
, uint32_t background
, uint8_t *image
,
126 int transparent
, uint8_t *mask
)
128 uint32_t *data
= c
->data
;
132 bpl
= cursor_get_mono_bpl(c
);
133 for (y
= 0; y
< c
->height
; y
++) {
135 for (x
= 0; x
< c
->width
; x
++, data
++) {
136 if (transparent
&& mask
[x
/8] & bit
) {
138 } else if (!transparent
&& !(mask
[x
/8] & bit
)) {
140 } else if (image
[x
/8] & bit
) {
141 *data
= 0xff000000 | foreground
;
143 *data
= 0xff000000 | background
;
155 void cursor_get_mono_image(QEMUCursor
*c
, int foreground
, uint8_t *image
)
157 uint32_t *data
= c
->data
;
161 bpl
= cursor_get_mono_bpl(c
);
162 memset(image
, 0, bpl
* c
->height
);
163 for (y
= 0; y
< c
->height
; y
++) {
165 for (x
= 0; x
< c
->width
; x
++, data
++) {
166 if (((*data
& 0xff000000) == 0xff000000) &&
167 ((*data
& 0x00ffffff) == foreground
)) {
179 void cursor_get_mono_mask(QEMUCursor
*c
, int transparent
, uint8_t *mask
)
181 uint32_t *data
= c
->data
;
185 bpl
= cursor_get_mono_bpl(c
);
186 memset(mask
, 0, bpl
* c
->height
);
187 for (y
= 0; y
< c
->height
; y
++) {
189 for (x
= 0; x
< c
->width
; x
++, data
++) {
190 if ((*data
& 0xff000000) != 0xff000000) {
191 if (transparent
!= 0) {
195 if (transparent
== 0) {