1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #if defined(PLATFORM_FREEBSD)
13 #include <machine/joystick.h>
20 // ============================================================================
21 // platform independent joystick functions
22 // ============================================================================
24 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
25 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
27 static void translate_joyname(int *joysymbol, char **name, int mode)
35 { JOY_LEFT, "joystick_left" },
36 { JOY_RIGHT, "joystick_right" },
37 { JOY_UP, "joystick_up" },
38 { JOY_DOWN, "joystick_down" },
39 { JOY_BUTTON_1, "joystick_button_1" },
40 { JOY_BUTTON_2, "joystick_button_2" },
45 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
47 *name = "[undefined]";
49 for (i = 0; i < 6; i++)
51 if (*joysymbol == translate_joy[i].joysymbol)
53 *name = translate_joy[i].name;
58 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
62 for (i = 0; i < 6; i++)
64 if (strEqual(*name, translate_joy[i].name))
66 *joysymbol = translate_joy[i].joysymbol;
73 char *getJoyNameFromJoySymbol(int joysymbol)
77 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
81 int getJoySymbolFromJoyName(char *name)
85 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
89 int getJoystickNrFromDeviceName(char *device_name)
94 if (device_name == NULL || device_name[0] == '\0')
97 c = device_name[strlen(device_name) - 1];
99 if (c >= '0' && c <= '9')
100 joystick_nr = (int)(c - '0');
102 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
108 char *getDeviceNameFromJoystickNr(int joystick_nr)
110 static char *joystick_device_name[MAX_PLAYERS] =
118 return (joystick_nr >= 0 && joystick_nr <= 3 ?
119 joystick_device_name[joystick_nr] : "");
122 char *getFormattedJoystickName(const char *name_raw)
124 static char name[MAX_JOYSTICK_NAME_LEN + 1];
125 boolean name_skip_space = TRUE;
128 if (name_raw == NULL)
129 name_raw = "(unknown joystick)";
131 // copy joystick name, cutting leading and multiple spaces
132 for (i = 0, j = 0; i < strlen(name_raw) && i < MAX_JOYSTICK_NAME_LEN; i++)
134 if (name_raw[i] != ' ')
136 name[j++] = name_raw[i];
137 name_skip_space = FALSE;
139 else if (!name_skip_space)
141 name[j++] = name_raw[i];
142 name_skip_space = TRUE;
146 // cut trailing space
147 if (j > 0 && name[j - 1] == ' ')
155 static int JoystickPositionPercent(int center, int border, int actual)
160 if (border < center && actual > center)
162 if (border > center && actual < center)
165 range = ABS(border - center);
166 position = ABS(actual - center);
168 percent = (int)(position * 100 / range);
176 void CheckJoystickData(void)
181 for (i = 0; i < MAX_PLAYERS; i++)
183 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
184 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
185 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
186 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
188 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
189 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
190 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
191 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
195 int JoystickExt(int player_nr, boolean use_as_joystick_nr)
197 int joystick_nr = joystick.nr[player_nr];
199 boolean js_b1, js_b2;
200 int left, right, up, down;
201 int result = JOY_NO_ACTION;
203 if (use_as_joystick_nr)
204 joystick_nr = player_nr;
206 if (joystick.status != JOYSTICK_ACTIVATED)
207 return JOY_NO_ACTION;
210 return JOY_NO_ACTION;
212 if (!ReadJoystick(joystick_nr, &js_x, &js_y, &js_b1, &js_b2))
214 Warn("cannot read joystick device '%s'",
215 setup.input[player_nr].joy.device_name);
217 joystick.status = JOYSTICK_NOT_AVAILABLE;
219 return JOY_NO_ACTION;
222 left = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
223 setup.input[player_nr].joy.xleft, js_x);
224 right = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
225 setup.input[player_nr].joy.xright, js_x);
226 up = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
227 setup.input[player_nr].joy.yupper, js_y);
228 down = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
229 setup.input[player_nr].joy.ylower, js_y);
231 if (left > JOYSTICK_PERCENT)
233 else if (right > JOYSTICK_PERCENT)
235 if (up > JOYSTICK_PERCENT)
237 else if (down > JOYSTICK_PERCENT)
241 result |= JOY_BUTTON_1;
243 result |= JOY_BUTTON_2;
248 int Joystick(int player_nr)
250 return JoystickExt(player_nr, FALSE);
253 static int JoystickButtonExt(int player_nr, boolean use_as_joystick_nr)
255 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
256 int joy_button = (JoystickExt(player_nr, use_as_joystick_nr) & JOY_BUTTON);
261 if (last_joy_button[player_nr])
262 result = JOY_BUTTON_PRESSED;
264 result = JOY_BUTTON_NEW_PRESSED;
268 if (last_joy_button[player_nr])
269 result = JOY_BUTTON_NEW_RELEASED;
271 result = JOY_BUTTON_NOT_PRESSED;
274 last_joy_button[player_nr] = joy_button;
278 int JoystickButton(int player_nr)
280 return JoystickButtonExt(player_nr, FALSE);
283 int AnyJoystick(void)
288 for (i = 0; i < MAX_PLAYERS; i++)
289 result |= JoystickExt(i, TRUE);
294 int AnyJoystickButton(void)
297 int result = JOY_BUTTON_NOT_PRESSED;
299 for (i = 0; i < MAX_PLAYERS; i++)
301 result = JoystickButtonExt(i, TRUE);
302 if (result != JOY_BUTTON_NOT_PRESSED)
309 void DeactivateJoystick(void)
311 /* Temporarily deactivate joystick. This is needed for calibration
312 screens, where the player has to select a joystick device that
313 should be calibrated. If there is a totally uncalibrated joystick
314 active, it may be impossible (due to messed up input from joystick)
315 to select the joystick device to calibrate even when trying to use
316 the mouse or keyboard to select the device. */
318 if (joystick.status & JOYSTICK_AVAILABLE)
319 joystick.status &= ~JOYSTICK_ACTIVE;
322 void ActivateJoystick(void)
324 // reactivate temporarily deactivated joystick
326 if (joystick.status & JOYSTICK_AVAILABLE)
327 joystick.status |= JOYSTICK_ACTIVE;