HelenOS sources
This source file includes following definitions.
- ui_ospec_parse
- ui_create
- ui_create_cons
- ui_create_disp
- ui_destroy
- ui_cons_event_process
- ui_run
- ui_paint
- ui_suspend
- ui_resume
- ui_is_suspended
- ui_lock
- ui_unlock
- ui_quit
- ui_is_textmode
- ui_is_fullscreen
- ui_get_rect
- ui_get_clickmatic
#include <adt/list.h>
#include <ctype.h>
#include <display.h>
#include <errno.h>
#include <fibril.h>
#include <fibril_synch.h>
#include <gfx/color.h>
#include <gfx/cursor.h>
#include <gfx/render.h>
#include <io/console.h>
#include <stdbool.h>
#include <stdlib.h>
#include <str.h>
#include <task.h>
#include <types/common.h>
#include <ui/clickmatic.h>
#include <ui/ui.h>
#include <ui/wdecor.h>
#include <ui/window.h>
#include "../private/wdecor.h"
#include "../private/window.h"
#include "../private/ui.h"
static errno_t ui_ospec_parse(const char *ospec, ui_winsys_t *ws,
char **osvc, sysarg_t *ridev_id)
{
const char *cp;
const char *qm;
const char *endptr;
uint64_t idev_id;
errno_t rc;
*ridev_id = 0;
cp = ospec;
while (isalpha(*cp))
++cp;
if (*cp == '@') {
if (str_lcmp(ospec, "disp@", str_length("disp@")) == 0) {
*ws = ui_ws_display;
} else if (str_lcmp(ospec, "cons@", str_length("cons@")) == 0) {
*ws = ui_ws_console;
} else if (str_lcmp(ospec, "null@", str_length("null@")) == 0) {
*ws = ui_ws_null;
} else if (str_lcmp(ospec, "@", str_length("@")) == 0) {
*ws = ui_ws_any;
} else {
*ws = ui_ws_unknown;
}
++cp;
} else {
*ws = ui_ws_display;
}
qm = str_chr(cp, '?');
if (qm != NULL) {
*osvc = str_ndup(cp, qm - cp);
} else {
*osvc = str_dup(cp);
}
if (*osvc == NULL)
return ENOMEM;
if (qm != NULL) {
cp = qm + 1;
if (str_lcmp(cp, "idev=", str_length("idev=")) == 0) {
cp += str_length("idev=");
rc = str_uint64_t(cp, &endptr, 10, false, &idev_id);
if (rc != EOK)
goto error;
*ridev_id = idev_id;
cp = endptr;
}
}
if (*cp != '\0') {
rc = EINVAL;
goto error;
}
return EOK;
error:
free(*osvc);
*osvc = NULL;
return rc;
}
errno_t ui_create(const char *ospec, ui_t **rui)
{
errno_t rc;
display_t *display;
console_ctrl_t *console;
console_gc_t *cgc;
ui_winsys_t ws;
char *osvc;
sysarg_t cols;
sysarg_t rows;
sysarg_t idev_id;
ui_t *ui;
rc = ui_ospec_parse(ospec, &ws, &osvc, &idev_id);
if (rc != EOK)
return rc;
if (ws == ui_ws_display || ws == ui_ws_any) {
rc = display_open((str_cmp(osvc, "") != 0) ? osvc :
DISPLAY_DEFAULT, &display);
if (rc != EOK)
goto disp_fail;
rc = ui_create_disp(display, &ui);
if (rc != EOK) {
display_close(display);
goto disp_fail;
}
free(osvc);
ui->myoutput = true;
ui->idev_id = idev_id;
*rui = ui;
return EOK;
}
disp_fail:
if (ws == ui_ws_console || ws == ui_ws_any) {
console = console_init(stdin, stdout);
if (console == NULL)
goto cons_fail;
rc = console_get_size(console, &cols, &rows);
if (rc != EOK) {
console_done(console);
goto cons_fail;
}
console_cursor_visibility(console, false);
rc = ui_create_cons(console, &ui);
if (rc != EOK) {
console_done(console);
goto cons_fail;
}
rc = console_gc_create(console, NULL, &cgc);
if (rc != EOK) {
ui_destroy(ui);
console_done(console);
goto cons_fail;
}
free(osvc);
ui->cgc = cgc;
ui->rect.p0.x = 0;
ui->rect.p0.y = 0;
ui->rect.p1.x = cols;
ui->rect.p1.y = rows;
(void) ui_paint(ui);
ui->myoutput = true;
*rui = ui;
return EOK;
}
cons_fail:
if (ws == ui_ws_null) {
free(osvc);
rc = ui_create_disp(NULL, &ui);
if (rc != EOK)
return rc;
ui->myoutput = true;
*rui = ui;
return EOK;
}
free(osvc);
return EINVAL;
}
errno_t ui_create_cons(console_ctrl_t *console, ui_t **rui)
{
ui_t *ui;
errno_t rc;
ui = calloc(1, sizeof(ui_t));
if (ui == NULL)
return ENOMEM;
rc = ui_clickmatic_create(ui, &ui->clickmatic);
if (rc != EOK) {
free(ui);
return rc;
}
ui->console = console;
list_initialize(&ui->windows);
fibril_mutex_initialize(&ui->lock);
*rui = ui;
return EOK;
}
errno_t ui_create_disp(display_t *disp, ui_t **rui)
{
ui_t *ui;
errno_t rc;
ui = calloc(1, sizeof(ui_t));
if (ui == NULL)
return ENOMEM;
rc = ui_clickmatic_create(ui, &ui->clickmatic);
if (rc != EOK) {
free(ui);
return rc;
}
ui->display = disp;
list_initialize(&ui->windows);
fibril_mutex_initialize(&ui->lock);
*rui = ui;
return EOK;
}
void ui_destroy(ui_t *ui)
{
if (ui == NULL)
return;
if (ui->myoutput) {
if (ui->cgc != NULL)
console_gc_delete(ui->cgc);
if (ui->console != NULL) {
console_cursor_visibility(ui->console, true);
console_done(ui->console);
}
if (ui->display != NULL)
display_close(ui->display);
}
free(ui);
}
static void ui_cons_event_process(ui_t *ui, cons_event_t *event)
{
ui_window_t *awnd;
ui_evclaim_t claim;
pos_event_t pos;
awnd = ui_window_get_active(ui);
if (awnd == NULL)
return;
switch (event->type) {
case CEV_KEY:
ui_lock(ui);
ui_window_send_kbd(awnd, &event->ev.key);
ui_unlock(ui);
break;
case CEV_POS:
pos = event->ev.pos;
pos.hpos -= awnd->dpos.x;
pos.vpos -= awnd->dpos.y;
claim = ui_wdecor_pos_event(awnd->wdecor, &pos);
if (claim == ui_unclaimed) {
ui_lock(ui);
ui_window_send_pos(awnd, &pos);
ui_unlock(ui);
}
break;
case CEV_RESIZE:
ui_lock(ui);
ui_window_send_resize(awnd);
ui_unlock(ui);
break;
}
}
void ui_run(ui_t *ui)
{
cons_event_t event;
usec_t timeout;
errno_t rc;
if (ui->display != NULL)
task_retval(0);
while (!ui->quit) {
if (ui->console != NULL) {
timeout = 100000;
rc = console_get_event_timeout(ui->console,
&event, &timeout);
if (rc == EOK) {
ui_cons_event_process(ui, &event);
} else if (rc != ETIMEOUT) {
break;
}
} else {
fibril_usleep(100000);
}
}
}
errno_t ui_paint(ui_t *ui)
{
errno_t rc;
gfx_context_t *gc;
ui_window_t *awnd;
gfx_color_t *color = NULL;
if (ui->cgc == NULL)
return EOK;
gc = console_gc_get_ctx(ui->cgc);
rc = gfx_color_new_ega(0x11, &color);
if (rc != EOK)
return rc;
rc = gfx_set_color(gc, color);
if (rc != EOK) {
gfx_color_delete(color);
return rc;
}
rc = gfx_fill_rect(gc, &ui->rect);
if (rc != EOK) {
gfx_color_delete(color);
return rc;
}
gfx_color_delete(color);
awnd = ui_window_get_active(ui);
if (awnd == NULL)
return EOK;
rc = ui_wdecor_paint(awnd->wdecor);
if (rc != EOK)
return rc;
return ui_window_paint(awnd);
}
errno_t ui_suspend(ui_t *ui)
{
errno_t rc;
assert(!ui->suspended);
if (ui->cgc == NULL) {
ui->suspended = true;
return EOK;
}
(void) console_set_caption(ui->console, "");
rc = console_gc_suspend(ui->cgc);
if (rc != EOK)
return rc;
ui->suspended = true;
return EOK;
}
errno_t ui_resume(ui_t *ui)
{
errno_t rc;
ui_window_t *awnd;
sysarg_t col;
sysarg_t row;
cons_event_t ev;
assert(ui->suspended);
if (ui->cgc == NULL) {
ui->suspended = false;
return EOK;
}
rc = console_get_pos(ui->console, &col, &row);
if (rc != EOK)
return rc;
if (col != 0 || row != 0) {
printf("Press any key or button to continue...\n");
while (true) {
rc = console_get_event(ui->console, &ev);
if (rc != EOK)
return EIO;
if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS)
break;
if (ev.type == CEV_POS && ev.ev.pos.type == POS_PRESS)
break;
}
}
rc = console_gc_resume(ui->cgc);
if (rc != EOK)
return rc;
ui->suspended = false;
awnd = ui_window_get_active(ui);
if (awnd != NULL)
(void) console_set_caption(ui->console, awnd->wdecor->caption);
rc = gfx_cursor_set_visible(console_gc_get_ctx(ui->cgc), false);
if (rc != EOK)
return rc;
return EOK;
}
bool ui_is_suspended(ui_t *ui)
{
return ui->suspended;
}
void ui_lock(ui_t *ui)
{
fibril_mutex_lock(&ui->lock);
}
void ui_unlock(ui_t *ui)
{
fibril_mutex_unlock(&ui->lock);
}
void ui_quit(ui_t *ui)
{
ui->quit = true;
}
bool ui_is_textmode(ui_t *ui)
{
return (ui->console != NULL);
}
bool ui_is_fullscreen(ui_t *ui)
{
return (ui->display == NULL);
}
errno_t ui_get_rect(ui_t *ui, gfx_rect_t *rect)
{
display_info_t info;
sysarg_t cols, rows;
errno_t rc;
if (ui->display != NULL) {
rc = display_get_info(ui->display, &info);
if (rc != EOK)
return rc;
*rect = info.rect;
} else if (ui->console != NULL) {
rc = console_get_size(ui->console, &cols, &rows);
if (rc != EOK)
return rc;
rect->p0.x = 0;
rect->p0.y = 0;
rect->p1.x = cols;
rect->p1.y = rows;
} else {
return ENOTSUP;
}
return EOK;
}
ui_clickmatic_t *ui_get_clickmatic(ui_t *ui)
{
return ui->clickmatic;
}
HelenOS homepage, sources at GitHub