rnd-20000718-1-src
[rocksndiamonds.git] / src / joystick.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  joystick.c                                              *
12 ***********************************************************/
13
14 #ifdef __FreeBSD__
15 #include <machine/joystick.h>
16 #endif
17
18 #include "joystick.h"
19 #include "misc.h"
20
21 void CheckJoystickData()
22 {
23   int i;
24   int distance = 100;
25
26   for(i=0; i<MAX_PLAYERS; i++)
27   {
28     if (setup.input[i].joy.xmiddle <= distance)
29       setup.input[i].joy.xmiddle = distance;
30     if (setup.input[i].joy.ymiddle <= distance)
31       setup.input[i].joy.ymiddle = distance;
32
33     if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
34       setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
35     if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
36       setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
37
38     if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
39       setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
40     if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
41       setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
42   }
43 }
44
45 #ifndef MSDOS
46 static int JoystickPosition(int middle, int margin, int actual)
47 {
48   long range, pos;
49   int percentage;
50
51   if (margin < middle && actual > middle)
52     return 0;
53   if (margin > middle && actual < middle)
54     return 0;
55
56   range = ABS(margin - middle);
57   pos = ABS(actual - middle);
58   percentage = (int)(pos * 100 / range);
59
60   if (percentage > 100)
61     percentage = 100;
62
63   return percentage;
64 }
65 #endif
66
67 #ifndef MSDOS
68 int Joystick(int player_nr)
69 {
70 #ifdef __FreeBSD__
71   struct joystick joy_ctrl;
72 #else
73   struct joystick_control
74   {
75     int buttons;
76     int x;
77     int y;
78   } joy_ctrl;
79 #endif
80
81   int joystick_fd = stored_player[player_nr].joystick_fd;
82   int js_x,js_y, js_b1,js_b2;
83   int left, right, up, down;
84   int result = 0;
85
86   if (joystick_status == JOYSTICK_OFF)
87     return 0;
88
89   if (game_status == SETUPINPUT)
90     return 0;
91
92   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
93     return 0;
94
95   if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
96   {
97     Error(ERR_WARN, "cannot read joystick device '%s'",
98           setup.input[player_nr].joy.device_name);
99     joystick_status = JOYSTICK_OFF;
100     return 0;
101   }
102
103   js_x  = joy_ctrl.x;
104   js_y  = joy_ctrl.y;
105
106 #ifdef __FreeBSD__
107   js_b1 = joy_ctrl.b1;
108   js_b2 = joy_ctrl.b2;
109 #else
110   js_b1 = joy_ctrl.buttons & 1;
111   js_b2 = joy_ctrl.buttons & 2;
112 #endif
113
114   left  = JoystickPosition(setup.input[player_nr].joy.xmiddle,
115                            setup.input[player_nr].joy.xleft,  js_x);
116   right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
117                            setup.input[player_nr].joy.xright, js_x);
118   up    = JoystickPosition(setup.input[player_nr].joy.ymiddle,
119                            setup.input[player_nr].joy.yupper, js_y);
120   down  = JoystickPosition(setup.input[player_nr].joy.ymiddle,
121                            setup.input[player_nr].joy.ylower, js_y);
122
123   if (left > JOYSTICK_PERCENT)
124     result |= JOY_LEFT;
125   else if (right > JOYSTICK_PERCENT)
126     result |= JOY_RIGHT;
127   if (up > JOYSTICK_PERCENT)
128     result |= JOY_UP;
129   else if (down > JOYSTICK_PERCENT)
130     result |= JOY_DOWN;
131
132   if (js_b1)
133     result |= JOY_BUTTON_1;
134   if (js_b2)
135     result |= JOY_BUTTON_2;
136
137   return result;
138 }
139
140 #else /* MSDOS */
141
142 /* allegro global variables for joystick control */
143 extern int num_joysticks;
144 extern JOYSTICK_INFO joy[];
145
146 int Joystick(int player_nr)
147 {
148   int joystick_nr = stored_player[player_nr].joystick_fd;
149   int result = 0;
150
151   if (joystick_status == JOYSTICK_OFF)
152     return 0;
153
154   if (game_status == SETUPINPUT)
155     return 0;
156
157   if (joystick_nr < 0)
158     return 0;
159
160   /* the allegro global variable 'num_joysticks' contains the number
161      of joysticks found at initialization under MSDOS / Windows */
162
163 #if 0
164   if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick)
165     return 0;
166 #else
167
168 #if 1
169   if (joystick_nr >= num_joysticks ||
170       (game_status == PLAYING && !setup.input[player_nr].use_joystick))
171     return 0;
172 #else
173   if (joystick_nr >= num_joysticks)
174     return 0;
175 #endif
176
177 #endif
178
179   poll_joystick();
180
181   if (joy[joystick_nr].stick[0].axis[0].d1)
182     result |= JOY_LEFT;
183   else if (joy[joystick_nr].stick[0].axis[0].d2)
184     result |= JOY_RIGHT;
185   if (joy[joystick_nr].stick[0].axis[1].d1)
186     result |= JOY_UP;
187   else if (joy[joystick_nr].stick[0].axis[1].d2)
188     result |= JOY_DOWN;
189
190   if (joy[joystick_nr].button[0].b)
191     result |= JOY_BUTTON_1;
192   if (joy[joystick_nr].button[1].b)
193     result |= JOY_BUTTON_2;
194
195   return result;
196 }
197 #endif /* MSDOS */
198
199 int JoystickButton(int player_nr)
200 {
201   static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
202   int joy_button = (Joystick(player_nr) & JOY_BUTTON);
203   int result;
204
205   if (joy_button)
206   {
207     if (last_joy_button[player_nr])
208       result = JOY_BUTTON_PRESSED;
209     else
210       result = JOY_BUTTON_NEW_PRESSED;
211   }
212   else
213   {
214     if (last_joy_button[player_nr])
215       result = JOY_BUTTON_NEW_RELEASED;
216     else
217       result = JOY_BUTTON_NOT_PRESSED;
218   }
219
220   last_joy_button[player_nr] = joy_button;
221   return result;
222 }
223
224 int AnyJoystick()
225 {
226   int i;
227   int result = 0;
228
229   for (i=0; i<MAX_PLAYERS; i++)
230   {
231
232     /*
233     if (!setup.input[i].use_joystick)
234       continue;
235       */
236
237
238     result |= Joystick(i);
239   }
240
241   return result;
242 }
243
244 int AnyJoystickButton()
245 {
246   int i;
247   int result;
248
249   for (i=0; i<MAX_PLAYERS; i++)
250   {
251
252     /*
253     if (!setup.input[i].use_joystick)
254       continue;
255       */
256
257     /*
258     result |= JoystickButton(i);
259     */
260
261     result = JoystickButton(i);
262     if (result != JOY_BUTTON_NOT_PRESSED)
263       break;
264   }
265
266   return result;
267 }