[cf32dbd] | 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>
|
---|
[4fbdc3d] | 38 | #include <gfx/color.h>
|
---|
| 39 | #include <gfx/render.h>
|
---|
[cf32dbd] | 40 | #include <stdlib.h>
|
---|
| 41 | #include "client.h"
|
---|
[4d8002d] | 42 | #include "cursor.h"
|
---|
[cf32dbd] | 43 | #include "display.h"
|
---|
| 44 | #include "seat.h"
|
---|
| 45 | #include "window.h"
|
---|
| 46 |
|
---|
[9901f267] | 47 | static errno_t ds_seat_clear_pointer(ds_seat_t *);
|
---|
| 48 | static errno_t ds_seat_draw_pointer(ds_seat_t *);
|
---|
| 49 |
|
---|
[cf32dbd] | 50 | /** Create seat.
|
---|
| 51 | *
|
---|
| 52 | * @param display Parent display
|
---|
| 53 | * @param rseat Place to store pointer to new seat.
|
---|
| 54 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 55 | */
|
---|
| 56 | errno_t ds_seat_create(ds_display_t *display, ds_seat_t **rseat)
|
---|
| 57 | {
|
---|
| 58 | ds_seat_t *seat;
|
---|
| 59 |
|
---|
| 60 | seat = calloc(1, sizeof(ds_seat_t));
|
---|
| 61 | if (seat == NULL)
|
---|
| 62 | return ENOMEM;
|
---|
| 63 |
|
---|
| 64 | ds_display_add_seat(display, seat);
|
---|
[c2250702] | 65 | seat->pntpos.x = 0;
|
---|
| 66 | seat->pntpos.y = 0;
|
---|
[cf32dbd] | 67 |
|
---|
[9901f267] | 68 | seat->client_cursor = display->cursor[dcurs_arrow];
|
---|
| 69 | seat->wm_cursor = NULL;
|
---|
[4d8002d] | 70 |
|
---|
[cf32dbd] | 71 | *rseat = seat;
|
---|
| 72 | return EOK;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Destroy seat.
|
---|
| 76 | *
|
---|
| 77 | * @param seat Seat
|
---|
| 78 | */
|
---|
| 79 | void ds_seat_destroy(ds_seat_t *seat)
|
---|
| 80 | {
|
---|
| 81 | ds_display_remove_seat(seat);
|
---|
| 82 | free(seat);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[4fbdc3d] | 85 | /** Set seat focus to a window.
|
---|
| 86 | *
|
---|
| 87 | * @param seat Seat
|
---|
| 88 | * @param wnd Window to focus
|
---|
| 89 | */
|
---|
[cf32dbd] | 90 | void ds_seat_set_focus(ds_seat_t *seat, ds_window_t *wnd)
|
---|
| 91 | {
|
---|
[b0a94854] | 92 | if (seat->focus != NULL)
|
---|
| 93 | ds_window_post_unfocus_event(seat->focus);
|
---|
| 94 |
|
---|
[cf32dbd] | 95 | seat->focus = wnd;
|
---|
[b0a94854] | 96 |
|
---|
[ef30659] | 97 | if (wnd != NULL) {
|
---|
[b0a94854] | 98 | ds_window_post_focus_event(wnd);
|
---|
[ef30659] | 99 | ds_window_bring_to_top(wnd);
|
---|
| 100 | }
|
---|
[cf32dbd] | 101 | }
|
---|
| 102 |
|
---|
[4fbdc3d] | 103 | /** Evacuate focus from window.
|
---|
| 104 | *
|
---|
| 105 | * If seat's focus is @a wnd, it will be set to a different window.
|
---|
| 106 | *
|
---|
| 107 | * @param seat Seat
|
---|
| 108 | * @param wnd Window to evacuate focus from
|
---|
| 109 | */
|
---|
[cf32dbd] | 110 | void ds_seat_evac_focus(ds_seat_t *seat, ds_window_t *wnd)
|
---|
| 111 | {
|
---|
| 112 | ds_window_t *nwnd;
|
---|
| 113 |
|
---|
| 114 | if (seat->focus == wnd) {
|
---|
[fd777a2] | 115 | nwnd = ds_display_next_window(wnd);
|
---|
[cf32dbd] | 116 | if (nwnd == NULL)
|
---|
[fd777a2] | 117 | nwnd = ds_display_first_window(wnd->display);
|
---|
[cf32dbd] | 118 | if (nwnd == wnd)
|
---|
| 119 | nwnd = NULL;
|
---|
| 120 |
|
---|
| 121 | ds_seat_set_focus(seat, nwnd);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** Post keyboard event to the seat's focused window.
|
---|
| 126 | *
|
---|
| 127 | * @param seat Seat
|
---|
| 128 | * @param event Event
|
---|
| 129 | *
|
---|
| 130 | * @return EOK on success or an error code
|
---|
| 131 | */
|
---|
| 132 | errno_t ds_seat_post_kbd_event(ds_seat_t *seat, kbd_event_t *event)
|
---|
| 133 | {
|
---|
[79949f3] | 134 | ds_window_t *dwindow;
|
---|
| 135 | bool alt_or_shift;
|
---|
[cf32dbd] | 136 |
|
---|
[79949f3] | 137 | alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
|
---|
| 138 | if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_TAB) {
|
---|
| 139 | /* On Alt-Tab or Shift-Tab, switch focus to next window */
|
---|
| 140 | ds_seat_evac_focus(seat, seat->focus);
|
---|
| 141 | return EOK;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | dwindow = seat->focus;
|
---|
[cf32dbd] | 145 | if (dwindow == NULL)
|
---|
| 146 | return EOK;
|
---|
| 147 |
|
---|
[338d0935] | 148 | return ds_window_post_kbd_event(dwindow, event);
|
---|
[cf32dbd] | 149 | }
|
---|
| 150 |
|
---|
[9901f267] | 151 | /** Get current cursor used by seat.
|
---|
| 152 | *
|
---|
| 153 | * @param wmcurs WM curor
|
---|
| 154 | * @param ccurs Client cursor
|
---|
| 155 | * @return
|
---|
| 156 | */
|
---|
| 157 | static ds_cursor_t *ds_seat_compute_cursor(ds_cursor_t *wmcurs, ds_cursor_t *ccurs)
|
---|
| 158 | {
|
---|
| 159 | if (wmcurs != NULL)
|
---|
| 160 | return wmcurs;
|
---|
| 161 |
|
---|
| 162 | return ccurs;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /** Get current cursor used by seat.
|
---|
| 166 | *
|
---|
| 167 | * @param seat Seat
|
---|
| 168 | * @return Current cursor
|
---|
| 169 | */
|
---|
| 170 | static ds_cursor_t *ds_seat_get_cursor(ds_seat_t *seat)
|
---|
| 171 | {
|
---|
| 172 | return ds_seat_compute_cursor(seat->wm_cursor, seat->client_cursor);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /** Set client cursor.
|
---|
| 176 | *
|
---|
| 177 | * Set cursor selected by client. This may update the actual cursor
|
---|
| 178 | * if WM is not overriding the cursor.
|
---|
| 179 | *
|
---|
| 180 | * @param seat Seat
|
---|
| 181 | * @param cursor Client cursor
|
---|
| 182 | */
|
---|
| 183 | static void ds_seat_set_client_cursor(ds_seat_t *seat, ds_cursor_t *cursor)
|
---|
| 184 | {
|
---|
| 185 | ds_cursor_t *old_cursor;
|
---|
| 186 | ds_cursor_t *new_cursor;
|
---|
| 187 |
|
---|
| 188 | old_cursor = ds_seat_get_cursor(seat);
|
---|
| 189 | new_cursor = ds_seat_compute_cursor(seat->wm_cursor, cursor);
|
---|
| 190 |
|
---|
| 191 | if (new_cursor != old_cursor)
|
---|
| 192 | ds_seat_clear_pointer(seat);
|
---|
| 193 |
|
---|
| 194 | seat->client_cursor = cursor;
|
---|
| 195 |
|
---|
| 196 | if (new_cursor != old_cursor)
|
---|
| 197 | ds_seat_draw_pointer(seat);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /** Set WM cursor.
|
---|
| 201 | *
|
---|
| 202 | * Set cursor override for window management.
|
---|
| 203 | *
|
---|
| 204 | * @param seat Seat
|
---|
| 205 | * @param cursor WM cursor override or @c NULL not to override the cursor
|
---|
| 206 | */
|
---|
| 207 | void ds_seat_set_wm_cursor(ds_seat_t *seat, ds_cursor_t *cursor)
|
---|
| 208 | {
|
---|
| 209 | ds_cursor_t *old_cursor;
|
---|
| 210 | ds_cursor_t *new_cursor;
|
---|
| 211 |
|
---|
| 212 | old_cursor = ds_seat_get_cursor(seat);
|
---|
| 213 | new_cursor = ds_seat_compute_cursor(cursor, seat->client_cursor);
|
---|
| 214 |
|
---|
| 215 | if (new_cursor != old_cursor)
|
---|
| 216 | ds_seat_clear_pointer(seat);
|
---|
| 217 |
|
---|
| 218 | seat->wm_cursor = cursor;
|
---|
| 219 |
|
---|
| 220 | if (new_cursor != old_cursor)
|
---|
| 221 | ds_seat_draw_pointer(seat);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[28db46b] | 224 | /** Draw seat pointer
|
---|
[4fbdc3d] | 225 | *
|
---|
| 226 | * @param seat Seat
|
---|
| 227 | *
|
---|
| 228 | * @return EOK on success or an error code
|
---|
| 229 | */
|
---|
[28db46b] | 230 | static errno_t ds_seat_draw_pointer(ds_seat_t *seat)
|
---|
[4fbdc3d] | 231 | {
|
---|
[9901f267] | 232 | ds_cursor_t *cursor;
|
---|
| 233 |
|
---|
| 234 | cursor = ds_seat_get_cursor(seat);
|
---|
| 235 | return ds_cursor_paint(cursor, &seat->pntpos);
|
---|
[4fbdc3d] | 236 | }
|
---|
| 237 |
|
---|
[28db46b] | 238 | /** Clear seat pointer
|
---|
| 239 | *
|
---|
| 240 | * @param seat Seat
|
---|
| 241 | *
|
---|
| 242 | * @return EOK on success or an error code
|
---|
| 243 | */
|
---|
| 244 | static errno_t ds_seat_clear_pointer(ds_seat_t *seat)
|
---|
| 245 | {
|
---|
| 246 | gfx_rect_t rect;
|
---|
[9901f267] | 247 | ds_cursor_t *cursor;
|
---|
| 248 |
|
---|
| 249 | cursor = ds_seat_get_cursor(seat);
|
---|
[28db46b] | 250 |
|
---|
[4d8002d] | 251 | /* Get rectangle covered by cursor */
|
---|
[9901f267] | 252 | ds_cursor_get_rect(cursor, &seat->pntpos, &rect);
|
---|
[28db46b] | 253 |
|
---|
[4d8002d] | 254 | /* Repaint it */
|
---|
[28db46b] | 255 | return ds_display_paint(seat->display, &rect);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[4fbdc3d] | 258 | /** Post pointing device event to the seat
|
---|
[a40ae0d] | 259 | *
|
---|
| 260 | * Update pointer position and generate position event.
|
---|
[4fbdc3d] | 261 | *
|
---|
| 262 | * @param seat Seat
|
---|
| 263 | * @param event Event
|
---|
| 264 | *
|
---|
| 265 | * @return EOK on success or an error code
|
---|
| 266 | */
|
---|
| 267 | errno_t ds_seat_post_ptd_event(ds_seat_t *seat, ptd_event_t *event)
|
---|
| 268 | {
|
---|
[e1f2079] | 269 | ds_display_t *disp = seat->display;
|
---|
[4fbdc3d] | 270 | gfx_coord2_t npos;
|
---|
[a40ae0d] | 271 | ds_window_t *wnd;
|
---|
| 272 | pos_event_t pevent;
|
---|
| 273 | errno_t rc;
|
---|
[4fbdc3d] | 274 |
|
---|
[a40ae0d] | 275 | wnd = ds_display_window_by_pos(seat->display, &seat->pntpos);
|
---|
| 276 |
|
---|
[4fbdc3d] | 277 | /* Focus window on button press */
|
---|
[a40ae0d] | 278 | if (event->type == PTD_PRESS && event->btn_num == 1) {
|
---|
[4fbdc3d] | 279 | if (wnd != NULL) {
|
---|
| 280 | ds_seat_set_focus(seat, wnd);
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[a40ae0d] | 284 | if (event->type == PTD_PRESS || event->type == PTD_RELEASE) {
|
---|
| 285 | pevent.pos_id = 0;
|
---|
| 286 | pevent.type = (event->type == PTD_PRESS) ?
|
---|
| 287 | POS_PRESS : POS_RELEASE;
|
---|
| 288 | pevent.btn_num = event->btn_num;
|
---|
| 289 | pevent.hpos = seat->pntpos.x;
|
---|
| 290 | pevent.vpos = seat->pntpos.y;
|
---|
| 291 |
|
---|
| 292 | rc = ds_seat_post_pos_event(seat, &pevent);
|
---|
| 293 | if (rc != EOK)
|
---|
| 294 | return rc;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[4fbdc3d] | 297 | if (event->type == PTD_MOVE) {
|
---|
| 298 | gfx_coord2_add(&seat->pntpos, &event->dmove, &npos);
|
---|
[e1f2079] | 299 | gfx_coord2_clip(&npos, &disp->rect, &npos);
|
---|
[4fbdc3d] | 300 |
|
---|
[28db46b] | 301 | (void) ds_seat_clear_pointer(seat);
|
---|
[4fbdc3d] | 302 | seat->pntpos = npos;
|
---|
[a40ae0d] | 303 |
|
---|
| 304 | pevent.pos_id = 0;
|
---|
| 305 | pevent.type = POS_UPDATE;
|
---|
| 306 | pevent.btn_num = 0;
|
---|
| 307 | pevent.hpos = seat->pntpos.x;
|
---|
| 308 | pevent.vpos = seat->pntpos.y;
|
---|
| 309 |
|
---|
| 310 | rc = ds_seat_post_pos_event(seat, &pevent);
|
---|
| 311 | if (rc != EOK)
|
---|
| 312 | return rc;
|
---|
| 313 |
|
---|
[28db46b] | 314 | (void) ds_seat_draw_pointer(seat);
|
---|
[4fbdc3d] | 315 | }
|
---|
| 316 |
|
---|
[1388f7f0] | 317 | if (event->type == PTD_ABS_MOVE) {
|
---|
| 318 | /*
|
---|
| 319 | * Project input device area onto display area. Technically
|
---|
| 320 | * we probably want to project onto the area of a particular
|
---|
| 321 | * display device. The tricky part is figuring out which
|
---|
| 322 | * display device the input device is associated with.
|
---|
| 323 | */
|
---|
| 324 | gfx_coord2_project(&event->apos, &event->abounds,
|
---|
| 325 | &disp->rect, &npos);
|
---|
| 326 |
|
---|
| 327 | gfx_coord2_clip(&npos, &disp->rect, &npos);
|
---|
| 328 |
|
---|
| 329 | (void) ds_seat_clear_pointer(seat);
|
---|
| 330 | seat->pntpos = npos;
|
---|
| 331 |
|
---|
| 332 | pevent.pos_id = 0;
|
---|
| 333 | pevent.type = POS_UPDATE;
|
---|
| 334 | pevent.btn_num = 0;
|
---|
| 335 | pevent.hpos = seat->pntpos.x;
|
---|
| 336 | pevent.vpos = seat->pntpos.y;
|
---|
| 337 |
|
---|
| 338 | rc = ds_seat_post_pos_event(seat, &pevent);
|
---|
| 339 | if (rc != EOK)
|
---|
| 340 | return rc;
|
---|
| 341 |
|
---|
| 342 | (void) ds_seat_draw_pointer(seat);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[4fbdc3d] | 345 | return EOK;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[a40ae0d] | 348 | /** Post position event to seat.
|
---|
| 349 | *
|
---|
| 350 | * Deliver event to relevant windows.
|
---|
| 351 | *
|
---|
| 352 | * @param seat Seat
|
---|
| 353 | * @param event Position event
|
---|
| 354 | */
|
---|
| 355 | errno_t ds_seat_post_pos_event(ds_seat_t *seat, pos_event_t *event)
|
---|
| 356 | {
|
---|
| 357 | ds_window_t *wnd;
|
---|
| 358 | errno_t rc;
|
---|
| 359 |
|
---|
| 360 | wnd = ds_display_window_by_pos(seat->display, &seat->pntpos);
|
---|
| 361 | if (wnd != NULL) {
|
---|
[9242ad9] | 362 | /* Moving over a window */
|
---|
[9901f267] | 363 | ds_seat_set_client_cursor(seat, wnd->cursor);
|
---|
[9242ad9] | 364 |
|
---|
[a40ae0d] | 365 | rc = ds_window_post_pos_event(wnd, event);
|
---|
| 366 | if (rc != EOK)
|
---|
| 367 | return rc;
|
---|
[9242ad9] | 368 | } else {
|
---|
| 369 | /* Not over a window */
|
---|
[9901f267] | 370 | ds_seat_set_client_cursor(seat, seat->display->cursor[dcurs_arrow]);
|
---|
[a40ae0d] | 371 | }
|
---|
| 372 |
|
---|
| 373 | if (seat->focus != wnd) {
|
---|
| 374 | rc = ds_window_post_pos_event(seat->focus, event);
|
---|
| 375 | if (rc != EOK)
|
---|
| 376 | return rc;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | return EOK;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[cf32dbd] | 382 | /** @}
|
---|
| 383 | */
|
---|