]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/joy.c
copied from d1x
[btb/d2x.git] / arch / sdl / joy.c
1 /*
2  * $Source: /cvs/cvsroot/d2x/arch/sdl/joy.c,v $
3  * $Revision: 1.2 $
4  * $Author: bradleyb $
5  * $Date: 2001-12-03 02:43:02 $
6  *
7  * SDL joystick support
8  *
9  * $Log: not supported by cvs2svn $
10  * Revision 1.1  2001/10/24 09:25:05  bradleyb
11  * Moved input stuff to arch subdirs, as in d1x.
12  *
13  * Revision 1.1  2001/10/10 03:01:29  bradleyb
14  * Replacing win32 joystick (broken) with SDL joystick (stubs)
15  *
16  *
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <conf.h>
21 #endif
22
23 #include <SDL/SDL.h>
24
25 #include "joy.h"
26 #include "error.h"
27 #include "timer.h"
28 #include "console.h"
29
30 #define MAX_JOYSTICKS 16
31 #define MAX_AXES 32
32
33 #define MAX_AXES_PER_JOYSTICK 8
34 #define MAX_BUTTONS_PER_JOYSTICK 16
35
36 char joy_present = 0;
37 int num_joysticks = 0;
38
39 struct joybutton {
40         int state;
41         fix time_went_down;
42         fix time_held_down;
43         int num_downs;
44         int num_ups;
45 };
46
47 static struct joyinfo {
48         int n_axes;
49         int n_buttons;
50         int axes[MAX_AXES];
51         struct joybutton buttons[MAX_BUTTONS];
52 } Joystick;
53
54 static struct {
55         SDL_Joystick *handle;
56         int n_axes;
57         int n_buttons;
58         int axis_map[MAX_AXES_PER_JOYSTICK];
59         int button_map[MAX_BUTTONS_PER_JOYSTICK];
60 } SDL_Joysticks[MAX_JOYSTICKS];
61
62 void joy_button_handler(SDL_JoyButtonEvent *jbe)
63 {
64         int button;
65
66         button = SDL_Joysticks[jbe->which].button_map[jbe->button];
67
68         Joystick.buttons[button].state = jbe->state;
69
70         switch (jbe->type) {
71         case SDL_JOYBUTTONDOWN:
72                 Joystick.buttons[button].time_went_down
73                         = timer_get_fixed_seconds();
74                 Joystick.buttons[button].num_downs++;
75                 break;
76         case SDL_JOYBUTTONUP:
77                 Joystick.buttons[button].time_held_down
78                         += timer_get_fixed_seconds()
79                         - Joystick.buttons[button].time_went_down;
80                 Joystick.buttons[button].num_ups++;
81                 break;
82         }
83 }
84
85 void joy_axis_handler(SDL_JoyAxisEvent *jae)
86 {
87         int axis;
88
89         axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
90         
91         Joystick.axes[axis] = jae->value;
92 }
93
94
95 /* ----------------------------------------------- */
96
97 int joy_init()
98 {
99         int i,j,n;
100
101         memset(&Joystick,0,sizeof(Joystick));
102
103         n = SDL_NumJoysticks();
104
105         con_printf(CON_VERBOSE, "Joystick: found %d joysticks\n", n);
106         for (i = 0; i < n; i++) {
107                 con_printf(CON_VERBOSE, "Joystick %d: %s\n", i, SDL_JoystickName(i));
108                 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
109                 if (SDL_Joysticks[num_joysticks].handle) {
110                         joy_present = 1;
111                         SDL_Joysticks[num_joysticks].n_axes
112                                 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
113                         SDL_Joysticks[num_joysticks].n_buttons
114                                 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
115                         con_printf(CON_VERBOSE, "Joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
116                         con_printf(CON_VERBOSE, "Joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
117                         for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
118                                 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
119                         for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
120                                 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
121                         num_joysticks++;
122                 } else
123                         con_printf(CON_VERBOSE, "Joystick: initialization failed!\n");
124         }
125                 
126         return joy_present;
127 }
128
129 void joy_close()
130 {
131         while (num_joysticks)
132                 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
133 }
134
135 void joy_get_pos(int *x, int *y)
136 {
137         *x = Joystick.axes[0] << 8;
138         *y = Joystick.axes[1] << 8;
139 }
140
141 int joy_get_btns()
142 {
143         return 0;
144 }
145
146 int joy_get_button_down_cnt( int btn )
147 {
148         return 0;
149 }
150
151 fix joy_get_button_down_time(int btn)
152 {
153         fix time;
154
155         time = Joystick.buttons[btn].time_held_down;
156         Joystick.buttons[btn].time_held_down = 0;
157
158         return time;
159 }
160
161 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
162 {
163         return 0;
164 }
165
166 void joy_flush()
167 {
168 }
169
170 int joy_get_button_state( int btn )
171 {
172         return 0;
173 }
174
175 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
176 {
177 }
178
179 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
180 {
181 }
182
183 int joy_get_scaled_reading( int raw, int axn )
184 {
185         return 0;
186 }
187
188 void joy_set_slow_reading( int flag )
189 {
190 }