Changeset f03d1308 in mainline


Ignore:
Timestamp:
2020-10-28T12:42:11Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8009dc27
Parents:
d284ce9
git-author:
Jiri Svoboda <jiri@…> (2020-10-28 12:41:11)
git-committer:
Jiri Svoboda <jiri@…> (2020-10-28 12:42:11)
Message:

Convert terminal to using ui_window

Location:
uspace
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/terminal/main.c

    rd284ce9 rf03d1308  
    11/*
    2  * Copyright (c) 2019 Jiri Svoboda
     2 * Copyright (c) 2020 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3333 */
    3434
    35 #include <display.h>
    3635#include <stdio.h>
    3736#include <task.h>
     37#include <ui/ui.h>
    3838#include "terminal.h"
    3939
     
    4343static void print_syntax(void)
    4444{
    45         printf("Syntax: %s [-d <display>]\n", NAME);
     45        printf("Syntax: %s [-d <display-spec>]\n", NAME);
    4646}
    4747
    4848int main(int argc, char *argv[])
    4949{
    50         const char *display_svc = DISPLAY_DEFAULT;
    51         display_t *display = NULL;
     50        const char *display_spec = UI_DISPLAY_DEFAULT;
    5251        terminal_t *terminal = NULL;
    5352        errno_t rc;
     
    6463                        }
    6564
    66                         display_svc = argv[i++];
     65                        display_spec = argv[i++];
    6766                } else {
    6867                        printf("Invalid option '%s'.\n", argv[i]);
     
    7776        }
    7877
    79         rc = display_open(display_svc, &display);
    80         if (rc != EOK) {
    81                 printf("%s: Error opening display.\n", NAME);
     78        rc = terminal_create(display_spec, 640, 480, &terminal);
     79        if (rc != EOK)
    8280                return 1;
    83         }
    84 
    85         rc = terminal_create(display, 640, 480, &terminal);
    86         if (rc != EOK) {
    87                 display_close(display);
    88                 return 1;
    89         }
    9081
    9182        task_retval(0);
  • uspace/app/terminal/terminal.c

    rd284ce9 rf03d1308  
    11/*
    2  * Copyright (c) 2019 Jiri Svoboda
     2 * Copyright (c) 2020 Jiri Svoboda
    33 * Copyright (c) 2012 Petr Koupy
    44 * All rights reserved.
     
    3232 */
    3333/**
    34  * @file Terminal application using display service for output
     34 * @file Terminal application
    3535 */
    3636
     
    5151#include <str.h>
    5252#include <ui/resource.h>
     53#include <ui/ui.h>
    5354#include <ui/wdecor.h>
     55#include <ui/window.h>
    5456
    5557#include "terminal.h"
     
    102104};
    103105
    104 static void terminal_close_event(void *);
    105 static void terminal_focus_event(void *);
    106 static void terminal_kbd_event(void *, kbd_event_t *);
    107 static void terminal_pos_event(void *, pos_event_t *);
    108 static void terminal_unfocus_event(void *);
    109 
    110 static display_wnd_cb_t terminal_wnd_cb = {
    111         .close_event = terminal_close_event,
    112         .focus_event = terminal_focus_event,
    113         .kbd_event = terminal_kbd_event,
    114         .pos_event = terminal_pos_event,
    115         .unfocus_event = terminal_unfocus_event
    116 };
    117 
    118 static void terminal_wd_close(ui_wdecor_t *, void *);
    119 static void terminal_wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
    120 
    121 static ui_wdecor_cb_t wdecor_cb = {
    122         .close = terminal_wd_close,
    123         .move = terminal_wd_move
     106static void terminal_close_event(ui_window_t *, void *);
     107static void terminal_focus_event(ui_window_t *, void *);
     108static void terminal_kbd_event(ui_window_t *, void *, kbd_event_t *);
     109static void terminal_pos_event(ui_window_t *, void *, pos_event_t *);
     110static void terminal_unfocus_event(ui_window_t *, void *);
     111
     112static ui_window_cb_t terminal_window_cb = {
     113        .close = terminal_close_event,
     114        .focus = terminal_focus_event,
     115        .kbd = terminal_kbd_event,
     116        .pos = terminal_pos_event,
     117        .unfocus = terminal_unfocus_event
    124118};
    125119
     
    677671
    678672/** Handle window close event. */
    679 static void terminal_close_event(void *arg)
     673static void terminal_close_event(ui_window_t *window, void *arg)
    680674{
    681675        terminal_t *term = (terminal_t *) arg;
     
    688682
    689683/** Handle window focus event. */
    690 static void terminal_focus_event(void *arg)
     684static void terminal_focus_event(ui_window_t *window, void *arg)
    691685{
    692686        terminal_t *term = (terminal_t *) arg;
    693687
    694         if (term->wdecor != NULL) {
    695                 ui_wdecor_set_active(term->wdecor, true);
    696                 ui_wdecor_paint(term->wdecor);
    697 
    698                 term->is_focused = true;
    699                 term_update(term);
    700         }
     688        term->is_focused = true;
     689        term_update(term);
    701690}
    702691
    703692/** Handle window keyboard event */
    704 static void terminal_kbd_event(void *arg, kbd_event_t *kbd_event)
     693static void terminal_kbd_event(ui_window_t *window, void *arg,
     694    kbd_event_t *kbd_event)
    705695{
    706696        terminal_t *term = (terminal_t *) arg;
     
    714704
    715705/** Handle window position event */
    716 static void terminal_pos_event(void *arg, pos_event_t *event)
     706static void terminal_pos_event(ui_window_t *window, void *arg, pos_event_t *event)
    717707{
    718708        cons_event_t cevent;
    719709        terminal_t *term = (terminal_t *) arg;
    720 
    721         /* Make sure we don't process events until fully initialized */
    722         if (term->wdecor == NULL)
    723                 return;
    724 
    725         ui_wdecor_pos_event(term->wdecor, event);
    726710
    727711        sysarg_t sx = -term->off.x;
     
    741725
    742726/** Handle window unfocus event. */
    743 static void terminal_unfocus_event(void *arg)
     727static void terminal_unfocus_event(ui_window_t *window, void *arg)
    744728{
    745729        terminal_t *term = (terminal_t *) arg;
    746730
    747         if (term->wdecor != NULL) {
    748                 ui_wdecor_set_active(term->wdecor, false);
    749                 ui_wdecor_paint(term->wdecor);
    750 
    751                 term->is_focused = false;
    752                 term_update(term);
    753         }
    754 }
    755 
    756 /** Window decoration requested window closure.
    757  *
    758  * @param wdecor Window decoration
    759  * @param arg Argument (demo)
    760  */
    761 static void terminal_wd_close(ui_wdecor_t *wdecor, void *arg)
    762 {
    763         terminal_t *term = (terminal_t *) arg;
    764 
    765         (void) term;
    766 
    767         // XXX This is not really a clean way of terminating
    768         exit(0);
    769 }
    770 
    771 /** Window decoration requested window move.
    772  *
    773  * @param wdecor Window decoration
    774  * @param arg Argument (demo)
    775  * @param pos Position where the title bar was pressed
    776  */
    777 static void terminal_wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
    778 {
    779         terminal_t *term = (terminal_t *) arg;
    780 
    781         if (term->window != NULL)
    782                 (void) display_window_move_req(term->window, pos);
     731        term->is_focused = false;
     732        term_update(term);
    783733}
    784734
     
    805755}
    806756
    807 errno_t terminal_create(display_t *display, sysarg_t width, sysarg_t height,
    808     terminal_t **rterm)
     757errno_t terminal_create(const char *display_spec, sysarg_t width,
     758    sysarg_t height, terminal_t **rterm)
    809759{
    810760        terminal_t *term;
    811761        gfx_bitmap_params_t params;
    812         display_wnd_params_t wparams;
     762        ui_wnd_params_t wparams;
    813763        gfx_rect_t rect;
    814764        gfx_coord2_t off;
     
    859809        rect.p1.y = height;
    860810
    861         display_wnd_params_init(&wparams);
     811        ui_wnd_params_init(&wparams);
     812        wparams.caption = "Terminal";
    862813
    863814        /*
     
    871822        term->off = off;
    872823
    873         rc = display_window_create(display, &wparams, &terminal_wnd_cb,
    874             (void *) term, &term->window);
     824        rc = ui_create(display_spec, &term->ui);
     825        if (rc != EOK) {
     826                printf("Error creating UI on %s.\n", display_spec);
     827                goto error;
     828        }
     829
     830        rc = ui_window_create(term->ui, &wparams, &term->window);
    875831        if (rc != EOK) {
    876832                printf("Error creating window.\n");
     
    878834        }
    879835
    880         rc = display_window_get_gc(term->window, &term->gc);
    881         if (rc != EOK) {
    882                 printf("Error getting window GC.\n");
    883                 goto error;
    884         }
    885 
    886         rc = ui_resource_create(term->gc, &term->ui_res);
    887         if (rc != EOK) {
    888                 printf("Error creating UI.\n");
    889                 goto error;
    890         }
    891 
    892         rc = ui_wdecor_create(term->ui_res, "Terminal", &term->wdecor);
    893         if (rc != EOK) {
    894                 printf("Error creating window decoration.\n");
    895                 goto error;
    896         }
    897 
    898         ui_wdecor_set_rect(term->wdecor, &wparams.rect);
    899         ui_wdecor_set_cb(term->wdecor, &wdecor_cb, (void *) term);
    900 
    901         (void) ui_wdecor_paint(term->wdecor);
     836        term->gc = ui_window_get_gc(term->window);
     837        term->ui_res = ui_window_get_res(term->window);
     838
     839        ui_window_set_cb(term->window, &terminal_window_cb, (void *) term);
    902840
    903841        gfx_bitmap_params_init(&params);
     
    943881        getterm(vc, "/app/bdsh");
    944882
     883        term->is_focused = true;
     884
    945885        term->update.p0.x = 0;
    946886        term->update.p0.y = 0;
     
    953893        return EOK;
    954894error:
    955         if (term->wdecor != NULL)
    956                 ui_wdecor_destroy(term->wdecor);
    957         if (term->ui_res != NULL)
    958                 ui_resource_destroy(term->ui_res);
    959         if (term->gc != NULL)
    960                 gfx_context_delete(term->gc);
    961895        if (term->window != NULL)
    962                 display_window_destroy(term->window);
     896                ui_window_destroy(term->window);
     897        if (term->ui != NULL)
     898                ui_destroy(term->ui);
    963899        if (term->frontbuf != NULL)
    964900                chargrid_destroy(term->frontbuf);
  • uspace/app/terminal/terminal.h

    rd284ce9 rf03d1308  
    3838#define TERMINAL_H
    3939
    40 #include <display.h>
    4140#include <errno.h>
    4241#include <fibril_synch.h>
     
    5049#include <stdatomic.h>
    5150#include <str.h>
    52 #include <ui/resource.h>
    53 #include <ui/wdecor.h>
     51#include <ui/ui.h>
     52#include <ui/window.h>
    5453
    5554#define UTF8_CHAR_BUFFER_SIZE  (STR_BOUNDS(1) + 1)
    5655
    5756typedef struct {
    58         display_window_t *window;
     57        ui_t *ui;
     58        ui_window_t *window;
     59        ui_resource_t *ui_res;
    5960        gfx_context_t *gc;
     61
    6062        gfx_bitmap_t *bmp;
    6163        sysarg_t w;
     
    6466        gfx_coord2_t off;
    6567        bool is_focused;
    66 
    67         ui_resource_t *ui_res;
    68         ui_wdecor_t *wdecor;
    6968
    7069        fibril_mutex_t mtx;
     
    8685} terminal_t;
    8786
    88 extern errno_t terminal_create(display_t *, sysarg_t, sysarg_t, terminal_t **);
     87extern errno_t terminal_create(const char *, sysarg_t, sysarg_t, terminal_t **);
    8988extern void terminal_destroy(terminal_t *);
    9089
  • uspace/lib/ui/include/types/ui/window.h

    rd284ce9 rf03d1308  
    3737#define _UI_TYPES_WINDOW_H
    3838
     39#include <io/kbd_event.h>
    3940#include <io/pos_event.h>
    4041
     
    5354typedef struct ui_window_cb {
    5455        void (*close)(ui_window_t *, void *);
     56        void (*focus)(ui_window_t *, void *);
     57        void (*kbd)(ui_window_t *, void *, kbd_event_t *);
    5558        void (*pos)(ui_window_t *, void *, pos_event_t *);
     59        void (*unfocus)(ui_window_t *, void *);
    5660} ui_window_cb_t;
    5761
  • uspace/lib/ui/private/window.h

    rd284ce9 rf03d1308  
    4040#include <display.h>
    4141#include <gfx/context.h>
     42#include <io/kbd_event.h>
    4243#include <io/pos_event.h>
    4344
     
    6465
    6566extern void ui_window_close(ui_window_t *);
     67extern void ui_window_focus(ui_window_t *);
     68extern void ui_window_kbd(ui_window_t *, kbd_event_t *);
    6669extern void ui_window_pos(ui_window_t *, pos_event_t *);
     70extern void ui_window_unfocus(ui_window_t *);
    6771
    6872#endif
  • uspace/lib/ui/src/window.c

    rd284ce9 rf03d1308  
    3737#include <errno.h>
    3838#include <gfx/context.h>
     39#include <io/kbd_event.h>
    3940#include <io/pos_event.h>
    4041#include <mem.h>
     
    221222                ui_wdecor_paint(window->wdecor);
    222223        }
     224
     225        ui_window_focus(window);
    223226}
    224227
     
    229232
    230233        (void) window;
    231         (void) kbd_event;
     234        ui_window_kbd(window, kbd_event);
    232235}
    233236
     
    254257                ui_wdecor_paint(window->wdecor);
    255258        }
     259
     260        ui_window_unfocus(window);
    256261}
    257262
     
    291296}
    292297
     298/** Send window focus event.
     299 *
     300 * @param window Window
     301 */
     302void ui_window_focus(ui_window_t *window)
     303{
     304        if (window->cb != NULL && window->cb->focus != NULL)
     305                window->cb->focus(window, window->arg);
     306}
     307
     308/** Send window keyboard event.
     309 *
     310 * @param window Window
     311 */
     312void ui_window_kbd(ui_window_t *window, kbd_event_t *kbd)
     313{
     314        if (window->cb != NULL && window->cb->kbd != NULL)
     315                window->cb->kbd(window, window->arg, kbd);
     316}
     317
    293318/** Send window position event.
    294319 *
     
    301326}
    302327
     328/** Send window unfocus event.
     329 *
     330 * @param window Window
     331 */
     332void ui_window_unfocus(ui_window_t *window)
     333{
     334        if (window->cb != NULL && window->cb->unfocus != NULL)
     335                window->cb->unfocus(window, window->arg);
     336}
     337
    303338/** @}
    304339 */
  • uspace/lib/ui/test/window.c

    rd284ce9 rf03d1308  
    2929#include <gfx/context.h>
    3030#include <gfx/coord.h>
     31#include <io/kbd_event.h>
    3132#include <io/pos_event.h>
    3233#include <mem.h>
     
    4344
    4445static void test_window_close(ui_window_t *, void *);
     46static void test_window_focus(ui_window_t *, void *);
     47static void test_window_kbd(ui_window_t *, void *, kbd_event_t *);
    4548static void test_window_pos(ui_window_t *, void *, pos_event_t *);
     49static void test_window_unfocus(ui_window_t *, void *);
    4650
    4751static ui_window_cb_t test_window_cb = {
    4852        .close = test_window_close,
    49         .pos = test_window_pos
     53        .focus = test_window_focus,
     54        .kbd = test_window_kbd,
     55        .pos = test_window_pos,
     56        .unfocus = test_window_unfocus
    5057};
    5158
     
    5562typedef struct {
    5663        bool close;
     64        bool focus;
     65        bool kbd;
     66        kbd_event_t kbd_event;
    5767        bool pos;
    5868        pos_event_t pos_event;
     69        bool unfocus;
    5970} test_cb_resp_t;
    6071
     
    151162        ui_window_close(window);
    152163        PCUT_ASSERT_TRUE(resp.close);
     164
     165        ui_window_destroy(window);
     166        ui_destroy(ui);
     167}
     168
     169/** ui_window_focus() calls focus callback set via ui_window_set_cb() */
     170PCUT_TEST(focus)
     171{
     172        errno_t rc;
     173        ui_t *ui = NULL;
     174        ui_wnd_params_t params;
     175        ui_window_t *window = NULL;
     176        test_cb_resp_t resp;
     177
     178        rc = ui_create_disp(NULL, &ui);
     179        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     180
     181        ui_wnd_params_init(&params);
     182        params.caption = "Hello";
     183
     184        rc = ui_window_create(ui, &params, &window);
     185        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     186        PCUT_ASSERT_NOT_NULL(window);
     187
     188        /* Focus callback with no callbacks set */
     189        ui_window_focus(window);
     190
     191        /* Focus callback with focus callback not implemented */
     192        ui_window_set_cb(window, &dummy_window_cb, NULL);
     193        ui_window_focus(window);
     194
     195        /* Focus callback with real callback set */
     196        resp.close = false;
     197        ui_window_set_cb(window, &test_window_cb, &resp);
     198        ui_window_focus(window);
     199        PCUT_ASSERT_TRUE(resp.focus);
     200
     201        ui_window_destroy(window);
     202        ui_destroy(ui);
     203}
     204
     205/** ui_window_kbd() calls kbd callback set via ui_window_set_cb() */
     206PCUT_TEST(kbd)
     207{
     208        errno_t rc;
     209        ui_t *ui = NULL;
     210        ui_wnd_params_t params;
     211        ui_window_t *window = NULL;
     212        kbd_event_t kbd_event;
     213        test_cb_resp_t resp;
     214
     215        rc = ui_create_disp(NULL, &ui);
     216        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     217
     218        ui_wnd_params_init(&params);
     219        params.caption = "Hello";
     220
     221        rc = ui_window_create(ui, &params, &window);
     222        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     223        PCUT_ASSERT_NOT_NULL(window);
     224
     225        kbd_event.type = POS_PRESS;
     226        kbd_event.key = KC_X;
     227        kbd_event.mods = 0;
     228        kbd_event.c = 'x';
     229
     230        /* Kbd callback with no callbacks set */
     231        ui_window_kbd(window, &kbd_event);
     232
     233        /* Kbd callback with kbd callback not implemented */
     234        ui_window_set_cb(window, &dummy_window_cb, NULL);
     235        ui_window_kbd(window, &kbd_event);
     236
     237        /* Kbd callback with real callback set */
     238        resp.kbd = false;
     239        ui_window_set_cb(window, &test_window_cb, &resp);
     240        ui_window_kbd(window, &kbd_event);
     241        PCUT_ASSERT_TRUE(resp.kbd);
     242        PCUT_ASSERT_EQUALS(kbd_event.type, resp.kbd_event.type);
     243        PCUT_ASSERT_INT_EQUALS(kbd_event.key, resp.kbd_event.key);
     244        PCUT_ASSERT_INT_EQUALS(kbd_event.mods, resp.kbd_event.mods);
     245        PCUT_ASSERT_INT_EQUALS(kbd_event.c, resp.kbd_event.c);
    153246
    154247        ui_window_destroy(window);
     
    204297}
    205298
     299/** ui_window_unfocus() calls unfocus callback set via ui_window_set_cb() */
     300PCUT_TEST(unfocus)
     301{
     302        errno_t rc;
     303        ui_t *ui = NULL;
     304        ui_wnd_params_t params;
     305        ui_window_t *window = NULL;
     306        test_cb_resp_t resp;
     307
     308        rc = ui_create_disp(NULL, &ui);
     309        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     310
     311        ui_wnd_params_init(&params);
     312        params.caption = "Hello";
     313
     314        rc = ui_window_create(ui, &params, &window);
     315        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     316        PCUT_ASSERT_NOT_NULL(window);
     317
     318        /* Unfocus callback with no callbacks set */
     319        ui_window_unfocus(window);
     320
     321        /* Unfocus callback with unfocus callback not implemented */
     322        ui_window_set_cb(window, &dummy_window_cb, NULL);
     323        ui_window_unfocus(window);
     324
     325        /* Unfocus callback with real callback set */
     326        resp.close = false;
     327        ui_window_set_cb(window, &test_window_cb, &resp);
     328        ui_window_unfocus(window);
     329        PCUT_ASSERT_TRUE(resp.unfocus);
     330
     331        ui_window_destroy(window);
     332        ui_destroy(ui);
     333}
     334
    206335static void test_window_close(ui_window_t *window, void *arg)
    207336{
     
    209338
    210339        resp->close = true;
     340}
     341
     342static void test_window_focus(ui_window_t *window, void *arg)
     343{
     344        test_cb_resp_t *resp = (test_cb_resp_t *) arg;
     345
     346        resp->focus = true;
     347}
     348
     349static void test_window_kbd(ui_window_t *window, void *arg,
     350    kbd_event_t *event)
     351{
     352        test_cb_resp_t *resp = (test_cb_resp_t *) arg;
     353
     354        resp->kbd = true;
     355        resp->kbd_event = *event;
    211356}
    212357
     
    220365}
    221366
     367static void test_window_unfocus(ui_window_t *window, void *arg)
     368{
     369        test_cb_resp_t *resp = (test_cb_resp_t *) arg;
     370
     371        resp->unfocus = true;
     372}
     373
    222374PCUT_EXPORT(window);
Note: See TracChangeset for help on using the changeset viewer.