Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/gfxdemo/gfxdemo.c

    r9e240c1 r901b302  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
     2 * Copyright (c) 2022 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5151#include <ui/window.h>
    5252#include <ui/wdecor.h>
    53 #include "gfxdemo.h"
    5453
    5554static void wnd_close_event(void *);
     
    6968};
    7069
    71 static void demo_kbd_event(kbd_event_t *);
    72 
    7370static bool quit = false;
    74 static FIBRIL_MUTEX_INITIALIZE(quit_lock);
    75 static FIBRIL_CONDVAR_INITIALIZE(quit_cv);
    7671static gfx_typeface_t *tface;
    7772static gfx_font_t *font;
    7873static gfx_coord_t vpad;
    79 static console_ctrl_t *con = NULL;
    80 static ui_t *ui;
    8174
    8275/** Determine if we are running in text mode.
     
    9083        // XXX Need a proper way to determine text mode
    9184        return w <= 80;
    92 }
    93 
    94 /** Sleep until timeout or quit request.
    95  *
    96  * @param msec Number of microseconds to sleep for
    97  */
    98 static void demo_msleep(unsigned msec)
    99 {
    100         errno_t rc;
    101         usec_t usec;
    102         cons_event_t cevent;
    103 
    104         if (ui != NULL)
    105                 ui_unlock(ui);
    106         fibril_mutex_lock(&quit_lock);
    107         if (!quit) {
    108                 if (con != NULL) {
    109                         usec = (usec_t)msec * 1000;
    110                         while (usec > 0 && !quit) {
    111                                 rc = console_get_event_timeout(con, &cevent, &usec);
    112                                 if (rc == EOK) {
    113                                         if (cevent.type == CEV_KEY) {
    114                                                 fibril_mutex_unlock(&quit_lock);
    115                                                 demo_kbd_event(&cevent.ev.key);
    116                                                 fibril_mutex_lock(&quit_lock);
    117                                         }
    118                                 }
    119                         }
    120                 } else {
    121                         (void) fibril_condvar_wait_timeout(&quit_cv, &quit_lock,
    122                             (usec_t)msec * 1000);
    123                 }
    124         }
    125         fibril_mutex_unlock(&quit_lock);
    126         if (ui != NULL)
    127                 ui_lock(ui);
    12885}
    12986
     
    359316                gfx_color_delete(color);
    360317
    361                 demo_msleep(500);
     318                fibril_usleep(500 * 1000);
     319
    362320                if (quit)
    363321                        break;
     
    520478                        if (rc != EOK)
    521479                                goto error;
    522 
    523                         demo_msleep(250);
     480                        fibril_usleep(250 * 1000);
     481
    524482                        if (quit)
    525483                                goto out;
     
    581539                }
    582540
    583                 demo_msleep(500);
     541                fibril_usleep(500 * 1000);
     542
    584543                if (quit)
    585544                        break;
     
    641600                }
    642601
    643                 demo_msleep(500);
     602                fibril_usleep(500 * 1000);
     603
    644604                if (quit)
    645605                        break;
     
    831791
    832792        for (i = 0; i < 10; i++) {
    833                 demo_msleep(500);
     793                fibril_usleep(500 * 1000);
    834794                if (quit)
    835795                        break;
     
    912872
    913873        for (i = 0; i < 10; i++) {
    914                 demo_msleep(500);
     874                fibril_usleep(500 * 1000);
    915875                if (quit)
    916876                        break;
     
    1009969                }
    1010970
    1011                 demo_msleep(500);
     971                fibril_usleep(500 * 1000);
     972
    1012973                if (quit)
    1013974                        break;
     
    10751036static errno_t demo_console(void)
    10761037{
     1038        console_ctrl_t *con = NULL;
    10771039        console_gc_t *cgc = NULL;
    10781040        gfx_context_t *gc;
    1079         sysarg_t cols, rows;
    1080         errno_t rc;
    1081 
     1041        errno_t rc;
     1042
     1043        printf("Init console..\n");
    10821044        con = console_init(stdin, stdout);
    10831045        if (con == NULL)
    10841046                return EIO;
    10851047
    1086         rc = console_get_size(con, &cols, &rows);
    1087         if (rc != EOK)
    1088                 return rc;
    1089 
     1048        printf("Create console GC\n");
    10901049        rc = console_gc_create(con, stdout, &cgc);
    10911050        if (rc != EOK)
     
    10941053        gc = console_gc_get_ctx(cgc);
    10951054
    1096         rc = demo_loop(gc, cols, rows);
     1055        rc = demo_loop(gc, 80, 25);
    10971056        if (rc != EOK)
    10981057                return rc;
     
    11031062
    11041063        return EOK;
    1105 }
    1106 
    1107 static errno_t demo_ui_fibril(void *arg)
    1108 {
    1109         demo_ui_args_t *args = (demo_ui_args_t *)arg;
    1110         errno_t rc;
    1111 
    1112         ui_lock(args->ui);
    1113         rc = demo_loop(args->gc, args->dims.x, args->dims.y);
    1114         ui_unlock(args->ui);
    1115         ui_quit(args->ui);
    1116         return rc;
    11171064}
    11181065
     
    11201067static errno_t demo_ui(const char *display_spec)
    11211068{
     1069        ui_t *ui = NULL;
    11221070        ui_wnd_params_t params;
    11231071        ui_window_t *window = NULL;
     
    11261074        gfx_rect_t wrect;
    11271075        gfx_coord2_t off;
    1128         gfx_rect_t ui_rect;
    1129         gfx_coord2_t dims;
    1130         demo_ui_args_t args;
    1131         fid_t fid;
    1132         errno_t rc;
     1076        errno_t rc;
     1077
     1078        printf("Init UI..\n");
    11331079
    11341080        rc = ui_create(display_spec, &ui);
    11351081        if (rc != EOK) {
    11361082                printf("Error initializing UI (%s)\n", display_spec);
    1137                 goto error;
    1138         }
    1139 
    1140         rc = ui_get_rect(ui, &ui_rect);
    1141         if (rc != EOK) {
    1142                 printf("Error getting display size.\n");
    11431083                goto error;
    11441084        }
     
    11511091        ui_wnd_params_init(&params);
    11521092        params.caption = "GFX Demo";
    1153 
    1154         /* Do not decorate the window in fullscreen mode */
    1155         if (ui_is_fullscreen(ui))
    1156                 params.style &= ~ui_wds_decorated;
    11571093
    11581094        /*
     
    11641100        gfx_rect_rtranslate(&off, &wrect, &params.rect);
    11651101
    1166         gfx_rect_dims(&ui_rect, &dims);
    1167 
    1168         /* Make sure window is not larger than the entire screen */
    1169         if (params.rect.p1.x > dims.x)
    1170                 params.rect.p1.x = dims.x;
    1171         if (params.rect.p1.y > dims.y)
    1172                 params.rect.p1.y = dims.y;
    1173 
    11741102        rc = ui_window_create(ui, &params, &window);
    11751103        if (rc != EOK) {
     
    11861114        }
    11871115
    1188         ui_window_get_app_rect(window, &rect);
    1189         gfx_rect_dims(&rect, &dims);
    1190 
    1191         if (!ui_is_fullscreen(ui))
    1192                 task_retval(0);
    1193 
    1194         args.gc = gc;
    1195         args.dims = dims;
    1196         args.ui = ui;
    1197 
    1198         fid = fibril_create(demo_ui_fibril, (void *)&args);
    1199         if (fid == 0) {
    1200                 rc = ENOMEM;
    1201                 goto error;
    1202         }
    1203 
    1204         fibril_add_ready(fid);
    1205 
    1206         ui_run(ui);
     1116        task_retval(0);
     1117
     1118        rc = demo_loop(gc, rect.p1.x, rect.p1.y);
     1119        if (rc != EOK)
     1120                goto error;
     1121
    12071122        ui_window_destroy(window);
    12081123        ui_destroy(ui);
     
    12261141        errno_t rc;
    12271142
     1143        printf("Init display..\n");
     1144
    12281145        rc = display_open(display_svc, &display);
    12291146        if (rc != EOK) {
     
    12671184}
    12681185
    1269 static void demo_quit(void)
    1270 {
    1271         fibril_mutex_lock(&quit_lock);
     1186static void wnd_close_event(void *arg)
     1187{
     1188        printf("Close event\n");
    12721189        quit = true;
    1273         fibril_mutex_unlock(&quit_lock);
    1274         fibril_condvar_broadcast(&quit_cv);
    1275 }
    1276 
    1277 static void wnd_close_event(void *arg)
    1278 {
    1279         demo_quit();
    1280 }
    1281 
    1282 static void demo_kbd_event(kbd_event_t *event)
    1283 {
    1284         if (event->type == KEY_PRESS) {
    1285                 /* Ctrl-Q */
    1286                 if ((event->mods & KM_CTRL) != 0 &&
    1287                     (event->mods & KM_ALT) == 0 &&
    1288                     (event->mods & KM_SHIFT) == 0 &&
    1289                     event->key == KC_Q) {
    1290                         demo_quit();
    1291                 }
    1292 
    1293                 /* Escape */
    1294                 if ((event->mods & KM_CTRL) == 0 &&
    1295                     (event->mods & KM_ALT) == 0 &&
    1296                     (event->mods & KM_SHIFT) == 0 &&
    1297                     event->key == KC_ESCAPE) {
    1298                         demo_quit();
    1299                 }
    1300         }
    13011190}
    13021191
    13031192static void wnd_kbd_event(void *arg, kbd_event_t *event)
    13041193{
    1305         (void)arg;
    1306         demo_kbd_event(event);
     1194        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     1195        if (event->type == KEY_PRESS)
     1196                quit = true;
    13071197}
    13081198
    13091199static void uiwnd_close_event(ui_window_t *window, void *arg)
    13101200{
    1311         demo_quit();
     1201        printf("Close event\n");
     1202        quit = true;
    13121203}
    13131204
    13141205static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event)
    13151206{
    1316         (void)window;
    1317         (void)arg;
    1318         demo_kbd_event(event);
     1207        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     1208        if (event->type == KEY_PRESS)
     1209                quit = true;
    13191210}
    13201211
     
    13281219        errno_t rc;
    13291220        const char *display_svc = DISPLAY_DEFAULT;
    1330         const char *ui_display_spec = UI_ANY_DEFAULT;
     1221        const char *ui_display_spec = UI_DISPLAY_DEFAULT;
    13311222        int i;
    13321223
Note: See TracChangeset for help on using the changeset viewer.