Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: c8f7f9ddc4a86b7f06206d17f0d64330d48c1948 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
   client/joystick.cc
   This file is part of the Osirion project and is distributed under
   the terms and conditions of the GNU General Public License version 2
*/

#include "SDL/SDL.h"

#include "auxiliary/functions.h"
#include "client/joystick.h"
#include "core/cvar.h"
#include "sys/sys.h"

namespace client
{

core::Cvar *input_joystick = 0;
int current_joystick_number = 0;
SDL_Joystick *current_joystick = 0;

void Joystick::init()
{
	SDL_InitSubSystem(SDL_INIT_JOYSTICK);

	int nbjoysticks = SDL_NumJoysticks();
	if (nbjoysticks) {
		for (int i = 0; i < nbjoysticks; i++) {
			SDL_Joystick *joystick = SDL_JoystickOpen(i);
			if (joystick) {
				con_print << "  joystick " << i + 1 << ": " <<
				SDL_JoystickName(i) << " " <<
				SDL_JoystickNumAxes(joystick) << " axes " <<
				SDL_JoystickNumButtons(joystick) << " buttons " << std::endl;

				SDL_JoystickClose(joystick);
			}
		}
	}

	input_joystick = core::Cvar::get("input_joystick", 0.0f, core::Cvar::Archive);
	input_joystick->set_info("[int] set joystick number to use");

	current_joystick_number = 0;
	SDL_JoystickEventState(SDL_IGNORE);
}

void Joystick::shutdown()
{
	if (current_joystick) {
		SDL_JoystickEventState(SDL_IGNORE);
		SDL_JoystickClose(current_joystick);
		current_joystick = 0;
	}

	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
}

void Joystick::list()
{
	if (current_joystick) {
		SDL_JoystickEventState(SDL_IGNORE);
		SDL_JoystickClose(current_joystick);
		current_joystick = 0;
	}
	/*
	// reset makes glorious segfaults
	//SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
	//SDL_InitSubSystem(SDL_INIT_JOYSTICK);
	*/
	int nbjoysticks = SDL_NumJoysticks();
	if (nbjoysticks) {
		for (int i = 0; i < nbjoysticks; i++) {
			SDL_Joystick *joystick = SDL_JoystickOpen(i);
			if (joystick) {
				con_print << "  joystick " << i + 1 << ": " <<
				SDL_JoystickName(i) << " " <<
				SDL_JoystickNumAxes(joystick) << " axes " <<
				SDL_JoystickNumButtons(joystick) << " buttons " << std::endl;

				SDL_JoystickClose(joystick);
			}
		}
	}

	current_joystick_number = (int) input_joystick->value();
	if ((current_joystick_number < 1) || (current_joystick_number > nbjoysticks)) {
		current_joystick_number = 0;
	}
	(*input_joystick) = (float) current_joystick_number;

	if (current_joystick_number) {
		current_joystick = SDL_JoystickOpen(current_joystick_number - 1);
	}

	if (current_joystick) {
		con_debug << "  using joystick " << SDL_JoystickName(current_joystick_number - 1) << std::endl;
		SDL_JoystickEventState(SDL_ENABLE);
	}
}

bool Joystick::is_enabled()
{
	return (current_joystick != 0);
}

void Joystick::frame()
{
	if (current_joystick_number != (int) input_joystick->value()) {
		if (current_joystick) {
			SDL_JoystickEventState(SDL_IGNORE);
			SDL_JoystickClose(current_joystick);
			current_joystick = 0;
		}

		current_joystick_number = (int) input_joystick->value();
		if ((current_joystick_number < 1) || (current_joystick_number > SDL_NumJoysticks())) {
			current_joystick_number = 0;
		}
		(*input_joystick) = (float) current_joystick_number;

		if (current_joystick_number) {
			current_joystick = SDL_JoystickOpen(current_joystick_number - 1);
		}

		if (current_joystick) {
			con_debug << "  using joystick " << SDL_JoystickName(current_joystick_number - 1) << std::endl;
			SDL_JoystickEventState(SDL_ENABLE);
		}
	}
}

}