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