Changeset 2f7a564 in mainline


Ignore:
Timestamp:
2011-06-13T22:05:30Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ef15fbb
Parents:
a07a454
Message:

Make key lock/modifier state as well as layout state per-device.

Location:
uspace/srv/hid/input
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/input/Makefile

    ra07a454 r2f7a564  
    4545SOURCES = \
    4646        generic/input.c \
     47        generic/layout.c \
    4748        genarch/gsp.c \
    4849        genarch/stroke.c \
  • uspace/srv/hid/input/generic/input.c

    ra07a454 r2f7a564  
    7070int client_phone = -1;
    7171
    72 /** Currently active modifiers. */
    73 static unsigned mods = KM_NUM_LOCK;
    74 
    75 /** Currently pressed lock keys. We track these to tackle autorepeat. */
    76 static unsigned lock_keys;
    77 
    7872/** List of keyboard devices */
    7973static link_t kbd_devs;
     
    8478#define NUM_LAYOUTS 3
    8579
    86 static layout_op_t *layout[NUM_LAYOUTS] = {
    87         &us_qwerty_op,
    88         &us_dvorak_op,
    89         &cz_op
     80static layout_ops_t *layout[NUM_LAYOUTS] = {
     81        &us_qwerty_ops,
     82        &us_dvorak_ops,
     83        &cz_ops
    9084};
    91 
    92 static int active_layout = 0;
    9385
    9486void kbd_push_scancode(kbd_dev_t *kdev, int scancode)
     
    115107        if (mod_mask != 0) {
    116108                if (type == KEY_PRESS)
    117                         mods = mods | mod_mask;
     109                        kdev->mods = kdev->mods | mod_mask;
    118110                else
    119                         mods = mods & ~mod_mask;
     111                        kdev->mods = kdev->mods & ~mod_mask;
    120112        }
    121113
     
    134126                         * up the lock state.
    135127                         */
    136                         mods = mods ^ (mod_mask & ~lock_keys);
    137                         lock_keys = lock_keys | mod_mask;
     128                        kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
     129                        kdev->lock_keys = kdev->lock_keys | mod_mask;
    138130
    139131                        /* Update keyboard lock indicator lights. */
    140                         (*kdev->ctl_ops->set_ind)(kdev, mods);
     132                        (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
    141133                } else {
    142                         lock_keys = lock_keys & ~mod_mask;
     134                        kdev->lock_keys = kdev->lock_keys & ~mod_mask;
    143135                }
    144136        }
     
    148140        printf("keycode: %u\n", key);
    149141*/
    150         if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     142        if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
    151143                key == KC_F1) {
    152                 active_layout = 0;
    153                 layout[active_layout]->reset();
     144                layout_destroy(kdev->active_layout);
     145                kdev->active_layout = layout_create(layout[0]);
    154146                return;
    155147        }
    156148
    157         if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     149        if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
    158150                key == KC_F2) {
    159                 active_layout = 1;
    160                 layout[active_layout]->reset();
     151                layout_destroy(kdev->active_layout);
     152                kdev->active_layout = layout_create(layout[1]);
    161153                return;
    162154        }
    163155
    164         if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     156        if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
    165157                key == KC_F3) {
    166                 active_layout = 2;
    167                 layout[active_layout]->reset();
     158                layout_destroy(kdev->active_layout);
     159                kdev->active_layout = layout_create(layout[2]);
    168160                return;
    169161        }
     
    171163        ev.type = type;
    172164        ev.key = key;
    173         ev.mods = mods;
    174 
    175         ev.c = layout[active_layout]->parse_ev(&ev);
     165        ev.mods = kdev->mods;
     166
     167        ev.c = layout_parse_ev(kdev->active_layout, &ev);
    176168
    177169        async_obsolete_msg_4(client_phone, INPUT_EVENT, ev.type, ev.key, ev.mods, ev.c);
     
    223215}
    224216
     217static kbd_dev_t *kbd_dev_new(void)
     218{
     219        kbd_dev_t *kdev;
     220
     221        kdev = calloc(1, sizeof(kbd_dev_t));
     222        if (kdev == NULL) {
     223                printf(NAME ": Allocating keyboard device. Out of memory.\n");
     224                return NULL;
     225        }
     226
     227        link_initialize(&kdev->kbd_devs);
     228
     229        kdev->mods = KM_NUM_LOCK;
     230        kdev->lock_keys = 0;
     231        kdev->active_layout = layout_create(layout[0]);
     232
     233        return kdev;
     234}
     235
    225236/** Add new legacy keyboard device. */
    226237static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
     
    228239        kbd_dev_t *kdev;
    229240
    230         kdev = malloc(sizeof(kbd_dev_t));
    231         if (kdev == NULL) {
    232                 printf(NAME ": Failed adding keyboard device. Out of memory.\n");
     241        kdev = kbd_dev_new();
     242        if (kdev == NULL)
    233243                return;
    234         }
    235 
    236         link_initialize(&kdev->kbd_devs);
    237         kdev->dev_path = NULL;
     244
    238245        kdev->port_ops = port;
    239246        kdev->ctl_ops = ctl;
     247        kdev->dev_path = NULL;
    240248
    241249        /* Initialize port driver. */
     
    263271        kbd_dev_t *kdev;
    264272
    265         kdev = malloc(sizeof(kbd_dev_t));
    266         if (kdev == NULL) {
    267                 printf(NAME ": Failed adding keyboard device. Out of memory.\n");
     273        kdev = kbd_dev_new();
     274        if (kdev == NULL)
    268275                return -1;
    269         }
    270 
    271         link_initialize(&kdev->kbd_devs);
     276
    272277        kdev->dev_path = dev_path;
    273278        kdev->port_ops = NULL;
     
    435440        kbd_add_legacy_devs();
    436441
    437         /* Initialize (reset) layout. */
    438         layout[active_layout]->reset();
    439442       
    440443        /* Register driver */
  • uspace/srv/hid/input/include/kbd.h

    ra07a454 r2f7a564  
    4747struct kbd_port_ops;
    4848struct kbd_ctl_ops;
     49struct layout;
    4950
    5051typedef struct kbd_dev {
     
    6364        /** Controller-private data */
    6465        void *ctl_private;
     66
     67        /** Currently active modifiers. */
     68        unsigned mods;
     69
     70        /** Currently pressed lock keys. We track these to tackle autorepeat. */
     71        unsigned lock_keys;
     72
     73        /** Active keyboard layout */
     74        struct layout *active_layout;
    6575} kbd_dev_t;
    6676
  • uspace/srv/hid/input/include/layout.h

    ra07a454 r2f7a564  
    4141#include <io/console.h>
    4242
    43 typedef struct {
    44         void (*reset)(void);
    45         wchar_t (*parse_ev)(kbd_event_t *);
    46 } layout_op_t;
     43/** Layout instance state */
     44typedef struct layout {
     45        /** Ops structure */
     46        struct layout_ops *ops;
    4747
    48 extern layout_op_t us_qwerty_op;
    49 extern layout_op_t us_dvorak_op;
    50 extern layout_op_t cz_op;
     48        /* Layout-private data */
     49        void *layout_priv;
     50} layout_t;
     51
     52/** Layout ops */
     53typedef struct layout_ops {
     54        int (*create)(layout_t *);
     55        void (*destroy)(layout_t *);
     56        wchar_t (*parse_ev)(layout_t *, kbd_event_t *);
     57} layout_ops_t;
     58
     59extern layout_ops_t us_qwerty_ops;
     60extern layout_ops_t us_dvorak_ops;
     61extern layout_ops_t cz_ops;
     62
     63extern layout_t *layout_create(layout_ops_t *);
     64extern void layout_destroy(layout_t *);
     65extern wchar_t layout_parse_ev(layout_t *, kbd_event_t *);
    5166
    5267#endif
  • uspace/srv/hid/input/layout/cz.c

    ra07a454 r2f7a564  
    11/*
    2  * Copyright (c) 2009 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3232 */
    3333
     34#include <errno.h>
    3435#include <kbd.h>
    3536#include <io/console.h>
     
    3738#include <bool.h>
    3839#include <layout.h>
    39 
    40 static void layout_reset(void);
    41 static wchar_t layout_parse_ev(kbd_event_t *ev);
     40#include <stdlib.h>
     41
     42static int cz_create(layout_t *);
     43static void cz_destroy(layout_t *);
     44static wchar_t cz_parse_ev(layout_t *, kbd_event_t *ev);
    4245
    4346enum m_state {
     
    4750};
    4851
    49 static enum m_state mstate;
    50 
    51 layout_op_t cz_op = {
    52         layout_reset,
    53         layout_parse_ev
     52typedef struct {
     53        enum m_state mstate;
     54} layout_cz_t;
     55
     56layout_ops_t cz_ops = {
     57        .create = cz_create,
     58        .destroy = cz_destroy,
     59        .parse_ev = cz_parse_ev
    5460};
    5561
     
    273279}
    274280
    275 static wchar_t parse_ms_hacek(kbd_event_t *ev)
     281static wchar_t parse_ms_hacek(layout_cz_t *cz_state, kbd_event_t *ev)
    276282{
    277283        wchar_t c;
    278284
    279         mstate = ms_start;
     285        cz_state->mstate = ms_start;
    280286
    281287        /* Produce no characters when Ctrl or Alt is pressed. */
     
    291297}
    292298
    293 static wchar_t parse_ms_carka(kbd_event_t *ev)
     299static wchar_t parse_ms_carka(layout_cz_t *cz_state, kbd_event_t *ev)
    294300{
    295301        wchar_t c;
    296302
    297         mstate = ms_start;
     303        cz_state->mstate = ms_start;
    298304
    299305        /* Produce no characters when Ctrl or Alt is pressed. */
     
    309315}
    310316
    311 static wchar_t parse_ms_start(kbd_event_t *ev)
     317static wchar_t parse_ms_start(layout_cz_t *cz_state, kbd_event_t *ev)
    312318{
    313319        wchar_t c;
     
    319325        if (ev->key == KC_EQUALS) {
    320326                if ((ev->mods & KM_SHIFT) != 0)
    321                         mstate = ms_hacek;
     327                        cz_state->mstate = ms_hacek;
    322328                else
    323                         mstate = ms_carka;
     329                        cz_state->mstate = ms_carka;
    324330
    325331                return 0;
     
    379385}
    380386
    381 static void layout_reset(void)
    382 {
    383         mstate = ms_start;
    384 }
    385 
    386 static wchar_t layout_parse_ev(kbd_event_t *ev)
    387 {
     387static int cz_create(layout_t *state)
     388{
     389        layout_cz_t *cz_state;
     390
     391        cz_state = malloc(sizeof(layout_cz_t));
     392        if (cz_state == NULL) {
     393                printf(NAME ": Out of memory.\n");
     394                return ENOMEM;
     395        }
     396
     397        cz_state->mstate = ms_start;
     398        state->layout_priv = (void *) cz_state;
     399
     400        return EOK;
     401}
     402
     403static void cz_destroy(layout_t *state)
     404{
     405        free(state->layout_priv);
     406}
     407
     408static wchar_t cz_parse_ev(layout_t *state, kbd_event_t *ev)
     409{
     410        layout_cz_t *cz_state = (layout_cz_t *) state->layout_priv;
     411
    388412        if (ev->type != KEY_PRESS)
    389413                return 0;
     
    392416                return 0;
    393417       
    394         switch (mstate) {
     418        switch (cz_state->mstate) {
    395419        case ms_start:
    396                 return parse_ms_start(ev);
     420                return parse_ms_start(cz_state, ev);
    397421        case ms_hacek:
    398                 return parse_ms_hacek(ev);
     422                return parse_ms_hacek(cz_state, ev);
    399423        case ms_carka:
    400                 return parse_ms_carka(ev);
     424                return parse_ms_carka(cz_state, ev);
    401425        }
    402426       
  • uspace/srv/hid/input/layout/us_dvorak.c

    ra07a454 r2f7a564  
    11/*
    2  * Copyright (c) 2009 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3232 */
    3333
     34#include <errno.h>
    3435#include <kbd.h>
    3536#include <io/console.h>
     
    3738#include <layout.h>
    3839
    39 static void layout_reset(void);
    40 static wchar_t layout_parse_ev(kbd_event_t *ev);
    41 
    42 layout_op_t us_dvorak_op = {
    43         layout_reset,
    44         layout_parse_ev
     40static int us_dvorak_create(layout_t *);
     41static void us_dvorak_destroy(layout_t *);
     42static wchar_t us_dvorak_parse_ev(layout_t *, kbd_event_t *ev);
     43
     44layout_ops_t us_dvorak_ops = {
     45        .create = us_dvorak_create,
     46        .destroy = us_dvorak_destroy,
     47        .parse_ev = us_dvorak_parse_ev
    4548};
    4649
     
    206209}
    207210
    208 static void layout_reset(void)
    209 {
    210 }
    211 
    212 static wchar_t layout_parse_ev(kbd_event_t *ev)
     211static int us_dvorak_create(layout_t *state)
     212{
     213        return EOK;
     214}
     215
     216static void us_dvorak_destroy(layout_t *state)
     217{
     218}
     219
     220static wchar_t us_dvorak_parse_ev(layout_t *state, kbd_event_t *ev)
    213221{
    214222        wchar_t c;
  • uspace/srv/hid/input/layout/us_qwerty.c

    ra07a454 r2f7a564  
    11/*
    2  * Copyright (c) 2009 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3232 */
    3333
     34#include <errno.h>
    3435#include <kbd.h>
    3536#include <io/console.h>
     
    3738#include <layout.h>
    3839
    39 static void layout_reset(void);
    40 static wchar_t layout_parse_ev(kbd_event_t *ev);
    41 
    42 layout_op_t us_qwerty_op = {
    43         layout_reset,
    44         layout_parse_ev
     40static int us_qwerty_create(layout_t *);
     41static void us_qwerty_destroy(layout_t *);
     42static wchar_t us_qwerty_parse_ev(layout_t *, kbd_event_t *ev);
     43
     44layout_ops_t us_qwerty_ops = {
     45        .create = us_qwerty_create,
     46        .destroy = us_qwerty_destroy,
     47        .parse_ev = us_qwerty_parse_ev
    4548};
    4649
     
    200203}
    201204
    202 static void layout_reset(void)
    203 {
    204 }
    205 
    206 static wchar_t layout_parse_ev(kbd_event_t *ev)
     205static int us_qwerty_create(layout_t *state)
     206{
     207        return EOK;
     208}
     209
     210static void us_qwerty_destroy(layout_t *state)
     211{
     212}
     213
     214static wchar_t us_qwerty_parse_ev(layout_t *state, kbd_event_t *ev)
    207215{
    208216        wchar_t c;
Note: See TracChangeset for help on using the changeset viewer.