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