2
0

cursor.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "qemu/osdep.h"
  2. #include "qemu-common.h"
  3. #include "ui/console.h"
  4. #include "cursor_hidden.xpm"
  5. #include "cursor_left_ptr.xpm"
  6. /* for creating built-in cursors */
  7. static QEMUCursor *cursor_parse_xpm(const char *xpm[])
  8. {
  9. QEMUCursor *c;
  10. uint32_t ctab[128];
  11. unsigned int width, height, colors, chars;
  12. unsigned int line = 0, i, r, g, b, x, y, pixel;
  13. char name[16];
  14. uint8_t idx;
  15. /* parse header line: width, height, #colors, #chars */
  16. if (sscanf(xpm[line], "%u %u %u %u",
  17. &width, &height, &colors, &chars) != 4) {
  18. fprintf(stderr, "%s: header parse error: \"%s\"\n",
  19. __FUNCTION__, xpm[line]);
  20. return NULL;
  21. }
  22. if (chars != 1) {
  23. fprintf(stderr, "%s: chars != 1 not supported\n", __FUNCTION__);
  24. return NULL;
  25. }
  26. line++;
  27. /* parse color table */
  28. for (i = 0; i < colors; i++, line++) {
  29. if (sscanf(xpm[line], "%c c %15s", &idx, name) == 2) {
  30. if (sscanf(name, "#%02x%02x%02x", &r, &g, &b) == 3) {
  31. ctab[idx] = (0xff << 24) | (b << 16) | (g << 8) | r;
  32. continue;
  33. }
  34. if (strcmp(name, "None") == 0) {
  35. ctab[idx] = 0x00000000;
  36. continue;
  37. }
  38. }
  39. fprintf(stderr, "%s: color parse error: \"%s\"\n",
  40. __FUNCTION__, xpm[line]);
  41. return NULL;
  42. }
  43. /* parse pixel data */
  44. c = cursor_alloc(width, height);
  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. int datasize = width * height * sizeof(uint32_t);
  86. c = g_malloc0(sizeof(QEMUCursor) + datasize);
  87. c->width = width;
  88. c->height = height;
  89. c->refcount = 1;
  90. return c;
  91. }
  92. void cursor_get(QEMUCursor *c)
  93. {
  94. c->refcount++;
  95. }
  96. void cursor_put(QEMUCursor *c)
  97. {
  98. if (c == NULL)
  99. return;
  100. c->refcount--;
  101. if (c->refcount)
  102. return;
  103. g_free(c);
  104. }
  105. int cursor_get_mono_bpl(QEMUCursor *c)
  106. {
  107. return (c->width + 7) / 8;
  108. }
  109. void cursor_set_mono(QEMUCursor *c,
  110. uint32_t foreground, uint32_t background, uint8_t *image,
  111. int transparent, uint8_t *mask)
  112. {
  113. uint32_t *data = c->data;
  114. uint8_t bit;
  115. int x,y,bpl;
  116. bpl = cursor_get_mono_bpl(c);
  117. for (y = 0; y < c->height; y++) {
  118. bit = 0x80;
  119. for (x = 0; x < c->width; x++, data++) {
  120. if (transparent && mask[x/8] & bit) {
  121. *data = 0x00000000;
  122. } else if (!transparent && !(mask[x/8] & bit)) {
  123. *data = 0x00000000;
  124. } else if (image[x/8] & bit) {
  125. *data = 0xff000000 | foreground;
  126. } else {
  127. *data = 0xff000000 | background;
  128. }
  129. bit >>= 1;
  130. if (bit == 0) {
  131. bit = 0x80;
  132. }
  133. }
  134. mask += bpl;
  135. image += bpl;
  136. }
  137. }
  138. void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *image)
  139. {
  140. uint32_t *data = c->data;
  141. uint8_t bit;
  142. int x,y,bpl;
  143. bpl = cursor_get_mono_bpl(c);
  144. memset(image, 0, bpl * c->height);
  145. for (y = 0; y < c->height; y++) {
  146. bit = 0x80;
  147. for (x = 0; x < c->width; x++, data++) {
  148. if (((*data & 0xff000000) == 0xff000000) &&
  149. ((*data & 0x00ffffff) == foreground)) {
  150. image[x/8] |= bit;
  151. }
  152. bit >>= 1;
  153. if (bit == 0) {
  154. bit = 0x80;
  155. }
  156. }
  157. image += bpl;
  158. }
  159. }
  160. void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask)
  161. {
  162. uint32_t *data = c->data;
  163. uint8_t bit;
  164. int x,y,bpl;
  165. bpl = cursor_get_mono_bpl(c);
  166. memset(mask, 0, bpl * c->height);
  167. for (y = 0; y < c->height; y++) {
  168. bit = 0x80;
  169. for (x = 0; x < c->width; x++, data++) {
  170. if ((*data & 0xff000000) != 0xff000000) {
  171. if (transparent != 0) {
  172. mask[x/8] |= bit;
  173. }
  174. } else {
  175. if (transparent == 0) {
  176. mask[x/8] |= bit;
  177. }
  178. }
  179. bit >>= 1;
  180. if (bit == 0) {
  181. bit = 0x80;
  182. }
  183. }
  184. mask += bpl;
  185. }
  186. }