Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2009 Tomer Shalev |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
| 22 | |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 23 | #include "action.h" |
| 24 | #include "lang.h" |
Tomer Shalev | 83d24f8 | 2009-10-23 13:29:19 +0000 | [diff] [blame] | 25 | #include "misc.h" |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 26 | #include "usbstack/usb_hid.h" |
Bertrik Sikken | 552835e | 2010-01-18 21:20:36 +0000 | [diff] [blame] | 27 | #include "usb_keymaps.h" |
Jeffrey Goode | 9d84268 | 2009-11-03 16:25:03 +0000 | [diff] [blame] | 28 | /*#define LOGF_ENABLE*/ |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 29 | #include "logf.h" |
| 30 | |
| 31 | #define MODIFIER(modifier, key) ((key) | (modifier << 8)) |
| 32 | |
| 33 | #define ALT(key) MODIFIER(HID_KEYBOARD_LEFT_ALT, key) |
| 34 | #define CTRL(key) MODIFIER(HID_KEYBOARD_LEFT_CONTROL, key) |
| 35 | #define SHIFT(key) MODIFIER(HID_KEYBOARD_LEFT_SHIFT, key) |
| 36 | |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 37 | typedef struct |
| 38 | { |
| 39 | int action; |
| 40 | int id; |
| 41 | } mapping_t; |
| 42 | |
| 43 | typedef struct |
| 44 | { |
| 45 | int lang_name; |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 46 | int context; |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 47 | usage_page_t usage_page; |
| 48 | mapping_t mapping[]; |
| 49 | } hid_key_mapping_t; |
| 50 | |
| 51 | static const hid_key_mapping_t hid_key_mapping_multimedia = { |
Tomer Shalev | 753ca71 | 2009-10-04 05:57:11 +0000 | [diff] [blame] | 52 | LANG_MULTIMEDIA_MODE, |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 53 | CONTEXT_USB_HID_MODE_MULTIMEDIA, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 54 | HID_USAGE_PAGE_CONSUMER, |
| 55 | { |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 56 | { ACTION_USB_HID_MULTIMEDIA_VOLUME_UP, HID_CONSUMER_USAGE_VOLUME_INCREMENT }, |
| 57 | { ACTION_USB_HID_MULTIMEDIA_VOLUME_DOWN, HID_CONSUMER_USAGE_VOLUME_DECREMENT }, |
| 58 | { ACTION_USB_HID_MULTIMEDIA_VOLUME_MUTE, HID_CONSUMER_USAGE_MUTE }, |
| 59 | { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_PLAY_PAUSE, HID_CONSUMER_USAGE_PLAY_PAUSE }, |
| 60 | { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_STOP, HID_CONSUMER_USAGE_STOP }, |
| 61 | { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_TRACK_PREV, HID_CONSUMER_USAGE_SCAN_PREVIOUS_TRACK }, |
| 62 | { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_TRACK_NEXT, HID_CONSUMER_USAGE_SCAN_NEXT_TRACK }, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 63 | { 0, 0 }, |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | static const hid_key_mapping_t hid_key_mapping_presentation = { |
Tomer Shalev | 753ca71 | 2009-10-04 05:57:11 +0000 | [diff] [blame] | 68 | LANG_PRESENTATION_MODE, |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 69 | CONTEXT_USB_HID_MODE_PRESENTATION, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 70 | HID_USAGE_PAGE_KEYBOARD_KEYPAD, |
| 71 | { |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 72 | { ACTION_USB_HID_PRESENTATION_SLIDESHOW_START, HID_KEYBOARD_F5 }, |
| 73 | { ACTION_USB_HID_PRESENTATION_SLIDESHOW_LEAVE, HID_KEYBOARD_ESCAPE }, |
| 74 | { ACTION_USB_HID_PRESENTATION_SLIDE_PREV, HID_KEYBOARD_P }, |
| 75 | { ACTION_USB_HID_PRESENTATION_SLIDE_NEXT, HID_KEYBOARD_N }, |
| 76 | { ACTION_USB_HID_PRESENTATION_SLIDE_FIRST, HID_KEYBOARD_HOME }, |
| 77 | { ACTION_USB_HID_PRESENTATION_SLIDE_LAST, HID_KEYBOARD_END }, |
| 78 | { ACTION_USB_HID_PRESENTATION_SCREEN_BLACK, HID_KEYBOARD_DOT }, |
| 79 | { ACTION_USB_HID_PRESENTATION_SCREEN_WHITE, HID_KEYBOARD_COMMA }, |
| 80 | { ACTION_USB_HID_PRESENTATION_LINK_PREV, SHIFT(HID_KEYBOARD_TAB) }, |
| 81 | { ACTION_USB_HID_PRESENTATION_LINK_NEXT, HID_KEYBOARD_TAB }, |
| 82 | { ACTION_USB_HID_PRESENTATION_MOUSE_CLICK, HID_KEYBOARD_RETURN }, |
| 83 | { ACTION_USB_HID_PRESENTATION_MOUSE_OVER, SHIFT(HID_KEYBOARD_RETURN) }, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 84 | { 0, 0 }, |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | static const hid_key_mapping_t hid_key_mapping_browser = { |
Tomer Shalev | 753ca71 | 2009-10-04 05:57:11 +0000 | [diff] [blame] | 89 | LANG_BROWSER_MODE, |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 90 | CONTEXT_USB_HID_MODE_BROWSER, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 91 | HID_USAGE_PAGE_KEYBOARD_KEYPAD, |
| 92 | { |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 93 | { ACTION_USB_HID_BROWSER_SCROLL_UP, HID_KEYBOARD_UP_ARROW }, |
| 94 | { ACTION_USB_HID_BROWSER_SCROLL_DOWN, HID_KEYBOARD_DOWN_ARROW }, |
| 95 | { ACTION_USB_HID_BROWSER_SCROLL_UP, HID_KEYBOARD_PAGE_UP }, |
| 96 | { ACTION_USB_HID_BROWSER_SCROLL_DOWN, HID_KEYBOARD_PAGE_DOWN }, |
| 97 | { ACTION_USB_HID_BROWSER_ZOOM_IN, CTRL(HID_KEYPAD_PLUS) }, |
| 98 | { ACTION_USB_HID_BROWSER_ZOOM_OUT, CTRL(HID_KEYPAD_MINUS) }, |
| 99 | { ACTION_USB_HID_BROWSER_ZOOM_RESET, CTRL(HID_KEYPAD_0_AND_INSERT) }, |
| 100 | { ACTION_USB_HID_BROWSER_TAB_PREV, CTRL(HID_KEYBOARD_PAGE_UP) }, |
| 101 | { ACTION_USB_HID_BROWSER_TAB_NEXT, CTRL(HID_KEYBOARD_PAGE_DOWN) }, |
| 102 | { ACTION_USB_HID_BROWSER_TAB_CLOSE, CTRL(HID_KEYBOARD_W) }, |
| 103 | { ACTION_USB_HID_BROWSER_HISTORY_BACK, ALT(HID_KEYBOARD_LEFT_ARROW) }, |
| 104 | { ACTION_USB_HID_BROWSER_HISTORY_FORWARD, ALT(HID_KEYBOARD_RIGHT_ARROW) }, |
| 105 | { ACTION_USB_HID_BROWSER_VIEW_FULL_SCREEN, HID_KEYBOARD_F11 }, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 106 | { 0, 0 }, |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | #ifdef HAVE_USB_HID_MOUSE |
| 111 | static const hid_key_mapping_t hid_key_mapping_mouse = { |
Tomer Shalev | 753ca71 | 2009-10-04 05:57:11 +0000 | [diff] [blame] | 112 | LANG_MOUSE_MODE, |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 113 | CONTEXT_USB_HID_MODE_MOUSE, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 114 | HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS, |
| 115 | { |
Tomer Shalev | 363c116 | 2009-09-30 12:04:06 +0000 | [diff] [blame] | 116 | /* Cursor move up */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 117 | { ACTION_USB_HID_MOUSE_UP, HID_MOUSE_UP }, |
| 118 | { ACTION_USB_HID_MOUSE_UP_REP, HID_MOUSE_UP_REP }, |
| 119 | { ACTION_USB_HID_MOUSE_LDRAG_UP, HID_MOUSE_LDRAG_UP }, |
| 120 | { ACTION_USB_HID_MOUSE_LDRAG_UP_REP, HID_MOUSE_LDRAG_UP_REP }, |
| 121 | { ACTION_USB_HID_MOUSE_RDRAG_UP, HID_MOUSE_RDRAG_UP }, |
| 122 | { ACTION_USB_HID_MOUSE_RDRAG_UP_REP, HID_MOUSE_RDRAG_UP_REP }, |
Tomer Shalev | 363c116 | 2009-09-30 12:04:06 +0000 | [diff] [blame] | 123 | /* Cursor move down */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 124 | { ACTION_USB_HID_MOUSE_DOWN, HID_MOUSE_DOWN }, |
| 125 | { ACTION_USB_HID_MOUSE_DOWN_REP, HID_MOUSE_DOWN_REP }, |
| 126 | { ACTION_USB_HID_MOUSE_LDRAG_DOWN, HID_MOUSE_LDRAG_DOWN }, |
| 127 | { ACTION_USB_HID_MOUSE_LDRAG_DOWN_REP, HID_MOUSE_LDRAG_DOWN_REP }, |
| 128 | { ACTION_USB_HID_MOUSE_RDRAG_DOWN, HID_MOUSE_RDRAG_DOWN }, |
| 129 | { ACTION_USB_HID_MOUSE_RDRAG_DOWN_REP, HID_MOUSE_RDRAG_DOWN_REP }, |
Tomer Shalev | 363c116 | 2009-09-30 12:04:06 +0000 | [diff] [blame] | 130 | /* Cursor move left */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 131 | { ACTION_USB_HID_MOUSE_LEFT, HID_MOUSE_LEFT }, |
| 132 | { ACTION_USB_HID_MOUSE_LEFT_REP, HID_MOUSE_LEFT_REP }, |
| 133 | { ACTION_USB_HID_MOUSE_LDRAG_LEFT, HID_MOUSE_LDRAG_LEFT }, |
| 134 | { ACTION_USB_HID_MOUSE_LDRAG_LEFT_REP, HID_MOUSE_LDRAG_LEFT_REP }, |
| 135 | { ACTION_USB_HID_MOUSE_RDRAG_LEFT, HID_MOUSE_RDRAG_LEFT }, |
| 136 | { ACTION_USB_HID_MOUSE_RDRAG_LEFT_REP, HID_MOUSE_RDRAG_LEFT_REP }, |
Tomer Shalev | 363c116 | 2009-09-30 12:04:06 +0000 | [diff] [blame] | 137 | /* Cursor move right */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 138 | { ACTION_USB_HID_MOUSE_RIGHT, HID_MOUSE_RIGHT }, |
| 139 | { ACTION_USB_HID_MOUSE_RIGHT_REP, HID_MOUSE_RIGHT_REP }, |
| 140 | { ACTION_USB_HID_MOUSE_LDRAG_RIGHT, HID_MOUSE_LDRAG_RIGHT }, |
| 141 | { ACTION_USB_HID_MOUSE_LDRAG_RIGHT_REP, HID_MOUSE_LDRAG_RIGHT_REP }, |
| 142 | { ACTION_USB_HID_MOUSE_RDRAG_RIGHT, HID_MOUSE_RDRAG_RIGHT }, |
| 143 | { ACTION_USB_HID_MOUSE_RDRAG_RIGHT_REP, HID_MOUSE_RDRAG_RIGHT_REP }, |
Tomer Shalev | 363c116 | 2009-09-30 12:04:06 +0000 | [diff] [blame] | 144 | /* Mouse button left-click */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 145 | { ACTION_USB_HID_MOUSE_BUTTON_LEFT, HID_MOUSE_BUTTON_LEFT }, |
| 146 | { ACTION_USB_HID_MOUSE_BUTTON_LEFT_REL, HID_MOUSE_BUTTON_LEFT_REL }, |
Tomer Shalev | 363c116 | 2009-09-30 12:04:06 +0000 | [diff] [blame] | 147 | /* Mouse button right-click */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 148 | { ACTION_USB_HID_MOUSE_BUTTON_RIGHT, HID_MOUSE_BUTTON_RIGHT }, |
| 149 | { ACTION_USB_HID_MOUSE_BUTTON_RIGHT_REL, HID_MOUSE_BUTTON_RIGHT_REL }, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 150 | /* Mouse wheel scroll up */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 151 | { ACTION_USB_HID_MOUSE_WHEEL_SCROLL_UP, HID_MOUSE_SCROLL_UP }, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 152 | /* Mouse wheel scroll down */ |
Tomer Shalev | 0c379b4 | 2009-10-05 06:42:39 +0000 | [diff] [blame] | 153 | { ACTION_USB_HID_MOUSE_WHEEL_SCROLL_DOWN, HID_MOUSE_SCROLL_DOWN }, |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 154 | { 0, 0 }, |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | #define USB_KEYPAD_MODE_MOUSE 3 /* Value of the mouse setting (hard-coded) */ |
| 159 | #endif /* HAVE_USB_HID_MOUSE */ |
| 160 | |
| 161 | static const hid_key_mapping_t *hid_key_mappings[] = |
| 162 | { |
| 163 | &hid_key_mapping_multimedia, |
| 164 | &hid_key_mapping_presentation, |
| 165 | &hid_key_mapping_browser, |
| 166 | #ifdef HAVE_USB_HID_MOUSE |
| 167 | &hid_key_mapping_mouse, |
| 168 | #endif |
| 169 | }; |
| 170 | |
| 171 | extern int usb_keypad_mode; |
| 172 | |
| 173 | int get_hid_usb_action(void) |
| 174 | { |
Tomer Shalev | 83d24f8 | 2009-10-23 13:29:19 +0000 | [diff] [blame] | 175 | int action, step; |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 176 | const hid_key_mapping_t *key_mapping = hid_key_mappings[usb_keypad_mode]; |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 177 | |
Tomer Shalev | 83d24f8 | 2009-10-23 13:29:19 +0000 | [diff] [blame] | 178 | step = -1; |
Tomer Shalev | 4f3d7b5 | 2009-10-17 05:39:54 +0000 | [diff] [blame] | 179 | action = get_action(key_mapping->context, HZ/4); |
Tomer Shalev | 83d24f8 | 2009-10-23 13:29:19 +0000 | [diff] [blame] | 180 | switch (action) |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 181 | { |
Tomer Shalev | 83d24f8 | 2009-10-23 13:29:19 +0000 | [diff] [blame] | 182 | case ACTION_USB_HID_MODE_SWITCH_NEXT: |
| 183 | step = 1; |
| 184 | case ACTION_USB_HID_MODE_SWITCH_PREV: |
| 185 | /* Switch key mappings in a cyclic way */ |
| 186 | usb_keypad_mode = clamp_value_wrap(usb_keypad_mode + step, |
Tomer Shalev | 4806458 | 2009-10-25 20:27:38 +0000 | [diff] [blame] | 187 | ARRAYLEN(hid_key_mappings) - 1, 0); |
Tomer Shalev | 83d24f8 | 2009-10-23 13:29:19 +0000 | [diff] [blame] | 188 | break; |
| 189 | default: |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 190 | { |
Tomer Shalev | 83d24f8 | 2009-10-23 13:29:19 +0000 | [diff] [blame] | 191 | const mapping_t *mapping; |
| 192 | const hid_key_mapping_t *key_mapping = |
| 193 | hid_key_mappings[usb_keypad_mode]; |
| 194 | |
| 195 | if (action <= ACTION_USB_HID_FIRST || |
| 196 | action >= ACTION_USB_HID_LAST) |
| 197 | { |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | for (mapping = key_mapping->mapping; mapping->action; mapping++) |
| 202 | { |
| 203 | if (action == mapping->action) |
| 204 | { |
| 205 | logf("Action %d", action); |
| 206 | usb_hid_send(key_mapping->usage_page, mapping->id); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | #ifdef DEBUG |
| 211 | if (!mapping->action) |
| 212 | logf("Action %d not found", action); |
| 213 | #endif |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
Tomer Shalev | 8c5141b | 2009-09-30 10:06:04 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | return action; |
| 219 | } |
| 220 | |
| 221 | int keypad_mode_name_get(void) |
| 222 | { |
| 223 | return hid_key_mappings[usb_keypad_mode]->lang_name; |
| 224 | } |