Changeset 3b2e387 in mainline for uspace/srv/hid/input/generic/input.c


Ignore:
Timestamp:
2011-06-21T19:36:05Z (13 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e3a6c45
Parents:
40f606b (diff), 022d9f67 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merge input server, console and various other improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/input/generic/input.c

    r40f606b r3b2e387  
    5858#include <kbd_port.h>
    5959#include <kbd_ctl.h>
     60#include <mouse_proto.h>
    6061#include <layout.h>
    6162#include <mouse.h>
     
    6566
    6667/* In microseconds */
    67 #define DISCOVERY_POLL_INTERVAL         (10*1000*1000)
    68 
    69 static void kbd_devs_yield(void);
    70 static void kbd_devs_reclaim(void);
    71 
    72 static void input_event_key(int, unsigned int, unsigned, wchar_t);
    73 
    74 int client_phone = -1;
    75 
    76 /** List of keyboard devices */
    77 static list_t kbd_devs;
    78 
    79 /** List of mouse devices */
    80 list_t mouse_devs;
    81 
    82 bool irc_service = false;
    83 int irc_phone = -1;
    84 
    85 #define NUM_LAYOUTS 3
     68#define DISCOVERY_POLL_INTERVAL  (10 * 1000 * 1000)
     69
     70#define NUM_LAYOUTS  3
    8671
    8772static layout_ops_t *layout[NUM_LAYOUTS] = {
     
    9176};
    9277
    93 void kbd_push_scancode(kbd_dev_t *kdev, int scancode)
    94 {
    95 /*      printf("scancode: 0x%x\n", scancode);*/
    96         (*kdev->ctl_ops->parse_scancode)(scancode);
    97 }
    98 
    99 void kbd_push_ev(kbd_dev_t *kdev, int type, unsigned int key)
     78static void kbd_devs_yield(void);
     79static void kbd_devs_reclaim(void);
     80
     81int client_phone = -1;
     82
     83/** List of keyboard devices */
     84static list_t kbd_devs;
     85
     86/** List of mouse devices */
     87static list_t mouse_devs;
     88
     89bool irc_service = false;
     90int irc_phone = -1;
     91
     92void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
     93{
     94        (*kdev->ctl_ops->parse)(data);
     95}
     96
     97void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
     98{
     99        (*mdev->proto_ops->parse)(data);
     100}
     101
     102void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
    100103{
    101104        kbd_event_t ev;
    102         unsigned mod_mask;
    103 
     105        unsigned int mod_mask;
     106       
    104107        switch (key) {
    105108        case KC_LCTRL: mod_mask = KM_LCTRL; break;
     
    111114        default: mod_mask = 0; break;
    112115        }
    113 
     116       
    114117        if (mod_mask != 0) {
    115118                if (type == KEY_PRESS)
     
    118121                        kdev->mods = kdev->mods & ~mod_mask;
    119122        }
    120 
     123       
    121124        switch (key) {
    122125        case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
     
    125128        default: mod_mask = 0; break;
    126129        }
    127 
     130       
    128131        if (mod_mask != 0) {
    129132                if (type == KEY_PRESS) {
     
    135138                        kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
    136139                        kdev->lock_keys = kdev->lock_keys | mod_mask;
    137 
     140                       
    138141                        /* Update keyboard lock indicator lights. */
    139142                        (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
     
    142145                }
    143146        }
    144 /*
    145         printf("type: %d\n", type);
    146         printf("mods: 0x%x\n", mods);
    147         printf("keycode: %u\n", key);
    148 */
     147       
    149148        if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
    150                 key == KC_F1) {
     149            key == KC_F1) {
    151150                layout_destroy(kdev->active_layout);
    152151                kdev->active_layout = layout_create(layout[0]);
    153152                return;
    154153        }
    155 
     154       
    156155        if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
    157                 key == KC_F2) {
     156            key == KC_F2) {
    158157                layout_destroy(kdev->active_layout);
    159158                kdev->active_layout = layout_create(layout[1]);
    160159                return;
    161160        }
    162 
     161       
    163162        if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
    164                 key == KC_F3) {
     163            key == KC_F3) {
    165164                layout_destroy(kdev->active_layout);
    166165                kdev->active_layout = layout_create(layout[2]);
    167166                return;
    168167        }
    169 
     168       
    170169        ev.type = type;
    171170        ev.key = key;
    172171        ev.mods = kdev->mods;
    173 
     172       
    174173        ev.c = layout_parse_ev(kdev->active_layout, &ev);
    175         input_event_key(ev.type, ev.key, ev.mods, ev.c);
    176 }
    177 
    178 /** Key has been pressed or released. */
    179 static void input_event_key(int type, unsigned int key, unsigned mods,
    180     wchar_t c)
    181 {
    182         async_obsolete_msg_4(client_phone, INPUT_EVENT_KEY, type, key,
    183             mods, c);
     174        async_obsolete_msg_4(client_phone, INPUT_EVENT_KEY, ev.type, ev.key,
     175            ev.mods, ev.c);
    184176}
    185177
    186178/** Mouse pointer has moved. */
    187 void input_event_move(int dx, int dy)
     179void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy)
    188180{
    189181        async_obsolete_msg_2(client_phone, INPUT_EVENT_MOVE, dx, dy);
     
    191183
    192184/** Mouse button has been pressed. */
    193 void input_event_button(int bnum, int press)
     185void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
    194186{
    195187        async_obsolete_msg_2(client_phone, INPUT_EVENT_BUTTON, bnum, press);
     
    201193        ipc_call_t call;
    202194        int retval;
    203 
     195       
    204196        async_answer_0(iid, EOK);
    205 
     197       
    206198        while (true) {
    207199                callid = async_get_call(&call);
     
    237229                        retval = EINVAL;
    238230                }
     231               
    239232                async_answer_0(callid, retval);
    240         }       
     233        }
    241234}
    242235
    243236static kbd_dev_t *kbd_dev_new(void)
    244237{
    245         kbd_dev_t *kdev;
    246 
    247         kdev = calloc(1, sizeof(kbd_dev_t));
     238        kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
    248239        if (kdev == NULL) {
    249                 printf(NAME ": Error allocating keyboard device. "
    250                     "Out of memory.\n");
     240                printf("%s: Error allocating keyboard device. "
     241                    "Out of memory.\n", NAME);
    251242                return NULL;
    252243        }
    253 
     244       
    254245        link_initialize(&kdev->kbd_devs);
    255 
     246       
    256247        kdev->mods = KM_NUM_LOCK;
    257248        kdev->lock_keys = 0;
    258249        kdev->active_layout = layout_create(layout[0]);
    259 
     250       
    260251        return kdev;
     252}
     253
     254static mouse_dev_t *mouse_dev_new(void)
     255{
     256        mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
     257        if (mdev == NULL) {
     258                printf("%s: Error allocating keyboard device. "
     259                    "Out of memory.\n", NAME);
     260                return NULL;
     261        }
     262       
     263        link_initialize(&mdev->mouse_devs);
     264       
     265        return mdev;
    261266}
    262267
     
    264269static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
    265270{
    266         kbd_dev_t *kdev;
    267 
    268         kdev = kbd_dev_new();
     271        kbd_dev_t *kdev = kbd_dev_new();
    269272        if (kdev == NULL)
    270273                return;
    271 
     274       
    272275        kdev->port_ops = port;
    273276        kdev->ctl_ops = ctl;
    274277        kdev->dev_path = NULL;
    275 
     278       
    276279        /* Initialize port driver. */
    277280        if ((*kdev->port_ops->init)(kdev) != 0)
    278281                goto fail;
    279 
     282       
    280283        /* Initialize controller driver. */
    281284        if ((*kdev->ctl_ops->init)(kdev) != 0) {
     
    283286                goto fail;
    284287        }
    285 
     288       
    286289        list_append(&kdev->kbd_devs, &kbd_devs);
    287290        return;
     291       
    288292fail:
    289293        free(kdev);
    290294}
    291295
     296/** Add new legacy mouse device. */
     297static void mouse_add_dev(mouse_port_ops_t *port, mouse_proto_ops_t *proto)
     298{
     299        mouse_dev_t *mdev = mouse_dev_new();
     300        if (mdev == NULL)
     301                return;
     302       
     303        mdev->port_ops = port;
     304        mdev->proto_ops = proto;
     305        mdev->dev_path = NULL;
     306       
     307        /* Initialize port driver. */
     308        if ((*mdev->port_ops->init)(mdev) != 0)
     309                goto fail;
     310       
     311        /* Initialize protocol driver. */
     312        if ((*mdev->proto_ops->init)(mdev) != 0) {
     313                /* XXX Uninit port */
     314                goto fail;
     315        }
     316       
     317        list_append(&mdev->mouse_devs, &mouse_devs);
     318        return;
     319       
     320fail:
     321        free(mdev);
     322}
     323
    292324/** Add new kbdev device.
    293325 *
    294  * @param dev_path      Filesystem path to the device (/dev/class/...)
     326 * @param dev_path Filesystem path to the device (/dev/class/...)
     327 *
    295328 */
    296329static int kbd_add_kbdev(const char *dev_path)
    297330{
    298         kbd_dev_t *kdev;
    299 
    300         kdev = kbd_dev_new();
     331        kbd_dev_t *kdev = kbd_dev_new();
    301332        if (kdev == NULL)
    302333                return -1;
    303 
     334       
    304335        kdev->dev_path = dev_path;
    305336        kdev->port_ops = NULL;
    306337        kdev->ctl_ops = &kbdev_ctl;
    307 
     338       
    308339        /* Initialize controller driver. */
    309340        if ((*kdev->ctl_ops->init)(kdev) != 0) {
    310341                goto fail;
    311342        }
    312 
     343       
    313344        list_append(&kdev->kbd_devs, &kbd_devs);
    314345        return EOK;
     346       
    315347fail:
    316348        free(kdev);
     349        return -1;
     350}
     351
     352/** Add new mousedev device.
     353 *
     354 * @param dev_path Filesystem path to the device (/dev/class/...)
     355 *
     356 */
     357static int mouse_add_mousedev(const char *dev_path)
     358{
     359        mouse_dev_t *mdev = mouse_dev_new();
     360        if (mdev == NULL)
     361                return -1;
     362       
     363        mdev->dev_path = dev_path;
     364        mdev->port_ops = NULL;
     365        mdev->proto_ops = &mousedev_proto;
     366       
     367        /* Initialize controller driver. */
     368        if ((*mdev->proto_ops->init)(mdev) != 0) {
     369                goto fail;
     370        }
     371       
     372        list_append(&mdev->mouse_devs, &mouse_devs);
     373        return EOK;
     374       
     375fail:
     376        free(mdev);
    317377        return -1;
    318378}
     
    375435}
    376436
     437/** Add legacy drivers/devices. */
     438static void mouse_add_legacy_devs(void)
     439{
     440        /*
     441         * Need to add these drivers based on config unless we can probe
     442         * them automatically.
     443         */
     444#if defined(UARCH_amd64)
     445        mouse_add_dev(&chardev_mouse_port, &ps2_proto);
     446#endif
     447#if defined(UARCH_ia32)
     448        mouse_add_dev(&chardev_mouse_port, &ps2_proto);
     449#endif
     450#if defined(MACHINE_i460GX)
     451        mouse_add_dev(&chardev_mouse_port, &ps2_proto);
     452#endif
     453#if defined(UARCH_ppc32)
     454        mouse_add_dev(&adb_mouse_port, &adb_proto);
     455#endif
     456        /* Silence warning on abs32le about mouse_add_dev() being unused */
     457        (void) mouse_add_dev;
     458}
     459
    377460static void kbd_devs_yield(void)
    378461{
     
    381464                kbd_dev_t *kdev = list_get_instance(kdev_link, kbd_dev_t,
    382465                    kbd_devs);
    383 
     466               
    384467                /* Yield port */
    385468                if (kdev->port_ops != NULL)
     
    394477                kbd_dev_t *kdev = list_get_instance(kdev_link, kbd_dev_t,
    395478                    kbd_devs);
    396 
     479               
    397480                /* Reclaim port */
    398481                if (kdev->port_ops != NULL)
     
    405488 * Looks under /dev/class/keyboard and /dev/class/mouse.
    406489 *
    407  * @param arg   Ignored
     490 * @param arg Ignored
     491 *
    408492 */
    409493static int dev_discovery_fibril(void *arg)
     
    413497        size_t mouse_id = 1;
    414498        int rc;
    415 
     499       
    416500        while (true) {
    417501                async_usleep(DISCOVERY_POLL_INTERVAL);
    418 
     502               
    419503                /*
    420504                 * Check for new keyboard device
     
    423507                if (rc < 0)
    424508                        continue;
    425 
     509               
    426510                if (kbd_add_kbdev(dev_path) == EOK) {
    427                         printf(NAME ": Connected keyboard device '%s'\n",
    428                             dev_path);
    429 
     511                        printf("%s: Connected keyboard device '%s'\n",
     512                            NAME, dev_path);
     513                       
    430514                        /* XXX Handle device removal */
    431515                        ++kbd_id;
    432516                }
    433 
     517               
    434518                free(dev_path);
    435 
     519               
    436520                /*
    437521                 * Check for new mouse device
     
    440524                if (rc < 0)
    441525                        continue;
    442 
    443                 if (mouse_add_dev(dev_path) == EOK) {
    444                         printf(NAME ": Connected mouse device '%s'\n",
    445                             dev_path);
    446 
     526               
     527                if (mouse_add_mousedev(dev_path) == EOK) {
     528                        printf("%s: Connected mouse device '%s'\n",
     529                            NAME, dev_path);
     530                       
    447531                        /* XXX Handle device removal */
    448532                        ++mouse_id;
    449533                }
    450 
     534               
    451535                free(dev_path);
    452536        }
    453 
     537       
    454538        return EOK;
    455539}
     
    458542static void input_start_dev_discovery(void)
    459543{
    460         fid_t fid;
    461 
    462         fid = fibril_create(dev_discovery_fibril, NULL);
     544        fid_t fid = fibril_create(dev_discovery_fibril, NULL);
    463545        if (!fid) {
    464                 printf(NAME ": Failed to create device discovery fibril.\n");
     546                printf("%s: Failed to create device discovery fibril.\n",
     547                    NAME);
    465548                return;
    466549        }
    467 
     550       
    468551        fibril_add_ready(fid);
    469552}
     
    490573        /* Add legacy keyboard devices. */
    491574        kbd_add_legacy_devs();
    492 
    493         /* Add legacy (devmap-style) mouse device. */
    494         (void) mouse_add_dev("/dev/hid_in/mouse");
     575       
     576        /* Add legacy mouse devices. */
     577        mouse_add_legacy_devs();
    495578       
    496579        /* Register driver */
     
    509592                return -1;
    510593        }
    511 
     594       
    512595        /* Start looking for new input devices */
    513596        input_start_dev_discovery();
    514 
    515         printf(NAME ": Accepting connections\n");
     597       
     598        printf("%s: Accepting connections\n", NAME);
    516599        async_manager();
    517 
     600       
    518601        /* Not reached. */
    519602        return 0;
Note: See TracChangeset for help on using the changeset viewer.