1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1995-2006 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 independent joystick functions */
24 /* ========================================================================= */
26 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
27 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
29 void translate_joyname(int *joysymbol, char **name, int mode)
37 { JOY_LEFT, "joystick_left" },
38 { JOY_RIGHT, "joystick_right" },
39 { JOY_UP, "joystick_up" },
40 { JOY_DOWN, "joystick_down" },
41 { JOY_BUTTON_1, "joystick_button_1" },
42 { JOY_BUTTON_2, "joystick_button_2" },
47 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
49 *name = "[undefined]";
51 for (i = 0; i < 6; i++)
53 if (*joysymbol == translate_joy[i].joysymbol)
55 *name = translate_joy[i].name;
60 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
64 for (i = 0; i < 6; i++)
66 if (strEqual(*name, translate_joy[i].name))
68 *joysymbol = translate_joy[i].joysymbol;
75 char *getJoyNameFromJoySymbol(int joysymbol)
79 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
83 int getJoySymbolFromJoyName(char *name)
87 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
91 int getJoystickNrFromDeviceName(char *device_name)
96 if (device_name == NULL || device_name[0] == '\0')
99 c = device_name[strlen(device_name) - 1];
101 if (c >= '0' && c <= '9')
102 joystick_nr = (int)(c - '0');
104 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
110 char *getDeviceNameFromJoystickNr(int joystick_nr)
112 static char *joystick_device_name[MAX_PLAYERS] =
120 return (joystick_nr >= 0 && joystick_nr <= 3 ?
121 joystick_device_name[joystick_nr] : "");
124 static int JoystickPositionPercent(int center, int border, int actual)
129 if (border < center && actual > center)
131 if (border > center && actual < center)
134 range = ABS(border - center);
135 position = ABS(actual - center);
137 percent = (int)(position * 100 / range);
145 void CheckJoystickData()
150 for (i = 0; i < MAX_PLAYERS; i++)
152 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
153 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
154 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
155 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
157 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
158 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
159 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
160 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
164 int Joystick(int player_nr)
166 int joystick_fd = joystick.fd[player_nr];
168 boolean js_b1, js_b2;
169 int left, right, up, down;
170 int result = JOY_NO_ACTION;
172 if (joystick.status != JOYSTICK_ACTIVATED)
173 return JOY_NO_ACTION;
175 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
176 return JOY_NO_ACTION;
178 if (!ReadJoystick(joystick_fd, &js_x, &js_y, &js_b1, &js_b2))
180 Error(ERR_WARN, "cannot read joystick device '%s'",
181 setup.input[player_nr].joy.device_name);
183 joystick.status = JOYSTICK_NOT_AVAILABLE;
184 return JOY_NO_ACTION;
187 left = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
188 setup.input[player_nr].joy.xleft, js_x);
189 right = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
190 setup.input[player_nr].joy.xright, js_x);
191 up = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
192 setup.input[player_nr].joy.yupper, js_y);
193 down = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
194 setup.input[player_nr].joy.ylower, js_y);
196 if (left > JOYSTICK_PERCENT)
198 else if (right > JOYSTICK_PERCENT)
200 if (up > JOYSTICK_PERCENT)
202 else if (down > JOYSTICK_PERCENT)
206 result |= JOY_BUTTON_1;
208 result |= JOY_BUTTON_2;
213 int JoystickButton(int player_nr)
215 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
216 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
221 if (last_joy_button[player_nr])
222 result = JOY_BUTTON_PRESSED;
224 result = JOY_BUTTON_NEW_PRESSED;
228 if (last_joy_button[player_nr])
229 result = JOY_BUTTON_NEW_RELEASED;
231 result = JOY_BUTTON_NOT_PRESSED;
234 last_joy_button[player_nr] = joy_button;
243 for (i = 0; i < MAX_PLAYERS; i++)
244 result |= Joystick(i);
249 int AnyJoystickButton()
252 int result = JOY_BUTTON_NOT_PRESSED;
254 for (i = 0; i < MAX_PLAYERS; i++)
256 result = JoystickButton(i);
257 if (result != JOY_BUTTON_NOT_PRESSED)
264 void DeactivateJoystick()
266 /* Temporarily deactivate joystick. This is needed for calibration
267 screens, where the player has to select a joystick device that
268 should be calibrated. If there is a totally uncalibrated joystick
269 active, it may be impossible (due to messed up input from joystick)
270 to select the joystick device to calibrate even when trying to use
271 the mouse or keyboard to select the device. */
273 if (joystick.status & JOYSTICK_AVAILABLE)
274 joystick.status &= ~JOYSTICK_ACTIVE;
277 void ActivateJoystick()
279 /* reactivate temporarily deactivated joystick */
281 if (joystick.status & JOYSTICK_AVAILABLE)
282 joystick.status |= JOYSTICK_ACTIVE;