[PATCH xf86-input-joystick 3/3] Add support for server managed fds

Hans de Goede hdegoede at redhat.com
Thu Mar 13 06:49:53 PDT 2014


Keep things simple by handling server managed fds in the common parts
of the open and close paths.

Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 src/backend_bsd.c      | 10 ++++++----
 src/backend_evdev.c    | 10 ++++++----
 src/backend_joystick.c | 10 ++++++----
 src/jstk.c             | 19 +++++++++++++++----
 src/jstk.h             |  4 ++++
 src/jstk_key.c         |  3 ++-
 6 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/src/backend_bsd.c b/src/backend_bsd.c
index f3f501e..d8c6bac 100644
--- a/src/backend_bsd.c
+++ b/src/backend_bsd.c
@@ -91,10 +91,12 @@ jstkOpenDevice_bsd(JoystickDevPtr joystick, Bool probe)
     report_desc_t rd;
     struct jstk_bsd_hid_data *bsddata;
 
-    if ((joystick->fd = open(joystick->device, O_RDWR | O_NDELAY, 0)) < 0) {
-        xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", joystick->device,
-                strerror(errno));
-        return -1;
+    if (joystick->fd == -1) {
+        if ((joystick->fd = open(joystick->device, O_RDWR | O_NDELAY, 0)) < 0) {
+            xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n",
+                    joystick->device, strerror(errno));
+            return -1;
+        }
     }
 
     if ((rd = hid_get_report_desc(joystick->fd)) == 0) {
diff --git a/src/backend_evdev.c b/src/backend_evdev.c
index 2a7b0e2..cae4b28 100644
--- a/src/backend_evdev.c
+++ b/src/backend_evdev.c
@@ -90,10 +90,12 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe)
     unsigned long key_bits[NBITS(KEY_MAX)];
     int axes, buttons, j;
 
-    if ((joystick->fd = open(joystick->device, O_RDONLY | O_NDELAY, 0)) < 0) {
-        xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", 
-                joystick->device, strerror(errno));
-        return -1;
+    if (joystick->fd == -1) {
+        if ((joystick->fd = open(joystick->device, O_RDONLY | O_NDELAY, 0)) < 0) {
+            xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n",
+                    joystick->device, strerror(errno));
+            return -1;
+        }
     }
 
     if (ioctl(joystick->fd, EVIOCGVERSION, &driver_version) == -1) {
diff --git a/src/backend_joystick.c b/src/backend_joystick.c
index d335bc2..172ce50 100644
--- a/src/backend_joystick.c
+++ b/src/backend_joystick.c
@@ -72,10 +72,12 @@ jstkOpenDevice_joystick(JoystickDevPtr joystick, Bool probe)
     unsigned char axes, buttons;
     int driver_version;
 
-    if ((joystick->fd = open(joystick->device, O_RDONLY | O_NDELAY, 0)) < 0) {
-        xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n", 
-                joystick->device, strerror(errno));
-        return -1;
+    if (joystick->fd == -1) {
+        if ((joystick->fd = open(joystick->device, O_RDONLY | O_NDELAY, 0)) < 0) {
+            xf86Msg(X_ERROR, "Cannot open joystick '%s' (%s)\n",
+                    joystick->device, strerror(errno));
+            return -1;
+        }
     }
 
     if (ioctl(joystick->fd, JSIOCGVERSION, &driver_version) == -1) {
diff --git a/src/jstk.c b/src/jstk.c
index 211fe32..6cf0ef9 100644
--- a/src/jstk.c
+++ b/src/jstk.c
@@ -80,6 +80,9 @@ jstkOpenDevice(JoystickDevPtr priv, BOOL probe)
     int fd;
     fd = -1;
 
+    if (priv->joystick_device->flags & XI86_SERVER_FD)
+        priv->fd = priv->joystick_device->fd;
+
     if (probe == FALSE && priv->open_proc)
         return priv->open_proc(priv, probe);
 
@@ -112,7 +115,8 @@ jstkOpenDevice(JoystickDevPtr priv, BOOL probe)
 void jstkCloseDevice(JoystickDevPtr priv)
 {
     if ((priv->fd >= 0)) {
-      xf86CloseSerial(priv->fd);
+      if (!(priv->joystick_device->flags & XI86_SERVER_FD))
+          xf86CloseSerial(priv->fd);
       priv->fd = -1;
     }
 }
@@ -434,7 +438,8 @@ jstkDeviceControlProc(DeviceIntPtr       pJstk,
 
         if (pInfo->fd >= 0)
             RemoveEnabledDevice(pInfo->fd);
-        pInfo->fd = -1;
+        if (!(pInfo->flags & XI86_SERVER_FD))
+            pInfo->fd = -1;
         if (priv->close_proc)
             priv->close_proc(priv);
         pJstk->public.on = FALSE;
@@ -485,7 +490,8 @@ jstkCorePreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
     pInfo->read_input = jstkReadProc;
     pInfo->control_proc = NULL;
     pInfo->switch_mode = NULL;
-    pInfo->fd = -1;
+    if (!(pInfo->flags & XI86_SERVER_FD))
+        pInfo->fd = -1;
     pInfo->dev = NULL;
     pInfo->type_name = XI_JOYSTICK;
 
@@ -506,6 +512,7 @@ jstkCorePreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
     priv->mouse_enabled = TRUE;
     priv->keys_enabled = TRUE;
     priv->amplify = 1.0f;
+    priv->joystick_device = pInfo;
     priv->keyboard_device = keyboard_device;
     priv->num_axes    = MAXAXES;
     priv->num_buttons = MAXBUTTONS;
@@ -682,7 +689,11 @@ _X_EXPORT InputDriverRec JOYSTICK = {
     NULL,
     jstkCorePreInit,
     jstkCoreUnInit,
-    NULL
+    NULL,
+    NULL,
+#ifdef XI86_DRV_CAP_SERVER_FD
+    XI86_DRV_CAP_SERVER_FD
+#endif
 };
 
 /*
diff --git a/src/jstk.h b/src/jstk.h
index 4955435..6421e7f 100644
--- a/src/jstk.h
+++ b/src/jstk.h
@@ -55,6 +55,9 @@
 #define XI_JOYSTICK "JOYSTICK"
 #endif
 
+#ifndef XI86_SERVER_FD
+#define XI86_SERVER_FD 0x20
+#endif
 
 typedef enum _JOYSTICKEVENT {
     EVENT_NONE=0,
@@ -106,6 +109,7 @@ typedef struct _JoystickDevRec {
     jstkReadDataProc read_proc; /* Callback for reading data from the backend */
     void         *devicedata; /* Extra platform device dependend data */
     char         *device;     /* Name of the device */
+    InputInfoPtr joystick_device; /* Back pointer to the joystick device */
     InputInfoPtr keyboard_device; /* Slave device for keyboard events */
 
     OsTimerPtr   timer;       /* Timer for axis movement */
diff --git a/src/jstk_key.c b/src/jstk_key.c
index 241b15d..5a4ed8e 100644
--- a/src/jstk_key.c
+++ b/src/jstk_key.c
@@ -200,7 +200,8 @@ int jstkKeyboardPreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
     pInfo->read_input = NULL;
     pInfo->control_proc = NULL;
     pInfo->switch_mode = NULL;
-    pInfo->fd = -1;
+    if (!(pInfo->flags & XI86_SERVER_FD))
+        pInfo->fd = -1;
     pInfo->type_name = XI_JOYSTICK;
 
     /* parse keyboard-related options */
-- 
1.9.0



More information about the xorg-devel mailing list