2
0

cursor.c 6.7 KB

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