1 | /*
|
---|
2 | * Copyright (c) 2019 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 seat
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <adt/list.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <gfx/color.h>
|
---|
39 | #include <gfx/render.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include "client.h"
|
---|
42 | #include "display.h"
|
---|
43 | #include "seat.h"
|
---|
44 | #include "window.h"
|
---|
45 |
|
---|
46 | /** Create seat.
|
---|
47 | *
|
---|
48 | * @param display Parent display
|
---|
49 | * @param rseat Place to store pointer to new seat.
|
---|
50 | * @return EOK on success, ENOMEM if out of memory
|
---|
51 | */
|
---|
52 | errno_t ds_seat_create(ds_display_t *display, ds_seat_t **rseat)
|
---|
53 | {
|
---|
54 | ds_seat_t *seat;
|
---|
55 |
|
---|
56 | seat = calloc(1, sizeof(ds_seat_t));
|
---|
57 | if (seat == NULL)
|
---|
58 | return ENOMEM;
|
---|
59 |
|
---|
60 | ds_display_add_seat(display, seat);
|
---|
61 | seat->pntpos.x = 0;
|
---|
62 | seat->pntpos.y = 0;
|
---|
63 |
|
---|
64 | *rseat = seat;
|
---|
65 | return EOK;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Destroy seat.
|
---|
69 | *
|
---|
70 | * @param seat Seat
|
---|
71 | */
|
---|
72 | void ds_seat_destroy(ds_seat_t *seat)
|
---|
73 | {
|
---|
74 | ds_display_remove_seat(seat);
|
---|
75 | free(seat);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Set seat focus to a window.
|
---|
79 | *
|
---|
80 | * @param seat Seat
|
---|
81 | * @param wnd Window to focus
|
---|
82 | */
|
---|
83 | void ds_seat_set_focus(ds_seat_t *seat, ds_window_t *wnd)
|
---|
84 | {
|
---|
85 | seat->focus = wnd;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Evacuate focus from window.
|
---|
89 | *
|
---|
90 | * If seat's focus is @a wnd, it will be set to a different window.
|
---|
91 | *
|
---|
92 | * @param seat Seat
|
---|
93 | * @param wnd Window to evacuate focus from
|
---|
94 | */
|
---|
95 | void ds_seat_evac_focus(ds_seat_t *seat, ds_window_t *wnd)
|
---|
96 | {
|
---|
97 | ds_window_t *nwnd;
|
---|
98 |
|
---|
99 | if (seat->focus == wnd) {
|
---|
100 | nwnd = ds_display_next_window(wnd);
|
---|
101 | if (nwnd == NULL)
|
---|
102 | nwnd = ds_display_first_window(wnd->display);
|
---|
103 | if (nwnd == wnd)
|
---|
104 | nwnd = NULL;
|
---|
105 |
|
---|
106 | ds_seat_set_focus(seat, nwnd);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Post keyboard event to the seat's focused window.
|
---|
111 | *
|
---|
112 | * @param seat Seat
|
---|
113 | * @param event Event
|
---|
114 | *
|
---|
115 | * @return EOK on success or an error code
|
---|
116 | */
|
---|
117 | errno_t ds_seat_post_kbd_event(ds_seat_t *seat, kbd_event_t *event)
|
---|
118 | {
|
---|
119 | ds_window_t *dwindow;
|
---|
120 | bool alt_or_shift;
|
---|
121 |
|
---|
122 | alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
|
---|
123 | if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_TAB) {
|
---|
124 | /* On Alt-Tab or Shift-Tab, switch focus to next window */
|
---|
125 | ds_seat_evac_focus(seat, seat->focus);
|
---|
126 | return EOK;
|
---|
127 | }
|
---|
128 |
|
---|
129 | dwindow = seat->focus;
|
---|
130 | if (dwindow == NULL)
|
---|
131 | return EOK;
|
---|
132 |
|
---|
133 | return ds_client_post_kbd_event(dwindow->client, dwindow, event);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Draw cross at seat pointer position.
|
---|
137 | *
|
---|
138 | * @param seat Seat
|
---|
139 | * @param len Cross length
|
---|
140 | * @param w Cross extra width
|
---|
141 | * @param br Brightness (0 to 65535)
|
---|
142 | *
|
---|
143 | * @return EOK on success or an error code
|
---|
144 | */
|
---|
145 | static errno_t ds_seat_draw_cross(ds_seat_t *seat, gfx_coord_t len,
|
---|
146 | gfx_coord_t w, uint16_t br)
|
---|
147 | {
|
---|
148 | gfx_color_t *color = NULL;
|
---|
149 | gfx_context_t *gc;
|
---|
150 | gfx_rect_t rect, r0;
|
---|
151 | errno_t rc;
|
---|
152 |
|
---|
153 | gc = ds_display_get_gc(seat->display);
|
---|
154 | if (gc == NULL)
|
---|
155 | return EOK;
|
---|
156 |
|
---|
157 | rc = gfx_color_new_rgb_i16(br, br, br, &color);
|
---|
158 | if (rc != EOK)
|
---|
159 | goto error;
|
---|
160 |
|
---|
161 | rc = gfx_set_color(gc, color);
|
---|
162 | if (rc != EOK)
|
---|
163 | goto error;
|
---|
164 |
|
---|
165 | r0.p0.x = -len;
|
---|
166 | r0.p0.y = -w;
|
---|
167 | r0.p1.x = +len + 1;
|
---|
168 | r0.p1.y = +w + 1;
|
---|
169 | gfx_rect_translate(&seat->pntpos, &r0, &rect);
|
---|
170 |
|
---|
171 | rc = gfx_fill_rect(gc, &rect);
|
---|
172 | if (rc != EOK)
|
---|
173 | goto error;
|
---|
174 |
|
---|
175 | r0.p0.x = -w;
|
---|
176 | r0.p0.y = -len;
|
---|
177 | r0.p1.x = +w + 1;
|
---|
178 | r0.p1.y = +len + 1;
|
---|
179 | gfx_rect_translate(&seat->pntpos, &r0, &rect);
|
---|
180 |
|
---|
181 | rc = gfx_fill_rect(gc, &rect);
|
---|
182 | if (rc != EOK)
|
---|
183 | goto error;
|
---|
184 |
|
---|
185 | gfx_color_delete(color);
|
---|
186 | return EOK;
|
---|
187 | error:
|
---|
188 | if (color != NULL)
|
---|
189 | gfx_color_delete(color);
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Draw or clear seat pointer
|
---|
194 | *
|
---|
195 | * @param seat Seat
|
---|
196 | * @param shown @c true to display pointer, @c false to clear it
|
---|
197 | *
|
---|
198 | * @return EOK on success or an error code
|
---|
199 | */
|
---|
200 | static errno_t ds_seat_draw_pointer(ds_seat_t *seat, bool shown)
|
---|
201 | {
|
---|
202 | errno_t rc;
|
---|
203 |
|
---|
204 | rc = ds_seat_draw_cross(seat, 8, 1, 0);
|
---|
205 | if (rc != EOK)
|
---|
206 | return rc;
|
---|
207 |
|
---|
208 | rc = ds_seat_draw_cross(seat, 8, 0, shown ? 65535 : 0);
|
---|
209 | if (rc != EOK)
|
---|
210 | return rc;
|
---|
211 |
|
---|
212 | return EOK;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** Post pointing device event to the seat
|
---|
216 | *
|
---|
217 | * Update pointer position and generate position event.
|
---|
218 | *
|
---|
219 | * @param seat Seat
|
---|
220 | * @param event Event
|
---|
221 | *
|
---|
222 | * @return EOK on success or an error code
|
---|
223 | */
|
---|
224 | #include <stdio.h>
|
---|
225 | errno_t ds_seat_post_ptd_event(ds_seat_t *seat, ptd_event_t *event)
|
---|
226 | {
|
---|
227 | gfx_coord2_t npos;
|
---|
228 | ds_window_t *wnd;
|
---|
229 | pos_event_t pevent;
|
---|
230 | errno_t rc;
|
---|
231 |
|
---|
232 | printf("ds_seat_post_ptd_event\n");
|
---|
233 | wnd = ds_display_window_by_pos(seat->display, &seat->pntpos);
|
---|
234 |
|
---|
235 | /* Focus window on button press */
|
---|
236 | if (event->type == PTD_PRESS && event->btn_num == 1) {
|
---|
237 | printf("PTD_PRESS (button = %d)\n", event->btn_num);
|
---|
238 | if (wnd != NULL) {
|
---|
239 | printf("set focus\n");
|
---|
240 | ds_seat_set_focus(seat, wnd);
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (event->type == PTD_PRESS || event->type == PTD_RELEASE) {
|
---|
245 | pevent.pos_id = 0;
|
---|
246 | pevent.type = (event->type == PTD_PRESS) ?
|
---|
247 | POS_PRESS : POS_RELEASE;
|
---|
248 | pevent.btn_num = event->btn_num;
|
---|
249 | pevent.hpos = seat->pntpos.x;
|
---|
250 | pevent.vpos = seat->pntpos.y;
|
---|
251 |
|
---|
252 | rc = ds_seat_post_pos_event(seat, &pevent);
|
---|
253 | if (rc != EOK)
|
---|
254 | return rc;
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (event->type == PTD_MOVE) {
|
---|
258 | printf("PTD_MOVE\n");
|
---|
259 |
|
---|
260 | gfx_coord2_add(&seat->pntpos, &event->dmove, &npos);
|
---|
261 | if (npos.x < 0)
|
---|
262 | npos.x = 0;
|
---|
263 | if (npos.y < 0)
|
---|
264 | npos.y = 0;
|
---|
265 | if (npos.x > 1024)
|
---|
266 | npos.x = 1024;
|
---|
267 | if (npos.y > 768)
|
---|
268 | npos.y = 768;
|
---|
269 |
|
---|
270 | printf("clear pointer\n");
|
---|
271 | (void) ds_seat_draw_pointer(seat, false);
|
---|
272 | seat->pntpos = npos;
|
---|
273 |
|
---|
274 | pevent.pos_id = 0;
|
---|
275 | pevent.type = POS_UPDATE;
|
---|
276 | pevent.btn_num = 0;
|
---|
277 | pevent.hpos = seat->pntpos.x;
|
---|
278 | pevent.vpos = seat->pntpos.y;
|
---|
279 |
|
---|
280 | rc = ds_seat_post_pos_event(seat, &pevent);
|
---|
281 | if (rc != EOK)
|
---|
282 | return rc;
|
---|
283 |
|
---|
284 | printf("draw pointer\n");
|
---|
285 | (void) ds_seat_draw_pointer(seat, true);
|
---|
286 | }
|
---|
287 |
|
---|
288 | return EOK;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /** Post position event to seat.
|
---|
292 | *
|
---|
293 | * Deliver event to relevant windows.
|
---|
294 | *
|
---|
295 | * @param seat Seat
|
---|
296 | * @param event Position event
|
---|
297 | */
|
---|
298 | errno_t ds_seat_post_pos_event(ds_seat_t *seat, pos_event_t *event)
|
---|
299 | {
|
---|
300 | ds_window_t *wnd;
|
---|
301 | errno_t rc;
|
---|
302 |
|
---|
303 | printf("ds_seat_post_pos_event\n");
|
---|
304 |
|
---|
305 | wnd = ds_display_window_by_pos(seat->display, &seat->pntpos);
|
---|
306 | if (wnd != NULL) {
|
---|
307 | printf("send event to window at pointer position\n");
|
---|
308 | rc = ds_window_post_pos_event(wnd, event);
|
---|
309 | if (rc != EOK)
|
---|
310 | return rc;
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (seat->focus != wnd) {
|
---|
314 | printf("send event to focused window\n");
|
---|
315 |
|
---|
316 | rc = ds_window_post_pos_event(seat->focus, event);
|
---|
317 | if (rc != EOK)
|
---|
318 | return rc;
|
---|
319 | }
|
---|
320 |
|
---|
321 | return EOK;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /** @}
|
---|
325 | */
|
---|