1 // FGEventInput.cxx -- handle event driven input devices for the Linux O/S
3 // Written by Torsten Dreyer, started July 2009.
5 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <sys/types.h>
29 #include <linux/input.h>
30 #include <dbus/dbus.h>
32 #include "FGLinuxEventInput.hxx"
35 #include <linux/input.h>
36 #include <dbus/dbus.h>
45 inline unsigned long hashCode() const {
46 return (unsigned long)type << 16 | (unsigned long)code;
49 bool operator < ( const TypeCode other) const {
50 return hashCode() < other.hashCode();
54 // event to name translation table
55 // events are from include <linux/input.h>
57 static struct EventTypes {
58 struct TypeCode typeCode;
61 { { EV_SYN, SYN_REPORT }, "syn-report" },
62 { { EV_SYN, SYN_CONFIG }, "syn-config" },
65 { { EV_KEY, BTN_0 }, "button-0" },
66 { { EV_KEY, BTN_1 }, "button-1" },
67 { { EV_KEY, BTN_2 }, "button-2" },
68 { { EV_KEY, BTN_3 }, "button-3" },
69 { { EV_KEY, BTN_4 }, "button-4" },
70 { { EV_KEY, BTN_5 }, "button-5" },
71 { { EV_KEY, BTN_6 }, "button-6" },
72 { { EV_KEY, BTN_7 }, "button-7" },
73 { { EV_KEY, BTN_8 }, "button-8" },
74 { { EV_KEY, BTN_9 }, "button-9" },
77 { { EV_KEY, BTN_LEFT }, "button-left" },
78 { { EV_KEY, BTN_RIGHT }, "button-right" },
79 { { EV_KEY, BTN_MIDDLE }, "button-middle" },
80 { { EV_KEY, BTN_SIDE }, "button-side" },
81 { { EV_KEY, BTN_EXTRA }, "button-extra" },
82 { { EV_KEY, BTN_FORWARD }, "button-forward" },
83 { { EV_KEY, BTN_BACK }, "button-back" },
84 { { EV_KEY, BTN_TASK }, "button-task" },
87 { { EV_KEY, BTN_TRIGGER }, "button-trigger" },
88 { { EV_KEY, BTN_THUMB }, "button-thumb" },
89 { { EV_KEY, BTN_THUMB2 }, "button-thumb2" },
90 { { EV_KEY, BTN_TOP }, "button-top" },
91 { { EV_KEY, BTN_TOP2 }, "button-top2" },
92 { { EV_KEY, BTN_PINKIE }, "button-pinkie" },
93 { { EV_KEY, BTN_BASE }, "button-base" },
94 { { EV_KEY, BTN_BASE2 }, "button-base2" },
95 { { EV_KEY, BTN_BASE3 }, "button-base3" },
96 { { EV_KEY, BTN_BASE4 }, "button-base4" },
97 { { EV_KEY, BTN_BASE5 }, "button-base5" },
98 { { EV_KEY, BTN_BASE6 }, "button-base6" },
99 { { EV_KEY, BTN_DEAD }, "button-dead" },
102 { { EV_KEY, BTN_A }, "button-a" },
103 { { EV_KEY, BTN_B }, "button-b" },
104 { { EV_KEY, BTN_C }, "button-c" },
105 { { EV_KEY, BTN_X }, "button-x" },
106 { { EV_KEY, BTN_Y }, "button-y" },
107 { { EV_KEY, BTN_Z }, "button-z" },
108 { { EV_KEY, BTN_TL }, "button-tl" },
109 { { EV_KEY, BTN_TR }, "button-tr" },
110 { { EV_KEY, BTN_TL2 }, "button-tl2" },
111 { { EV_KEY, BTN_TR2 }, "button-tr2" },
112 { { EV_KEY, BTN_SELECT }, "button-select" },
113 { { EV_KEY, BTN_START }, "button-start" },
114 { { EV_KEY, BTN_MODE }, "button-mode" },
115 { { EV_KEY, BTN_THUMBL }, "button-thumbl" },
116 { { EV_KEY, BTN_THUMBR }, "button-thumbr" },
119 { { EV_KEY, BTN_TOOL_PEN }, "button-pen" },
120 { { EV_KEY, BTN_TOOL_RUBBER }, "button-rubber" },
121 { { EV_KEY, BTN_TOOL_BRUSH }, "button-brush" },
122 { { EV_KEY, BTN_TOOL_PENCIL }, "button-pencil" },
123 { { EV_KEY, BTN_TOOL_AIRBRUSH }, "button-airbrush" },
124 { { EV_KEY, BTN_TOOL_FINGER }, "button-finger" },
125 { { EV_KEY, BTN_TOOL_MOUSE }, "button-mouse" },
126 { { EV_KEY, BTN_TOOL_LENS }, "button-lens" },
127 { { EV_KEY, BTN_TOUCH }, "button-touch" },
128 { { EV_KEY, BTN_STYLUS }, "button-stylus" },
129 { { EV_KEY, BTN_STYLUS2 }, "button-stylus2" },
130 { { EV_KEY, BTN_TOOL_DOUBLETAP }, "button-doubletap" },
131 { { EV_KEY, BTN_TOOL_TRIPLETAP }, "button-trippletap" },
133 { { EV_KEY, BTN_WHEEL }, "button-wheel" },
134 { { EV_KEY, BTN_GEAR_DOWN }, "button-gear-down" },
135 { { EV_KEY, BTN_GEAR_UP }, "button-gear-up" },
137 { { EV_REL, REL_X }, "rel-x-translate" },
138 { { EV_REL, REL_Y}, "rel-y-translate" },
139 { { EV_REL, REL_Z}, "rel-z-translate" },
140 { { EV_REL, REL_RX}, "rel-x-rotate" },
141 { { EV_REL, REL_RY}, "rel-y-rotate" },
142 { { EV_REL, REL_RZ}, "rel-z-rotate" },
143 { { EV_REL, REL_HWHEEL}, "rel-hwheel" },
144 { { EV_REL, REL_DIAL}, "rel-dial" },
145 { { EV_REL, REL_WHEEL}, "rel-wheel" },
146 { { EV_REL, REL_MISC}, "rel-misc" },
148 { { EV_ABS, ABS_X }, "abs-x-translate" },
149 { { EV_ABS, ABS_Y }, "abs-y-translate" },
150 { { EV_ABS, ABS_Z }, "abs-z-translate" },
151 { { EV_ABS, ABS_RX }, "abs-x-rotate" },
152 { { EV_ABS, ABS_RY }, "abs-y-rotate" },
153 { { EV_ABS, ABS_RZ }, "abs-z-rotate" },
154 { { EV_ABS, ABS_THROTTLE }, "abs-throttle" },
155 { { EV_ABS, ABS_RUDDER }, "abs-rudder" },
156 { { EV_ABS, ABS_WHEEL }, "abs-wheel" },
157 { { EV_ABS, ABS_GAS }, "abs-gas" },
158 { { EV_ABS, ABS_BRAKE }, "abs-brake" },
159 { { EV_ABS, ABS_HAT0X }, "abs-hat0-x" },
160 { { EV_ABS, ABS_HAT0Y }, "abs-hat0-y" },
161 { { EV_ABS, ABS_HAT1X }, "abs-hat1-x" },
162 { { EV_ABS, ABS_HAT1Y }, "abs-hat1-y" },
163 { { EV_ABS, ABS_HAT2X }, "abs-hat2-x" },
164 { { EV_ABS, ABS_HAT2Y }, "abs-hat2-y" },
165 { { EV_ABS, ABS_HAT3X }, "abs-hat3-x" },
166 { { EV_ABS, ABS_HAT3Y }, "abs-hat3-y" },
167 { { EV_ABS, ABS_PRESSURE }, "abs-pressure" },
168 { { EV_ABS, ABS_DISTANCE }, "abs-distance" },
169 { { EV_ABS, ABS_TILT_X }, "abs-tilt-x" },
170 { { EV_ABS, ABS_TILT_Y }, "abs-tilt-y" },
171 { { EV_ABS, ABS_TOOL_WIDTH }, "abs-toold-width" },
172 { { EV_ABS, ABS_VOLUME }, "abs-volume" },
173 { { EV_ABS, ABS_MISC }, "abs-misc" },
175 { { EV_MSC, MSC_SERIAL }, "misc-serial" },
176 { { EV_MSC, MSC_PULSELED }, "misc-pulseled" },
177 { { EV_MSC, MSC_GESTURE }, "misc-gesture" },
178 { { EV_MSC, MSC_RAW }, "misc-raw" },
179 { { EV_MSC, MSC_SCAN }, "misc-scan" },
182 { { EV_SW, SW_LID }, "switch-lid" },
183 { { EV_SW, SW_TABLET_MODE }, "switch-tablet-mode" },
184 { { EV_SW, SW_HEADPHONE_INSERT }, "switch-headphone-insert" },
186 { { EV_SW, SW_RFKILL_ALL }, "switch-rfkill" },
188 #ifdef SW_MICROPHONE_INSERT
189 { { EV_SW, SW_MICROPHONE_INSERT }, "switch-microphone-insert" },
192 { { EV_SW, SW_DOCK }, "switch-dock" },
195 { { EV_LED, LED_NUML}, "led-numlock" },
196 { { EV_LED, LED_CAPSL}, "led-capslock" },
197 { { EV_LED, LED_SCROLLL}, "led-scrolllock" },
198 { { EV_LED, LED_COMPOSE}, "led-compose" },
199 { { EV_LED, LED_KANA}, "led-kana" },
200 { { EV_LED, LED_SLEEP}, "led-sleep" },
201 { { EV_LED, LED_SUSPEND}, "led-suspend" },
202 { { EV_LED, LED_MUTE}, "led-mute" },
203 { { EV_LED, LED_MISC}, "led-misc" },
204 { { EV_LED, LED_MAIL}, "led-mail" },
205 { { EV_LED, LED_CHARGING}, "led-charging" }
209 static struct enbet {
212 } EVENT_NAMES_BY_EVENT_TYPE[] = {
214 { EV_KEY, "button" },
224 { EV_FF_STATUS, "ff-status" }
228 class EventNameByEventType : public std::map<unsigned,const char*> {
230 EventNameByEventType() {
231 for( unsigned i = 0; i < sizeof(EVENT_NAMES_BY_EVENT_TYPE)/sizeof(EVENT_NAMES_BY_EVENT_TYPE[0]); i++ )
232 (*this)[EVENT_NAMES_BY_EVENT_TYPE[i].type] = EVENT_NAMES_BY_EVENT_TYPE[i].name;
235 static EventNameByEventType EVENT_NAME_BY_EVENT_TYPE;
237 class EventNameByType : public std::map<TypeCode,const char*> {
240 for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
241 (*this)[EVENT_TYPES[i].typeCode] = EVENT_TYPES[i].name;
244 static EventNameByType EVENT_NAME_BY_TYPE;
248 bool operator()(const char * s1, const char * s2 ) const {
249 return std::string(s1).compare( s2 ) < 0;
253 class EventTypeByName : public std::map<const char *,TypeCode,ltstr> {
256 for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
257 (*this)[EVENT_TYPES[i].name] = EVENT_TYPES[i].typeCode;
260 static EventTypeByName EVENT_TYPE_BY_NAME;
263 FGLinuxInputDevice::FGLinuxInputDevice( std::string aName, std::string aDevname ) :
264 FGInputDevice(aName),
270 FGLinuxInputDevice::~FGLinuxInputDevice()
279 FGLinuxInputDevice::FGLinuxInputDevice() :
284 static inline bool bitSet( unsigned char * buf, unsigned bit )
286 return (buf[bit/sizeof(unsigned char)/8] >> (bit%(sizeof(unsigned char)*8))) & 1;
289 void FGLinuxInputDevice::Open()
291 if( fd != -1 ) return;
292 if( (fd = ::open( devname.c_str(), O_RDWR )) == -1 ) {
293 throw std::exception();
296 if( GetGrab() && ioctl( fd, EVIOCGRAB, 2 ) == -1 ) {
297 SG_LOG( SG_INPUT, SG_WARN, "Can't grab " << devname << " for exclusive access" );
301 unsigned char buf[ABS_CNT/sizeof(unsigned char)/8];
303 if( ioctl( fd, EVIOCGBIT(EV_ABS,ABS_MAX), buf ) == -1 ) {
304 SG_LOG( SG_INPUT, SG_WARN, "Can't get abs-axes for " << devname );
306 for( unsigned i = 0; i < ABS_MAX; i++ ) {
307 if( bitSet( buf, i ) ) {
308 struct input_absinfo ai;
309 if( ioctl(fd, EVIOCGABS(i), &ai) == -1 ) {
310 SG_LOG( SG_INPUT, SG_WARN, "Can't get abs-axes maximums for " << devname );
315 SG_LOG( SG_INPUT, SG_INFO, "Axis #" << i <<
316 ": value=" << ai.value <<
317 ": minimum=" << ai.minimum <<
318 ": maximum=" << ai.maximum <<
319 ": fuzz=" << ai.fuzz <<
320 ": flat=" << ai.flat <<
321 ": resolution=" << ai.resolution );
324 // kick an initial event
325 struct input_event event;
328 event.value = ai.value;
329 FGLinuxEventData eventData( event, 0, 0 );
330 eventData.value = Normalize( event );
331 HandleEvent(eventData);
337 unsigned char mask[KEY_CNT/sizeof(unsigned char)/8];
338 unsigned char flag[KEY_CNT/sizeof(unsigned char)/8];
339 memset(mask,0,sizeof(mask));
340 memset(flag,0,sizeof(flag));
341 if( ioctl( fd, EVIOCGKEY(sizeof(flag)), flag ) == -1 ||
342 ioctl( fd, EVIOCGBIT(EV_KEY, sizeof(mask)), mask ) == -1 ) {
343 SG_LOG( SG_INPUT, SG_WARN, "Can't get keys for " << devname );
345 for( unsigned i = 0; i < KEY_MAX; i++ ) {
346 if( bitSet( mask, i ) ) {
347 struct input_event event;
350 event.value = bitSet(flag,i);
351 FGLinuxEventData eventData( event, 0, 0 );
352 HandleEvent(eventData);
358 unsigned char buf[SW_CNT/sizeof(unsigned char)/8];
359 if( ioctl( fd, EVIOCGSW(sizeof(buf)), buf ) == -1 ) {
360 SG_LOG( SG_INPUT, SG_WARN, "Can't get switches for " << devname );
362 for( unsigned i = 0; i < SW_MAX; i++ ) {
363 if( bitSet( buf, i ) ) {
364 struct input_event event;
368 FGLinuxEventData eventData( event, 0, 0 );
369 HandleEvent(eventData);
376 double FGLinuxInputDevice::Normalize( struct input_event & event )
378 if( absinfo.count(event.code) > 0 ) {
379 const struct input_absinfo & ai = absinfo[(unsigned int)event.code];
380 if( ai.maximum == ai.minimum )
382 return ((double)event.value-(double)ai.minimum)/((double)ai.maximum-(double)ai.minimum);
384 return (double)event.value;
388 void FGLinuxInputDevice::Close()
391 if( GetGrab() && ioctl( fd, EVIOCGRAB, 0 ) != 0 ) {
392 SG_LOG( SG_INPUT, SG_WARN, "Can't ungrab " << devname );
399 void FGLinuxInputDevice::Send( const char * eventName, double value )
401 if( EVENT_TYPE_BY_NAME.count( eventName ) <= 0 ) {
402 SG_LOG( SG_INPUT, SG_ALERT, "Can't send unknown event " << eventName );
406 TypeCode & typeCode = EVENT_TYPE_BY_NAME[ eventName ];
412 evt.type=typeCode.type;
413 evt.code = typeCode.code;
414 evt.value = (long)value;
416 evt.time.tv_usec = 0;
417 write( fd, &evt, sizeof(evt) );
418 SG_LOG( SG_INPUT, SG_DEBUG, "Written event " << eventName
419 << " as type=" << evt.type << ", code=" << evt.code << " value=" << evt.value );
422 static char ugly_buffer[128];
423 const char * FGLinuxInputDevice::TranslateEventName( FGEventData & eventData )
425 FGLinuxEventData & linuxEventData = (FGLinuxEventData&)eventData;
427 typeCode.type = linuxEventData.type;
428 typeCode.code = linuxEventData.code;
429 if( EVENT_NAME_BY_TYPE.count(typeCode) == 0 ) {
430 // event not known in translation tables
431 if( EVENT_NAME_BY_EVENT_TYPE.count(linuxEventData.type) == 0 ) {
432 // event type not known in translation tables
433 sprintf( ugly_buffer, "unknown-%u-%u", (unsigned)linuxEventData.type, (unsigned)linuxEventData.code );
436 sprintf( ugly_buffer, "%s-%u", EVENT_NAME_BY_EVENT_TYPE[linuxEventData.type], (unsigned)linuxEventData.code );
440 return EVENT_NAME_BY_TYPE[typeCode];
443 void FGLinuxInputDevice::SetDevname( std::string name )
445 this->devname = name;
448 FGLinuxEventInput::FGLinuxEventInput() :
453 FGLinuxEventInput::~FGLinuxEventInput()
455 if( halcontext != NULL ) {
456 libhal_ctx_shutdown( halcontext, NULL);
457 libhal_ctx_free( halcontext );
463 //TODO: enable hotplug support
464 static void DeviceAddedCallback (LibHalContext *ctx, const char *udi)
466 FGLinuxEventInput * linuxEventInput = (FGLinuxEventInput*)libhal_ctx_get_user_data (ctx);
467 linuxEventInput->AddHalDevice( udi );
470 static void DeviceRemovedCallback (LibHalContext *ctx, const char *udi)
475 void FGLinuxEventInput::postinit()
477 FGEventInput::postinit();
479 DBusConnection * connection;
480 DBusError dbus_error;
482 dbus_error_init(&dbus_error);
483 connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error);
484 if (dbus_error_is_set(&dbus_error)) {
485 SG_LOG( SG_INPUT, SG_ALERT, "Can't connect to system bus " << dbus_error.message);
486 dbus_error_free (&dbus_error);
490 halcontext = libhal_ctx_new();
492 libhal_ctx_set_dbus_connection (halcontext, connection );
493 dbus_error_init (&dbus_error);
495 if( libhal_ctx_init( halcontext, &dbus_error )) {
498 char ** devices = libhal_find_device_by_capability(halcontext, "input", &num_devices, NULL);
500 for ( int i = 0; i < num_devices; i++)
501 AddHalDevice( devices[i] );
503 libhal_free_string_array (devices);
505 //TODO: enable hotplug support
506 // libhal_ctx_set_user_data( halcontext, this );
507 // libhal_ctx_set_device_added( halcontext, DeviceAddedCallback );
508 // libhal_ctx_set_device_removed( halcontext, DeviceRemovedCallback );
510 if(dbus_error_is_set (&dbus_error) ) {
511 SG_LOG( SG_INPUT, SG_ALERT, "Can't connect to hald: " << dbus_error.message);
512 dbus_error_free (&dbus_error);
514 SG_LOG( SG_INPUT, SG_ALERT, "Can't connect to hald." );
517 libhal_ctx_free (halcontext);
522 void FGLinuxEventInput::AddHalDevice( const char * udi )
524 char * device = libhal_device_get_property_string( halcontext, udi, "input.device", NULL);
525 char * product = libhal_device_get_property_string( halcontext, udi, "input.product", NULL);
527 if( product != NULL && device != NULL )
528 AddDevice( new FGLinuxInputDevice(product, device) );
530 SG_LOG( SG_INPUT, SG_ALERT, "Can't get device or product property of " << udi );
532 if( device != NULL ) libhal_free_string( device );
533 if( product != NULL ) libhal_free_string( product );
537 void FGLinuxEventInput::update( double dt )
539 FGEventInput::update( dt );
540 // index the input devices by the associated fd and prepare
541 // the pollfd array by filling in the file descriptor
542 struct pollfd fds[input_devices.size()];
543 std::map<int,FGLinuxInputDevice*> devicesByFd;
544 std::map<int,FGInputDevice*>::const_iterator it;
546 for( i=0, it = input_devices.begin(); it != input_devices.end(); ++it, i++ ) {
547 FGInputDevice* p = (*it).second;
548 int fd = ((FGLinuxInputDevice*)p)->GetFd();
550 fds[i].events = POLLIN;
551 devicesByFd[fd] = (FGLinuxInputDevice*)p;
554 int modifiers = fgGetKeyModifiers();
555 // poll all devices until no more events are in the queue
556 // do no more than maxpolls in a single loop to prevent locking
558 while( maxpolls-- > 0 && ::poll( fds, i, 0 ) > 0 ) {
559 for( unsigned i = 0; i < sizeof(fds)/sizeof(fds[0]); i++ ) {
560 if( fds[i].revents & POLLIN ) {
562 // if this device reports data ready, it should be a input_event struct
563 struct input_event event;
565 if( read( fds[i].fd, &event, sizeof(event) ) != sizeof(event) )
568 FGLinuxEventData eventData( event, dt, modifiers );
570 if( event.type == EV_ABS )
571 eventData.value = devicesByFd[fds[i].fd]->Normalize( event );
573 // let the FGInputDevice handle the data
574 devicesByFd[fds[i].fd]->HandleEvent( eventData );