2 * Portions copied from DirectFB:
3 * Copyright (C) 2001-2009 The world wide DirectFB Open Source Community (directfb.org)
4 * Copyright (C) 2000-2004 Convergence (integrated media) GmbH
6 * Written by Denis Oliver Kropp <dok@directfb.org>,
7 * Andreas Hundt <andi@fischlustig.de>,
8 * Sven Neumann <neo@directfb.org>,
9 * Ville Syrjälä <syrjala@sci.fi> and
10 * Claudio Ciccani <klan@users.sf.net>.
12 * Copyright (C) 2005-2013 Team XBMC
15 * This Program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
20 * This Program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with XBMC; see the file COPYING. If not, see
27 * <http://www.gnu.org/licenses/>.
31 #if defined(HAS_LINUX_EVENTS)
33 #if defined(HAS_LIBAMCODEC)
34 #include "utils/AMLUtils.h"
37 #include <linux/version.h>
39 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
40 typedef unsigned long kernel_ulong_t;
41 #define BITS_PER_LONG (sizeof(long)*8)
44 #include <linux/input.h>
47 #define EV_CNT (EV_MAX+1)
48 #define KEY_CNT (KEY_MAX+1)
49 #define REL_CNT (REL_MAX+1)
50 #define ABS_CNT (ABS_MAX+1)
51 #define LED_CNT (LED_MAX+1)
54 /* compat defines for older kernel like 2.4.x */
59 #define ABS_TOOL_WIDTH 0x1c
60 #define BTN_TOOL_DOUBLETAP 0x14d
61 #define BTN_TOOL_TRIPLETAP 0x14e
65 #define EVIOCGLED(len) _IOC(_IOC_READ, 'E', 0x19, len)
69 #define EVIOCGRAB _IOW('E', 0x90, int)
73 #define XBMC_BUTTON_LEFT 1
74 #define XBMC_BUTTON_MIDDLE 2
75 #define XBMC_BUTTON_RIGHT 3
76 #define XBMC_BUTTON_WHEELUP 4
77 #define XBMC_BUTTON_WHEELDOWN 5
80 #include <linux/keyboard.h>
88 #include <sys/types.h>
90 #include <sys/ioctl.h>
94 #include "guilib/GraphicContext.h"
95 #include "input/XBMC_keysym.h"
96 #include "LinuxInputDevices.h"
97 #include "input/MouseStat.h"
98 #include "utils/log.h"
100 #ifndef BITS_PER_LONG
101 #define BITS_PER_LONG (sizeof(long) * 8)
103 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
104 #define OFF(x) ((x)%BITS_PER_LONG)
105 #define BIT(x) (1UL<<OFF(x))
106 #define LONG(x) ((x)/BITS_PER_LONG)
108 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
110 #define MAX_LINUX_INPUT_DEVICES 16
119 { KEY_ESC , XBMCK_ESCAPE },
130 { KEY_MINUS , XBMCK_MINUS },
131 { KEY_EQUAL , XBMCK_EQUALS },
132 { KEY_BACKSPACE , XBMCK_BACKSPACE },
133 { KEY_TAB , XBMCK_TAB },
144 { KEY_LEFTBRACE , XBMCK_LEFTBRACKET },
145 { KEY_RIGHTBRACE , XBMCK_RIGHTBRACKET},
146 { KEY_ENTER , XBMCK_RETURN },
147 { KEY_LEFTCTRL , XBMCK_LCTRL },
157 { KEY_SEMICOLON , XBMCK_SEMICOLON },
158 { KEY_APOSTROPHE , XBMCK_QUOTE },
159 { KEY_GRAVE , XBMCK_BACKQUOTE },
160 { KEY_LEFTSHIFT , XBMCK_LSHIFT },
161 { KEY_BACKSLASH , XBMCK_BACKSLASH },
169 { KEY_COMMA , XBMCK_COMMA },
170 { KEY_DOT , XBMCK_PERIOD },
171 { KEY_SLASH , XBMCK_SLASH },
172 { KEY_RIGHTSHIFT , XBMCK_RSHIFT },
173 { KEY_KPASTERISK , XBMCK_KP_MULTIPLY },
174 { KEY_LEFTALT , XBMCK_LALT },
175 { KEY_SPACE , XBMCK_SPACE },
176 { KEY_CAPSLOCK , XBMCK_CAPSLOCK },
177 { KEY_F1 , XBMCK_F1 },
178 { KEY_F2 , XBMCK_F2 },
179 { KEY_F3 , XBMCK_F3 },
180 { KEY_F4 , XBMCK_F4 },
181 { KEY_F5 , XBMCK_F5 },
182 { KEY_F6 , XBMCK_F6 },
183 { KEY_F7 , XBMCK_F7 },
184 { KEY_F8 , XBMCK_F8 },
185 { KEY_F9 , XBMCK_F9 },
186 { KEY_F10 , XBMCK_F10 },
187 { KEY_NUMLOCK , XBMCK_NUMLOCK },
188 { KEY_SCROLLLOCK , XBMCK_SCROLLOCK },
189 { KEY_KP7 , XBMCK_KP7 },
190 { KEY_KP8 , XBMCK_KP8 },
191 { KEY_KP9 , XBMCK_KP9 },
192 { KEY_KPMINUS , XBMCK_KP_MINUS },
193 { KEY_KP4 , XBMCK_KP4 },
194 { KEY_KP5 , XBMCK_KP5 },
195 { KEY_KP6 , XBMCK_KP6 },
196 { KEY_KPPLUS , XBMCK_KP_PLUS },
197 { KEY_KP1 , XBMCK_KP1 },
198 { KEY_KP2 , XBMCK_KP2 },
199 { KEY_KP3 , XBMCK_KP3 },
200 { KEY_KP0 , XBMCK_KP0 },
201 { KEY_KPDOT , XBMCK_KP_PERIOD },
202 { 84 , XBMCK_BACKSLASH },
205 { KEY_F11 , XBMCK_F11 },
206 { KEY_F12 , XBMCK_F12 },
209 { KEY_KPENTER , XBMCK_KP_ENTER },
210 { KEY_RIGHTCTRL , XBMCK_RCTRL },
211 { KEY_KPSLASH , XBMCK_KP_DIVIDE },
212 { KEY_SYSRQ , XBMCK_PRINT },
213 { KEY_RIGHTALT , XBMCK_MODE },
214 { KEY_HOME , XBMCK_HOME },
215 { KEY_UP , XBMCK_UP },
216 { KEY_PAGEUP , XBMCK_PAGEUP },
217 { KEY_LEFT , XBMCK_LEFT },
218 { KEY_RIGHT , XBMCK_RIGHT },
219 { KEY_END , XBMCK_END },
220 { KEY_DOWN , XBMCK_DOWN },
221 { KEY_PAGEDOWN , XBMCK_PAGEDOWN },
222 { KEY_INSERT , XBMCK_INSERT },
223 { KEY_DELETE , XBMCK_DELETE },
224 { KEY_MUTE , XBMCK_VOLUME_MUTE },
225 { KEY_VOLUMEDOWN , XBMCK_VOLUME_DOWN },
226 { KEY_VOLUMEUP , XBMCK_VOLUME_UP },
227 { KEY_POWER , XBMCK_POWER },
228 { KEY_KPEQUAL , XBMCK_KP_EQUALS },
229 { KEY_PAUSE , XBMCK_PAUSE },
230 { KEY_LEFTMETA , XBMCK_LMETA },
231 { KEY_RIGHTMETA , XBMCK_RMETA },
232 { KEY_COMPOSE , XBMCK_LSUPER },
233 { KEY_STOP , XBMCK_MEDIA_STOP },
234 { KEY_HELP , XBMCK_HELP },
235 { KEY_CLOSECD , XBMCK_EJECT },
236 { KEY_EJECTCD , XBMCK_EJECT },
237 { KEY_EJECTCLOSECD , XBMCK_EJECT },
238 { KEY_NEXTSONG , XBMCK_MEDIA_NEXT_TRACK},
239 { KEY_PLAYPAUSE , XBMCK_MEDIA_PLAY_PAUSE},
240 { KEY_PREVIOUSSONG , XBMCK_MEDIA_PREV_TRACK},
241 { KEY_STOPCD , XBMCK_MEDIA_STOP },
242 { KEY_RECORD , XBMCK_RECORD },
243 { KEY_REWIND , XBMCK_REWIND },
244 { KEY_PHONE , XBMCK_PHONE },
245 { KEY_REFRESH , XBMCK_SHUFFLE },
246 { KEY_SCROLLUP , XBMCK_PAGEUP },
247 { KEY_SCROLLDOWN , XBMCK_PAGEDOWN },
248 { KEY_PLAY , XBMCK_PLAY },
249 { KEY_FASTFORWARD , XBMCK_FASTFORWARD },
250 { KEY_PRINT , XBMCK_PRINT },
251 { KEY_QUESTION , XBMCK_HELP },
252 // The Little Black Box Remote Additions
253 { 384 , XBMCK_LEFT }, // Red
254 { 378 , XBMCK_RIGHT }, // Green
255 { 381 , XBMCK_UP }, // Yellow
256 { 366 , XBMCK_DOWN }, // Blue
263 LI_DEVICE_JOYSTICK = 2,
264 LI_DEVICE_KEYBOARD = 4,
266 } LinuxInputDeviceType;
273 } LinuxInputCapsType;
275 static char remoteStatus = 0xFF; // paired, battery OK
277 CLinuxInputDevice::CLinuxInputDevice(const std::string fileName, int index)
282 m_fileName = fileName;
283 m_ledState[0] = false;
284 m_ledState[1] = false;
285 m_ledState[2] = false;
288 m_deviceIndex = index;
289 m_keyMods = XBMCKMOD_NONE;
290 m_lastKeyMods = XBMCKMOD_NONE;
291 strcpy(m_deviceName, "");
293 m_devicePreferredId = 0;
295 m_deviceMinKeyCode = 0;
296 m_deviceMaxKeyCode = 0;
298 m_bUnplugged = false;
303 CLinuxInputDevice::~CLinuxInputDevice()
309 * Translates a Linux input keycode into an XBMC keycode.
311 XBMCKey CLinuxInputDevice::TranslateKey(unsigned short code)
313 for (size_t index = 0; index < sizeof(keyMap) / sizeof(KeyMap); index++)
315 if (code == keyMap[index].Key)
316 return keyMap[index].xbmcKey;
319 return XBMCK_UNKNOWN;
322 int CLinuxInputDevice::KeyboardGetSymbol(unsigned short value)
324 unsigned char type = KTYP(value);
325 unsigned char index = KVAL(value);
331 return XBMCK_F1 + index;
340 return XBMCK_BACKSPACE;
342 return XBMCK_EURO; /* euro currency sign */
353 return DIKS_DEAD_GRAVE;
356 return DIKS_DEAD_ACUTE;
359 return DIKS_DEAD_CIRCUMFLEX;
362 return DIKS_DEAD_TILDE;
365 return DIKS_DEAD_DIAERESIS;
368 return DIKS_DEAD_CEDILLA;
376 if (index <= 9 && level != DIKSI_BASE)
377 return (DFBInputDeviceKeySymbol) (DIKS_0 + index);
382 return XBMCK_UNKNOWN;
385 unsigned short CLinuxInputDevice::KeyboardReadValue(unsigned char table, unsigned char index)
387 struct kbentry entry;
389 entry.kb_table = table;
390 entry.kb_index = index;
393 if (ioctl(m_vt_fd, KDGKBENT, &entry))
395 CLog::Log(LOGWARNING, "CLinuxInputDevice::KeyboardReadValue: KDGKBENT (table: %d, index: %d) "
396 "failed!\n", table, index);
400 return entry.kb_value;
403 XBMCMod CLinuxInputDevice::UpdateModifiers(XBMC_Event& devt)
405 XBMCMod modifier = XBMCKMOD_NONE;
406 switch (devt.key.keysym.sym)
408 case XBMCK_LSHIFT: modifier = XBMCKMOD_LSHIFT; break;
409 case XBMCK_RSHIFT: modifier = XBMCKMOD_RSHIFT; break;
410 case XBMCK_LCTRL: modifier = XBMCKMOD_LCTRL; break;
411 case XBMCK_RCTRL: modifier = XBMCKMOD_RCTRL; break;
412 case XBMCK_LALT: modifier = XBMCKMOD_LALT; break;
413 case XBMCK_RALT: modifier = XBMCKMOD_RALT; break;
414 case XBMCK_LMETA: modifier = XBMCKMOD_LMETA; break;
415 case XBMCK_RMETA: modifier = XBMCKMOD_RMETA; break;
419 if (devt.key.type == XBMC_KEYDOWN)
421 m_keyMods |= modifier;
425 m_keyMods &= ~modifier;
428 if (devt.key.type == XBMC_KEYDOWN)
430 modifier = XBMCKMOD_NONE;
431 switch (devt.key.keysym.sym)
433 case XBMCK_NUMLOCK: modifier = XBMCKMOD_NUM; break;
434 case XBMCK_CAPSLOCK: modifier = XBMCKMOD_CAPS; break;
438 if (m_keyMods & modifier)
440 m_keyMods &= ~modifier;
444 m_keyMods |= modifier;
448 return (XBMCMod) m_keyMods;
452 * Translates key and button events.
454 bool CLinuxInputDevice::KeyEvent(const struct input_event& levt, XBMC_Event& devt)
456 int code = levt.code;
458 /* map touchscreen and smartpad events to button mouse */
459 if (code == BTN_TOUCH || code == BTN_TOOL_FINGER)
462 if ((code >= BTN_MOUSE && code < BTN_JOYSTICK) || code == BTN_TOUCH)
464 /* ignore repeat events for buttons */
468 devt.type = levt.value ? XBMC_MOUSEBUTTONDOWN : XBMC_MOUSEBUTTONUP;
469 devt.button.state = levt.value ? XBMC_PRESSED : XBMC_RELEASED;
470 devt.button.type = devt.type;
471 devt.button.x = m_mouseX;
472 devt.button.y = m_mouseY;
477 devt.button.button = XBMC_BUTTON_RIGHT;
481 devt.button.button = XBMC_BUTTON_LEFT;
485 devt.button.button = XBMC_BUTTON_RIGHT;
489 devt.button.button = XBMC_BUTTON_WHEELDOWN;
493 devt.button.button = XBMC_BUTTON_WHEELUP;
497 devt.button.button = XBMC_BUTTON_LEFT;
500 case BTN_TOOL_DOUBLETAP:
501 devt.button.button = XBMC_BUTTON_RIGHT;
505 CLog::Log(LOGWARNING, "CLinuxInputDevice::KeyEvent: Unknown mouse button code: %d\n", levt.code);
511 XBMCKey key = TranslateKey(code);
513 if (key == XBMCK_UNKNOWN)
515 CLog::Log(LOGDEBUG, "CLinuxInputDevice::KeyEvent: TranslateKey returned XBMCK_UNKNOWN from code(%d)", code);
519 devt.type = levt.value ? XBMC_KEYDOWN : XBMC_KEYUP;
520 devt.key.type = devt.type;
521 // warning, key.keysym.scancode is unsigned char so 0 - 255 only
522 devt.key.keysym.scancode = code;
523 devt.key.keysym.sym = key;
524 devt.key.keysym.mod = UpdateModifiers(devt);
525 devt.key.keysym.unicode = 0;
529 if (GetKeymapEntry(entry))
532 if (devt.key.keysym.mod & (XBMCKMOD_SHIFT | XBMCKMOD_CAPS)) keyMapValue = entry.shift;
533 else if (devt.key.keysym.mod & XBMCKMOD_ALT) keyMapValue = entry.alt;
534 else if (devt.key.keysym.mod & XBMCKMOD_META) keyMapValue = entry.altShift;
535 else keyMapValue = entry.base;
537 if (keyMapValue != XBMCK_UNKNOWN)
539 devt.key.keysym.sym = (XBMCKey) keyMapValue;
540 if (keyMapValue > 0 && keyMapValue < 127)
542 devt.key.keysym.unicode = devt.key.keysym.sym;
552 * Translates relative axis events.
554 bool CLinuxInputDevice::RelEvent(const struct input_event& levt, XBMC_Event& devt)
559 m_mouseX += levt.value;
560 devt.motion.xrel = levt.value;
561 devt.motion.yrel = 0;
565 m_mouseY += levt.value;
566 devt.motion.xrel = 0;
567 devt.motion.yrel = levt.value;
573 CLog::Log(LOGWARNING, "CLinuxInputDevice::RelEvent: Unknown rel event code: %d\n", levt.code);
577 // limit the mouse to the screen width
578 m_mouseX = std::min(g_graphicsContext.GetWidth(), m_mouseX);
579 m_mouseX = std::max(0, m_mouseX);
581 // limit the mouse to the screen height
582 m_mouseY = std::min(g_graphicsContext.GetHeight(), m_mouseY);
583 m_mouseY = std::max(0, m_mouseY);
586 devt.type = XBMC_MOUSEMOTION;
587 devt.motion.type = XBMC_MOUSEMOTION;
588 devt.motion.x = m_mouseX;
589 devt.motion.y = m_mouseY;
590 devt.motion.state = 0;
591 devt.motion.which = m_deviceIndex;
598 * Translates absolute axis events.
600 bool CLinuxInputDevice::AbsEvent(const struct input_event& levt, XBMC_Event& devt)
605 m_mouseX = levt.value;
609 m_mouseY = levt.value;
613 remoteStatus = levt.value & 0xFF;
621 devt.type = XBMC_MOUSEMOTION;
622 devt.motion.type = XBMC_MOUSEMOTION;
623 devt.motion.x = m_mouseX;
624 devt.motion.y = m_mouseY;
625 devt.motion.state = 0;
626 devt.motion.xrel = 0;
627 devt.motion.yrel = 0;
628 devt.motion.which = m_deviceIndex;
634 * Translates a Linux input event into a DirectFB input event.
636 bool CLinuxInputDevice::TranslateEvent(const struct input_event& levt,
642 return KeyEvent(levt, devt);
645 if (m_bSkipNonKeyEvents)
647 CLog::Log(LOGINFO, "read a relative event which will be ignored (device name %s) (file name %s)", m_deviceName, m_fileName.c_str());
651 return RelEvent(levt, devt);
654 if (m_bSkipNonKeyEvents)
656 CLog::Log(LOGINFO, "read an absolute event which will be ignored (device name %s) (file name %s)", m_deviceName, m_fileName.c_str());
660 return AbsEvent(levt, devt);
669 void CLinuxInputDevice::SetLed(int led, int state)
671 struct input_event levt;
675 levt.value = !!state;
677 write(m_fd, &levt, sizeof(levt));
681 * Input thread reading from device.
682 * Generates events on incoming data.
684 XBMC_Event CLinuxInputDevice::ReadEvent()
687 struct input_event levt;
693 bzero(&levt, sizeof(levt));
695 bzero(&devt, sizeof(devt));
696 devt.type = XBMC_NOEVENT;
698 if(m_devicePreferredId == LI_DEVICE_NONE)
701 readlen = read(m_fd, &levt, sizeof(levt));
707 CLog::Log(LOGINFO,"input device was unplugged %s",m_deviceName);
714 //printf("read event readlen = %d device name %s m_fileName %s\n", readlen, m_deviceName, m_fileName.c_str());
716 // sanity check if we realy read the event
717 if(readlen != sizeof(levt))
719 printf("CLinuxInputDevice: read error : %s\n", strerror(errno));
723 if (!TranslateEvent(levt, devt))
726 /* Flush previous event with DIEF_FOLLOW? */
727 if (devt.type != XBMC_NOEVENT)
729 //printf("new event! type = %d\n", devt.type);
730 //printf("key: %d %d %d %c\n", devt.key.keysym.scancode, devt.key.keysym.sym, devt.key.keysym.mod, devt.key.keysym.unicode);
732 if (m_hasLeds && (m_keyMods != m_lastKeyMods))
734 SetLed(LED_NUML, m_keyMods & XBMCKMOD_NUM);
735 SetLed(LED_CAPSL, m_keyMods & XBMCKMOD_CAPS);
736 m_lastKeyMods = m_keyMods;
746 void CLinuxInputDevice::SetupKeyboardAutoRepeat(int fd)
750 #if defined(HAS_LIBAMCODEC)
753 // ignore the native aml driver named 'key_input',
754 // it is the dedicated power key handler (am_key_input)
755 if (strncmp(m_deviceName, "key_input", strlen("key_input")) == 0)
757 // ignore the native aml driver named 'aml_keypad',
758 // it is the dedicated IR remote handler (amremote)
759 else if (strncmp(m_deviceName, "aml_keypad", strlen("aml_keypad")) == 0)
762 // turn off any keyboard autorepeat, there is a kernel bug
763 // where if the cpu is max'ed then key up is missed and
764 // we get a flood of EV_REP that never stop until next
765 // key down/up. Very nasty when seeking during video playback.
772 int kbdrep[2] = { 400, 80 };
773 ioctl(fd, EVIOCSREP, kbdrep);
777 struct input_event event;
778 memset(&event, 0, sizeof(event));
780 gettimeofday(&event.time, NULL);
782 event.code = REP_DELAY;
784 write(fd, &event, sizeof(event));
786 gettimeofday(&event.time, NULL);
788 event.code = REP_PERIOD;
790 write(fd, &event, sizeof(event));
792 CLog::Log(LOGINFO, "CLinuxInputDevice: auto key repeat disabled on device '%s'\n", m_deviceName);
797 * Fill device information.
798 * Queries the input device and tries to classify it.
800 void CLinuxInputDevice::GetInfo(int fd)
802 unsigned int num_keys = 0;
803 unsigned int num_ext_keys = 0;
804 unsigned int num_buttons = 0;
805 unsigned int num_rels = 0;
806 unsigned int num_abs = 0;
808 unsigned long evbit[NBITS(EV_CNT)];
809 unsigned long keybit[NBITS(KEY_CNT)];
811 /* get device name */
812 bzero(m_deviceName, sizeof(m_deviceName));
813 ioctl(fd, EVIOCGNAME(sizeof(m_deviceName)-1), m_deviceName);
815 if (strncmp(m_deviceName, "D-Link Boxee D-Link Boxee Receiver", strlen("D-Link Boxee D-Link Boxee Receiver")) == 0)
817 m_bSkipNonKeyEvents = true;
821 m_bSkipNonKeyEvents = false;
823 CLog::Log(LOGINFO, "opened device '%s' (file name %s), m_bSkipNonKeyEvents %d\n", m_deviceName, m_fileName.c_str(), m_bSkipNonKeyEvents);
825 /* get event type bits */
826 ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit);
828 if (test_bit( EV_KEY, evbit ))
832 /* get keyboard bits */
833 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit);
835 /** count typical keyboard keys only */
836 for (i = KEY_Q; i <= KEY_M; i++)
837 if (test_bit( i, keybit ))
840 for (i = KEY_OK; i < KEY_CNT; i++)
841 if (test_bit( i, keybit ))
844 for (i = BTN_MOUSE; i < BTN_JOYSTICK; i++)
845 if (test_bit( i, keybit ))
850 unsigned long relbit[NBITS(REL_CNT)];
851 unsigned long absbit[NBITS(ABS_CNT)];
853 if (test_bit( EV_REL, evbit ))
857 /* get bits for relative axes */
858 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit);
860 for (i = 0; i < REL_CNT; i++)
861 if (test_bit( i, relbit ))
865 if (test_bit( EV_ABS, evbit ))
869 /* get bits for absolute axes */
870 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit);
872 for (i = 0; i < ABS_PRESSURE; i++)
873 if (test_bit( i, absbit ))
877 /* Mouse, Touchscreen or Smartpad ? */
878 if ((test_bit( EV_KEY, evbit ) && (test_bit( BTN_TOUCH, keybit )
879 || test_bit( BTN_TOOL_FINGER, keybit ))) || ((num_rels >= 2
880 && num_buttons) || (num_abs == 2 && (num_buttons == 1))))
881 m_deviceType |= LI_DEVICE_MOUSE;
882 else if (num_abs && num_buttons) /* Or a Joystick? */
883 m_deviceType |= LI_DEVICE_JOYSTICK;
886 /* A Keyboard, do we have at least some letters? */
889 m_deviceType |= LI_DEVICE_KEYBOARD;
890 m_deviceCaps |= LI_CAPS_KEYS;
892 m_deviceMinKeyCode = 0;
893 m_deviceMaxKeyCode = 127;
896 /* A Remote Control? */
899 m_deviceType |= LI_DEVICE_REMOTE;
900 m_deviceCaps |= LI_CAPS_KEYS;
906 m_deviceCaps |= LI_CAPS_BUTTONS;
907 m_deviceMaxKeyCode = num_buttons - 1;
911 if (num_rels || num_abs)
913 m_deviceCaps |= LI_CAPS_AXES;
914 m_deviceMaxAxis = std::max(num_rels, num_abs) - 1;
917 /* Decide which primary input device to be. */
918 if (m_deviceType & LI_DEVICE_KEYBOARD)
919 m_devicePreferredId = LI_DEVICE_KEYBOARD;
920 else if (m_deviceType & LI_DEVICE_REMOTE)
921 m_devicePreferredId = LI_DEVICE_REMOTE;
922 else if (m_deviceType & LI_DEVICE_JOYSTICK)
923 m_devicePreferredId = LI_DEVICE_JOYSTICK;
924 else if (m_deviceType & LI_DEVICE_MOUSE)
925 m_devicePreferredId = LI_DEVICE_MOUSE;
927 m_devicePreferredId = LI_DEVICE_NONE;
929 //printf("type: %d\n", m_deviceType);
930 //printf("caps: %d\n", m_deviceCaps);
931 //printf("pref: %d\n", m_devicePreferredId);
934 const std::string& CLinuxInputDevice::GetFileName()
939 bool CLinuxInputDevice::IsUnplugged()
944 bool CLinuxInputDevices::CheckDevice(const char *device)
948 // Does the device exists?
950 if (stat(device, &buffer) != 0)
953 /* Check if we are able to open the device */
954 fd = open(device, O_RDWR);
958 if (ioctl(fd, EVIOCGRAB, 1) && errno != EINVAL)
964 ioctl(fd, EVIOCGRAB, 0);
971 /* exported symbols */
974 * Return the number of available devices.
975 * Called once during initialization of DirectFB.
977 void CLinuxInputDevices::InitAvailable()
979 CSingleLock lock(m_devicesListLock);
981 /* Close any devices that may have been initialized previously */
982 for (size_t i = 0; i < m_devices.size(); i++)
990 /* No devices specified. Try to guess some. */
991 for (int i = 0; i < MAX_LINUX_INPUT_DEVICES; i++)
995 snprintf(buf, 32, "/dev/input/event%d", i);
996 if (CheckDevice(buf))
998 CLog::Log(LOGINFO, "Found input device %s", buf);
999 m_devices.push_back(new CLinuxInputDevice(buf, deviceId));
1006 * Check for hot plugged devices.
1008 void CLinuxInputDevices::CheckHotplugged()
1010 CSingleLock lock(m_devicesListLock);
1012 int deviceId = m_devices.size();
1014 /* No devices specified. Try to guess some. */
1015 for (int i = 0; i < MAX_LINUX_INPUT_DEVICES; i++)
1018 bool ispresent = false;
1020 snprintf(buf, 32, "/dev/input/event%d", i);
1022 for (size_t j = 0; j < m_devices.size(); j++)
1024 if (m_devices[j]->GetFileName().compare(buf) == 0)
1031 if (!ispresent && CheckDevice(buf))
1033 CLog::Log(LOGINFO, "Found input device %s", buf);
1034 m_devices.push_back(new CLinuxInputDevice(buf, deviceId));
1041 * Open the device, fill out information about it,
1042 * allocate and fill private data, start input thread.
1044 bool CLinuxInputDevice::Open()
1047 unsigned long ledbit[NBITS(LED_CNT)];
1050 fd = open(m_fileName.c_str(), O_RDWR | O_NONBLOCK);
1053 CLog::Log(LOGERROR, "CLinuxInputDevice: could not open device: %s\n", m_fileName.c_str());
1058 ret = ioctl(fd, EVIOCGRAB, 1);
1059 if (ret && errno != EINVAL)
1061 CLog::Log(LOGERROR, "CLinuxInputDevice: could not grab device: %s\n", m_fileName.c_str());
1066 // Set the socket to non-blocking
1068 if ((opts = fcntl(fd, F_GETFL)) < 0)
1070 CLog::Log(LOGERROR, "CLinuxInputDevice %s: fcntl(F_GETFL) failed: %s", __FUNCTION__ , strerror(errno));
1075 opts = (opts | O_NONBLOCK);
1076 if (fcntl(fd, F_SETFL, opts) < 0)
1078 CLog::Log(LOGERROR, "CLinuxInputDevice %s: fcntl(F_SETFL) failed: %s", __FUNCTION__, strerror(errno));
1083 /* fill device info structure */
1086 if (m_deviceType & LI_DEVICE_KEYBOARD)
1087 SetupKeyboardAutoRepeat(fd);
1092 if (m_deviceMinKeyCode >= 0 && m_deviceMaxKeyCode >= m_deviceMinKeyCode)
1095 m_vt_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1098 m_vt_fd = open("/dev/tty1", O_RDWR | O_NOCTTY);
1101 CLog::Log(LOGWARNING, "no keymap support (requires /dev/tty0 - CONFIG_VT)");
1104 /* check if the device has LEDs */
1105 ret = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(ledbit)), ledbit);
1107 CLog::Log(LOGWARNING, "DirectFB/linux_input: could not get LED bits" );
1109 m_hasLeds = test_bit( LED_SCROLLL, ledbit ) || test_bit( LED_NUML, ledbit )
1110 || test_bit( LED_CAPSL, ledbit );
1115 ret = ioctl(fd, EVIOCGLED(sizeof(m_ledState)), m_ledState);
1118 CLog::Log(LOGERROR, "DirectFB/linux_input: could not get LED state");
1119 goto driver_open_device_error;
1123 SetLed(LED_SCROLLL, 0);
1124 SetLed(LED_NUML, 0);
1125 SetLed(LED_CAPSL, 0);
1130 driver_open_device_error:
1132 ioctl(fd, EVIOCGRAB, 0);
1145 * Fetch one entry from the kernel keymap.
1147 bool CLinuxInputDevice::GetKeymapEntry(KeymapEntry& entry)
1149 int code = entry.code;
1150 unsigned short value;
1151 //DFBInputDeviceKeyIdentifier identifier;
1156 // to support '+' and '/' with Boxee's remote control we do something ugly like this for now
1157 if (KVAL(code) == 98)
1159 code = K(KTYP(code),53);
1162 /* fetch the base level */
1163 value = KeyboardGetSymbol(KeyboardReadValue(K_NORMTAB, code));
1164 //printf("base=%d typ=%d code %d\n", KVAL(value), KTYP(value), code);
1166 /* write base level symbol to entry */
1167 entry.base = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_BASE);
1169 /* fetch the shifted base level */
1170 value = KeyboardGetSymbol(KeyboardReadValue(K_SHIFTTAB, entry.code));
1171 //printf("shift=%d\n", value);
1173 /* write shifted base level symbol to entry */
1174 entry.shift = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_SHIFT);
1176 // to support '+' and '/' with Boxee's remote control we could do ugly something like this for now
1177 if (KVAL(code) == 78)
1179 //code = K(KTYP(code),13);
1180 //entry.code = K(KTYP(code),13);
1181 entry.base = K(KTYP(code),43);
1184 /* fetch the alternative level */
1185 value = KeyboardGetSymbol(KeyboardReadValue(K_ALTTAB, entry.code));
1186 //printf("alt=%d\n", value);
1188 /* write alternative level symbol to entry */
1189 entry.alt = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_ALT);
1191 /* fetch the shifted alternative level */
1192 value = KeyboardGetSymbol(KeyboardReadValue(K_ALTSHIFTTAB, entry.code));
1193 //printf("altshift=%d\n", value);
1195 /* write shifted alternative level symbol to entry */
1196 entry.altShift = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_ALT_SHIFT);
1202 * End thread, close device and free private data.
1204 void CLinuxInputDevice::Close()
1206 /* release device */
1207 ioctl(m_fd, EVIOCGRAB, 0);
1216 XBMC_Event CLinuxInputDevices::ReadEvent()
1218 if (m_bReInitialize)
1221 m_bReInitialize = false;
1228 if ((now - m_lastHotplugCheck) >= 10)
1231 m_lastHotplugCheck = now;
1235 CSingleLock lock(m_devicesListLock);
1238 event.type = XBMC_NOEVENT;
1240 for (size_t i = 0; i < m_devices.size(); i++)
1242 event = m_devices[i]->ReadEvent();
1243 if (event.type != XBMC_NOEVENT)
1248 if (m_devices[i]->IsUnplugged())
1250 m_bReInitialize = true;
1259 - 0x7F -> if not paired, battery OK
1260 - 0xFF -> if paired, battery OK
1261 - 0x00 -> if not paired, battery low
1262 - 0x80 -> if paired, battery low
1264 bool CLinuxInputDevices::IsRemoteLowBattery()
1266 bool bLowBattery = !(remoteStatus & 0xF);
1270 bool CLinuxInputDevices::IsRemoteNotPaired()
1272 bool bRemoteNotPaired = !(remoteStatus & 0x70) || !(remoteStatus & 0x80);
1273 return bRemoteNotPaired;
1279 CLinuxInputDevices devices;
1280 devices.InitAvailable();
1283 XBMC_Event event = devices.ReadEvent();
1284 if (event.type != XBMC_NOEVENT)
1286 printf("%d\n", event.type);