qemu/ui/cursor.c
<<
>>
Prefs
   1#include "qemu/osdep.h"
   2#include "ui/console.h"
   3
   4#include "cursor_hidden.xpm"
   5#include "cursor_left_ptr.xpm"
   6
   7/* for creating built-in cursors */
   8static QEMUCursor *cursor_parse_xpm(const char *xpm[])
   9{
  10    QEMUCursor *c;
  11    uint32_t ctab[128];
  12    unsigned int width, height, colors, chars;
  13    unsigned int line = 0, i, r, g, b, x, y, pixel;
  14    char name[16];
  15    uint8_t idx;
  16
  17    /* parse header line: width, height, #colors, #chars */
  18    if (sscanf(xpm[line], "%u %u %u %u",
  19               &width, &height, &colors, &chars) != 4) {
  20        fprintf(stderr, "%s: header parse error: \"%s\"\n",
  21                __func__, xpm[line]);
  22        return NULL;
  23    }
  24    if (chars != 1) {
  25        fprintf(stderr, "%s: chars != 1 not supported\n", __func__);
  26        return NULL;
  27    }
  28    line++;
  29
  30    /* parse color table */
  31    for (i = 0; i < colors; i++, line++) {
  32        if (sscanf(xpm[line], "%c c %15s", &idx, name) == 2) {
  33            if (sscanf(name, "#%02x%02x%02x", &r, &g, &b) == 3) {
  34                ctab[idx] = (0xff << 24) | (b << 16) | (g << 8) | r;
  35                continue;
  36            }
  37            if (strcmp(name, "None") == 0) {
  38                ctab[idx] = 0x00000000;
  39                continue;
  40            }
  41        }
  42        fprintf(stderr, "%s: color parse error: \"%s\"\n",
  43                __func__, xpm[line]);
  44        return NULL;
  45    }
  46
  47    /* parse pixel data */
  48    c = cursor_alloc(width, height);
  49    assert(c != NULL);
  50
  51    for (pixel = 0, y = 0; y < height; y++, line++) {
  52        for (x = 0; x < height; x++, pixel++) {
  53            idx = xpm[line][x];
  54            c->data[pixel] = ctab[idx];
  55        }
  56    }
  57    return c;
  58}
  59
  60/* nice for debugging */
  61void cursor_print_ascii_art(QEMUCursor *c, const char *prefix)
  62{
  63    uint32_t *data = c->data;
  64    int x,y;
  65
  66    for (y = 0; y < c->height; y++) {
  67        fprintf(stderr, "%s: %2d: |", prefix, y);
  68        for (x = 0; x < c->width; x++, data++) {
  69            if ((*data & 0xff000000) != 0xff000000) {
  70                fprintf(stderr, " "); /* transparent */
  71            } else if ((*data & 0x00ffffff) == 0x00ffffff) {
  72                fprintf(stderr, "."); /* white */
  73            } else if ((*data & 0x00ffffff) == 0x00000000) {
  74                fprintf(stderr, "X"); /* black */
  75            } else {
  76                fprintf(stderr, "o"); /* other */
  77            }
  78        }
  79        fprintf(stderr, "|\n");
  80    }
  81}
  82
  83QEMUCursor *cursor_builtin_hidden(void)
  84{
  85    return cursor_parse_xpm(cursor_hidden_xpm);
  86}
  87
  88QEMUCursor *cursor_builtin_left_ptr(void)
  89{
  90    return cursor_parse_xpm(cursor_left_ptr_xpm);
  91}
  92
  93QEMUCursor *cursor_alloc(int width, int height)
  94{
  95    QEMUCursor *c;
  96    size_t datasize = width * height * sizeof(uint32_t);
  97
  98    if (width > 512 || height > 512) {
  99        return NULL;
 100    }
 101
 102    c = g_malloc0(sizeof(QEMUCursor) + datasize);
 103    c->width  = width;
 104    c->height = height;
 105    c->refcount = 1;
 106    return c;
 107}
 108
 109QEMUCursor *cursor_ref(QEMUCursor *c)
 110{
 111    c->refcount++;
 112    return c;
 113}
 114
 115void cursor_unref(QEMUCursor *c)
 116{
 117    if (c == NULL)
 118        return;
 119    c->refcount--;
 120    if (c->refcount)
 121        return;
 122    g_free(c);
 123}
 124
 125int cursor_get_mono_bpl(QEMUCursor *c)
 126{
 127    return DIV_ROUND_UP(c->width, 8);
 128}
 129
 130void cursor_set_mono(QEMUCursor *c,
 131                     uint32_t foreground, uint32_t background, uint8_t *image,
 132                     int transparent, uint8_t *mask)
 133{
 134    uint32_t *data = c->data;
 135    uint8_t bit;
 136    int x,y,bpl;
 137    bool expand_bitmap_only = image == mask;
 138    bool has_inverted_colors = false;
 139    const uint32_t inverted = 0x80000000;
 140
 141    /*
 142     * Converts a monochrome bitmap with XOR mask 'image' and AND mask 'mask':
 143     * https://docs.microsoft.com/en-us/windows-hardware/drivers/display/drawing-monochrome-pointers
 144     */
 145    bpl = cursor_get_mono_bpl(c);
 146    for (y = 0; y < c->height; y++) {
 147        bit = 0x80;
 148        for (x = 0; x < c->width; x++, data++) {
 149            if (transparent && mask[x/8] & bit) {
 150                if (!expand_bitmap_only && image[x / 8] & bit) {
 151                    *data = inverted;
 152                    has_inverted_colors = true;
 153                } else {
 154                    *data = 0x00000000;
 155                }
 156            } else if (!transparent && !(mask[x/8] & bit)) {
 157                *data = 0x00000000;
 158            } else if (image[x/8] & bit) {
 159                *data = 0xff000000 | foreground;
 160            } else {
 161                *data = 0xff000000 | background;
 162            }
 163            bit >>= 1;
 164            if (bit == 0) {
 165                bit = 0x80;
 166            }
 167        }
 168        mask  += bpl;
 169        image += bpl;
 170    }
 171
 172    /*
 173     * If there are any pixels with inverted colors, create an outline (fill
 174     * transparent neighbors with the background color) and use the foreground
 175     * color as "inverted" color.
 176     */
 177    if (has_inverted_colors) {
 178        data = c->data;
 179        for (y = 0; y < c->height; y++) {
 180            for (x = 0; x < c->width; x++, data++) {
 181                if (*data == 0 /* transparent */ &&
 182                        ((x > 0 && data[-1] == inverted) ||
 183                         (x + 1 < c->width && data[1] == inverted) ||
 184                         (y > 0 && data[-c->width] == inverted) ||
 185                         (y + 1 < c->height && data[c->width] == inverted))) {
 186                    *data = 0xff000000 | background;
 187                }
 188            }
 189        }
 190        data = c->data;
 191        for (x = 0; x < c->width * c->height; x++, data++) {
 192            if (*data == inverted) {
 193                *data = 0xff000000 | foreground;
 194            }
 195        }
 196    }
 197}
 198
 199void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *image)
 200{
 201    uint32_t *data = c->data;
 202    uint8_t bit;
 203    int x,y,bpl;
 204
 205    bpl = cursor_get_mono_bpl(c);
 206    memset(image, 0, bpl * c->height);
 207    for (y = 0; y < c->height; y++) {
 208        bit = 0x80;
 209        for (x = 0; x < c->width; x++, data++) {
 210            if (((*data & 0xff000000) == 0xff000000) &&
 211                ((*data & 0x00ffffff) == foreground)) {
 212                image[x/8] |= bit;
 213            }
 214            bit >>= 1;
 215            if (bit == 0) {
 216                bit = 0x80;
 217            }
 218        }
 219        image += bpl;
 220    }
 221}
 222
 223void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask)
 224{
 225    uint32_t *data = c->data;
 226    uint8_t bit;
 227    int x,y,bpl;
 228
 229    bpl = cursor_get_mono_bpl(c);
 230    memset(mask, 0, bpl * c->height);
 231    for (y = 0; y < c->height; y++) {
 232        bit = 0x80;
 233        for (x = 0; x < c->width; x++, data++) {
 234            if ((*data & 0xff000000) != 0xff000000) {
 235                if (transparent != 0) {
 236                    mask[x/8] |= bit;
 237                }
 238            } else {
 239                if (transparent == 0) {
 240                    mask[x/8] |= bit;
 241                }
 242            }
 243            bit >>= 1;
 244            if (bit == 0) {
 245                bit = 0x80;
 246            }
 247        }
 248        mask += bpl;
 249    }
 250}
 251