Ignore:
File:
1 edited

Legend:

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

    rd0dfbba r266ec54  
    11/*
    2  * Copyright (c) 2026 Jiri Svoboda
     2 * Copyright (c) 2020 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#include <display.h>
    3737#include <fibril.h>
    38 #include <fibril_synch.h>
    3938#include <gfx/bitmap.h>
    4039#include <gfx/color.h>
     
    5251#include <ui/window.h>
    5352#include <ui/wdecor.h>
    54 #include "gfxdemo.h"
    5553
    5654static void wnd_close_event(void *);
     
    6260};
    6361
    64 static void uiwnd_resize_event(ui_window_t *, void *);
    6562static void uiwnd_close_event(ui_window_t *, void *);
    6663static void uiwnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
    6764
    6865static ui_window_cb_t ui_window_cb = {
    69         .resize = uiwnd_resize_event,
    7066        .close = uiwnd_close_event,
    7167        .kbd = uiwnd_kbd_event
    7268};
    7369
    74 static void demo_kbd_event(kbd_event_t *);
    75 
    7670static bool quit = false;
    77 static FIBRIL_MUTEX_INITIALIZE(quit_lock);
    78 static FIBRIL_CONDVAR_INITIALIZE(quit_cv);
    79 static gfx_typeface_t *tface;
    80 static gfx_font_t *font;
    81 static gfx_coord_t vpad;
    82 static console_ctrl_t *con = NULL;
    83 static bool textmode;
    84 static console_gc_t *cgc = NULL;
    85 static unsigned scr_width, scr_height;
    86 static 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  */
    92 static 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  */
    101 static 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 }
    14371
    14472/** Clear screen.
     
    179107}
    180108
    181 /** Initialize demo font.
     109/** Run rectangle demo on a graphic context.
    182110 *
    183111 * @param gc Graphic context
    184112 * @param w Width
    185113 * @param h Height
    186  * @return EOK on success or an error code
    187  */
    188 static 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;
    237 error:
    238         if (tface != NULL)
    239                 gfx_typeface_destroy(tface);
    240         return rc;
    241 }
    242 
    243 /** Finalize demo font. */
    244 static 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  */
    266 static 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;
    328 error:
    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
    337114 */
    338115static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     
    346123                return EOK;
    347124
    348         rc = demo_begin(gc, w, h, "Rectangle rendering");
     125        rc = clear_scr(gc, w, h);
    349126        if (rc != EOK)
    350127                return rc;
     
    373150                gfx_color_delete(color);
    374151
    375                 demo_msleep(500);
     152                fibril_usleep(500 * 1000);
     153
    376154                if (quit)
    377155                        break;
     
    479257                        pixelmap_put_pixel(&pixelmap, i, j,
    480258                            k < w * w / 2 ? PIXEL(255, 0, 255, 0) :
    481                             PIXEL(255, 255, 0, 255));
     259                            PIXEL(0, 255, 0, 255));
    482260                }
    483261        }
     
    504282                return EOK;
    505283
    506         rc = demo_begin(gc, w, h, "Bitmap rendering without offset");
     284        rc = clear_scr(gc, w, h);
    507285        if (rc != EOK)
    508286                return rc;
     
    534312                        if (rc != EOK)
    535313                                goto error;
    536 
    537                         demo_msleep(250);
     314                        fibril_usleep(250 * 1000);
     315
    538316                        if (quit)
    539317                                goto out;
     
    567345                return EOK;
    568346
    569         rc = demo_begin(gc, w, h, "Bitmap rendering with offset");
     347        rc = clear_scr(gc, w, h);
    570348        if (rc != EOK)
    571349                return rc;
     
    595373                }
    596374
    597                 demo_msleep(500);
     375                fibril_usleep(500 * 1000);
     376
    598377                if (quit)
    599378                        break;
     
    625404                return EOK;
    626405
    627         rc = demo_begin(gc, w, h, "Bitmap rendering with color key");
     406        rc = clear_scr(gc, w, h);
    628407        if (rc != EOK)
    629408                return rc;
     
    635414        params.rect.p1.y = 40;
    636415        params.flags = bmpf_color_key;
    637         params.key_color = PIXEL(255, 255, 0, 255);
     416        params.key_color = PIXEL(0, 255, 0, 255);
    638417
    639418        rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
     
    655434                }
    656435
    657                 demo_msleep(500);
     436                fibril_usleep(500 * 1000);
     437
    658438                if (quit)
    659439                        break;
     
    678458        gfx_color_t *color = NULL;
    679459        gfx_rect_t rect;
     460        gfx_typeface_t *tface = NULL;
     461        gfx_font_info_t *finfo;
     462        gfx_font_t *font = NULL;
    680463        gfx_coord2_t pos;
    681464        gfx_text_fmt_t fmt;
     
    686469                return EOK;
    687470
    688         rc = demo_begin(gc, w, h, "Text rendering");
     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);
    689491        if (rc != EOK)
    690492                goto error;
     
    723525
    724526        rect.p0.x = w / 20;
    725         rect.p0.y = 1 * h / 15;
     527        rect.p0.y = 2 * h / 15;
    726528        rect.p1.x = w - w / 20;
    727         rect.p1.y = 4 * h / 15;
     529        rect.p1.y = 5 * h / 15;
    728530
    729531        rc = gfx_fill_rect(gc, &rect);
     
    733535        gfx_color_delete(color);
    734536
    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         }
     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;
    744544
    745545        gfx_text_fmt_init(&fmt);
    746         fmt.font = font;
    747         fmt.color = color;
    748546
    749547        pos.x = rect.p0.x;
    750548        pos.y = rect.p0.y;
    751         rc = gfx_puttext(&pos, &fmt, "Top left");
     549        rc = gfx_puttext(font, &pos, &fmt, "Top left");
    752550        if (rc != EOK) {
    753551                printf("Error rendering text.\n");
     
    758556        pos.y = rect.p0.y;
    759557        fmt.halign = gfx_halign_center;
    760         rc = gfx_puttext(&pos, &fmt, "Top center");
    761         if (rc != EOK)
    762                 goto error;
    763 
    764         pos.x = rect.p1.x;
     558        rc = gfx_puttext(font, &pos, &fmt, "Top center");
     559        if (rc != EOK)
     560                goto error;
     561
     562        pos.x = rect.p1.x - 1;
    765563        pos.y = rect.p0.y;
    766564        fmt.halign = gfx_halign_right;
    767         rc = gfx_puttext(&pos, &fmt, "Top right");
     565        rc = gfx_puttext(font, &pos, &fmt, "Top right");
    768566        if (rc != EOK)
    769567                goto error;
     
    774572        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    775573        fmt.halign = gfx_halign_left;
    776         rc = gfx_puttext(&pos, &fmt, "Center left");
     574        rc = gfx_puttext(font, &pos, &fmt, "Center left");
    777575        if (rc != EOK)
    778576                goto error;
     
    781579        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    782580        fmt.halign = gfx_halign_center;
    783         rc = gfx_puttext(&pos, &fmt, "Center");
    784         if (rc != EOK)
    785                 goto error;
    786 
    787         pos.x = rect.p1.x;
     581        rc = gfx_puttext(font, &pos, &fmt, "Center");
     582        if (rc != EOK)
     583                goto error;
     584
     585        pos.x = rect.p1.x - 1;
    788586        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
    789587        fmt.halign = gfx_halign_right;
    790         rc = gfx_puttext(&pos, &fmt, "Center right");
     588        rc = gfx_puttext(font, &pos, &fmt, "Center right");
    791589        if (rc != EOK)
    792590                goto error;
     
    797595        pos.y = rect.p1.y - 1;
    798596        fmt.halign = gfx_halign_left;
    799         rc = gfx_puttext(&pos, &fmt, "Bottom left");
     597        rc = gfx_puttext(font, &pos, &fmt, "Bottom left");
    800598        if (rc != EOK)
    801599                goto error;
    802600
    803601        pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
    804         pos.y = rect.p1.y;
     602        pos.y = rect.p1.y - 1;
    805603        fmt.halign = gfx_halign_center;
    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;
     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;
    812610        fmt.halign = gfx_halign_right;
    813         rc = gfx_puttext(&pos, &fmt, "Bottom right");
     611        rc = gfx_puttext(font, &pos, &fmt, "Bottom right");
    814612        if (rc != EOK)
    815613                goto error;
     
    818616
    819617        gfx_text_fmt_init(&fmt);
    820         fmt.font = font;
    821618
    822619        for (i = 0; i < 8; i++) {
    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;
     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;
    836628
    837629                pos.x = w / 20;
    838                 pos.y = (6 + i) * h / 15;
    839                 rc = gfx_puttext(&pos, &fmt, "The quick brown fox jumps over the lazy dog.");
     630                pos.y = (7 + i) * h / 15;
     631                rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
    840632                if (rc != EOK)
    841633                        goto error;
     
    845637
    846638        for (i = 0; i < 10; i++) {
    847                 demo_msleep(500);
     639                fibril_usleep(500 * 1000);
    848640                if (quit)
    849641                        break;
    850642        }
    851643
     644        gfx_font_close(font);
     645        gfx_typeface_destroy(tface);
    852646        return EOK;
    853647error:
     648        if (font != NULL)
     649                gfx_font_close(font);
     650        if (tface != NULL)
     651                gfx_typeface_destroy(tface);
    854652        return rc;
    855653}
    856654
    857 /** Run text abbreviation demo on a graphic context.
     655/** Run demo loop on a graphic context.
    858656 *
    859657 * @param gc Graphic context
     
    861659 * @param h Height
    862660 */
    863 static 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;
    934 error:
    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  */
    944 static 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);
     661static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     662{
     663        errno_t rc;
     664
     665        while (!quit) {
     666                rc = demo_rects(gc, w, h);
    982667                if (rc != EOK)
    983668                        return rc;
    984669
    985                 rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
    986                     rand() % 0x10000, &color);
     670                rc = demo_bitmap(gc, w, h);
    987671                if (rc != EOK)
    988672                        return rc;
    989673
    990                 rc = gfx_set_color(gc, color);
     674                rc = demo_bitmap2(gc, w, h);
    991675                if (rc != EOK)
    992676                        return rc;
    993677
    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);
     678                rc = demo_bitmap_kc(gc, w, h);
    1013679                if (rc != EOK)
    1014680                        return rc;
    1015681
    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;
    1033 error:
    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  */
    1043 static errno_t demo_loop(gfx_context_t *gc)
    1044 {
    1045         errno_t rc;
    1046 
    1047         (void) demo_font_init(gc, scr_width, scr_height);
    1048 
    1049         while (!quit) {
    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;
    1081 error:
    1082         demo_font_fini();
    1083         return rc;
     682                rc = demo_text(gc, w, h);
     683                if (rc != EOK)
     684                        return rc;
     685        }
     686
     687        return EOK;
    1084688}
    1085689
     
    1087691static errno_t demo_console(void)
    1088692{
     693        console_ctrl_t *con = NULL;
     694        console_gc_t *cgc = NULL;
    1089695        gfx_context_t *gc;
    1090         sysarg_t cols, rows;
    1091         errno_t rc;
    1092 
     696        errno_t rc;
     697
     698        printf("Init console..\n");
    1093699        con = console_init(stdin, stdout);
    1094700        if (con == NULL)
    1095701                return EIO;
    1096702
    1097         rc = console_get_size(con, &cols, &rows);
    1098         if (rc != EOK)
    1099                 return rc;
    1100 
     703        printf("Create console GC\n");
    1101704        rc = console_gc_create(con, stdout, &cgc);
    1102705        if (rc != EOK)
     
    1105708        gc = console_gc_get_ctx(cgc);
    1106709
    1107         /* Currently console is always text. */
    1108         textmode = true;
    1109 
    1110         scr_width = cols;
    1111         scr_height = rows;
    1112 
    1113         rc = demo_loop(gc);
     710        rc = demo_loop(gc, 80, 25);
    1114711        if (rc != EOK)
    1115712                return rc;
     
    1120717
    1121718        return EOK;
    1122 }
    1123 
    1124 static 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;
    1136719}
    1137720
     
    1139722static errno_t demo_ui(const char *display_spec)
    1140723{
     724        ui_t *ui = NULL;
    1141725        ui_wnd_params_t params;
    1142726        ui_window_t *window = NULL;
     
    1145729        gfx_rect_t wrect;
    1146730        gfx_coord2_t off;
    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;
     731        errno_t rc;
     732
     733        printf("Init UI..\n");
    1152734
    1153735        rc = ui_create(display_spec, &ui);
    1154736        if (rc != EOK) {
    1155737                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");
    1162738                goto error;
    1163739        }
     
    1171747        params.caption = "GFX Demo";
    1172748
    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 
    1179749        /*
    1180750         * Compute window rectangle such that application area corresponds
    1181751         * to rect
    1182752         */
    1183         ui_wdecor_rect_from_app(ui, params.style, &rect, &wrect);
     753        ui_wdecor_rect_from_app(params.style, &rect, &wrect);
    1184754        off = wrect.p0;
    1185755        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;
    1194756
    1195757        rc = ui_window_create(ui, &params, &window);
     
    1207769        }
    1208770
    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);
     771        task_retval(0);
     772
     773        rc = demo_loop(gc, rect.p1.x, rect.p1.y);
     774        if (rc != EOK)
     775                goto error;
     776
    1230777        ui_window_destroy(window);
    1231778        ui_destroy(ui);
     
    1249796        errno_t rc;
    1250797
     798        printf("Init display..\n");
     799
    1251800        rc = display_open(display_svc, &display);
    1252801        if (rc != EOK) {
     
    1260809        params.rect.p1.x = 400;
    1261810        params.rect.p1.y = 300;
    1262         params.caption = "GFX Demo";
    1263811
    1264812        rc = display_window_create(display, &params, &wnd_cb, NULL, &window);
     
    1276824        task_retval(0);
    1277825
    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);
     826        rc = demo_loop(gc, 400, 300);
    1284827        if (rc != EOK)
    1285828                return rc;
     
    1295838}
    1296839
    1297 static void demo_quit(void)
    1298 {
    1299         fibril_mutex_lock(&quit_lock);
     840static void wnd_close_event(void *arg)
     841{
     842        printf("Close event\n");
    1300843        quit = true;
    1301         fibril_mutex_unlock(&quit_lock);
    1302         fibril_condvar_broadcast(&quit_cv);
    1303 }
    1304 
    1305 static void wnd_close_event(void *arg)
    1306 {
    1307         demo_quit();
    1308 }
    1309 
    1310 static 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         }
    1329844}
    1330845
    1331846static void wnd_kbd_event(void *arg, kbd_event_t *event)
    1332847{
    1333         (void)arg;
    1334         demo_kbd_event(event);
    1335 }
    1336 
    1337 static 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;
     848        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     849        if (event->type == KEY_PRESS)
     850                quit = true;
    1346851}
    1347852
    1348853static void uiwnd_close_event(ui_window_t *window, void *arg)
    1349854{
    1350         demo_quit();
     855        printf("Close event\n");
     856        quit = true;
    1351857}
    1352858
    1353859static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event)
    1354860{
    1355         (void)window;
    1356         (void)arg;
    1357         demo_kbd_event(event);
     861        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     862        if (event->type == KEY_PRESS)
     863                quit = true;
    1358864}
    1359865
    1360866static void print_syntax(void)
    1361867{
    1362         printf("Syntax: gfxdemo [-d <display>] {console|display|ui}\n");
     868        printf("Syntax: gfxdemo [-d <display>] {canvas|console|display}\n");
    1363869}
    1364870
     
    1367873        errno_t rc;
    1368874        const char *display_svc = DISPLAY_DEFAULT;
    1369         const char *ui_display_spec = UI_ANY_DEFAULT;
    1370875        int i;
    1371876
     
    1380885                        }
    1381886
    1382                         display_svc = ui_display_spec = argv[i++];
     887                        display_svc = argv[i++];
    1383888                } else {
    1384889                        printf("Invalid option '%s'.\n", argv[i]);
     
    1388893        }
    1389894
    1390         if (i >= argc || str_cmp(argv[i], "ui") == 0) {
    1391                 rc = demo_ui(ui_display_spec);
     895        if (i >= argc || str_cmp(argv[i], "display") == 0) {
     896                rc = demo_display(display_svc);
    1392897                if (rc != EOK)
    1393898                        return 1;
     
    1396901                if (rc != EOK)
    1397902                        return 1;
    1398         } else if (str_cmp(argv[i], "display") == 0) {
    1399                 rc = demo_display(display_svc);
     903        } else if (str_cmp(argv[i], "ui") == 0) {
     904                rc = demo_ui(display_svc);
    1400905                if (rc != EOK)
    1401906                        return 1;
Note: See TracChangeset for help on using the changeset viewer.