arm_linux.S for cache ops and random fixes
[libpicofe.git] / gp2x / usbjoy.c
index 569f615..d92e8f6 100644 (file)
@@ -34,6 +34,8 @@
 
 #include "usbjoy.h"
 
+/* This is a try to support analog joys. Untested. */
+#define DEAD_ZONE (8*1024)
 
 /*
   Function: joy_open
@@ -55,8 +57,8 @@ struct usbjoy *joy_open(int joynumber)
        char path [128];
        struct usbjoy * joy = NULL;
        struct js_event event;
+#ifdef __GP2X__
        static char insmod_done = 0;
-
        // notaz: on my system I get unresolved input_* symbols, so have to 'insmod input' too
        // also we should insmod only once, not on every joy_open() call.
        if (!insmod_done) {
@@ -64,13 +66,18 @@ struct usbjoy *joy_open(int joynumber)
                system ("insmod joydev"); // Loads joydev module
                insmod_done = 1;
        }
+#endif
 
        if (joynumber == 0) {
        }
        else if (joynumber > 0) {
                sprintf (path, "/dev/input/js%d", joynumber-1);
                fd = open(path, O_RDONLY, 0);
-               if (fd > 0) {
+               if (fd == -1) {
+                       sprintf (path, "/dev/js%d", joynumber-1);
+                       fd = open(path, O_RDONLY, 0);
+               }
+               if (fd != -1) {
                        joy = (struct usbjoy *) malloc(sizeof(*joy));
                        if (joy == NULL) { close(fd); return NULL; }
                        memset(joy, 0, sizeof(*joy));
@@ -210,14 +217,16 @@ int joy_update (struct usbjoy * joy) {
        switch (events[i].type & ~JS_EVENT_INIT) {
        case JS_EVENT_AXIS:
          if (events[i].number == 0) {
-           if (events[i].value == 0) joy->stateaxes[JOYLEFT] = joy->stateaxes[JOYRIGHT] = 0;
-           else if (events[i].value < 0) joy->stateaxes[JOYLEFT] = 1;
-           else joy->stateaxes[JOYRIGHT] = 1;
+           joy->stateaxes[JOYLEFT] = joy->stateaxes[JOYRIGHT] = 0;
+           if      (events[i].value < -DEAD_ZONE) joy->stateaxes[JOYLEFT] = 1;
+           else if (events[i].value >  DEAD_ZONE) joy->stateaxes[JOYRIGHT] = 1;
+           joy->axevals[0] = events[i].value;
          }
          else if (events[i].number == 1) {
-           if (events[i].value == 0) joy->stateaxes[JOYUP] = joy->stateaxes[JOYDOWN] = 0;
-           else if (events[i].value < 0) joy->stateaxes[JOYUP] = 1;
-           else joy->stateaxes[JOYDOWN] = 1;
+           joy->stateaxes[JOYUP] = joy->stateaxes[JOYDOWN] = 0;
+           if      (events[i].value < -DEAD_ZONE) joy->stateaxes[JOYUP] = 1;
+           else if (events[i].value >  DEAD_ZONE) joy->stateaxes[JOYDOWN] = 1;
+           joy->axevals[1] = events[i].value;
          }
          event = 1;
          break;
@@ -418,6 +427,7 @@ void gp2x_usbjoy_deinit (void) {
        int i;
        for (i=0; i<num_of_joys; i++) {
                joy_close (joys[i]);
+               joys[i] = NULL;
        }
        num_of_joys = 0;
 }