[913add60] | 1 | /*
|
---|
| 2 | * Copyright (c) 2022 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 WM client
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <adt/list.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include "display.h"
|
---|
| 40 | #include "window.h"
|
---|
| 41 | #include "wmclient.h"
|
---|
| 42 |
|
---|
| 43 | /** Create WM client.
|
---|
| 44 | *
|
---|
| 45 | * @param display Parent display
|
---|
| 46 | * @param cb WM client callbacks
|
---|
| 47 | * @param cb_arg Callback argument
|
---|
| 48 | * @param rwmclient Place to store pointer to new WM client.
|
---|
| 49 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 50 | */
|
---|
| 51 | errno_t ds_wmclient_create(ds_display_t *display, ds_wmclient_cb_t *cb,
|
---|
| 52 | void *cb_arg, ds_wmclient_t **rwmclient)
|
---|
| 53 | {
|
---|
| 54 | ds_wmclient_t *wmclient;
|
---|
| 55 |
|
---|
| 56 | wmclient = calloc(1, sizeof(ds_wmclient_t));
|
---|
| 57 | if (wmclient == NULL)
|
---|
| 58 | return ENOMEM;
|
---|
| 59 |
|
---|
| 60 | list_initialize(&wmclient->events);
|
---|
| 61 | wmclient->cb = cb;
|
---|
| 62 | wmclient->cb_arg = cb_arg;
|
---|
| 63 |
|
---|
| 64 | ds_display_add_wmclient(display, wmclient);
|
---|
| 65 |
|
---|
| 66 | *rwmclient = wmclient;
|
---|
| 67 | return EOK;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /** Destroy WM client.
|
---|
| 71 | *
|
---|
| 72 | * @param wmclient WM client
|
---|
| 73 | */
|
---|
| 74 | void ds_wmclient_destroy(ds_wmclient_t *wmclient)
|
---|
| 75 | {
|
---|
| 76 | ds_wmclient_purge_events(wmclient);
|
---|
| 77 | ds_display_remove_wmclient(wmclient);
|
---|
| 78 | free(wmclient);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /** Get next event from WM client event queue.
|
---|
| 82 | *
|
---|
| 83 | * @param wmclient WM client
|
---|
| 84 | * @param event Place to store event
|
---|
| 85 | * @return EOK on success, ENOENT if event queue is empty
|
---|
| 86 | */
|
---|
| 87 | errno_t ds_wmclient_get_event(ds_wmclient_t *wmclient, wndmgt_ev_t *event)
|
---|
| 88 | {
|
---|
| 89 | link_t *link;
|
---|
| 90 | ds_wmclient_ev_t *wevent;
|
---|
| 91 |
|
---|
| 92 | link = list_first(&wmclient->events);
|
---|
| 93 | if (link == NULL)
|
---|
| 94 | return ENOENT;
|
---|
| 95 |
|
---|
| 96 | wevent = list_get_instance(link, ds_wmclient_ev_t, levents);
|
---|
| 97 | list_remove(link);
|
---|
| 98 |
|
---|
| 99 | *event = wevent->event;
|
---|
| 100 | free(wevent);
|
---|
| 101 | return EOK;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Purge events from WM client event queue.
|
---|
| 105 | *
|
---|
| 106 | * @param client Client
|
---|
| 107 | */
|
---|
| 108 | void ds_wmclient_purge_events(ds_wmclient_t *wmclient)
|
---|
| 109 | {
|
---|
| 110 | link_t *cur;
|
---|
| 111 | link_t *next;
|
---|
| 112 | ds_wmclient_ev_t *wevent;
|
---|
| 113 |
|
---|
| 114 | cur = list_first(&wmclient->events);
|
---|
| 115 | while (cur != NULL) {
|
---|
| 116 | next = list_next(cur, &wmclient->events);
|
---|
| 117 | wevent = list_get_instance(cur, ds_wmclient_ev_t, levents);
|
---|
| 118 |
|
---|
| 119 | list_remove(cur);
|
---|
| 120 | free(wevent);
|
---|
| 121 | cur = next;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | #include <io/log.h>
|
---|
| 125 | /** Post window added event to the WM client's message queue.
|
---|
| 126 | *
|
---|
| 127 | * @param wmclient WM client
|
---|
| 128 | * @param wnd_id Window ID of the added window
|
---|
| 129 | *
|
---|
| 130 | * @return EOK on success or an error code
|
---|
| 131 | */
|
---|
| 132 | errno_t ds_wmclient_post_wnd_added_event(ds_wmclient_t *wmclient,
|
---|
| 133 | sysarg_t wnd_id)
|
---|
| 134 | {
|
---|
| 135 | ds_wmclient_ev_t *wevent;
|
---|
| 136 |
|
---|
[d19d15b] | 137 | log_msg(LOG_DEFAULT, LVL_DEBUG, "wmclient_pos_wnd_added_event "
|
---|
| 138 | "wmclient=%p wnd_id=%zu\n", (void *)wmclient, wnd_id);
|
---|
[913add60] | 139 |
|
---|
| 140 | wevent = calloc(1, sizeof(ds_wmclient_ev_t));
|
---|
| 141 | if (wevent == NULL)
|
---|
| 142 | return ENOMEM;
|
---|
| 143 |
|
---|
| 144 | wevent->wmclient = wmclient;
|
---|
| 145 | wevent->event.etype = wmev_window_added;
|
---|
| 146 | wevent->event.wnd_id = wnd_id;
|
---|
| 147 | list_append(&wevent->levents, &wmclient->events);
|
---|
| 148 |
|
---|
| 149 | /* Notify the client */
|
---|
| 150 | // TODO Do not send more than once until client drains the queue
|
---|
| 151 | if (wmclient->cb != NULL && wmclient->cb->ev_pending != NULL)
|
---|
| 152 | wmclient->cb->ev_pending(wmclient->cb_arg);
|
---|
| 153 |
|
---|
| 154 | return EOK;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /** Post window removed event to the WM client's message queue.
|
---|
| 158 | *
|
---|
| 159 | * @param wmclient WM client
|
---|
| 160 | * @param wnd_id Window ID of the added window
|
---|
| 161 | *
|
---|
| 162 | * @return EOK on success or an error code
|
---|
| 163 | */
|
---|
| 164 | errno_t ds_wmclient_post_wnd_removed_event(ds_wmclient_t *wmclient,
|
---|
| 165 | sysarg_t wnd_id)
|
---|
| 166 | {
|
---|
| 167 | ds_wmclient_ev_t *wevent;
|
---|
| 168 |
|
---|
[d19d15b] | 169 | log_msg(LOG_DEFAULT, LVL_DEBUG, "wmclient_pos_wnd_removed_event "
|
---|
| 170 | "wmclient=%p wnd_id=%zu\n", (void *)wmclient, wnd_id);
|
---|
| 171 |
|
---|
[913add60] | 172 | wevent = calloc(1, sizeof(ds_wmclient_ev_t));
|
---|
| 173 | if (wevent == NULL)
|
---|
| 174 | return ENOMEM;
|
---|
| 175 |
|
---|
| 176 | wevent->wmclient = wmclient;
|
---|
| 177 | wevent->event.etype = wmev_window_removed;
|
---|
| 178 | wevent->event.wnd_id = wnd_id;
|
---|
| 179 | list_append(&wevent->levents, &wmclient->events);
|
---|
| 180 |
|
---|
| 181 | /* Notify the client */
|
---|
| 182 | // TODO Do not send more than once until client drains the queue
|
---|
| 183 | if (wmclient->cb != NULL && wmclient->cb->ev_pending != NULL)
|
---|
| 184 | wmclient->cb->ev_pending(wmclient->cb_arg);
|
---|
| 185 |
|
---|
| 186 | return EOK;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[f1f433d] | 189 | /** Post window changed event to the WM client's message queue.
|
---|
| 190 | *
|
---|
| 191 | * @param wmclient WM client
|
---|
| 192 | * @param wnd_id Window ID of the added window
|
---|
| 193 | *
|
---|
| 194 | * @return EOK on success or an error code
|
---|
| 195 | */
|
---|
| 196 | errno_t ds_wmclient_post_wnd_changed_event(ds_wmclient_t *wmclient,
|
---|
| 197 | sysarg_t wnd_id)
|
---|
| 198 | {
|
---|
| 199 | ds_wmclient_ev_t *wevent;
|
---|
| 200 |
|
---|
[d19d15b] | 201 | log_msg(LOG_DEFAULT, LVL_DEBUG, "wmclient_pos_wnd_changed_event "
|
---|
| 202 | "wmclient=%p wnd_id=%zu\n", (void *)wmclient, wnd_id);
|
---|
| 203 |
|
---|
[f1f433d] | 204 | wevent = calloc(1, sizeof(ds_wmclient_ev_t));
|
---|
| 205 | if (wevent == NULL)
|
---|
| 206 | return ENOMEM;
|
---|
| 207 |
|
---|
| 208 | wevent->wmclient = wmclient;
|
---|
| 209 | wevent->event.etype = wmev_window_changed;
|
---|
| 210 | wevent->event.wnd_id = wnd_id;
|
---|
| 211 | list_append(&wevent->levents, &wmclient->events);
|
---|
| 212 |
|
---|
| 213 | /* Notify the client */
|
---|
| 214 | // TODO Do not send more than once until client drains the queue
|
---|
| 215 | if (wmclient->cb != NULL && wmclient->cb->ev_pending != NULL)
|
---|
| 216 | wmclient->cb->ev_pending(wmclient->cb_arg);
|
---|
| 217 |
|
---|
| 218 | return EOK;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[913add60] | 221 | /** @}
|
---|
| 222 | */
|
---|