Changeset d284ce9 in mainline for uspace/app/uidemo/uidemo.c


Ignore:
Timestamp:
2020-10-27T21:56:15Z (5 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f03d1308
Parents:
f7a90df
Message:

Let ui_window handle window decoration, display window

File:
1 edited

Legend:

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

    rf7a90df rd284ce9  
    3333 */
    3434
    35 #include <display.h>
    3635#include <gfx/color.h>
    3736#include <gfx/coord.h>
    3837#include <gfx/render.h>
     38#include <io/pos_event.h>
    3939#include <stdio.h>
    4040#include <str.h>
     
    4343#include <ui/pbutton.h>
    4444#include <ui/resource.h>
     45#include <ui/ui.h>
    4546#include <ui/wdecor.h>
     47#include <ui/window.h>
    4648#include "uidemo.h"
    4749
    48 static void wnd_close_event(void *);
    49 static void wnd_focus_event(void *);
    50 static void wnd_kbd_event(void *, kbd_event_t *);
    51 static void wnd_pos_event(void *, pos_event_t *);
    52 static void wnd_unfocus_event(void *);
    53 
    54 static display_wnd_cb_t wnd_cb = {
    55         .close_event = wnd_close_event,
    56         .focus_event = wnd_focus_event,
    57         .kbd_event = wnd_kbd_event,
    58         .pos_event = wnd_pos_event,
    59         .unfocus_event = wnd_unfocus_event
     50static void wnd_close(ui_window_t *, void *);
     51static void wnd_pos(ui_window_t *, void *, pos_event_t *pos);
     52
     53static ui_window_cb_t window_cb = {
     54        .close = wnd_close,
     55        .pos = wnd_pos
    6056};
    6157
     
    6662};
    6763
    68 static void wd_close(ui_wdecor_t *, void *);
    69 static void wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
    70 
    71 static ui_wdecor_cb_t wdecor_cb = {
    72         .close = wd_close,
    73         .move = wd_move
    74 };
    75 
    76 /** Print syntax. */
    77 static void print_syntax(void)
    78 {
    79         printf("Syntax: uidemo [-d <display>]\n");
    80 }
    81 
    82 /** Handle window close event. */
    83 static void wnd_close_event(void *arg)
     64/** Window close button was clicked.
     65 *
     66 * @param window Window
     67 * @param arg Argument (demo)
     68 */
     69static void wnd_close(ui_window_t *window, void *arg)
    8470{
    8571        ui_demo_t *demo = (ui_demo_t *) arg;
    8672
    87         demo->quit = true;
    88 }
    89 
    90 /** Handle window focus event. */
    91 static void wnd_focus_event(void *arg)
     73        ui_quit(demo->ui);
     74}
     75
     76/** Window position event.
     77 *
     78 * @param window Window
     79 * @param arg Argument (demo)
     80 */
     81static void wnd_pos(ui_window_t *window, void *arg, pos_event_t *event)
    9282{
    9383        ui_demo_t *demo = (ui_demo_t *) arg;
    9484
    95         if (demo->wdecor != NULL) {
    96                 ui_wdecor_set_active(demo->wdecor, true);
    97                 ui_wdecor_paint(demo->wdecor);
    98         }
    99 }
    100 
    101 /** Handle window keyboard event */
    102 static void wnd_kbd_event(void *arg, kbd_event_t *event)
    103 {
    104         ui_demo_t *demo = (ui_demo_t *) arg;
    105 
    106         (void) demo;
    107 }
    108 
    109 /** Handle window position event */
    110 static void wnd_pos_event(void *arg, pos_event_t *event)
    111 {
    112         ui_demo_t *demo = (ui_demo_t *) arg;
    113 
    11485        /* Make sure we don't process events until fully initialized */
    115         if (demo->wdecor == NULL || demo->pb1 == NULL || demo->pb2 == NULL)
     86        if (demo->pb1 == NULL || demo->pb2 == NULL)
    11687                return;
    11788
    118         ui_wdecor_pos_event(demo->wdecor, event);
    11989        ui_pbutton_pos_event(demo->pb1, event);
    12090        ui_pbutton_pos_event(demo->pb2, event);
    121 }
    122 
    123 /** Handle window unfocus event. */
    124 static void wnd_unfocus_event(void *arg)
    125 {
    126         ui_demo_t *demo = (ui_demo_t *) arg;
    127 
    128         if (demo->wdecor != NULL) {
    129                 ui_wdecor_set_active(demo->wdecor, false);
    130                 ui_wdecor_paint(demo->wdecor);
    131         }
    13291}
    13392
     
    155114}
    156115
    157 /** Window decoration requested window closure.
    158  *
    159  * @param wdecor Window decoration
    160  * @param arg Argument (demo)
    161  */
    162 static void wd_close(ui_wdecor_t *wdecor, void *arg)
    163 {
    164         ui_demo_t *demo = (ui_demo_t *) arg;
    165 
    166         demo->quit = true;
    167 }
    168 
    169 /** Window decoration requested window move.
    170  *
    171  * @param wdecor Window decoration
    172  * @param arg Argument (demo)
    173  * @param pos Position where the title bar was pressed
    174  */
    175 static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
    176 {
    177         ui_demo_t *demo = (ui_demo_t *) arg;
    178 
    179         (void) display_window_move_req(demo->dwindow, pos);
    180 }
    181 
    182116/** Run UI demo on display server. */
    183 static errno_t ui_demo_display(const char *display_svc)
    184 {
    185         display_t *display = NULL;
    186         gfx_context_t *gc;
    187         display_wnd_params_t params;
    188         display_window_t *window = NULL;
    189         ui_resource_t *ui_res;
     117static errno_t ui_demo(const char *display_spec)
     118{
     119        ui_t *ui = NULL;
     120        ui_wnd_params_t params;
     121        ui_window_t *window = NULL;
    190122        ui_demo_t demo;
    191123        gfx_rect_t rect;
    192         gfx_color_t *color = NULL;
     124        gfx_rect_t app_rect;
     125        gfx_color_t *color;
     126        ui_resource_t *ui_res;
     127        gfx_context_t *gc;
    193128        errno_t rc;
    194129
    195         rc = display_open(display_svc, &display);
    196         if (rc != EOK) {
    197                 printf("Error opening display.\n");
    198                 return rc;
    199         }
    200 
    201         display_wnd_params_init(&params);
     130        rc = ui_create(display_spec, &ui);
     131        if (rc != EOK) {
     132                printf("Error creating UI on display %s.\n", display_spec);
     133                return rc;
     134        }
     135
     136        ui_wnd_params_init(&params);
     137        params.caption = "UI Demo";
    202138        params.rect.p0.x = 0;
    203139        params.rect.p0.y = 0;
     
    206142
    207143        memset((void *) &demo, 0, sizeof(demo));
    208 
    209         rc = display_window_create(display, &params, &wnd_cb, (void *) &demo,
    210             &window);
     144        demo.ui = ui;
     145
     146        rc = ui_window_create(ui, &params, &window);
    211147        if (rc != EOK) {
    212148                printf("Error creating window.\n");
     
    214150        }
    215151
    216         demo.quit = false;
    217         demo.dwindow = window;
    218 
    219         rc = display_window_get_gc(window, &gc);
    220         if (rc != EOK) {
    221                 printf("Error getting graphics context.\n");
    222                 return rc;
    223         }
     152        ui_window_set_cb(window, &window_cb, (void *) &demo);
     153        demo.window = window;
    224154
    225155        task_retval(0);
    226156
    227         rc = ui_resource_create(gc, &ui_res);
    228         if (rc != EOK) {
    229                 printf("Error creating UI.\n");
    230                 return rc;
    231         }
    232 
    233         rc = ui_wdecor_create(ui_res, "UI Demo", &demo.wdecor);
    234         if (rc != EOK) {
    235                 printf("Error creating window decoration.\n");
    236                 return rc;
    237         }
    238 
    239         ui_wdecor_set_rect(demo.wdecor, &params.rect);
    240         ui_wdecor_set_cb(demo.wdecor, &wdecor_cb, (void *) &demo);
     157        ui_res = ui_window_get_res(window);
     158        gc = ui_window_get_gc(window);
     159        ui_window_get_app_rect(window, &app_rect);
    241160
    242161        rc = ui_label_create(ui_res, "Hello there!", &demo.label);
     
    295214        }
    296215
    297         rc = gfx_fill_rect(gc, &params.rect);
     216        rc = gfx_fill_rect(gc, &app_rect);
    298217        if (rc != EOK) {
    299218                printf("Error filling background.\n");
     
    301220        }
    302221
    303         gfx_color_delete(color);
    304         color = NULL;
    305 
    306         rc = ui_wdecor_paint(demo.wdecor);
    307         if (rc != EOK) {
    308                 printf("Error painting window decoration.\n");
    309                 return rc;
    310         }
    311 
    312222        rc = ui_label_paint(demo.label);
    313223        if (rc != EOK) {
     
    328238        }
    329239
    330         while (!demo.quit) {
    331                 fibril_usleep(100 * 1000);
    332         }
    333 
    334         ui_wdecor_destroy(demo.wdecor);
     240        ui_run(ui);
     241
    335242        ui_pbutton_destroy(demo.pb1);
    336243        ui_pbutton_destroy(demo.pb2);
    337244
    338         rc = gfx_context_delete(gc);
    339         if (rc != EOK)
    340                 return rc;
    341 
    342         display_window_destroy(window);
    343         display_close(display);
     245        ui_window_destroy(window);
     246        ui_destroy(ui);
    344247
    345248        return EOK;
    346249}
    347250
     251static void print_syntax(void)
     252{
     253        printf("Syntax: uidemo [-d <display-spec>]\n");
     254}
     255
    348256int main(int argc, char *argv[])
    349257{
    350         const char *display_svc = DISPLAY_DEFAULT;
     258        const char *display_spec = UI_DISPLAY_DEFAULT;
    351259        errno_t rc;
    352260        int i;
     
    362270                        }
    363271
    364                         display_svc = argv[i++];
     272                        display_spec = argv[i++];
    365273                } else {
    366274                        printf("Invalid option '%s'.\n", argv[i]);
     
    375283        }
    376284
    377         rc = ui_demo_display(display_svc);
     285        rc = ui_demo(display_spec);
    378286        if (rc != EOK)
    379287                return 1;
Note: See TracChangeset for help on using the changeset viewer.