Changeset dc5c303 in mainline for uspace/app/gfxdemo/gfxdemo.c


Ignore:
Timestamp:
2023-12-28T13:59:23Z (2 years ago)
Author:
GitHub <noreply@…>
Children:
6b66de6b
Parents:
42c2e65 (diff), f87ff8e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
boba-buba <120932204+boba-buba@…> (2023-12-28 13:59:23)
git-committer:
GitHub <noreply@…> (2023-12-28 13:59:23)
Message:

Merge branch 'master' into topic/packet-capture

File:
1 edited

Legend:

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

    r42c2e65 rdc5c303  
    11/*
    2  * Copyright (c) 2022 Jiri Svoboda
     2 * Copyright (c) 2023 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5151#include <ui/window.h>
    5252#include <ui/wdecor.h>
     53#include "gfxdemo.h"
    5354
    5455static void wnd_close_event(void *);
     
    6869};
    6970
     71static void demo_kbd_event(kbd_event_t *);
     72
    7073static bool quit = false;
     74static FIBRIL_MUTEX_INITIALIZE(quit_lock);
     75static FIBRIL_CONDVAR_INITIALIZE(quit_cv);
    7176static gfx_typeface_t *tface;
    7277static gfx_font_t *font;
    7378static gfx_coord_t vpad;
     79static console_ctrl_t *con = NULL;
     80static ui_t *ui;
    7481
    7582/** Determine if we are running in text mode.
     
    8390        // XXX Need a proper way to determine text mode
    8491        return w <= 80;
     92}
     93
     94/** Sleep until timeout or quit request.
     95 *
     96 * @param msec Number of microseconds to sleep for
     97 */
     98static 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);
    85128}
    86129
     
    316359                gfx_color_delete(color);
    317360
    318                 fibril_usleep(500 * 1000);
    319 
     361                demo_msleep(500);
    320362                if (quit)
    321363                        break;
     
    478520                        if (rc != EOK)
    479521                                goto error;
    480                         fibril_usleep(250 * 1000);
    481 
     522
     523                        demo_msleep(250);
    482524                        if (quit)
    483525                                goto out;
     
    539581                }
    540582
    541                 fibril_usleep(500 * 1000);
    542 
     583                demo_msleep(500);
    543584                if (quit)
    544585                        break;
     
    600641                }
    601642
    602                 fibril_usleep(500 * 1000);
    603 
     643                demo_msleep(500);
    604644                if (quit)
    605645                        break;
     
    791831
    792832        for (i = 0; i < 10; i++) {
    793                 fibril_usleep(500 * 1000);
     833                demo_msleep(500);
    794834                if (quit)
    795835                        break;
     
    872912
    873913        for (i = 0; i < 10; i++) {
    874                 fibril_usleep(500 * 1000);
     914                demo_msleep(500);
    875915                if (quit)
    876916                        break;
     
    9691009                }
    9701010
    971                 fibril_usleep(500 * 1000);
    972 
     1011                demo_msleep(500);
    9731012                if (quit)
    9741013                        break;
     
    10361075static errno_t demo_console(void)
    10371076{
    1038         console_ctrl_t *con = NULL;
    10391077        console_gc_t *cgc = NULL;
    10401078        gfx_context_t *gc;
    1041         errno_t rc;
    1042 
    1043         printf("Init console..\n");
     1079        sysarg_t cols, rows;
     1080        errno_t rc;
     1081
    10441082        con = console_init(stdin, stdout);
    10451083        if (con == NULL)
    10461084                return EIO;
    10471085
    1048         printf("Create console GC\n");
     1086        rc = console_get_size(con, &cols, &rows);
     1087        if (rc != EOK)
     1088                return rc;
     1089
    10491090        rc = console_gc_create(con, stdout, &cgc);
    10501091        if (rc != EOK)
     
    10531094        gc = console_gc_get_ctx(cgc);
    10541095
    1055         rc = demo_loop(gc, 80, 25);
     1096        rc = demo_loop(gc, cols, rows);
    10561097        if (rc != EOK)
    10571098                return rc;
     
    10621103
    10631104        return EOK;
     1105}
     1106
     1107static 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;
    10641117}
    10651118
     
    10671120static errno_t demo_ui(const char *display_spec)
    10681121{
    1069         ui_t *ui = NULL;
    10701122        ui_wnd_params_t params;
    10711123        ui_window_t *window = NULL;
     
    10741126        gfx_rect_t wrect;
    10751127        gfx_coord2_t off;
    1076         errno_t rc;
    1077 
    1078         printf("Init UI..\n");
     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;
    10791133
    10801134        rc = ui_create(display_spec, &ui);
    10811135        if (rc != EOK) {
    10821136                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");
    10831143                goto error;
    10841144        }
     
    10911151        ui_wnd_params_init(&params);
    10921152        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;
    10931157
    10941158        /*
     
    11001164        gfx_rect_rtranslate(&off, &wrect, &params.rect);
    11011165
     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
    11021174        rc = ui_window_create(ui, &params, &window);
    11031175        if (rc != EOK) {
     
    11141186        }
    11151187
    1116         task_retval(0);
    1117 
    1118         rc = demo_loop(gc, rect.p1.x, rect.p1.y);
    1119         if (rc != EOK)
    1120                 goto error;
    1121 
     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);
    11221207        ui_window_destroy(window);
    11231208        ui_destroy(ui);
     
    11411226        errno_t rc;
    11421227
    1143         printf("Init display..\n");
    1144 
    11451228        rc = display_open(display_svc, &display);
    11461229        if (rc != EOK) {
     
    11841267}
    11851268
     1269static void demo_quit(void)
     1270{
     1271        fibril_mutex_lock(&quit_lock);
     1272        quit = true;
     1273        fibril_mutex_unlock(&quit_lock);
     1274        fibril_condvar_broadcast(&quit_cv);
     1275}
     1276
    11861277static void wnd_close_event(void *arg)
    11871278{
    1188         printf("Close event\n");
    1189         quit = true;
     1279        demo_quit();
     1280}
     1281
     1282static 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        }
    11901301}
    11911302
    11921303static void wnd_kbd_event(void *arg, kbd_event_t *event)
    11931304{
    1194         printf("Keyboard event type=%d key=%d\n", event->type, event->key);
    1195         if (event->type == KEY_PRESS)
    1196                 quit = true;
     1305        (void)arg;
     1306        demo_kbd_event(event);
    11971307}
    11981308
    11991309static void uiwnd_close_event(ui_window_t *window, void *arg)
    12001310{
    1201         printf("Close event\n");
    1202         quit = true;
     1311        demo_quit();
    12031312}
    12041313
    12051314static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event)
    12061315{
    1207         printf("Keyboard event type=%d key=%d\n", event->type, event->key);
    1208         if (event->type == KEY_PRESS)
    1209                 quit = true;
     1316        (void)window;
     1317        (void)arg;
     1318        demo_kbd_event(event);
    12101319}
    12111320
     
    12191328        errno_t rc;
    12201329        const char *display_svc = DISPLAY_DEFAULT;
    1221         const char *ui_display_spec = UI_DISPLAY_DEFAULT;
     1330        const char *ui_display_spec = UI_ANY_DEFAULT;
    12221331        int i;
    12231332
Note: See TracChangeset for help on using the changeset viewer.