7240e88cf6bfaac94f88b681b74581cacc3f9086
[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 int Joystick(int player_nr)
68 {
69 #ifndef MSDOS
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 #endif
81
82   int joystick_fd = stored_player[player_nr].joystick_fd;
83
84 #ifndef MSDOS
85   int js_x,js_y, js_b1,js_b2;
86   int left, right, up, down;
87   int result = 0;
88 #endif
89
90   if (joystick_status == JOYSTICK_OFF)
91     return 0;
92
93   if (!setup.input[player_nr].use_joystick || joystick_fd < 0)
94     return 0;
95
96 #ifndef MSDOS
97   if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
98   {
99     Error(ERR_WARN, "cannot read joystick device '%s'",
100           setup.input[player_nr].joy.device_name);
101     joystick_status = JOYSTICK_OFF;
102     return 0;
103   }
104
105   js_x  = joy_ctrl.x;
106   js_y  = joy_ctrl.y;
107
108 #ifdef __FreeBSD__
109   js_b1 = joy_ctrl.b1;
110   js_b2 = joy_ctrl.b2;
111 #else
112   js_b1 = joy_ctrl.buttons & 1;
113   js_b2 = joy_ctrl.buttons & 2;
114 #endif
115
116   left  = JoystickPosition(setup.input[player_nr].joy.xmiddle,
117                            setup.input[player_nr].joy.xleft,  js_x);
118   right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
119                            setup.input[player_nr].joy.xright, js_x);
120   up    = JoystickPosition(setup.input[player_nr].joy.ymiddle,
121                            setup.input[player_nr].joy.yupper, js_y);
122   down  = JoystickPosition(setup.input[player_nr].joy.ymiddle,
123                            setup.input[player_nr].joy.ylower, js_y);
124
125   if (left > JOYSTICK_PERCENT)
126     result |= JOY_LEFT;
127   else if (right > JOYSTICK_PERCENT)
128     result |= JOY_RIGHT;
129   if (up > JOYSTICK_PERCENT)
130     result |= JOY_UP;
131   else if (down > JOYSTICK_PERCENT)
132     result |= JOY_DOWN;
133
134   if (js_b1)
135     result |= JOY_BUTTON_1;
136   if (js_b2)
137     result |= JOY_BUTTON_2;
138
139   return result;
140 #else
141   return 0;
142 #endif
143 }
144
145 int JoystickButton(int player_nr)
146 {
147   static int last_joy_button = 0;
148   int joy_button = (Joystick(player_nr) & JOY_BUTTON);
149   int result;
150
151   if (joy_button)
152   {
153     if (last_joy_button)
154       result = JOY_BUTTON_PRESSED;
155     else
156       result = JOY_BUTTON_NEW_PRESSED;
157   }
158   else
159   {
160     if (last_joy_button)
161       result = JOY_BUTTON_NEW_RELEASED;
162     else
163       result = JOY_BUTTON_NOT_PRESSED;
164   }
165
166   last_joy_button = joy_button;
167   return result;
168 }
169
170 int AnyJoystick()
171 {
172   int i;
173   int result = 0;
174
175   for (i=0; i<MAX_PLAYERS; i++)
176   {
177     if (!setup.input[i].use_joystick)
178       continue;
179
180     result |= Joystick(i);
181   }
182
183   return result;
184 }
185
186 int AnyJoystickButton()
187 {
188   int i;
189   int result = 0;
190
191   for (i=0; i<MAX_PLAYERS; i++)
192   {
193     if (!setup.input[i].use_joystick)
194       continue;
195
196     result |= JoystickButton(i);
197   }
198
199   return result;
200 }