[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>
|
---|
[77ffa01] | 41 | #include <io/console.h>
|
---|
[87822ce] | 42 | #include <stdbool.h>
|
---|
[f7a90df] | 43 | #include <stdlib.h>
|
---|
[77ffa01] | 44 | #include <str.h>
|
---|
[d55ab823] | 45 | #include <task.h>
|
---|
[f7a90df] | 46 | #include <ui/ui.h>
|
---|
[77ffa01] | 47 | #include <ui/wdecor.h>
|
---|
[252d03c] | 48 | #include <ui/window.h>
|
---|
[77ffa01] | 49 | #include "../private/window.h"
|
---|
[f7a90df] | 50 | #include "../private/ui.h"
|
---|
| 51 |
|
---|
[77ffa01] | 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 | */
|
---|
| 62 | static 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 |
|
---|
[f7a90df] | 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 | */
|
---|
| 103 | errno_t ui_create(const char *ospec, ui_t **rui)
|
---|
| 104 | {
|
---|
| 105 | errno_t rc;
|
---|
| 106 | display_t *display;
|
---|
[77ffa01] | 107 | console_ctrl_t *console;
|
---|
[252d03c] | 108 | console_gc_t *cgc;
|
---|
[77ffa01] | 109 | ui_winsys_t ws;
|
---|
| 110 | const char *osvc;
|
---|
[f7a90df] | 111 | ui_t *ui;
|
---|
| 112 |
|
---|
[77ffa01] | 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;
|
---|
[f7a90df] | 129 |
|
---|
[77ffa01] | 130 | /* ws == ui_ws_console */
|
---|
| 131 | rc = ui_create_cons(console, &ui);
|
---|
| 132 | if (rc != EOK) {
|
---|
| 133 | console_done(console);
|
---|
| 134 | return rc;
|
---|
| 135 | }
|
---|
[252d03c] | 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;
|
---|
[77ffa01] | 145 | } else {
|
---|
| 146 | return EINVAL;
|
---|
[f7a90df] | 147 | }
|
---|
| 148 |
|
---|
| 149 | ui->myoutput = true;
|
---|
[d284ce9] | 150 | *rui = ui;
|
---|
[f7a90df] | 151 | return EOK;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[77ffa01] | 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 | */
|
---|
| 159 | errno_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;
|
---|
[252d03c] | 168 | list_initialize(&ui->windows);
|
---|
[77ffa01] | 169 | *rui = ui;
|
---|
| 170 | return EOK;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[f7a90df] | 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 | */
|
---|
| 179 | errno_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;
|
---|
[252d03c] | 188 | list_initialize(&ui->windows);
|
---|
[f7a90df] | 189 | *rui = ui;
|
---|
| 190 | return EOK;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /** Destroy user interface.
|
---|
| 194 | *
|
---|
| 195 | * @param ui User interface or @c NULL
|
---|
| 196 | */
|
---|
| 197 | void ui_destroy(ui_t *ui)
|
---|
| 198 | {
|
---|
| 199 | if (ui == NULL)
|
---|
| 200 | return;
|
---|
| 201 |
|
---|
[77ffa01] | 202 | if (ui->myoutput) {
|
---|
[252d03c] | 203 | if (ui->cgc != NULL)
|
---|
| 204 | console_gc_delete(ui->cgc);
|
---|
[77ffa01] | 205 | if (ui->console != NULL)
|
---|
| 206 | console_done(ui->console);
|
---|
| 207 | if (ui->display != NULL)
|
---|
| 208 | display_close(ui->display);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[f7a90df] | 211 | free(ui);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[77ffa01] | 214 | static void ui_cons_event_process(ui_t *ui, cons_event_t *event)
|
---|
| 215 | {
|
---|
[252d03c] | 216 | ui_window_t *awnd;
|
---|
| 217 | ui_evclaim_t claim;
|
---|
| 218 |
|
---|
| 219 | awnd = ui_window_get_active(ui);
|
---|
| 220 | if (awnd == NULL)
|
---|
[77ffa01] | 221 | return;
|
---|
| 222 |
|
---|
| 223 | switch (event->type) {
|
---|
| 224 | case CEV_KEY:
|
---|
[252d03c] | 225 | ui_window_send_kbd(awnd, &event->ev.key);
|
---|
[77ffa01] | 226 | break;
|
---|
| 227 | case CEV_POS:
|
---|
[252d03c] | 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);
|
---|
[77ffa01] | 232 | break;
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[d284ce9] | 236 | /** Execute user interface.
|
---|
| 237 | *
|
---|
[d55ab823] | 238 | * Return task exit code of zero and block unitl the application starts
|
---|
| 239 | * the termination process by calling ui_quit(@a ui).
|
---|
[d284ce9] | 240 | *
|
---|
| 241 | * @param ui User interface
|
---|
| 242 | */
|
---|
| 243 | void ui_run(ui_t *ui)
|
---|
| 244 | {
|
---|
[77ffa01] | 245 | cons_event_t event;
|
---|
| 246 | usec_t timeout;
|
---|
[87822ce] | 247 | errno_t rc;
|
---|
[d55ab823] | 248 |
|
---|
[77ffa01] | 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;
|
---|
[87822ce] | 256 | rc = console_get_event_timeout(ui->console,
|
---|
[77ffa01] | 257 | &event, &timeout);
|
---|
[87822ce] | 258 |
|
---|
| 259 | /* Do we actually have an event? */
|
---|
| 260 | if (rc == EOK) {
|
---|
[77ffa01] | 261 | ui_cons_event_process(ui, &event);
|
---|
[87822ce] | 262 | } else if (rc != ETIMEOUT) {
|
---|
| 263 | /* Error, quit */
|
---|
| 264 | break;
|
---|
| 265 | }
|
---|
[77ffa01] | 266 | } else {
|
---|
| 267 | fibril_usleep(100000);
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
[d284ce9] | 270 | }
|
---|
| 271 |
|
---|
[252d03c] | 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 | */
|
---|
| 279 | errno_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 |
|
---|
[d284ce9] | 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 | */
|
---|
| 304 | void ui_quit(ui_t *ui)
|
---|
| 305 | {
|
---|
| 306 | ui->quit = true;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[9c7dc8e] | 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 | */
|
---|
| 314 | bool 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 |
|
---|
[252d03c] | 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 | */
|
---|
| 328 | bool ui_is_fullscreen(ui_t *ui)
|
---|
| 329 | {
|
---|
| 330 | return (ui->display == NULL);
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[f7a90df] | 333 | /** @}
|
---|
| 334 | */
|
---|