[dfe53af] | 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 |
|
---|
[7309799] | 47 |
|
---|
| 48 | /** Delay between auto-repeat state checks when no key is being repeated. */
|
---|
[17ada7a] | 49 | static unsigned int CHECK_DELAY = 10000;
|
---|
[dfe53af] | 50 |
|
---|
| 51 | /*----------------------------------------------------------------------------*/
|
---|
[7309799] | 52 | /**
|
---|
| 53 | * Main loop handling the auto-repeat of keys.
|
---|
| 54 | *
|
---|
| 55 | * This functions periodically checks if there is some key to be auto-repeated.
|
---|
| 56 | *
|
---|
| 57 | * If a new key is to be repeated, it uses the delay before first repeat stored
|
---|
| 58 | * in the keyboard structure to wait until the key has to start repeating.
|
---|
| 59 | *
|
---|
| 60 | * If the same key is still pressed, it uses the delay between repeats stored
|
---|
| 61 | * in the keyboard structure to wait until the key should be repeated.
|
---|
| 62 | *
|
---|
| 63 | * If the currently repeated key is not pressed any more (
|
---|
| 64 | * usbhid_kbd_repeat_stop() was called), it stops repeating it and starts
|
---|
| 65 | * checking again.
|
---|
| 66 | *
|
---|
| 67 | * @note For accessing the keyboard device auto-repeat information a fibril
|
---|
| 68 | * mutex (repeat_mtx) from the @a kbd structure is used.
|
---|
| 69 | *
|
---|
| 70 | * @param kbd Keyboard device structure.
|
---|
| 71 | */
|
---|
[dfe53af] | 72 | static void usbhid_kbd_repeat_loop(usbhid_kbd_t *kbd)
|
---|
| 73 | {
|
---|
| 74 | unsigned int delay = 0;
|
---|
| 75 |
|
---|
| 76 | usb_log_debug("Starting autorepeat loop.\n");
|
---|
| 77 |
|
---|
| 78 | while (true) {
|
---|
[00b13408] | 79 | // check if the kbd structure is usable
|
---|
[18b3cfd] | 80 | if (!usbhid_kbd_is_initialized(kbd)) {
|
---|
| 81 | if (usbhid_kbd_is_ready_to_destroy(kbd)) {
|
---|
| 82 | usbhid_kbd_free(&kbd);
|
---|
| 83 | assert(kbd == NULL);
|
---|
| 84 | }
|
---|
[00b13408] | 85 | return;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[dfe53af] | 88 | fibril_mutex_lock(kbd->repeat_mtx);
|
---|
| 89 |
|
---|
| 90 | if (kbd->repeat.key_new > 0) {
|
---|
| 91 | if (kbd->repeat.key_new == kbd->repeat.key_repeated) {
|
---|
| 92 | usb_log_debug2("Repeating key: %u.\n",
|
---|
| 93 | kbd->repeat.key_repeated);
|
---|
| 94 | usbhid_kbd_push_ev(kbd, KEY_PRESS,
|
---|
| 95 | kbd->repeat.key_repeated);
|
---|
| 96 | delay = kbd->repeat.delay_between;
|
---|
| 97 | } else {
|
---|
| 98 | usb_log_debug("New key to repeat: %u.\n",
|
---|
| 99 | kbd->repeat.key_new);
|
---|
| 100 | kbd->repeat.key_repeated = kbd->repeat.key_new;
|
---|
| 101 | delay = kbd->repeat.delay_before;
|
---|
| 102 | }
|
---|
| 103 | } else {
|
---|
| 104 | if (kbd->repeat.key_repeated > 0) {
|
---|
| 105 | usb_log_debug("Stopping to repeat key: %u.\n",
|
---|
| 106 | kbd->repeat.key_repeated);
|
---|
| 107 | kbd->repeat.key_repeated = 0;
|
---|
| 108 | }
|
---|
| 109 | delay = CHECK_DELAY;
|
---|
| 110 | }
|
---|
| 111 | fibril_mutex_unlock(kbd->repeat_mtx);
|
---|
| 112 |
|
---|
| 113 | async_usleep(delay);
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /*----------------------------------------------------------------------------*/
|
---|
[7309799] | 118 | /**
|
---|
| 119 | * Main routine to be executed by a fibril for handling auto-repeat.
|
---|
| 120 | *
|
---|
| 121 | * Starts the loop for checking changes in auto-repeat.
|
---|
| 122 | *
|
---|
| 123 | * @param arg User-specified argument. Expects pointer to the keyboard device
|
---|
| 124 | * structure representing the keyboard.
|
---|
| 125 | *
|
---|
| 126 | * @retval EOK if the routine has finished.
|
---|
| 127 | * @retval EINVAL if no argument is supplied.
|
---|
| 128 | */
|
---|
[dfe53af] | 129 | int usbhid_kbd_repeat_fibril(void *arg)
|
---|
| 130 | {
|
---|
| 131 | usb_log_debug("Autorepeat fibril spawned.\n");
|
---|
| 132 |
|
---|
| 133 | if (arg == NULL) {
|
---|
| 134 | usb_log_error("No device!\n");
|
---|
| 135 | return EINVAL;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | usbhid_kbd_t *kbd = (usbhid_kbd_t *)arg;
|
---|
| 139 |
|
---|
| 140 | usbhid_kbd_repeat_loop(kbd);
|
---|
| 141 |
|
---|
| 142 | return EOK;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /*----------------------------------------------------------------------------*/
|
---|
[7309799] | 146 | /**
|
---|
| 147 | * Start repeating particular key.
|
---|
| 148 | *
|
---|
| 149 | * @note Only one key is repeated at any time, so calling this function
|
---|
| 150 | * effectively cancels auto-repeat of the current repeated key (if any)
|
---|
| 151 | * and 'schedules' another key for auto-repeat.
|
---|
| 152 | *
|
---|
| 153 | * @param kbd Keyboard device structure.
|
---|
| 154 | * @param key Key to start repeating.
|
---|
| 155 | */
|
---|
[dfe53af] | 156 | void usbhid_kbd_repeat_start(usbhid_kbd_t *kbd, unsigned int key)
|
---|
| 157 | {
|
---|
| 158 | fibril_mutex_lock(kbd->repeat_mtx);
|
---|
| 159 | kbd->repeat.key_new = key;
|
---|
| 160 | fibril_mutex_unlock(kbd->repeat_mtx);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /*----------------------------------------------------------------------------*/
|
---|
[7309799] | 164 | /**
|
---|
| 165 | * Stop repeating particular key.
|
---|
| 166 | *
|
---|
| 167 | * @note Only one key is repeated at any time, but this function may be called
|
---|
| 168 | * even with key that is not currently repeated (in that case nothing
|
---|
| 169 | * happens).
|
---|
| 170 | *
|
---|
| 171 | * @param kbd Keyboard device structure.
|
---|
| 172 | * @param key Key to stop repeating.
|
---|
| 173 | */
|
---|
[dfe53af] | 174 | void usbhid_kbd_repeat_stop(usbhid_kbd_t *kbd, unsigned int key)
|
---|
| 175 | {
|
---|
| 176 | fibril_mutex_lock(kbd->repeat_mtx);
|
---|
| 177 | if (key == kbd->repeat.key_new) {
|
---|
| 178 | kbd->repeat.key_new = 0;
|
---|
| 179 | }
|
---|
| 180 | fibril_mutex_unlock(kbd->repeat_mtx);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
| 184 | * @}
|
---|
| 185 | */
|
---|