Ignore:
File:
1 edited

Legend:

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

    r3d588be r400a16d  
    11/*
    2  * Copyright (c) 2024 Jiri Svoboda
     2 * Copyright (c) 2021 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 bool textmode;
    81 static ui_t *ui;
    8274
    8375/** Determine if we are running in text mode.
    8476 *
     77 * @param w Screen width
     78 * @param h Screen height
    8579 * @return @c true iff we are running in text mode
    8680 */
    87 static bool demo_is_text(void)
    88 {
    89         return textmode;
    90 }
    91 
    92 /** Sleep until timeout or quit request.
    93  *
    94  * @param msec Number of microseconds to sleep for
    95  */
    96 static void demo_msleep(unsigned msec)
    97 {
    98         errno_t rc;
    99         usec_t usec;
    100         cons_event_t cevent;
    101 
    102         if (ui != NULL)
    103                 ui_unlock(ui);
    104         fibril_mutex_lock(&quit_lock);
    105         if (!quit) {
    106                 if (con != NULL) {
    107                         usec = (usec_t)msec * 1000;
    108                         while (usec > 0 && !quit) {
    109                                 rc = console_get_event_timeout(con, &cevent, &usec);
    110                                 if (rc == EOK) {
    111                                         if (cevent.type == CEV_KEY) {
    112                                                 fibril_mutex_unlock(&quit_lock);
    113                                                 demo_kbd_event(&cevent.ev.key);
    114                                                 fibril_mutex_lock(&quit_lock);
    115                                         }
    116                                 }
    117                         }
    118                 } else {
    119                         (void) fibril_condvar_wait_timeout(&quit_cv, &quit_lock,
    120                             (usec_t)msec * 1000);
    121                 }
    122         }
    123         fibril_mutex_unlock(&quit_lock);
    124         if (ui != NULL)
    125                 ui_lock(ui);
     81static bool demo_is_text(gfx_coord_t w, gfx_coord_t h)
     82{
     83        // XXX Need a proper way to determine text mode
     84        return w <= 80;
    12685}
    12786
     
    179138
    180139        /* XXX Crude way of detecting text mode */
    181         if (demo_is_text()) {
     140        if (w < 256) {
    182141                /* Create dummy font for text mode */
    183142                rc = gfx_typeface_create(gc, &tface);
     
    268227
    269228        if (font != NULL) {
    270                 if (demo_is_text()) {
     229                if (demo_is_text(w, h)) {
    271230                        rc = gfx_color_new_ega(0x1e, &color);
    272231                        if (rc != EOK)
     
    279238
    280239                gfx_text_fmt_init(&fmt);
    281                 fmt.font = font;
    282240                fmt.color = color;
    283241                fmt.halign = gfx_halign_center;
     
    285243
    286244                pos.x = w / 2;
    287                 pos.y = h;
    288                 rc = gfx_puttext(&pos, &fmt, text);
     245                pos.y = h - 1;
     246                rc = gfx_puttext(font, &pos, &fmt, text);
    289247                if (rc != EOK) {
    290248                        printf("Error rendering text.\n");
     
    357315                gfx_color_delete(color);
    358316
    359                 demo_msleep(500);
     317                fibril_usleep(500 * 1000);
     318
    360319                if (quit)
    361320                        break;
     
    391350                for (j = 0; j < h; j++) {
    392351                        pixelmap_put_pixel(&pixelmap, i, j,
    393                             PIXEL(255, (i % 30) < 3 ? 255 : 0,
     352                            PIXEL(0, (i % 30) < 3 ? 255 : 0,
    394353                            (j % 30) < 3 ? 255 : 0, i / 2));
    395354                }
     
    427386                        k = i * i + j * j;
    428387                        pixelmap_put_pixel(&pixelmap, i, j,
    429                             PIXEL(255, k, k, k));
     388                            PIXEL(0, k, k, k));
    430389                }
    431390        }
     
    462421                        k = i * i + j * j;
    463422                        pixelmap_put_pixel(&pixelmap, i, j,
    464                             k < w * w / 2 ? PIXEL(255, 0, 255, 0) :
    465                             PIXEL(255, 255, 0, 255));
     423                            k < w * w / 2 ? PIXEL(0, 0, 255, 0) :
     424                            PIXEL(0, 255, 0, 255));
    466425                }
    467426        }
     
    518477                        if (rc != EOK)
    519478                                goto error;
    520 
    521                         demo_msleep(250);
     479                        fibril_usleep(250 * 1000);
     480
    522481                        if (quit)
    523482                                goto out;
     
    579538                }
    580539
    581                 demo_msleep(500);
     540                fibril_usleep(500 * 1000);
     541
    582542                if (quit)
    583543                        break;
     
    619579        params.rect.p1.y = 40;
    620580        params.flags = bmpf_color_key;
    621         params.key_color = PIXEL(255, 255, 0, 255);
     581        params.key_color = PIXEL(0, 255, 0, 255);
    622582
    623583        rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
     
    639599                }
    640600
    641                 demo_msleep(500);
     601                fibril_usleep(500 * 1000);
     602
    642603                if (quit)
    643604                        break;
     
    717678        gfx_color_delete(color);
    718679
    719         if (demo_is_text()) {
     680        if (demo_is_text(w, h)) {
    720681                rc = gfx_color_new_ega(0x1f, &color);
    721682                if (rc != EOK)
     
    728689
    729690        gfx_text_fmt_init(&fmt);
    730         fmt.font = font;
    731691        fmt.color = color;
    732692
    733693        pos.x = rect.p0.x;
    734694        pos.y = rect.p0.y;
    735         rc = gfx_puttext(&pos, &fmt, "Top left");
     695        rc = gfx_puttext(font, &pos, &fmt, "Top left");
    736696        if (rc != EOK) {
    737697                printf("Error rendering text.\n");
     
    742702        pos.y = rect.p0.y;
    743703        fmt.halign = gfx_halign_center;
    744         rc = gfx_puttext(&pos, &fmt, "Top center");
     704        rc = gfx_puttext(font, &pos, &fmt, "Top center");
    745705        if (rc != EOK)
    746706                goto error;
     
    749709        pos.y = rect.p0.y;
    750710        fmt.halign = gfx_halign_right;
    751         rc = gfx_puttext(&pos, &fmt, "Top right");
     711        rc = gfx_puttext(font, &pos, &fmt, "Top right");
    752712        if (rc != EOK)
    753713                goto error;
     
    758718        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    759719        fmt.halign = gfx_halign_left;
    760         rc = gfx_puttext(&pos, &fmt, "Center left");
     720        rc = gfx_puttext(font, &pos, &fmt, "Center left");
    761721        if (rc != EOK)
    762722                goto error;
     
    765725        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    766726        fmt.halign = gfx_halign_center;
    767         rc = gfx_puttext(&pos, &fmt, "Center");
     727        rc = gfx_puttext(font, &pos, &fmt, "Center");
    768728        if (rc != EOK)
    769729                goto error;
     
    772732        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    773733        fmt.halign = gfx_halign_right;
    774         rc = gfx_puttext(&pos, &fmt, "Center right");
     734        rc = gfx_puttext(font, &pos, &fmt, "Center right");
    775735        if (rc != EOK)
    776736                goto error;
     
    781741        pos.y = rect.p1.y - 1;
    782742        fmt.halign = gfx_halign_left;
    783         rc = gfx_puttext(&pos, &fmt, "Bottom left");
     743        rc = gfx_puttext(font, &pos, &fmt, "Bottom left");
    784744        if (rc != EOK)
    785745                goto error;
     
    788748        pos.y = rect.p1.y;
    789749        fmt.halign = gfx_halign_center;
    790         rc = gfx_puttext(&pos, &fmt, "Bottom center");
     750        rc = gfx_puttext(font, &pos, &fmt, "Bottom center");
    791751        if (rc != EOK)
    792752                goto error;
     
    795755        pos.y = rect.p1.y;
    796756        fmt.halign = gfx_halign_right;
    797         rc = gfx_puttext(&pos, &fmt, "Bottom right");
     757        rc = gfx_puttext(font, &pos, &fmt, "Bottom right");
    798758        if (rc != EOK)
    799759                goto error;
     
    802762
    803763        gfx_text_fmt_init(&fmt);
    804         fmt.font = font;
    805764
    806765        for (i = 0; i < 8; i++) {
    807                 if (demo_is_text()) {
     766                if (demo_is_text(w, h)) {
    808767                        rc = gfx_color_new_ega(i != 0 ? i : 0x10, &color);
    809768                        if (rc != EOK)
     
    817776
    818777                fmt.color = color;
    819                 fmt.underline = !fmt.underline;
    820778
    821779                pos.x = w / 20;
    822780                pos.y = (6 + i) * h / 15;
    823                 rc = gfx_puttext(&pos, &fmt, "The quick brown fox jumps over the lazy dog.");
     781                rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
    824782                if (rc != EOK)
    825783                        goto error;
     
    829787
    830788        for (i = 0; i < 10; i++) {
    831                 demo_msleep(500);
    832                 if (quit)
    833                         break;
    834         }
    835 
    836         return EOK;
    837 error:
    838         return rc;
    839 }
    840 
    841 /** Run text abbreviation demo on a graphic context.
    842  *
    843  * @param gc Graphic context
    844  * @param w Width
    845  * @param h Height
    846  */
    847 static errno_t demo_text_abbr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
    848 {
    849         gfx_color_t *color = NULL;
    850         gfx_rect_t rect;
    851         gfx_coord2_t pos;
    852         gfx_text_fmt_t fmt;
    853         int i;
    854         errno_t rc;
    855 
    856         if (quit)
    857                 return EOK;
    858 
    859         rc = demo_begin(gc, w, h, "Text abbreviation");
    860         if (rc != EOK)
    861                 goto error;
    862 
    863         for (i = 0; i < 11; i++) {
    864 
    865                 rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color);
    866                 if (rc != EOK)
    867                         goto error;
    868 
    869                 rc = gfx_set_color(gc, color);
    870                 if (rc != EOK)
    871                         goto error;
    872 
    873                 rect.p0.x = w / 20;
    874                 rect.p0.y = (2 + 2 * i) * h / 25;
    875                 rect.p1.x = w - w / 20 - w * i / 12;
    876                 rect.p1.y = (3 + 2 * i) * h / 25;
    877 
    878                 rc = gfx_fill_rect(gc, &rect);
    879                 if (rc != EOK)
    880                         goto error;
    881 
    882                 gfx_color_delete(color);
    883 
    884                 if (demo_is_text()) {
    885                         rc = gfx_color_new_ega(0x1f, &color);
    886                         if (rc != EOK)
    887                                 goto error;
    888                 } else {
    889                         rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
    890                             &color);
    891                         if (rc != EOK)
    892                                 goto error;
    893                 }
    894 
    895                 gfx_text_fmt_init(&fmt);
    896                 fmt.font = font;
    897                 fmt.color = color;
    898                 fmt.abbreviate = true;
    899                 fmt.width = rect.p1.x - rect.p0.x;
    900 
    901                 pos.x = rect.p0.x;
    902                 pos.y = rect.p0.y;
    903                 rc = gfx_puttext(&pos, &fmt,
    904                     "The quick brow fox jumps over the lazy dog!");
    905                 if (rc != EOK) {
    906                         printf("Error rendering text.\n");
    907                         goto error;
    908                 }
    909         }
    910 
    911         for (i = 0; i < 10; i++) {
    912                 demo_msleep(500);
     789                fibril_usleep(500 * 1000);
    913790                if (quit)
    914791                        break;
     
    1007884                }
    1008885
    1009                 demo_msleep(500);
     886                fibril_usleep(500 * 1000);
     887
    1010888                if (quit)
    1011889                        break;
     
    1054932                        goto error;
    1055933
    1056                 rc = demo_text_abbr(gc, w, h);
    1057                 if (rc != EOK)
    1058                         goto error;
    1059 
    1060934                rc = demo_clip(gc, w, h);
    1061935                if (rc != EOK)
     
    1073947static errno_t demo_console(void)
    1074948{
     949        console_ctrl_t *con = NULL;
    1075950        console_gc_t *cgc = NULL;
    1076951        gfx_context_t *gc;
    1077         sysarg_t cols, rows;
    1078         errno_t rc;
    1079 
     952        errno_t rc;
     953
     954        printf("Init console..\n");
    1080955        con = console_init(stdin, stdout);
    1081956        if (con == NULL)
    1082957                return EIO;
    1083958
    1084         rc = console_get_size(con, &cols, &rows);
    1085         if (rc != EOK)
    1086                 return rc;
    1087 
     959        printf("Create console GC\n");
    1088960        rc = console_gc_create(con, stdout, &cgc);
    1089961        if (rc != EOK)
     
    1092964        gc = console_gc_get_ctx(cgc);
    1093965
    1094         /* Currently console is always text. */
    1095         textmode = true;
    1096 
    1097         rc = demo_loop(gc, cols, rows);
     966        rc = demo_loop(gc, 80, 25);
    1098967        if (rc != EOK)
    1099968                return rc;
     
    1104973
    1105974        return EOK;
    1106 }
    1107 
    1108 static errno_t demo_ui_fibril(void *arg)
    1109 {
    1110         demo_ui_args_t *args = (demo_ui_args_t *)arg;
    1111         errno_t rc;
    1112 
    1113         ui_lock(args->ui);
    1114         rc = demo_loop(args->gc, args->dims.x, args->dims.y);
    1115         ui_unlock(args->ui);
    1116         ui_quit(args->ui);
    1117         return rc;
    1118975}
    1119976
     
    1121978static errno_t demo_ui(const char *display_spec)
    1122979{
     980        ui_t *ui = NULL;
    1123981        ui_wnd_params_t params;
    1124982        ui_window_t *window = NULL;
     
    1127985        gfx_rect_t wrect;
    1128986        gfx_coord2_t off;
    1129         gfx_rect_t ui_rect;
    1130         gfx_coord2_t dims;
    1131         demo_ui_args_t args;
    1132         fid_t fid;
    1133         errno_t rc;
     987        errno_t rc;
     988
     989        printf("Init UI..\n");
    1134990
    1135991        rc = ui_create(display_spec, &ui);
    1136992        if (rc != EOK) {
    1137993                printf("Error initializing UI (%s)\n", display_spec);
    1138                 goto error;
    1139         }
    1140 
    1141         rc = ui_get_rect(ui, &ui_rect);
    1142         if (rc != EOK) {
    1143                 printf("Error getting display size.\n");
    1144994                goto error;
    1145995        }
     
    11531003        params.caption = "GFX Demo";
    11541004
    1155         /* Do not decorate the window in fullscreen mode */
    1156         if (ui_is_fullscreen(ui))
    1157                 params.style &= ~ui_wds_decorated;
    1158 
    11591005        /*
    11601006         * Compute window rectangle such that application area corresponds
    11611007         * to rect
    11621008         */
    1163         ui_wdecor_rect_from_app(ui, params.style, &rect, &wrect);
     1009        ui_wdecor_rect_from_app(params.style, &rect, &wrect);
    11641010        off = wrect.p0;
    11651011        gfx_rect_rtranslate(&off, &wrect, &params.rect);
    1166 
    1167         gfx_rect_dims(&ui_rect, &dims);
    1168 
    1169         /* Make sure window is not larger than the entire screen */
    1170         if (params.rect.p1.x > dims.x)
    1171                 params.rect.p1.x = dims.x;
    1172         if (params.rect.p1.y > dims.y)
    1173                 params.rect.p1.y = dims.y;
    11741012
    11751013        rc = ui_window_create(ui, &params, &window);
     
    11871025        }
    11881026
    1189         ui_window_get_app_rect(window, &rect);
    1190         gfx_rect_dims(&rect, &dims);
    1191 
    1192         if (!ui_is_fullscreen(ui))
    1193                 task_retval(0);
    1194 
    1195         textmode = ui_is_textmode(ui);
    1196 
    1197         args.gc = gc;
    1198         args.dims = dims;
    1199         args.ui = ui;
    1200 
    1201         fid = fibril_create(demo_ui_fibril, (void *)&args);
    1202         if (fid == 0) {
    1203                 rc = ENOMEM;
    1204                 goto error;
    1205         }
    1206 
    1207         fibril_add_ready(fid);
    1208 
    1209         ui_run(ui);
     1027        task_retval(0);
     1028
     1029        rc = demo_loop(gc, rect.p1.x, rect.p1.y);
     1030        if (rc != EOK)
     1031                goto error;
     1032
    12101033        ui_window_destroy(window);
    12111034        ui_destroy(ui);
     
    12291052        errno_t rc;
    12301053
     1054        printf("Init display..\n");
     1055
    12311056        rc = display_open(display_svc, &display);
    12321057        if (rc != EOK) {
     
    12401065        params.rect.p1.x = 400;
    12411066        params.rect.p1.y = 300;
    1242         params.caption = "GFX Demo";
    12431067
    12441068        rc = display_window_create(display, &params, &wnd_cb, NULL, &window);
     
    12561080        task_retval(0);
    12571081
    1258         /* FIXME Assuming display service is not text mode. */
    1259         textmode = false;
    1260 
    12611082        rc = demo_loop(gc, 400, 300);
    12621083        if (rc != EOK)
     
    12731094}
    12741095
    1275 static void demo_quit(void)
    1276 {
    1277         fibril_mutex_lock(&quit_lock);
     1096static void wnd_close_event(void *arg)
     1097{
     1098        printf("Close event\n");
    12781099        quit = true;
    1279         fibril_mutex_unlock(&quit_lock);
    1280         fibril_condvar_broadcast(&quit_cv);
    1281 }
    1282 
    1283 static void wnd_close_event(void *arg)
    1284 {
    1285         demo_quit();
    1286 }
    1287 
    1288 static void demo_kbd_event(kbd_event_t *event)
    1289 {
    1290         if (event->type == KEY_PRESS) {
    1291                 /* Ctrl-Q */
    1292                 if ((event->mods & KM_CTRL) != 0 &&
    1293                     (event->mods & KM_ALT) == 0 &&
    1294                     (event->mods & KM_SHIFT) == 0 &&
    1295                     event->key == KC_Q) {
    1296                         demo_quit();
    1297                 }
    1298 
    1299                 /* Escape */
    1300                 if ((event->mods & KM_CTRL) == 0 &&
    1301                     (event->mods & KM_ALT) == 0 &&
    1302                     (event->mods & KM_SHIFT) == 0 &&
    1303                     event->key == KC_ESCAPE) {
    1304                         demo_quit();
    1305                 }
    1306         }
    13071100}
    13081101
    13091102static void wnd_kbd_event(void *arg, kbd_event_t *event)
    13101103{
    1311         (void)arg;
    1312         demo_kbd_event(event);
     1104        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     1105        if (event->type == KEY_PRESS)
     1106                quit = true;
    13131107}
    13141108
    13151109static void uiwnd_close_event(ui_window_t *window, void *arg)
    13161110{
    1317         demo_quit();
     1111        printf("Close event\n");
     1112        quit = true;
    13181113}
    13191114
    13201115static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event)
    13211116{
    1322         (void)window;
    1323         (void)arg;
    1324         demo_kbd_event(event);
     1117        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     1118        if (event->type == KEY_PRESS)
     1119                quit = true;
    13251120}
    13261121
     
    13341129        errno_t rc;
    13351130        const char *display_svc = DISPLAY_DEFAULT;
    1336         const char *ui_display_spec = UI_ANY_DEFAULT;
    13371131        int i;
    13381132
     
    13471141                        }
    13481142
    1349                         display_svc = ui_display_spec = argv[i++];
     1143                        display_svc = argv[i++];
    13501144                } else {
    13511145                        printf("Invalid option '%s'.\n", argv[i]);
     
    13641158                        return 1;
    13651159        } else if (str_cmp(argv[i], "ui") == 0) {
    1366                 rc = demo_ui(ui_display_spec);
     1160                rc = demo_ui(display_svc);
    13671161                if (rc != EOK)
    13681162                        return 1;
Note: See TracChangeset for help on using the changeset viewer.