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 #include <linux/version.h>
35 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
36 typedef unsigned long kernel_ulong_t;
37 #define BITS_PER_LONG (sizeof(long)*8)
40 #include <linux/input.h>
43 #define EV_CNT (EV_MAX+1)
44 #define KEY_CNT (KEY_MAX+1)
45 #define REL_CNT (REL_MAX+1)
46 #define ABS_CNT (ABS_MAX+1)
47 #define LED_CNT (LED_MAX+1)
50 /* compat defines for older kernel like 2.4.x */
55 #define ABS_TOOL_WIDTH 0x1c
56 #define BTN_TOOL_DOUBLETAP 0x14d
57 #define BTN_TOOL_TRIPLETAP 0x14e
61 #define EVIOCGLED(len) _IOC(_IOC_READ, 'E', 0x19, len)
65 #define EVIOCGRAB _IOW('E', 0x90, int)
69 #define XBMC_BUTTON_LEFT 1
70 #define XBMC_BUTTON_MIDDLE 2
71 #define XBMC_BUTTON_RIGHT 3
72 #define XBMC_BUTTON_WHEELUP 4
73 #define XBMC_BUTTON_WHEELDOWN 5
76 #include <linux/keyboard.h>
84 #include <sys/types.h>
86 #include <sys/ioctl.h>
90 #include "guilib/GraphicContext.h"
91 #include "input/XBMC_keysym.h"
92 #include "LinuxInputDevices.h"
93 #include "input/MouseStat.h"
94 #include "utils/log.h"
97 #define BITS_PER_LONG (sizeof(long) * 8)
99 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
100 #define OFF(x) ((x)%BITS_PER_LONG)
101 #define BIT(x) (1UL<<OFF(x))
102 #define LONG(x) ((x)/BITS_PER_LONG)
104 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
106 #define MAX_LINUX_INPUT_DEVICES 16
115 { KEY_ESC , XBMCK_ESCAPE },
126 { KEY_MINUS , XBMCK_MINUS },
127 { KEY_EQUAL , XBMCK_EQUALS },
128 { KEY_BACKSPACE , XBMCK_BACKSPACE },
129 { KEY_TAB , XBMCK_TAB },
140 { KEY_LEFTBRACE , XBMCK_LEFTBRACKET },
141 { KEY_RIGHTBRACE , XBMCK_RIGHTBRACKET},
142 { KEY_ENTER , XBMCK_RETURN },
143 { KEY_LEFTCTRL , XBMCK_LCTRL },
153 { KEY_SEMICOLON , XBMCK_SEMICOLON },
154 { KEY_APOSTROPHE , XBMCK_QUOTE },
155 { KEY_GRAVE , XBMCK_BACKQUOTE },
156 { KEY_LEFTSHIFT , XBMCK_LSHIFT },
157 { KEY_BACKSLASH , XBMCK_BACKSLASH },
165 { KEY_COMMA , XBMCK_COMMA },
166 { KEY_DOT , XBMCK_PERIOD },
167 { KEY_SLASH , XBMCK_SLASH },
168 { KEY_RIGHTSHIFT , XBMCK_RSHIFT },
169 { KEY_KPASTERISK , XBMCK_KP_MULTIPLY },
170 { KEY_LEFTALT , XBMCK_LALT },
171 { KEY_SPACE , XBMCK_SPACE },
172 { KEY_CAPSLOCK , XBMCK_CAPSLOCK },
173 { KEY_F1 , XBMCK_F1 },
174 { KEY_F2 , XBMCK_F2 },
175 { KEY_F3 , XBMCK_F3 },
176 { KEY_F4 , XBMCK_F4 },
177 { KEY_F5 , XBMCK_F5 },
178 { KEY_F6 , XBMCK_F6 },
179 { KEY_F7 , XBMCK_F7 },
180 { KEY_F8 , XBMCK_F8 },
181 { KEY_F9 , XBMCK_F9 },
182 { KEY_F10 , XBMCK_F10 },
183 { KEY_NUMLOCK , XBMCK_NUMLOCK },
184 { KEY_SCROLLLOCK , XBMCK_SCROLLOCK },
185 { KEY_KP7 , XBMCK_KP7 },
186 { KEY_KP8 , XBMCK_KP8 },
187 { KEY_KP9 , XBMCK_KP9 },
188 { KEY_KPMINUS , XBMCK_KP_MINUS },
189 { KEY_KP4 , XBMCK_KP4 },
190 { KEY_KP5 , XBMCK_KP5 },
191 { KEY_KP6 , XBMCK_KP6 },
192 { KEY_KPPLUS , XBMCK_KP_PLUS },
193 { KEY_KP1 , XBMCK_KP1 },
194 { KEY_KP2 , XBMCK_KP2 },
195 { KEY_KP3 , XBMCK_KP3 },
196 { KEY_KP0 , XBMCK_KP0 },
197 { KEY_KPDOT , XBMCK_KP_PERIOD },
198 { 84 , XBMCK_BACKSLASH },
201 { KEY_F11 , XBMCK_F11 },
202 { KEY_F12 , XBMCK_F12 },
205 { KEY_KPENTER , XBMCK_KP_ENTER },
206 { KEY_RIGHTCTRL , XBMCK_RCTRL },
207 { KEY_KPSLASH , XBMCK_KP_DIVIDE },
208 { KEY_SYSRQ , XBMCK_PRINT },
209 { KEY_RIGHTALT , XBMCK_MODE },
210 { KEY_HOME , XBMCK_HOME },
211 { KEY_UP , XBMCK_UP },
212 { KEY_PAGEUP , XBMCK_PAGEUP },
213 { KEY_LEFT , XBMCK_LEFT },
214 { KEY_RIGHT , XBMCK_RIGHT },
215 { KEY_END , XBMCK_END },
216 { KEY_DOWN , XBMCK_DOWN },
217 { KEY_PAGEDOWN , XBMCK_PAGEDOWN },
218 { KEY_INSERT , XBMCK_INSERT },
219 { KEY_DELETE , XBMCK_DELETE },
220 { KEY_MUTE , XBMCK_VOLUME_MUTE },
221 { KEY_VOLUMEDOWN , XBMCK_VOLUME_DOWN },
222 { KEY_VOLUMEUP , XBMCK_VOLUME_UP },
223 { KEY_POWER , XBMCK_POWER },
224 { KEY_KPEQUAL , XBMCK_KP_EQUALS },
225 { KEY_PAUSE , XBMCK_PAUSE },
226 { KEY_LEFTMETA , XBMCK_LMETA },
227 { KEY_RIGHTMETA , XBMCK_RMETA },
228 { KEY_COMPOSE , XBMCK_LSUPER },
229 { KEY_STOP , XBMCK_MEDIA_STOP },
230 { KEY_HELP , XBMCK_HELP },
231 { KEY_CLOSECD , XBMCK_EJECT },
232 { KEY_EJECTCD , XBMCK_EJECT },
233 { KEY_EJECTCLOSECD , XBMCK_EJECT },
234 { KEY_NEXTSONG , XBMCK_MEDIA_NEXT_TRACK},
235 { KEY_PLAYPAUSE , XBMCK_MEDIA_PLAY_PAUSE},
236 { KEY_PREVIOUSSONG , XBMCK_MEDIA_PREV_TRACK},
237 { KEY_STOPCD , XBMCK_MEDIA_STOP },
238 { KEY_RECORD , XBMCK_RECORD },
239 { KEY_REWIND , XBMCK_REWIND },
240 { KEY_PHONE , XBMCK_PHONE },
241 { KEY_REFRESH , XBMCK_SHUFFLE },
242 { KEY_SCROLLUP , XBMCK_PAGEUP },
243 { KEY_SCROLLDOWN , XBMCK_PAGEDOWN },
244 { KEY_PLAY , XBMCK_PLAY },
245 { KEY_FASTFORWARD , XBMCK_FASTFORWARD },
246 { KEY_PRINT , XBMCK_PRINT },
247 { KEY_QUESTION , XBMCK_HELP },
248 // The Little Black Box Remote Additions
249 { 384 , XBMCK_LEFT }, // Red
250 { 378 , XBMCK_RIGHT }, // Green
251 { 381 , XBMCK_UP }, // Yellow
252 { 366 , XBMCK_DOWN }, // Blue
259 LI_DEVICE_JOYSTICK = 2,
260 LI_DEVICE_KEYBOARD = 4,
262 } LinuxInputDeviceType;
269 } LinuxInputCapsType;
271 static char remoteStatus = 0xFF; // paired, battery OK
273 CLinuxInputDevice::CLinuxInputDevice(const std::string fileName, int index)
278 m_fileName = fileName;
279 m_ledState[0] = false;
280 m_ledState[1] = false;
281 m_ledState[2] = false;
284 m_deviceIndex = index;
285 m_keyMods = XBMCKMOD_NONE;
286 m_lastKeyMods = XBMCKMOD_NONE;
287 strcpy(m_deviceName, "");
289 m_devicePreferredId = 0;
291 m_deviceMinKeyCode = 0;
292 m_deviceMaxKeyCode = 0;
294 m_bUnplugged = false;
299 CLinuxInputDevice::~CLinuxInputDevice()
305 * Translates a Linux input keycode into an XBMC keycode.
307 XBMCKey CLinuxInputDevice::TranslateKey(unsigned short code)
309 for (size_t index = 0; index < sizeof(keyMap) / sizeof(KeyMap); index++)
311 if (code == keyMap[index].Key)
312 return keyMap[index].xbmcKey;
315 return XBMCK_UNKNOWN;
318 int CLinuxInputDevice::KeyboardGetSymbol(unsigned short value)
320 unsigned char type = KTYP(value);
321 unsigned char index = KVAL(value);
327 return XBMCK_F1 + index;
336 return XBMCK_BACKSPACE;
338 return XBMCK_EURO; /* euro currency sign */
349 return DIKS_DEAD_GRAVE;
352 return DIKS_DEAD_ACUTE;
355 return DIKS_DEAD_CIRCUMFLEX;
358 return DIKS_DEAD_TILDE;
361 return DIKS_DEAD_DIAERESIS;
364 return DIKS_DEAD_CEDILLA;
372 if (index <= 9 && level != DIKSI_BASE)
373 return (DFBInputDeviceKeySymbol) (DIKS_0 + index);
378 return XBMCK_UNKNOWN;
381 unsigned short CLinuxInputDevice::KeyboardReadValue(unsigned char table, unsigned char index)
383 struct kbentry entry;
385 entry.kb_table = table;
386 entry.kb_index = index;
389 if (ioctl(m_vt_fd, KDGKBENT, &entry))
391 CLog::Log(LOGWARNING, "CLinuxInputDevice::KeyboardReadValue: KDGKBENT (table: %d, index: %d) "
392 "failed!\n", table, index);
396 return entry.kb_value;
399 XBMCMod CLinuxInputDevice::UpdateModifiers(XBMC_Event& devt)
401 XBMCMod modifier = XBMCKMOD_NONE;
402 switch (devt.key.keysym.sym)
404 case XBMCK_LSHIFT: modifier = XBMCKMOD_LSHIFT; break;
405 case XBMCK_RSHIFT: modifier = XBMCKMOD_RSHIFT; break;
406 case XBMCK_LCTRL: modifier = XBMCKMOD_LCTRL; break;
407 case XBMCK_RCTRL: modifier = XBMCKMOD_RCTRL; break;
408 case XBMCK_LALT: modifier = XBMCKMOD_LALT; break;
409 case XBMCK_RALT: modifier = XBMCKMOD_RALT; break;
410 case XBMCK_LMETA: modifier = XBMCKMOD_LMETA; break;
411 case XBMCK_RMETA: modifier = XBMCKMOD_RMETA; break;
415 if (devt.key.type == XBMC_KEYDOWN)
417 m_keyMods |= modifier;
421 m_keyMods &= ~modifier;
424 if (devt.key.type == XBMC_KEYDOWN)
426 modifier = XBMCKMOD_NONE;
427 switch (devt.key.keysym.sym)
429 case XBMCK_NUMLOCK: modifier = XBMCKMOD_NUM; break;
430 case XBMCK_CAPSLOCK: modifier = XBMCKMOD_CAPS; break;
434 if (m_keyMods & modifier)
436 m_keyMods &= ~modifier;
440 m_keyMods |= modifier;
444 return (XBMCMod) m_keyMods;
448 * Translates key and button events.
450 bool CLinuxInputDevice::KeyEvent(const struct input_event& levt, XBMC_Event& devt)
452 int code = levt.code;
454 /* map touchscreen and smartpad events to button mouse */
455 if (code == BTN_TOUCH || code == BTN_TOOL_FINGER)
458 if ((code >= BTN_MOUSE && code < BTN_JOYSTICK) || code == BTN_TOUCH)
460 /* ignore repeat events for buttons */
464 devt.type = levt.value ? XBMC_MOUSEBUTTONDOWN : XBMC_MOUSEBUTTONUP;
465 devt.button.state = levt.value ? XBMC_PRESSED : XBMC_RELEASED;
466 devt.button.type = devt.type;
467 devt.button.x = m_mouseX;
468 devt.button.y = m_mouseY;
473 devt.button.button = XBMC_BUTTON_RIGHT;
477 devt.button.button = XBMC_BUTTON_LEFT;
481 devt.button.button = XBMC_BUTTON_RIGHT;
485 devt.button.button = XBMC_BUTTON_WHEELDOWN;
489 devt.button.button = XBMC_BUTTON_WHEELUP;
493 devt.button.button = XBMC_BUTTON_LEFT;
496 case BTN_TOOL_DOUBLETAP:
497 devt.button.button = XBMC_BUTTON_RIGHT;
501 CLog::Log(LOGWARNING, "CLinuxInputDevice::KeyEvent: Unknown mouse button code: %d\n", levt.code);
507 XBMCKey key = TranslateKey(code);
509 if (key == XBMCK_UNKNOWN)
511 CLog::Log(LOGDEBUG, "CLinuxInputDevice::KeyEvent: TranslateKey returned XBMCK_UNKNOWN from code(%d)", code);
515 devt.type = levt.value ? XBMC_KEYDOWN : XBMC_KEYUP;
516 devt.key.type = devt.type;
517 // warning, key.keysym.scancode is unsigned char so 0 - 255 only
518 devt.key.keysym.scancode = code;
519 devt.key.keysym.sym = key;
520 devt.key.keysym.mod = UpdateModifiers(devt);
521 devt.key.keysym.unicode = 0;
525 if (GetKeymapEntry(entry))
528 if (devt.key.keysym.mod & (XBMCKMOD_SHIFT | XBMCKMOD_CAPS)) keyMapValue = entry.shift;
529 else if (devt.key.keysym.mod & XBMCKMOD_ALT) keyMapValue = entry.alt;
530 else if (devt.key.keysym.mod & XBMCKMOD_META) keyMapValue = entry.altShift;
531 else keyMapValue = entry.base;
533 if (keyMapValue != XBMCK_UNKNOWN)
535 devt.key.keysym.sym = (XBMCKey) keyMapValue;
536 if (keyMapValue > 0 && keyMapValue < 127)
538 devt.key.keysym.unicode = devt.key.keysym.sym;
548 * Translates relative axis events.
550 bool CLinuxInputDevice::RelEvent(const struct input_event& levt, XBMC_Event& devt)
555 m_mouseX += levt.value;
556 devt.motion.xrel = levt.value;
557 devt.motion.yrel = 0;
561 m_mouseY += levt.value;
562 devt.motion.xrel = 0;
563 devt.motion.yrel = levt.value;
569 CLog::Log(LOGWARNING, "CLinuxInputDevice::RelEvent: Unknown rel event code: %d\n", levt.code);
573 // limit the mouse to the screen width
574 m_mouseX = std::min(g_graphicsContext.GetWidth(), m_mouseX);
575 m_mouseX = std::max(0, m_mouseX);
577 // limit the mouse to the screen height
578 m_mouseY = std::min(g_graphicsContext.GetHeight(), m_mouseY);
579 m_mouseY = std::max(0, m_mouseY);
582 devt.type = XBMC_MOUSEMOTION;
583 devt.motion.type = XBMC_MOUSEMOTION;
584 devt.motion.x = m_mouseX;
585 devt.motion.y = m_mouseY;
586 devt.motion.state = 0;
587 devt.motion.which = m_deviceIndex;
594 * Translates absolute axis events.
596 bool CLinuxInputDevice::AbsEvent(const struct input_event& levt, XBMC_Event& devt)
601 m_mouseX = levt.value;
605 m_mouseY = levt.value;
609 remoteStatus = levt.value & 0xFF;
617 devt.type = XBMC_MOUSEMOTION;
618 devt.motion.type = XBMC_MOUSEMOTION;
619 devt.motion.x = m_mouseX;
620 devt.motion.y = m_mouseY;
621 devt.motion.state = 0;
622 devt.motion.xrel = 0;
623 devt.motion.yrel = 0;
624 devt.motion.which = m_deviceIndex;
630 * Translates a Linux input event into a DirectFB input event.
632 bool CLinuxInputDevice::TranslateEvent(const struct input_event& levt,
638 return KeyEvent(levt, devt);
641 if (m_bSkipNonKeyEvents)
643 CLog::Log(LOGINFO, "read a relative event which will be ignored (device name %s) (file name %s)", m_deviceName, m_fileName.c_str());
647 return RelEvent(levt, devt);
650 if (m_bSkipNonKeyEvents)
652 CLog::Log(LOGINFO, "read an absolute event which will be ignored (device name %s) (file name %s)", m_deviceName, m_fileName.c_str());
656 return AbsEvent(levt, devt);
665 void CLinuxInputDevice::SetLed(int led, int state)
667 struct input_event levt;
671 levt.value = !!state;
673 write(m_fd, &levt, sizeof(levt));
677 * Input thread reading from device.
678 * Generates events on incoming data.
680 XBMC_Event CLinuxInputDevice::ReadEvent()
683 struct input_event levt;
689 bzero(&levt, sizeof(levt));
691 bzero(&devt, sizeof(devt));
692 devt.type = XBMC_NOEVENT;
694 if(m_devicePreferredId == LI_DEVICE_NONE)
697 readlen = read(m_fd, &levt, sizeof(levt));
703 CLog::Log(LOGINFO,"input device was unplugged %s",m_deviceName);
710 //printf("read event readlen = %d device name %s m_fileName %s\n", readlen, m_deviceName, m_fileName.c_str());
712 // sanity check if we realy read the event
713 if(readlen != sizeof(levt))
715 printf("CLinuxInputDevice: read error : %s\n", strerror(errno));
719 if (!TranslateEvent(levt, devt))
722 /* Flush previous event with DIEF_FOLLOW? */
723 if (devt.type != XBMC_NOEVENT)
725 //printf("new event! type = %d\n", devt.type);
726 //printf("key: %d %d %d %c\n", devt.key.keysym.scancode, devt.key.keysym.sym, devt.key.keysym.mod, devt.key.keysym.unicode);
728 if (m_hasLeds && (m_keyMods != m_lastKeyMods))
730 SetLed(LED_NUML, m_keyMods & XBMCKMOD_NUM);
731 SetLed(LED_CAPSL, m_keyMods & XBMCKMOD_CAPS);
732 m_lastKeyMods = m_keyMods;
742 void CLinuxInputDevice::SetupKeyboardAutoRepeat(int fd)
746 #if defined(HAS_AMLPLAYER)
747 // ignore the native aml driver named 'key_input',
748 // it is the dedicated power key handler (am_key_input)
749 if (strncmp(m_deviceName, "key_input", strlen("key_input")) == 0)
751 // ignore the native aml driver named 'aml_keypad',
752 // it is the dedicated IR remote handler (amremote)
753 else if (strncmp(m_deviceName, "aml_keypad", strlen("aml_keypad")) == 0)
756 // turn off any keyboard autorepeat, there is a kernel bug
757 // where if the cpu is max'ed then key up is missed and
758 // we get a flood of EV_REP that never stop until next
759 // key down/up. Very nasty when seeking during video playback.
765 int kbdrep[2] = { 400, 80 };
766 ioctl(fd, EVIOCSREP, kbdrep);
770 struct input_event event;
771 memset(&event, 0, sizeof(event));
773 gettimeofday(&event.time, NULL);
775 event.code = REP_DELAY;
777 write(fd, &event, sizeof(event));
779 gettimeofday(&event.time, NULL);
781 event.code = REP_PERIOD;
783 write(fd, &event, sizeof(event));
785 CLog::Log(LOGINFO, "CLinuxInputDevice: auto key repeat disabled on device '%s'\n", m_deviceName);
790 * Fill device information.
791 * Queries the input device and tries to classify it.
793 void CLinuxInputDevice::GetInfo(int fd)
795 unsigned int num_keys = 0;
796 unsigned int num_ext_keys = 0;
797 unsigned int num_buttons = 0;
798 unsigned int num_rels = 0;
799 unsigned int num_abs = 0;
801 unsigned long evbit[NBITS(EV_CNT)];
802 unsigned long keybit[NBITS(KEY_CNT)];
804 /* get device name */
805 bzero(m_deviceName, sizeof(m_deviceName));
806 ioctl(fd, EVIOCGNAME(sizeof(m_deviceName)-1), m_deviceName);
808 if (strncmp(m_deviceName, "D-Link Boxee D-Link Boxee Receiver", strlen("D-Link Boxee D-Link Boxee Receiver")) == 0)
810 m_bSkipNonKeyEvents = true;
814 m_bSkipNonKeyEvents = false;
816 CLog::Log(LOGINFO, "opened device '%s' (file name %s), m_bSkipNonKeyEvents %d\n", m_deviceName, m_fileName.c_str(), m_bSkipNonKeyEvents);
818 /* get event type bits */
819 ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit);
821 if (test_bit( EV_KEY, evbit ))
825 /* get keyboard bits */
826 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit);
828 /** count typical keyboard keys only */
829 for (i = KEY_Q; i <= KEY_M; i++)
830 if (test_bit( i, keybit ))
833 for (i = KEY_OK; i < KEY_CNT; i++)
834 if (test_bit( i, keybit ))
837 for (i = BTN_MOUSE; i < BTN_JOYSTICK; i++)
838 if (test_bit( i, keybit ))
843 unsigned long relbit[NBITS(REL_CNT)];
844 unsigned long absbit[NBITS(ABS_CNT)];
846 if (test_bit( EV_REL, evbit ))
850 /* get bits for relative axes */
851 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit);
853 for (i = 0; i < REL_CNT; i++)
854 if (test_bit( i, relbit ))
858 if (test_bit( EV_ABS, evbit ))
862 /* get bits for absolute axes */
863 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit);
865 for (i = 0; i < ABS_PRESSURE; i++)
866 if (test_bit( i, absbit ))
870 /* Mouse, Touchscreen or Smartpad ? */
871 if ((test_bit( EV_KEY, evbit ) && (test_bit( BTN_TOUCH, keybit )
872 || test_bit( BTN_TOOL_FINGER, keybit ))) || ((num_rels >= 2
873 && num_buttons) || (num_abs == 2 && (num_buttons == 1))))
874 m_deviceType |= LI_DEVICE_MOUSE;
875 else if (num_abs && num_buttons) /* Or a Joystick? */
876 m_deviceType |= LI_DEVICE_JOYSTICK;
879 /* A Keyboard, do we have at least some letters? */
882 m_deviceType |= LI_DEVICE_KEYBOARD;
883 m_deviceCaps |= LI_CAPS_KEYS;
885 m_deviceMinKeyCode = 0;
886 m_deviceMaxKeyCode = 127;
889 /* A Remote Control? */
892 m_deviceType |= LI_DEVICE_REMOTE;
893 m_deviceCaps |= LI_CAPS_KEYS;
899 m_deviceCaps |= LI_CAPS_BUTTONS;
900 m_deviceMaxKeyCode = num_buttons - 1;
904 if (num_rels || num_abs)
906 m_deviceCaps |= LI_CAPS_AXES;
907 m_deviceMaxAxis = std::max(num_rels, num_abs) - 1;
910 /* Decide which primary input device to be. */
911 if (m_deviceType & LI_DEVICE_KEYBOARD)
912 m_devicePreferredId = LI_DEVICE_KEYBOARD;
913 else if (m_deviceType & LI_DEVICE_REMOTE)
914 m_devicePreferredId = LI_DEVICE_REMOTE;
915 else if (m_deviceType & LI_DEVICE_JOYSTICK)
916 m_devicePreferredId = LI_DEVICE_JOYSTICK;
917 else if (m_deviceType & LI_DEVICE_MOUSE)
918 m_devicePreferredId = LI_DEVICE_MOUSE;
920 m_devicePreferredId = LI_DEVICE_NONE;
922 //printf("type: %d\n", m_deviceType);
923 //printf("caps: %d\n", m_deviceCaps);
924 //printf("pref: %d\n", m_devicePreferredId);
927 char* CLinuxInputDevice::GetDeviceName()
932 bool CLinuxInputDevice::IsUnplugged()
937 bool CLinuxInputDevices::CheckDevice(const char *device)
941 /* Check if we are able to open the device */
942 fd = open(device, O_RDWR);
946 if (ioctl(fd, EVIOCGRAB, 1) && errno != EINVAL)
952 ioctl(fd, EVIOCGRAB, 0);
959 /* exported symbols */
962 * Return the number of available devices.
963 * Called once during initialization of DirectFB.
965 void CLinuxInputDevices::InitAvailable()
967 CSingleLock lock(m_devicesListLock);
969 /* Close any devices that may have been initialized previously */
970 for (size_t i = 0; i < m_devices.size(); i++)
978 /* No devices specified. Try to guess some. */
979 for (int i = 0; i < MAX_LINUX_INPUT_DEVICES; i++)
983 snprintf(buf, 32, "/dev/input/event%d", i);
984 if (CheckDevice(buf))
986 CLog::Log(LOGINFO, "Found input device %s", buf);
987 m_devices.push_back(new CLinuxInputDevice(buf, deviceId));
994 * Check for hot plugged devices.
996 void CLinuxInputDevices::CheckHotplugged()
998 CSingleLock lock(m_devicesListLock);
1000 int deviceId = m_devices.size();
1002 /* No devices specified. Try to guess some. */
1003 for (int i = 0; i < MAX_LINUX_INPUT_DEVICES; i++)
1006 bool ispresent = false;
1008 snprintf(buf, 32, "/dev/input/event%d", i);
1010 for (size_t j = 0; j < m_devices.size(); j++)
1012 if (strcmp(m_devices[j]->GetDeviceName(),buf) == 0)
1019 if (!ispresent && CheckDevice(buf))
1021 CLog::Log(LOGINFO, "Found input device %s", buf);
1022 m_devices.push_back(new CLinuxInputDevice(buf, deviceId));
1029 * Open the device, fill out information about it,
1030 * allocate and fill private data, start input thread.
1032 bool CLinuxInputDevice::Open()
1035 unsigned long ledbit[NBITS(LED_CNT)];
1038 fd = open(m_fileName.c_str(), O_RDWR | O_NONBLOCK);
1041 CLog::Log(LOGERROR, "CLinuxInputDevice: could not open device: %s\n", m_fileName.c_str());
1046 ret = ioctl(fd, EVIOCGRAB, 1);
1047 if (ret && errno != EINVAL)
1049 CLog::Log(LOGERROR, "CLinuxInputDevice: could not grab device: %s\n", m_fileName.c_str());
1054 // Set the socket to non-blocking
1056 if ((opts = fcntl(fd, F_GETFL)) < 0)
1058 CLog::Log(LOGERROR, "CLinuxInputDevice %s: fcntl(F_GETFL) failed: %s", __FUNCTION__ , strerror(errno));
1063 opts = (opts | O_NONBLOCK);
1064 if (fcntl(fd, F_SETFL, opts) < 0)
1066 CLog::Log(LOGERROR, "CLinuxInputDevice %s: fcntl(F_SETFL) failed: %s", __FUNCTION__, strerror(errno));
1071 /* fill device info structure */
1074 if (m_deviceType & LI_DEVICE_KEYBOARD)
1075 SetupKeyboardAutoRepeat(fd);
1080 if (m_deviceMinKeyCode >= 0 && m_deviceMaxKeyCode >= m_deviceMinKeyCode)
1083 m_vt_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1086 m_vt_fd = open("/dev/tty1", O_RDWR | O_NOCTTY);
1089 CLog::Log(LOGWARNING, "no keymap support (requires /dev/tty0 - CONFIG_VT)");
1092 /* check if the device has LEDs */
1093 ret = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(ledbit)), ledbit);
1095 CLog::Log(LOGWARNING, "DirectFB/linux_input: could not get LED bits" );
1097 m_hasLeds = test_bit( LED_SCROLLL, ledbit ) || test_bit( LED_NUML, ledbit )
1098 || test_bit( LED_CAPSL, ledbit );
1103 ret = ioctl(fd, EVIOCGLED(sizeof(m_ledState)), m_ledState);
1106 CLog::Log(LOGERROR, "DirectFB/linux_input: could not get LED state");
1107 goto driver_open_device_error;
1111 SetLed(LED_SCROLLL, 0);
1112 SetLed(LED_NUML, 0);
1113 SetLed(LED_CAPSL, 0);
1118 driver_open_device_error:
1120 ioctl(fd, EVIOCGRAB, 0);
1133 * Fetch one entry from the kernel keymap.
1135 bool CLinuxInputDevice::GetKeymapEntry(KeymapEntry& entry)
1137 int code = entry.code;
1138 unsigned short value;
1139 //DFBInputDeviceKeyIdentifier identifier;
1144 // to support '+' and '/' with Boxee's remote control we do something ugly like this for now
1145 if (KVAL(code) == 98)
1147 code = K(KTYP(code),53);
1150 /* fetch the base level */
1151 value = KeyboardGetSymbol(KeyboardReadValue(K_NORMTAB, code));
1152 //printf("base=%d typ=%d code %d\n", KVAL(value), KTYP(value), code);
1154 /* write base level symbol to entry */
1155 entry.base = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_BASE);
1157 /* fetch the shifted base level */
1158 value = KeyboardGetSymbol(KeyboardReadValue(K_SHIFTTAB, entry.code));
1159 //printf("shift=%d\n", value);
1161 /* write shifted base level symbol to entry */
1162 entry.shift = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_SHIFT);
1164 // to support '+' and '/' with Boxee's remote control we could do ugly something like this for now
1165 if (KVAL(code) == 78)
1167 //code = K(KTYP(code),13);
1168 //entry.code = K(KTYP(code),13);
1169 entry.base = K(KTYP(code),43);
1172 /* fetch the alternative level */
1173 value = KeyboardGetSymbol(KeyboardReadValue(K_ALTTAB, entry.code));
1174 //printf("alt=%d\n", value);
1176 /* write alternative level symbol to entry */
1177 entry.alt = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_ALT);
1179 /* fetch the shifted alternative level */
1180 value = KeyboardGetSymbol(KeyboardReadValue(K_ALTSHIFTTAB, entry.code));
1181 //printf("altshift=%d\n", value);
1183 /* write shifted alternative level symbol to entry */
1184 entry.altShift = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_ALT_SHIFT);
1190 * End thread, close device and free private data.
1192 void CLinuxInputDevice::Close()
1194 /* release device */
1195 ioctl(m_fd, EVIOCGRAB, 0);
1204 XBMC_Event CLinuxInputDevices::ReadEvent()
1206 if (m_bReInitialize)
1209 m_bReInitialize = false;
1216 if ((now - m_lastHotplugCheck) >= 10)
1219 m_lastHotplugCheck = now;
1223 CSingleLock lock(m_devicesListLock);
1226 event.type = XBMC_NOEVENT;
1228 for (size_t i = 0; i < m_devices.size(); i++)
1230 event = m_devices[i]->ReadEvent();
1231 if (event.type != XBMC_NOEVENT)
1236 if (m_devices[i]->IsUnplugged())
1238 m_bReInitialize = true;
1247 - 0x7F -> if not paired, battery OK
1248 - 0xFF -> if paired, battery OK
1249 - 0x00 -> if not paired, battery low
1250 - 0x80 -> if paired, battery low
1252 bool CLinuxInputDevices::IsRemoteLowBattery()
1254 bool bLowBattery = !(remoteStatus & 0xF);
1258 bool CLinuxInputDevices::IsRemoteNotPaired()
1260 bool bRemoteNotPaired = !(remoteStatus & 0x70) || !(remoteStatus & 0x80);
1261 return bRemoteNotPaired;
1267 CLinuxInputDevices devices;
1268 devices.InitAvailable();
1271 XBMC_Event event = devices.ReadEvent();
1272 if (event.type != XBMC_NOEVENT)
1274 printf("%d\n", event.type);