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)
107 #define D_ARRAY_SIZE(array) ((int)(sizeof(array) / sizeof((array)[0])))
110 #define MAX_LINUX_INPUT_DEVICES 16
113 XBMCKey basic_keycodes[] = { XBMCK_UNKNOWN, XBMCK_ESCAPE, XBMCK_1, XBMCK_2, XBMCK_3,
114 XBMCK_4, XBMCK_5, XBMCK_6, XBMCK_7, XBMCK_8, XBMCK_9, XBMCK_0, XBMCK_MINUS,
115 XBMCK_EQUALS, XBMCK_BACKSPACE,
117 XBMCK_TAB, XBMCK_q, XBMCK_w, XBMCK_e, XBMCK_r, XBMCK_t, XBMCK_y, XBMCK_u, XBMCK_i,
118 XBMCK_o, XBMCK_p, XBMCK_LEFTBRACKET, XBMCK_RIGHTBRACKET, XBMCK_RETURN,
120 XBMCK_LCTRL, XBMCK_a, XBMCK_s, XBMCK_d, XBMCK_f, XBMCK_g, XBMCK_h, XBMCK_j,
121 XBMCK_k, XBMCK_l, XBMCK_SEMICOLON, XBMCK_QUOTE, XBMCK_BACKQUOTE,
123 XBMCK_LSHIFT, XBMCK_BACKSLASH, XBMCK_z, XBMCK_x, XBMCK_c, XBMCK_v, XBMCK_b,
124 XBMCK_n, XBMCK_m, XBMCK_COMMA, XBMCK_PERIOD, XBMCK_SLASH, XBMCK_RSHIFT,
125 XBMCK_KP_MULTIPLY, XBMCK_LALT, XBMCK_SPACE, XBMCK_CAPSLOCK,
127 XBMCK_F1, XBMCK_F2, XBMCK_F3, XBMCK_F4, XBMCK_F5, XBMCK_F6, XBMCK_F7, XBMCK_F8,
128 XBMCK_F9, XBMCK_F10, XBMCK_NUMLOCK, XBMCK_SCROLLOCK,
130 XBMCK_KP7, XBMCK_KP8, XBMCK_KP9, XBMCK_KP_MINUS, XBMCK_KP4, XBMCK_KP5,
131 XBMCK_KP6, XBMCK_KP_PLUS, XBMCK_KP1, XBMCK_KP2, XBMCK_KP3, XBMCK_KP0,
134 /*KEY_103RD,*/XBMCK_BACKSLASH,
135 /*KEY_F13,*/XBMCK_F13,
136 /*KEY_102ND*/XBMCK_LESS,
138 XBMCK_F11, XBMCK_F12, XBMCK_F14, XBMCK_F15,
139 XBMCK_UNKNOWN,XBMCK_UNKNOWN,XBMCK_UNKNOWN, /*XBMCK_F16, XBMCK_F17, XBMCK_F18,*/
140 XBMCK_UNKNOWN, XBMCK_UNKNOWN, /*XBMCK_F19, XBMCK_F20,*/
142 XBMCK_KP_ENTER, XBMCK_RCTRL, XBMCK_KP_DIVIDE, XBMCK_PRINT, XBMCK_MODE,
144 /*KEY_LINEFEED*/XBMCK_UNKNOWN,
146 XBMCK_HOME, XBMCK_UP, XBMCK_PAGEUP, XBMCK_LEFT, XBMCK_RIGHT, XBMCK_END,
147 XBMCK_DOWN, XBMCK_PAGEDOWN, XBMCK_INSERT, XBMCK_DELETE,
149 /*KEY_MACRO,*/XBMCK_UNKNOWN,
151 XBMCK_VOLUME_MUTE, XBMCK_VOLUME_DOWN, XBMCK_VOLUME_UP, XBMCK_POWER, XBMCK_KP_EQUALS,
153 /*KEY_KPPLUSMINUS,*/XBMCK_UNKNOWN,
155 XBMCK_PAUSE, XBMCK_UNKNOWN, XBMCK_UNKNOWN, /*DFB_FUNCTION_KEY(21), DFB_FUNCTION_KEY(22), */
156 XBMCK_UNKNOWN, XBMCK_UNKNOWN, /*DFB_FUNCTION_KEY(23), DFB_FUNCTION_KEY(24),*/
158 /*DIKI_KP_SEPARATOR*/XBMCK_UNKNOWN, XBMCK_LMETA, XBMCK_RMETA, XBMCK_LSUPER,
162 /*DIKS_AGAIN, DIKS_PROPS, DIKS_UNDO, DIKS_FRONT, DIKS_COPY,
163 DIKS_OPEN, DIKS_PASTE, DIKS_FIND, DIKS_CUT,*/
164 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
165 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
169 /* DIKS_MENU, DIKS_CALCULATOR, DIKS_SETUP, */
170 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
172 /*KEY_SLEEP, KEY_WAKEUP, KEY_FILE, KEY_SENDFILE, KEY_DELETEFILE,
174 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
177 /*KEY_PROG1, KEY_PROG2,*/
178 XBMCK_UNKNOWN, XBMCK_UNKNOWN,
180 /*DIKS_INTERNET*/ XBMCK_UNKNOWN,
182 /*KEY_MSDOS, KEY_COFFEE, KEY_DIRECTION, KEY_CYCLEWINDOWS,*/
183 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
185 /*DIKS_MAIL*/ XBMCK_UNKNOWN,
187 /*KEY_BOOKMARKS, KEY_COMPUTER, */
188 XBMCK_UNKNOWN, XBMCK_UNKNOWN,
190 /*DIKS_BACK, DIKS_FORWARD,*/
191 XBMCK_UNKNOWN, XBMCK_UNKNOWN,
193 /*KEY_CLOSECD, KEY_EJECTCD, KEY_EJECTCLOSECD,*/
194 XBMCK_EJECT, XBMCK_EJECT, XBMCK_EJECT,
196 XBMCK_MEDIA_NEXT_TRACK, XBMCK_MEDIA_PLAY_PAUSE, XBMCK_MEDIA_PREV_TRACK, XBMCK_MEDIA_STOP, XBMCK_RECORD,
197 XBMCK_REWIND, XBMCK_PHONE,
199 /*KEY_ISO,*/XBMCK_UNKNOWN,
200 /*KEY_CONFIG,*/XBMCK_UNKNOWN,
201 /*KEY_HOMEPAGE, KEY_REFRESH,*/XBMCK_UNKNOWN, XBMCK_SHUFFLE,
203 /*DIKS_EXIT*/XBMCK_UNKNOWN, /*KEY_MOVE,*/XBMCK_UNKNOWN, /*DIKS_EDITOR*/XBMCK_UNKNOWN,
205 /*KEY_SCROLLUP,*/XBMCK_PAGEUP,
206 /*KEY_SCROLLDOWN,*/XBMCK_PAGEDOWN,
207 /*KEY_KPLEFTPAREN,*/XBMCK_UNKNOWN,
208 /*KEY_KPRIGHTPAREN,*/XBMCK_UNKNOWN,
210 /* unused codes 181-182: */
211 XBMCK_UNKNOWN, XBMCK_UNKNOWN,
214 DFB_FUNCTION_KEY(13), DFB_FUNCTION_KEY(14), DFB_FUNCTION_KEY(15),
215 DFB_FUNCTION_KEY(16), DFB_FUNCTION_KEY(17), DFB_FUNCTION_KEY(18),
216 DFB_FUNCTION_KEY(19), DFB_FUNCTION_KEY(20), DFB_FUNCTION_KEY(21),
217 DFB_FUNCTION_KEY(22), DFB_FUNCTION_KEY(23), DFB_FUNCTION_KEY(24),
219 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
220 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
221 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
222 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
224 /* unused codes 195-199: */
225 XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN, XBMCK_UNKNOWN,
227 /* KEY_PLAYCD, KEY_PAUSECD */
228 XBMCK_PLAY, XBMCK_PAUSE,
230 /*KEY_PROG3, KEY_PROG4,*/
231 XBMCK_UNKNOWN, XBMCK_UNKNOWN,
235 /*KEY_SUSPEND, KEY_CLOSE*/
236 XBMCK_UNKNOWN, XBMCK_UNKNOWN,
241 /* KEY_FASTFORWARD */
250 /* KEY_HP */XBMCK_UNKNOWN,
251 /* KEY_CAMERA */XBMCK_UNKNOWN,
252 /* KEY_SOUND */XBMCK_UNKNOWN,
253 /* KEY_QUESTION */XBMCK_HELP,
254 /* KEY_EMAIL */XBMCK_UNKNOWN,
255 /* KEY_CHAT */XBMCK_UNKNOWN,
256 /* KEY_SEARCH */XBMCK_UNKNOWN,
257 /* KEY_CONNECT */XBMCK_UNKNOWN,
258 /* KEY_FINANCE */XBMCK_UNKNOWN,
259 /* KEY_SPORT */XBMCK_UNKNOWN,
260 /* KEY_SHOP */XBMCK_UNKNOWN,
261 /* KEY_ALTERASE */XBMCK_UNKNOWN,
262 /* KEY_CANCEL */XBMCK_UNKNOWN,
263 /* KEY_BRIGHTNESSDOWN */XBMCK_UNKNOWN,
264 /* KEY_BRIGHTNESSUP */XBMCK_UNKNOWN,
265 /* KEY_MEDIA */XBMCK_UNKNOWN, };
268 In the future we may want it...
271 int ext_keycodes[] = { DIKS_OK, DIKS_SELECT, DIKS_GOTO, DIKS_CLEAR,
272 DIKS_POWER2, DIKS_OPTION, DIKS_INFO, DIKS_TIME, DIKS_VENDOR, DIKS_ARCHIVE,
273 DIKS_PROGRAM, DIKS_CHANNEL, DIKS_FAVORITES, DIKS_EPG, DIKS_PVR, DIKS_MHP,
274 DIKS_LANGUAGE, DIKS_TITLE, DIKS_SUBTITLE, DIKS_ANGLE, DIKS_ZOOM, DIKS_MODE,
275 DIKS_KEYBOARD, DIKS_SCREEN, DIKS_PC, DIKS_TV, DIKS_TV2, DIKS_VCR,
276 DIKS_VCR2, DIKS_SAT, DIKS_SAT2, DIKS_CD, DIKS_TAPE, DIKS_RADIO, DIKS_TUNER,
277 DIKS_PLAYER, DIKS_TEXT, DIKS_DVD, DIKS_AUX, DIKS_MP3, DIKS_AUDIO,
278 DIKS_VIDEO, DIKS_DIRECTORY, DIKS_LIST, DIKS_MEMO, DIKS_CALENDAR, DIKS_RED,
279 DIKS_GREEN, DIKS_YELLOW, DIKS_BLUE, DIKS_CHANNEL_UP, DIKS_CHANNEL_DOWN,
280 DIKS_FIRST, DIKS_LAST, DIKS_AB, DIKS_NEXT, DIKS_RESTART, DIKS_SLOW,
281 DIKS_SHUFFLE, DIKS_FASTFORWARD, DIKS_PREVIOUS, DIKS_NEXT, DIKS_DIGITS,
282 DIKS_TEEN, DIKS_TWEN, DIKS_BREAK };
289 LI_DEVICE_JOYSTICK = 2,
290 LI_DEVICE_KEYBOARD = 4,
292 } LinuxInputDeviceType;
299 } LinuxInputCapsType;
301 static char remoteStatus = 0xFF; // paired, battery OK
303 CLinuxInputDevice::CLinuxInputDevice(const std::string fileName, int index)
308 m_fileName = fileName;
309 m_ledState[0] = false;
310 m_ledState[1] = false;
311 m_ledState[2] = false;
314 m_deviceIndex = index;
315 m_keyMods = XBMCKMOD_NONE;
316 m_lastKeyMods = XBMCKMOD_NONE;
317 strcpy(m_deviceName, "");
319 m_devicePreferredId = 0;
321 m_deviceMinKeyCode = 0;
322 m_deviceMaxKeyCode = 0;
324 m_bUnplugged = false;
329 CLinuxInputDevice::~CLinuxInputDevice()
335 * Translates a Linux input keycode into an XBMC keycode.
337 XBMCKey CLinuxInputDevice::TranslateKey(unsigned short code)
339 if (code < D_ARRAY_SIZE(basic_keycodes))
340 return basic_keycodes[code];
343 In the future we may want it...
346 if (code - KEY_OK < D_ARRAY_SIZE(ext_keycodes))
347 return ext_keycodes[code - KEY_OK];
350 return XBMCK_UNKNOWN;
353 int CLinuxInputDevice::KeyboardGetSymbol(unsigned short value)
355 unsigned char type = KTYP(value);
356 unsigned char index = KVAL(value);
362 return XBMCK_F1 + index;
371 return XBMCK_BACKSPACE;
373 return XBMCK_EURO; /* euro currency sign */
384 return DIKS_DEAD_GRAVE;
387 return DIKS_DEAD_ACUTE;
390 return DIKS_DEAD_CIRCUMFLEX;
393 return DIKS_DEAD_TILDE;
396 return DIKS_DEAD_DIAERESIS;
399 return DIKS_DEAD_CEDILLA;
407 if (index <= 9 && level != DIKSI_BASE)
408 return (DFBInputDeviceKeySymbol) (DIKS_0 + index);
413 return XBMCK_UNKNOWN;
416 unsigned short CLinuxInputDevice::KeyboardReadValue(unsigned char table, unsigned char index)
418 struct kbentry entry;
420 entry.kb_table = table;
421 entry.kb_index = index;
424 if (ioctl(m_vt_fd, KDGKBENT, &entry))
426 CLog::Log(LOGWARNING, "CLinuxInputDevice::KeyboardReadValue: KDGKBENT (table: %d, index: %d) "
427 "failed!\n", table, index);
431 return entry.kb_value;
434 XBMCMod CLinuxInputDevice::UpdateModifiers(XBMC_Event& devt)
436 XBMCMod modifier = XBMCKMOD_NONE;
437 switch (devt.key.keysym.sym)
439 case XBMCK_LSHIFT: modifier = XBMCKMOD_LSHIFT; break;
440 case XBMCK_RSHIFT: modifier = XBMCKMOD_RSHIFT; break;
441 case XBMCK_LCTRL: modifier = XBMCKMOD_LCTRL; break;
442 case XBMCK_RCTRL: modifier = XBMCKMOD_RCTRL; break;
443 case XBMCK_LALT: modifier = XBMCKMOD_LALT; break;
444 case XBMCK_RALT: modifier = XBMCKMOD_RALT; break;
445 case XBMCK_LMETA: modifier = XBMCKMOD_LMETA; break;
446 case XBMCK_RMETA: modifier = XBMCKMOD_RMETA; break;
450 if (devt.key.type == XBMC_KEYDOWN)
452 m_keyMods |= modifier;
456 m_keyMods &= ~modifier;
459 if (devt.key.type == XBMC_KEYDOWN)
461 modifier = XBMCKMOD_NONE;
462 switch (devt.key.keysym.sym)
464 case XBMCK_NUMLOCK: modifier = XBMCKMOD_NUM; break;
465 case XBMCK_CAPSLOCK: modifier = XBMCKMOD_CAPS; break;
469 if (m_keyMods & modifier)
471 m_keyMods &= ~modifier;
475 m_keyMods |= modifier;
479 return (XBMCMod) m_keyMods;
483 * Translates key and button events.
485 bool CLinuxInputDevice::KeyEvent(const struct input_event& levt, XBMC_Event& devt)
487 int code = levt.code;
489 /* map touchscreen and smartpad events to button mouse */
490 if (code == BTN_TOUCH || code == BTN_TOOL_FINGER)
493 if ((code >= BTN_MOUSE && code < BTN_JOYSTICK) || code == BTN_TOUCH)
495 /* ignore repeat events for buttons */
499 devt.type = levt.value ? XBMC_MOUSEBUTTONDOWN : XBMC_MOUSEBUTTONUP;
500 devt.button.state = levt.value ? XBMC_PRESSED : XBMC_RELEASED;
501 devt.button.type = devt.type;
502 devt.button.x = m_mouseX;
503 devt.button.y = m_mouseY;
508 devt.button.button = XBMC_BUTTON_RIGHT;
512 devt.button.button = XBMC_BUTTON_LEFT;
516 devt.button.button = XBMC_BUTTON_RIGHT;
520 devt.button.button = XBMC_BUTTON_WHEELDOWN;
524 devt.button.button = XBMC_BUTTON_WHEELUP;
528 devt.button.button = XBMC_BUTTON_LEFT;
531 case BTN_TOOL_DOUBLETAP:
532 devt.button.button = XBMC_BUTTON_RIGHT;
536 CLog::Log(LOGWARNING, "CLinuxInputDevice::KeyEvent: Unknown mouse button code: %d\n", levt.code);
542 XBMCKey key = TranslateKey(code);
544 if (key == XBMCK_UNKNOWN)
547 devt.type = levt.value ? XBMC_KEYDOWN : XBMC_KEYUP;
548 devt.key.type = devt.type;
549 devt.key.keysym.scancode = code;
550 devt.key.keysym.sym = key;
551 devt.key.keysym.mod = UpdateModifiers(devt);
552 devt.key.keysym.unicode = 0;
556 if (GetKeymapEntry(entry))
559 if (devt.key.keysym.mod & (XBMCKMOD_SHIFT | XBMCKMOD_CAPS)) keyMapValue = entry.shift;
560 else if (devt.key.keysym.mod & XBMCKMOD_ALT) keyMapValue = entry.alt;
561 else if (devt.key.keysym.mod & XBMCKMOD_META) keyMapValue = entry.altShift;
562 else keyMapValue = entry.base;
564 if (keyMapValue != XBMCK_UNKNOWN)
566 devt.key.keysym.sym = (XBMCKey) keyMapValue;
567 if (keyMapValue > 0 && keyMapValue < 127)
569 devt.key.keysym.unicode = devt.key.keysym.sym;
579 * Translates relative axis events.
581 bool CLinuxInputDevice::RelEvent(const struct input_event& levt, XBMC_Event& devt)
586 m_mouseX += levt.value;
587 devt.motion.xrel = levt.value;
588 devt.motion.yrel = 0;
592 m_mouseY += levt.value;
593 devt.motion.xrel = 0;
594 devt.motion.yrel = levt.value;
600 CLog::Log(LOGWARNING, "CLinuxInputDevice::RelEvent: Unknown rel event code: %d\n", levt.code);
604 // limit the mouse to the screen width
605 m_mouseX = std::min(g_graphicsContext.GetWidth(), m_mouseX);
606 m_mouseX = std::max(0, m_mouseX);
608 // limit the mouse to the screen height
609 m_mouseY = std::min(g_graphicsContext.GetHeight(), m_mouseY);
610 m_mouseY = std::max(0, m_mouseY);
613 devt.type = XBMC_MOUSEMOTION;
614 devt.motion.type = XBMC_MOUSEMOTION;
615 devt.motion.x = m_mouseX;
616 devt.motion.y = m_mouseY;
617 devt.motion.state = 0;
618 devt.motion.which = m_deviceIndex;
625 * Translates absolute axis events.
627 bool CLinuxInputDevice::AbsEvent(const struct input_event& levt, XBMC_Event& devt)
632 m_mouseX = levt.value;
636 m_mouseY = levt.value;
640 remoteStatus = levt.value & 0xFF;
648 devt.type = XBMC_MOUSEMOTION;
649 devt.motion.type = XBMC_MOUSEMOTION;
650 devt.motion.x = m_mouseX;
651 devt.motion.y = m_mouseY;
652 devt.motion.state = 0;
653 devt.motion.xrel = 0;
654 devt.motion.yrel = 0;
655 devt.motion.which = m_deviceIndex;
661 * Translates a Linux input event into a DirectFB input event.
663 bool CLinuxInputDevice::TranslateEvent(const struct input_event& levt,
669 return KeyEvent(levt, devt);
672 if (m_bSkipNonKeyEvents)
674 CLog::Log(LOGINFO, "read a relative event which will be ignored (device name %s) (file name %s)", m_deviceName, m_fileName.c_str());
678 return RelEvent(levt, devt);
681 if (m_bSkipNonKeyEvents)
683 CLog::Log(LOGINFO, "read an absolute event which will be ignored (device name %s) (file name %s)", m_deviceName, m_fileName.c_str());
687 return AbsEvent(levt, devt);
696 void CLinuxInputDevice::SetLed(int led, int state)
698 struct input_event levt;
702 levt.value = !!state;
704 write(m_fd, &levt, sizeof(levt));
708 * Input thread reading from device.
709 * Generates events on incoming data.
711 XBMC_Event CLinuxInputDevice::ReadEvent()
714 struct input_event levt;
720 bzero(&levt, sizeof(levt));
722 bzero(&devt, sizeof(devt));
723 devt.type = XBMC_NOEVENT;
725 if(m_devicePreferredId == LI_DEVICE_NONE)
728 readlen = read(m_fd, &levt, sizeof(levt));
734 CLog::Log(LOGINFO,"input device was unplugged %s",m_deviceName);
741 //printf("read event readlen = %d device name %s m_fileName %s\n", readlen, m_deviceName, m_fileName.c_str());
743 // sanity check if we realy read the event
744 if(readlen != sizeof(levt))
746 printf("CLinuxInputDevice: read error : %s\n", strerror(errno));
750 if (!TranslateEvent(levt, devt))
753 /* Flush previous event with DIEF_FOLLOW? */
754 if (devt.type != XBMC_NOEVENT)
756 //printf("new event! type = %d\n", devt.type);
757 //printf("key: %d %d %d %c\n", devt.key.keysym.scancode, devt.key.keysym.sym, devt.key.keysym.mod, devt.key.keysym.unicode);
759 if (m_hasLeds && (m_keyMods != m_lastKeyMods))
761 SetLed(LED_NUML, m_keyMods & XBMCKMOD_NUM);
762 SetLed(LED_CAPSL, m_keyMods & XBMCKMOD_CAPS);
763 m_lastKeyMods = m_keyMods;
773 void CLinuxInputDevice::SetupKeyboardAutoRepeat(int fd)
777 #if defined(HAS_AMLPLAYER)
778 // ignore the native aml driver named 'key_input',
779 // it is the dedicated power key handler (am_key_input)
780 if (strncmp(m_deviceName, "key_input", strlen("key_input")) == 0)
782 // ignore the native aml driver named 'aml_keypad',
783 // it is the dedicated IR remote handler (amremote)
784 else if (strncmp(m_deviceName, "aml_keypad", strlen("aml_keypad")) == 0)
787 // turn off any keyboard autorepeat, there is a kernel bug
788 // where if the cpu is max'ed then key up is missed and
789 // we get a flood of EV_REP that never stop until next
790 // key down/up. Very nasty when seeking during video playback.
796 int kbdrep[2] = { 400, 80 };
797 ioctl(fd, EVIOCSREP, kbdrep);
801 struct input_event event;
802 memset(&event, 0, sizeof(event));
804 gettimeofday(&event.time, NULL);
806 event.code = REP_DELAY;
808 write(fd, &event, sizeof(event));
810 gettimeofday(&event.time, NULL);
812 event.code = REP_PERIOD;
814 write(fd, &event, sizeof(event));
816 CLog::Log(LOGINFO, "CLinuxInputDevice: auto key repeat disabled on device '%s'\n", m_deviceName);
821 * Fill device information.
822 * Queries the input device and tries to classify it.
824 void CLinuxInputDevice::GetInfo(int fd)
826 unsigned int num_keys = 0;
827 unsigned int num_ext_keys = 0;
828 unsigned int num_buttons = 0;
829 unsigned int num_rels = 0;
830 unsigned int num_abs = 0;
832 unsigned long evbit[NBITS(EV_CNT)];
833 unsigned long keybit[NBITS(KEY_CNT)];
835 /* get device name */
836 bzero(m_deviceName, sizeof(m_deviceName));
837 ioctl(fd, EVIOCGNAME(sizeof(m_deviceName)-1), m_deviceName);
839 if (strncmp(m_deviceName, "D-Link Boxee D-Link Boxee Receiver", strlen("D-Link Boxee D-Link Boxee Receiver")) == 0)
841 m_bSkipNonKeyEvents = true;
845 m_bSkipNonKeyEvents = false;
847 CLog::Log(LOGINFO, "opened device '%s' (file name %s), m_bSkipNonKeyEvents %d\n", m_deviceName, m_fileName.c_str(), m_bSkipNonKeyEvents);
849 /* get event type bits */
850 ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit);
852 if (test_bit( EV_KEY, evbit ))
856 /* get keyboard bits */
857 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit);
859 /** count typical keyboard keys only */
860 for (i = KEY_Q; i <= KEY_M; i++)
861 if (test_bit( i, keybit ))
864 for (i = KEY_OK; i < KEY_CNT; i++)
865 if (test_bit( i, keybit ))
868 for (i = BTN_MOUSE; i < BTN_JOYSTICK; i++)
869 if (test_bit( i, keybit ))
874 unsigned long relbit[NBITS(REL_CNT)];
875 unsigned long absbit[NBITS(ABS_CNT)];
877 if (test_bit( EV_REL, evbit ))
881 /* get bits for relative axes */
882 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit);
884 for (i = 0; i < REL_CNT; i++)
885 if (test_bit( i, relbit ))
889 if (test_bit( EV_ABS, evbit ))
893 /* get bits for absolute axes */
894 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit);
896 for (i = 0; i < ABS_PRESSURE; i++)
897 if (test_bit( i, absbit ))
901 /* Mouse, Touchscreen or Smartpad ? */
902 if ((test_bit( EV_KEY, evbit ) && (test_bit( BTN_TOUCH, keybit )
903 || test_bit( BTN_TOOL_FINGER, keybit ))) || ((num_rels >= 2
904 && num_buttons) || (num_abs == 2 && (num_buttons == 1))))
905 m_deviceType |= LI_DEVICE_MOUSE;
906 else if (num_abs && num_buttons) /* Or a Joystick? */
907 m_deviceType |= LI_DEVICE_JOYSTICK;
910 /* A Keyboard, do we have at least some letters? */
913 m_deviceType |= LI_DEVICE_KEYBOARD;
914 m_deviceCaps |= LI_CAPS_KEYS;
916 m_deviceMinKeyCode = 0;
917 m_deviceMaxKeyCode = 127;
920 /* A Remote Control? */
923 m_deviceType |= LI_DEVICE_REMOTE;
924 m_deviceCaps |= LI_CAPS_KEYS;
930 m_deviceCaps |= LI_CAPS_BUTTONS;
931 m_deviceMaxKeyCode = num_buttons - 1;
935 if (num_rels || num_abs)
937 m_deviceCaps |= LI_CAPS_AXES;
938 m_deviceMaxAxis = std::max(num_rels, num_abs) - 1;
941 /* Decide which primary input device to be. */
942 if (m_deviceType & LI_DEVICE_KEYBOARD)
943 m_devicePreferredId = LI_DEVICE_KEYBOARD;
944 else if (m_deviceType & LI_DEVICE_REMOTE)
945 m_devicePreferredId = LI_DEVICE_REMOTE;
946 else if (m_deviceType & LI_DEVICE_JOYSTICK)
947 m_devicePreferredId = LI_DEVICE_JOYSTICK;
948 else if (m_deviceType & LI_DEVICE_MOUSE)
949 m_devicePreferredId = LI_DEVICE_MOUSE;
951 m_devicePreferredId = LI_DEVICE_NONE;
953 //printf("type: %d\n", m_deviceType);
954 //printf("caps: %d\n", m_deviceCaps);
955 //printf("pref: %d\n", m_devicePreferredId);
958 char* CLinuxInputDevice::GetDeviceName()
963 bool CLinuxInputDevice::IsUnplugged()
968 bool CLinuxInputDevices::CheckDevice(const char *device)
972 /* Check if we are able to open the device */
973 fd = open(device, O_RDWR);
977 if (ioctl(fd, EVIOCGRAB, 1) && errno != EINVAL)
983 ioctl(fd, EVIOCGRAB, 0);
990 /* exported symbols */
993 * Return the number of available devices.
994 * Called once during initialization of DirectFB.
996 void CLinuxInputDevices::InitAvailable()
998 CSingleLock lock(m_devicesListLock);
1000 /* Close any devices that may have been initialized previously */
1001 for (size_t i = 0; i < m_devices.size(); i++)
1003 delete m_devices[i];
1009 /* No devices specified. Try to guess some. */
1010 for (int i = 0; i < MAX_LINUX_INPUT_DEVICES; i++)
1014 snprintf(buf, 32, "/dev/input/event%d", i);
1015 if (CheckDevice(buf))
1017 CLog::Log(LOGINFO, "Found input device %s", buf);
1018 m_devices.push_back(new CLinuxInputDevice(buf, deviceId));
1025 * Check for hot plugged devices.
1027 void CLinuxInputDevices::CheckHotplugged()
1029 CSingleLock lock(m_devicesListLock);
1031 int deviceId = m_devices.size();
1033 /* No devices specified. Try to guess some. */
1034 for (int i = 0; i < MAX_LINUX_INPUT_DEVICES; i++)
1037 bool ispresent = false;
1039 snprintf(buf, 32, "/dev/input/event%d", i);
1041 for (size_t j = 0; j < m_devices.size(); j++)
1043 if (strcmp(m_devices[j]->GetDeviceName(),buf) == 0)
1050 if (!ispresent && CheckDevice(buf))
1052 CLog::Log(LOGINFO, "Found input device %s", buf);
1053 m_devices.push_back(new CLinuxInputDevice(buf, deviceId));
1060 * Open the device, fill out information about it,
1061 * allocate and fill private data, start input thread.
1063 bool CLinuxInputDevice::Open()
1066 unsigned long ledbit[NBITS(LED_CNT)];
1069 fd = open(m_fileName.c_str(), O_RDWR | O_NONBLOCK);
1072 CLog::Log(LOGERROR, "CLinuxInputDevice: could not open device: %s\n", m_fileName.c_str());
1077 ret = ioctl(fd, EVIOCGRAB, 1);
1078 if (ret && errno != EINVAL)
1080 CLog::Log(LOGERROR, "CLinuxInputDevice: could not grab device: %s\n", m_fileName.c_str());
1085 // Set the socket to non-blocking
1087 if ((opts = fcntl(fd, F_GETFL)) < 0)
1089 CLog::Log(LOGERROR, "CLinuxInputDevice %s: fcntl(F_GETFL) failed: %s", __FUNCTION__ , strerror(errno));
1094 opts = (opts | O_NONBLOCK);
1095 if (fcntl(fd, F_SETFL, opts) < 0)
1097 CLog::Log(LOGERROR, "CLinuxInputDevice %s: fcntl(F_SETFL) failed: %s", __FUNCTION__, strerror(errno));
1102 /* fill device info structure */
1105 if (m_deviceType & LI_DEVICE_KEYBOARD)
1106 SetupKeyboardAutoRepeat(fd);
1111 if (m_deviceMinKeyCode >= 0 && m_deviceMaxKeyCode >= m_deviceMinKeyCode)
1114 m_vt_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1117 m_vt_fd = open("/dev/tty1", O_RDWR | O_NOCTTY);
1120 CLog::Log(LOGWARNING, "no keymap support (requires /dev/tty0 - CONFIG_VT)");
1123 /* check if the device has LEDs */
1124 ret = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(ledbit)), ledbit);
1126 CLog::Log(LOGWARNING, "DirectFB/linux_input: could not get LED bits" );
1128 m_hasLeds = test_bit( LED_SCROLLL, ledbit ) || test_bit( LED_NUML, ledbit )
1129 || test_bit( LED_CAPSL, ledbit );
1134 ret = ioctl(fd, EVIOCGLED(sizeof(m_ledState)), m_ledState);
1137 CLog::Log(LOGERROR, "DirectFB/linux_input: could not get LED state");
1138 goto driver_open_device_error;
1142 SetLed(LED_SCROLLL, 0);
1143 SetLed(LED_NUML, 0);
1144 SetLed(LED_CAPSL, 0);
1149 driver_open_device_error:
1151 ioctl(fd, EVIOCGRAB, 0);
1164 * Fetch one entry from the kernel keymap.
1166 bool CLinuxInputDevice::GetKeymapEntry(KeymapEntry& entry)
1168 int code = entry.code;
1169 unsigned short value;
1170 //DFBInputDeviceKeyIdentifier identifier;
1175 // to support '+' and '/' with Boxee's remote control we do something ugly like this for now
1176 if (KVAL(code) == 98)
1178 code = K(KTYP(code),53);
1181 /* fetch the base level */
1182 value = KeyboardGetSymbol(KeyboardReadValue(K_NORMTAB, code));
1183 //printf("base=%d typ=%d code %d\n", KVAL(value), KTYP(value), code);
1185 /* write base level symbol to entry */
1186 entry.base = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_BASE);
1188 /* fetch the shifted base level */
1189 value = KeyboardGetSymbol(KeyboardReadValue(K_SHIFTTAB, entry.code));
1190 //printf("shift=%d\n", value);
1192 /* write shifted base level symbol to entry */
1193 entry.shift = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_SHIFT);
1195 // to support '+' and '/' with Boxee's remote control we could do ugly something like this for now
1196 if (KVAL(code) == 78)
1198 //code = K(KTYP(code),13);
1199 //entry.code = K(KTYP(code),13);
1200 entry.base = K(KTYP(code),43);
1203 /* fetch the alternative level */
1204 value = KeyboardGetSymbol(KeyboardReadValue(K_ALTTAB, entry.code));
1205 //printf("alt=%d\n", value);
1207 /* write alternative level symbol to entry */
1208 entry.alt = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_ALT);
1210 /* fetch the shifted alternative level */
1211 value = KeyboardGetSymbol(KeyboardReadValue(K_ALTSHIFTTAB, entry.code));
1212 //printf("altshift=%d\n", value);
1214 /* write shifted alternative level symbol to entry */
1215 entry.altShift = value; //KeyboardGetSymbol(code, value, LI_KEYLEVEL_ALT_SHIFT);
1221 * End thread, close device and free private data.
1223 void CLinuxInputDevice::Close()
1225 /* release device */
1226 ioctl(m_fd, EVIOCGRAB, 0);
1235 XBMC_Event CLinuxInputDevices::ReadEvent()
1237 if (m_bReInitialize)
1240 m_bReInitialize = false;
1247 if ((now - m_lastHotplugCheck) >= 10)
1250 m_lastHotplugCheck = now;
1254 CSingleLock lock(m_devicesListLock);
1257 event.type = XBMC_NOEVENT;
1259 for (size_t i = 0; i < m_devices.size(); i++)
1261 event = m_devices[i]->ReadEvent();
1262 if (event.type != XBMC_NOEVENT)
1267 if (m_devices[i]->IsUnplugged())
1269 m_bReInitialize = true;
1278 - 0x7F -> if not paired, battery OK
1279 - 0xFF -> if paired, battery OK
1280 - 0x00 -> if not paired, battery low
1281 - 0x80 -> if paired, battery low
1283 bool CLinuxInputDevices::IsRemoteLowBattery()
1285 bool bLowBattery = !(remoteStatus & 0xF);
1289 bool CLinuxInputDevices::IsRemoteNotPaired()
1291 bool bRemoteNotPaired = !(remoteStatus & 0x70) || !(remoteStatus & 0x80);
1292 return bRemoteNotPaired;
1298 CLinuxInputDevices devices;
1299 devices.InitAvailable();
1302 XBMC_Event event = devices.ReadEvent();
1303 if (event.type != XBMC_NOEVENT)
1305 printf("%d\n", event.type);