1 | /*
|
---|
2 | * Copyright (c) 2023 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 CFG client
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <adt/list.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <io/log.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include "cfgclient.h"
|
---|
41 | #include "display.h"
|
---|
42 | #include "window.h"
|
---|
43 |
|
---|
44 | /** Create CFG client.
|
---|
45 | *
|
---|
46 | * @param display Parent display
|
---|
47 | * @param cb CFG client callbacks
|
---|
48 | * @param cb_arg Callback argument
|
---|
49 | * @param rcfgclient Place to store pointer to new CFG client.
|
---|
50 | * @return EOK on success, ENOMEM if out of memory
|
---|
51 | */
|
---|
52 | errno_t ds_cfgclient_create(ds_display_t *display, ds_cfgclient_cb_t *cb,
|
---|
53 | void *cb_arg, ds_cfgclient_t **rcfgclient)
|
---|
54 | {
|
---|
55 | ds_cfgclient_t *cfgclient;
|
---|
56 |
|
---|
57 | cfgclient = calloc(1, sizeof(ds_cfgclient_t));
|
---|
58 | if (cfgclient == NULL)
|
---|
59 | return ENOMEM;
|
---|
60 |
|
---|
61 | list_initialize(&cfgclient->events);
|
---|
62 | cfgclient->cb = cb;
|
---|
63 | cfgclient->cb_arg = cb_arg;
|
---|
64 |
|
---|
65 | ds_display_add_cfgclient(display, cfgclient);
|
---|
66 |
|
---|
67 | *rcfgclient = cfgclient;
|
---|
68 | return EOK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Destroy CFG client.
|
---|
72 | *
|
---|
73 | * @param cfgclient CFG client
|
---|
74 | */
|
---|
75 | void ds_cfgclient_destroy(ds_cfgclient_t *cfgclient)
|
---|
76 | {
|
---|
77 | ds_cfgclient_purge_events(cfgclient);
|
---|
78 | ds_display_remove_cfgclient(cfgclient);
|
---|
79 | free(cfgclient);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Get next event from CFG client event queue.
|
---|
83 | *
|
---|
84 | * @param cfgclient CFG client
|
---|
85 | * @param event Place to store event
|
---|
86 | * @return EOK on success, ENOENT if event queue is empty
|
---|
87 | */
|
---|
88 | errno_t ds_cfgclient_get_event(ds_cfgclient_t *cfgclient, dispcfg_ev_t *event)
|
---|
89 | {
|
---|
90 | link_t *link;
|
---|
91 | ds_cfgclient_ev_t *wevent;
|
---|
92 |
|
---|
93 | link = list_first(&cfgclient->events);
|
---|
94 | if (link == NULL)
|
---|
95 | return ENOENT;
|
---|
96 |
|
---|
97 | wevent = list_get_instance(link, ds_cfgclient_ev_t, levents);
|
---|
98 | list_remove(link);
|
---|
99 |
|
---|
100 | *event = wevent->event;
|
---|
101 | free(wevent);
|
---|
102 | return EOK;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /** Purge events from CFG client event queue.
|
---|
106 | *
|
---|
107 | * @param client Client
|
---|
108 | */
|
---|
109 | void ds_cfgclient_purge_events(ds_cfgclient_t *cfgclient)
|
---|
110 | {
|
---|
111 | link_t *cur;
|
---|
112 | link_t *next;
|
---|
113 | ds_cfgclient_ev_t *wevent;
|
---|
114 |
|
---|
115 | cur = list_first(&cfgclient->events);
|
---|
116 | while (cur != NULL) {
|
---|
117 | next = list_next(cur, &cfgclient->events);
|
---|
118 | wevent = list_get_instance(cur, ds_cfgclient_ev_t, levents);
|
---|
119 |
|
---|
120 | list_remove(cur);
|
---|
121 | free(wevent);
|
---|
122 | cur = next;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Post seat added event to the CFG client's message queue.
|
---|
127 | *
|
---|
128 | * @param cfgclient CFG client
|
---|
129 | * @param seat_id Seat ID of the added seat
|
---|
130 | *
|
---|
131 | * @return EOK on success or an error code
|
---|
132 | */
|
---|
133 | errno_t ds_cfgclient_post_seat_added_event(ds_cfgclient_t *cfgclient,
|
---|
134 | sysarg_t seat_id)
|
---|
135 | {
|
---|
136 | ds_cfgclient_ev_t *sevent;
|
---|
137 |
|
---|
138 | log_msg(LOG_DEFAULT, LVL_DEBUG, "cfgclient_pos_seat_added_event "
|
---|
139 | "cfgclient=%p seat_id=%zu\n", (void *)cfgclient, seat_id);
|
---|
140 |
|
---|
141 | sevent = calloc(1, sizeof(ds_cfgclient_ev_t));
|
---|
142 | if (sevent == NULL)
|
---|
143 | return ENOMEM;
|
---|
144 |
|
---|
145 | sevent->cfgclient = cfgclient;
|
---|
146 | sevent->event.etype = dcev_seat_added;
|
---|
147 | sevent->event.seat_id = seat_id;
|
---|
148 | list_append(&sevent->levents, &cfgclient->events);
|
---|
149 |
|
---|
150 | /* Notify the client */
|
---|
151 | // TODO Do not send more than once until client drains the queue
|
---|
152 | if (cfgclient->cb != NULL && cfgclient->cb->ev_pending != NULL)
|
---|
153 | cfgclient->cb->ev_pending(cfgclient->cb_arg);
|
---|
154 |
|
---|
155 | return EOK;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Post seat removed event to the CFG client's message queue.
|
---|
159 | *
|
---|
160 | * @param cfgclient CFG client
|
---|
161 | * @param seat_id Seat ID of the added seat
|
---|
162 | *
|
---|
163 | * @return EOK on success or an error code
|
---|
164 | */
|
---|
165 | errno_t ds_cfgclient_post_seat_removed_event(ds_cfgclient_t *cfgclient,
|
---|
166 | sysarg_t seat_id)
|
---|
167 | {
|
---|
168 | ds_cfgclient_ev_t *sevent;
|
---|
169 |
|
---|
170 | log_msg(LOG_DEFAULT, LVL_DEBUG, "cfgclient_pos_seat_removed_event "
|
---|
171 | "cfgclient=%p seat_id=%zu\n", (void *)cfgclient, seat_id);
|
---|
172 |
|
---|
173 | sevent = calloc(1, sizeof(ds_cfgclient_ev_t));
|
---|
174 | if (sevent == NULL)
|
---|
175 | return ENOMEM;
|
---|
176 |
|
---|
177 | sevent->cfgclient = cfgclient;
|
---|
178 | sevent->event.etype = dcev_seat_removed;
|
---|
179 | sevent->event.seat_id = seat_id;
|
---|
180 | list_append(&sevent->levents, &cfgclient->events);
|
---|
181 |
|
---|
182 | /* Notify the client */
|
---|
183 | // TODO Do not send more than once until client drains the queue
|
---|
184 | if (cfgclient->cb != NULL && cfgclient->cb->ev_pending != NULL)
|
---|
185 | cfgclient->cb->ev_pending(cfgclient->cb_arg);
|
---|
186 |
|
---|
187 | return EOK;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** @}
|
---|
191 | */
|
---|