cursor.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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], "%d %d %d %d", &width, &height, &colors, &chars) != 4) {
  16. fprintf(stderr, "%s: header parse error: \"%s\"\n",
  17. __FUNCTION__, xpm[line]);
  18. return NULL;
  19. }
  20. if (chars != 1) {
  21. fprintf(stderr, "%s: chars != 1 not supported\n", __FUNCTION__);
  22. return NULL;
  23. }
  24. line++;
  25. /* parse color table */
  26. for (i = 0; i < colors; i++, line++) {
  27. if (sscanf(xpm[line], "%c c %15s", &idx, name) == 2) {
  28. if (sscanf(name, "#%02x%02x%02x", &r, &g, &b) == 3) {
  29. ctab[idx] = (0xff << 24) | (b << 16) | (g << 8) | r;
  30. continue;
  31. }
  32. if (strcmp(name, "None") == 0) {
  33. ctab[idx] = 0x00000000;
  34. continue;
  35. }
  36. }
  37. fprintf(stderr, "%s: color parse error: \"%s\"\n",
  38. __FUNCTION__, xpm[line]);
  39. return NULL;
  40. }
  41. /* parse pixel data */
  42. c = cursor_alloc(width, height);
  43. for (pixel = 0, y = 0; y < height; y++, line++) {
  44. for (x = 0; x < height; x++, pixel++) {
  45. idx = xpm[line][x];
  46. c->data[pixel] = ctab[idx];
  47. }
  48. }
  49. return c;
  50. }
  51. /* nice for debugging */
  52. void cursor_print_ascii_art(QEMUCursor *c, const char *prefix)
  53. {
  54. uint32_t *data = c->data;
  55. int x,y;
  56. for (y = 0; y < c->height; y++) {
  57. fprintf(stderr, "%s: %2d: |", prefix, y);
  58. for (x = 0; x < c->width; x++, data++) {
  59. if ((*data & 0xff000000) != 0xff000000) {
  60. fprintf(stderr, " "); /* transparent */
  61. } else if ((*data & 0x00ffffff) == 0x00ffffff) {
  62. fprintf(stderr, "."); /* white */
  63. } else if ((*data & 0x00ffffff) == 0x00000000) {
  64. fprintf(stderr, "X"); /* black */
  65. } else {
  66. fprintf(stderr, "o"); /* other */
  67. }
  68. }
  69. fprintf(stderr, "|\n");
  70. }
  71. }
  72. QEMUCursor *cursor_builtin_hidden(void)
  73. {
  74. QEMUCursor *c;
  75. c = cursor_parse_xpm(cursor_hidden_xpm);
  76. return c;
  77. }
  78. QEMUCursor *cursor_builtin_left_ptr(void)
  79. {
  80. QEMUCursor *c;
  81. c = cursor_parse_xpm(cursor_left_ptr_xpm);
  82. return c;
  83. }
  84. QEMUCursor *cursor_alloc(int width, int height)
  85. {
  86. QEMUCursor *c;
  87. int datasize = width * height * sizeof(uint32_t);
  88. c = qemu_mallocz(sizeof(QEMUCursor) + datasize);
  89. c->width = width;
  90. c->height = height;
  91. c->refcount = 1;
  92. return c;
  93. }
  94. void cursor_get(QEMUCursor *c)
  95. {
  96. c->refcount++;
  97. }
  98. void cursor_put(QEMUCursor *c)
  99. {
  100. if (c == NULL)
  101. return;
  102. c->refcount--;
  103. if (c->refcount)
  104. return;
  105. qemu_free(c);
  106. }
  107. int cursor_get_mono_bpl(QEMUCursor *c)
  108. {
  109. return (c->width + 7) / 8;
  110. }
  111. void cursor_set_mono(QEMUCursor *c,
  112. uint32_t foreground, uint32_t background, uint8_t *image,
  113. int transparent, uint8_t *mask)
  114. {
  115. uint32_t *data = c->data;
  116. uint8_t bit;
  117. int x,y,bpl;
  118. bpl = cursor_get_mono_bpl(c);
  119. for (y = 0; y < c->height; y++) {
  120. bit = 0x80;
  121. for (x = 0; x < c->width; x++, data++) {
  122. if (transparent && mask[x/8] & bit) {
  123. *data = 0x00000000;
  124. } else if (!transparent && !(mask[x/8] & bit)) {
  125. *data = 0x00000000;
  126. } else if (image[x/8] & bit) {
  127. *data = 0xff000000 | foreground;
  128. } else {
  129. *data = 0xff000000 | background;
  130. }
  131. bit >>= 1;
  132. if (bit == 0) {
  133. bit = 0x80;
  134. }
  135. }
  136. mask += bpl;
  137. image += bpl;
  138. }
  139. }
  140. void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *image)
  141. {
  142. uint32_t *data = c->data;
  143. uint8_t bit;
  144. int x,y,bpl;
  145. bpl = cursor_get_mono_bpl(c);
  146. memset(image, 0, bpl * c->height);
  147. for (y = 0; y < c->height; y++) {
  148. bit = 0x80;
  149. for (x = 0; x < c->width; x++, data++) {
  150. if (((*data & 0xff000000) == 0xff000000) &&
  151. ((*data & 0x00ffffff) == foreground)) {
  152. image[x/8] |= bit;
  153. }
  154. bit >>= 1;
  155. if (bit == 0) {
  156. bit = 0x80;
  157. }
  158. }
  159. image += bpl;
  160. }
  161. }
  162. void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask)
  163. {
  164. uint32_t *data = c->data;
  165. uint8_t bit;
  166. int x,y,bpl;
  167. bpl = cursor_get_mono_bpl(c);
  168. memset(mask, 0, bpl * c->height);
  169. for (y = 0; y < c->height; y++) {
  170. bit = 0x80;
  171. for (x = 0; x < c->width; x++, data++) {
  172. if ((*data & 0xff000000) != 0xff000000) {
  173. if (transparent != 0) {
  174. mask[x/8] |= bit;
  175. }
  176. } else {
  177. if (transparent == 0) {
  178. mask[x/8] |= bit;
  179. }
  180. }
  181. bit >>= 1;
  182. if (bit == 0) {
  183. bit = 0x80;
  184. }
  185. }
  186. mask += bpl;
  187. }
  188. }