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

joystick_linux.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: joystick_linux.cpp,v 1.1 2001/03/06 15:09:22 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #include "Core/precomp.h"
00016 
00017 #include <sys/ioctl.h>
00018 #include <fcntl.h>
00019 #include <unistd.h>
00020 #include <stdio.h>
00021 
00022 #include "API/Core/System/cl_assert.h"
00023 #include <API/Display/Input/inputaxis.h>
00024 #include <API/Display/Input/inputbutton.h>
00025 #include <Display/Input/X11/joystick_linux.h>
00026 #ifdef USE_JOY
00027 #include "joystick_linux.h"
00028 
00029 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
00030 
00031 CL_LinuxJoystick::CL_LinuxJoystick()
00032 {
00033         fd = -1;
00034         num_buttons = 0;
00035         num_axes = 0;
00036         axes = NULL;
00037         buttons = NULL;
00038 }
00039 
00040 CL_LinuxJoystick::~CL_LinuxJoystick()
00041 {
00042         if (fd != -1)
00043         {
00044                 close(fd);
00045         }
00046         
00047         delete[] axes;
00048         delete[] buttons;
00049 }
00050 
00051 bool CL_LinuxJoystick::init(int number)
00052 {
00053         cl_assert(fd == -1); // do not call init twice!
00054 
00055         char devname[10];
00056         sprintf( devname, "/dev/js%d", number );
00057         fd = open( devname, O_RDONLY | O_NONBLOCK );
00058         if (fd == -1) return false; // no joystick available
00059 
00060         ioctl( fd, JSIOCGBUTTONS, &num_buttons );
00061         ioctl( fd, JSIOCGAXES, &num_axes );
00062 /*
00063         cout << "Number of axes:     " << num_axes << endl;
00064         cout << "Number of buttons:  " << num_buttons << endl;
00065 */
00066         axes = new CL_LinuxJoystick_Axis[num_axes];
00067         buttons = new CL_LinuxJoystick_Button[num_buttons];
00068 
00069         return true;
00070 }
00071 
00072 void CL_LinuxJoystick::keep_alive()
00073 {       
00074         cl_assert(fd != -1); // init _MUST_ be called before update!
00075         
00076         while (read( fd, &jev, sizeof(js_event) ) != -1)
00077         {
00078                 switch (jev.type)
00079                 {
00080                 case JS_EVENT_AXIS:
00081                         axes[jev.number].set_value(jev.value);
00082                         break;
00083 
00084                 case JS_EVENT_BUTTON:
00085                         buttons[jev.number].set_value(jev.value);
00086                         break;
00087                 }
00088         }
00089 }
00090 
00091 CL_InputAxis *CL_LinuxJoystick::get_axis(int num)
00092 {
00093         cl_assert(num >= 0); // disallow negative values
00094 
00095         if (num >= num_axes) return NULL;
00096         return &axes[num];
00097 }
00098 
00099 CL_InputButton *CL_LinuxJoystick::get_button(int num)
00100 {
00101         cl_assert(num >= 0); // disallow negative values
00102 
00103         if (num >= num_buttons) return NULL;
00104         return &buttons[num];
00105 }
00106 #endif
00107 #endif

Generated at Wed Apr 4 19:54:01 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001