New input system with automatic joystick detection
[tennix.git] / input.c
blob098bfe41efd9a7b69dcb31e2ea9fb21679e52ee6
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include <string.h>
26 #include "tennix.h"
27 #include "input.h"
29 static InputDevice devices[MAX_INPUT_DEVICES];
30 static int devices_count;
32 void init_input()
34 int n, x;
36 SDL_JoystickEventState(SDL_ENABLE);
38 /* keyboard presets */
39 devices[devices_count].type = INPUT_TYPE_KEYBOARD;
40 devices[devices_count].up_key = 'w';
41 devices[devices_count].down_key = 's';
42 devices[devices_count].input_keys[INPUT_KEY_HIT]= 'd';
43 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 'e';
44 devices[devices_count].input_keys[INPUT_KEY_SMASH] = 'f';
45 strcpy(devices[devices_count].name, "Keyboard (WS/DEF)");
46 devices_count++;
48 /* keyboard presets */
49 devices[devices_count].type = INPUT_TYPE_KEYBOARD;
50 devices[devices_count].up_key = 'o';
51 devices[devices_count].down_key = 'l';
52 devices[devices_count].input_keys[INPUT_KEY_HIT]= 'k';
53 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 'i';
54 devices[devices_count].input_keys[INPUT_KEY_SMASH] = 'j';
55 strcpy(devices[devices_count].name, "Keyboard (OL/KIJ)");
56 devices_count++;
58 /* keyboard presets */
59 devices[devices_count].type = INPUT_TYPE_KEYBOARD;
60 devices[devices_count].up_key = SDLK_UP;
61 devices[devices_count].down_key = SDLK_DOWN;
62 devices[devices_count].input_keys[INPUT_KEY_HIT]= SDLK_SPACE;
63 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = SDLK_LCTRL;
64 devices[devices_count].input_keys[INPUT_KEY_SMASH] = SDLK_LALT;
65 strcpy(devices[devices_count].name, "Keyboard (arrows + space, lctrl, lalt)");
66 devices_count++;
68 /* joysticks */
69 n = SDL_NumJoysticks();
70 for (x=0; x<n && devices_count<MAX_INPUT_DEVICES; x++) {
71 strcpy(devices[devices_count].name, SDL_JoystickName(x));
72 devices[devices_count].type = INPUT_TYPE_JOYSTICK;
73 devices[devices_count].joystick = SDL_JoystickOpen(x);
74 devices[devices_count].x_axis = 0;
75 devices[devices_count].y_axis = 0;
76 devices[devices_count].input_keys[INPUT_KEY_HIT]= 0;
77 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 1;
78 devices[devices_count].input_keys[INPUT_KEY_SMASH] = 2;
79 devices_count++;
83 void uninit_input()
85 int i;
86 SDL_JoystickEventState(SDL_IGNORE);
88 for (i=0; i<devices_count; i++) {
89 if (devices[i].type == INPUT_TYPE_JOYSTICK) {
90 SDL_JoystickClose(devices[i].joystick);
93 devices_count = 0;
97 InputDevice* find_input_devices(unsigned int* count)
99 *count = devices_count;
100 return devices;
103 const char* input_device_get_name(InputDevice* d)
105 return d->name;
108 float input_device_get_axis(InputDevice* d, unsigned const char axis) {
109 Uint8 *keystate;
110 SDL_PumpEvents();
112 if (d->type == INPUT_TYPE_KEYBOARD) {
113 keystate = SDL_GetKeyState(NULL);
114 if (axis == INPUT_AXIS_X) {
115 return 1.0*keystate[d->right_key] + -1.0*keystate[d->left_key];
116 } else {
117 return 1.0*keystate[d->down_key] + -1.0*keystate[d->up_key];
119 } else if (d->type == INPUT_TYPE_JOYSTICK) {
120 if (axis == INPUT_AXIS_X) {
121 return JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d->joystick, d->x_axis*2));
122 } else {
123 return JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d->joystick, 1+d->y_axis*2));
125 } else {
126 /* unimplemented */
128 return 0.0;
131 char input_device_get_key(InputDevice* d, unsigned const char key) {
132 SDL_PumpEvents();
134 if (d->type == INPUT_TYPE_KEYBOARD) {
135 return SDL_GetKeyState(NULL)[d->input_keys[key]];
136 } else if (d->type == INPUT_TYPE_JOYSTICK) {
137 return SDL_JoystickGetButton(d->joystick, d->input_keys[key]);
138 } else {
139 /* unimplemented */
141 return 0;