File: joystick_linux.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (107 lines) | stat: -rw-r--r-- 2,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
	$Id: joystick_linux.cpp,v 1.1 2001/03/06 15:09:22 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#include "Core/precomp.h"

#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

#include "API/Core/System/cl_assert.h"
#include <API/Display/Input/inputaxis.h>
#include <API/Display/Input/inputbutton.h>
#include <Display/Input/X11/joystick_linux.h>
#ifdef USE_JOY
#include "joystick_linux.h"

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)

CL_LinuxJoystick::CL_LinuxJoystick()
{
	fd = -1;
	num_buttons = 0;
	num_axes = 0;
	axes = NULL;
	buttons = NULL;
}

CL_LinuxJoystick::~CL_LinuxJoystick()
{
	if (fd != -1)
	{
		close(fd);
	}
	
	delete[] axes;
	delete[] buttons;
}

bool CL_LinuxJoystick::init(int number)
{
	cl_assert(fd == -1); // do not call init twice!

	char devname[10];
	sprintf( devname, "/dev/js%d", number );
	fd = open( devname, O_RDONLY | O_NONBLOCK );
	if (fd == -1) return false; // no joystick available

	ioctl( fd, JSIOCGBUTTONS, &num_buttons );
	ioctl( fd, JSIOCGAXES, &num_axes );
/*
	cout << "Number of axes:     " << num_axes << endl;
	cout << "Number of buttons:  " << num_buttons << endl;
*/
	axes = new CL_LinuxJoystick_Axis[num_axes];
	buttons = new CL_LinuxJoystick_Button[num_buttons];

	return true;
}

void CL_LinuxJoystick::keep_alive()
{	
	cl_assert(fd != -1); // init _MUST_ be called before update!
	
	while (read( fd, &jev, sizeof(js_event) ) != -1)
	{
		switch (jev.type)
		{
		case JS_EVENT_AXIS:
			axes[jev.number].set_value(jev.value);
			break;

		case JS_EVENT_BUTTON:
			buttons[jev.number].set_value(jev.value);
			break;
		}
	}
}

CL_InputAxis *CL_LinuxJoystick::get_axis(int num)
{
	cl_assert(num >= 0); // disallow negative values

	if (num >= num_axes) return NULL;
	return &axes[num];
}

CL_InputButton *CL_LinuxJoystick::get_button(int num)
{
	cl_assert(num >= 0); // disallow negative values

	if (num >= num_buttons) return NULL;
	return &buttons[num];
}
#endif
#endif