Changeset 338d0935 in mainline


Ignore:
Timestamp:
2020-03-02T11:22:01Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a2e104e
Parents:
7bb45e3
git-author:
Jiri Svoboda <jiri@…> (2020-02-01 11:18:50)
git-committer:
Jiri Svoboda <jiri@…> (2020-03-02 11:22:01)
Message:

Closing windows

Location:
uspace
Files:
13 edited

Legend:

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

    r7bb45e3 r338d0935  
    5050#include <window.h>
    5151
     52static void wnd_close_event(void *);
    5253static void wnd_kbd_event(void *, kbd_event_t *);
    5354
    5455static display_wnd_cb_t wnd_cb = {
     56        .close_event = wnd_close_event,
    5557        .kbd_event = wnd_kbd_event
    5658};
     
    500502}
    501503
     504static void wnd_close_event(void *arg)
     505{
     506        printf("Close event\n");
     507        quit = true;
     508}
     509
    502510static void wnd_kbd_event(void *arg, kbd_event_t *event)
    503511{
  • uspace/lib/display/include/types/display.h

    r7bb45e3 r338d0935  
    6262/** Display window callbacks */
    6363typedef struct {
     64        /** Close event */
     65        void (*close_event)(void *);
    6466        /** Focus event */
    6567        void (*focus_event)(void *);
    66         /** Keyboard event callback */
     68        /** Keyboard event */
    6769        void (*kbd_event)(void *, kbd_event_t *);
    68         /** Position event callback */
     70        /** Position event */
    6971        void (*pos_event)(void *, pos_event_t *);
    7072        /** Unfocus event */
  • uspace/lib/display/include/types/display/event.h

    r7bb45e3 r338d0935  
    4141/** Display window event type */
    4242typedef enum {
     43        /** Request to close window */
     44        wev_close,
    4345        /** Window gained focus */
    4446        wev_focus,
  • uspace/lib/display/src/display.c

    r7bb45e3 r338d0935  
    370370
    371371                switch (event.etype) {
     372                case wev_close:
     373                        if (window->cb != NULL && window->cb->close_event != NULL) {
     374                                window->cb->close_event(window->cb_arg);
     375                        }
     376                        break;
    372377                case wev_focus:
    373378                        if (window->cb != NULL && window->cb->focus_event != NULL) {
  • uspace/lib/display/test/display.c

    r7bb45e3 r338d0935  
    4848static void test_display_conn(ipc_call_t *, void *);
    4949
     50static void test_close_event(void *);
    5051static void test_focus_event(void *);
    5152static void test_kbd_event(void *, kbd_event_t *);
     
    6970
    7071static display_wnd_cb_t test_display_wnd_cb = {
     72        .close_event = test_close_event,
    7173        .focus_event = test_focus_event,
    7274        .kbd_event = test_kbd_event,
     
    100102        bool get_event_called;
    101103        bool set_color_called;
     104        bool close_event_called;
    102105        bool focus_event_called;
    103106        bool kbd_event_called;
     
    526529
    527530        display_close(disp);
     531        rc = loc_service_unregister(sid);
     532        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     533}
     534
     535/** Close event can be delivered from server to client callback function */
     536PCUT_TEST(close_event_deliver)
     537{
     538        errno_t rc;
     539        service_id_t sid;
     540        display_t *disp = NULL;
     541        display_wnd_params_t params;
     542        display_window_t *wnd;
     543        test_response_t resp;
     544
     545        async_set_fallback_port_handler(test_display_conn, &resp);
     546
     547        // FIXME This causes this test to be non-reentrant!
     548        rc = loc_server_register(test_display_server);
     549        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     550
     551        rc = loc_service_register(test_display_svc, &sid);
     552        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     553
     554        rc = display_open(test_display_svc, &disp);
     555        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     556        PCUT_ASSERT_NOT_NULL(disp);
     557        PCUT_ASSERT_NOT_NULL(resp.srv);
     558
     559        wnd = NULL;
     560        resp.rc = EOK;
     561        display_wnd_params_init(&params);
     562        params.rect.p0.x = 0;
     563        params.rect.p0.y = 0;
     564        params.rect.p0.x = 100;
     565        params.rect.p0.y = 100;
     566
     567        rc = display_window_create(disp, &params, &test_display_wnd_cb,
     568            (void *) &resp, &wnd);
     569        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     570        PCUT_ASSERT_NOT_NULL(wnd);
     571
     572        resp.event_cnt = 1;
     573        resp.event.etype = wev_close;
     574        resp.wnd_id = wnd->id;
     575        resp.close_event_called = false;
     576        fibril_mutex_initialize(&resp.event_lock);
     577        fibril_condvar_initialize(&resp.event_cv);
     578        display_srv_ev_pending(resp.srv);
     579
     580        /* Wait for the event handler to be called. */
     581        fibril_mutex_lock(&resp.event_lock);
     582        while (!resp.close_event_called) {
     583                fibril_condvar_wait(&resp.event_cv, &resp.event_lock);
     584        }
     585        fibril_mutex_unlock(&resp.event_lock);
     586
     587        /* Verify that the event was delivered correctly */
     588        PCUT_ASSERT_EQUALS(resp.event.etype,
     589            resp.revent.etype);
     590
     591        rc = display_window_destroy(wnd);
     592        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     593
     594        display_close(disp);
     595
    528596        rc = loc_service_unregister(sid);
    529597        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     
    861929}
    862930
     931static void test_close_event(void *arg)
     932{
     933        test_response_t *resp = (test_response_t *) arg;
     934
     935        resp->revent.etype = wev_close;
     936
     937        fibril_mutex_lock(&resp->event_lock);
     938        resp->close_event_called = true;
     939        fibril_condvar_broadcast(&resp->event_cv);
     940        fibril_mutex_unlock(&resp->event_lock);
     941}
     942
    863943static void test_focus_event(void *arg)
    864944{
  • uspace/lib/gui/window.c

    r7bb45e3 r338d0935  
    8383static pixel_t color_caption_unfocus = PIXEL(255, 207, 207, 207);
    8484
     85static void window_close_event(void *);
    8586static void window_focus_event(void *);
    8687static void window_kbd_event(void *, kbd_event_t *);
     
    8990
    9091static display_wnd_cb_t window_cb = {
     92        .close_event = window_close_event,
    9193        .focus_event = window_focus_event,
    9294        .kbd_event = window_kbd_event,
     
    332334                        //win_grab(widget->window->osess, event.pos_id, flags);
    333335                } else if (close && btn_left) {
    334                         //win_close_request(widget->window->osess);
     336                        window_close(widget->window);
    335337                } else if (header && btn_left) {
    336338                        window_grab_flags_t flags = GF_EMPTY;
     
    766768void window_close(window_t *win)
    767769{
    768         /* Request compositor to init closing cascade. */
    769         //win_close_request(win->osess);
     770        window_event_t *event;
     771
     772        event = (window_event_t *) calloc(1, sizeof(window_event_t));
     773        if (event == NULL)
     774                return;
     775
     776        link_initialize(&event->link);
     777        event->type = ET_WINDOW_CLOSE;
     778        prodcons_produce(&win->events, &event->link);
     779}
     780
     781static void window_close_event(void *arg)
     782{
     783        window_t *win = (window_t *) arg;
     784
     785        window_close(win);
    770786}
    771787
  • uspace/srv/hid/display/client.c

    r7bb45e3 r338d0935  
    203203}
    204204
    205 /** Post focus event to the client's message queue.
    206  *
    207  * @param client Client
    208  * @param ewindow Window that the message is targetted to
    209  *
    210  * @return EOK on success or an error code
    211  */
    212 errno_t ds_client_post_focus_event(ds_client_t *client, ds_window_t *ewindow)
    213 {
    214         ds_window_ev_t *wevent;
    215 
    216         wevent = calloc(1, sizeof(ds_window_ev_t));
    217         if (wevent == NULL)
    218                 return ENOMEM;
    219 
    220         wevent->window = ewindow;
    221         wevent->event.etype = wev_focus;
     205/** Post close event to the client's message queue.
     206 *
     207 * @param client Client
     208 * @param ewindow Window that the message is targetted to
     209 *
     210 * @return EOK on success or an error code
     211 */
     212errno_t ds_client_post_close_event(ds_client_t *client, ds_window_t *ewindow)
     213{
     214        ds_window_ev_t *wevent;
     215
     216        wevent = calloc(1, sizeof(ds_window_ev_t));
     217        if (wevent == NULL)
     218                return ENOMEM;
     219
     220        wevent->window = ewindow;
     221        wevent->event.etype = wev_close;
    222222        list_append(&wevent->levents, &client->events);
    223223
     
    230230}
    231231
     232/** Post focus event to the client's message queue.
     233 *
     234 * @param client Client
     235 * @param ewindow Window that the message is targetted to
     236 *
     237 * @return EOK on success or an error code
     238 */
     239errno_t ds_client_post_focus_event(ds_client_t *client, ds_window_t *ewindow)
     240{
     241        ds_window_ev_t *wevent;
     242
     243        wevent = calloc(1, sizeof(ds_window_ev_t));
     244        if (wevent == NULL)
     245                return ENOMEM;
     246
     247        wevent->window = ewindow;
     248        wevent->event.etype = wev_focus;
     249        list_append(&wevent->levents, &client->events);
     250
     251        /* Notify the client */
     252        // TODO Do not send more than once until client drains the queue
     253        if (client->cb != NULL && client->cb->ev_pending != NULL)
     254                client->cb->ev_pending(client->cb_arg);
     255
     256        return EOK;
     257}
     258
    232259/** Post keyboard event to the client's message queue.
    233260 *
  • uspace/srv/hid/display/client.h

    r7bb45e3 r338d0935  
    5353extern errno_t ds_client_get_event(ds_client_t *, ds_window_t **,
    5454    display_wnd_ev_t *);
     55extern errno_t ds_client_post_close_event(ds_client_t *, ds_window_t *);
    5556extern errno_t ds_client_post_focus_event(ds_client_t *, ds_window_t *);
    5657extern errno_t ds_client_post_kbd_event(ds_client_t *, ds_window_t *,
  • uspace/srv/hid/display/seat.c

    r7bb45e3 r338d0935  
    137137                return EOK;
    138138
    139         return ds_client_post_kbd_event(dwindow->client, dwindow, event);
     139        return ds_window_post_kbd_event(dwindow, event);
    140140}
    141141
  • uspace/srv/hid/display/test/client.c

    r7bb45e3 r338d0935  
    163163}
    164164
     165/** Test ds_client_get_event(), ds_client_post_close_event(). */
     166PCUT_TEST(client_get_post_close_event)
     167{
     168        ds_display_t *disp;
     169        ds_client_t *client;
     170        ds_window_t *wnd;
     171        display_wnd_params_t params;
     172        ds_window_t *rwindow;
     173        display_wnd_ev_t revent;
     174        bool called_cb = NULL;
     175        errno_t rc;
     176
     177        rc = ds_display_create(NULL, &disp);
     178        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     179
     180        rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
     181        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     182
     183        display_wnd_params_init(&params);
     184        params.rect.p0.x = params.rect.p0.y = 0;
     185        params.rect.p1.x = params.rect.p1.y = 1;
     186
     187        rc = ds_window_create(client, &params, &wnd);
     188        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     189
     190        PCUT_ASSERT_FALSE(called_cb);
     191
     192        rc = ds_client_get_event(client, &rwindow, &revent);
     193        PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
     194
     195        rc = ds_client_post_close_event(client, wnd);
     196        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     197        PCUT_ASSERT_TRUE(called_cb);
     198
     199        rc = ds_client_get_event(client, &rwindow, &revent);
     200        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     201        PCUT_ASSERT_EQUALS(wnd, rwindow);
     202        PCUT_ASSERT_EQUALS(wev_close, revent.etype);
     203
     204        rc = ds_client_get_event(client, &rwindow, &revent);
     205        PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
     206
     207        ds_window_destroy(wnd);
     208        ds_client_destroy(client);
     209        ds_display_destroy(disp);
     210}
     211
    165212/** Test ds_client_get_event(), ds_client_post_focus_event(). */
    166213PCUT_TEST(client_get_post_focus_event)
  • uspace/srv/hid/display/test/window.c

    r7bb45e3 r338d0935  
    124124}
    125125
     126/** Test ds_window_post_kbd_event() with Alt-F4 sends close event */
     127PCUT_TEST(window_post_kbd_event_alt_f4)
     128{
     129        gfx_context_t *gc;
     130        ds_display_t *disp;
     131        ds_client_t *client;
     132        ds_window_t *wnd;
     133        ds_window_t *rwindow;
     134        display_wnd_ev_t revent;
     135        display_wnd_params_t params;
     136        kbd_event_t event;
     137        errno_t rc;
     138
     139        rc = gfx_context_new(&dummy_ops, NULL, &gc);
     140        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     141
     142        rc = ds_display_create(gc, &disp);
     143        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     144
     145        rc = ds_client_create(disp, NULL, NULL, &client);
     146        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     147
     148        display_wnd_params_init(&params);
     149        params.rect.p0.x = params.rect.p0.y = 0;
     150        params.rect.p1.x = params.rect.p1.y = 1;
     151
     152        rc = ds_window_create(client, &params, &wnd);
     153        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     154
     155        event.type = KEY_PRESS;
     156        event.mods = KM_ALT;
     157        event.key = KC_F4;
     158        rc = ds_window_post_kbd_event(wnd, &event);
     159        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     160
     161        rc = ds_client_get_event(client, &rwindow, &revent);
     162        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     163        PCUT_ASSERT_EQUALS(wnd, rwindow);
     164        PCUT_ASSERT_EQUALS(wev_close, revent.etype);
     165
     166        rc = ds_client_get_event(client, &rwindow, &revent);
     167        PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
     168
     169        ds_window_destroy(wnd);
     170        ds_client_destroy(client);
     171        ds_display_destroy(disp);
     172}
     173
    126174/** Test ds_window_post_pos_event() */
    127175PCUT_TEST(window_post_pos_event)
  • uspace/srv/hid/display/window.c

    r7bb45e3 r338d0935  
    529529}
    530530
     531/** Post keyboard event to window.
     532 *
     533 * @param wnd Window
     534 * @param event Event
     535 *
     536 * @return EOK on success or an error code
     537 */
     538errno_t ds_window_post_kbd_event(ds_window_t *wnd, kbd_event_t *event)
     539{
     540        bool alt_or_shift;
     541
     542        alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
     543
     544        if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_F4) {
     545                /* On Alt-F4 or Shift-F4 send close event to the window */
     546                ds_client_post_close_event(wnd->client, wnd);
     547                return EOK;
     548        }
     549
     550        return ds_client_post_kbd_event(wnd->client, wnd, event);
     551}
     552
    531553/** Post position event to window.
    532554 *
  • uspace/srv/hid/display/window.h

    r7bb45e3 r338d0935  
    5454extern gfx_context_t *ds_window_get_ctx(ds_window_t *);
    5555extern errno_t ds_window_paint(ds_window_t *, gfx_rect_t *);
     56extern errno_t ds_window_post_kbd_event(ds_window_t *, kbd_event_t *);
    5657extern errno_t ds_window_post_pos_event(ds_window_t *, pos_event_t *);
    5758extern errno_t ds_window_post_focus_event(ds_window_t *);
Note: See TracChangeset for help on using the changeset viewer.