HelenOS sources
This source file includes following definitions.
- ds_cursor_create
- ds_cursor_destroy
- ds_cursor_paint
- ds_cursor_get_rect
#include <gfx/bitmap.h>
#include <gfx/coord.h>
#include <stdlib.h>
#include "cursor.h"
#include "display.h"
errno_t ds_cursor_create(ds_display_t *disp, gfx_rect_t *rect,
const uint8_t *image, ds_cursor_t **rcursor)
{
ds_cursor_t *cursor = NULL;
errno_t rc;
cursor = calloc(1, sizeof(ds_cursor_t));
if (cursor == NULL) {
rc = ENOMEM;
goto error;
}
ds_display_add_cursor(disp, cursor);
cursor->rect = *rect;
cursor->image = image;
*rcursor = cursor;
return EOK;
error:
return rc;
}
void ds_cursor_destroy(ds_cursor_t *cursor)
{
list_remove(&cursor->ldisplay);
if (cursor->bitmap != NULL)
gfx_bitmap_destroy(cursor->bitmap);
free(cursor);
}
errno_t ds_cursor_paint(ds_cursor_t *cursor, gfx_coord2_t *pos,
gfx_rect_t *clip)
{
gfx_context_t *dgc;
gfx_coord_t x, y;
gfx_bitmap_params_t bparams;
gfx_bitmap_alloc_t alloc;
gfx_coord2_t dims;
gfx_rect_t sclip;
gfx_rect_t srect;
pixelmap_t pixelmap;
pixel_t pixel;
const uint8_t *pp;
errno_t rc;
dgc = ds_display_get_gc(cursor->display);
if (dgc == NULL)
return EOK;
gfx_bitmap_params_init(&bparams);
bparams.rect = cursor->rect;
bparams.flags = bmpf_color_key;
bparams.key_color = PIXEL(0, 0, 255, 255);
if (cursor->bitmap == NULL) {
rc = gfx_bitmap_create(dgc, &bparams, NULL, &cursor->bitmap);
if (rc != EOK)
goto error;
rc = gfx_bitmap_get_alloc(cursor->bitmap, &alloc);
if (rc != EOK) {
gfx_bitmap_destroy(cursor->bitmap);
cursor->bitmap = NULL;
goto error;
}
gfx_rect_dims(&cursor->rect, &dims);
pixelmap.width = dims.x;
pixelmap.height = dims.y;
pixelmap.data = alloc.pixels;
pp = cursor->image;
for (y = 0; y < dims.y; y++) {
for (x = 0; x < dims.x; x++) {
switch (*pp) {
case 1:
pixel = PIXEL(0, 0, 0, 0);
break;
case 2:
pixel = PIXEL(0, 255, 255, 255);
break;
default:
pixel = PIXEL(0, 0, 255, 255);
break;
}
pixelmap_put_pixel(&pixelmap, x, y, pixel);
++pp;
}
}
}
if (clip == NULL) {
srect = cursor->rect;
} else {
gfx_rect_rtranslate(pos, clip, &sclip);
gfx_rect_clip(&cursor->rect, &sclip, &srect);
}
if (!gfx_rect_is_empty(&srect)) {
rc = gfx_bitmap_render(cursor->bitmap, &srect, pos);
if (rc != EOK)
return rc;
}
return EOK;
error:
return rc;
}
void ds_cursor_get_rect(ds_cursor_t *cursor, gfx_coord2_t *pos,
gfx_rect_t *drect)
{
gfx_rect_translate(pos, &cursor->rect, drect);
}
HelenOS homepage, sources at GitHub