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 | */
|
---|
51 | errno_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;
|
---|
69 | error:
|
---|
70 | return rc;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Destroy cusror.
|
---|
74 | *
|
---|
75 | * @param cursor Cursor
|
---|
76 | */
|
---|
77 | void 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 | */
|
---|
93 | errno_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 | if (dgc == NULL)
|
---|
107 | return EOK;
|
---|
108 |
|
---|
109 | gfx_bitmap_params_init(&bparams);
|
---|
110 | bparams.rect = cursor->rect;
|
---|
111 | bparams.flags = bmpf_color_key;
|
---|
112 | bparams.key_color = PIXEL(0, 0, 255, 255);
|
---|
113 |
|
---|
114 | if (cursor->bitmap == NULL) {
|
---|
115 | rc = gfx_bitmap_create(dgc, &bparams, NULL, &cursor->bitmap);
|
---|
116 | if (rc != EOK)
|
---|
117 | goto error;
|
---|
118 |
|
---|
119 | rc = gfx_bitmap_get_alloc(cursor->bitmap, &alloc);
|
---|
120 | if (rc != EOK) {
|
---|
121 | gfx_bitmap_destroy(cursor->bitmap);
|
---|
122 | cursor->bitmap = NULL;
|
---|
123 | goto error;
|
---|
124 | }
|
---|
125 |
|
---|
126 | gfx_rect_dims(&cursor->rect, &dims);
|
---|
127 | pixelmap.width = dims.x;
|
---|
128 | pixelmap.height = dims.y;
|
---|
129 | pixelmap.data = alloc.pixels;
|
---|
130 |
|
---|
131 | pp = cursor->image;
|
---|
132 | for (y = 0; y < dims.y; y++) {
|
---|
133 | for (x = 0; x < dims.x; x++) {
|
---|
134 | switch (*pp) {
|
---|
135 | case 1:
|
---|
136 | pixel = PIXEL(0, 0, 0, 0);
|
---|
137 | break;
|
---|
138 | case 2:
|
---|
139 | pixel = PIXEL(0, 255, 255, 255);
|
---|
140 | break;
|
---|
141 | default:
|
---|
142 | pixel = PIXEL(0, 0, 255, 255);
|
---|
143 | break;
|
---|
144 | }
|
---|
145 |
|
---|
146 | pixelmap_put_pixel(&pixelmap, x, y, pixel);
|
---|
147 | ++pp;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return gfx_bitmap_render(cursor->bitmap, NULL, pos);
|
---|
153 | error:
|
---|
154 | return rc;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Get rectangle covered by cursor when drawn at a specified position.
|
---|
158 | *
|
---|
159 | * @param cursor Cursor
|
---|
160 | * @param pos Position where cursor is situated
|
---|
161 | * @param rect Place to store rectangle covered by cursor
|
---|
162 | */
|
---|
163 | void ds_cursor_get_rect(ds_cursor_t *cursor, gfx_coord2_t *pos,
|
---|
164 | gfx_rect_t *drect)
|
---|
165 | {
|
---|
166 | gfx_rect_translate(pos, &cursor->rect, drect);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** @}
|
---|
170 | */
|
---|