| 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 client
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include "client.h"
|
|---|
| 40 | #include "display.h"
|
|---|
| 41 | #include "seat.h"
|
|---|
| 42 | #include "window.h"
|
|---|
| 43 |
|
|---|
| 44 | /** Create client.
|
|---|
| 45 | *
|
|---|
| 46 | * @param display Parent display
|
|---|
| 47 | * @param cb Client callbacks
|
|---|
| 48 | * @param cb_arg Callback argument
|
|---|
| 49 | * @param rclient Place to store pointer to new client.
|
|---|
| 50 | * @return EOK on success, ENOMEM if out of memory
|
|---|
| 51 | */
|
|---|
| 52 | errno_t ds_client_create(ds_display_t *display, ds_client_cb_t *cb,
|
|---|
| 53 | void *cb_arg, ds_client_t **rclient)
|
|---|
| 54 | {
|
|---|
| 55 | ds_client_t *client;
|
|---|
| 56 |
|
|---|
| 57 | client = calloc(1, sizeof(ds_client_t));
|
|---|
| 58 | if (client == NULL)
|
|---|
| 59 | return ENOMEM;
|
|---|
| 60 |
|
|---|
| 61 | list_initialize(&client->windows);
|
|---|
| 62 | list_initialize(&client->events);
|
|---|
| 63 | client->cb = cb;
|
|---|
| 64 | client->cb_arg = cb_arg;
|
|---|
| 65 |
|
|---|
| 66 | ds_display_add_client(display, client);
|
|---|
| 67 |
|
|---|
| 68 | *rclient = client;
|
|---|
| 69 | return EOK;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /** Destroy client.
|
|---|
| 73 | *
|
|---|
| 74 | * @param client Client
|
|---|
| 75 | */
|
|---|
| 76 | void ds_client_destroy(ds_client_t *client)
|
|---|
| 77 | {
|
|---|
| 78 | ds_window_t *window;
|
|---|
| 79 |
|
|---|
| 80 | window = ds_client_first_window(client);
|
|---|
| 81 | while (window != NULL) {
|
|---|
| 82 | ds_window_destroy(window);
|
|---|
| 83 | window = ds_client_first_window(client);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | assert(list_empty(&client->windows));
|
|---|
| 87 | ds_display_remove_client(client);
|
|---|
| 88 | free(client);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /** Add window to client.
|
|---|
| 92 | *
|
|---|
| 93 | * @param client client
|
|---|
| 94 | * @param wnd Window
|
|---|
| 95 | */
|
|---|
| 96 | void ds_client_add_window(ds_client_t *client, ds_window_t *wnd)
|
|---|
| 97 | {
|
|---|
| 98 | assert(wnd->client == NULL);
|
|---|
| 99 | assert(!link_used(&wnd->lcwindows));
|
|---|
| 100 |
|
|---|
| 101 | wnd->client = client;
|
|---|
| 102 | wnd->id = client->display->next_wnd_id++;
|
|---|
| 103 | list_append(&wnd->lcwindows, &client->windows);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Remove window from client.
|
|---|
| 107 | *
|
|---|
| 108 | * @param wnd Window
|
|---|
| 109 | */
|
|---|
| 110 | void ds_client_remove_window(ds_window_t *wnd)
|
|---|
| 111 | {
|
|---|
| 112 | ds_seat_t *seat;
|
|---|
| 113 |
|
|---|
| 114 | /* Make sure window is no longer focused in any seat */
|
|---|
| 115 | seat = ds_display_first_seat(wnd->display);
|
|---|
| 116 | while (seat != NULL) {
|
|---|
| 117 | ds_seat_evac_focus(seat, wnd);
|
|---|
| 118 | seat = ds_display_next_seat(seat);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | list_remove(&wnd->lcwindows);
|
|---|
| 122 | wnd->client = NULL;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** Find window by ID.
|
|---|
| 126 | *
|
|---|
| 127 | * @param client Client
|
|---|
| 128 | * @param id Window ID
|
|---|
| 129 | */
|
|---|
| 130 | ds_window_t *ds_client_find_window(ds_client_t *client, ds_wnd_id_t id)
|
|---|
| 131 | {
|
|---|
| 132 | ds_window_t *wnd;
|
|---|
| 133 |
|
|---|
| 134 | // TODO Make this faster
|
|---|
| 135 | wnd = ds_client_first_window(client);
|
|---|
| 136 | while (wnd != NULL) {
|
|---|
| 137 | if (wnd->id == id)
|
|---|
| 138 | return wnd;
|
|---|
| 139 | wnd = ds_client_next_window(wnd);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | return NULL;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** Get first window in client.
|
|---|
| 146 | *
|
|---|
| 147 | * @param client Client
|
|---|
| 148 | * @return First window or @c NULL if there is none
|
|---|
| 149 | */
|
|---|
| 150 | ds_window_t *ds_client_first_window(ds_client_t *client)
|
|---|
| 151 | {
|
|---|
| 152 | link_t *link = list_first(&client->windows);
|
|---|
| 153 |
|
|---|
| 154 | if (link == NULL)
|
|---|
| 155 | return NULL;
|
|---|
| 156 |
|
|---|
| 157 | return list_get_instance(link, ds_window_t, lcwindows);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Get next window in client.
|
|---|
| 161 | *
|
|---|
| 162 | * @param wnd Current window
|
|---|
| 163 | * @return Next window or @c NULL if there is none
|
|---|
| 164 | */
|
|---|
| 165 | ds_window_t *ds_client_next_window(ds_window_t *wnd)
|
|---|
| 166 | {
|
|---|
| 167 | link_t *link = list_next(&wnd->lcwindows, &wnd->client->windows);
|
|---|
| 168 |
|
|---|
| 169 | if (link == NULL)
|
|---|
| 170 | return NULL;
|
|---|
| 171 |
|
|---|
| 172 | return list_get_instance(link, ds_window_t, lcwindows);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /** Get next event from client event queue.
|
|---|
| 176 | *
|
|---|
| 177 | * @param client Client
|
|---|
| 178 | * @param ewindow Place to store pointer to window receiving the event
|
|---|
| 179 | * @param event Place to store event
|
|---|
| 180 | * @return Graphic context
|
|---|
| 181 | */
|
|---|
| 182 | errno_t ds_client_get_event(ds_client_t *client, ds_window_t **ewindow,
|
|---|
| 183 | display_wnd_ev_t *event)
|
|---|
| 184 | {
|
|---|
| 185 | link_t *link;
|
|---|
| 186 | ds_window_ev_t *wevent;
|
|---|
| 187 |
|
|---|
| 188 | link = list_first(&client->events);
|
|---|
| 189 | if (link == NULL)
|
|---|
| 190 | return ENOENT;
|
|---|
| 191 |
|
|---|
| 192 | wevent = list_get_instance(link, ds_window_ev_t, levents);
|
|---|
| 193 | list_remove(link);
|
|---|
| 194 |
|
|---|
| 195 | *ewindow = wevent->window;
|
|---|
| 196 | *event = wevent->event;
|
|---|
| 197 | free(wevent);
|
|---|
| 198 | return EOK;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** Post close event to the client's message queue.
|
|---|
| 202 | *
|
|---|
| 203 | * @param client Client
|
|---|
| 204 | * @param ewindow Window that the message is targetted to
|
|---|
| 205 | *
|
|---|
| 206 | * @return EOK on success or an error code
|
|---|
| 207 | */
|
|---|
| 208 | errno_t ds_client_post_close_event(ds_client_t *client, ds_window_t *ewindow)
|
|---|
| 209 | {
|
|---|
| 210 | ds_window_ev_t *wevent;
|
|---|
| 211 |
|
|---|
| 212 | wevent = calloc(1, sizeof(ds_window_ev_t));
|
|---|
| 213 | if (wevent == NULL)
|
|---|
| 214 | return ENOMEM;
|
|---|
| 215 |
|
|---|
| 216 | wevent->window = ewindow;
|
|---|
| 217 | wevent->event.etype = wev_close;
|
|---|
| 218 | list_append(&wevent->levents, &client->events);
|
|---|
| 219 |
|
|---|
| 220 | /* Notify the client */
|
|---|
| 221 | // TODO Do not send more than once until client drains the queue
|
|---|
| 222 | if (client->cb != NULL && client->cb->ev_pending != NULL)
|
|---|
| 223 | client->cb->ev_pending(client->cb_arg);
|
|---|
| 224 |
|
|---|
| 225 | return EOK;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /** Post focus event to the client's message queue.
|
|---|
| 229 | *
|
|---|
| 230 | * @param client Client
|
|---|
| 231 | * @param ewindow Window that the message is targetted to
|
|---|
| 232 | *
|
|---|
| 233 | * @return EOK on success or an error code
|
|---|
| 234 | */
|
|---|
| 235 | errno_t ds_client_post_focus_event(ds_client_t *client, ds_window_t *ewindow)
|
|---|
| 236 | {
|
|---|
| 237 | ds_window_ev_t *wevent;
|
|---|
| 238 |
|
|---|
| 239 | wevent = calloc(1, sizeof(ds_window_ev_t));
|
|---|
| 240 | if (wevent == NULL)
|
|---|
| 241 | return ENOMEM;
|
|---|
| 242 |
|
|---|
| 243 | wevent->window = ewindow;
|
|---|
| 244 | wevent->event.etype = wev_focus;
|
|---|
| 245 | list_append(&wevent->levents, &client->events);
|
|---|
| 246 |
|
|---|
| 247 | /* Notify the client */
|
|---|
| 248 | // TODO Do not send more than once until client drains the queue
|
|---|
| 249 | if (client->cb != NULL && client->cb->ev_pending != NULL)
|
|---|
| 250 | client->cb->ev_pending(client->cb_arg);
|
|---|
| 251 |
|
|---|
| 252 | return EOK;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /** Post keyboard event to the client's message queue.
|
|---|
| 256 | *
|
|---|
| 257 | * @param client Client
|
|---|
| 258 | * @param ewindow Window that the message is targetted to
|
|---|
| 259 | * @param event Event
|
|---|
| 260 | *
|
|---|
| 261 | * @return EOK on success or an error code
|
|---|
| 262 | */
|
|---|
| 263 | errno_t ds_client_post_kbd_event(ds_client_t *client, ds_window_t *ewindow,
|
|---|
| 264 | kbd_event_t *event)
|
|---|
| 265 | {
|
|---|
| 266 | ds_window_ev_t *wevent;
|
|---|
| 267 |
|
|---|
| 268 | wevent = calloc(1, sizeof(ds_window_ev_t));
|
|---|
| 269 | if (wevent == NULL)
|
|---|
| 270 | return ENOMEM;
|
|---|
| 271 |
|
|---|
| 272 | wevent->window = ewindow;
|
|---|
| 273 | wevent->event.etype = wev_kbd;
|
|---|
| 274 | wevent->event.ev.kbd = *event;
|
|---|
| 275 | list_append(&wevent->levents, &client->events);
|
|---|
| 276 |
|
|---|
| 277 | /* Notify the client */
|
|---|
| 278 | // TODO Do not send more than once until client drains the queue
|
|---|
| 279 | client->cb->ev_pending(client->cb_arg);
|
|---|
| 280 |
|
|---|
| 281 | return EOK;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** Post position event to the client's message queue.
|
|---|
| 285 | *
|
|---|
| 286 | * @param client Client
|
|---|
| 287 | * @param ewindow Window that the message is targetted to
|
|---|
| 288 | * @param event Event
|
|---|
| 289 | *
|
|---|
| 290 | * @return EOK on success or an error code
|
|---|
| 291 | */
|
|---|
| 292 | errno_t ds_client_post_pos_event(ds_client_t *client, ds_window_t *ewindow,
|
|---|
| 293 | pos_event_t *event)
|
|---|
| 294 | {
|
|---|
| 295 | ds_window_ev_t *wevent;
|
|---|
| 296 |
|
|---|
| 297 | wevent = calloc(1, sizeof(ds_window_ev_t));
|
|---|
| 298 | if (wevent == NULL)
|
|---|
| 299 | return ENOMEM;
|
|---|
| 300 |
|
|---|
| 301 | wevent->window = ewindow;
|
|---|
| 302 | wevent->event.etype = wev_pos;
|
|---|
| 303 | wevent->event.ev.pos = *event;
|
|---|
| 304 | list_append(&wevent->levents, &client->events);
|
|---|
| 305 |
|
|---|
| 306 | /* Notify the client */
|
|---|
| 307 | // TODO Do not send more than once until client drains the queue
|
|---|
| 308 | if (client->cb != NULL && client->cb->ev_pending != NULL)
|
|---|
| 309 | client->cb->ev_pending(client->cb_arg);
|
|---|
| 310 |
|
|---|
| 311 | return EOK;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /** Post resize event to the client's message queue.
|
|---|
| 315 | *
|
|---|
| 316 | * @param client Client
|
|---|
| 317 | * @param ewindow Window that the message is targetted to
|
|---|
| 318 | * @param rect New window rectangle
|
|---|
| 319 | *
|
|---|
| 320 | * @return EOK on success or an error code
|
|---|
| 321 | */
|
|---|
| 322 | errno_t ds_client_post_resize_event(ds_client_t *client, ds_window_t *ewindow,
|
|---|
| 323 | gfx_rect_t *rect)
|
|---|
| 324 | {
|
|---|
| 325 | ds_window_ev_t *wevent;
|
|---|
| 326 |
|
|---|
| 327 | wevent = calloc(1, sizeof(ds_window_ev_t));
|
|---|
| 328 | if (wevent == NULL)
|
|---|
| 329 | return ENOMEM;
|
|---|
| 330 |
|
|---|
| 331 | wevent->window = ewindow;
|
|---|
| 332 | wevent->event.etype = wev_resize;
|
|---|
| 333 | wevent->event.ev.resize.rect = *rect;
|
|---|
| 334 | list_append(&wevent->levents, &client->events);
|
|---|
| 335 |
|
|---|
| 336 | /* Notify the client */
|
|---|
| 337 | // TODO Do not send more than once until client drains the queue
|
|---|
| 338 | if (client->cb != NULL && client->cb->ev_pending != NULL)
|
|---|
| 339 | client->cb->ev_pending(client->cb_arg);
|
|---|
| 340 |
|
|---|
| 341 | return EOK;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | /** Post unfocus event to the client's message queue.
|
|---|
| 345 | *
|
|---|
| 346 | * @param client Client
|
|---|
| 347 | * @param ewindow Window that the message is targetted to
|
|---|
| 348 | *
|
|---|
| 349 | * @return EOK on success or an error code
|
|---|
| 350 | */
|
|---|
| 351 | errno_t ds_client_post_unfocus_event(ds_client_t *client, ds_window_t *ewindow)
|
|---|
| 352 | {
|
|---|
| 353 | ds_window_ev_t *wevent;
|
|---|
| 354 |
|
|---|
| 355 | wevent = calloc(1, sizeof(ds_window_ev_t));
|
|---|
| 356 | if (wevent == NULL)
|
|---|
| 357 | return ENOMEM;
|
|---|
| 358 |
|
|---|
| 359 | wevent->window = ewindow;
|
|---|
| 360 | wevent->event.etype = wev_unfocus;
|
|---|
| 361 | list_append(&wevent->levents, &client->events);
|
|---|
| 362 |
|
|---|
| 363 | /* Notify the client */
|
|---|
| 364 | // TODO Do not send more than once until client drains the queue
|
|---|
| 365 | if (client->cb != NULL && client->cb->ev_pending != NULL)
|
|---|
| 366 | client->cb->ev_pending(client->cb_arg);
|
|---|
| 367 |
|
|---|
| 368 | return EOK;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | /** @}
|
|---|
| 372 | */
|
|---|