cursor.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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(int width, int height)
  83. {
  84. QEMUCursor *c;
  85. size_t datasize = width * height * sizeof(uint32_t);
  86. if (width > 512 || height > 512) {
  87. return NULL;
  88. }
  89. c = g_malloc0(sizeof(QEMUCursor) + datasize);
  90. c->width = width;
  91. c->height = height;
  92. c->refcount = 1;
  93. return c;
  94. }
  95. QEMUCursor *cursor_ref(QEMUCursor *c)
  96. {
  97. c->refcount++;
  98. return c;
  99. }
  100. void cursor_unref(QEMUCursor *c)
  101. {
  102. if (c == NULL)
  103. return;
  104. c->refcount--;
  105. if (c->refcount)
  106. return;
  107. g_free(c);
  108. }
  109. int cursor_get_mono_bpl(QEMUCursor *c)
  110. {
  111. return DIV_ROUND_UP(c->width, 8);
  112. }
  113. void cursor_set_mono(QEMUCursor *c,
  114. uint32_t foreground, uint32_t background, uint8_t *image,
  115. int transparent, uint8_t *mask)
  116. {
  117. uint32_t *data = c->data;
  118. uint8_t bit;
  119. int x,y,bpl;
  120. bool expand_bitmap_only = image == mask;
  121. bool has_inverted_colors = false;
  122. const uint32_t inverted = 0x80000000;
  123. /*
  124. * Converts a monochrome bitmap with XOR mask 'image' and AND mask 'mask':
  125. * https://docs.microsoft.com/en-us/windows-hardware/drivers/display/drawing-monochrome-pointers
  126. */
  127. bpl = cursor_get_mono_bpl(c);
  128. for (y = 0; y < c->height; y++) {
  129. bit = 0x80;
  130. for (x = 0; x < c->width; x++, data++) {
  131. if (transparent && mask[x/8] & bit) {
  132. if (!expand_bitmap_only && image[x / 8] & bit) {
  133. *data = inverted;
  134. has_inverted_colors = true;
  135. } else {
  136. *data = 0x00000000;
  137. }
  138. } else if (!transparent && !(mask[x/8] & bit)) {
  139. *data = 0x00000000;
  140. } else if (image[x/8] & bit) {
  141. *data = 0xff000000 | foreground;
  142. } else {
  143. *data = 0xff000000 | background;
  144. }
  145. bit >>= 1;
  146. if (bit == 0) {
  147. bit = 0x80;
  148. }
  149. }
  150. mask += bpl;
  151. image += bpl;
  152. }
  153. /*
  154. * If there are any pixels with inverted colors, create an outline (fill
  155. * transparent neighbors with the background color) and use the foreground
  156. * color as "inverted" color.
  157. */
  158. if (has_inverted_colors) {
  159. data = c->data;
  160. for (y = 0; y < c->height; y++) {
  161. for (x = 0; x < c->width; x++, data++) {
  162. if (*data == 0 /* transparent */ &&
  163. ((x > 0 && data[-1] == inverted) ||
  164. (x + 1 < c->width && data[1] == inverted) ||
  165. (y > 0 && data[-c->width] == inverted) ||
  166. (y + 1 < c->height && data[c->width] == inverted))) {
  167. *data = 0xff000000 | background;
  168. }
  169. }
  170. }
  171. data = c->data;
  172. for (x = 0; x < c->width * c->height; x++, data++) {
  173. if (*data == inverted) {
  174. *data = 0xff000000 | foreground;
  175. }
  176. }
  177. }
  178. }
  179. void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *image)
  180. {
  181. uint32_t *data = c->data;
  182. uint8_t bit;
  183. int x,y,bpl;
  184. bpl = cursor_get_mono_bpl(c);
  185. memset(image, 0, bpl * c->height);
  186. for (y = 0; y < c->height; y++) {
  187. bit = 0x80;
  188. for (x = 0; x < c->width; x++, data++) {
  189. if (((*data & 0xff000000) == 0xff000000) &&
  190. ((*data & 0x00ffffff) == foreground)) {
  191. image[x/8] |= bit;
  192. }
  193. bit >>= 1;
  194. if (bit == 0) {
  195. bit = 0x80;
  196. }
  197. }
  198. image += bpl;
  199. }
  200. }
  201. void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask)
  202. {
  203. uint32_t *data = c->data;
  204. uint8_t bit;
  205. int x,y,bpl;
  206. bpl = cursor_get_mono_bpl(c);
  207. memset(mask, 0, bpl * c->height);
  208. for (y = 0; y < c->height; y++) {
  209. bit = 0x80;
  210. for (x = 0; x < c->width; x++, data++) {
  211. if ((*data & 0xff000000) != 0xff000000) {
  212. if (transparent != 0) {
  213. mask[x/8] |= bit;
  214. }
  215. } else {
  216. if (transparent == 0) {
  217. mask[x/8] |= bit;
  218. }
  219. }
  220. bit >>= 1;
  221. if (bit == 0) {
  222. bit = 0x80;
  223. }
  224. }
  225. mask += bpl;
  226. }
  227. }