source: mainline/uspace/srv/hid/display/cfgops.c@ a77c722

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

Refuse to delete the last seat

  • Property mode set to 100644
File size: 9.6 KB
Line 
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 configuration ops implementation
34 */
35
36#include <errno.h>
37#include <io/log.h>
38#include <stdlib.h>
39#include <str.h>
40#include <dispcfg_srv.h>
41#include "display.h"
42#include "idevcfg.h"
43#include "seat.h"
44#include "cfgclient.h"
45
46static errno_t dispc_get_seat_list(void *, dispcfg_seat_list_t **);
47static errno_t dispc_get_seat_info(void *, sysarg_t, dispcfg_seat_info_t **);
48static errno_t dispc_seat_create(void *, const char *, sysarg_t *);
49static errno_t dispc_seat_delete(void *, sysarg_t);
50static errno_t dispc_dev_assign(void *, sysarg_t, sysarg_t);
51static errno_t dispc_dev_unassign(void *, sysarg_t);
52static errno_t dispc_get_asgn_dev_list(void *, sysarg_t, dispcfg_dev_list_t **);
53static errno_t dispc_get_event(void *, dispcfg_ev_t *);
54
55dispcfg_ops_t dispcfg_srv_ops = {
56 .get_seat_list = dispc_get_seat_list,
57 .get_seat_info = dispc_get_seat_info,
58 .seat_create = dispc_seat_create,
59 .seat_delete = dispc_seat_delete,
60 .dev_assign = dispc_dev_assign,
61 .dev_unassign = dispc_dev_unassign,
62 .get_asgn_dev_list = dispc_get_asgn_dev_list,
63 .get_event = dispc_get_event,
64};
65
66/** Get seat list.
67 *
68 * @param arg Argument (CFG client)
69 * @param rlist Place to store pointer to new list
70 * @return EOK on success or an error code
71 */
72static errno_t dispc_get_seat_list(void *arg, dispcfg_seat_list_t **rlist)
73{
74 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
75 dispcfg_seat_list_t *list;
76 ds_seat_t *seat;
77 unsigned i;
78
79 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_get_seat_list()");
80
81 list = calloc(1, sizeof(dispcfg_seat_list_t));
82 if (list == NULL)
83 return ENOMEM;
84
85 ds_display_lock(cfgclient->display);
86
87 /* Count the number of seats */
88 list->nseats = 0;
89 seat = ds_display_first_seat(cfgclient->display);
90 while (seat != NULL) {
91 ++list->nseats;
92 seat = ds_display_next_seat(seat);
93 }
94
95 /* Allocate array for seat IDs */
96 list->seats = calloc(list->nseats, sizeof(sysarg_t));
97 if (list->seats == NULL) {
98 ds_display_unlock(cfgclient->display);
99 free(list);
100 return ENOMEM;
101 }
102
103 /* Fill in seat IDs */
104 i = 0;
105 seat = ds_display_first_seat(cfgclient->display);
106 while (seat != NULL) {
107 list->seats[i++] = seat->id;
108 seat = ds_display_next_seat(seat);
109 }
110
111 ds_display_unlock(cfgclient->display);
112 *rlist = list;
113 return EOK;
114}
115
116/** Get seat information.
117 *
118 * @param arg Argument (CFG client)
119 * @param seat_id Seat ID
120 * @param rinfo Place to store pointer to new seat information structure
121 * @return EOK on success or an error code
122 */
123static errno_t dispc_get_seat_info(void *arg, sysarg_t seat_id,
124 dispcfg_seat_info_t **rinfo)
125{
126 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
127 ds_seat_t *seat;
128 dispcfg_seat_info_t *info;
129
130 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_get_seat_info()");
131
132 ds_display_lock(cfgclient->display);
133 seat = ds_display_find_seat(cfgclient->display, seat_id);
134 if (seat == NULL) {
135 ds_display_unlock(cfgclient->display);
136 return ENOENT;
137 }
138
139 info = calloc(1, sizeof(dispcfg_seat_info_t));
140 if (info == NULL) {
141 ds_display_unlock(cfgclient->display);
142 return ENOMEM;
143 }
144
145 (void)seat;
146 info->name = str_dup(seat->name);
147 if (info->name == NULL) {
148 ds_display_unlock(cfgclient->display);
149 free(info);
150 return ENOMEM;
151 }
152
153 ds_display_unlock(cfgclient->display);
154 *rinfo = info;
155 return EOK;
156}
157
158/** Create seat.
159 *
160 * @param arg Argument (CFG client)
161 * @param name Seat name
162 * @param rseat_id Place to store ID of the new seat
163 * @return EOK on success or an error code
164 */
165static errno_t dispc_seat_create(void *arg, const char *name,
166 sysarg_t *rseat_id)
167{
168 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
169 ds_seat_t *seat;
170 errno_t rc;
171
172 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_seat_create()");
173
174 ds_display_lock(cfgclient->display);
175
176 rc = ds_seat_create(cfgclient->display, name, &seat);
177 if (rc != EOK) {
178 ds_display_unlock(cfgclient->display);
179 return rc;
180 }
181
182 (void) ds_display_paint(cfgclient->display, NULL);
183 ds_display_unlock(cfgclient->display);
184
185 *rseat_id = seat->id;
186 return EOK;
187}
188
189/** Delete seat.
190 *
191 * @param arg Argument (CFG client)
192 * @param seat_id Seat ID
193 * @return EOK on success or an error code
194 */
195static errno_t dispc_seat_delete(void *arg, sysarg_t seat_id)
196{
197 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
198 ds_seat_t *seat;
199 ds_seat_t *s;
200
201 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_seat_delete()");
202
203 ds_display_lock(cfgclient->display);
204 seat = ds_display_find_seat(cfgclient->display, seat_id);
205 if (seat == NULL) {
206 ds_display_unlock(cfgclient->display);
207 return ENOENT;
208 }
209
210 /* Verify we are not deleting the last seat */
211 s = ds_display_first_seat(cfgclient->display);
212 if (s == seat && ds_display_next_seat(s) == NULL) {
213 ds_display_unlock(cfgclient->display);
214 return EBUSY;
215 }
216
217 ds_seat_destroy(seat);
218
219 (void) ds_display_paint(cfgclient->display, NULL);
220 ds_display_unlock(cfgclient->display);
221 return EOK;
222}
223
224/** Assign device to seat.
225 *
226 * @param arg Argument (CFG client)
227 * @param svc_id Device service ID
228 * @param seat_id Seat ID
229 * @return EOK on success or an error code
230 */
231static errno_t dispc_dev_assign(void *arg, sysarg_t svc_id, sysarg_t seat_id)
232{
233 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
234 ds_seat_t *seat;
235 ds_idevcfg_t *idevcfg;
236 errno_t rc;
237
238 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_dev_assign()");
239
240 ds_display_lock(cfgclient->display);
241 seat = ds_display_find_seat(cfgclient->display, seat_id);
242 if (seat == NULL) {
243 ds_display_unlock(cfgclient->display);
244 return ENOENT;
245 }
246
247 rc = ds_idevcfg_create(cfgclient->display, svc_id, seat, &idevcfg);
248 if (rc != EOK) {
249 assert(rc == ENOMEM);
250 ds_display_unlock(cfgclient->display);
251 return ENOMEM;
252 }
253
254 (void)idevcfg;
255
256 ds_display_unlock(cfgclient->display);
257 return EOK;
258}
259
260/** Unassign device from any seat.
261 *
262 * @param arg Argument (CFG client)
263 * @param svc_id Device service ID
264 * @return EOK on success or an error code
265 */
266static errno_t dispc_dev_unassign(void *arg, sysarg_t svc_id)
267{
268 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
269 ds_idevcfg_t *idevcfg;
270
271 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_dev_unassign()");
272
273 ds_display_lock(cfgclient->display);
274
275 idevcfg = ds_display_first_idevcfg(cfgclient->display);
276 while (idevcfg != NULL) {
277 if (idevcfg->svc_id == svc_id)
278 break;
279
280 idevcfg = ds_display_next_idevcfg(idevcfg);
281 }
282
283 if (idevcfg == NULL) {
284 ds_display_unlock(cfgclient->display);
285 return ENOENT;
286 }
287
288 ds_idevcfg_destroy(idevcfg);
289 ds_display_unlock(cfgclient->display);
290 return EOK;
291}
292
293/** Get assigned device list.
294 *
295 * @param arg Argument (CFG client)
296 * @param seat_id Seat ID
297 * @param rlist Place to store pointer to new list
298 * @return EOK on success or an error code
299 */
300static errno_t dispc_get_asgn_dev_list(void *arg, sysarg_t seat_id,
301 dispcfg_dev_list_t **rlist)
302{
303 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
304 dispcfg_dev_list_t *list;
305 ds_seat_t *seat;
306 ds_idevcfg_t *idevcfg;
307 unsigned i;
308
309 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_get_asgn_dev_list()");
310
311 list = calloc(1, sizeof(dispcfg_dev_list_t));
312 if (list == NULL)
313 return ENOMEM;
314
315 ds_display_lock(cfgclient->display);
316
317 seat = ds_display_find_seat(cfgclient->display, seat_id);
318 if (seat == NULL) {
319 ds_display_unlock(cfgclient->display);
320 free(list);
321 return ENOENT;
322 }
323
324 /* Count the number of devices */
325 list->ndevs = 0;
326 idevcfg = ds_seat_first_idevcfg(seat);
327 while (idevcfg != NULL) {
328 ++list->ndevs;
329 idevcfg = ds_display_next_idevcfg(idevcfg);
330 }
331
332 /* Allocate array for device IDs */
333 list->devs = calloc(list->ndevs, sizeof(sysarg_t));
334 if (list->devs == NULL) {
335 ds_display_unlock(cfgclient->display);
336 free(list);
337 return ENOMEM;
338 }
339
340 /* Fill in device IDs */
341 i = 0;
342 idevcfg = ds_seat_first_idevcfg(seat);
343 while (idevcfg != NULL) {
344 list->devs[i++] = idevcfg->svc_id;
345 idevcfg = ds_display_next_idevcfg(idevcfg);
346 }
347
348 ds_display_unlock(cfgclient->display);
349 *rlist = list;
350 return EOK;
351}
352
353/** Get display configuration event.
354 *
355 * @param arg Argument (CFG client)
356 * @param ev Place to store event
357 * @return EOK on success, ENOENT if there are no events
358 */
359static errno_t dispc_get_event(void *arg, dispcfg_ev_t *ev)
360{
361 ds_cfgclient_t *cfgclient = (ds_cfgclient_t *)arg;
362 errno_t rc;
363
364 log_msg(LOG_DEFAULT, LVL_DEBUG, "dispcfg_get_event()");
365
366 ds_display_lock(cfgclient->display);
367 rc = ds_cfgclient_get_event(cfgclient, ev);
368 ds_display_unlock(cfgclient->display);
369 return rc;
370}
371
372/** @}
373 */
Note: See TracBrowser for help on using the repository browser.