cursor.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. assert(c != NULL);
  45. for (pixel = 0, y = 0; y < height; y++, line++) {
  46. for (x = 0; x < height; x++, pixel++) {
  47. idx = xpm[line][x];
  48. c->data[pixel] = ctab[idx];
  49. }
  50. }
  51. return c;
  52. }
  53. /* nice for debugging */
  54. void cursor_print_ascii_art(QEMUCursor *c, const char *prefix)
  55. {
  56. uint32_t *data = c->data;
  57. int x,y;
  58. for (y = 0; y < c->height; y++) {
  59. fprintf(stderr, "%s: %2d: |", prefix, y);
  60. for (x = 0; x < c->width; x++, data++) {
  61. if ((*data & 0xff000000) != 0xff000000) {
  62. fprintf(stderr, " "); /* transparent */
  63. } else if ((*data & 0x00ffffff) == 0x00ffffff) {
  64. fprintf(stderr, "."); /* white */
  65. } else if ((*data & 0x00ffffff) == 0x00000000) {
  66. fprintf(stderr, "X"); /* black */
  67. } else {
  68. fprintf(stderr, "o"); /* other */
  69. }
  70. }
  71. fprintf(stderr, "|\n");
  72. }
  73. }
  74. QEMUCursor *cursor_builtin_hidden(void)
  75. {
  76. return cursor_parse_xpm(cursor_hidden_xpm);
  77. }
  78. QEMUCursor *cursor_builtin_left_ptr(void)
  79. {
  80. return cursor_parse_xpm(cursor_left_ptr_xpm);
  81. }
  82. QEMUCursor *cursor_alloc(uint16_t width, uint16_t height)
  83. {
  84. QEMUCursor *c;
  85. size_t datasize = width * height * sizeof(uint32_t);
  86. /* Modern physical hardware typically uses 512x512 sprites */
  87. if (width > 512 || height > 512) {
  88. return NULL;
  89. }
  90. c = g_malloc0(sizeof(QEMUCursor) + datasize);
  91. c->width = width;
  92. c->height = height;
  93. c->refcount = 1;
  94. return c;
  95. }
  96. QEMUCursor *cursor_ref(QEMUCursor *c)
  97. {
  98. c->refcount++;
  99. return c;
  100. }
  101. void cursor_unref(QEMUCursor *c)
  102. {
  103. if (c == NULL)
  104. return;
  105. c->refcount--;
  106. if (c->refcount)
  107. return;
  108. g_free(c);
  109. }
  110. int cursor_get_mono_bpl(QEMUCursor *c)
  111. {
  112. return DIV_ROUND_UP(c->width, 8);
  113. }
  114. void cursor_set_mono(QEMUCursor *c,
  115. uint32_t foreground, uint32_t background, uint8_t *image,
  116. int transparent, uint8_t *mask)
  117. {
  118. uint32_t *data = c->data;
  119. uint8_t bit;
  120. int x,y,bpl;
  121. bool expand_bitmap_only = image == mask;
  122. bool has_inverted_colors = false;
  123. const uint32_t inverted = 0x80000000;
  124. /*
  125. * Converts a monochrome bitmap with XOR mask 'image' and AND mask 'mask':
  126. * https://docs.microsoft.com/en-us/windows-hardware/drivers/display/drawing-monochrome-pointers
  127. */
  128. bpl = cursor_get_mono_bpl(c);
  129. for (y = 0; y < c->height; y++) {
  130. bit = 0x80;
  131. for (x = 0; x < c->width; x++, data++) {
  132. if (transparent && mask[x/8] & bit) {
  133. if (!expand_bitmap_only && image[x / 8] & bit) {
  134. *data = inverted;
  135. has_inverted_colors = true;
  136. } else {
  137. *data = 0x00000000;
  138. }
  139. } else if (!transparent && !(mask[x/8] & bit)) {
  140. *data = 0x00000000;
  141. } else if (image[x/8] & bit) {
  142. *data = 0xff000000 | foreground;
  143. } else {
  144. *data = 0xff000000 | background;
  145. }
  146. bit >>= 1;
  147. if (bit == 0) {
  148. bit = 0x80;
  149. }
  150. }
  151. mask += bpl;
  152. image += bpl;
  153. }
  154. /*
  155. * If there are any pixels with inverted colors, create an outline (fill
  156. * transparent neighbors with the background color) and use the foreground
  157. * color as "inverted" color.
  158. */
  159. if (has_inverted_colors) {
  160. data = c->data;
  161. for (y = 0; y < c->height; y++) {
  162. for (x = 0; x < c->width; x++, data++) {
  163. if (*data == 0 /* transparent */ &&
  164. ((x > 0 && data[-1] == inverted) ||
  165. (x + 1 < c->width && data[1] == inverted) ||
  166. (y > 0 && data[-c->width] == inverted) ||
  167. (y + 1 < c->height && data[c->width] == inverted))) {
  168. *data = 0xff000000 | background;
  169. }
  170. }
  171. }
  172. data = c->data;
  173. for (x = 0; x < c->width * c->height; x++, data++) {
  174. if (*data == inverted) {
  175. *data = 0xff000000 | foreground;
  176. }
  177. }
  178. }
  179. }
  180. void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask)
  181. {
  182. uint32_t *data = c->data;
  183. uint8_t bit;
  184. int x,y,bpl;
  185. bpl = cursor_get_mono_bpl(c);
  186. memset(mask, 0, bpl * c->height);
  187. for (y = 0; y < c->height; y++) {
  188. bit = 0x80;
  189. for (x = 0; x < c->width; x++, data++) {
  190. if ((*data & 0x80000000) == 0x0) { /* Alpha < 0x80 (128) */
  191. if (transparent != 0) {
  192. mask[x/8] |= bit;
  193. }
  194. } else {
  195. if (transparent == 0) {
  196. mask[x/8] |= bit;
  197. }
  198. }
  199. bit >>= 1;
  200. if (bit == 0) {
  201. bit = 0x80;
  202. }
  203. }
  204. mask += bpl;
  205. }
  206. }