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 disp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Display configuration utility.
|
---|
33 | *
|
---|
34 | * Configures the display service.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <dispcfg.h>
|
---|
39 | #include <io/table.h>
|
---|
40 | #include <loc.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <stdint.h>
|
---|
44 | #include <str.h>
|
---|
45 | #include <str_error.h>
|
---|
46 |
|
---|
47 | #define NAME "disp"
|
---|
48 |
|
---|
49 | static void print_syntax(void)
|
---|
50 | {
|
---|
51 | printf("%s: Display configuration utility.\n", NAME);
|
---|
52 | printf("Syntax:\n");
|
---|
53 | printf(" %s list-seat\n", NAME);
|
---|
54 | printf(" %s create-seat <name>\n", NAME);
|
---|
55 | printf(" %s delete-seat <name>\n", NAME);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /** Find seat by name.
|
---|
59 | *
|
---|
60 | * @param dispcfg Display configuration
|
---|
61 | * @param name Seat name
|
---|
62 | * @param rseat_id Place to store seat ID
|
---|
63 | * @return EOK on success or an error code
|
---|
64 | */
|
---|
65 | static errno_t seat_find_by_name(dispcfg_t *dispcfg, const char *name,
|
---|
66 | sysarg_t *rseat_id)
|
---|
67 | {
|
---|
68 | dispcfg_seat_list_t *seat_list;
|
---|
69 | dispcfg_seat_info_t *sinfo;
|
---|
70 |
|
---|
71 | size_t i;
|
---|
72 | errno_t rc;
|
---|
73 |
|
---|
74 | rc = dispcfg_get_seat_list(dispcfg, &seat_list);
|
---|
75 | if (rc != EOK) {
|
---|
76 | printf(NAME ": Failed getting seat list.\n");
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | for (i = 0; i < seat_list->nseats; i++) {
|
---|
81 | rc = dispcfg_get_seat_info(dispcfg, seat_list->seats[i],
|
---|
82 | &sinfo);
|
---|
83 | if (rc != EOK)
|
---|
84 | continue;
|
---|
85 |
|
---|
86 | if (str_cmp(sinfo->name, name) == 0) {
|
---|
87 | *rseat_id = seat_list->seats[i];
|
---|
88 | dispcfg_free_seat_info(sinfo);
|
---|
89 | return EOK;
|
---|
90 | }
|
---|
91 |
|
---|
92 | dispcfg_free_seat_info(sinfo);
|
---|
93 | }
|
---|
94 |
|
---|
95 | return ENOENT;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Create seat subcommand.
|
---|
99 | *
|
---|
100 | * @param dcfg_svc Display configuration service name
|
---|
101 | * @param argc Number of arguments
|
---|
102 | * @param argv Arguments
|
---|
103 | * @return EOK on success or an erro code
|
---|
104 | */
|
---|
105 | static errno_t create_seat(const char *dcfg_svc, int argc, char *argv[])
|
---|
106 | {
|
---|
107 | dispcfg_t *dispcfg;
|
---|
108 | char *seat_name;
|
---|
109 | sysarg_t seat_id;
|
---|
110 | errno_t rc;
|
---|
111 |
|
---|
112 | if (argc < 1) {
|
---|
113 | printf(NAME ": Missing arguments.\n");
|
---|
114 | print_syntax();
|
---|
115 | return EINVAL;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (argc > 1) {
|
---|
119 | printf(NAME ": Too many arguments.\n");
|
---|
120 | print_syntax();
|
---|
121 | return EINVAL;
|
---|
122 | }
|
---|
123 |
|
---|
124 | seat_name = argv[0];
|
---|
125 |
|
---|
126 | rc = dispcfg_open(dcfg_svc, NULL, NULL, &dispcfg);
|
---|
127 | if (rc != EOK) {
|
---|
128 | printf(NAME ": Failed connecting to display configuration "
|
---|
129 | "service: %s.\n", str_error(rc));
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 |
|
---|
133 | rc = dispcfg_seat_create(dispcfg, seat_name, &seat_id);
|
---|
134 | if (rc != EOK) {
|
---|
135 | printf(NAME ": Failed creating seat '%s' (%s)\n",
|
---|
136 | seat_name, str_error(rc));
|
---|
137 | dispcfg_close(dispcfg);
|
---|
138 | return EIO;
|
---|
139 | }
|
---|
140 |
|
---|
141 | (void)seat_id;
|
---|
142 | dispcfg_close(dispcfg);
|
---|
143 | return EOK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** Delete seat subcommand.
|
---|
147 | *
|
---|
148 | * @param dcfg_svc Display configuration service name
|
---|
149 | * @param argc Number of arguments
|
---|
150 | * @param argv Arguments
|
---|
151 | * @return EOK on success or an erro code
|
---|
152 | */
|
---|
153 | static errno_t delete_seat(const char *dcfg_svc, int argc, char *argv[])
|
---|
154 | {
|
---|
155 | dispcfg_t *dispcfg;
|
---|
156 | char *seat_name;
|
---|
157 | sysarg_t seat_id;
|
---|
158 | errno_t rc;
|
---|
159 |
|
---|
160 | if (argc < 1) {
|
---|
161 | printf(NAME ": Missing arguments.\n");
|
---|
162 | print_syntax();
|
---|
163 | return EINVAL;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (argc > 1) {
|
---|
167 | printf(NAME ": Too many arguments.\n");
|
---|
168 | print_syntax();
|
---|
169 | return EINVAL;
|
---|
170 | }
|
---|
171 |
|
---|
172 | seat_name = argv[0];
|
---|
173 |
|
---|
174 | rc = dispcfg_open(dcfg_svc, NULL, NULL, &dispcfg);
|
---|
175 | if (rc != EOK) {
|
---|
176 | printf(NAME ": Failed connecting to display configuration "
|
---|
177 | "service: %s.\n", str_error(rc));
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | rc = seat_find_by_name(dispcfg, seat_name, &seat_id);
|
---|
182 | if (rc != EOK) {
|
---|
183 | printf(NAME ": Seat '%s' not found.\n", seat_name);
|
---|
184 | dispcfg_close(dispcfg);
|
---|
185 | return ENOENT;
|
---|
186 | }
|
---|
187 |
|
---|
188 | rc = dispcfg_seat_delete(dispcfg, seat_id);
|
---|
189 | if (rc != EOK) {
|
---|
190 | printf(NAME ": Failed deleting seat '%s': %s\n", seat_name,
|
---|
191 | str_error(rc));
|
---|
192 | dispcfg_close(dispcfg);
|
---|
193 | return EIO;
|
---|
194 | }
|
---|
195 |
|
---|
196 | dispcfg_close(dispcfg);
|
---|
197 | return EOK;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** List seats subcommand.
|
---|
201 | *
|
---|
202 | * @param dcfg_svc Display configuration service name
|
---|
203 | * @param argc Number of arguments
|
---|
204 | * @param argv Arguments
|
---|
205 | * @return EOK on success or an erro code
|
---|
206 | */
|
---|
207 | static errno_t list_seat(const char *dcfg_svc, int argc, char *argv[])
|
---|
208 | {
|
---|
209 | dispcfg_t *dispcfg;
|
---|
210 | dispcfg_seat_list_t *seat_list;
|
---|
211 | dispcfg_seat_info_t *sinfo;
|
---|
212 | table_t *table = NULL;
|
---|
213 | size_t i;
|
---|
214 | errno_t rc;
|
---|
215 |
|
---|
216 | if (argc > 0) {
|
---|
217 | printf(NAME ": Too many arguments.\n");
|
---|
218 | print_syntax();
|
---|
219 | return EINVAL;
|
---|
220 | }
|
---|
221 |
|
---|
222 | rc = dispcfg_open(dcfg_svc, NULL, NULL, &dispcfg);
|
---|
223 | if (rc != EOK) {
|
---|
224 | printf(NAME ": Failed connecting to display configuration "
|
---|
225 | "service: %s.\n", str_error(rc));
|
---|
226 | return rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 | rc = dispcfg_get_seat_list(dispcfg, &seat_list);
|
---|
230 | if (rc != EOK) {
|
---|
231 | printf(NAME ": Failed getting seat list.\n");
|
---|
232 | dispcfg_close(dispcfg);
|
---|
233 | return rc;
|
---|
234 | }
|
---|
235 |
|
---|
236 | rc = table_create(&table);
|
---|
237 | if (rc != EOK) {
|
---|
238 | printf("Memory allocation failed.\n");
|
---|
239 | dispcfg_close(dispcfg);
|
---|
240 | goto out;
|
---|
241 | }
|
---|
242 |
|
---|
243 | table_header_row(table);
|
---|
244 | table_printf(table, "Seat Name\n");
|
---|
245 |
|
---|
246 | for (i = 0; i < seat_list->nseats; i++) {
|
---|
247 | rc = dispcfg_get_seat_info(dispcfg, seat_list->seats[i],
|
---|
248 | &sinfo);
|
---|
249 | if (rc != EOK) {
|
---|
250 | printf("Failed getting properties of seat %zu.\n",
|
---|
251 | (size_t)seat_list->seats[i]);
|
---|
252 | continue;
|
---|
253 | }
|
---|
254 |
|
---|
255 | table_printf(table, "%s\n", sinfo->name);
|
---|
256 |
|
---|
257 | dispcfg_free_seat_info(sinfo);
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (seat_list->nseats != 0) {
|
---|
261 | rc = table_print_out(table, stdout);
|
---|
262 | if (rc != EOK) {
|
---|
263 | printf("Error printing table.\n");
|
---|
264 | goto out;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | rc = EOK;
|
---|
269 | out:
|
---|
270 | table_destroy(table);
|
---|
271 | dispcfg_close(dispcfg);
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 | int main(int argc, char *argv[])
|
---|
276 | {
|
---|
277 | const char *dispcfg_svc = DISPCFG_DEFAULT;
|
---|
278 | errno_t rc;
|
---|
279 |
|
---|
280 | if (argc < 2 || str_cmp(argv[1], "-h") == 0) {
|
---|
281 | print_syntax();
|
---|
282 | return 0;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (str_cmp(argv[1], "list-seat") == 0) {
|
---|
286 | rc = list_seat(dispcfg_svc, argc - 2, argv + 2);
|
---|
287 | if (rc != EOK)
|
---|
288 | return 1;
|
---|
289 | } else if (str_cmp(argv[1], "create-seat") == 0) {
|
---|
290 | rc = create_seat(dispcfg_svc, argc - 2, argv + 2);
|
---|
291 | if (rc != EOK)
|
---|
292 | return 1;
|
---|
293 | } else if (str_cmp(argv[1], "delete-seat") == 0) {
|
---|
294 | rc = delete_seat(dispcfg_svc, argc - 2, argv + 2);
|
---|
295 | if (rc != EOK)
|
---|
296 | return 1;
|
---|
297 | } else {
|
---|
298 | printf(NAME ": Unknown command '%s'.\n", argv[1]);
|
---|
299 | print_syntax();
|
---|
300 | return 1;
|
---|
301 | }
|
---|
302 |
|
---|
303 | return 0;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /** @}
|
---|
307 | */
|
---|