| 1 | /* | 
|---|
| 2 | * Copyright (c) 2011 Lubos Slovak | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup drvusbhid | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 | /** | 
|---|
| 33 | * @file | 
|---|
| 34 | * USB HID keyboard autorepeat facilities | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #include <fibril_synch.h> | 
|---|
| 38 | #include <io/keycode.h> | 
|---|
| 39 | #include <io/console.h> | 
|---|
| 40 | #include <errno.h> | 
|---|
| 41 |  | 
|---|
| 42 | #include <usb/debug.h> | 
|---|
| 43 |  | 
|---|
| 44 | #include "kbdrepeat.h" | 
|---|
| 45 | #include "kbddev.h" | 
|---|
| 46 |  | 
|---|
| 47 | /** | 
|---|
| 48 | * Main loop handling the auto-repeat of keys. | 
|---|
| 49 | * | 
|---|
| 50 | * This functions periodically checks if there is some key to be auto-repeated. | 
|---|
| 51 | * | 
|---|
| 52 | * If a new key is to be repeated, it uses the delay before first repeat stored | 
|---|
| 53 | * in the keyboard structure to wait until the key has to start repeating. | 
|---|
| 54 | * | 
|---|
| 55 | * If the same key is still pressed, it uses the delay between repeats stored | 
|---|
| 56 | * in the keyboard structure to wait until the key should be repeated. | 
|---|
| 57 | * | 
|---|
| 58 | * If the currently repeated key is not pressed any more ( | 
|---|
| 59 | * usb_kbd_repeat_stop() was called), it stops repeating it and starts | 
|---|
| 60 | * checking again. | 
|---|
| 61 | * | 
|---|
| 62 | * @note For accessing the keyboard device auto-repeat information a fibril | 
|---|
| 63 | *       mutex (repeat_mtx) from the @a kbd structure is used. | 
|---|
| 64 | * | 
|---|
| 65 | * @param kbd Keyboard device structure. | 
|---|
| 66 | */ | 
|---|
| 67 | static void usb_kbd_repeat_loop(usb_kbd_t *kbd) | 
|---|
| 68 | { | 
|---|
| 69 | unsigned int delay = 0; | 
|---|
| 70 |  | 
|---|
| 71 | usb_log_debug("Starting autorepeat loop.\n"); | 
|---|
| 72 |  | 
|---|
| 73 | while (true) { | 
|---|
| 74 | /* Check if the kbd structure is usable. */ | 
|---|
| 75 | if (!usb_kbd_is_initialized(kbd)) { | 
|---|
| 76 | usb_log_warning("kbd not ready, exiting autorepeat.\n"); | 
|---|
| 77 | return; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | fibril_mutex_lock(&kbd->repeat_mtx); | 
|---|
| 81 |  | 
|---|
| 82 | if (kbd->repeat.key_new > 0) { | 
|---|
| 83 | if (kbd->repeat.key_new == kbd->repeat.key_repeated) { | 
|---|
| 84 | usb_log_debug2("Repeating key: %u.\n", | 
|---|
| 85 | kbd->repeat.key_repeated); | 
|---|
| 86 | usb_kbd_push_ev(kbd, KEY_PRESS, | 
|---|
| 87 | kbd->repeat.key_repeated); | 
|---|
| 88 | delay = kbd->repeat.delay_between; | 
|---|
| 89 | } else { | 
|---|
| 90 | usb_log_debug2("New key to repeat: %u.\n", | 
|---|
| 91 | kbd->repeat.key_new); | 
|---|
| 92 | kbd->repeat.key_repeated = kbd->repeat.key_new; | 
|---|
| 93 | delay = kbd->repeat.delay_before; | 
|---|
| 94 | } | 
|---|
| 95 | } else { | 
|---|
| 96 | if (kbd->repeat.key_repeated > 0) { | 
|---|
| 97 | usb_log_debug2("Stopping to repeat key: %u.\n", | 
|---|
| 98 | kbd->repeat.key_repeated); | 
|---|
| 99 | kbd->repeat.key_repeated = 0; | 
|---|
| 100 | } | 
|---|
| 101 | delay = CHECK_DELAY; | 
|---|
| 102 | } | 
|---|
| 103 | fibril_mutex_unlock(&kbd->repeat_mtx); | 
|---|
| 104 | async_usleep(delay); | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 | /*----------------------------------------------------------------------------*/ | 
|---|
| 108 | /** | 
|---|
| 109 | * Main routine to be executed by a fibril for handling auto-repeat. | 
|---|
| 110 | * | 
|---|
| 111 | * Starts the loop for checking changes in auto-repeat. | 
|---|
| 112 | * | 
|---|
| 113 | * @param arg User-specified argument. Expects pointer to the keyboard device | 
|---|
| 114 | *            structure representing the keyboard. | 
|---|
| 115 | * | 
|---|
| 116 | * @retval EOK if the routine has finished. | 
|---|
| 117 | * @retval EINVAL if no argument is supplied. | 
|---|
| 118 | */ | 
|---|
| 119 | int usb_kbd_repeat_fibril(void *arg) | 
|---|
| 120 | { | 
|---|
| 121 | usb_log_debug("Autorepeat fibril spawned.\n"); | 
|---|
| 122 |  | 
|---|
| 123 | if (arg == NULL) { | 
|---|
| 124 | usb_log_error("No device!\n"); | 
|---|
| 125 | return EINVAL; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | usb_kbd_t *kbd = arg; | 
|---|
| 129 |  | 
|---|
| 130 | usb_kbd_repeat_loop(kbd); | 
|---|
| 131 |  | 
|---|
| 132 | return EOK; | 
|---|
| 133 | } | 
|---|
| 134 | /*----------------------------------------------------------------------------*/ | 
|---|
| 135 | /** | 
|---|
| 136 | * Start repeating particular key. | 
|---|
| 137 | * | 
|---|
| 138 | * @note Only one key is repeated at any time, so calling this function | 
|---|
| 139 | *       effectively cancels auto-repeat of the current repeated key (if any) | 
|---|
| 140 | *       and 'schedules' another key for auto-repeat. | 
|---|
| 141 | * | 
|---|
| 142 | * @param kbd Keyboard device structure. | 
|---|
| 143 | * @param key Key to start repeating. | 
|---|
| 144 | */ | 
|---|
| 145 | void usb_kbd_repeat_start(usb_kbd_t *kbd, unsigned int key) | 
|---|
| 146 | { | 
|---|
| 147 | fibril_mutex_lock(&kbd->repeat_mtx); | 
|---|
| 148 | kbd->repeat.key_new = key; | 
|---|
| 149 | fibril_mutex_unlock(&kbd->repeat_mtx); | 
|---|
| 150 | } | 
|---|
| 151 | /*----------------------------------------------------------------------------*/ | 
|---|
| 152 | /** | 
|---|
| 153 | * Stop repeating particular key. | 
|---|
| 154 | * | 
|---|
| 155 | * @note Only one key is repeated at any time, but this function may be called | 
|---|
| 156 | *       even with key that is not currently repeated (in that case nothing | 
|---|
| 157 | *       happens). | 
|---|
| 158 | * | 
|---|
| 159 | * @param kbd Keyboard device structure. | 
|---|
| 160 | * @param key Key to stop repeating. | 
|---|
| 161 | */ | 
|---|
| 162 | void usb_kbd_repeat_stop(usb_kbd_t *kbd, unsigned int key) | 
|---|
| 163 | { | 
|---|
| 164 | fibril_mutex_lock(&kbd->repeat_mtx); | 
|---|
| 165 | if (key == kbd->repeat.key_new) { | 
|---|
| 166 | kbd->repeat.key_new = 0; | 
|---|
| 167 | } | 
|---|
| 168 | fibril_mutex_unlock(&kbd->repeat_mtx); | 
|---|
| 169 | } | 
|---|
| 170 | /** | 
|---|
| 171 | * @} | 
|---|
| 172 | */ | 
|---|