Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

sjoy.c

Go to the documentation of this file.
00001 // Copyright(C) 2001 Sony Computer Entertainment Inc. All Rights Reserved.
00002 //
00003 // "sjoy.c"
00004 //
00005 //
00006 #include <stdio.h>
00007 #include <unistd.h>
00008 #include <fcntl.h>
00009 #include <assert.h>
00010 #include <linux/types.h>
00011 #include <linux/joystick.h>
00012 #include "sjoy.h"
00013 
00014 #define N_JOY   2
00015 
00016 static char *g_devName[N_JOY] = {
00017         "/dev/js0",
00018         "/dev/js1",
00019 };
00020 
00021 static int g_fd[N_JOY] = {
00022         -1, -1,
00023 };
00024 
00025 static __u32 g_button[N_JOY];   // max 32 buttons per joystick
00026 static __s16 g_axis[N_JOY][2];  // max 2 axis per joystick
00027 
00028 //----------------------------------------------------------------------
00029 int sjoy_open(void)
00030 {
00031         int joy;
00032         
00033         sjoy_close();
00034         
00035         for (joy = 0; joy < N_JOY; joy++) {
00036                 assert(g_fd[joy] == -1);
00037                 g_fd[joy] = open(g_devName[joy], O_RDONLY | O_NONBLOCK);
00038                 if (g_fd[joy] < 0) {
00039                         fprintf(stderr, "can't open %s\n", g_devName[joy]);
00040                         fprintf(stderr,
00041                                         "You don't have permission, or should load module for joysticks.\n"
00042                                         "How to load joystick module:\n"
00043                                         "    # modprobe ps2pad\n");
00044                         return -1;
00045                 }
00046         }
00047         
00048         return 0;
00049 }
00050 
00051 //----------------------------------------------------------------------
00052 int sjoy_close(void)
00053 {
00054         int joy;
00055         int fail = 0;
00056         
00057         for (joy = 0; joy < N_JOY; joy++) {
00058                 if (g_fd[joy] >= 0) {
00059                         fail |= close(g_fd[joy]);
00060                 }
00061                 g_fd[joy] = -1;
00062         }
00063         
00064         return fail ? -1 : 0;
00065 }
00066 
00067 //----------------------------------------------------------------------
00068 void sjoy_poll(void)
00069 {
00070         int joy;
00071         
00072         for (joy = 0; joy < N_JOY; joy++) {
00073                 if (g_fd[joy] < 0) {
00074                         continue;
00075                 }
00076                 for (; ;) {
00077                         struct js_event e;
00078                         int n = read(g_fd[joy], &e, sizeof(e));
00079                         if (n != sizeof(e)) {
00080                                 break;
00081                         }
00082                         switch (e.type & ~JS_EVENT_INIT) {
00083                         case JS_EVENT_BUTTON:
00084                                 g_button[joy] &= ~(1 << e.number);
00085                                 g_button[joy] |= (e.value << e.number);
00086                                 break;
00087                                 
00088                         case JS_EVENT_AXIS:
00089                                 g_axis[joy][e.number] = e.value;
00090                                 break;
00091                                 
00092                         default:
00093                                 assert(0);
00094                                 break;
00095                         }
00096                 }
00097         }
00098 }
00099 
00100 //----------------------------------------------------------------------
00101 int sjoy_get_button(int joy)
00102 {
00103         return g_button[joy];
00104 }
00105 
00106 //----------------------------------------------------------------------
00107 int sjoy_get_axis(int joy, int axis)
00108 {
00109         return g_axis[joy][axis];
00110 }
00111 
00112 //----------------------------------------------------------------------
00113 int sjoy_get_ps2_button(int joy)
00114 {
00115         int w = g_button[joy];
00116         int a0 = g_axis[joy][0];
00117         int a1 = g_axis[joy][1];
00118         int th = 0x4000;
00119         
00120         w |= (a0 < -th) ? SJOY_PS2_L_LEFT : 0;
00121         w |= (a1 > th) ? SJOY_PS2_L_DOWN : 0;
00122         w |= (a0 > th) ? SJOY_PS2_L_RIGHT : 0;
00123         w |= (a1 < -th) ? SJOY_PS2_L_UP : 0;
00124         
00125         return w;
00126 }

ps2gl version cvs