[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;
|
---|
[3c3657c] | 238 | pos_event_t pos;
|
---|
[252d03c] | 239 |
|
---|
| 240 | awnd = ui_window_get_active(ui);
|
---|
| 241 | if (awnd == NULL)
|
---|
[77ffa01] | 242 | return;
|
---|
| 243 |
|
---|
| 244 | switch (event->type) {
|
---|
| 245 | case CEV_KEY:
|
---|
[252d03c] | 246 | ui_window_send_kbd(awnd, &event->ev.key);
|
---|
[77ffa01] | 247 | break;
|
---|
| 248 | case CEV_POS:
|
---|
[3c3657c] | 249 | pos = event->ev.pos;
|
---|
[8ce56a6] | 250 | /* Translate event to window-relative coordinates */
|
---|
[3c3657c] | 251 | pos.hpos -= awnd->dpos.x;
|
---|
| 252 | pos.vpos -= awnd->dpos.y;
|
---|
| 253 |
|
---|
| 254 | claim = ui_wdecor_pos_event(awnd->wdecor, &pos);
|
---|
[252d03c] | 255 | /* Note: If event is claimed, awnd might not be valid anymore */
|
---|
| 256 | if (claim == ui_unclaimed)
|
---|
[3c3657c] | 257 | ui_window_send_pos(awnd, &pos);
|
---|
| 258 |
|
---|
[77ffa01] | 259 | break;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[d284ce9] | 263 | /** Execute user interface.
|
---|
| 264 | *
|
---|
[d55ab823] | 265 | * Return task exit code of zero and block unitl the application starts
|
---|
| 266 | * the termination process by calling ui_quit(@a ui).
|
---|
[d284ce9] | 267 | *
|
---|
| 268 | * @param ui User interface
|
---|
| 269 | */
|
---|
| 270 | void ui_run(ui_t *ui)
|
---|
| 271 | {
|
---|
[77ffa01] | 272 | cons_event_t event;
|
---|
| 273 | usec_t timeout;
|
---|
[87822ce] | 274 | errno_t rc;
|
---|
[d55ab823] | 275 |
|
---|
[77ffa01] | 276 | /* Only return command prompt if we are running in a separate window */
|
---|
| 277 | if (ui->display != NULL)
|
---|
| 278 | task_retval(0);
|
---|
| 279 |
|
---|
| 280 | while (!ui->quit) {
|
---|
| 281 | if (ui->console != NULL) {
|
---|
| 282 | timeout = 100000;
|
---|
[87822ce] | 283 | rc = console_get_event_timeout(ui->console,
|
---|
[77ffa01] | 284 | &event, &timeout);
|
---|
[87822ce] | 285 |
|
---|
| 286 | /* Do we actually have an event? */
|
---|
| 287 | if (rc == EOK) {
|
---|
[77ffa01] | 288 | ui_cons_event_process(ui, &event);
|
---|
[87822ce] | 289 | } else if (rc != ETIMEOUT) {
|
---|
| 290 | /* Error, quit */
|
---|
| 291 | break;
|
---|
| 292 | }
|
---|
[77ffa01] | 293 | } else {
|
---|
| 294 | fibril_usleep(100000);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
[d284ce9] | 297 | }
|
---|
| 298 |
|
---|
[252d03c] | 299 | /** Repaint UI (only used in fullscreen mode).
|
---|
| 300 | *
|
---|
| 301 | * This is used when an area is exposed in fullscreen mode.
|
---|
| 302 | *
|
---|
| 303 | * @param ui UI
|
---|
| 304 | * @return @c EOK on success or an error code
|
---|
| 305 | */
|
---|
| 306 | errno_t ui_paint(ui_t *ui)
|
---|
| 307 | {
|
---|
| 308 | errno_t rc;
|
---|
[1ebcb791] | 309 | gfx_context_t *gc;
|
---|
[252d03c] | 310 | ui_window_t *awnd;
|
---|
[1ebcb791] | 311 | gfx_color_t *color = NULL;
|
---|
| 312 |
|
---|
| 313 | gc = console_gc_get_ctx(ui->cgc);
|
---|
| 314 |
|
---|
| 315 | rc = gfx_color_new_ega(0x11, &color);
|
---|
| 316 | if (rc != EOK)
|
---|
| 317 | return rc;
|
---|
| 318 |
|
---|
| 319 | rc = gfx_set_color(gc, color);
|
---|
| 320 | if (rc != EOK) {
|
---|
| 321 | gfx_color_delete(color);
|
---|
| 322 | return rc;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | rc = gfx_fill_rect(gc, &ui->rect);
|
---|
| 326 | if (rc != EOK) {
|
---|
| 327 | gfx_color_delete(color);
|
---|
| 328 | return rc;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | gfx_color_delete(color);
|
---|
[252d03c] | 332 |
|
---|
| 333 | /* XXX Should repaint all windows */
|
---|
| 334 | awnd = ui_window_get_active(ui);
|
---|
| 335 | if (awnd == NULL)
|
---|
| 336 | return EOK;
|
---|
| 337 |
|
---|
| 338 | rc = ui_wdecor_paint(awnd->wdecor);
|
---|
| 339 | if (rc != EOK)
|
---|
| 340 | return rc;
|
---|
| 341 |
|
---|
| 342 | return ui_window_paint(awnd);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[d284ce9] | 345 | /** Terminate user interface.
|
---|
| 346 | *
|
---|
| 347 | * Calling this function causes the user interface to terminate
|
---|
| 348 | * (i.e. exit from ui_run()). This would be typically called from
|
---|
| 349 | * an event handler.
|
---|
| 350 | *
|
---|
| 351 | * @param ui User interface
|
---|
| 352 | */
|
---|
| 353 | void ui_quit(ui_t *ui)
|
---|
| 354 | {
|
---|
| 355 | ui->quit = true;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[9c7dc8e] | 358 | /** Determine if we are running in text mode.
|
---|
| 359 | *
|
---|
| 360 | * @param ui User interface
|
---|
| 361 | * @return @c true iff we are running in text mode
|
---|
| 362 | */
|
---|
| 363 | bool ui_is_textmode(ui_t *ui)
|
---|
| 364 | {
|
---|
| 365 | /*
|
---|
| 366 | * XXX Currently console is always text and display is always
|
---|
| 367 | * graphics, but this need not always be true.
|
---|
| 368 | */
|
---|
| 369 | return (ui->console != NULL);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[252d03c] | 372 | /** Determine if we are emulating windows.
|
---|
| 373 | *
|
---|
| 374 | * @param ui User interface
|
---|
| 375 | * @return @c true iff we are running in text mode
|
---|
| 376 | */
|
---|
| 377 | bool ui_is_fullscreen(ui_t *ui)
|
---|
| 378 | {
|
---|
| 379 | return (ui->display == NULL);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[f7a90df] | 382 | /** @}
|
---|
| 383 | */
|
---|