Ignore:
File:
1 edited

Legend:

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

    r266ec54 rd0dfbba  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
     2 * Copyright (c) 2026 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#include <display.h>
    3737#include <fibril.h>
     38#include <fibril_synch.h>
    3839#include <gfx/bitmap.h>
    3940#include <gfx/color.h>
     
    5152#include <ui/window.h>
    5253#include <ui/wdecor.h>
     54#include "gfxdemo.h"
    5355
    5456static void wnd_close_event(void *);
     
    6062};
    6163
     64static void uiwnd_resize_event(ui_window_t *, void *);
    6265static void uiwnd_close_event(ui_window_t *, void *);
    6366static void uiwnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
    6467
    6568static ui_window_cb_t ui_window_cb = {
     69        .resize = uiwnd_resize_event,
    6670        .close = uiwnd_close_event,
    6771        .kbd = uiwnd_kbd_event
    6872};
    6973
     74static void demo_kbd_event(kbd_event_t *);
     75
    7076static bool quit = false;
     77static FIBRIL_MUTEX_INITIALIZE(quit_lock);
     78static FIBRIL_CONDVAR_INITIALIZE(quit_cv);
     79static gfx_typeface_t *tface;
     80static gfx_font_t *font;
     81static gfx_coord_t vpad;
     82static console_ctrl_t *con = NULL;
     83static bool textmode;
     84static console_gc_t *cgc = NULL;
     85static unsigned scr_width, scr_height;
     86static ui_t *ui;
     87
     88/** Determine if we are running in text mode.
     89 *
     90 * @return @c true iff we are running in text mode
     91 */
     92static bool demo_is_text(void)
     93{
     94        return textmode;
     95}
     96
     97/** Sleep until timeout or quit request.
     98 *
     99 * @param msec Number of microseconds to sleep for
     100 */
     101static void demo_msleep(unsigned msec)
     102{
     103        errno_t rc;
     104        usec_t usec;
     105        cons_event_t cevent;
     106        sysarg_t cols, rows;
     107
     108        if (ui != NULL)
     109                ui_unlock(ui);
     110        fibril_mutex_lock(&quit_lock);
     111        if (!quit) {
     112                if (con != NULL) {
     113                        usec = (usec_t)msec * 1000;
     114                        while (usec > 0 && !quit) {
     115                                rc = console_get_event_timeout(con, &cevent, &usec);
     116                                if (rc == EOK) {
     117                                        if (cevent.type == CEV_KEY) {
     118                                                fibril_mutex_unlock(&quit_lock);
     119                                                demo_kbd_event(&cevent.ev.key);
     120                                                fibril_mutex_lock(&quit_lock);
     121                                        } else if (cevent.type == CEV_RESIZE) {
     122                                                rc = console_get_size(con,
     123                                                    &cols, &rows);
     124                                                if (rc == EOK) {
     125                                                        scr_width = cols;
     126                                                        scr_height = rows;
     127                                                }
     128                                                rc = console_gc_resize(cgc);
     129                                                if (rc != EOK)
     130                                                        exit(1);
     131                                        }
     132                                }
     133                        }
     134                } else {
     135                        (void) fibril_condvar_wait_timeout(&quit_cv, &quit_lock,
     136                            (usec_t)msec * 1000);
     137                }
     138        }
     139        fibril_mutex_unlock(&quit_lock);
     140        if (ui != NULL)
     141                ui_lock(ui);
     142}
    71143
    72144/** Clear screen.
     
    107179}
    108180
    109 /** Run rectangle demo on a graphic context.
     181/** Initialize demo font.
    110182 *
    111183 * @param gc Graphic context
    112184 * @param w Width
    113185 * @param h Height
     186 * @return EOK on success or an error code
     187 */
     188static errno_t demo_font_init(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     189{
     190        gfx_font_info_t *finfo;
     191        errno_t rc;
     192
     193        if (quit)
     194                return EOK;
     195
     196        /* XXX Crude way of detecting text mode */
     197        if (demo_is_text()) {
     198                /* Create dummy font for text mode */
     199                rc = gfx_typeface_create(gc, &tface);
     200                if (rc != EOK) {
     201                        printf("Error creating typeface\n");
     202                        goto error;
     203                }
     204
     205                rc = gfx_font_create_textmode(tface, &font);
     206                if (rc != EOK) {
     207                        printf("Error creating font\n");
     208                        goto error;
     209                }
     210
     211                vpad = 0;
     212        } else {
     213                /* Load font */
     214                rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface);
     215                if (rc != EOK) {
     216                        printf("Error opening typeface\n");
     217                        goto error;
     218                }
     219
     220                finfo = gfx_typeface_first_font(tface);
     221                if (finfo == NULL) {
     222                        printf("Typeface contains no font.\n");
     223                        rc = ENOENT;
     224                        goto error;
     225                }
     226
     227                rc = gfx_font_open(finfo, &font);
     228                if (rc != EOK) {
     229                        printf("Error opening font.\n");
     230                        goto error;
     231                }
     232
     233                vpad = 5;
     234        }
     235
     236        return EOK;
     237error:
     238        if (tface != NULL)
     239                gfx_typeface_destroy(tface);
     240        return rc;
     241}
     242
     243/** Finalize demo font. */
     244static void demo_font_fini(void)
     245{
     246        if (font == NULL)
     247                return;
     248
     249        gfx_font_close(font);
     250        font = NULL;
     251
     252        gfx_typeface_destroy(tface);
     253        tface = NULL;
     254}
     255
     256/** Start a new demo screen.
     257 *
     258 * Clear the screen, display a status line and set up clipping.
     259 *
     260 * @param gc Graphic context
     261 * @param w Width
     262 * @param h Height
     263 * @param text Demo screen description
     264 * @return EOK on success or an error code
     265 */
     266static errno_t demo_begin(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h,
     267    const char *text)
     268{
     269        gfx_text_fmt_t fmt;
     270        gfx_font_metrics_t metrics;
     271        gfx_coord2_t pos;
     272        gfx_color_t *color;
     273        gfx_rect_t rect;
     274        gfx_coord_t height;
     275        errno_t rc;
     276
     277        rc = gfx_set_clip_rect(gc, NULL);
     278        if (rc != EOK)
     279                return rc;
     280
     281        rc = clear_scr(gc, w, h);
     282        if (rc != EOK)
     283                return rc;
     284
     285        if (font != NULL) {
     286                if (demo_is_text()) {
     287                        rc = gfx_color_new_ega(0x1e, &color);
     288                        if (rc != EOK)
     289                                goto error;
     290                } else {
     291                        rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
     292                        if (rc != EOK)
     293                                goto error;
     294                }
     295
     296                gfx_text_fmt_init(&fmt);
     297                fmt.font = font;
     298                fmt.color = color;
     299                fmt.halign = gfx_halign_center;
     300                fmt.valign = gfx_valign_bottom;
     301
     302                pos.x = w / 2;
     303                pos.y = h;
     304                rc = gfx_puttext(&pos, &fmt, text);
     305                if (rc != EOK) {
     306                        printf("Error rendering text.\n");
     307                        gfx_color_delete(color);
     308                        goto error;
     309                }
     310
     311                gfx_color_delete(color);
     312
     313                gfx_font_get_metrics(font, &metrics);
     314                height = metrics.ascent + metrics.descent + 1;
     315        } else {
     316                height = 0;
     317        }
     318
     319        rect.p0.x = 0;
     320        rect.p0.y = 0;
     321        rect.p1.x = w;
     322        rect.p1.y = h - height - vpad;
     323        rc = gfx_set_clip_rect(gc, &rect);
     324        if (rc != EOK)
     325                return rc;
     326
     327        return EOK;
     328error:
     329        return rc;
     330}
     331
     332/** Run rectangle demo on a graphic context.
     333 *
     334 * @param gc Graphic context
     335 * @param w Width
     336 * @param h Height
    114337 */
    115338static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     
    123346                return EOK;
    124347
    125         rc = clear_scr(gc, w, h);
     348        rc = demo_begin(gc, w, h, "Rectangle rendering");
    126349        if (rc != EOK)
    127350                return rc;
     
    150373                gfx_color_delete(color);
    151374
    152                 fibril_usleep(500 * 1000);
    153 
     375                demo_msleep(500);
    154376                if (quit)
    155377                        break;
     
    257479                        pixelmap_put_pixel(&pixelmap, i, j,
    258480                            k < w * w / 2 ? PIXEL(255, 0, 255, 0) :
    259                             PIXEL(0, 255, 0, 255));
     481                            PIXEL(255, 255, 0, 255));
    260482                }
    261483        }
     
    282504                return EOK;
    283505
    284         rc = clear_scr(gc, w, h);
     506        rc = demo_begin(gc, w, h, "Bitmap rendering without offset");
    285507        if (rc != EOK)
    286508                return rc;
     
    312534                        if (rc != EOK)
    313535                                goto error;
    314                         fibril_usleep(250 * 1000);
    315 
     536
     537                        demo_msleep(250);
    316538                        if (quit)
    317539                                goto out;
     
    345567                return EOK;
    346568
    347         rc = clear_scr(gc, w, h);
     569        rc = demo_begin(gc, w, h, "Bitmap rendering with offset");
    348570        if (rc != EOK)
    349571                return rc;
     
    373595                }
    374596
    375                 fibril_usleep(500 * 1000);
    376 
     597                demo_msleep(500);
    377598                if (quit)
    378599                        break;
     
    404625                return EOK;
    405626
    406         rc = clear_scr(gc, w, h);
     627        rc = demo_begin(gc, w, h, "Bitmap rendering with color key");
    407628        if (rc != EOK)
    408629                return rc;
     
    414635        params.rect.p1.y = 40;
    415636        params.flags = bmpf_color_key;
    416         params.key_color = PIXEL(0, 255, 0, 255);
     637        params.key_color = PIXEL(255, 255, 0, 255);
    417638
    418639        rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
     
    434655                }
    435656
    436                 fibril_usleep(500 * 1000);
    437 
     657                demo_msleep(500);
    438658                if (quit)
    439659                        break;
     
    458678        gfx_color_t *color = NULL;
    459679        gfx_rect_t rect;
    460         gfx_typeface_t *tface = NULL;
    461         gfx_font_info_t *finfo;
    462         gfx_font_t *font = NULL;
    463680        gfx_coord2_t pos;
    464681        gfx_text_fmt_t fmt;
     
    469686                return EOK;
    470687
    471         rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface);
    472         if (rc != EOK) {
    473                 printf("Error opening typeface\n");
    474                 goto error;
    475         }
    476 
    477         finfo = gfx_typeface_first_font(tface);
    478         if (finfo == NULL) {
    479                 printf("Typeface contains no font.\n");
    480                 rc = ENOENT;
    481                 goto error;
    482         }
    483 
    484         rc = gfx_font_open(finfo, &font);
    485         if (rc != EOK) {
    486                 printf("Error opening font.\n");
    487                 goto error;
    488         }
    489 
    490         rc = clear_scr(gc, w, h);
     688        rc = demo_begin(gc, w, h, "Text rendering");
    491689        if (rc != EOK)
    492690                goto error;
     
    525723
    526724        rect.p0.x = w / 20;
    527         rect.p0.y = 2 * h / 15;
     725        rect.p0.y = 1 * h / 15;
    528726        rect.p1.x = w - w / 20;
    529         rect.p1.y = 5 * h / 15;
     727        rect.p1.y = 4 * h / 15;
    530728
    531729        rc = gfx_fill_rect(gc, &rect);
     
    535733        gfx_color_delete(color);
    536734
    537         rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
    538         if (rc != EOK)
    539                 goto error;
    540 
    541         rc = gfx_set_color(gc, color);
    542         if (rc != EOK)
    543                 goto error;
     735        if (demo_is_text()) {
     736                rc = gfx_color_new_ega(0x1f, &color);
     737                if (rc != EOK)
     738                        goto error;
     739        } else {
     740                rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
     741                if (rc != EOK)
     742                        goto error;
     743        }
    544744
    545745        gfx_text_fmt_init(&fmt);
     746        fmt.font = font;
     747        fmt.color = color;
    546748
    547749        pos.x = rect.p0.x;
    548750        pos.y = rect.p0.y;
    549         rc = gfx_puttext(font, &pos, &fmt, "Top left");
     751        rc = gfx_puttext(&pos, &fmt, "Top left");
    550752        if (rc != EOK) {
    551753                printf("Error rendering text.\n");
     
    556758        pos.y = rect.p0.y;
    557759        fmt.halign = gfx_halign_center;
    558         rc = gfx_puttext(font, &pos, &fmt, "Top center");
    559         if (rc != EOK)
    560                 goto error;
    561 
    562         pos.x = rect.p1.x - 1;
     760        rc = gfx_puttext(&pos, &fmt, "Top center");
     761        if (rc != EOK)
     762                goto error;
     763
     764        pos.x = rect.p1.x;
    563765        pos.y = rect.p0.y;
    564766        fmt.halign = gfx_halign_right;
    565         rc = gfx_puttext(font, &pos, &fmt, "Top right");
     767        rc = gfx_puttext(&pos, &fmt, "Top right");
    566768        if (rc != EOK)
    567769                goto error;
     
    572774        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    573775        fmt.halign = gfx_halign_left;
    574         rc = gfx_puttext(font, &pos, &fmt, "Center left");
     776        rc = gfx_puttext(&pos, &fmt, "Center left");
    575777        if (rc != EOK)
    576778                goto error;
     
    579781        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    580782        fmt.halign = gfx_halign_center;
    581         rc = gfx_puttext(font, &pos, &fmt, "Center");
    582         if (rc != EOK)
    583                 goto error;
    584 
    585         pos.x = rect.p1.x - 1;
     783        rc = gfx_puttext(&pos, &fmt, "Center");
     784        if (rc != EOK)
     785                goto error;
     786
     787        pos.x = rect.p1.x;
    586788        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    587789        fmt.halign = gfx_halign_right;
    588         rc = gfx_puttext(font, &pos, &fmt, "Center right");
     790        rc = gfx_puttext(&pos, &fmt, "Center right");
    589791        if (rc != EOK)
    590792                goto error;
     
    595797        pos.y = rect.p1.y - 1;
    596798        fmt.halign = gfx_halign_left;
    597         rc = gfx_puttext(font, &pos, &fmt, "Bottom left");
     799        rc = gfx_puttext(&pos, &fmt, "Bottom left");
    598800        if (rc != EOK)
    599801                goto error;
    600802
    601803        pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
    602         pos.y = rect.p1.y - 1;
     804        pos.y = rect.p1.y;
    603805        fmt.halign = gfx_halign_center;
    604         rc = gfx_puttext(font, &pos, &fmt, "Bottom center");
    605         if (rc != EOK)
    606                 goto error;
    607 
    608         pos.x = rect.p1.x - 1;
    609         pos.y = rect.p1.y - 1;
     806        rc = gfx_puttext(&pos, &fmt, "Bottom center");
     807        if (rc != EOK)
     808                goto error;
     809
     810        pos.x = rect.p1.x;
     811        pos.y = rect.p1.y;
    610812        fmt.halign = gfx_halign_right;
    611         rc = gfx_puttext(font, &pos, &fmt, "Bottom right");
     813        rc = gfx_puttext(&pos, &fmt, "Bottom right");
    612814        if (rc != EOK)
    613815                goto error;
     
    616818
    617819        gfx_text_fmt_init(&fmt);
     820        fmt.font = font;
    618821
    619822        for (i = 0; i < 8; i++) {
    620                 rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0,
    621                     (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color);
    622                 if (rc != EOK)
    623                         goto error;
    624 
    625                 rc = gfx_set_color(gc, color);
    626                 if (rc != EOK)
    627                         goto error;
     823                if (demo_is_text()) {
     824                        rc = gfx_color_new_ega(i != 0 ? i : 0x10, &color);
     825                        if (rc != EOK)
     826                                goto error;
     827                } else {
     828                        rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0,
     829                            (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color);
     830                        if (rc != EOK)
     831                                goto error;
     832                }
     833
     834                fmt.color = color;
     835                fmt.underline = !fmt.underline;
    628836
    629837                pos.x = w / 20;
    630                 pos.y = (7 + i) * h / 15;
    631                 rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
     838                pos.y = (6 + i) * h / 15;
     839                rc = gfx_puttext(&pos, &fmt, "The quick brown fox jumps over the lazy dog.");
    632840                if (rc != EOK)
    633841                        goto error;
     
    637845
    638846        for (i = 0; i < 10; i++) {
    639                 fibril_usleep(500 * 1000);
     847                demo_msleep(500);
    640848                if (quit)
    641849                        break;
    642850        }
    643851
    644         gfx_font_close(font);
    645         gfx_typeface_destroy(tface);
    646852        return EOK;
    647853error:
    648         if (font != NULL)
    649                 gfx_font_close(font);
    650         if (tface != NULL)
    651                 gfx_typeface_destroy(tface);
    652854        return rc;
    653855}
    654856
    655 /** Run demo loop on a graphic context.
     857/** Run text abbreviation demo on a graphic context.
    656858 *
    657859 * @param gc Graphic context
     
    659861 * @param h Height
    660862 */
    661 static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
    662 {
    663         errno_t rc;
     863static errno_t demo_text_abbr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     864{
     865        gfx_color_t *color = NULL;
     866        gfx_rect_t rect;
     867        gfx_coord2_t pos;
     868        gfx_text_fmt_t fmt;
     869        int i;
     870        errno_t rc;
     871
     872        if (quit)
     873                return EOK;
     874
     875        rc = demo_begin(gc, w, h, "Text abbreviation");
     876        if (rc != EOK)
     877                goto error;
     878
     879        for (i = 0; i < 11; i++) {
     880
     881                rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color);
     882                if (rc != EOK)
     883                        goto error;
     884
     885                rc = gfx_set_color(gc, color);
     886                if (rc != EOK)
     887                        goto error;
     888
     889                rect.p0.x = w / 20;
     890                rect.p0.y = (2 + 2 * i) * h / 25;
     891                rect.p1.x = w - w / 20 - w * i / 12;
     892                rect.p1.y = (3 + 2 * i) * h / 25;
     893
     894                rc = gfx_fill_rect(gc, &rect);
     895                if (rc != EOK)
     896                        goto error;
     897
     898                gfx_color_delete(color);
     899
     900                if (demo_is_text()) {
     901                        rc = gfx_color_new_ega(0x1f, &color);
     902                        if (rc != EOK)
     903                                goto error;
     904                } else {
     905                        rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
     906                            &color);
     907                        if (rc != EOK)
     908                                goto error;
     909                }
     910
     911                gfx_text_fmt_init(&fmt);
     912                fmt.font = font;
     913                fmt.color = color;
     914                fmt.abbreviate = true;
     915                fmt.width = rect.p1.x - rect.p0.x;
     916
     917                pos.x = rect.p0.x;
     918                pos.y = rect.p0.y;
     919                rc = gfx_puttext(&pos, &fmt,
     920                    "The quick brow fox jumps over the lazy dog!");
     921                if (rc != EOK) {
     922                        printf("Error rendering text.\n");
     923                        goto error;
     924                }
     925        }
     926
     927        for (i = 0; i < 10; i++) {
     928                demo_msleep(500);
     929                if (quit)
     930                        break;
     931        }
     932
     933        return EOK;
     934error:
     935        return rc;
     936}
     937
     938/** Run clipping demo on a graphic context.
     939 *
     940 * @param gc Graphic context
     941 * @param w Width
     942 * @param h Height
     943 */
     944static errno_t demo_clip(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     945{
     946        gfx_bitmap_t *bitmap;
     947        gfx_color_t *color;
     948        gfx_bitmap_params_t params;
     949        int i, j;
     950        gfx_coord2_t offs;
     951        gfx_rect_t rect;
     952        errno_t rc;
     953
     954        if (quit)
     955                return EOK;
     956
     957        rc = demo_begin(gc, w, h, "Clipping demonstration");
     958        if (rc != EOK)
     959                return rc;
     960
     961        gfx_bitmap_params_init(&params);
     962        params.rect.p0.x = 0;
     963        params.rect.p0.y = 0;
     964        params.rect.p1.x = 40;
     965        params.rect.p1.y = 20;
     966
     967        rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
     968        if (rc != EOK)
     969                return rc;
     970
     971        rc = bitmap_moire(bitmap, 40, 20);
     972        if (rc != EOK)
     973                goto error;
     974
     975        for (j = 0; j < 10; j++) {
     976                rect.p0.x = w / 8;
     977                rect.p0.y = h / 8;
     978                rect.p1.x = w * 7 / 8;
     979                rect.p1.y = h * 3 / 8;
     980
     981                rc = gfx_set_clip_rect(gc, &rect);
     982                if (rc != EOK)
     983                        return rc;
     984
     985                rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
     986                    rand() % 0x10000, &color);
     987                if (rc != EOK)
     988                        return rc;
     989
     990                rc = gfx_set_color(gc, color);
     991                if (rc != EOK)
     992                        return rc;
     993
     994                for (i = 0; i < 10; i++) {
     995                        rect.p0.x = rand() % (w - 1);
     996                        rect.p0.y = rand() % (h - 1);
     997                        rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x);
     998                        rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y);
     999
     1000                        rc = gfx_fill_rect(gc, &rect);
     1001                        if (rc != EOK)
     1002                                return rc;
     1003                }
     1004
     1005                gfx_color_delete(color);
     1006
     1007                rect.p0.x = w / 8;
     1008                rect.p0.y = h * 5 / 8;
     1009                rect.p1.x = w * 7 / 8;
     1010                rect.p1.y = h * 7 / 8;
     1011
     1012                rc = gfx_set_clip_rect(gc, &rect);
     1013                if (rc != EOK)
     1014                        return rc;
     1015
     1016                for (i = 0; i < 10; i++) {
     1017                        offs.x = rand() % (w - 40);
     1018                        offs.y = rand() % (h - 20);
     1019
     1020                        rc = gfx_bitmap_render(bitmap, NULL, &offs);
     1021                        if (rc != EOK)
     1022                                goto error;
     1023                }
     1024
     1025                demo_msleep(500);
     1026                if (quit)
     1027                        break;
     1028        }
     1029
     1030        (void) gfx_set_clip_rect(gc, NULL);
     1031        gfx_bitmap_destroy(bitmap);
     1032        return EOK;
     1033error:
     1034        (void) gfx_set_clip_rect(gc, NULL);
     1035        gfx_bitmap_destroy(bitmap);
     1036        return rc;
     1037}
     1038
     1039/** Run demo loop on a graphic context.
     1040 *
     1041 * @param gc Graphic context
     1042 */
     1043static errno_t demo_loop(gfx_context_t *gc)
     1044{
     1045        errno_t rc;
     1046
     1047        (void) demo_font_init(gc, scr_width, scr_height);
    6641048
    6651049        while (!quit) {
    666                 rc = demo_rects(gc, w, h);
    667                 if (rc != EOK)
    668                         return rc;
    669 
    670                 rc = demo_bitmap(gc, w, h);
    671                 if (rc != EOK)
    672                         return rc;
    673 
    674                 rc = demo_bitmap2(gc, w, h);
    675                 if (rc != EOK)
    676                         return rc;
    677 
    678                 rc = demo_bitmap_kc(gc, w, h);
    679                 if (rc != EOK)
    680                         return rc;
    681 
    682                 rc = demo_text(gc, w, h);
    683                 if (rc != EOK)
    684                         return rc;
    685         }
    686 
    687         return EOK;
     1050                rc = demo_rects(gc, scr_width, scr_height);
     1051                if (rc != EOK)
     1052                        goto error;
     1053
     1054                rc = demo_bitmap(gc, scr_width, scr_height);
     1055                if (rc != EOK)
     1056                        goto error;
     1057
     1058                rc = demo_bitmap2(gc, scr_width, scr_height);
     1059                if (rc != EOK)
     1060                        goto error;
     1061
     1062                rc = demo_bitmap_kc(gc, scr_width, scr_height);
     1063                if (rc != EOK)
     1064                        goto error;
     1065
     1066                rc = demo_text(gc, scr_width, scr_height);
     1067                if (rc != EOK)
     1068                        goto error;
     1069
     1070                rc = demo_text_abbr(gc, scr_width, scr_height);
     1071                if (rc != EOK)
     1072                        goto error;
     1073
     1074                rc = demo_clip(gc, scr_width, scr_height);
     1075                if (rc != EOK)
     1076                        goto error;
     1077        }
     1078
     1079        demo_font_fini();
     1080        return EOK;
     1081error:
     1082        demo_font_fini();
     1083        return rc;
    6881084}
    6891085
     
    6911087static errno_t demo_console(void)
    6921088{
    693         console_ctrl_t *con = NULL;
    694         console_gc_t *cgc = NULL;
    6951089        gfx_context_t *gc;
    696         errno_t rc;
    697 
    698         printf("Init console..\n");
     1090        sysarg_t cols, rows;
     1091        errno_t rc;
     1092
    6991093        con = console_init(stdin, stdout);
    7001094        if (con == NULL)
    7011095                return EIO;
    7021096
    703         printf("Create console GC\n");
     1097        rc = console_get_size(con, &cols, &rows);
     1098        if (rc != EOK)
     1099                return rc;
     1100
    7041101        rc = console_gc_create(con, stdout, &cgc);
    7051102        if (rc != EOK)
     
    7081105        gc = console_gc_get_ctx(cgc);
    7091106
    710         rc = demo_loop(gc, 80, 25);
     1107        /* Currently console is always text. */
     1108        textmode = true;
     1109
     1110        scr_width = cols;
     1111        scr_height = rows;
     1112
     1113        rc = demo_loop(gc);
    7111114        if (rc != EOK)
    7121115                return rc;
     
    7171120
    7181121        return EOK;
     1122}
     1123
     1124static errno_t demo_ui_fibril(void *arg)
     1125{
     1126        demo_ui_args_t *args = (demo_ui_args_t *)arg;
     1127        errno_t rc;
     1128
     1129        ui_lock(args->ui);
     1130        scr_width = args->dims.x;
     1131        scr_height = args->dims.y;
     1132        rc = demo_loop(args->gc);
     1133        ui_unlock(args->ui);
     1134        ui_quit(args->ui);
     1135        return rc;
    7191136}
    7201137
     
    7221139static errno_t demo_ui(const char *display_spec)
    7231140{
    724         ui_t *ui = NULL;
    7251141        ui_wnd_params_t params;
    7261142        ui_window_t *window = NULL;
     
    7291145        gfx_rect_t wrect;
    7301146        gfx_coord2_t off;
    731         errno_t rc;
    732 
    733         printf("Init UI..\n");
     1147        gfx_rect_t ui_rect;
     1148        gfx_coord2_t dims;
     1149        demo_ui_args_t args;
     1150        fid_t fid;
     1151        errno_t rc;
    7341152
    7351153        rc = ui_create(display_spec, &ui);
    7361154        if (rc != EOK) {
    7371155                printf("Error initializing UI (%s)\n", display_spec);
     1156                goto error;
     1157        }
     1158
     1159        rc = ui_get_rect(ui, &ui_rect);
     1160        if (rc != EOK) {
     1161                printf("Error getting display size.\n");
    7381162                goto error;
    7391163        }
     
    7471171        params.caption = "GFX Demo";
    7481172
     1173        /* Do not decorate the window in fullscreen mode */
     1174        if (ui_is_fullscreen(ui)) {
     1175                params.style &= ~ui_wds_decorated;
     1176                params.placement = ui_wnd_place_full_screen;
     1177        }
     1178
    7491179        /*
    7501180         * Compute window rectangle such that application area corresponds
    7511181         * to rect
    7521182         */
    753         ui_wdecor_rect_from_app(params.style, &rect, &wrect);
     1183        ui_wdecor_rect_from_app(ui, params.style, &rect, &wrect);
    7541184        off = wrect.p0;
    7551185        gfx_rect_rtranslate(&off, &wrect, &params.rect);
     1186
     1187        gfx_rect_dims(&ui_rect, &dims);
     1188
     1189        /* Make sure window is not larger than the entire screen */
     1190        if (params.rect.p1.x > dims.x)
     1191                params.rect.p1.x = dims.x;
     1192        if (params.rect.p1.y > dims.y)
     1193                params.rect.p1.y = dims.y;
    7561194
    7571195        rc = ui_window_create(ui, &params, &window);
     
    7691207        }
    7701208
    771         task_retval(0);
    772 
    773         rc = demo_loop(gc, rect.p1.x, rect.p1.y);
    774         if (rc != EOK)
    775                 goto error;
    776 
     1209        ui_window_get_app_rect(window, &rect);
     1210        gfx_rect_dims(&rect, &dims);
     1211
     1212        if (!ui_is_fullscreen(ui))
     1213                task_retval(0);
     1214
     1215        textmode = ui_is_textmode(ui);
     1216
     1217        args.gc = gc;
     1218        args.dims = dims;
     1219        args.ui = ui;
     1220
     1221        fid = fibril_create(demo_ui_fibril, (void *)&args);
     1222        if (fid == 0) {
     1223                rc = ENOMEM;
     1224                goto error;
     1225        }
     1226
     1227        fibril_add_ready(fid);
     1228
     1229        ui_run(ui);
    7771230        ui_window_destroy(window);
    7781231        ui_destroy(ui);
     
    7961249        errno_t rc;
    7971250
    798         printf("Init display..\n");
    799 
    8001251        rc = display_open(display_svc, &display);
    8011252        if (rc != EOK) {
     
    8091260        params.rect.p1.x = 400;
    8101261        params.rect.p1.y = 300;
     1262        params.caption = "GFX Demo";
    8111263
    8121264        rc = display_window_create(display, &params, &wnd_cb, NULL, &window);
     
    8241276        task_retval(0);
    8251277
    826         rc = demo_loop(gc, 400, 300);
     1278        /* FIXME Assuming display service is not text mode. */
     1279        textmode = false;
     1280
     1281        scr_width = 400;
     1282        scr_height = 300;
     1283        rc = demo_loop(gc);
    8271284        if (rc != EOK)
    8281285                return rc;
     
    8381295}
    8391296
     1297static void demo_quit(void)
     1298{
     1299        fibril_mutex_lock(&quit_lock);
     1300        quit = true;
     1301        fibril_mutex_unlock(&quit_lock);
     1302        fibril_condvar_broadcast(&quit_cv);
     1303}
     1304
    8401305static void wnd_close_event(void *arg)
    8411306{
    842         printf("Close event\n");
    843         quit = true;
     1307        demo_quit();
     1308}
     1309
     1310static void demo_kbd_event(kbd_event_t *event)
     1311{
     1312        if (event->type == KEY_PRESS) {
     1313                /* Ctrl-Q */
     1314                if ((event->mods & KM_CTRL) != 0 &&
     1315                    (event->mods & KM_ALT) == 0 &&
     1316                    (event->mods & KM_SHIFT) == 0 &&
     1317                    event->key == KC_Q) {
     1318                        demo_quit();
     1319                }
     1320
     1321                /* Escape */
     1322                if ((event->mods & KM_CTRL) == 0 &&
     1323                    (event->mods & KM_ALT) == 0 &&
     1324                    (event->mods & KM_SHIFT) == 0 &&
     1325                    event->key == KC_ESCAPE) {
     1326                        demo_quit();
     1327                }
     1328        }
    8441329}
    8451330
    8461331static void wnd_kbd_event(void *arg, kbd_event_t *event)
    8471332{
    848         printf("Keyboard event type=%d key=%d\n", event->type, event->key);
    849         if (event->type == KEY_PRESS)
    850                 quit = true;
     1333        (void)arg;
     1334        demo_kbd_event(event);
     1335}
     1336
     1337static void uiwnd_resize_event(ui_window_t *window, void *arg)
     1338{
     1339        gfx_rect_t rect;
     1340        gfx_coord2_t dims;
     1341
     1342        ui_window_get_app_rect(window, &rect);
     1343        gfx_rect_dims(&rect, &dims);
     1344        scr_width = dims.x;
     1345        scr_height = dims.y;
    8511346}
    8521347
    8531348static void uiwnd_close_event(ui_window_t *window, void *arg)
    8541349{
    855         printf("Close event\n");
    856         quit = true;
     1350        demo_quit();
    8571351}
    8581352
    8591353static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event)
    8601354{
    861         printf("Keyboard event type=%d key=%d\n", event->type, event->key);
    862         if (event->type == KEY_PRESS)
    863                 quit = true;
     1355        (void)window;
     1356        (void)arg;
     1357        demo_kbd_event(event);
    8641358}
    8651359
    8661360static void print_syntax(void)
    8671361{
    868         printf("Syntax: gfxdemo [-d <display>] {canvas|console|display}\n");
     1362        printf("Syntax: gfxdemo [-d <display>] {console|display|ui}\n");
    8691363}
    8701364
     
    8731367        errno_t rc;
    8741368        const char *display_svc = DISPLAY_DEFAULT;
     1369        const char *ui_display_spec = UI_ANY_DEFAULT;
    8751370        int i;
    8761371
     
    8851380                        }
    8861381
    887                         display_svc = argv[i++];
     1382                        display_svc = ui_display_spec = argv[i++];
    8881383                } else {
    8891384                        printf("Invalid option '%s'.\n", argv[i]);
     
    8931388        }
    8941389
    895         if (i >= argc || str_cmp(argv[i], "display") == 0) {
    896                 rc = demo_display(display_svc);
     1390        if (i >= argc || str_cmp(argv[i], "ui") == 0) {
     1391                rc = demo_ui(ui_display_spec);
    8971392                if (rc != EOK)
    8981393                        return 1;
     
    9011396                if (rc != EOK)
    9021397                        return 1;
    903         } else if (str_cmp(argv[i], "ui") == 0) {
    904                 rc = demo_ui(display_svc);
     1398        } else if (str_cmp(argv[i], "display") == 0) {
     1399                rc = demo_display(display_svc);
    9051400                if (rc != EOK)
    9061401                        return 1;
Note: See TracChangeset for help on using the changeset viewer.