Add myself to docs/COMMITTERS
[kugel-rb.git] / apps / usb_keymaps.c
blob466cf6f9e86d97b5cdc2b01fdc05247bd7bd26f3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Tomer Shalev
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #ifdef USB_ENABLE_HID
24 #include "action.h"
25 #include "lang.h"
26 #include "usbstack/usb_hid.h"
27 //#define LOGF_ENABLE
28 #include "logf.h"
30 #define MODIFIER(modifier, key) ((key) | (modifier << 8))
32 #define ALT(key) MODIFIER(HID_KEYBOARD_LEFT_ALT, key)
33 #define CTRL(key) MODIFIER(HID_KEYBOARD_LEFT_CONTROL, key)
34 #define SHIFT(key) MODIFIER(HID_KEYBOARD_LEFT_SHIFT, key)
36 #define NUM_KEY_MAPPINGS (sizeof(hid_key_mappings) / \
37 sizeof(hid_key_mappings[0]))
39 typedef struct
41 int action;
42 int id;
43 } mapping_t;
45 typedef struct
47 int lang_name;
48 usage_page_t usage_page;
49 mapping_t mapping[];
50 } hid_key_mapping_t;
52 static const hid_key_mapping_t hid_key_mapping_multimedia = {
53 LANG_MULTIMEDIA,
54 HID_USAGE_PAGE_CONSUMER,
56 /* Volume up */
57 { ACTION_USB_HID_DEC, HID_CONSUMER_USAGE_VOLUME_DECREMENT },
58 /* Volume down */
59 { ACTION_USB_HID_INC, HID_CONSUMER_USAGE_VOLUME_INCREMENT },
60 /* Volume mute */
61 { ACTION_USB_HID_SELECT, HID_CONSUMER_USAGE_MUTE },
62 /* Playback play / pause */
63 { ACTION_USB_HID_START, HID_CONSUMER_USAGE_PLAY_PAUSE },
64 /* Playback stop */
65 { ACTION_USB_HID_QUIT, HID_CONSUMER_USAGE_STOP },
66 /* Scan previous track */
67 { ACTION_USB_HID_LEFT, HID_CONSUMER_USAGE_SCAN_PREVIOUS_TRACK },
68 /* Scan next track */
69 { ACTION_USB_HID_RIGHT, HID_CONSUMER_USAGE_SCAN_NEXT_TRACK },
70 { 0, 0 },
74 static const hid_key_mapping_t hid_key_mapping_presentation = {
75 LANG_PRESENTATION,
76 HID_USAGE_PAGE_KEYBOARD_KEYPAD,
78 /* Slideshow start */
79 { ACTION_USB_HID_START, HID_KEYBOARD_F5 },
80 /* Slideshow leave */
81 { ACTION_USB_HID_QUIT, HID_KEYBOARD_ESCAPE },
82 /* Slide previous */
83 { ACTION_USB_HID_LEFT, HID_KEYBOARD_P },
84 /* Slide next */
85 { ACTION_USB_HID_RIGHT, HID_KEYBOARD_N },
86 /* Slide first */
87 { ACTION_USB_HID_LEFT_LONG, HID_KEYBOARD_HOME },
88 /* Slide last */
89 { ACTION_USB_HID_RIGHT_LONG, HID_KEYBOARD_END },
90 /* Screen black */
91 { ACTION_USB_HID_MENU, HID_KEYBOARD_DOT },
92 /* Screen white*/
93 { ACTION_USB_HID_MENU_LONG, HID_KEYBOARD_COMMA },
94 /* Link previous */
95 { ACTION_USB_HID_DEC, SHIFT(HID_KEYBOARD_TAB) },
96 /* Link next */
97 { ACTION_USB_HID_INC, HID_KEYBOARD_TAB },
98 /* Mouse click */
99 { ACTION_USB_HID_SELECT, HID_KEYBOARD_RETURN },
100 /* Mouse over */
101 { ACTION_USB_HID_SELECT_LONG, SHIFT(HID_KEYBOARD_RETURN) },
102 { 0, 0 },
106 static const hid_key_mapping_t hid_key_mapping_browser = {
107 LANG_BROWSER,
108 HID_USAGE_PAGE_KEYBOARD_KEYPAD,
110 /* Scroll up */
111 { ACTION_USB_HID_DEC, HID_KEYBOARD_UP_ARROW },
112 /* Scroll down */
113 { ACTION_USB_HID_INC, HID_KEYBOARD_DOWN_ARROW },
114 /* Scroll page up */
115 { ACTION_USB_HID_START, HID_KEYBOARD_PAGE_UP },
116 /* Scroll page down */
117 { ACTION_USB_HID_MENU, HID_KEYBOARD_PAGE_DOWN },
118 /* Zoom in */
119 { ACTION_USB_HID_START_LONG, CTRL(HID_KEYPAD_PLUS) },
120 /* Zoom out */
121 { ACTION_USB_HID_MENU_LONG, CTRL(HID_KEYPAD_MINUS) },
122 /* Zoom reset */
123 { ACTION_USB_HID_SELECT_LONG, CTRL(HID_KEYPAD_0_AND_INSERT) },
124 /* Tab previous */
125 { ACTION_USB_HID_LEFT, CTRL(HID_KEYBOARD_PAGE_UP) },
126 /* Tab next */
127 { ACTION_USB_HID_RIGHT, CTRL(HID_KEYBOARD_PAGE_DOWN) },
128 /* Tab close */
129 { ACTION_USB_HID_QUIT_LONG, CTRL(HID_KEYBOARD_W) },
130 /* History back */
131 { ACTION_USB_HID_LEFT_LONG, ALT(HID_KEYBOARD_LEFT_ARROW) },
132 /* History forward */
133 { ACTION_USB_HID_RIGHT_LONG, ALT(HID_KEYBOARD_RIGHT_ARROW) },
134 /* View full-screen */
135 { ACTION_USB_HID_SELECT, HID_KEYBOARD_F11 },
136 { 0, 0 },
140 #ifdef HAVE_USB_HID_MOUSE
141 static const hid_key_mapping_t hid_key_mapping_mouse = {
142 LANG_MOUSE,
143 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS,
145 /* Cursor move up */
146 { ACTION_USB_HID_MOUSE_UP, HID_MOUSE_UP },
147 { ACTION_USB_HID_MOUSE_UP_REP, HID_MOUSE_UP_REP },
148 { ACTION_USB_HID_MOUSE_LDRAG_UP, HID_MOUSE_LDRAG_UP },
149 { ACTION_USB_HID_MOUSE_LDRAG_UP_REP, HID_MOUSE_LDRAG_UP_REP },
150 { ACTION_USB_HID_MOUSE_RDRAG_UP, HID_MOUSE_RDRAG_UP },
151 { ACTION_USB_HID_MOUSE_RDRAG_UP_REP, HID_MOUSE_RDRAG_UP_REP },
152 /* Cursor move down */
153 { ACTION_USB_HID_MOUSE_DOWN, HID_MOUSE_DOWN },
154 { ACTION_USB_HID_MOUSE_DOWN_REP, HID_MOUSE_DOWN_REP },
155 { ACTION_USB_HID_MOUSE_LDRAG_DOWN, HID_MOUSE_LDRAG_DOWN },
156 { ACTION_USB_HID_MOUSE_LDRAG_DOWN_REP, HID_MOUSE_LDRAG_DOWN_REP },
157 { ACTION_USB_HID_MOUSE_RDRAG_DOWN, HID_MOUSE_RDRAG_DOWN },
158 { ACTION_USB_HID_MOUSE_RDRAG_DOWN_REP, HID_MOUSE_RDRAG_DOWN_REP },
159 /* Cursor move left */
160 { ACTION_USB_HID_MOUSE_LEFT, HID_MOUSE_LEFT },
161 { ACTION_USB_HID_MOUSE_LEFT_REP, HID_MOUSE_LEFT_REP },
162 { ACTION_USB_HID_MOUSE_LDRAG_LEFT, HID_MOUSE_LDRAG_LEFT },
163 { ACTION_USB_HID_MOUSE_LDRAG_LEFT_REP, HID_MOUSE_LDRAG_LEFT_REP },
164 { ACTION_USB_HID_MOUSE_RDRAG_LEFT, HID_MOUSE_RDRAG_LEFT },
165 { ACTION_USB_HID_MOUSE_RDRAG_LEFT_REP, HID_MOUSE_RDRAG_LEFT_REP },
166 /* Cursor move right */
167 { ACTION_USB_HID_MOUSE_RIGHT, HID_MOUSE_RIGHT },
168 { ACTION_USB_HID_MOUSE_RIGHT_REP, HID_MOUSE_RIGHT_REP },
169 { ACTION_USB_HID_MOUSE_LDRAG_RIGHT, HID_MOUSE_LDRAG_RIGHT },
170 { ACTION_USB_HID_MOUSE_LDRAG_RIGHT_REP, HID_MOUSE_LDRAG_RIGHT_REP },
171 { ACTION_USB_HID_MOUSE_RDRAG_RIGHT, HID_MOUSE_RDRAG_RIGHT },
172 { ACTION_USB_HID_MOUSE_RDRAG_RIGHT_REP, HID_MOUSE_RDRAG_RIGHT_REP },
173 /* Mouse button left-click */
174 { ACTION_USB_HID_MOUSE_BUTTON_LEFT, HID_MOUSE_BUTTON_LEFT },
175 { ACTION_USB_HID_MOUSE_BUTTON_LEFT_REL, HID_MOUSE_BUTTON_LEFT_REL },
176 /* Mouse button right-click */
177 { ACTION_USB_HID_MOUSE_BUTTON_RIGHT, HID_MOUSE_BUTTON_RIGHT },
178 { ACTION_USB_HID_MOUSE_BUTTON_RIGHT_REL, HID_MOUSE_BUTTON_RIGHT_REL },
179 /* Mouse wheel scroll up */
180 { ACTION_USB_HID_DEC, HID_MOUSE_SCROLL_UP },
181 /* Mouse wheel scroll down */
182 { ACTION_USB_HID_INC, HID_MOUSE_SCROLL_DOWN },
183 { 0, 0 },
187 #define USB_KEYPAD_MODE_MOUSE 3 /* Value of the mouse setting (hard-coded) */
188 #endif /* HAVE_USB_HID_MOUSE */
190 static const hid_key_mapping_t *hid_key_mappings[] =
192 &hid_key_mapping_multimedia,
193 &hid_key_mapping_presentation,
194 &hid_key_mapping_browser,
195 #ifdef HAVE_USB_HID_MOUSE
196 &hid_key_mapping_mouse,
197 #endif
200 extern int usb_keypad_mode;
202 int get_hid_usb_action(void)
204 int action, context = CONTEXT_USB_HID;
206 #ifdef HAVE_USB_HID_MOUSE
207 if (usb_keypad_mode == USB_KEYPAD_MODE_MOUSE)
208 context = CONTEXT_USB_HID_MOUSE;
209 #endif
211 action = get_action(context, HZ/4);
212 /* Skip key mappings in a cyclic way */
213 if (action == ACTION_USB_HID_MODE)
215 usb_keypad_mode = (usb_keypad_mode + 1) % NUM_KEY_MAPPINGS;
217 else if (action == ACTION_USB_HID_MODE_LONG)
219 usb_keypad_mode = (usb_keypad_mode - 1) % NUM_KEY_MAPPINGS;
221 else if (action > ACTION_USB_HID_FIRST && action < ACTION_USB_HID_LAST)
223 const mapping_t *mapping;
224 const hid_key_mapping_t *key_mapping =
225 hid_key_mappings[usb_keypad_mode];
227 for (mapping = key_mapping->mapping; mapping->action; mapping++)
229 if (action == mapping->action)
231 logf("Action %d", action);
232 usb_hid_send(key_mapping->usage_page, mapping->id);
233 break;
236 #ifdef DEBUG
237 if (!mapping->action)
238 logf("Action %d not found", action);
239 #endif
242 return action;
245 int keypad_mode_name_get(void)
247 return hid_key_mappings[usb_keypad_mode]->lang_name;
250 #endif