5e1cc13275aa8cd5f5513de21e8eef561645593d
[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 static int JoystickPosition(int middle, int margin, int actual)
46 {
47   long range, pos;
48   int percentage;
49
50   if (margin < middle && actual > middle)
51     return 0;
52   if (margin > middle && actual < middle)
53     return 0;
54
55   range = ABS(margin - middle);
56   pos = ABS(actual - middle);
57   percentage = (int)(pos * 100 / range);
58
59   if (percentage > 100)
60     percentage = 100;
61
62   return percentage;
63 }
64
65 int Joystick(int player_nr)
66 {
67 #ifdef __FreeBSD__
68   struct joystick joy_ctrl;
69 #else
70   struct joystick_control
71   {
72     int buttons;
73     int x;
74     int y;
75   } joy_ctrl;
76 #endif
77
78   int joystick_fd = stored_player[player_nr].joystick_fd;
79   int js_x,js_y, js_b1,js_b2;
80   int left, right, up, down;
81   int result = 0;
82
83   if (joystick_status == JOYSTICK_OFF)
84     return 0;
85
86   if (!setup.input[player_nr].use_joystick || joystick_fd < 0)
87     return 0;
88
89 #ifndef MSDOS
90   if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
91   {
92     Error(ERR_WARN, "cannot read joystick device '%s'",
93           setup.input[player_nr].joy.device_name);
94     joystick_status = JOYSTICK_OFF;
95     return 0;
96   }
97
98   js_x  = joy_ctrl.x;
99   js_y  = joy_ctrl.y;
100
101 #ifdef __FreeBSD__
102   js_b1 = joy_ctrl.b1;
103   js_b2 = joy_ctrl.b2;
104 #else
105   js_b1 = joy_ctrl.buttons & 1;
106   js_b2 = joy_ctrl.buttons & 2;
107 #endif
108
109   left  = JoystickPosition(setup.input[player_nr].joy.xmiddle,
110                            setup.input[player_nr].joy.xleft,  js_x);
111   right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
112                            setup.input[player_nr].joy.xright, js_x);
113   up    = JoystickPosition(setup.input[player_nr].joy.ymiddle,
114                            setup.input[player_nr].joy.yupper, js_y);
115   down  = JoystickPosition(setup.input[player_nr].joy.ymiddle,
116                            setup.input[player_nr].joy.ylower, js_y);
117
118   if (left > JOYSTICK_PERCENT)
119     result |= JOY_LEFT;
120   else if (right > JOYSTICK_PERCENT)
121     result |= JOY_RIGHT;
122   if (up > JOYSTICK_PERCENT)
123     result |= JOY_UP;
124   else if (down > JOYSTICK_PERCENT)
125     result |= JOY_DOWN;
126
127   if (js_b1)
128     result |= JOY_BUTTON_1;
129   if (js_b2)
130     result |= JOY_BUTTON_2;
131
132   return result;
133 #else
134   return 0;
135 #endif
136 }
137
138 int JoystickButton(int player_nr)
139 {
140   static int last_joy_button = 0;
141   int joy_button = (Joystick(player_nr) & JOY_BUTTON);
142   int result;
143
144   if (joy_button)
145   {
146     if (last_joy_button)
147       result = JOY_BUTTON_PRESSED;
148     else
149       result = JOY_BUTTON_NEW_PRESSED;
150   }
151   else
152   {
153     if (last_joy_button)
154       result = JOY_BUTTON_NEW_RELEASED;
155     else
156       result = JOY_BUTTON_NOT_PRESSED;
157   }
158
159   last_joy_button = joy_button;
160   return result;
161 }
162
163 int AnyJoystick()
164 {
165   int i;
166   int result = 0;
167
168   for (i=0; i<MAX_PLAYERS; i++)
169   {
170     if (!setup.input[i].use_joystick)
171       continue;
172
173     result |= Joystick(i);
174   }
175
176   return result;
177 }
178
179 int AnyJoystickButton()
180 {
181   int i;
182   int result = 0;
183
184   for (i=0; i<MAX_PLAYERS; i++)
185   {
186     if (!setup.input[i].use_joystick)
187       continue;
188
189     result |= JoystickButton(i);
190   }
191
192   return result;
193 }