Fix a dircache NULL-pointer dereference.
[kugel-rb.git] / apps / usb_keymaps.c
blobade2891720083f9ea3afb8938d26c88bfc6700de
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 "misc.h"
27 #include "usbstack/usb_hid.h"
28 #include "usb_keymaps.h"
29 /*#define LOGF_ENABLE*/
30 #include "logf.h"
32 #define MODIFIER(modifier, key) ((key) | (modifier << 8))
34 #define ALT(key) MODIFIER(HID_KEYBOARD_LEFT_ALT, key)
35 #define CTRL(key) MODIFIER(HID_KEYBOARD_LEFT_CONTROL, key)
36 #define SHIFT(key) MODIFIER(HID_KEYBOARD_LEFT_SHIFT, key)
38 typedef struct
40 int action;
41 int id;
42 } mapping_t;
44 typedef struct
46 int lang_name;
47 int context;
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_MODE,
54 CONTEXT_USB_HID_MODE_MULTIMEDIA,
55 HID_USAGE_PAGE_CONSUMER,
57 { ACTION_USB_HID_MULTIMEDIA_VOLUME_UP, HID_CONSUMER_USAGE_VOLUME_INCREMENT },
58 { ACTION_USB_HID_MULTIMEDIA_VOLUME_DOWN, HID_CONSUMER_USAGE_VOLUME_DECREMENT },
59 { ACTION_USB_HID_MULTIMEDIA_VOLUME_MUTE, HID_CONSUMER_USAGE_MUTE },
60 { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_PLAY_PAUSE, HID_CONSUMER_USAGE_PLAY_PAUSE },
61 { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_STOP, HID_CONSUMER_USAGE_STOP },
62 { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_TRACK_PREV, HID_CONSUMER_USAGE_SCAN_PREVIOUS_TRACK },
63 { ACTION_USB_HID_MULTIMEDIA_PLAYBACK_TRACK_NEXT, HID_CONSUMER_USAGE_SCAN_NEXT_TRACK },
64 { 0, 0 },
68 static const hid_key_mapping_t hid_key_mapping_presentation = {
69 LANG_PRESENTATION_MODE,
70 CONTEXT_USB_HID_MODE_PRESENTATION,
71 HID_USAGE_PAGE_KEYBOARD_KEYPAD,
73 { ACTION_USB_HID_PRESENTATION_SLIDESHOW_START, HID_KEYBOARD_F5 },
74 { ACTION_USB_HID_PRESENTATION_SLIDESHOW_LEAVE, HID_KEYBOARD_ESCAPE },
75 { ACTION_USB_HID_PRESENTATION_SLIDE_PREV, HID_KEYBOARD_P },
76 { ACTION_USB_HID_PRESENTATION_SLIDE_NEXT, HID_KEYBOARD_N },
77 { ACTION_USB_HID_PRESENTATION_SLIDE_FIRST, HID_KEYBOARD_HOME },
78 { ACTION_USB_HID_PRESENTATION_SLIDE_LAST, HID_KEYBOARD_END },
79 { ACTION_USB_HID_PRESENTATION_SCREEN_BLACK, HID_KEYBOARD_DOT },
80 { ACTION_USB_HID_PRESENTATION_SCREEN_WHITE, HID_KEYBOARD_COMMA },
81 { ACTION_USB_HID_PRESENTATION_LINK_PREV, SHIFT(HID_KEYBOARD_TAB) },
82 { ACTION_USB_HID_PRESENTATION_LINK_NEXT, HID_KEYBOARD_TAB },
83 { ACTION_USB_HID_PRESENTATION_MOUSE_CLICK, HID_KEYBOARD_RETURN },
84 { ACTION_USB_HID_PRESENTATION_MOUSE_OVER, SHIFT(HID_KEYBOARD_RETURN) },
85 { 0, 0 },
89 static const hid_key_mapping_t hid_key_mapping_browser = {
90 LANG_BROWSER_MODE,
91 CONTEXT_USB_HID_MODE_BROWSER,
92 HID_USAGE_PAGE_KEYBOARD_KEYPAD,
94 { ACTION_USB_HID_BROWSER_SCROLL_UP, HID_KEYBOARD_UP_ARROW },
95 { ACTION_USB_HID_BROWSER_SCROLL_DOWN, HID_KEYBOARD_DOWN_ARROW },
96 { ACTION_USB_HID_BROWSER_SCROLL_UP, HID_KEYBOARD_PAGE_UP },
97 { ACTION_USB_HID_BROWSER_SCROLL_DOWN, HID_KEYBOARD_PAGE_DOWN },
98 { ACTION_USB_HID_BROWSER_ZOOM_IN, CTRL(HID_KEYPAD_PLUS) },
99 { ACTION_USB_HID_BROWSER_ZOOM_OUT, CTRL(HID_KEYPAD_MINUS) },
100 { ACTION_USB_HID_BROWSER_ZOOM_RESET, CTRL(HID_KEYPAD_0_AND_INSERT) },
101 { ACTION_USB_HID_BROWSER_TAB_PREV, CTRL(HID_KEYBOARD_PAGE_UP) },
102 { ACTION_USB_HID_BROWSER_TAB_NEXT, CTRL(HID_KEYBOARD_PAGE_DOWN) },
103 { ACTION_USB_HID_BROWSER_TAB_CLOSE, CTRL(HID_KEYBOARD_W) },
104 { ACTION_USB_HID_BROWSER_HISTORY_BACK, ALT(HID_KEYBOARD_LEFT_ARROW) },
105 { ACTION_USB_HID_BROWSER_HISTORY_FORWARD, ALT(HID_KEYBOARD_RIGHT_ARROW) },
106 { ACTION_USB_HID_BROWSER_VIEW_FULL_SCREEN, HID_KEYBOARD_F11 },
107 { 0, 0 },
111 #ifdef HAVE_USB_HID_MOUSE
112 static const hid_key_mapping_t hid_key_mapping_mouse = {
113 LANG_MOUSE_MODE,
114 CONTEXT_USB_HID_MODE_MOUSE,
115 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS,
117 /* Cursor move up */
118 { ACTION_USB_HID_MOUSE_UP, HID_MOUSE_UP },
119 { ACTION_USB_HID_MOUSE_UP_REP, HID_MOUSE_UP_REP },
120 { ACTION_USB_HID_MOUSE_LDRAG_UP, HID_MOUSE_LDRAG_UP },
121 { ACTION_USB_HID_MOUSE_LDRAG_UP_REP, HID_MOUSE_LDRAG_UP_REP },
122 { ACTION_USB_HID_MOUSE_RDRAG_UP, HID_MOUSE_RDRAG_UP },
123 { ACTION_USB_HID_MOUSE_RDRAG_UP_REP, HID_MOUSE_RDRAG_UP_REP },
124 /* Cursor move down */
125 { ACTION_USB_HID_MOUSE_DOWN, HID_MOUSE_DOWN },
126 { ACTION_USB_HID_MOUSE_DOWN_REP, HID_MOUSE_DOWN_REP },
127 { ACTION_USB_HID_MOUSE_LDRAG_DOWN, HID_MOUSE_LDRAG_DOWN },
128 { ACTION_USB_HID_MOUSE_LDRAG_DOWN_REP, HID_MOUSE_LDRAG_DOWN_REP },
129 { ACTION_USB_HID_MOUSE_RDRAG_DOWN, HID_MOUSE_RDRAG_DOWN },
130 { ACTION_USB_HID_MOUSE_RDRAG_DOWN_REP, HID_MOUSE_RDRAG_DOWN_REP },
131 /* Cursor move left */
132 { ACTION_USB_HID_MOUSE_LEFT, HID_MOUSE_LEFT },
133 { ACTION_USB_HID_MOUSE_LEFT_REP, HID_MOUSE_LEFT_REP },
134 { ACTION_USB_HID_MOUSE_LDRAG_LEFT, HID_MOUSE_LDRAG_LEFT },
135 { ACTION_USB_HID_MOUSE_LDRAG_LEFT_REP, HID_MOUSE_LDRAG_LEFT_REP },
136 { ACTION_USB_HID_MOUSE_RDRAG_LEFT, HID_MOUSE_RDRAG_LEFT },
137 { ACTION_USB_HID_MOUSE_RDRAG_LEFT_REP, HID_MOUSE_RDRAG_LEFT_REP },
138 /* Cursor move right */
139 { ACTION_USB_HID_MOUSE_RIGHT, HID_MOUSE_RIGHT },
140 { ACTION_USB_HID_MOUSE_RIGHT_REP, HID_MOUSE_RIGHT_REP },
141 { ACTION_USB_HID_MOUSE_LDRAG_RIGHT, HID_MOUSE_LDRAG_RIGHT },
142 { ACTION_USB_HID_MOUSE_LDRAG_RIGHT_REP, HID_MOUSE_LDRAG_RIGHT_REP },
143 { ACTION_USB_HID_MOUSE_RDRAG_RIGHT, HID_MOUSE_RDRAG_RIGHT },
144 { ACTION_USB_HID_MOUSE_RDRAG_RIGHT_REP, HID_MOUSE_RDRAG_RIGHT_REP },
145 /* Mouse button left-click */
146 { ACTION_USB_HID_MOUSE_BUTTON_LEFT, HID_MOUSE_BUTTON_LEFT },
147 { ACTION_USB_HID_MOUSE_BUTTON_LEFT_REL, HID_MOUSE_BUTTON_LEFT_REL },
148 /* Mouse button right-click */
149 { ACTION_USB_HID_MOUSE_BUTTON_RIGHT, HID_MOUSE_BUTTON_RIGHT },
150 { ACTION_USB_HID_MOUSE_BUTTON_RIGHT_REL, HID_MOUSE_BUTTON_RIGHT_REL },
151 /* Mouse wheel scroll up */
152 { ACTION_USB_HID_MOUSE_WHEEL_SCROLL_UP, HID_MOUSE_SCROLL_UP },
153 /* Mouse wheel scroll down */
154 { ACTION_USB_HID_MOUSE_WHEEL_SCROLL_DOWN, HID_MOUSE_SCROLL_DOWN },
155 { 0, 0 },
159 #define USB_KEYPAD_MODE_MOUSE 3 /* Value of the mouse setting (hard-coded) */
160 #endif /* HAVE_USB_HID_MOUSE */
162 static const hid_key_mapping_t *hid_key_mappings[] =
164 &hid_key_mapping_multimedia,
165 &hid_key_mapping_presentation,
166 &hid_key_mapping_browser,
167 #ifdef HAVE_USB_HID_MOUSE
168 &hid_key_mapping_mouse,
169 #endif
172 extern int usb_keypad_mode;
174 int get_hid_usb_action(void)
176 int action, step;
177 const hid_key_mapping_t *key_mapping = hid_key_mappings[usb_keypad_mode];
179 step = -1;
180 action = get_action(key_mapping->context, HZ/4);
181 switch (action)
183 case ACTION_USB_HID_MODE_SWITCH_NEXT:
184 step = 1;
185 case ACTION_USB_HID_MODE_SWITCH_PREV:
186 /* Switch key mappings in a cyclic way */
187 usb_keypad_mode = clamp_value_wrap(usb_keypad_mode + step,
188 ARRAYLEN(hid_key_mappings) - 1, 0);
189 break;
190 default:
192 const mapping_t *mapping;
193 const hid_key_mapping_t *key_mapping =
194 hid_key_mappings[usb_keypad_mode];
196 if (action <= ACTION_USB_HID_FIRST ||
197 action >= ACTION_USB_HID_LAST)
199 break;
202 for (mapping = key_mapping->mapping; mapping->action; mapping++)
204 if (action == mapping->action)
206 logf("Action %d", action);
207 usb_hid_send(key_mapping->usage_page, mapping->id);
208 break;
211 #ifdef DEBUG
212 if (!mapping->action)
213 logf("Action %d not found", action);
214 #endif
215 break;
219 return action;
222 int keypad_mode_name_get(void)
224 return hid_key_mappings[usb_keypad_mode]->lang_name;
227 #endif