1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #if defined(PLATFORM_FREEBSD)
15 #include <machine/joystick.h>
22 /* ========================================================================= */
23 /* platform dependant joystick functions */
24 /* ========================================================================= */
26 #if defined(PLATFORM_UNIX) && !defined(TARGET_SDL)
27 void UnixInitJoysticks()
31 for (i=0; i<MAX_PLAYERS; i++)
33 char *device_name = setup.input[i].joy.device_name;
35 /* this allows subsequent calls to 'InitJoysticks' for re-initialization */
36 if (joystick.fd[i] != -1)
38 close(joystick.fd[i]);
42 if (!setup.input[i].use_joystick)
45 if (access(device_name, R_OK) != 0)
47 Error(ERR_WARN, "cannot access joystick device '%s'", device_name);
51 if ((joystick.fd[i] = open(device_name, O_RDONLY)) < 0)
53 Error(ERR_WARN, "cannot open joystick device '%s'", device_name);
57 joystick.status = JOYSTICK_ACTIVATED;
61 boolean UnixReadJoystick(int fd, int *x, int *y, boolean *b1, boolean *b2)
63 #if defined(PLATFORM_FREEBSD)
64 struct joystick joy_ctrl;
66 struct joystick_control
74 if (read(fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
82 #if defined(PLATFORM_FREEBSD)
89 *b1 = joy_ctrl.buttons & 1;
91 *b2 = joy_ctrl.buttons & 2;
96 #endif /* PLATFORM_UNIX && !TARGET_SDL */
99 /* ========================================================================= */
100 /* platform independant joystick functions */
101 /* ========================================================================= */
103 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
104 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
106 void translate_joyname(int *joysymbol, char **name, int mode)
114 { JOY_LEFT, "joystick_left" },
115 { JOY_RIGHT, "joystick_right" },
116 { JOY_UP, "joystick_up" },
117 { JOY_DOWN, "joystick_down" },
118 { JOY_BUTTON_1, "joystick_button_1" },
119 { JOY_BUTTON_2, "joystick_button_2" },
124 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
126 *name = "[undefined]";
130 if (*joysymbol == translate_joy[i].joysymbol)
132 *name = translate_joy[i].name;
137 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
143 if (strcmp(*name, translate_joy[i].name) == 0)
145 *joysymbol = translate_joy[i].joysymbol;
152 char *getJoyNameFromJoySymbol(int joysymbol)
156 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
160 int getJoySymbolFromJoyName(char *name)
164 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
168 int getJoystickNrFromDeviceName(char *device_name)
173 if (device_name == NULL || device_name[0] == '\0')
176 c = device_name[strlen(device_name) - 1];
178 if (c >= '0' && c <= '9')
179 joystick_nr = (int)(c - '0');
181 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
187 char *getDeviceNameFromJoystickNr(int joystick_nr)
189 static char *joystick_device_name[MAX_PLAYERS] =
197 return (joystick_nr >= 0 && joystick_nr <= 3 ?
198 joystick_device_name[joystick_nr] : "");
201 static int JoystickPositionPercent(int center, int border, int actual)
203 long range, position;
206 if (border < center && actual > center)
208 if (border > center && actual < center)
211 range = ABS(border - center);
212 position = ABS(actual - center);
214 percent = (int)(position * 100 / range);
222 void CheckJoystickData()
227 for(i=0; i<MAX_PLAYERS; i++)
229 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
230 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
231 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
232 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
234 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
235 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
236 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
237 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
241 int Joystick(int player_nr)
243 int joystick_fd = joystick.fd[player_nr];
245 boolean js_b1, js_b2;
246 int left, right, up, down;
247 int result = JOY_NO_ACTION;
249 if (joystick.status != JOYSTICK_ACTIVATED)
250 return JOY_NO_ACTION;
252 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
253 return JOY_NO_ACTION;
255 if (!ReadJoystick(joystick_fd, &js_x, &js_y, &js_b1, &js_b2))
257 Error(ERR_WARN, "cannot read joystick device '%s'",
258 setup.input[player_nr].joy.device_name);
260 joystick.status = JOYSTICK_NOT_AVAILABLE;
261 return JOY_NO_ACTION;
264 left = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
265 setup.input[player_nr].joy.xleft, js_x);
266 right = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
267 setup.input[player_nr].joy.xright, js_x);
268 up = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
269 setup.input[player_nr].joy.yupper, js_y);
270 down = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
271 setup.input[player_nr].joy.ylower, js_y);
273 if (left > JOYSTICK_PERCENT)
275 else if (right > JOYSTICK_PERCENT)
277 if (up > JOYSTICK_PERCENT)
279 else if (down > JOYSTICK_PERCENT)
283 result |= JOY_BUTTON_1;
285 result |= JOY_BUTTON_2;
290 int JoystickButton(int player_nr)
292 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
293 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
298 if (last_joy_button[player_nr])
299 result = JOY_BUTTON_PRESSED;
301 result = JOY_BUTTON_NEW_PRESSED;
305 if (last_joy_button[player_nr])
306 result = JOY_BUTTON_NEW_RELEASED;
308 result = JOY_BUTTON_NOT_PRESSED;
311 last_joy_button[player_nr] = joy_button;
320 for (i=0; i<MAX_PLAYERS; i++)
321 result |= Joystick(i);
326 int AnyJoystickButton()
329 int result = JOY_BUTTON_NOT_PRESSED;
331 for (i=0; i<MAX_PLAYERS; i++)
333 result = JoystickButton(i);
334 if (result != JOY_BUTTON_NOT_PRESSED)
341 void DeactivateJoystick()
343 /* Temporarily deactivate joystick. This is needed for calibration
344 screens, where the player has to select a joystick device that
345 should be calibrated. If there is a totally uncalibrated joystick
346 active, it may be impossible (due to messed up input from joystick)
347 to select the joystick device to calibrate even when trying to use
348 the mouse or keyboard to select the device. */
350 if (joystick.status & JOYSTICK_AVAILABLE)
351 joystick.status &= ~JOYSTICK_ACTIVE;
354 void ActivateJoystick()
356 /* reactivate temporarily deactivated joystick */
358 if (joystick.status & JOYSTICK_AVAILABLE)
359 joystick.status |= JOYSTICK_ACTIVE;