source: mainline/uspace/srv/hid/display/cursor.c@ 4d8002d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4d8002d was 4d8002d, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Bitmapped mouse cursor

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup display
30 * @{
31 */
32/**
33 * @file Display server cursor
34 */
35
36#include <gfx/bitmap.h>
37#include <gfx/coord.h>
38#include <stdlib.h>
39#include "cursor.h"
40#include "display.h"
41
42/** Create cursor.
43 *
44 * @param disp Display
45 * @param rect Rectangle bounding cursor graphic
46 * @param image Cursor image (one entire byte per pixel)
47 * @param rcursor Place to store pointer to new cursor.
48 *
49 * @return EOK on success or an error code
50 */
51errno_t ds_cursor_create(ds_display_t *disp, gfx_rect_t *rect,
52 const uint8_t *image, ds_cursor_t **rcursor)
53{
54 ds_cursor_t *cursor = NULL;
55 errno_t rc;
56
57 cursor = calloc(1, sizeof(ds_cursor_t));
58 if (cursor == NULL) {
59 rc = ENOMEM;
60 goto error;
61 }
62
63 ds_display_add_cursor(disp, cursor);
64
65 cursor->rect = *rect;
66 cursor->image = image;
67 *rcursor = cursor;
68 return EOK;
69error:
70 return rc;
71}
72
73/** Destroy cusror.
74 *
75 * @param cursor Cursor
76 */
77void ds_cursor_destroy(ds_cursor_t *cursor)
78{
79 list_remove(&cursor->ldisplay);
80
81 if (cursor->bitmap != NULL)
82 gfx_bitmap_destroy(cursor->bitmap);
83
84 free(cursor);
85}
86
87/** Paint cusor.
88 *
89 * @param cursor Cusor to paint
90 * @param pos Position to paint at
91 * @return EOK on success or an error code
92 */
93errno_t ds_cursor_paint(ds_cursor_t *cursor, gfx_coord2_t *pos)
94{
95 gfx_context_t *dgc;
96 gfx_coord_t x, y;
97 gfx_bitmap_params_t bparams;
98 gfx_bitmap_alloc_t alloc;
99 gfx_coord2_t dims;
100 pixelmap_t pixelmap;
101 pixel_t pixel;
102 const uint8_t *pp;
103 errno_t rc;
104
105 dgc = ds_display_get_gc(cursor->display); // XXX
106
107 gfx_bitmap_params_init(&bparams);
108 bparams.rect = cursor->rect;
109
110 if (cursor->bitmap == NULL) {
111 rc = gfx_bitmap_create(dgc, &bparams, NULL, &cursor->bitmap);
112 if (rc != EOK)
113 goto error;
114
115 rc = gfx_bitmap_get_alloc(cursor->bitmap, &alloc);
116 if (rc != EOK) {
117 gfx_bitmap_destroy(cursor->bitmap);
118 cursor->bitmap = NULL;
119 goto error;
120 }
121
122 gfx_rect_dims(&cursor->rect, &dims);
123 pixelmap.width = dims.x;
124 pixelmap.height = dims.y;
125 pixelmap.data = alloc.pixels;
126
127 pp = cursor->image;
128 for (y = 0; y < dims.y; y++) {
129 for (x = 0; x < dims.x; x++) {
130 switch (*pp) {
131 case 1:
132 pixel = PIXEL(0, 0, 0, 0);
133 break;
134 case 2:
135 pixel = PIXEL(0, 255, 255, 255);
136 break;
137 default:
138 pixel = PIXEL(0, 0, 255, 255);
139 break;
140 }
141
142 pixelmap_put_pixel(&pixelmap, x, y, pixel);
143 ++pp;
144 }
145 }
146
147 return EOK;
148 }
149
150 return gfx_bitmap_render(cursor->bitmap, NULL, pos);
151error:
152 return rc;
153}
154
155/** Get rectangle covered by cursor when drawn at a specified position.
156 *
157 * @param cursor Cursor
158 * @param pos Position where cursor is situated
159 * @param rect Place to store rectangle covered by cursor
160 */
161void ds_cursor_get_rect(ds_cursor_t *cursor, gfx_coord2_t *pos,
162 gfx_rect_t *drect)
163{
164 gfx_rect_translate(pos, &cursor->rect, drect);
165}
166
167/** @}
168 */
Note: See TracBrowser for help on using the repository browser.