Mercurial > hg > old > magoroku_racing.bad
view sjoy.cc @ 139:00d7de7fe9df default tip
fix collision
author | Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 21 Jul 2011 22:49:57 +0900 |
parents | e5f2eb98b575 |
children |
line wrap: on
line source
// Copyright(C) 2001 Sony Computer Entertainment Inc. All Rights Reserved. // // "sjoy.c" // // #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <assert.h> #include "sjoy.h" #define N_JOY 2 static const char *g_devName[N_JOY] = { "/dev/js0", "/dev/js1", }; static int g_fd[N_JOY] = { -1, -1, }; //static __u32 g_button[N_JOY]; // max 32 buttons per joystick //static __s16 g_axis[N_JOY][2]; // max 2 axis per joystick //---------------------------------------------------------------------- int sjoy_open(void) { int joy; sjoy_close(); for (joy = 0; joy < N_JOY; joy++) { assert(g_fd[joy] == -1); g_fd[joy] = open(g_devName[joy], O_RDONLY | O_NONBLOCK); if (g_fd[joy] < 0) { fprintf(stderr, "can't open %s\n", g_devName[joy]); fprintf(stderr, "You don't have permission, or should load module for joysticks.\n" "How to load joystick module:\n" " # modprobe ps2pad\n"); return -1; } } return 0; } //---------------------------------------------------------------------- int sjoy_close(void) { int joy; int fail = 0; for (joy = 0; joy < N_JOY; joy++) { if (g_fd[joy] >= 0) { fail |= close(g_fd[joy]); } g_fd[joy] = -1; } return fail ? -1 : 0; } /* 適当に自作:miya */ struct js_event { int type; int number; int value; }; int g_button[10]; int g_axis[10][10]; #define JS_EVENT_INIT 1 #define JS_EVENT_BUTTON 2 #define JS_EVENT_AXIS 3 /* ここまで適当に自作:miya */ //---------------------------------------------------------------------- void sjoy_poll(void) { int joy; for (joy = 0; joy < N_JOY; joy++) { if (g_fd[joy] < 0) { continue; } for (; ;) { struct js_event e; int n = read(g_fd[joy], &e, sizeof(e)); if (n != sizeof(e)) { break; } switch (e.type & ~JS_EVENT_INIT) { case JS_EVENT_BUTTON: g_button[joy] &= ~(1 << e.number); g_button[joy] |= (e.value << e.number); break; case JS_EVENT_AXIS: g_axis[joy][e.number] = e.value; break; default: assert(0); break; } } } } //---------------------------------------------------------------------- int sjoy_get_button(int joy) { return g_button[joy]; } //---------------------------------------------------------------------- int sjoy_get_axis(int joy, int axis) { return g_axis[joy][axis]; } //---------------------------------------------------------------------- int sjoy_get_ps2_button(int joy) { int w = g_button[joy]; int a0 = g_axis[joy][0]; int a1 = g_axis[joy][1]; int th = 0x4000; w |= (a0 < -th) ? SJOY_PS2_L_LEFT : 0; w |= (a1 > th) ? SJOY_PS2_L_DOWN : 0; w |= (a0 > th) ? SJOY_PS2_L_RIGHT : 0; w |= (a1 < -th) ? SJOY_PS2_L_UP : 0; return w; }