2
0

cursor.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "qemu-common.h"
  2. #include "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. __FUNCTION__, xpm[line]);
  19. return NULL;
  20. }
  21. if (chars != 1) {
  22. fprintf(stderr, "%s: chars != 1 not supported\n", __FUNCTION__);
  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. __FUNCTION__, 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. QEMUCursor *c;
  76. c = cursor_parse_xpm(cursor_hidden_xpm);
  77. return c;
  78. }
  79. QEMUCursor *cursor_builtin_left_ptr(void)
  80. {
  81. QEMUCursor *c;
  82. c = cursor_parse_xpm(cursor_left_ptr_xpm);
  83. return c;
  84. }
  85. QEMUCursor *cursor_alloc(int width, int height)
  86. {
  87. QEMUCursor *c;
  88. int datasize = width * height * sizeof(uint32_t);
  89. c = g_malloc0(sizeof(QEMUCursor) + datasize);
  90. c->width = width;
  91. c->height = height;
  92. c->refcount = 1;
  93. return c;
  94. }
  95. void cursor_get(QEMUCursor *c)
  96. {
  97. c->refcount++;
  98. }
  99. void cursor_put(QEMUCursor *c)
  100. {
  101. if (c == NULL)
  102. return;
  103. c->refcount--;
  104. if (c->refcount)
  105. return;
  106. g_free(c);
  107. }
  108. int cursor_get_mono_bpl(QEMUCursor *c)
  109. {
  110. return (c->width + 7) / 8;
  111. }
  112. void cursor_set_mono(QEMUCursor *c,
  113. uint32_t foreground, uint32_t background, uint8_t *image,
  114. int transparent, uint8_t *mask)
  115. {
  116. uint32_t *data = c->data;
  117. uint8_t bit;
  118. int x,y,bpl;
  119. bpl = cursor_get_mono_bpl(c);
  120. for (y = 0; y < c->height; y++) {
  121. bit = 0x80;
  122. for (x = 0; x < c->width; x++, data++) {
  123. if (transparent && mask[x/8] & bit) {
  124. *data = 0x00000000;
  125. } else if (!transparent && !(mask[x/8] & bit)) {
  126. *data = 0x00000000;
  127. } else if (image[x/8] & bit) {
  128. *data = 0xff000000 | foreground;
  129. } else {
  130. *data = 0xff000000 | background;
  131. }
  132. bit >>= 1;
  133. if (bit == 0) {
  134. bit = 0x80;
  135. }
  136. }
  137. mask += bpl;
  138. image += bpl;
  139. }
  140. }
  141. void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *image)
  142. {
  143. uint32_t *data = c->data;
  144. uint8_t bit;
  145. int x,y,bpl;
  146. bpl = cursor_get_mono_bpl(c);
  147. memset(image, 0, bpl * c->height);
  148. for (y = 0; y < c->height; y++) {
  149. bit = 0x80;
  150. for (x = 0; x < c->width; x++, data++) {
  151. if (((*data & 0xff000000) == 0xff000000) &&
  152. ((*data & 0x00ffffff) == foreground)) {
  153. image[x/8] |= bit;
  154. }
  155. bit >>= 1;
  156. if (bit == 0) {
  157. bit = 0x80;
  158. }
  159. }
  160. image += bpl;
  161. }
  162. }
  163. void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask)
  164. {
  165. uint32_t *data = c->data;
  166. uint8_t bit;
  167. int x,y,bpl;
  168. bpl = cursor_get_mono_bpl(c);
  169. memset(mask, 0, bpl * c->height);
  170. for (y = 0; y < c->height; y++) {
  171. bit = 0x80;
  172. for (x = 0; x < c->width; x++, data++) {
  173. if ((*data & 0xff000000) != 0xff000000) {
  174. if (transparent != 0) {
  175. mask[x/8] |= bit;
  176. }
  177. } else {
  178. if (transparent == 0) {
  179. mask[x/8] |= bit;
  180. }
  181. }
  182. bit >>= 1;
  183. if (bit == 0) {
  184. bit = 0x80;
  185. }
  186. }
  187. mask += bpl;
  188. }
  189. }