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 int getJoystickNrFromDeviceName(char *device_name)
29 if (device_name == NULL || device_name[0] == '\0')
32 c = device_name[strlen(device_name) - 1];
34 if (c >= '0' && c <= '9')
35 joystick_nr = (int)(c - '0');
37 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
43 char *getDeviceNameFromJoystickNr(int joystick_nr)
45 static char *joystick_device_name[MAX_PLAYERS] =
53 return (joystick_nr >= 0 && joystick_nr <= 3 ?
54 joystick_device_name[joystick_nr] : "");
57 char *getFormattedJoystickName(const char *name_raw)
59 static char name[MAX_JOYSTICK_NAME_LEN + 1];
60 boolean name_skip_space = TRUE;
64 name_raw = "(unknown joystick)";
66 // copy joystick name, cutting leading and multiple spaces
67 for (i = 0, j = 0; i < strlen(name_raw) && i < MAX_JOYSTICK_NAME_LEN; i++)
69 if (name_raw[i] != ' ')
71 name[j++] = name_raw[i];
72 name_skip_space = FALSE;
74 else if (!name_skip_space)
76 name[j++] = name_raw[i];
77 name_skip_space = TRUE;
82 if (j > 0 && name[j - 1] == ' ')
90 static int JoystickPositionPercent(int center, int border, int actual)
95 if (border < center && actual > center)
97 if (border > center && actual < center)
100 range = ABS(border - center);
101 position = ABS(actual - center);
103 percent = (int)(position * 100 / range);
111 int JoystickExt(int player_nr, boolean use_as_joystick_nr)
113 int joystick_nr = joystick.nr[player_nr];
115 boolean js_b1, js_b2;
116 int left, right, up, down;
117 int result = JOY_NO_ACTION;
119 if (use_as_joystick_nr)
120 joystick_nr = player_nr;
122 if (joystick.status != JOYSTICK_ACTIVATED)
123 return JOY_NO_ACTION;
126 return JOY_NO_ACTION;
128 if (!ReadJoystick(joystick_nr, &js_x, &js_y, &js_b1, &js_b2))
130 Warn("cannot read joystick device '%s'",
131 setup.input[player_nr].joy.device_name);
133 joystick.status = JOYSTICK_NOT_AVAILABLE;
135 return JOY_NO_ACTION;
138 left = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
139 setup.input[player_nr].joy.xleft, js_x);
140 right = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
141 setup.input[player_nr].joy.xright, js_x);
142 up = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
143 setup.input[player_nr].joy.yupper, js_y);
144 down = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
145 setup.input[player_nr].joy.ylower, js_y);
147 if (left > JOYSTICK_PERCENT)
149 else if (right > JOYSTICK_PERCENT)
151 if (up > JOYSTICK_PERCENT)
153 else if (down > JOYSTICK_PERCENT)
157 result |= JOY_BUTTON_1;
159 result |= JOY_BUTTON_2;
164 int Joystick(int player_nr)
166 return JoystickExt(player_nr, FALSE);
169 static int JoystickButtonExt(int player_nr, boolean use_as_joystick_nr)
171 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
172 int joy_button = (JoystickExt(player_nr, use_as_joystick_nr) & JOY_BUTTON);
177 if (last_joy_button[player_nr])
178 result = JOY_BUTTON_PRESSED;
180 result = JOY_BUTTON_NEW_PRESSED;
184 if (last_joy_button[player_nr])
185 result = JOY_BUTTON_NEW_RELEASED;
187 result = JOY_BUTTON_NOT_PRESSED;
190 last_joy_button[player_nr] = joy_button;
194 int JoystickButton(int player_nr)
196 return JoystickButtonExt(player_nr, FALSE);
199 int AnyJoystick(void)
204 for (i = 0; i < MAX_PLAYERS; i++)
205 result |= JoystickExt(i, TRUE);
210 int AnyJoystickButton(void)
213 int result = JOY_BUTTON_NOT_PRESSED;
215 for (i = 0; i < MAX_PLAYERS; i++)
217 result = JoystickButtonExt(i, TRUE);
218 if (result != JOY_BUTTON_NOT_PRESSED)