source: mainline/uspace/lib/ui/src/ui.c@ 252d03c

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

Popping up a message, in text mode as well

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * Copyright (c) 2021 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 libui
30 * @{
31 */
32/**
33 * @file User interface
34 */
35
36#include <adt/list.h>
37#include <ctype.h>
38#include <display.h>
39#include <errno.h>
40#include <fibril.h>
41#include <io/console.h>
42#include <stdbool.h>
43#include <stdlib.h>
44#include <str.h>
45#include <task.h>
46#include <ui/ui.h>
47#include <ui/wdecor.h>
48#include <ui/window.h>
49#include "../private/window.h"
50#include "../private/ui.h"
51
52/** Parse output specification.
53 *
54 * Output specification has the form <proto>@<service> where proto is
55 * eiher 'disp' for display service or 'cons' for console. Service
56 * is a location ID service name (e.g. hid/display).
57 *
58 * @param ospec Output specification
59 * @param ws Place to store window system type (protocol)
60 * @param osvc Place to store pointer to output service name
61 */
62static void ui_ospec_parse(const char *ospec, ui_winsys_t *ws,
63 const char **osvc)
64{
65 const char *cp;
66
67 if (ospec == UI_DISPLAY_DEFAULT) {
68 *ws = ui_ws_display;
69 *osvc = DISPLAY_DEFAULT;
70 return;
71 }
72
73 cp = ospec;
74 while (isalpha(*cp))
75 ++cp;
76
77 if (*cp == '@') {
78 if (str_lcmp(ospec, "disp@", str_length("disp@")) == 0) {
79 *ws = ui_ws_display;
80 } else if (str_lcmp(ospec, "cons@", str_length("cons@")) == 0) {
81 *ws = ui_ws_console;
82 } else {
83 *ws = ui_ws_unknown;
84 }
85
86 if (cp[1] != '\0')
87 *osvc = cp + 1;
88 else
89 *osvc = NULL;
90 } else {
91 *ws = ui_ws_display;
92 *osvc = ospec;
93 }
94}
95
96/** Create new user interface.
97 *
98 * @param ospec Output specification or @c UI_DISPLAY_DEFAULT to use
99 * the default output
100 * @param rui Place to store pointer to new UI
101 * @return EOK on success or an error code
102 */
103errno_t ui_create(const char *ospec, ui_t **rui)
104{
105 errno_t rc;
106 display_t *display;
107 console_ctrl_t *console;
108 console_gc_t *cgc;
109 ui_winsys_t ws;
110 const char *osvc;
111 ui_t *ui;
112
113 ui_ospec_parse(ospec, &ws, &osvc);
114
115 if (ws == ui_ws_display) {
116 rc = display_open(osvc, &display);
117 if (rc != EOK)
118 return rc;
119
120 rc = ui_create_disp(display, &ui);
121 if (rc != EOK) {
122 display_close(display);
123 return rc;
124 }
125 } else if (ws == ui_ws_console) {
126 console = console_init(stdin, stdout);
127 if (console == NULL)
128 return EIO;
129
130 /* ws == ui_ws_console */
131 rc = ui_create_cons(console, &ui);
132 if (rc != EOK) {
133 console_done(console);
134 return rc;
135 }
136
137 rc = console_gc_create(console, NULL, &cgc);
138 if (rc != EOK) {
139 ui_destroy(ui);
140 console_done(console);
141 return rc;
142 }
143
144 ui->cgc = cgc;
145 } else {
146 return EINVAL;
147 }
148
149 ui->myoutput = true;
150 *rui = ui;
151 return EOK;
152}
153
154/** Create new user interface using console service.
155 *
156 * @param rui Place to store pointer to new UI
157 * @return EOK on success or an error code
158 */
159errno_t ui_create_cons(console_ctrl_t *console, ui_t **rui)
160{
161 ui_t *ui;
162
163 ui = calloc(1, sizeof(ui_t));
164 if (ui == NULL)
165 return ENOMEM;
166
167 ui->console = console;
168 list_initialize(&ui->windows);
169 *rui = ui;
170 return EOK;
171}
172
173/** Create new user interface using display service.
174 *
175 * @param disp Display
176 * @param rui Place to store pointer to new UI
177 * @return EOK on success or an error code
178 */
179errno_t ui_create_disp(display_t *disp, ui_t **rui)
180{
181 ui_t *ui;
182
183 ui = calloc(1, sizeof(ui_t));
184 if (ui == NULL)
185 return ENOMEM;
186
187 ui->display = disp;
188 list_initialize(&ui->windows);
189 *rui = ui;
190 return EOK;
191}
192
193/** Destroy user interface.
194 *
195 * @param ui User interface or @c NULL
196 */
197void ui_destroy(ui_t *ui)
198{
199 if (ui == NULL)
200 return;
201
202 if (ui->myoutput) {
203 if (ui->cgc != NULL)
204 console_gc_delete(ui->cgc);
205 if (ui->console != NULL)
206 console_done(ui->console);
207 if (ui->display != NULL)
208 display_close(ui->display);
209 }
210
211 free(ui);
212}
213
214static void ui_cons_event_process(ui_t *ui, cons_event_t *event)
215{
216 ui_window_t *awnd;
217 ui_evclaim_t claim;
218
219 awnd = ui_window_get_active(ui);
220 if (awnd == NULL)
221 return;
222
223 switch (event->type) {
224 case CEV_KEY:
225 ui_window_send_kbd(awnd, &event->ev.key);
226 break;
227 case CEV_POS:
228 claim = ui_wdecor_pos_event(awnd->wdecor, &event->ev.pos);
229 /* Note: If event is claimed, awnd might not be valid anymore */
230 if (claim == ui_unclaimed)
231 ui_window_send_pos(awnd, &event->ev.pos);
232 break;
233 }
234}
235
236/** Execute user interface.
237 *
238 * Return task exit code of zero and block unitl the application starts
239 * the termination process by calling ui_quit(@a ui).
240 *
241 * @param ui User interface
242 */
243void ui_run(ui_t *ui)
244{
245 cons_event_t event;
246 usec_t timeout;
247 errno_t rc;
248
249 /* Only return command prompt if we are running in a separate window */
250 if (ui->display != NULL)
251 task_retval(0);
252
253 while (!ui->quit) {
254 if (ui->console != NULL) {
255 timeout = 100000;
256 rc = console_get_event_timeout(ui->console,
257 &event, &timeout);
258
259 /* Do we actually have an event? */
260 if (rc == EOK) {
261 ui_cons_event_process(ui, &event);
262 } else if (rc != ETIMEOUT) {
263 /* Error, quit */
264 break;
265 }
266 } else {
267 fibril_usleep(100000);
268 }
269 }
270}
271
272/** Repaint UI (only used in fullscreen mode).
273 *
274 * This is used when an area is exposed in fullscreen mode.
275 *
276 * @param ui UI
277 * @return @c EOK on success or an error code
278 */
279errno_t ui_paint(ui_t *ui)
280{
281 errno_t rc;
282 ui_window_t *awnd;
283
284 /* XXX Should repaint all windows */
285 awnd = ui_window_get_active(ui);
286 if (awnd == NULL)
287 return EOK;
288
289 rc = ui_wdecor_paint(awnd->wdecor);
290 if (rc != EOK)
291 return rc;
292
293 return ui_window_paint(awnd);
294}
295
296/** Terminate user interface.
297 *
298 * Calling this function causes the user interface to terminate
299 * (i.e. exit from ui_run()). This would be typically called from
300 * an event handler.
301 *
302 * @param ui User interface
303 */
304void ui_quit(ui_t *ui)
305{
306 ui->quit = true;
307}
308
309/** Determine if we are running in text mode.
310 *
311 * @param ui User interface
312 * @return @c true iff we are running in text mode
313 */
314bool ui_is_textmode(ui_t *ui)
315{
316 /*
317 * XXX Currently console is always text and display is always
318 * graphics, but this need not always be true.
319 */
320 return (ui->console != NULL);
321}
322
323/** Determine if we are emulating windows.
324 *
325 * @param ui User interface
326 * @return @c true iff we are running in text mode
327 */
328bool ui_is_fullscreen(ui_t *ui)
329{
330 return (ui->display == NULL);
331}
332
333/** @}
334 */
Note: See TracBrowser for help on using the repository browser.