[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 | * @return EOK on success, ENOMEM if there are no free window identifiers
|
---|
| 96 | */
|
---|
| 97 | errno_t ds_client_add_window(ds_client_t *client, ds_window_t *wnd)
|
---|
| 98 | {
|
---|
| 99 | assert(wnd->client == NULL);
|
---|
| 100 | assert(!link_used(&wnd->lwindows));
|
---|
| 101 |
|
---|
| 102 | wnd->client = client;
|
---|
| 103 | wnd->id = client->display->next_wnd_id++;
|
---|
| 104 | list_append(&wnd->lwindows, &client->windows);
|
---|
| 105 |
|
---|
| 106 | return EOK;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /** Remove window from client.
|
---|
| 110 | *
|
---|
| 111 | * @param wnd Window
|
---|
| 112 | */
|
---|
| 113 | void ds_client_remove_window(ds_window_t *wnd)
|
---|
| 114 | {
|
---|
[cf32dbd] | 115 | ds_seat_t *seat;
|
---|
| 116 |
|
---|
| 117 | /* Make sure window is no longer focused in any seat */
|
---|
| 118 | seat = ds_display_first_seat(wnd->client->display);
|
---|
| 119 | while (seat != NULL) {
|
---|
| 120 | ds_seat_evac_focus(seat, wnd);
|
---|
| 121 | seat = ds_display_next_seat(seat);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[b3c185b6] | 124 | list_remove(&wnd->lwindows);
|
---|
| 125 | wnd->client = NULL;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /** Find window by ID.
|
---|
| 129 | *
|
---|
| 130 | * @param client Client
|
---|
| 131 | * @param id Window ID
|
---|
| 132 | */
|
---|
| 133 | #include <stdio.h>
|
---|
| 134 | ds_window_t *ds_client_find_window(ds_client_t *client, ds_wnd_id_t id)
|
---|
| 135 | {
|
---|
| 136 | ds_window_t *wnd;
|
---|
| 137 |
|
---|
| 138 | // TODO Make this faster
|
---|
[bf22cb78] | 139 | printf("ds_client_find_window: id=0x%x\n", (unsigned) id);
|
---|
[b3c185b6] | 140 | wnd = ds_client_first_window(client);
|
---|
| 141 | while (wnd != NULL) {
|
---|
[bf22cb78] | 142 | printf("ds_client_find_window: wnd=%p wnd->id=0x%x\n", wnd,
|
---|
| 143 | (unsigned) wnd->id);
|
---|
[b3c185b6] | 144 | if (wnd->id == id)
|
---|
| 145 | return wnd;
|
---|
| 146 | wnd = ds_client_next_window(wnd);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return NULL;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /** Get first window in client.
|
---|
| 153 | *
|
---|
| 154 | * @param client Client
|
---|
| 155 | * @return First window or @c NULL if there is none
|
---|
| 156 | */
|
---|
| 157 | ds_window_t *ds_client_first_window(ds_client_t *client)
|
---|
| 158 | {
|
---|
| 159 | link_t *link = list_first(&client->windows);
|
---|
| 160 |
|
---|
| 161 | if (link == NULL)
|
---|
| 162 | return NULL;
|
---|
| 163 |
|
---|
| 164 | return list_get_instance(link, ds_window_t, lwindows);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /** Get next window in client.
|
---|
| 168 | *
|
---|
| 169 | * @param wnd Current window
|
---|
| 170 | * @return Next window or @c NULL if there is none
|
---|
| 171 | */
|
---|
| 172 | ds_window_t *ds_client_next_window(ds_window_t *wnd)
|
---|
| 173 | {
|
---|
| 174 | link_t *link = list_next(&wnd->lwindows, &wnd->client->windows);
|
---|
| 175 |
|
---|
| 176 | if (link == NULL)
|
---|
| 177 | return NULL;
|
---|
| 178 |
|
---|
| 179 | return list_get_instance(link, ds_window_t, lwindows);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /** Get next event from client event queue.
|
---|
| 183 | *
|
---|
[e422ff6] | 184 | * @param client Client
|
---|
| 185 | * @param ewindow Place to store pointer to window receiving the event
|
---|
| 186 | * @param event Place to store event
|
---|
[b3c185b6] | 187 | * @return Graphic context
|
---|
| 188 | */
|
---|
| 189 | errno_t ds_client_get_event(ds_client_t *client, ds_window_t **ewindow,
|
---|
| 190 | display_wnd_ev_t *event)
|
---|
| 191 | {
|
---|
| 192 | link_t *link;
|
---|
| 193 | ds_window_ev_t *wevent;
|
---|
| 194 |
|
---|
[e422ff6] | 195 | link = list_first(&client->events);
|
---|
| 196 | if (link == NULL)
|
---|
| 197 | return ENOENT;
|
---|
[dcac756] | 198 |
|
---|
[b3c185b6] | 199 | wevent = list_get_instance(link, ds_window_ev_t, levents);
|
---|
[dcac756] | 200 | list_remove(link);
|
---|
[b3c185b6] | 201 |
|
---|
| 202 | *ewindow = wevent->window;
|
---|
| 203 | *event = wevent->event;
|
---|
| 204 | free(wevent);
|
---|
| 205 | return EOK;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[e422ff6] | 208 | /** Post keyboard event to the client's message queue.
|
---|
| 209 | *
|
---|
| 210 | * @param client Client
|
---|
| 211 | * @param ewindow Window that the message is targetted to
|
---|
| 212 | * @param event Event
|
---|
| 213 | *
|
---|
| 214 | * @return EOK on success or an error code
|
---|
| 215 | */
|
---|
[b3c185b6] | 216 | errno_t ds_client_post_kbd_event(ds_client_t *client, ds_window_t *ewindow,
|
---|
| 217 | kbd_event_t *event)
|
---|
| 218 | {
|
---|
| 219 | ds_window_ev_t *wevent;
|
---|
| 220 |
|
---|
| 221 | wevent = calloc(1, sizeof(ds_window_ev_t));
|
---|
| 222 | if (wevent == NULL)
|
---|
| 223 | return ENOMEM;
|
---|
| 224 |
|
---|
| 225 | wevent->window = ewindow;
|
---|
| 226 | wevent->event.kbd_event = *event;
|
---|
[e422ff6] | 227 | list_append(&wevent->levents, &client->events);
|
---|
[b3c185b6] | 228 |
|
---|
| 229 | /* Notify the client */
|
---|
| 230 | // TODO Do not send more than once until client drains the queue
|
---|
[be15256] | 231 | client->cb->ev_pending(client->cb_arg);
|
---|
[b3c185b6] | 232 |
|
---|
| 233 | return EOK;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /** @}
|
---|
| 237 | */
|
---|