New input selection UI + Mouse support, updated TODO list
[tennix.git] / input.c
blobd62bb6057f3281872e139d72b93f715929d7bc93
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 "graphics.h"
28 #include "game.h"
29 #include "input.h"
30 #include "util.h"
32 static InputDevice devices[MAX_INPUT_DEVICES];
33 static int devices_count;
35 void init_input()
37 int n, x;
39 SDL_JoystickEventState(SDL_ENABLE);
41 #ifndef MAEMO
42 /* keyboard presets */
43 devices[devices_count].type = INPUT_TYPE_KEYBOARD;
44 devices[devices_count].up_key = 'w';
45 devices[devices_count].down_key = 's';
46 devices[devices_count].input_keys[INPUT_KEY_HIT]= 'd';
47 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 'e';
48 devices[devices_count].input_keys[INPUT_KEY_SMASH] = 'f';
49 devices[devices_count].icon = GR_INPUT_KEYBOARD_WS;
50 devices[devices_count].exclusive_to_player = 1;
51 strcpy(devices[devices_count].name, "Keyboard (WS/DEF)");
52 devices_count++;
54 /* keyboard presets */
55 devices[devices_count].type = INPUT_TYPE_KEYBOARD;
56 devices[devices_count].up_key = 'o';
57 devices[devices_count].down_key = 'l';
58 devices[devices_count].input_keys[INPUT_KEY_HIT]= 'k';
59 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 'i';
60 devices[devices_count].input_keys[INPUT_KEY_SMASH] = 'j';
61 devices[devices_count].icon = GR_INPUT_KEYBOARD_OL;
62 devices[devices_count].exclusive_to_player = 2;
63 strcpy(devices[devices_count].name, "Keyboard (OL/KIJ)");
64 devices_count++;
65 #endif
67 /* keyboard presets */
68 devices[devices_count].type = INPUT_TYPE_KEYBOARD;
69 devices[devices_count].up_key = SDLK_UP;
70 devices[devices_count].down_key = SDLK_DOWN;
71 devices[devices_count].input_keys[INPUT_KEY_HIT]= SDLK_SPACE;
72 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = SDLK_LCTRL;
73 devices[devices_count].input_keys[INPUT_KEY_SMASH] = SDLK_LALT;
74 #ifdef MAEMO
75 devices[devices_count].icon = GR_INPUT_MAEMO_DPAD;
76 strcpy(devices[devices_count].name, "D-Pad");
77 devices[devices_count].input_keys[INPUT_KEY_HIT]= SDLK_RETURN;
78 /* FIXME: No TOPSPIN and SMASH keys on Maemo yet with d-pad */
79 #else
80 devices[devices_count].icon = GR_INPUT_KEYBOARD_ARROWS;
81 strcpy(devices[devices_count].name, "Keyboard (arrows + space, lctrl, lalt)");
82 #endif
83 devices_count++;
85 /* mouse */
86 devices[devices_count].type = INPUT_TYPE_MOUSE;
87 devices[devices_count].input_keys[INPUT_KEY_HIT]= SDL_BUTTON(1);
88 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = SDL_BUTTON(2);
89 devices[devices_count].input_keys[INPUT_KEY_SMASH] = SDL_BUTTON(3);
90 #ifdef MAEMO
91 devices[devices_count].icon = GR_INPUT_TOUCHSCREEN;
92 strcpy(devices[devices_count].name, "Touchscreen");
93 #else
94 devices[devices_count].icon = GR_INPUT_MOUSE;
95 strcpy(devices[devices_count].name, "Mouse");
96 #endif
97 devices_count++;
99 /* joysticks */
100 n = SDL_NumJoysticks();
101 for (x=0; x<n && devices_count<MAX_INPUT_DEVICES; x++) {
102 strcpy(devices[devices_count].name, SDL_JoystickName(x));
103 devices[devices_count].type = INPUT_TYPE_JOYSTICK;
104 devices[devices_count].joystick = SDL_JoystickOpen(x);
105 devices[devices_count].x_axis = 0;
106 devices[devices_count].y_axis = 0;
107 devices[devices_count].input_keys[INPUT_KEY_HIT]= 0;
108 devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 1;
109 devices[devices_count].input_keys[INPUT_KEY_SMASH] = 2;
110 devices[devices_count].icon = GR_INPUT_GAMEPAD;
111 devices_count++;
115 void uninit_input()
117 int i;
118 SDL_JoystickEventState(SDL_IGNORE);
120 for (i=0; i<devices_count; i++) {
121 if (devices[i].type == INPUT_TYPE_JOYSTICK) {
122 SDL_JoystickClose(devices[i].joystick);
125 devices_count = 0;
129 InputDevice* find_input_devices(unsigned int* count)
131 *count = devices_count;
132 return devices;
135 const char* input_device_get_name(InputDevice* d)
137 return d->name;
140 float input_device_get_axis(InputDevice* d, unsigned const char axis) {
141 Uint8 *keystate;
142 Uint8 mb;
144 SDL_PumpEvents();
146 if (d->type == INPUT_TYPE_KEYBOARD) {
147 keystate = SDL_GetKeyState(NULL);
148 if (axis == INPUT_AXIS_X) {
149 return 1.0*keystate[d->right_key] + -1.0*keystate[d->left_key];
150 } else {
151 return 1.0*keystate[d->down_key] + -1.0*keystate[d->up_key];
153 } else if (d->type == INPUT_TYPE_JOYSTICK) {
154 if (axis == INPUT_AXIS_X) {
155 return JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d->joystick, d->x_axis*2));
156 } else {
157 return JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d->joystick, 1+d->y_axis*2));
159 } else if (d->type == INPUT_TYPE_MOUSE) {
160 mb = SDL_GetMouseState(&d->mx, &d->my);
161 if (axis == INPUT_AXIS_X) {
162 /* Not x-movement yet (PLAYER_MOVE_X is not defined!) */
163 /*if (absf(d->mx - d->player_x) > PLAYER_MOVE_X) {
164 if (d->mx > d->player_x) {
165 return 1.0;
166 } else if (d->mx < d->player_x) {
167 return -1.0;
170 } else {
171 if (absf(d->my - d->player_y) > PLAYER_MOVE_Y) {
172 if (d->my > d->player_y) {
173 return 1.0;
174 } else if (d->my < d->player_y) {
175 return -1.0;
179 } else {
180 /* unimplemented */
182 return 0.0;
185 char input_device_get_key(InputDevice* d, unsigned const char key) {
186 Uint8 mb;
187 SDL_PumpEvents();
189 if (d->type == INPUT_TYPE_KEYBOARD) {
190 return SDL_GetKeyState(NULL)[d->input_keys[key]];
191 } else if (d->type == INPUT_TYPE_JOYSTICK) {
192 return SDL_JoystickGetButton(d->joystick, d->input_keys[key]);
193 } else if (d->type == INPUT_TYPE_MOUSE) {
194 mb = SDL_GetMouseState(NULL, NULL);
195 return (mb & d->input_keys[key]) != 0;
196 } else {
197 /* unimplemented */
199 return 0;