]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGLinuxEventInput.cxx
add option <grab/> to the device configuration. If this is set to 'true', the events...
[flightgear.git] / src / Input / FGLinuxEventInput.cxx
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 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include "FGLinuxEventInput.hxx"
28
29 #include <poll.h>
30 #include <linux/input.h>
31 #include <dbus/dbus.h>
32
33 struct TypeCode {
34   unsigned type;
35   unsigned code;
36
37   inline unsigned long hashCode() const {
38     return (unsigned long)type << 16 | (unsigned long)code;
39   }
40
41   bool operator < ( const TypeCode other) const {
42     return hashCode() < other.hashCode();
43   }
44 };
45
46 // event to name translation table
47 // events are from include <linux/input.h>
48
49 static struct EventTypes {
50   struct TypeCode typeCode;
51   const char * name;
52 } EVENT_TYPES[] = {
53   { { EV_SYN, SYN_REPORT },     "syn-report" },
54   { { EV_SYN, SYN_CONFIG },     "syn-config" },
55
56   // misc
57   { { EV_KEY, BTN_0 }, "button-0" },
58   { { EV_KEY, BTN_1 }, "button-1" },
59   { { EV_KEY, BTN_2 }, "button-2" },
60   { { EV_KEY, BTN_3 }, "button-3" },
61   { { EV_KEY, BTN_4 }, "button-4" },
62   { { EV_KEY, BTN_5 }, "button-5" },
63   { { EV_KEY, BTN_6 }, "button-6" },
64   { { EV_KEY, BTN_7 }, "button-7" },
65   { { EV_KEY, BTN_8 }, "button-8" },
66   { { EV_KEY, BTN_9 }, "button-9" },
67
68   // mouse
69   { { EV_KEY, BTN_LEFT },    "button-left" },
70   { { EV_KEY, BTN_RIGHT },   "button-right" },
71   { { EV_KEY, BTN_MIDDLE },  "button-middle" },
72   { { EV_KEY, BTN_SIDE },    "button-side" },
73   { { EV_KEY, BTN_EXTRA },   "button-extra" },
74   { { EV_KEY, BTN_FORWARD }, "button-forward" },
75   { { EV_KEY, BTN_BACK },    "button-back" },
76   { { EV_KEY, BTN_TASK },    "button-task" },
77
78   // joystick
79   { { EV_KEY, BTN_TRIGGER }, "button-trigger" },
80   { { EV_KEY, BTN_THUMB },   "button-thumb" },
81   { { EV_KEY, BTN_THUMB2 },  "button-thumb2" },
82   { { EV_KEY, BTN_TOP },     "button-top" },
83   { { EV_KEY, BTN_TOP2 },    "button-top2" },
84   { { EV_KEY, BTN_PINKIE },  "button-pinkie" },
85   { { EV_KEY, BTN_BASE },    "button-base" },
86   { { EV_KEY, BTN_BASE2 },   "button-base2" },
87   { { EV_KEY, BTN_BASE3 },   "button-base3" },
88   { { EV_KEY, BTN_BASE4 },   "button-base4" },
89   { { EV_KEY, BTN_BASE5 },   "button-base5" },
90   { { EV_KEY, BTN_BASE6 },   "button-base6" },
91   { { EV_KEY, BTN_DEAD },    "button-dead" },
92
93   // gamepad
94   { { EV_KEY, BTN_A },      "button-a" },
95   { { EV_KEY, BTN_B },      "button-b" },
96   { { EV_KEY, BTN_C },      "button-c" },
97   { { EV_KEY, BTN_X },      "button-x" },
98   { { EV_KEY, BTN_Y },      "button-y" },
99   { { EV_KEY, BTN_Z },      "button-z" },
100   { { EV_KEY, BTN_TL },     "button-tl" },
101   { { EV_KEY, BTN_TR },     "button-tr" },
102   { { EV_KEY, BTN_TL2 },    "button-tl2" },
103   { { EV_KEY, BTN_TR2 },    "button-tr2" },
104   { { EV_KEY, BTN_SELECT }, "button-select" },
105   { { EV_KEY, BTN_START },  "button-start" },
106   { { EV_KEY, BTN_MODE },   "button-mode" },
107   { { EV_KEY, BTN_THUMBL }, "button-thumbl" },
108   { { EV_KEY, BTN_THUMBR }, "button-thumbr" },
109
110   // digitizer
111   { { EV_KEY, BTN_TOOL_PEN },       "button-pen" },
112   { { EV_KEY, BTN_TOOL_RUBBER },    "button-rubber" },
113   { { EV_KEY, BTN_TOOL_BRUSH },     "button-brush" },
114   { { EV_KEY, BTN_TOOL_PENCIL },    "button-pencil" },
115   { { EV_KEY, BTN_TOOL_AIRBRUSH },  "button-airbrush" },
116   { { EV_KEY, BTN_TOOL_FINGER },    "button-finger" },
117   { { EV_KEY, BTN_TOOL_MOUSE },     "button-mouse" },
118   { { EV_KEY, BTN_TOOL_LENS },      "button-lens" },
119   { { EV_KEY, BTN_TOUCH },          "button-touch" },
120   { { EV_KEY, BTN_STYLUS },         "button-stylus" },
121   { { EV_KEY, BTN_STYLUS2 },        "button-stylus2" },
122   { { EV_KEY, BTN_TOOL_DOUBLETAP }, "button-doubletap" },
123   { { EV_KEY, BTN_TOOL_TRIPLETAP }, "button-trippletap" },
124
125   { { EV_KEY, BTN_WHEEL },          "button-wheel" },
126   { { EV_KEY, BTN_GEAR_DOWN },      "button-gear-down" },
127   { { EV_KEY, BTN_GEAR_UP },        "button-gear-up" },
128
129   { { EV_REL, REL_X },     "rel-x-translate" },
130   { { EV_REL, REL_Y},      "rel-y-translate" },
131   { { EV_REL, REL_Z},      "rel-z-translate" },
132   { { EV_REL, REL_RX},     "rel-x-rotate" },
133   { { EV_REL, REL_RY},     "rel-y-rotate" },
134   { { EV_REL, REL_RZ},     "rel-z-rotate" },
135   { { EV_REL, REL_HWHEEL}, "rel-hwheel" },
136   { { EV_REL, REL_DIAL},   "rel-dial" },
137   { { EV_REL, REL_WHEEL},  "rel-wheel" },
138   { { EV_REL, REL_MISC},   "rel-misc" },
139
140   { { EV_ABS, ABS_X },          "abs-x-translate" },
141   { { EV_ABS, ABS_Y },          "abs-y-translate" },
142   { { EV_ABS, ABS_Z },          "abs-z-translate" },
143   { { EV_ABS, ABS_RX },         "abs-x-rotate" },
144   { { EV_ABS, ABS_RY },         "abs-y-rotate" },
145   { { EV_ABS, ABS_RZ },         "abs-z-rotate" },
146   { { EV_ABS, ABS_THROTTLE },   "abs-throttle" },
147   { { EV_ABS, ABS_RUDDER },     "abs-rudder" },
148   { { EV_ABS, ABS_WHEEL },      "abs-wheel" },
149   { { EV_ABS, ABS_GAS },        "abs-gas" },
150   { { EV_ABS, ABS_BRAKE },      "abs-brake" },
151   { { EV_ABS, ABS_HAT0X },      "abs-hat0-x" },
152   { { EV_ABS, ABS_HAT0Y },      "abs-hat0-y" },
153   { { EV_ABS, ABS_HAT1X },      "abs-hat1-x" },
154   { { EV_ABS, ABS_HAT1Y },      "abs-hat1-y" },
155   { { EV_ABS, ABS_HAT2X },      "abs-hat2-x" },
156   { { EV_ABS, ABS_HAT2Y },      "abs-hat2-y" },
157   { { EV_ABS, ABS_HAT3X },      "abs-hat3-x" },
158   { { EV_ABS, ABS_HAT3Y },      "abs-hat3-y" },
159   { { EV_ABS, ABS_PRESSURE },   "abs-pressure" },
160   { { EV_ABS, ABS_DISTANCE },   "abs-distance" },
161   { { EV_ABS, ABS_TILT_X },     "abs-tilt-x" },
162   { { EV_ABS, ABS_TILT_Y },     "abs-tilt-y" },
163   { { EV_ABS, ABS_TOOL_WIDTH }, "abs-toold-width" },
164   { { EV_ABS, ABS_VOLUME },     "abs-volume" },
165   { { EV_ABS, ABS_MISC },       "abs-misc" },
166
167   { { EV_MSC,  MSC_SERIAL },    "misc-serial" },
168   { { EV_MSC,  MSC_PULSELED },  "misc-pulseled" },
169   { { EV_MSC,  MSC_GESTURE },   "misc-gesture" },
170   { { EV_MSC,  MSC_RAW },       "misc-raw" },
171   { { EV_MSC,  MSC_SCAN },      "misc-scan" },
172
173   // switch
174   { { EV_SW, SW_LID },               "switch-lid" },
175   { { EV_SW, SW_TABLET_MODE },       "switch-tablet-mode" },
176   { { EV_SW, SW_HEADPHONE_INSERT },  "switch-headphone-insert" },
177 #ifdef SW_RFKILL_ALL 
178   { { EV_SW, SW_RFKILL_ALL },        "switch-rfkill" },
179 #endif
180 #ifdef SW_MICROPHONE_INSERT 
181   { { EV_SW, SW_MICROPHONE_INSERT }, "switch-microphone-insert" },
182 #endif
183 #ifdef SW_DOCK
184   { { EV_SW, SW_DOCK },              "switch-dock" },
185 #endif
186
187   { { EV_LED, LED_NUML},     "led-numlock" },
188   { { EV_LED, LED_CAPSL},    "led-capslock" },
189   { { EV_LED, LED_SCROLLL},  "led-scrolllock" },
190   { { EV_LED, LED_COMPOSE},  "led-compose" },
191   { { EV_LED, LED_KANA},     "led-kana" },
192   { { EV_LED, LED_SLEEP},    "led-sleep" },
193   { { EV_LED, LED_SUSPEND},  "led-suspend" },
194   { { EV_LED, LED_MUTE},     "led-mute" },
195   { { EV_LED, LED_MISC},     "led-misc" },
196   { { EV_LED, LED_MAIL},     "led-mail" },
197   { { EV_LED, LED_CHARGING}, "led-charging" }
198
199 };
200
201 class EventNameByType : public map<TypeCode,const char*> {
202 public:
203   EventNameByType() {
204     for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
205       (*this)[EVENT_TYPES[i].typeCode] = EVENT_TYPES[i].name;
206   }
207 };
208 static EventNameByType EVENT_NAME_BY_TYPE;
209
210
211 struct ltstr {
212   bool operator()(const char * s1, const char * s2 ) const {
213     return strcmp( s1, s2 ) < 0;
214   }
215 };
216
217 class EventTypeByName : public map<const char *,TypeCode,ltstr> {
218 public:
219   EventTypeByName() {
220     for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
221       (*this)[EVENT_TYPES[i].name] = EVENT_TYPES[i].typeCode;
222   }
223 };
224 static EventTypeByName EVENT_TYPE_BY_NAME;
225
226
227 FGLinuxInputDevice::FGLinuxInputDevice( string aName, string aDevname ) :
228   FGInputDevice(aName),
229   devname( aDevname ),
230   fd(-1)
231 {
232 }
233
234 FGLinuxInputDevice::~FGLinuxInputDevice()
235 {
236   try {
237     Close();
238   } 
239   catch(...) {
240   }
241 }
242
243 FGLinuxInputDevice::FGLinuxInputDevice() :
244   fd(-1)
245 {
246 }
247
248 void FGLinuxInputDevice::Open()
249 {
250   if( fd != -1 ) return;
251   if( (fd = ::open( devname.c_str(), O_RDWR )) == -1 ) { 
252     throw exception();
253   }
254
255   if( GetGrab() && ioctl( fd, EVIOCGRAB, 2 ) != 0 ) {
256     SG_LOG( SG_INPUT, SG_WARN, "Can't grab " << devname << " for exclusive access" );
257   }
258 }
259
260 void FGLinuxInputDevice::Close()
261 {
262   if( fd != -1 ) {
263     if( GetGrab() && ioctl( fd, EVIOCGRAB, 0 ) != 0 ) {
264       SG_LOG( SG_INPUT, SG_WARN, "Can't ungrab " << devname );
265     }
266     ::close(fd);
267   }
268   fd = -1;
269 }
270
271 void FGLinuxInputDevice::Send( const char * eventName, double value )
272 {
273   if( EVENT_TYPE_BY_NAME.count( eventName ) <= 0 ) {
274     SG_LOG( SG_INPUT, SG_ALERT, "Can't send unknown event " << eventName );
275     return;
276   }
277
278   TypeCode & typeCode = EVENT_TYPE_BY_NAME[ eventName ];
279
280   if( fd == -1 )
281     return;
282
283   input_event evt;
284   evt.type=typeCode.type;
285   evt.code = typeCode.code;
286   evt.value = (long)value;
287   evt.time.tv_sec = 0;
288   evt.time.tv_usec = 0;
289   write( fd, &evt, sizeof(evt) );
290   SG_LOG( SG_INPUT, SG_DEBUG, "Written event " << eventName 
291           << " as type=" << evt.type << ", code=" << evt.code << " value=" << evt.value );
292 }
293
294 static char ugly_buffer[128];
295 const char * FGLinuxInputDevice::TranslateEventName( FGEventData & eventData ) 
296 {
297   FGLinuxEventData & linuxEventData = (FGLinuxEventData&)eventData;
298   TypeCode typeCode;
299   typeCode.type = linuxEventData.type;
300   typeCode.code = linuxEventData.code;
301   if( EVENT_NAME_BY_TYPE.count(typeCode) == 0 ) {
302     sprintf( ugly_buffer, "unknown-%u-%u", (unsigned)linuxEventData.type, (unsigned)linuxEventData.code );
303     return ugly_buffer;
304   }
305
306   return EVENT_NAME_BY_TYPE[typeCode];
307 }
308
309 void FGLinuxInputDevice::SetDevname( string name )
310 {
311   this->devname = name; 
312 }
313
314 FGLinuxEventInput::FGLinuxEventInput() : 
315   halcontext(NULL)
316 {
317 }
318
319 FGLinuxEventInput::~FGLinuxEventInput()
320 {
321   if( halcontext != NULL ) {
322     libhal_ctx_shutdown( halcontext, NULL);
323     libhal_ctx_free( halcontext );
324     halcontext = NULL;
325   }
326 }
327
328 #if 0
329 //TODO: enable hotplug support
330 static void DeviceAddedCallback (LibHalContext *ctx, const char *udi)
331 {
332   FGLinuxEventInput * linuxEventInput = (FGLinuxEventInput*)libhal_ctx_get_user_data (ctx);
333   linuxEventInput->AddHalDevice( udi );
334 }
335
336 static void DeviceRemovedCallback (LibHalContext *ctx, const char *udi)
337 {
338 }
339 #endif
340
341 void FGLinuxEventInput::init()
342 {
343   FGEventInput::init();
344
345   DBusConnection * connection;
346   DBusError dbus_error;
347
348   dbus_error_init(&dbus_error);
349   connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error);
350   if (dbus_error_is_set(&dbus_error)) {
351     SG_LOG( SG_INPUT, SG_ALERT, "Can't connect to system bus " << dbus_error.message);
352     dbus_error_free (&dbus_error);
353     return;
354   }
355
356   halcontext = libhal_ctx_new();
357
358   libhal_ctx_set_dbus_connection (halcontext, connection );
359   dbus_error_init (&dbus_error);
360
361   if( libhal_ctx_init( halcontext,  &dbus_error )) {
362
363       int num_devices = 0;
364       char ** devices = libhal_find_device_by_capability(halcontext, "input", &num_devices, NULL);
365
366       for ( int i = 0; i < num_devices; i++)
367         AddHalDevice( devices[i] );
368
369       libhal_free_string_array (devices);
370
371 //TODO: enable hotplug support
372 //      libhal_ctx_set_user_data( halcontext, this );
373 //      libhal_ctx_set_device_added( halcontext, DeviceAddedCallback );
374 //      libhal_ctx_set_device_removed( halcontext, DeviceRemovedCallback );
375     } else {
376       if(dbus_error_is_set (&dbus_error) ) {
377         SG_LOG( SG_INPUT, SG_ALERT, "Can't connect to hald: " << dbus_error.message);
378         dbus_error_free (&dbus_error);
379       } else {
380         SG_LOG( SG_INPUT, SG_ALERT, "Can't connect to hald." );
381       }
382
383       libhal_ctx_free (halcontext);
384       halcontext = NULL;
385     }
386 }
387
388 void FGLinuxEventInput::AddHalDevice( const char * udi )
389 {
390   char * device = libhal_device_get_property_string( halcontext, udi, "input.device", NULL);
391   char * product = libhal_device_get_property_string( halcontext, udi, "input.product", NULL);
392
393   if( product != NULL && device != NULL ) 
394     AddDevice( new FGLinuxInputDevice(product, device) );
395   else
396     SG_LOG( SG_INPUT, SG_ALERT, "Can't get device or product property of " << udi );
397
398   if( device != NULL ) libhal_free_string( device );
399   if( product != NULL ) libhal_free_string( product );
400
401 }
402
403 void FGLinuxEventInput::update( double dt )
404 {
405   FGEventInput::update( dt );
406
407   // index the input devices by the associated fd and prepare
408   // the pollfd array by filling in the file descriptor
409   struct pollfd fds[input_devices.size()];
410   map<int,FGLinuxInputDevice*> devicesByFd;
411   map<int,FGInputDevice*>::const_iterator it;
412   int i;
413   for( i=0, it = input_devices.begin(); it != input_devices.end(); ++it, i++ ) {
414     FGInputDevice* p = (*it).second;
415     int fd = ((FGLinuxInputDevice*)p)->GetFd();
416     fds[i].fd = fd;
417     fds[i].events = POLLIN;
418     devicesByFd[fd] = (FGLinuxInputDevice*)p;
419   }
420
421   int modifiers = fgGetKeyModifiers();
422   // poll all devices until no more events are in the queue
423   // do no more than maxpolls in a single loop to prevent locking
424   int maxpolls = 100;
425   while( maxpolls-- > 0 && ::poll( fds, i, 0 ) > 0 ) {
426     for( unsigned i = 0; i < sizeof(fds)/sizeof(fds[0]); i++ ) {
427       if( fds[i].revents & POLLIN ) {
428
429         // if this device reports data ready, it should be a input_event struct
430         struct input_event event;
431
432         if( read( fds[i].fd, &event, sizeof(event) ) != sizeof(event) )
433           continue;
434
435         FGLinuxEventData eventData( event, dt, modifiers );
436
437         // let the FGInputDevice handle the data
438         devicesByFd[fds[i].fd]->HandleEvent( eventData );
439       }
440     }
441   }
442 }