Changeset 24cf391a in mainline


Ignore:
Timestamp:
2019-12-03T10:59:47Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
973efd36
Parents:
79949f3
git-author:
Jiri Svoboda <jiri@…> (2019-11-02 18:59:43)
git-committer:
Jiri Svoboda <jiri@…> (2019-12-03 10:59:47)
Message:

Switch window focus on mouse click

Location:
uspace/srv/hid/display
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/display/display.c

    r79949f3 r24cf391a  
    164164}
    165165
     166/** Find window by display position.
     167 *
     168 * @param display Display
     169 * @param pos Display position
     170 */
     171ds_window_t *ds_display_window_by_pos(ds_display_t *display, gfx_coord2_t *pos)
     172{
     173        ds_window_t *wnd;
     174
     175        wnd = ds_display_first_window(display);
     176        while (wnd != NULL) {
     177                // XXX Need to know window dimensions
     178                if (pos->x >= wnd->dpos.x && pos->y >= wnd->dpos.y &&
     179                    pos->x <= wnd->dpos.x + 100 && pos->y <= wnd->dpos.y + 100) {
     180                        return wnd;
     181                }
     182
     183                wnd = ds_display_next_window(wnd);
     184        }
     185
     186        return NULL;
     187}
     188
    166189/** Add window to display.
    167190 *
     
    239262}
    240263
     264/** Post position event to a display.
     265 *
     266 * @param display Display
     267 * @param event Event
     268 */
     269errno_t ds_display_post_pos_event(ds_display_t *display, pos_event_t *event)
     270{
     271        gfx_coord2_t pos;
     272        ds_window_t *wnd;
     273        ds_seat_t *seat;
     274
     275        /* Focus window on button press */
     276        if (event->type == POS_PRESS) {
     277                printf("Button press\n");
     278                pos.x = event->hpos;
     279                pos.y = event->vpos;
     280
     281                wnd = ds_display_window_by_pos(display, &pos);
     282                if (wnd != NULL) {
     283                        seat = ds_display_first_seat(display);
     284                        if (seat == NULL)
     285                                return EOK;
     286
     287                        ds_seat_set_focus(seat, wnd);
     288                        return EOK;
     289                }
     290        }
     291
     292        return EOK;
     293}
     294
    241295/** Add seat to display.
    242296 *
  • uspace/srv/hid/display/display.h

    r79949f3 r24cf391a  
    4040#include <gfx/context.h>
    4141#include <io/kbd_event.h>
     42#include <io/pos_event.h>
    4243#include "types/display/client.h"
    4344#include "types/display/display.h"
     
    5152extern ds_client_t *ds_display_next_client(ds_client_t *);
    5253extern ds_window_t *ds_display_find_window(ds_display_t *, ds_wnd_id_t);
     54extern ds_window_t *ds_display_window_by_pos(ds_display_t *, gfx_coord2_t *);
    5355extern void ds_display_add_window(ds_display_t *, ds_window_t *);
    5456extern void ds_display_remove_window(ds_window_t *);
     
    5658extern ds_window_t *ds_display_next_window(ds_window_t *);
    5759extern errno_t ds_display_post_kbd_event(ds_display_t *, kbd_event_t *);
     60extern errno_t ds_display_post_pos_event(ds_display_t *, pos_event_t *);
    5861extern void ds_display_add_seat(ds_display_t *, ds_seat_t *);
    5962extern void ds_display_remove_seat(ds_seat_t *);
  • uspace/srv/hid/display/main.c

    r79949f3 r24cf391a  
    4040#include <str_error.h>
    4141#include <io/log.h>
     42#include <io/kbd_event.h>
     43#include <io/pos_event.h>
    4244#include <ipc/services.h>
    4345#include <ipcgfx/server.h>
     
    6466        ds_display_t *disp = (ds_display_t *) arg;
    6567
    66         printf("display_kbd_event\n");
    6768        ds_display_post_kbd_event(disp, event);
     69}
     70
     71static void display_pos_event(void *arg, pos_event_t *event)
     72{
     73        ds_display_t *disp = (ds_display_t *) arg;
     74
     75        ds_display_post_pos_event(disp, event);
    6876}
    6977
     
    93101                goto error;
    94102
    95         rc = output_init(display_kbd_event, (void *) disp, &gc);
     103        rc = output_init(display_kbd_event, (void *) disp,
     104            display_pos_event, (void *) disp, &gc);
    96105        if (rc != EOK)
    97106                goto error;
  • uspace/srv/hid/display/output.c

    r79949f3 r24cf391a  
    3737#include <gfx/context.h>
    3838#include <guigfx/canvas.h>
     39#include <io/kbd_event.h>
     40#include <io/pos_event.h>
    3941#include <stdio.h>
    4042#include <stdlib.h>
     
    4446static void (*kbd_ev_handler)(void *, kbd_event_t *);
    4547static void *kbd_ev_arg;
     48static void (*pos_ev_handler)(void *, pos_event_t *);
     49static void *pos_ev_arg;
    4650
    4751static void on_keyboard_event(widget_t *widget, void *data)
     
    5155}
    5256
     57static void on_position_event(widget_t *widget, void *data)
     58{
     59        pos_ev_handler(pos_ev_arg, (pos_event_t *) data);
     60}
     61
    5362errno_t output_init(void (*kbd_event_handler)(void *, kbd_event_t *),
    54     void *arg, gfx_context_t **rgc)
     63    void *karg, void (*pos_event_handler)(void *, pos_event_t *),
     64    void *parg, gfx_context_t **rgc)
    5565{
    5666        canvas_gc_t *cgc = NULL;
     
    6474        printf("Init canvas..\n");
    6575        kbd_ev_handler = kbd_event_handler;
    66         kbd_ev_arg = arg;
     76        kbd_ev_arg = karg;
     77
     78        pos_ev_handler = pos_event_handler;
     79        pos_ev_arg = parg;
    6780
    6881        window = window_open("comp:0/winreg", NULL,
     
    96109
    97110        sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
     111        sig_connect(&canvas->position_event, NULL, on_position_event);
    98112
    99113        window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY);
  • uspace/srv/hid/display/output.h

    r79949f3 r24cf391a  
    3838
    3939#include <gfx/context.h>
     40#include <io/kbd_event.h>
     41#include <io/pos_event.h>
    4042
    4143extern errno_t output_init(void (*)(void *, kbd_event_t *), void *,
     44    void (*)(void *, pos_event_t *), void *,
    4245    gfx_context_t **);
    4346
  • uspace/srv/hid/display/test/display.c

    r79949f3 r24cf391a  
    140140}
    141141
     142/** Test ds_display_window_by_pos(). */
     143PCUT_TEST(display_window_by_pos)
     144{
     145        ds_display_t *disp;
     146        ds_client_t *client;
     147        ds_window_t *w0;
     148        ds_window_t *w1;
     149        ds_window_t *wnd;
     150        gfx_coord2_t pos;
     151        errno_t rc;
     152
     153        rc = ds_display_create(NULL, &disp);
     154        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     155
     156        rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
     157        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     158
     159        rc = ds_window_create(client, &w0);
     160        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     161
     162        rc = ds_window_create(client, &w1);
     163        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     164
     165        w0->dpos.x = 10;
     166        w0->dpos.y = 10;
     167
     168        w1->dpos.x = 400;
     169        w1->dpos.y = 400;
     170
     171        pos.x = 10;
     172        pos.y = 10;
     173        wnd = ds_display_window_by_pos(disp, &pos);
     174        PCUT_ASSERT_EQUALS(w0, wnd);
     175
     176        pos.x = 400;
     177        pos.y = 400;
     178        wnd = ds_display_window_by_pos(disp, &pos);
     179        PCUT_ASSERT_EQUALS(w1, wnd);
     180
     181        ds_window_destroy(w0);
     182        ds_window_destroy(w1);
     183        ds_client_destroy(client);
     184        ds_display_destroy(disp);
     185}
     186
    142187/** Basic seat operation. */
    143188PCUT_TEST(display_seat)
     
    164209}
    165210
    166 /** Test ds_display_post_kbd_event(). */
     211/** Test ds_display_post_kbd_event() delivers event to client callback.
     212 */
    167213PCUT_TEST(display_post_kbd_event)
    168214{
     
    172218        ds_window_t *wnd;
    173219        kbd_event_t event;
    174         bool called_cb = NULL;
     220        bool called_cb = false;
    175221        errno_t rc;
    176222
     
    206252}
    207253
     254/** Test ds_display_post_kbd_event() with Alt-Tab switches focus.
     255 */
     256PCUT_TEST(display_post_kbd_event_alt_tab)
     257{
     258        ds_display_t *disp;
     259        ds_seat_t *seat;
     260        ds_client_t *client;
     261        ds_window_t *w0, *w1;
     262        kbd_event_t event;
     263        bool called_cb = false;
     264        errno_t rc;
     265
     266        rc = ds_display_create(NULL, &disp);
     267        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     268
     269        rc = ds_seat_create(disp, &seat);
     270        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     271
     272        rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
     273        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     274
     275        rc = ds_window_create(client, &w0);
     276        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     277
     278        rc = ds_window_create(client, &w1);
     279        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     280
     281        ds_seat_set_focus(seat, w0);
     282
     283        event.type = KEY_PRESS;
     284        event.key = KC_TAB;
     285        event.mods = KM_ALT;
     286        event.c = L'\0';
     287
     288        PCUT_ASSERT_FALSE(called_cb);
     289
     290        rc = ds_display_post_kbd_event(disp, &event);
     291        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     292        PCUT_ASSERT_FALSE(called_cb);
     293
     294        /* Next window should be focused */
     295        PCUT_ASSERT_EQUALS(w1, seat->focus);
     296
     297        rc = ds_display_post_kbd_event(disp, &event);
     298        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     299        PCUT_ASSERT_FALSE(called_cb);
     300
     301        /* Focus should be back to the first window */
     302        PCUT_ASSERT_EQUALS(w0, seat->focus);
     303
     304        ds_window_destroy(w0);
     305        ds_window_destroy(w1);
     306        ds_client_destroy(client);
     307        ds_seat_destroy(seat);
     308        ds_display_destroy(disp);
     309}
     310
     311/** Test ds_display_post_pos_event() with click on window switches focus
     312 */
     313PCUT_TEST(display_post_pos_event_wnd_switch)
     314{
     315        ds_display_t *disp;
     316        ds_seat_t *seat;
     317        ds_client_t *client;
     318        ds_window_t *w0, *w1;
     319        pos_event_t event;
     320        bool called_cb = false;
     321        errno_t rc;
     322
     323        rc = ds_display_create(NULL, &disp);
     324        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     325
     326        rc = ds_seat_create(disp, &seat);
     327        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     328
     329        rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
     330        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     331
     332        rc = ds_window_create(client, &w0);
     333        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     334
     335        rc = ds_window_create(client, &w1);
     336        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     337
     338        w0->dpos.x = 10;
     339        w0->dpos.y = 10;
     340
     341        w1->dpos.x = 400;
     342        w1->dpos.y = 400;
     343
     344        ds_seat_set_focus(seat, w0);
     345
     346        event.type = POS_PRESS;
     347        event.btn_num = 1;
     348
     349        PCUT_ASSERT_FALSE(called_cb);
     350
     351        event.hpos = 400;
     352        event.vpos = 400;
     353        rc = ds_display_post_pos_event(disp, &event);
     354        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     355        PCUT_ASSERT_FALSE(called_cb);
     356
     357        PCUT_ASSERT_EQUALS(w1, seat->focus);
     358
     359        event.hpos = 10;
     360        event.vpos = 10;
     361        rc = ds_display_post_pos_event(disp, &event);
     362        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     363        PCUT_ASSERT_FALSE(called_cb);
     364
     365        PCUT_ASSERT_EQUALS(w0, seat->focus);
     366
     367        ds_window_destroy(w0);
     368        ds_window_destroy(w1);
     369        ds_client_destroy(client);
     370        ds_seat_destroy(seat);
     371        ds_display_destroy(disp);
     372}
     373
    208374PCUT_EXPORT(display);
Note: See TracChangeset for help on using the changeset viewer.