source: mainline/uspace/lib/wndmgt/src/wndmgt_srv.c@ f1f433d

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f1f433d was 0761448, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Window management protocol library

This allows the client to enumerate windows, activate or close a window.

  • Property mode set to 100644
File size: 7.7 KB
Line 
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 libwndmgt
30 * @{
31 */
32/**
33 * @file
34 * @brief Window management protocol server stub
35 */
36
37#include <wndmgt_srv.h>
38#include <errno.h>
39#include <io/log.h>
40#include <ipc/wndmgt.h>
41#include <mem.h>
42#include <stdlib.h>
43#include <stddef.h>
44#include <str.h>
45#include <wndmgt.h>
46#include "../private/wndmgt.h"
47
48static void wndmgt_callback_create_srv(wndmgt_srv_t *srv, ipc_call_t *call)
49{
50 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
51 if (sess == NULL) {
52 async_answer_0(call, ENOMEM);
53 return;
54 }
55
56 srv->client_sess = sess;
57 async_answer_0(call, EOK);
58}
59
60static void wndmgt_get_window_list_srv(wndmgt_srv_t *srv, ipc_call_t *icall)
61{
62 ipc_call_t call;
63 wndmgt_window_list_t *list = NULL;
64 size_t size;
65 errno_t rc;
66
67 if (srv->ops->get_window_list == NULL) {
68 async_answer_0(icall, ENOTSUP);
69 return;
70 }
71
72 rc = srv->ops->get_window_list(srv->arg, &list);
73 if (rc != EOK) {
74 async_answer_0(&call, rc);
75 async_answer_0(icall, rc);
76 return;
77 }
78
79 /* Send list size */
80
81 if (!async_data_read_receive(&call, &size)) {
82 wndmgt_free_window_list(list);
83 async_answer_0(icall, EREFUSED);
84 return;
85 }
86
87 if (size != sizeof(list->nwindows)) {
88 wndmgt_free_window_list(list);
89 async_answer_0(&call, EINVAL);
90 async_answer_0(icall, EINVAL);
91 return;
92 }
93
94 rc = async_data_read_finalize(&call, &list->nwindows, size);
95 if (rc != EOK) {
96 wndmgt_free_window_list(list);
97 async_answer_0(&call, rc);
98 async_answer_0(icall, rc);
99 return;
100 }
101
102 /* Send window list */
103
104 if (!async_data_read_receive(&call, &size)) {
105 wndmgt_free_window_list(list);
106 async_answer_0(icall, EREFUSED);
107 return;
108 }
109
110 if (size != list->nwindows * sizeof(sysarg_t)) {
111 wndmgt_free_window_list(list);
112 async_answer_0(&call, EINVAL);
113 async_answer_0(icall, EINVAL);
114 return;
115 }
116
117 rc = async_data_read_finalize(&call, list->windows, size);
118 if (rc != EOK) {
119 wndmgt_free_window_list(list);
120 async_answer_0(&call, rc);
121 async_answer_0(icall, rc);
122 return;
123 }
124
125 async_answer_0(icall, EOK);
126 wndmgt_free_window_list(list);
127}
128
129static void wndmgt_get_window_info_srv(wndmgt_srv_t *srv, ipc_call_t *icall)
130{
131 sysarg_t wnd_id;
132 ipc_call_t call;
133 wndmgt_window_info_t *info = NULL;
134 size_t capsize;
135 size_t size;
136 errno_t rc;
137
138 wnd_id = ipc_get_arg1(icall);
139
140 if (srv->ops->get_window_info == NULL) {
141 async_answer_0(icall, ENOTSUP);
142 return;
143 }
144
145 rc = srv->ops->get_window_info(srv->arg, wnd_id, &info);
146 if (rc != EOK) {
147 async_answer_0(&call, rc);
148 async_answer_0(icall, rc);
149 return;
150 }
151
152 /* Send caption size */
153
154 if (!async_data_read_receive(&call, &size)) {
155 wndmgt_free_window_info(info);
156 async_answer_0(icall, EREFUSED);
157 return;
158 }
159
160 if (size != sizeof(size_t)) {
161 wndmgt_free_window_info(info);
162 async_answer_0(&call, EINVAL);
163 async_answer_0(icall, EINVAL);
164 return;
165 }
166
167 capsize = str_size(info->caption);
168
169 rc = async_data_read_finalize(&call, &capsize, size);
170 if (rc != EOK) {
171 wndmgt_free_window_info(info);
172 async_answer_0(&call, rc);
173 async_answer_0(icall, rc);
174 return;
175 }
176
177 /* Send caption */
178
179 if (!async_data_read_receive(&call, &size)) {
180 wndmgt_free_window_info(info);
181 async_answer_0(icall, EREFUSED);
182 return;
183 }
184
185 if (size != capsize) {
186 wndmgt_free_window_info(info);
187 async_answer_0(&call, EINVAL);
188 async_answer_0(icall, EINVAL);
189 return;
190 }
191
192 rc = async_data_read_finalize(&call, info->caption, size);
193 if (rc != EOK) {
194 wndmgt_free_window_info(info);
195 async_answer_0(&call, rc);
196 async_answer_0(icall, rc);
197 return;
198 }
199
200 async_answer_0(icall, EOK);
201 wndmgt_free_window_info(info);
202}
203
204static void wndmgt_activate_window_srv(wndmgt_srv_t *srv, ipc_call_t *icall)
205{
206 sysarg_t wnd_id;
207 errno_t rc;
208
209 wnd_id = ipc_get_arg1(icall);
210
211 if (srv->ops->activate_window == NULL) {
212 async_answer_0(icall, ENOTSUP);
213 return;
214 }
215
216 rc = srv->ops->activate_window(srv->arg, wnd_id);
217 async_answer_0(icall, rc);
218}
219
220static void wndmgt_close_window_srv(wndmgt_srv_t *srv, ipc_call_t *icall)
221{
222 sysarg_t wnd_id;
223 errno_t rc;
224
225 wnd_id = ipc_get_arg1(icall);
226
227 if (srv->ops->activate_window == NULL) {
228 async_answer_0(icall, ENOTSUP);
229 return;
230 }
231
232 rc = srv->ops->close_window(srv->arg, wnd_id);
233 async_answer_0(icall, rc);
234}
235
236static void wndmgt_get_event_srv(wndmgt_srv_t *srv, ipc_call_t *icall)
237{
238 wndmgt_ev_t event;
239 ipc_call_t call;
240 size_t size;
241 errno_t rc;
242
243 if (srv->ops->get_event == NULL) {
244 async_answer_0(icall, ENOTSUP);
245 return;
246 }
247
248 rc = srv->ops->get_event(srv->arg, &event);
249 if (rc != EOK) {
250 async_answer_0(icall, rc);
251 return;
252 }
253
254 /* Transfer event data */
255 if (!async_data_read_receive(&call, &size)) {
256 async_answer_0(icall, EREFUSED);
257 return;
258 }
259
260 if (size != sizeof(event)) {
261 async_answer_0(icall, EREFUSED);
262 async_answer_0(&call, EREFUSED);
263 return;
264 }
265
266 rc = async_data_read_finalize(&call, &event, sizeof(event));
267 if (rc != EOK) {
268 async_answer_0(icall, rc);
269 async_answer_0(&call, rc);
270 return;
271 }
272
273 async_answer_0(icall, EOK);
274}
275
276void wndmgt_conn(ipc_call_t *icall, wndmgt_srv_t *srv)
277{
278 /* Accept the connection */
279 async_accept_0(icall);
280
281 while (true) {
282 ipc_call_t call;
283
284 async_get_call(&call);
285 sysarg_t method = ipc_get_imethod(&call);
286
287 if (!method) {
288 /* The other side has hung up */
289 async_answer_0(&call, EOK);
290 break;
291 }
292
293 switch (method) {
294 case WNDMGT_CALLBACK_CREATE:
295 wndmgt_callback_create_srv(srv, &call);
296 break;
297 case WNDMGT_GET_WINDOW_LIST:
298 wndmgt_get_window_list_srv(srv, &call);
299 break;
300 case WNDMGT_GET_WINDOW_INFO:
301 wndmgt_get_window_info_srv(srv, &call);
302 break;
303 case WNDMGT_ACTIVATE_WINDOW:
304 wndmgt_activate_window_srv(srv, &call);
305 break;
306 case WNDMGT_CLOSE_WINDOW:
307 wndmgt_close_window_srv(srv, &call);
308 break;
309 case WNDMGT_GET_EVENT:
310 wndmgt_get_event_srv(srv, &call);
311 break;
312 default:
313 async_answer_0(&call, ENOTSUP);
314 }
315 }
316
317 /* Hang up callback session */
318 if (srv->client_sess != NULL) {
319 async_hangup(srv->client_sess);
320 srv->client_sess = NULL;
321 }
322}
323
324/** Initialize window management server structure
325 *
326 * @param srv Window management server structure to initialize
327 */
328void wndmgt_srv_initialize(wndmgt_srv_t *srv)
329{
330 memset(srv, 0, sizeof(*srv));
331}
332
333/** Send 'pending' event to client.
334 *
335 * @param srv Window management server structure
336 */
337void wndmgt_srv_ev_pending(wndmgt_srv_t *srv)
338{
339 async_exch_t *exch;
340
341 exch = async_exchange_begin(srv->client_sess);
342 async_msg_0(exch, WNDMGT_EV_PENDING);
343 async_exchange_end(exch);
344}
345
346/** @}
347 */
Note: See TracBrowser for help on using the repository browser.