1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
26 #include "usbstack/usb_hid.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]))
48 usage_page_t usage_page
;
52 static const hid_key_mapping_t hid_key_mapping_multimedia
= {
54 HID_USAGE_PAGE_CONSUMER
,
57 { ACTION_USB_HID_DEC
, HID_CONSUMER_USAGE_VOLUME_DECREMENT
},
59 { ACTION_USB_HID_INC
, HID_CONSUMER_USAGE_VOLUME_INCREMENT
},
61 { ACTION_USB_HID_SELECT
, HID_CONSUMER_USAGE_MUTE
},
62 /* Playback play / pause */
63 { ACTION_USB_HID_START
, HID_CONSUMER_USAGE_PLAY_PAUSE
},
65 { ACTION_USB_HID_QUIT
, HID_CONSUMER_USAGE_STOP
},
66 /* Scan previous track */
67 { ACTION_USB_HID_LEFT
, HID_CONSUMER_USAGE_SCAN_PREVIOUS_TRACK
},
69 { ACTION_USB_HID_RIGHT
, HID_CONSUMER_USAGE_SCAN_NEXT_TRACK
},
74 static const hid_key_mapping_t hid_key_mapping_presentation
= {
76 HID_USAGE_PAGE_KEYBOARD_KEYPAD
,
79 { ACTION_USB_HID_START
, HID_KEYBOARD_F5
},
81 { ACTION_USB_HID_QUIT
, HID_KEYBOARD_ESCAPE
},
83 { ACTION_USB_HID_LEFT
, HID_KEYBOARD_P
},
85 { ACTION_USB_HID_RIGHT
, HID_KEYBOARD_N
},
87 { ACTION_USB_HID_LEFT_LONG
, HID_KEYBOARD_HOME
},
89 { ACTION_USB_HID_RIGHT_LONG
, HID_KEYBOARD_END
},
91 { ACTION_USB_HID_MENU
, HID_KEYBOARD_DOT
},
93 { ACTION_USB_HID_MENU_LONG
, HID_KEYBOARD_COMMA
},
95 { ACTION_USB_HID_DEC
, SHIFT(HID_KEYBOARD_TAB
) },
97 { ACTION_USB_HID_INC
, HID_KEYBOARD_TAB
},
99 { ACTION_USB_HID_SELECT
, HID_KEYBOARD_RETURN
},
101 { ACTION_USB_HID_SELECT_LONG
, SHIFT(HID_KEYBOARD_RETURN
) },
106 static const hid_key_mapping_t hid_key_mapping_browser
= {
108 HID_USAGE_PAGE_KEYBOARD_KEYPAD
,
111 { ACTION_USB_HID_DEC
, HID_KEYBOARD_UP_ARROW
},
113 { ACTION_USB_HID_INC
, HID_KEYBOARD_DOWN_ARROW
},
115 { ACTION_USB_HID_START
, HID_KEYBOARD_PAGE_UP
},
116 /* Scroll page down */
117 { ACTION_USB_HID_MENU
, HID_KEYBOARD_PAGE_DOWN
},
119 { ACTION_USB_HID_START_LONG
, CTRL(HID_KEYPAD_PLUS
) },
121 { ACTION_USB_HID_MENU_LONG
, CTRL(HID_KEYPAD_MINUS
) },
123 { ACTION_USB_HID_SELECT_LONG
, CTRL(HID_KEYPAD_0_AND_INSERT
) },
125 { ACTION_USB_HID_LEFT
, CTRL(HID_KEYBOARD_PAGE_UP
) },
127 { ACTION_USB_HID_RIGHT
, CTRL(HID_KEYBOARD_PAGE_DOWN
) },
129 { ACTION_USB_HID_QUIT_LONG
, CTRL(HID_KEYBOARD_W
) },
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
},
140 #ifdef HAVE_USB_HID_MOUSE
141 static const hid_key_mapping_t hid_key_mapping_mouse
= {
143 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS
,
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
},
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
,
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
;
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
);
237 if (!mapping
->action
)
238 logf("Action %d not found", action
);
245 int keypad_mode_name_get(void)
247 return hid_key_mappings
[usb_keypad_mode
]->lang_name
;