[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"
|
---|
| 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);
|
---|
[c2250702] | 61 | seat->pntpos.x = 0;
|
---|
| 62 | seat->pntpos.y = 0;
|
---|
[cf32dbd] | 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 |
|
---|
[4fbdc3d] | 78 | /** Set seat focus to a window.
|
---|
| 79 | *
|
---|
| 80 | * @param seat Seat
|
---|
| 81 | * @param wnd Window to focus
|
---|
| 82 | */
|
---|
[cf32dbd] | 83 | void ds_seat_set_focus(ds_seat_t *seat, ds_window_t *wnd)
|
---|
| 84 | {
|
---|
| 85 | seat->focus = wnd;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[4fbdc3d] | 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 | */
|
---|
[cf32dbd] | 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) {
|
---|
[fd777a2] | 100 | nwnd = ds_display_next_window(wnd);
|
---|
[cf32dbd] | 101 | if (nwnd == NULL)
|
---|
[fd777a2] | 102 | nwnd = ds_display_first_window(wnd->display);
|
---|
[cf32dbd] | 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 | {
|
---|
[79949f3] | 119 | ds_window_t *dwindow;
|
---|
| 120 | bool alt_or_shift;
|
---|
[cf32dbd] | 121 |
|
---|
[79949f3] | 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;
|
---|
[cf32dbd] | 130 | if (dwindow == NULL)
|
---|
| 131 | return EOK;
|
---|
| 132 |
|
---|
| 133 | return ds_client_post_kbd_event(dwindow->client, dwindow, event);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[4fbdc3d] | 136 | /** Draw cross at seat pointer position.
|
---|
| 137 | *
|
---|
| 138 | * @param seat Seat
|
---|
| 139 | * @param len Cross length
|
---|
| 140 | * @param w Cross extra width
|
---|
[c79545e] | 141 | * @param color Color
|
---|
[4fbdc3d] | 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,
|
---|
[c79545e] | 146 | gfx_coord_t w, gfx_color_t *color)
|
---|
[4fbdc3d] | 147 | {
|
---|
| 148 | gfx_context_t *gc;
|
---|
| 149 | gfx_rect_t rect, r0;
|
---|
| 150 | errno_t rc;
|
---|
| 151 |
|
---|
| 152 | gc = ds_display_get_gc(seat->display);
|
---|
| 153 | if (gc == NULL)
|
---|
| 154 | return EOK;
|
---|
| 155 |
|
---|
| 156 | rc = gfx_set_color(gc, color);
|
---|
| 157 | if (rc != EOK)
|
---|
| 158 | goto error;
|
---|
| 159 |
|
---|
| 160 | r0.p0.x = -len;
|
---|
| 161 | r0.p0.y = -w;
|
---|
| 162 | r0.p1.x = +len + 1;
|
---|
| 163 | r0.p1.y = +w + 1;
|
---|
| 164 | gfx_rect_translate(&seat->pntpos, &r0, &rect);
|
---|
| 165 |
|
---|
| 166 | rc = gfx_fill_rect(gc, &rect);
|
---|
| 167 | if (rc != EOK)
|
---|
| 168 | goto error;
|
---|
| 169 |
|
---|
| 170 | r0.p0.x = -w;
|
---|
| 171 | r0.p0.y = -len;
|
---|
| 172 | r0.p1.x = +w + 1;
|
---|
| 173 | r0.p1.y = +len + 1;
|
---|
| 174 | gfx_rect_translate(&seat->pntpos, &r0, &rect);
|
---|
| 175 |
|
---|
| 176 | rc = gfx_fill_rect(gc, &rect);
|
---|
| 177 | if (rc != EOK)
|
---|
| 178 | goto error;
|
---|
| 179 |
|
---|
| 180 | return EOK;
|
---|
| 181 | error:
|
---|
| 182 | return rc;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[28db46b] | 185 | /** Draw seat pointer
|
---|
[4fbdc3d] | 186 | *
|
---|
| 187 | * @param seat Seat
|
---|
| 188 | *
|
---|
| 189 | * @return EOK on success or an error code
|
---|
| 190 | */
|
---|
[28db46b] | 191 | static errno_t ds_seat_draw_pointer(ds_seat_t *seat)
|
---|
[4fbdc3d] | 192 | {
|
---|
| 193 | errno_t rc;
|
---|
[c79545e] | 194 | gfx_color_t *black = NULL;
|
---|
| 195 | gfx_color_t *white;
|
---|
| 196 |
|
---|
| 197 | rc = gfx_color_new_rgb_i16(0, 0, 0, &black);
|
---|
| 198 | if (rc != EOK)
|
---|
| 199 | goto error;
|
---|
[4fbdc3d] | 200 |
|
---|
[c79545e] | 201 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &white);
|
---|
| 202 | if (rc != EOK)
|
---|
| 203 | goto error;
|
---|
| 204 |
|
---|
[28db46b] | 205 | rc = ds_seat_draw_cross(seat, 8, 1, black);
|
---|
[4fbdc3d] | 206 | if (rc != EOK)
|
---|
[28db46b] | 207 | goto error;
|
---|
[4fbdc3d] | 208 |
|
---|
[28db46b] | 209 | rc = ds_seat_draw_cross(seat, 8, 0, white);
|
---|
[4fbdc3d] | 210 | if (rc != EOK)
|
---|
[28db46b] | 211 | goto error;
|
---|
| 212 |
|
---|
| 213 | gfx_color_delete(black);
|
---|
| 214 | gfx_color_delete(white);
|
---|
[4fbdc3d] | 215 |
|
---|
| 216 | return EOK;
|
---|
[c79545e] | 217 | error:
|
---|
| 218 | if (black != NULL)
|
---|
| 219 | gfx_color_delete(black);
|
---|
[28db46b] | 220 | if (white != NULL)
|
---|
| 221 | gfx_color_delete(white);
|
---|
[c79545e] | 222 | return rc;
|
---|
[4fbdc3d] | 223 | }
|
---|
| 224 |
|
---|
[28db46b] | 225 | /** Clear seat pointer
|
---|
| 226 | *
|
---|
| 227 | * @param seat Seat
|
---|
| 228 | *
|
---|
| 229 | * @return EOK on success or an error code
|
---|
| 230 | */
|
---|
| 231 | static errno_t ds_seat_clear_pointer(ds_seat_t *seat)
|
---|
| 232 | {
|
---|
| 233 | gfx_rect_t rect;
|
---|
| 234 |
|
---|
| 235 | rect.p0.x = seat->pntpos.x - 8;
|
---|
| 236 | rect.p0.y = seat->pntpos.y - 8;
|
---|
| 237 | rect.p1.x = seat->pntpos.x + 8 + 1;
|
---|
| 238 | rect.p1.y = seat->pntpos.y + 8 + 1;
|
---|
| 239 |
|
---|
| 240 | return ds_display_paint(seat->display, &rect);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[4fbdc3d] | 243 | /** Post pointing device event to the seat
|
---|
[a40ae0d] | 244 | *
|
---|
| 245 | * Update pointer position and generate position event.
|
---|
[4fbdc3d] | 246 | *
|
---|
| 247 | * @param seat Seat
|
---|
| 248 | * @param event Event
|
---|
| 249 | *
|
---|
| 250 | * @return EOK on success or an error code
|
---|
| 251 | */
|
---|
| 252 | #include <stdio.h>
|
---|
| 253 | errno_t ds_seat_post_ptd_event(ds_seat_t *seat, ptd_event_t *event)
|
---|
| 254 | {
|
---|
[e1f2079] | 255 | ds_display_t *disp = seat->display;
|
---|
[4fbdc3d] | 256 | gfx_coord2_t npos;
|
---|
[a40ae0d] | 257 | ds_window_t *wnd;
|
---|
| 258 | pos_event_t pevent;
|
---|
| 259 | errno_t rc;
|
---|
[4fbdc3d] | 260 |
|
---|
| 261 | printf("ds_seat_post_ptd_event\n");
|
---|
[a40ae0d] | 262 | wnd = ds_display_window_by_pos(seat->display, &seat->pntpos);
|
---|
| 263 |
|
---|
[4fbdc3d] | 264 | /* Focus window on button press */
|
---|
[a40ae0d] | 265 | if (event->type == PTD_PRESS && event->btn_num == 1) {
|
---|
| 266 | printf("PTD_PRESS (button = %d)\n", event->btn_num);
|
---|
[4fbdc3d] | 267 | if (wnd != NULL) {
|
---|
| 268 | printf("set focus\n");
|
---|
| 269 | ds_seat_set_focus(seat, wnd);
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[a40ae0d] | 273 | if (event->type == PTD_PRESS || event->type == PTD_RELEASE) {
|
---|
| 274 | pevent.pos_id = 0;
|
---|
| 275 | pevent.type = (event->type == PTD_PRESS) ?
|
---|
| 276 | POS_PRESS : POS_RELEASE;
|
---|
| 277 | pevent.btn_num = event->btn_num;
|
---|
| 278 | pevent.hpos = seat->pntpos.x;
|
---|
| 279 | pevent.vpos = seat->pntpos.y;
|
---|
| 280 |
|
---|
| 281 | rc = ds_seat_post_pos_event(seat, &pevent);
|
---|
| 282 | if (rc != EOK)
|
---|
| 283 | return rc;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[4fbdc3d] | 286 | if (event->type == PTD_MOVE) {
|
---|
| 287 | printf("PTD_MOVE\n");
|
---|
[a40ae0d] | 288 |
|
---|
[4fbdc3d] | 289 | gfx_coord2_add(&seat->pntpos, &event->dmove, &npos);
|
---|
[e1f2079] | 290 | gfx_coord2_clip(&npos, &disp->rect, &npos);
|
---|
[4fbdc3d] | 291 |
|
---|
| 292 | printf("clear pointer\n");
|
---|
[28db46b] | 293 | (void) ds_seat_clear_pointer(seat);
|
---|
[4fbdc3d] | 294 | seat->pntpos = npos;
|
---|
[a40ae0d] | 295 |
|
---|
| 296 | pevent.pos_id = 0;
|
---|
| 297 | pevent.type = POS_UPDATE;
|
---|
| 298 | pevent.btn_num = 0;
|
---|
| 299 | pevent.hpos = seat->pntpos.x;
|
---|
| 300 | pevent.vpos = seat->pntpos.y;
|
---|
| 301 |
|
---|
| 302 | rc = ds_seat_post_pos_event(seat, &pevent);
|
---|
| 303 | if (rc != EOK)
|
---|
| 304 | return rc;
|
---|
| 305 |
|
---|
[4fbdc3d] | 306 | printf("draw pointer\n");
|
---|
[28db46b] | 307 | (void) ds_seat_draw_pointer(seat);
|
---|
[4fbdc3d] | 308 | }
|
---|
| 309 |
|
---|
| 310 | return EOK;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[a40ae0d] | 313 | /** Post position event to seat.
|
---|
| 314 | *
|
---|
| 315 | * Deliver event to relevant windows.
|
---|
| 316 | *
|
---|
| 317 | * @param seat Seat
|
---|
| 318 | * @param event Position event
|
---|
| 319 | */
|
---|
| 320 | errno_t ds_seat_post_pos_event(ds_seat_t *seat, pos_event_t *event)
|
---|
| 321 | {
|
---|
| 322 | ds_window_t *wnd;
|
---|
| 323 | errno_t rc;
|
---|
| 324 |
|
---|
| 325 | printf("ds_seat_post_pos_event\n");
|
---|
| 326 |
|
---|
| 327 | wnd = ds_display_window_by_pos(seat->display, &seat->pntpos);
|
---|
| 328 | if (wnd != NULL) {
|
---|
| 329 | printf("send event to window at pointer position\n");
|
---|
| 330 | rc = ds_window_post_pos_event(wnd, event);
|
---|
| 331 | if (rc != EOK)
|
---|
| 332 | return rc;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | if (seat->focus != wnd) {
|
---|
| 336 | printf("send event to focused window\n");
|
---|
| 337 |
|
---|
| 338 | rc = ds_window_post_pos_event(seat->focus, event);
|
---|
| 339 | if (rc != EOK)
|
---|
| 340 | return rc;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | return EOK;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[cf32dbd] | 346 | /** @}
|
---|
| 347 | */
|
---|