Re: [AD] Ouya controller support

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


If the idea is ok, here’s a patch. I only tested it on Ouya and not desktop Linux. I had to change the call to inotify_init1 to inotify_init with an fcntl since Ouya doesn’t have that function or constant (IN_NONBLOCK.)
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 347465d..627c86c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -443,7 +443,6 @@ check_include_files(machine/soundcard.h ALLEGRO_HAVE_MACHINE_SOUNDCARD_H)
check_include_files(linux/soundcard.h ALLEGRO_HAVE_LINUX_SOUNDCARD_H)
check_include_files(libkern/OSAtomic.h ALLEGRO_HAVE_OSATOMIC_H)
check_include_files(sys/inotify.h ALLEGRO_HAVE_SYS_INOTIFY_H)
-check_include_files(sys/timerfd.h ALLEGRO_HAVE_SYS_TIMERFD_H)
 
check_function_exists(getexecname ALLEGRO_HAVE_GETEXECNAME)
check_function_exists(mkstemp ALLEGRO_HAVE_MKSTEMP)
diff --git a/include/allegro5/platform/alplatf.h.cmake b/include/allegro5/platform/alplatf.h.cmake
index 9c26764..c7d41a7 100644
--- a/include/allegro5/platform/alplatf.h.cmake
+++ b/include/allegro5/platform/alplatf.h.cmake
@@ -49,7 +49,6 @@
#cmakedefine ALLEGRO_HAVE_SYS_TYPES_H
#cmakedefine ALLEGRO_HAVE_OSATOMIC_H
#cmakedefine ALLEGRO_HAVE_SYS_INOTIFY_H
-#cmakedefine ALLEGRO_HAVE_SYS_TIMERFD_H
 
/* Define to 1 if the corresponding functions are available. */
#cmakedefine ALLEGRO_HAVE_GETEXECNAME
diff --git a/src/linux/ljoynu.c b/src/linux/ljoynu.c
index 928640e..97668a5 100644
--- a/src/linux/ljoynu.c
+++ b/src/linux/ljoynu.c
@@ -42,10 +42,9 @@
#include <sys/types.h>
#include <linux/input.h>
 
-#if defined(ALLEGRO_HAVE_SYS_INOTIFY_H) && defined(ALLEGRO_HAVE_SYS_TIMERFD_H)
+#if defined(ALLEGRO_HAVE_SYS_INOTIFY_H)
    #define SUPPORT_HOTPLUG
    #include <sys/inotify.h>
-   #include <sys/timerfd.h>
#endif
 
ALLEGRO_DEBUG_CHANNEL("ljoy");
@@ -127,9 +126,34 @@ static volatile bool config_needs_merging;
static ALLEGRO_MUTEX *config_mutex;
#ifdef SUPPORT_HOTPLUG
static int inotify_fd = -1;
-static int timer_fd = -1;
-#endif
 
+static ALLEGRO_THREAD *hotplug_thread;
+static ALLEGRO_MUTEX *hotplug_mutex;
+static ALLEGRO_COND *hotplug_cond;
+static bool hotplug_ended = false;
+static void ljoy_scan(bool configure);
+
+static void *hotplug_proc(ALLEGRO_THREAD *thread, void *data)
+{
+   (void)data;
+
+   while (!al_get_thread_should_stop(thread)) {
+      if (!hotplug_ended) {
+         al_wait_cond(hotplug_cond, hotplug_mutex);
+      }
+
+      al_rest(1);
+
+      al_lock_mutex(config_mutex);
+      ljoy_scan(true);
+      al_unlock_mutex(config_mutex);
+   }
+
+   hotplug_ended = false;
+
+   return NULL;
+}
+#endif
 
 
/* Return true if a joystick-related button or key:
@@ -595,7 +619,6 @@ static void ljoy_merge(void)
static void ljoy_config_dev_changed(void *data)
{
    char buf[128];
-   struct itimerspec spec;
    (void)data;
 
    /* Empty the event buffer. We only care that some inotify event was sent but it
@@ -605,33 +628,7 @@ static void ljoy_config_dev_changed(void *data)
    while (read(inotify_fd, buf, sizeof(buf)) > 0) {
    }
 
-   /* Set the timer to fire once in one second.
-    * We cannot scan immediately because the devices may not be ready yet :-P
-    */
-   spec.it_value.tv_sec = 1;
-   spec.it_value.tv_nsec = 0;
-   spec.it_interval.tv_sec = 0;
-   spec.it_interval.tv_nsec = 0;
-   timerfd_settime(timer_fd, 0, &spec, NULL);
-}
-
-
-
-/* ljoy_config_rescan: [fdwatch thread]
- *  Rescans for joystick devices a little while after devices change.
- */
-static void ljoy_config_rescan(void *data)
-{
-   uint64_t exp;
-   (void)data;
-
-   /* Empty the event buffer. */
-   while (read(timer_fd, &exp, sizeof(uint64_t)) > 0) {
-   }
-
-   al_lock_mutex(config_mutex);
-   ljoy_scan(true);
-   al_unlock_mutex(config_mutex);
+   al_signal_cond(hotplug_cond);
}
#endif
 
@@ -652,13 +649,12 @@ static bool ljoy_init_joystick(void)
    config_mutex = al_create_mutex();
 
#ifdef SUPPORT_HOTPLUG
-   inotify_fd = inotify_init1(IN_NONBLOCK);
-   timer_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
-   if (inotify_fd != -1 && timer_fd != -1) {
+   inotify_fd = inotify_init();
+   if (inotify_fd != -1) {
+      fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
       /* Modern Linux probably only needs to monitor /dev/input. */
       inotify_add_watch(inotify_fd, "/dev/input", IN_CREATE|IN_DELETE);
       _al_unix_start_watching_fd(inotify_fd, ljoy_config_dev_changed, NULL);
-      _al_unix_start_watching_fd(timer_fd, ljoy_config_rescan, NULL);
       ALLEGRO_INFO("Hotplugging enabled\n");
    }
    else {
@@ -667,11 +663,12 @@ static bool ljoy_init_joystick(void)
          close(inotify_fd);
          inotify_fd = -1;
       }
-      if (timer_fd != -1) {
-         close(timer_fd);
-         timer_fd = -1;
-      }
    }
+
+   hotplug_mutex = al_create_mutex();
+   hotplug_cond = al_create_cond();
+   hotplug_thread = al_create_thread(hotplug_proc, NULL);
+   al_start_thread(hotplug_thread);
#endif
 
    return true;
@@ -692,11 +689,9 @@ static void ljoy_exit_joystick(void)
       close(inotify_fd);
       inotify_fd = -1;
    }
-   if (timer_fd != -1) {
-      _al_unix_stop_watching_fd(timer_fd);
-      close(timer_fd);
-      timer_fd = -1;
-   }
+   hotplug_ended = true;
+   al_signal_cond(hotplug_cond);
+   al_join_thread(hotplug_thread, NULL);
#endif
 
    al_destroy_mutex(config_mutex);
 
Trent
 
Sent: Saturday, July 13, 2013 4:28 PM
Subject: Re: [AD] Ouya controller support
 
I just looked too, and you’re right. It looks like it would be simple to use a thread for this. I guess the idea was to avoid the overhead of a thread? Peter is that an OK solution?

Trent
 
Sent: Saturday, July 13, 2013 4:19 PM
Subject: Re: [AD] Ouya controller support
 
I gave it a quick look earlier. Seems like a timer file descriptor is just a fancy timer that starts a new poll for joysticks. Maybe it's possible to use inotify to watch changes of the /dev/input directory and skip timer based polling. Inotify is already working for the individual devices.


2013/7/14 Trent Gamblin <nooskewl@xxxxxxxxxx>
I tried copying timerfd headers from a Raspberry Pi, but the actual function calls do not exist in the libraries on the Ouya (I get runtime errors about missing timerfd_* functions.) So another approach is needed. I’m not too familiar with glob()... I’ll have to look into it some more, see what the timerfd stuff in Allegro is really doing since I don’t really know yet.

Trent
 
Subject: Re: [AD] Ouya controller support
 
Op 13-07-13 21:13, Trent Gamblin schreef:
Slight problem here. Peter, is it possible to do hotplugging without sys/timerfd.h? It’s not in the latest NDK (or earlier.) Without it the joysticks eventually shut off for power saving and then the only thing you can do is force close the game.

Trent


The Linux kernel of Android should /probably/ still support the ioctl system calls, so we could just copy the header(s) from a desktop linux and try those. Otherwise, if that fails, we'll have to set up our own timer and poll /dev/input/eventXXX with glob() .  hope we won't heed to, but there's always the hidraw interface as well,... Then again we may need hidraw fpr mapping of the player number to the joystick, as the OUYA event interface doesn' t seem to support LED IO... 

Kind Regards,

B.


------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
--
https://lists.sourceforge.net/lists/listinfo/alleg-developers

------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
--
https://lists.sourceforge.net/lists/listinfo/alleg-developers
 


------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk


--
https://lists.sourceforge.net/lists/listinfo/alleg-developers


------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk


--
https://lists.sourceforge.net/lists/listinfo/alleg-developers


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/