1 // FGEventInput.cxx -- handle event driven input devices for the Linux O/S
2 //
3 // Written by Torsten Dreyer, started July 2009.
4 //
5 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <cstring>
26 #include <cstdio>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include "FGLinuxEventInput.hxx"
31
32 extern "C" {
33 #include <libudev.h>
34 }
35
36 #include <poll.h>
37 #include <linux/input.h>
38 #include <fcntl.h>
39
40 #include <string.h>
41
42 struct TypeCode {
43 unsigned type;
44 unsigned code;
45
hashCodeTypeCode46 inline unsigned long hashCode() const {
47 return (unsigned long)type << 16 | (unsigned long)code;
48 }
49
operator <TypeCode50 bool operator < ( const TypeCode & other) const {
51 return hashCode() < other.hashCode();
52 }
53 };
54
55 // event to name translation table
56 // events are from include <linux/input.h>
57
58 static struct EventTypes {
59 struct TypeCode typeCode;
60 const char * name;
61 } EVENT_TYPES[] = {
62 { { EV_SYN, SYN_REPORT }, "syn-report" },
63 { { EV_SYN, SYN_CONFIG }, "syn-config" },
64
65 // misc
66 { { EV_KEY, BTN_0 }, "button-0" },
67 { { EV_KEY, BTN_1 }, "button-1" },
68 { { EV_KEY, BTN_2 }, "button-2" },
69 { { EV_KEY, BTN_3 }, "button-3" },
70 { { EV_KEY, BTN_4 }, "button-4" },
71 { { EV_KEY, BTN_5 }, "button-5" },
72 { { EV_KEY, BTN_6 }, "button-6" },
73 { { EV_KEY, BTN_7 }, "button-7" },
74 { { EV_KEY, BTN_8 }, "button-8" },
75 { { EV_KEY, BTN_9 }, "button-9" },
76
77 // mouse
78 { { EV_KEY, BTN_LEFT }, "button-left" },
79 { { EV_KEY, BTN_RIGHT }, "button-right" },
80 { { EV_KEY, BTN_MIDDLE }, "button-middle" },
81 { { EV_KEY, BTN_SIDE }, "button-side" },
82 { { EV_KEY, BTN_EXTRA }, "button-extra" },
83 { { EV_KEY, BTN_FORWARD }, "button-forward" },
84 { { EV_KEY, BTN_BACK }, "button-back" },
85 { { EV_KEY, BTN_TASK }, "button-task" },
86
87 // joystick
88 { { EV_KEY, BTN_TRIGGER }, "button-trigger" },
89 { { EV_KEY, BTN_THUMB }, "button-thumb" },
90 { { EV_KEY, BTN_THUMB2 }, "button-thumb2" },
91 { { EV_KEY, BTN_TOP }, "button-top" },
92 { { EV_KEY, BTN_TOP2 }, "button-top2" },
93 { { EV_KEY, BTN_PINKIE }, "button-pinkie" },
94 { { EV_KEY, BTN_BASE }, "button-base" },
95 { { EV_KEY, BTN_BASE2 }, "button-base2" },
96 { { EV_KEY, BTN_BASE3 }, "button-base3" },
97 { { EV_KEY, BTN_BASE4 }, "button-base4" },
98 { { EV_KEY, BTN_BASE5 }, "button-base5" },
99 { { EV_KEY, BTN_BASE6 }, "button-base6" },
100 { { EV_KEY, BTN_DEAD }, "button-dead" },
101
102 // gamepad
103 { { EV_KEY, BTN_A }, "button-a" },
104 { { EV_KEY, BTN_B }, "button-b" },
105 { { EV_KEY, BTN_C }, "button-c" },
106 { { EV_KEY, BTN_X }, "button-x" },
107 { { EV_KEY, BTN_Y }, "button-y" },
108 { { EV_KEY, BTN_Z }, "button-z" },
109 { { EV_KEY, BTN_TL }, "button-tl" },
110 { { EV_KEY, BTN_TR }, "button-tr" },
111 { { EV_KEY, BTN_TL2 }, "button-tl2" },
112 { { EV_KEY, BTN_TR2 }, "button-tr2" },
113 { { EV_KEY, BTN_SELECT }, "button-select" },
114 { { EV_KEY, BTN_START }, "button-start" },
115 { { EV_KEY, BTN_MODE }, "button-mode" },
116 { { EV_KEY, BTN_THUMBL }, "button-thumbl" },
117 { { EV_KEY, BTN_THUMBR }, "button-thumbr" },
118
119 // digitizer
120 { { EV_KEY, BTN_TOOL_PEN }, "button-pen" },
121 { { EV_KEY, BTN_TOOL_RUBBER }, "button-rubber" },
122 { { EV_KEY, BTN_TOOL_BRUSH }, "button-brush" },
123 { { EV_KEY, BTN_TOOL_PENCIL }, "button-pencil" },
124 { { EV_KEY, BTN_TOOL_AIRBRUSH }, "button-airbrush" },
125 { { EV_KEY, BTN_TOOL_FINGER }, "button-finger" },
126 { { EV_KEY, BTN_TOOL_MOUSE }, "button-mouse" },
127 { { EV_KEY, BTN_TOOL_LENS }, "button-lens" },
128 { { EV_KEY, BTN_TOUCH }, "button-touch" },
129 { { EV_KEY, BTN_STYLUS }, "button-stylus" },
130 { { EV_KEY, BTN_STYLUS2 }, "button-stylus2" },
131 { { EV_KEY, BTN_TOOL_DOUBLETAP }, "button-doubletap" },
132 { { EV_KEY, BTN_TOOL_TRIPLETAP }, "button-trippletap" },
133
134 { { EV_KEY, BTN_WHEEL }, "button-wheel" },
135 { { EV_KEY, BTN_GEAR_DOWN }, "button-gear-down" },
136 { { EV_KEY, BTN_GEAR_UP }, "button-gear-up" },
137
138 { { EV_REL, REL_X }, "rel-x-translate" },
139 { { EV_REL, REL_Y}, "rel-y-translate" },
140 { { EV_REL, REL_Z}, "rel-z-translate" },
141 { { EV_REL, REL_RX}, "rel-x-rotate" },
142 { { EV_REL, REL_RY}, "rel-y-rotate" },
143 { { EV_REL, REL_RZ}, "rel-z-rotate" },
144 { { EV_REL, REL_HWHEEL}, "rel-hwheel" },
145 { { EV_REL, REL_DIAL}, "rel-dial" },
146 { { EV_REL, REL_WHEEL}, "rel-wheel" },
147 { { EV_REL, REL_MISC}, "rel-misc" },
148
149 { { EV_ABS, ABS_X }, "abs-x-translate" },
150 { { EV_ABS, ABS_Y }, "abs-y-translate" },
151 { { EV_ABS, ABS_Z }, "abs-z-translate" },
152 { { EV_ABS, ABS_RX }, "abs-x-rotate" },
153 { { EV_ABS, ABS_RY }, "abs-y-rotate" },
154 { { EV_ABS, ABS_RZ }, "abs-z-rotate" },
155 { { EV_ABS, ABS_THROTTLE }, "abs-throttle" },
156 { { EV_ABS, ABS_RUDDER }, "abs-rudder" },
157 { { EV_ABS, ABS_WHEEL }, "abs-wheel" },
158 { { EV_ABS, ABS_GAS }, "abs-gas" },
159 { { EV_ABS, ABS_BRAKE }, "abs-brake" },
160 { { EV_ABS, ABS_HAT0X }, "abs-hat0-x" },
161 { { EV_ABS, ABS_HAT0Y }, "abs-hat0-y" },
162 { { EV_ABS, ABS_HAT1X }, "abs-hat1-x" },
163 { { EV_ABS, ABS_HAT1Y }, "abs-hat1-y" },
164 { { EV_ABS, ABS_HAT2X }, "abs-hat2-x" },
165 { { EV_ABS, ABS_HAT2Y }, "abs-hat2-y" },
166 { { EV_ABS, ABS_HAT3X }, "abs-hat3-x" },
167 { { EV_ABS, ABS_HAT3Y }, "abs-hat3-y" },
168 { { EV_ABS, ABS_PRESSURE }, "abs-pressure" },
169 { { EV_ABS, ABS_DISTANCE }, "abs-distance" },
170 { { EV_ABS, ABS_TILT_X }, "abs-tilt-x" },
171 { { EV_ABS, ABS_TILT_Y }, "abs-tilt-y" },
172 { { EV_ABS, ABS_TOOL_WIDTH }, "abs-toold-width" },
173 { { EV_ABS, ABS_VOLUME }, "abs-volume" },
174 { { EV_ABS, ABS_MISC }, "abs-misc" },
175
176 { { EV_MSC, MSC_SERIAL }, "misc-serial" },
177 { { EV_MSC, MSC_PULSELED }, "misc-pulseled" },
178 { { EV_MSC, MSC_GESTURE }, "misc-gesture" },
179 { { EV_MSC, MSC_RAW }, "misc-raw" },
180 { { EV_MSC, MSC_SCAN }, "misc-scan" },
181
182 // switch
183 { { EV_SW, SW_LID }, "switch-lid" },
184 { { EV_SW, SW_TABLET_MODE }, "switch-tablet-mode" },
185 { { EV_SW, SW_HEADPHONE_INSERT }, "switch-headphone-insert" },
186 #ifdef SW_RFKILL_ALL
187 { { EV_SW, SW_RFKILL_ALL }, "switch-rfkill" },
188 #endif
189 #ifdef SW_MICROPHONE_INSERT
190 { { EV_SW, SW_MICROPHONE_INSERT }, "switch-microphone-insert" },
191 #endif
192 #ifdef SW_DOCK
193 { { EV_SW, SW_DOCK }, "switch-dock" },
194 #endif
195
196 { { EV_LED, LED_NUML}, "led-numlock" },
197 { { EV_LED, LED_CAPSL}, "led-capslock" },
198 { { EV_LED, LED_SCROLLL}, "led-scrolllock" },
199 { { EV_LED, LED_COMPOSE}, "led-compose" },
200 { { EV_LED, LED_KANA}, "led-kana" },
201 { { EV_LED, LED_SLEEP}, "led-sleep" },
202 { { EV_LED, LED_SUSPEND}, "led-suspend" },
203 { { EV_LED, LED_MUTE}, "led-mute" },
204 { { EV_LED, LED_MISC}, "led-misc" },
205 { { EV_LED, LED_MAIL}, "led-mail" },
206 { { EV_LED, LED_CHARGING}, "led-charging" }
207
208 };
209
210 static struct enbet {
211 unsigned type;
212 const char * name;
213 } EVENT_NAMES_BY_EVENT_TYPE[] = {
214 { EV_SYN, "syn" },
215 { EV_KEY, "button" },
216 { EV_REL, "rel" },
217 { EV_ABS, "abs" },
218 { EV_MSC, "msc" },
219 { EV_SW, "button" },
220 { EV_LED, "led" },
221 { EV_SND, "snd" },
222 { EV_REP, "rep" },
223 { EV_FF, "ff" },
224 { EV_PWR, "pwr" },
225 { EV_FF_STATUS, "ff-status" }
226 };
227
228
229 class EventNameByEventType : public std::map<unsigned,const char*> {
230 public:
EventNameByEventType()231 EventNameByEventType() {
232 for( unsigned i = 0; i < sizeof(EVENT_NAMES_BY_EVENT_TYPE)/sizeof(EVENT_NAMES_BY_EVENT_TYPE[0]); i++ )
233 (*this)[EVENT_NAMES_BY_EVENT_TYPE[i].type] = EVENT_NAMES_BY_EVENT_TYPE[i].name;
234 }
235 };
236 static EventNameByEventType EVENT_NAME_BY_EVENT_TYPE;
237
238 class EventNameByType : public std::map<TypeCode,const char*> {
239 public:
EventNameByType()240 EventNameByType() {
241 for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
242 (*this)[EVENT_TYPES[i].typeCode] = EVENT_TYPES[i].name;
243 }
244 };
245 static EventNameByType EVENT_NAME_BY_TYPE;
246
247
248 struct ltstr {
operator ()ltstr249 bool operator()(const char * s1, const char * s2 ) const {
250 return std::string(s1).compare( s2 ) < 0;
251 }
252 };
253
254 class EventTypeByName : public std::map<const char *,TypeCode,ltstr> {
255 public:
EventTypeByName()256 EventTypeByName() {
257 for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
258 (*this)[EVENT_TYPES[i].name] = EVENT_TYPES[i].typeCode;
259 }
260 };
261 static EventTypeByName EVENT_TYPE_BY_NAME;
262
263
FGLinuxInputDevice(std::string aName,std::string aDevname,std::string aSerial)264 FGLinuxInputDevice::FGLinuxInputDevice( std::string aName, std::string aDevname, std::string aSerial ) :
265 FGInputDevice(aName,aSerial),
266 devname( aDevname ),
267 fd(-1)
268 {
269 }
270
~FGLinuxInputDevice()271 FGLinuxInputDevice::~FGLinuxInputDevice()
272 {
273 try {
274 Close();
275 }
276 catch(...) {
277 }
278 }
279
FGLinuxInputDevice()280 FGLinuxInputDevice::FGLinuxInputDevice() :
281 fd(-1)
282 {
283 }
284
bitSet(unsigned char * buf,unsigned bit)285 static inline bool bitSet( unsigned char * buf, unsigned bit )
286 {
287 return (buf[bit/sizeof(unsigned char)/8] >> (bit%(sizeof(unsigned char)*8))) & 1;
288 }
289
Open()290 bool FGLinuxInputDevice::Open()
291 {
292 if( fd != -1 ) return true;
293 if( (fd = ::open( devname.c_str(), O_RDWR )) == -1 ) {
294 throw std::exception();
295 }
296
297 if( GetGrab() && ioctl( fd, EVIOCGRAB, 2 ) == -1 ) {
298 SG_LOG( SG_INPUT, SG_WARN, "Can't grab " << devname << " for exclusive access" );
299 }
300
301 {
302 unsigned char buf[ABS_CNT/sizeof(unsigned char)/8];
303 // get axes maximums
304 if( ioctl( fd, EVIOCGBIT(EV_ABS,ABS_MAX), buf ) == -1 ) {
305 SG_LOG( SG_INPUT, SG_WARN, "Can't get abs-axes for " << devname );
306 } else {
307 for( unsigned i = 0; i < ABS_MAX; i++ ) {
308 if( bitSet( buf, i ) ) {
309 struct input_absinfo ai;
310 if( ioctl(fd, EVIOCGABS(i), &ai) == -1 ) {
311 SG_LOG( SG_INPUT, SG_WARN, "Can't get abs-axes maximums for " << devname );
312 continue;
313 }
314 absinfo[i] = ai;
315 /*
316 SG_LOG( SG_INPUT, SG_INFO, "Axis #" << i <<
317 ": value=" << ai.value <<
318 ": minimum=" << ai.minimum <<
319 ": maximum=" << ai.maximum <<
320 ": fuzz=" << ai.fuzz <<
321 ": flat=" << ai.flat <<
322 ": resolution=" << ai.resolution );
323 */
324
325 // kick an initial event
326 struct input_event event;
327 event.type = EV_ABS;
328 event.code = i;
329 event.value = ai.value;
330 FGLinuxEventData eventData( event, 0, 0 );
331 eventData.value = Normalize( event );
332 HandleEvent(eventData);
333 }
334 }
335 }
336 }
337 {
338 unsigned char mask[KEY_CNT/sizeof(unsigned char)/8];
339 unsigned char flag[KEY_CNT/sizeof(unsigned char)/8];
340 memset(mask,0,sizeof(mask));
341 memset(flag,0,sizeof(flag));
342 if( ioctl( fd, EVIOCGKEY(sizeof(flag)), flag ) == -1 ||
343 ioctl( fd, EVIOCGBIT(EV_KEY, sizeof(mask)), mask ) == -1 ) {
344 SG_LOG( SG_INPUT, SG_WARN, "Can't get keys for " << devname );
345 } else {
346 for( unsigned i = 0; i < KEY_MAX; i++ ) {
347 if( bitSet( mask, i ) ) {
348 struct input_event event;
349 event.type = EV_KEY;
350 event.code = i;
351 event.value = bitSet(flag,i);
352 FGLinuxEventData eventData( event, 0, 0 );
353 HandleEvent(eventData);
354 }
355 }
356 }
357 }
358 {
359 unsigned char buf[SW_CNT/sizeof(unsigned char)/8];
360 if( ioctl( fd, EVIOCGSW(sizeof(buf)), buf ) == -1 ) {
361 SG_LOG( SG_INPUT, SG_WARN, "Can't get switches for " << devname );
362 } else {
363 for( unsigned i = 0; i < SW_MAX; i++ ) {
364 if( bitSet( buf, i ) ) {
365 struct input_event event;
366 event.type = EV_SW;
367 event.code = i;
368 event.value = 1;
369 FGLinuxEventData eventData( event, 0, 0 );
370 HandleEvent(eventData);
371 }
372 }
373 }
374 }
375
376 return true;
377 }
378
Normalize(struct input_event & event)379 double FGLinuxInputDevice::Normalize( struct input_event & event )
380 {
381 if( absinfo.count(event.code) > 0 ) {
382 const struct input_absinfo & ai = absinfo[(unsigned int)event.code];
383 if( ai.maximum == ai.minimum )
384 return 0.0;
385 return ((double)event.value-(double)ai.minimum)/((double)ai.maximum-(double)ai.minimum);
386 } else {
387 return (double)event.value;
388 }
389 }
390
Close()391 void FGLinuxInputDevice::Close()
392 {
393 if( fd != -1 ) {
394 if( GetGrab() && ioctl( fd, EVIOCGRAB, 0 ) != 0 ) {
395 SG_LOG( SG_INPUT, SG_WARN, "Can't ungrab " << devname );
396 }
397 ::close(fd);
398 }
399 fd = -1;
400 }
401
Send(const char * eventName,double value)402 void FGLinuxInputDevice::Send( const char * eventName, double value )
403 {
404 if( EVENT_TYPE_BY_NAME.count( eventName ) <= 0 ) {
405 SG_LOG( SG_INPUT, SG_ALERT, "Can't send unknown event " << eventName );
406 return;
407 }
408
409 TypeCode & typeCode = EVENT_TYPE_BY_NAME[ eventName ];
410
411 if( fd == -1 )
412 return;
413
414 input_event evt;
415 evt.type=typeCode.type;
416 evt.code = typeCode.code;
417 evt.value = (long)value;
418 evt.time.tv_sec = 0;
419 evt.time.tv_usec = 0;
420 size_t bytes_written = write(fd, &evt, sizeof(evt));
421
422 if( bytes_written == sizeof(evt) )
423 SG_LOG( SG_INPUT,
424 SG_DEBUG,
425 "Written event " << eventName << " as type=" << evt.type
426 << ", code=" << evt.code
427 << " value=" << evt.value );
428 else
429 SG_LOG( SG_INPUT,
430 SG_WARN,
431 "Failed to write event: written = " << bytes_written );
432 }
433
434 static char ugly_buffer[128];
TranslateEventName(FGEventData & eventData)435 const char * FGLinuxInputDevice::TranslateEventName( FGEventData & eventData )
436 {
437 FGLinuxEventData & linuxEventData = (FGLinuxEventData&)eventData;
438 TypeCode typeCode;
439 typeCode.type = linuxEventData.type;
440 typeCode.code = linuxEventData.code;
441 if( EVENT_NAME_BY_TYPE.count(typeCode) == 0 ) {
442 // event not known in translation tables
443 if( EVENT_NAME_BY_EVENT_TYPE.count(linuxEventData.type) == 0 ) {
444 // event type not known in translation tables
445 sprintf( ugly_buffer, "unknown-%u-%u", (unsigned)linuxEventData.type, (unsigned)linuxEventData.code );
446 return ugly_buffer;
447 }
448 sprintf( ugly_buffer, "%s-%u", EVENT_NAME_BY_EVENT_TYPE[linuxEventData.type], (unsigned)linuxEventData.code );
449 return ugly_buffer;
450 }
451
452 return EVENT_NAME_BY_TYPE[typeCode];
453 }
454
SetDevname(const std::string & name)455 void FGLinuxInputDevice::SetDevname( const std::string & name )
456 {
457 this->devname = name;
458 }
459
FGLinuxEventInput()460 FGLinuxEventInput::FGLinuxEventInput()
461 {
462 }
463
~FGLinuxEventInput()464 FGLinuxEventInput::~FGLinuxEventInput()
465 {
466 }
467
postinit()468 void FGLinuxEventInput::postinit()
469 {
470 FGEventInput::postinit();
471
472 struct udev * udev = udev_new();
473
474 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
475 udev_enumerate_add_match_subsystem(enumerate, "input");
476 udev_enumerate_scan_devices(enumerate);
477 struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
478 struct udev_list_entry *dev_list_entry;
479
480 udev_list_entry_foreach(dev_list_entry, devices) {
481 const char * path = udev_list_entry_get_name(dev_list_entry);
482 struct udev_device *dev = udev_device_new_from_syspath(udev, path);
483 const char * node = udev_device_get_devnode(dev);
484
485 struct udev_device * parent_dev = udev_device_get_parent( dev );
486 if ( parent_dev != NULL ) {
487 const char * name = udev_device_get_sysattr_value(parent_dev,"name");
488 const char * serial = udev_device_get_sysattr_value(parent_dev, "serial");
489 SG_LOG(SG_INPUT,SG_DEBUG, "name=" << (name?name:"<null>") << ", node=" << (node?node:"<null>"));
490 if( name && node ) {
491 std::string serialString = serial ? serial : std::string{};
492 AddDevice( new FGLinuxInputDevice(name, node, serialString) );
493 }
494 }
495
496 udev_device_unref(dev);
497 }
498
499 udev_unref(udev);
500
501 }
502
update(double dt)503 void FGLinuxEventInput::update( double dt )
504 {
505 FGEventInput::update( dt );
506 // index the input devices by the associated fd and prepare
507 // the pollfd array by filling in the file descriptor
508 struct pollfd fds[input_devices.size()];
509 std::map<int,FGLinuxInputDevice*> devicesByFd;
510 std::map<int,FGInputDevice*>::const_iterator it;
511 int i;
512 for( i=0, it = input_devices.begin(); it != input_devices.end(); ++it, i++ ) {
513 FGInputDevice* p = (*it).second;
514 int fd = ((FGLinuxInputDevice*)p)->GetFd();
515 fds[i].fd = fd;
516 fds[i].events = POLLIN;
517 devicesByFd[fd] = (FGLinuxInputDevice*)p;
518 }
519
520 int modifiers = fgGetKeyModifiers();
521 // poll all devices until no more events are in the queue
522 // do no more than maxpolls in a single loop to prevent locking
523 int maxpolls = 100;
524 while( maxpolls-- > 0 && ::poll( fds, i, 0 ) > 0 ) {
525 for( unsigned i = 0; i < sizeof(fds)/sizeof(fds[0]); i++ ) {
526 if( fds[i].revents & POLLIN ) {
527
528 // if this device reports data ready, it should be a input_event struct
529 struct input_event event;
530
531 if( read( fds[i].fd, &event, sizeof(event) ) != sizeof(event) )
532 continue;
533
534 FGLinuxEventData eventData( event, dt, modifiers );
535
536 if( event.type == EV_ABS )
537 eventData.value = devicesByFd[fds[i].fd]->Normalize( event );
538
539 // let the FGInputDevice handle the data
540 devicesByFd[fds[i].fd]->HandleEvent( eventData );
541 }
542 }
543 }
544 }
545
546