Changes in uspace/lib/ui/src/ui.c [252d03c:e3e64f6] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/src/ui.c

    r252d03c re3e64f6  
    3939#include <errno.h>
    4040#include <fibril.h>
     41#include <gfx/color.h>
     42#include <gfx/cursor.h>
     43#include <gfx/render.h>
    4144#include <io/console.h>
    4245#include <stdbool.h>
     
    4750#include <ui/wdecor.h>
    4851#include <ui/window.h>
     52#include "../private/wdecor.h"
    4953#include "../private/window.h"
    5054#include "../private/ui.h"
     
    5357 *
    5458 * 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).
     59 * eiher 'disp' for display service, 'cons' for console, 'null'
     60 * for dummy output. Service is a location ID service name (e.g. hid/display).
    5761 *
    5862 * @param ospec Output specification
     
    6468{
    6569        const char *cp;
    66 
    67         if (ospec == UI_DISPLAY_DEFAULT) {
    68                 *ws = ui_ws_display;
    69                 *osvc = DISPLAY_DEFAULT;
    70                 return;
    71         }
    7270
    7371        cp = ospec;
     
    8078                } else if (str_lcmp(ospec, "cons@", str_length("cons@")) == 0) {
    8179                        *ws = ui_ws_console;
     80                } else if (str_lcmp(ospec, "null@", str_length("null@")) == 0) {
     81                        *ws = ui_ws_null;
     82                } else if (str_lcmp(ospec, "@", str_length("@")) == 0) {
     83                        *ws = ui_ws_any;
    8284                } else {
    8385                        *ws = ui_ws_unknown;
     
    9799 *
    98100 * @param ospec Output specification or @c UI_DISPLAY_DEFAULT to use
    99  *              the default output
     101 *              the default display service, UI_CONSOLE_DEFAULT to use
     102 *              the default console service, UI_DISPLAY_NULL to use
     103 *              dummy output.
    100104 * @param rui Place to store pointer to new UI
    101105 * @return EOK on success or an error code
     
    109113        ui_winsys_t ws;
    110114        const char *osvc;
     115        sysarg_t cols;
     116        sysarg_t rows;
    111117        ui_t *ui;
    112118
    113119        ui_ospec_parse(ospec, &ws, &osvc);
    114120
    115         if (ws == ui_ws_display) {
    116                 rc = display_open(osvc, &display);
     121        if (ws == ui_ws_display || ws == ui_ws_any) {
     122                rc = display_open(osvc != NULL ? osvc : DISPLAY_DEFAULT,
     123                    &display);
    117124                if (rc != EOK)
    118                         return rc;
     125                        goto disp_fail;
    119126
    120127                rc = ui_create_disp(display, &ui);
    121128                if (rc != EOK) {
    122129                        display_close(display);
    123                         return rc;
    124                 }
    125         } else if (ws == ui_ws_console) {
     130                        goto disp_fail;
     131                }
     132
     133                ui->myoutput = true;
     134                *rui = ui;
     135                return EOK;
     136        }
     137
     138disp_fail:
     139        if (ws == ui_ws_console || ws == ui_ws_any) {
    126140                console = console_init(stdin, stdout);
    127141                if (console == NULL)
    128                         return EIO;
     142                        goto cons_fail;
     143
     144                rc = console_get_size(console, &cols, &rows);
     145                if (rc != EOK) {
     146                        console_done(console);
     147                        goto cons_fail;
     148                }
     149
     150                console_cursor_visibility(console, false);
    129151
    130152                /* ws == ui_ws_console */
     
    132154                if (rc != EOK) {
    133155                        console_done(console);
    134                         return rc;
     156                        goto cons_fail;
    135157                }
    136158
     
    139161                        ui_destroy(ui);
    140162                        console_done(console);
     163                        goto cons_fail;
     164                }
     165
     166                ui->cgc = cgc;
     167                ui->rect.p0.x = 0;
     168                ui->rect.p0.y = 0;
     169                ui->rect.p1.x = cols;
     170                ui->rect.p1.y = rows;
     171
     172                (void) ui_paint(ui);
     173                ui->myoutput = true;
     174                *rui = ui;
     175                return EOK;
     176        }
     177
     178cons_fail:
     179        if (ws == ui_ws_null) {
     180                rc = ui_create_disp(NULL, &ui);
     181                if (rc != EOK)
    141182                        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;
     183
     184                ui->myoutput = true;
     185                *rui = ui;
     186                return EOK;
     187        }
     188
     189        return EINVAL;
    152190}
    153191
     
    203241                if (ui->cgc != NULL)
    204242                        console_gc_delete(ui->cgc);
    205                 if (ui->console != NULL)
     243                if (ui->console != NULL) {
     244                        console_cursor_visibility(ui->console, true);
    206245                        console_done(ui->console);
     246                }
    207247                if (ui->display != NULL)
    208248                        display_close(ui->display);
     
    216256        ui_window_t *awnd;
    217257        ui_evclaim_t claim;
     258        pos_event_t pos;
    218259
    219260        awnd = ui_window_get_active(ui);
     
    226267                break;
    227268        case CEV_POS:
    228                 claim = ui_wdecor_pos_event(awnd->wdecor, &event->ev.pos);
     269                pos = event->ev.pos;
     270                /* Translate event to window-relative coordinates */
     271                pos.hpos -= awnd->dpos.x;
     272                pos.vpos -= awnd->dpos.y;
     273
     274                claim = ui_wdecor_pos_event(awnd->wdecor, &pos);
    229275                /* Note: If event is claimed, awnd might not be valid anymore */
    230276                if (claim == ui_unclaimed)
    231                         ui_window_send_pos(awnd, &event->ev.pos);
     277                        ui_window_send_pos(awnd, &pos);
     278
    232279                break;
    233280        }
     
    280327{
    281328        errno_t rc;
     329        gfx_context_t *gc;
    282330        ui_window_t *awnd;
     331        gfx_color_t *color = NULL;
     332
     333        /* In case of null output */
     334        if (ui->cgc == NULL)
     335                return EOK;
     336
     337        gc = console_gc_get_ctx(ui->cgc);
     338
     339        rc = gfx_color_new_ega(0x11, &color);
     340        if (rc != EOK)
     341                return rc;
     342
     343        rc = gfx_set_color(gc, color);
     344        if (rc != EOK) {
     345                gfx_color_delete(color);
     346                return rc;
     347        }
     348
     349        rc = gfx_fill_rect(gc, &ui->rect);
     350        if (rc != EOK) {
     351                gfx_color_delete(color);
     352                return rc;
     353        }
     354
     355        gfx_color_delete(color);
    283356
    284357        /* XXX Should repaint all windows */
     
    292365
    293366        return ui_window_paint(awnd);
     367}
     368
     369/** Free up console for other users.
     370 *
     371 * Release console resources for another application (that the current
     372 * task is starting). After the other application finishes, resume
     373 * operation with ui_resume(). No calls to UI must happen inbetween
     374 * and no events must be processed (i.e. the calling function must not
     375 * return control to UI.
     376 *
     377 * @param ui UI
     378 * @return EOK on success or an error code
     379 */
     380errno_t ui_suspend(ui_t *ui)
     381{
     382        if (ui->cgc == NULL)
     383                return EOK;
     384
     385        (void) console_set_caption(ui->console, "");
     386        return console_gc_suspend(ui->cgc);
     387}
     388
     389/** Resume suspended UI.
     390 *
     391 * Reclaim console resources (after child application has finished running)
     392 * and restore UI operation previously suspended by calling ui_suspend().
     393 *
     394 * @param ui UI
     395 * @return EOK on success or an error code
     396 */
     397errno_t ui_resume(ui_t *ui)
     398{
     399        errno_t rc;
     400        ui_window_t *awnd;
     401        sysarg_t col;
     402        sysarg_t row;
     403        cons_event_t ev;
     404
     405        if (ui->cgc == NULL)
     406                return EOK;
     407
     408        rc = console_get_pos(ui->console, &col, &row);
     409        if (rc != EOK)
     410                return rc;
     411
     412        /*
     413         * Here's a little heuristic to help determine if we need
     414         * to pause before returning to the UI. If we are in the
     415         * top-left corner, chances are the screen is empty and
     416         * there is no need to pause.
     417         */
     418        if (col != 0 || row != 0) {
     419                printf("Press any key or button to continue...\n");
     420
     421                while (true) {
     422                        rc = console_get_event(ui->console, &ev);
     423                        if (rc != EOK)
     424                                return EIO;
     425
     426                        if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS)
     427                                break;
     428
     429                        if (ev.type == CEV_POS && ev.ev.pos.type == POS_PRESS)
     430                                break;
     431                }
     432        }
     433
     434        rc = console_gc_resume(ui->cgc);
     435        if (rc != EOK)
     436                return rc;
     437
     438        awnd = ui_window_get_active(ui);
     439        if (awnd != NULL)
     440                (void) console_set_caption(ui->console, awnd->wdecor->caption);
     441
     442        return gfx_cursor_set_visible(console_gc_get_ctx(ui->cgc), false);
    294443}
    295444
Note: See TracChangeset for help on using the changeset viewer.