| 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 |
|
|---|
| 137 | log_msg(LOG_DEFAULT, LVL_NOTE, "post wnd added event wmclient=%p\n",
|
|---|
| 138 | (void *)wmclient);
|
|---|
| 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 |
|
|---|
| 169 | wevent = calloc(1, sizeof(ds_wmclient_ev_t));
|
|---|
| 170 | if (wevent == NULL)
|
|---|
| 171 | return ENOMEM;
|
|---|
| 172 |
|
|---|
| 173 | wevent->wmclient = wmclient;
|
|---|
| 174 | wevent->event.etype = wmev_window_removed;
|
|---|
| 175 | wevent->event.wnd_id = wnd_id;
|
|---|
| 176 | list_append(&wevent->levents, &wmclient->events);
|
|---|
| 177 |
|
|---|
| 178 | /* Notify the client */
|
|---|
| 179 | // TODO Do not send more than once until client drains the queue
|
|---|
| 180 | if (wmclient->cb != NULL && wmclient->cb->ev_pending != NULL)
|
|---|
| 181 | wmclient->cb->ev_pending(wmclient->cb_arg);
|
|---|
| 182 |
|
|---|
| 183 | return EOK;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** @}
|
|---|
| 187 | */
|
|---|