pad_linux.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002 GamePad RT-Component
00003 Copyright (c) 2010, Segway-Japan, Ltd.
00004 All rights reserved.
00005 
00006 Contact us if you use this software for sell.
00007 If you use this software not for sell, you can use this software under BSD lisence.
00008 See the files LICENSE.TXT and LICENSE-BSD.TXT for more details.                     
00009 ************************************************************************************/
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include "pad_linux.h"
00013 
00014 
00015 #include <sys/ioctl.h>
00016 #include <sys/time.h>
00017 #include <sys/types.h>
00018 #include <stdlib.h>
00019 #include <fcntl.h>
00020 #include <unistd.h>
00021 #include <stdio.h>
00022 #include <errno.h>
00023 #include <string.h>
00024 #include <linux/joystick.h>
00025 
00026 int fd = 0;
00027 unsigned char axes = 2;
00028 unsigned char buttons = 2;
00029 
00030 
00031 static bool usejoypad =false;//ysuzuki add
00032 int *axis = NULL;
00033 int *button = NULL;
00034 
00035 #define NAME_LENGTH 512
00036 
00037 
00038 
00039 
00040 int pad_sdl_js_open(const char* device)
00041 {
00042 
00043   int version = 0x000800;
00044   char name[NAME_LENGTH] = "Unknown";
00045 
00046   if ((fd = open(device, O_RDONLY)) < 0) {
00047     perror(device);
00048     exit(1);
00049   }
00050 
00051   ioctl(fd, JSIOCGVERSION, &version);
00052   ioctl(fd, JSIOCGAXES, &axes);
00053   ioctl(fd, JSIOCGBUTTONS, &buttons);
00054   ioctl(fd, JSIOCGNAME(NAME_LENGTH), name);
00055 
00056   fcntl(fd, F_SETFL, O_NONBLOCK);
00057 
00058   printf("Joystick (%s) has %d axes and %d buttons. Driver version is %d.%d.%d.\n",
00059          name, axes, buttons, version >> 16, (version >> 8) & 0xff, version & 0xff);
00060   //printf("Testing ... (interrupt to exit)\n");
00061   
00062   axis = (int*)calloc(axes, sizeof(int));
00063   button = (int*)calloc(buttons, sizeof(int));
00064 
00065   return 0;
00066 }
00067 
00068 void pad_sdl_js_close()
00069 {
00070   close(fd);
00071   fd = 0;
00072 }
00073 
00074 bool pad_sdl_js_isok()
00075 { 
00076   return(fd && usejoypad); 
00077   //return true;
00078 }
00079 
00080 void pad_sdl_js_setok(bool b)
00081 {
00082 }
00083 
00084 void pad_sdl_js_update()
00085 {
00086   if (fd) {
00087 
00088     int i;
00089     struct js_event js;
00090     
00091     for (int i = 0; i < 4; i++) {
00092       
00093       int r = 0;
00094       while (r < sizeof(struct js_event)) {
00095         r += read(fd, &js, sizeof(struct js_event) -r);
00096         switch(js.type & ~JS_EVENT_INIT) {
00097         case JS_EVENT_BUTTON:
00098           //ysuzuki debug printf("number value %d\t%d\n",js.number,js.value );
00099           button[js.number] = js.value;
00100 
00101 
00102           break;
00103         case JS_EVENT_AXIS:
00104           axis[js.number] = js.value;
00105           break;
00106 
00107         }
00108       }
00109     }
00110     //ysuzuki add-
00111     if(js.number==5 && js.value==1){
00112       usejoypad=!usejoypad;
00113       printf("pad:joypad %s\n", (usejoypad)? "enable" : "disable");
00114     }
00115     //-add
00116 
00117   }
00118 
00119 }
00120 
00121 int pad_sdl_js_axis(int n)
00122 {
00123   if (axis) {
00124     return axis[n];
00125   }
00126   return 0;
00127 }
00128 
00129 float pad_sdl_js_axisf(int n)
00130 {
00131   int j = pad_sdl_js_axis(n);
00132 
00133   return (float)((float)j / (float)PAD_SDL_JS_AXIS_MAX);
00134 }
00135 
00136 int pad_sdl_js_hat(int n)
00137 {
00138   return 0;
00139 }
00140 
00141 int pad_sdl_js_button(int n)
00142 {
00143   if (button && n < buttons) {
00144     return button[n];
00145   }
00146   return 0;
00147 }
00148 
00149 int pad_sdl_js_hat_push(int n)
00150 {
00151   return 0;
00152 }
00153 
00154 int pad_sdl_js_button_push(int n)
00155 {
00156   return 0;
00157 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


RS003
Author(s):
autogenerated on Thu Jun 27 2013 14:58:49