]> git.mxchange.org Git - flightgear.git/blobdiff - src/Input/FGLinuxEventInput.cxx
fix a pointer reference.
[flightgear.git] / src / Input / FGLinuxEventInput.cxx
index ec0f23da2c6b4843a751c80ddf09e48ac419e675..382a99c8aa0d2b9c5ef51997ae0d26a72e66d795 100644 (file)
@@ -198,10 +198,38 @@ static struct EventTypes {
 
 };
 
+static struct enbet {
+  unsigned type;
+  const char * name;
+} EVENT_NAMES_BY_EVENT_TYPE[] = {
+  { EV_SYN, "syn" },
+  { EV_KEY, "button" },
+  { EV_REL, "rel" },
+  { EV_ABS, "abs" },
+  { EV_MSC, "msc" },
+  { EV_SW, "button" },
+  { EV_LED, "led" },
+  { EV_SND, "snd" },
+  { EV_REP, "rep" },
+  { EV_FF, "ff" },
+  { EV_PWR, "pwr" },
+  { EV_FF_STATUS, "ff-status" }
+};
+
+
+class EventNameByEventType : public map<unsigned,const char*> {
+public:
+  EventNameByEventType() {
+    for( unsigned i = 0; i < sizeof(EVENT_NAMES_BY_EVENT_TYPE)/sizeof(EVENT_NAMES_BY_EVENT_TYPE[0]); i++ )
+      (*this)[EVENT_NAMES_BY_EVENT_TYPE[i].type] = EVENT_NAMES_BY_EVENT_TYPE[i].name;
+  }
+};
+static EventNameByEventType EVENT_NAME_BY_EVENT_TYPE;
+
 class EventNameByType : public map<TypeCode,const char*> {
 public:
   EventNameByType() {
-    for( int i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
+    for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
       (*this)[EVENT_TYPES[i].typeCode] = EVENT_TYPES[i].name;
   }
 };
@@ -217,7 +245,7 @@ struct ltstr {
 class EventTypeByName : public map<const char *,TypeCode,ltstr> {
 public:
   EventTypeByName() {
-    for( int i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
+    for( unsigned i = 0; i < sizeof(EVENT_TYPES)/sizeof(EVENT_TYPES[0]); i++ )
       (*this)[EVENT_TYPES[i].name] = EVENT_TYPES[i].typeCode;
   }
 };
@@ -226,9 +254,8 @@ static EventTypeByName EVENT_TYPE_BY_NAME;
 
 FGLinuxInputDevice::FGLinuxInputDevice( string aName, string aDevname ) :
   FGInputDevice(aName),
-  fd(-1),
-  devname( aDevname )
-
+  devname( aDevname ),
+  fd(-1)
 {
 }
 
@@ -252,11 +279,20 @@ void FGLinuxInputDevice::Open()
   if( (fd = ::open( devname.c_str(), O_RDWR )) == -1 ) { 
     throw exception();
   }
+
+  if( GetGrab() && ioctl( fd, EVIOCGRAB, 2 ) != 0 ) {
+    SG_LOG( SG_INPUT, SG_WARN, "Can't grab " << devname << " for exclusive access" );
+  }
 }
 
 void FGLinuxInputDevice::Close()
 {
-  if( fd != -1 ) ::close(fd);
+  if( fd != -1 ) {
+    if( GetGrab() && ioctl( fd, EVIOCGRAB, 0 ) != 0 ) {
+      SG_LOG( SG_INPUT, SG_WARN, "Can't ungrab " << devname );
+    }
+    ::close(fd);
+  }
   fd = -1;
 }
 
@@ -291,7 +327,13 @@ const char * FGLinuxInputDevice::TranslateEventName( FGEventData & eventData )
   typeCode.type = linuxEventData.type;
   typeCode.code = linuxEventData.code;
   if( EVENT_NAME_BY_TYPE.count(typeCode) == 0 ) {
-    sprintf( ugly_buffer, "unknown-%u-%u", (unsigned)linuxEventData.type, (unsigned)linuxEventData.code );
+    // event not known in translation tables
+    if( EVENT_NAME_BY_EVENT_TYPE.count(linuxEventData.type) == 0 ) {
+      // event type not known in translation tables
+      sprintf( ugly_buffer, "unknown-%u-%u", (unsigned)linuxEventData.type, (unsigned)linuxEventData.code );
+      return ugly_buffer;
+    }
+    sprintf( ugly_buffer, "%s-%u", EVENT_NAME_BY_EVENT_TYPE[linuxEventData.type], (unsigned)linuxEventData.code );
     return ugly_buffer;
   }
 
@@ -330,9 +372,9 @@ static void DeviceRemovedCallback (LibHalContext *ctx, const char *udi)
 }
 #endif
 
-void FGLinuxEventInput::init()
+void FGLinuxEventInput::postinit()
 {
-  FGEventInput::init();
+  FGEventInput::postinit();
 
   DBusConnection * connection;
   DBusError dbus_error;
@@ -415,7 +457,7 @@ void FGLinuxEventInput::update( double dt )
   // do no more than maxpolls in a single loop to prevent locking
   int maxpolls = 100;
   while( maxpolls-- > 0 && ::poll( fds, i, 0 ) > 0 ) {
-    for( int i = 0; i < sizeof(fds)/sizeof(fds[0]); i++ ) {
+    for( unsigned i = 0; i < sizeof(fds)/sizeof(fds[0]); i++ ) {
       if( fds[i].revents & POLLIN ) {
 
         // if this device reports data ready, it should be a input_event struct