Skip to content

Commit c53d9ee

Browse files
committed
PC: implement joystick support
Use glfw to get the joystick position. Add a new global variable `joystick` which is set to the active joystick ID (or to -1, if no joysticks are detected). For simplicity, for now we pick the first available joystick, but later we could make it configurable.
1 parent 8b92af9 commit c53d9ee

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

source/platform/input.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern GLFWwindow* window;
3232
static bool input_pointer_enabled;
3333
static double input_old_pointer_x, input_old_pointer_y;
3434
static bool input_key_held[1024];
35+
static int joystick = -1;
3536

3637
void input_init() {
3738
for(int k = 0; k < 1024; k++)
@@ -40,6 +41,27 @@ void input_init() {
4041
input_pointer_enabled = false;
4142
input_old_pointer_x = 0;
4243
input_old_pointer_y = 0;
44+
45+
// Find joystick
46+
glfwInit();
47+
for(int joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) {
48+
int num_axes = 0;
49+
const float *axes = glfwGetJoystickAxes(joy, &num_axes);
50+
if(num_axes < 2) continue;
51+
52+
/* Workaround some buggy motherboard which reports the LED controller
53+
* as a joystick; in such cases the controller is reported as having a
54+
* ridiculously high number of axes. See for example:
55+
* https://bbs.archlinux.org/viewtopic.php?id=261161
56+
*/
57+
if (num_axes >= 10) {
58+
fprintf(stderr, "Skipping controller '%s' with %d axes\n",
59+
glfwGetJoystickName(joy), num_axes);
60+
continue;
61+
}
62+
joystick = joy;
63+
break;
64+
}
4365
}
4466

4567
void input_poll() { }
@@ -103,16 +125,23 @@ bool input_pointer(float* x, float* y, float* angle) {
103125
}
104126

105127
void input_native_joystick(float dt, float* dx, float* dy) {
128+
*dx = 0.0F;
129+
*dy = 0.0F;
106130
if(!input_pointer_enabled) {
107131
double x2, y2;
108132
glfwGetCursorPos(window, &x2, &y2);
109133
*dx = (x2 - input_old_pointer_x) * 0.001F;
110134
*dy = -(y2 - input_old_pointer_y) * 0.001F;
111135
input_old_pointer_x = x2;
112136
input_old_pointer_y = y2;
113-
} else {
114-
*dx = 0.0F;
115-
*dy = 0.0F;
137+
} else if(joystick >= 0) {
138+
int num_axes = 0;
139+
const float *axes = glfwGetJoystickAxes(joystick, &num_axes);
140+
if(axes && num_axes >= 2) {
141+
fprintf(stderr, "Axes: %f, %f\n", axes[0], axes[1]);
142+
*dx = axes[0] * dt;
143+
*dy = -axes[1] * dt;
144+
}
116145
}
117146
}
118147

0 commit comments

Comments
 (0)