|
Lines 1-455
Link Here
|
| 1 |
--- unix/unix.cpp 2007-04-28 19:51:08.000000000 -0500 |
|
|
| 2 |
+++ unix/unix.cpp 2007-10-03 16:53:16.000000000 -0500 |
| 3 |
@@ -173,7 +173,7 @@ |
| 4 |
#include <fcntl.h> |
| 5 |
#include <sys/time.h> |
| 6 |
/* FIXME: Linux alpha (aristocat) has a broken timercmp. Make testcase */ |
| 7 |
-#if defined(__linux) |
| 8 |
+#if defined(__linux) || defined(__FreeBSD__) |
| 9 |
# ifndef timercmp |
| 10 |
# define timercmp(tvp, uvp, cmp)\ |
| 11 |
((tvp)->tv_sec cmp (uvp)->tv_sec ||\ |
| 12 |
@@ -197,7 +197,7 @@ |
| 13 |
pthread_mutex_t mutex; |
| 14 |
#endif |
| 15 |
|
| 16 |
-#if !defined(NOSOUND) && defined(__linux) |
| 17 |
+#if !defined(NOSOUND) && (defined(__linux) || defined(__FreeBSD__)) |
| 18 |
#include <sys/soundcard.h> |
| 19 |
#include <sys/mman.h> |
| 20 |
#endif |
| 21 |
@@ -210,6 +210,11 @@ |
| 22 |
#endif |
| 23 |
#endif |
| 24 |
|
| 25 |
+#if defined(__FreeBSD__) |
| 26 |
+typedef sig_t SIG_PF; |
| 27 |
+#include <err.h> |
| 28 |
+#endif |
| 29 |
+ |
| 30 |
//Breaks sol9 and probably others. |
| 31 |
//#if defined(__sun) && defined(__GNUC__) |
| 32 |
//typedef void (*SIG_PF)(); |
| 33 |
@@ -233,11 +238,12 @@ |
| 34 |
|
| 35 |
#ifdef NETPLAY_SUPPORT |
| 36 |
#include "netplay.h" |
| 37 |
- |
| 38 |
#ifdef _DEBUG |
| 39 |
#define NP_DEBUG 2 |
| 40 |
#endif |
| 41 |
+#endif |
| 42 |
|
| 43 |
+#if defined(NETPLAY_SUPPORT) || defined(JOYSTICK_SUPPORT) |
| 44 |
/* accessors into controls.cpp static variables */ |
| 45 |
uint16 MovieGetJoypad(int i); |
| 46 |
void MovieSetJoypad(int i, uint16 buttons); |
| 47 |
@@ -245,22 +251,86 @@ |
| 48 |
/* Joypads status information */ |
| 49 |
uint32 old_joypads[8]; |
| 50 |
uint32 joypads[8]; |
| 51 |
- |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifdef JOYSTICK_SUPPORT |
| 55 |
+#define JOYSTICK_MAX_DEVICES 4 |
| 56 |
+int JoystickShift = 0; |
| 57 |
+uint8 js_mod [8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 58 |
#if defined(__linux) |
| 59 |
+#define JOYSTICK_MAX_BUTTONS 4 |
| 60 |
#include <linux/joystick.h> |
| 61 |
-uint8 js_mod [8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 62 |
int js_fd [8] = {-1, -1, -1, -1, -1, -1, -1, -1}; |
| 63 |
char *js_device [8] = {"/dev/js0", "/dev/js1", "/dev/js2", "/dev/js3", "/dev/js4", "/dev/js5", "/dev/js6", "/dev/js7" }; |
| 64 |
#endif |
| 65 |
|
| 66 |
+#if defined(__FreeBSD__) |
| 67 |
+#include <sys/types.h> |
| 68 |
+#include <sys/param.h> |
| 69 |
+ |
| 70 |
+extern "C" { |
| 71 |
+#define class klass |
| 72 |
+#include <dev/usb/usb.h> |
| 73 |
+#include <dev/usb/usbhid.h> |
| 74 |
+#undef class |
| 75 |
+ |
| 76 |
+#if __FreeBSD_version < 500111 |
| 77 |
+#include <libusbhid.h> |
| 78 |
+#else |
| 79 |
+#include <usbhid.h> |
| 80 |
+#endif |
| 81 |
+} |
| 82 |
+ |
| 83 |
+#define JOYSTICK_MAX_BUTTONS 16 |
| 84 |
+ |
| 85 |
+#define JOYSTICK_MAP_BUTTON_INITIALIZER \ |
| 86 |
+ { \ |
| 87 |
+ SNES_Y_MASK, SNES_B_MASK, SNES_A_MASK, \ |
| 88 |
+ SNES_X_MASK, SNES_TL_MASK, SNES_TR_MASK, \ |
| 89 |
+ SNES_TL_MASK, SNES_TR_MASK, \ |
| 90 |
+ SNES_SELECT_MASK, SNES_START_MASK, 0, 0, 0, 0, 0, 0 \ |
| 91 |
+ } |
| 92 |
+ |
| 93 |
+int js_map_button[JOYSTICK_MAX_BUTTONS][16] = { |
| 94 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 95 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 96 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 97 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 98 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 99 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 100 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 101 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 102 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 103 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 104 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 105 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 106 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 107 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 108 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER, |
| 109 |
+ JOYSTICK_MAP_BUTTON_INITIALIZER |
| 110 |
+}; |
| 111 |
+char *js_device [4] = {"/dev/uhid0", "/dev/uhid1", "/dev/uhid2", "/dev/uhid3"}; |
| 112 |
+struct js_range { |
| 113 |
+ int min, max, lower_third, higher_third; |
| 114 |
+}; |
| 115 |
+struct js_info { |
| 116 |
+ char *buf; |
| 117 |
+ int reportlen; |
| 118 |
+ int offset; |
| 119 |
+ struct js_range x, y; |
| 120 |
+ int buttons; |
| 121 |
+ struct hid_item *hids; |
| 122 |
+ int fd; |
| 123 |
+ int joy; |
| 124 |
+} js_info[4]; |
| 125 |
+int js_count = 0; |
| 126 |
+#endif |
| 127 |
+ |
| 128 |
void InitJoysticks (); |
| 129 |
void ReadJoysticks (); |
| 130 |
#endif |
| 131 |
|
| 132 |
-#if !defined(NOSOUND) && defined (__linux) |
| 133 |
+#if !defined(NOSOUND) && (defined (__linux) || defined(__FreeBSD__)) |
| 134 |
#define CONFIGURABLE_SOUND_DEVICE |
| 135 |
char *sound_device = "/dev/dsp"; |
| 136 |
#endif |
| 137 |
@@ -285,7 +355,7 @@ |
| 138 |
const char *inc_format="%03d"; |
| 139 |
|
| 140 |
//FIXME: I see no reason not to configureenable this for all Unixen |
| 141 |
-#if defined(DEBUGGER) && (defined(__linux) || defined(__sun)) |
| 142 |
+#if defined(DEBUGGER) && (defined(__linux) || defined(__sun) || defined(__FreeBSD__)) |
| 143 |
static void sigbrkhandler(int) |
| 144 |
{ |
| 145 |
CPU.Flags |= DEBUG_MODE_FLAG; |
| 146 |
@@ -326,22 +396,54 @@ |
| 147 |
|
| 148 |
void S9xParseArg (char **argv, int &i, int argc) |
| 149 |
{ |
| 150 |
+#ifdef JOYSTICK_SUPPORT |
| 151 |
if (strcmp (argv [i], "-j") == 0 || |
| 152 |
strcasecmp (argv [i], "-nojoy") == 0) { |
| 153 |
-#ifdef JOYSTICK_SUPPORT |
| 154 |
Settings.JoystickEnabled = FALSE; |
| 155 |
+ } |
| 156 |
+ |
| 157 |
+#ifdef __FreeBSD__ |
| 158 |
+ else if (strcasecmp(argv[i], "-joyshift") == 0) |
| 159 |
+ JoystickShift = 1; |
| 160 |
+#endif |
| 161 |
+ else if (strncasecmp(argv[i], "-joydev", sizeof("-joydev") - 1) == 0) { |
| 162 |
+ char *end, *snum = argv[i] + sizeof("-joydev") - 1; |
| 163 |
+ unsigned long num; |
| 164 |
+ |
| 165 |
+ if (*snum == '\0' || argc < i + 1) |
| 166 |
+ S9xUsage(); |
| 167 |
+ if ((num = strtoul(snum, &end, 10)) < JOYSTICK_MAX_DEVICES && |
| 168 |
+ num > 0 && *end == '\0') |
| 169 |
+ js_device[num - 1] = argv[++i]; |
| 170 |
+ else |
| 171 |
+ S9xUsage (); |
| 172 |
+ } |
| 173 |
+ else if (strncasecmp(argv[i], "-joymap", sizeof("-joymap") - 1) == 0) { |
| 174 |
+ static const int button_masks[] = { |
| 175 |
+ SNES_A_MASK, SNES_B_MASK, SNES_X_MASK, SNES_Y_MASK, |
| 176 |
+ SNES_TL_MASK, SNES_TR_MASK, SNES_START_MASK, SNES_SELECT_MASK |
| 177 |
+ }; |
| 178 |
+ char *end, *snum = argv[i] + sizeof("-joymap") - 1; |
| 179 |
+ unsigned long num; |
| 180 |
+ int *js_buttons, b; |
| 181 |
+ |
| 182 |
+ if (argc < i + (sizeof(button_masks) / sizeof(int)) || *snum == '\0') |
| 183 |
+ S9xUsage(); |
| 184 |
+ num = strtoul(snum, &end, 10); |
| 185 |
+ if (*end != '\0') |
| 186 |
+ S9xUsage(); |
| 187 |
+ if (num == 0 || num > 4) /* hardcode: max joysticks */ |
| 188 |
+ S9xUsage(); |
| 189 |
+ js_buttons = js_map_button[num - 1]; |
| 190 |
+ for (b = 0; b < (sizeof(button_masks) / sizeof(int)); b++) |
| 191 |
+ if ((num = strtoul(argv[++i], &end, 10)) <= 16 && num != 0 && |
| 192 |
+ *end == '\0') |
| 193 |
+ js_buttons[num - 1] = button_masks[b]; |
| 194 |
+ } |
| 195 |
+ |
| 196 |
+ else |
| 197 |
#endif |
| 198 |
- } else if (strncasecmp (argv [i], "-joydev", 7) == 0 && argv[i][7]>='1' && argv[i][7]<='8' && argv[i][8]=='\0') |
| 199 |
- { |
| 200 |
- int j = argv[i][7]-'1'; |
| 201 |
- if (i + 1 < argc) { |
| 202 |
-#ifdef JOYSTICK_SUPPORT |
| 203 |
- js_device[j] = argv[++i]; |
| 204 |
-#else |
| 205 |
- S9xMessage(S9X_WARNING, S9X_USAGE, "Joystick support is not available!"); |
| 206 |
-#endif |
| 207 |
- } else S9xUsage (); |
| 208 |
- } else if (strcasecmp (argv [i], "-sounddev") == 0) { |
| 209 |
+ if (strcasecmp (argv [i], "-sounddev") == 0) { |
| 210 |
if (i + 1 < argc) { |
| 211 |
#if defined(CONFIGURABLE_SOUND_DEVICE) |
| 212 |
sound_device = argv[++i]; |
| 213 |
@@ -552,7 +654,7 @@ |
| 214 |
#if !defined(__MSDOS) && defined(DEBUGGER) |
| 215 |
#if defined(__unix) && !defined(__NeXT__) |
| 216 |
struct sigaction sa; |
| 217 |
-#if defined(__linux) |
| 218 |
+#if defined(__linux) || defined(__FreeBSD__) |
| 219 |
sa.sa_handler = sigbrkhandler; |
| 220 |
#else |
| 221 |
sa.sa_handler = (SIG_PF) sigbrkhandler; |
| 222 |
@@ -770,6 +872,7 @@ |
| 223 |
} |
| 224 |
|
| 225 |
#ifdef JOYSTICK_SUPPORT |
| 226 |
+#ifdef __linux |
| 227 |
void InitJoysticks () |
| 228 |
{ |
| 229 |
#ifdef JSIOCGVERSION |
| 230 |
@@ -854,6 +957,189 @@ |
| 231 |
} |
| 232 |
#endif |
| 233 |
} |
| 234 |
+#endif |
| 235 |
+#if defined(__FreeBSD__) |
| 236 |
+ |
| 237 |
+/* |
| 238 |
+ * USB HID code for FreeBSD/NetBSD |
| 239 |
+ * Brian Feldman <green@FreeBSD.org> |
| 240 |
+ * |
| 241 |
+ * Reference used: |
| 242 |
+ * X-Mame USB HID joystick driver for NetBSD. |
| 243 |
+ * |
| 244 |
+ * Written by Krister Walfridsson <cato@df.lth.se> |
| 245 |
+ */ |
| 246 |
+ |
| 247 |
+int |
| 248 |
+InitJoystickFd(int fd, char *dev) { |
| 249 |
+ report_desc_t rd; |
| 250 |
+ struct hid_data *d; |
| 251 |
+ struct hid_item h, new_h; |
| 252 |
+ struct js_info *my_info; |
| 253 |
+ int reportlen, report_id, is_joystick; |
| 254 |
+ |
| 255 |
+ my_info = &js_info[js_count]; |
| 256 |
+ rd = hid_get_report_desc(fd); |
| 257 |
+ if (rd == 0) { |
| 258 |
+ warn("%s", dev); |
| 259 |
+ return (-1); |
| 260 |
+ } |
| 261 |
+ report_id = 0; |
| 262 |
+#if __FreeBSD_version < 500111 |
| 263 |
+ reportlen = hid_report_size(rd, report_id, hid_input); |
| 264 |
+#else |
| 265 |
+ reportlen = hid_report_size(rd, hid_input, report_id); |
| 266 |
+#endif |
| 267 |
+ my_info->buf = (char *)malloc(reportlen); |
| 268 |
+ if (my_info->buf == NULL) |
| 269 |
+ err(1, "malloc"); |
| 270 |
+ my_info->reportlen = reportlen; |
| 271 |
+ my_info->offset = report_id != 0; |
| 272 |
+ my_info->buttons = 0; |
| 273 |
+ my_info->hids = NULL; |
| 274 |
+ |
| 275 |
+ is_joystick = 0; |
| 276 |
+#if __FreeBSD_version < 500111 |
| 277 |
+ for (d = hid_start_parse(rd, 1 << hid_input); |
| 278 |
+#else |
| 279 |
+ for (d = hid_start_parse(rd, 1 << hid_input, report_id); |
| 280 |
+#endif |
| 281 |
+ hid_get_item(d, &h);) { |
| 282 |
+ int usage, page, link_it = 0; |
| 283 |
+ struct js_range *axis; |
| 284 |
+ |
| 285 |
+ page = HID_PAGE(h.usage); |
| 286 |
+ usage = HID_USAGE(h.usage); |
| 287 |
+ if (!is_joystick) |
| 288 |
+ is_joystick = (h.kind == hid_collection && |
| 289 |
+ page == HUP_GENERIC_DESKTOP && |
| 290 |
+ (usage == HUG_JOYSTICK || usage == HUG_GAME_PAD)); |
| 291 |
+ if (h.kind != hid_input || !is_joystick) |
| 292 |
+ continue; |
| 293 |
+ axis = NULL; |
| 294 |
+ if (page == HUP_GENERIC_DESKTOP) { |
| 295 |
+ switch (usage) { |
| 296 |
+ case HUG_X: |
| 297 |
+ case HUG_RX: |
| 298 |
+ axis = &my_info->x; |
| 299 |
+ break; |
| 300 |
+ case HUG_Y: |
| 301 |
+ case HUG_RY: |
| 302 |
+ axis = &my_info->y; |
| 303 |
+ break; |
| 304 |
+ } |
| 305 |
+ } |
| 306 |
+ if (axis != NULL) { |
| 307 |
+ axis->min = h.logical_minimum; |
| 308 |
+ axis->max = h.logical_maximum; |
| 309 |
+ axis->lower_third = axis->min + |
| 310 |
+ (axis->max - axis->min) / 3; |
| 311 |
+ axis->higher_third = axis->min + |
| 312 |
+ (axis->max - axis->min) * 2 / 3; |
| 313 |
+ link_it = 1; |
| 314 |
+ } |
| 315 |
+ if (!link_it) { |
| 316 |
+ if (page != HUP_BUTTON || usage > 16) |
| 317 |
+ continue; |
| 318 |
+ if (usage > my_info->buttons) |
| 319 |
+ my_info->buttons = usage; |
| 320 |
+ } |
| 321 |
+ new_h = h; |
| 322 |
+ new_h.next = my_info->hids; |
| 323 |
+ my_info->hids = (struct hid_item *) |
| 324 |
+ malloc(sizeof(*my_info->hids)); |
| 325 |
+ if (my_info->hids == NULL) |
| 326 |
+ err(1, "malloc"); |
| 327 |
+ *my_info->hids = new_h; |
| 328 |
+ } |
| 329 |
+ printf("Joystick %s: %d buttons, X range %d - %d, Y range %d - %d\n", |
| 330 |
+ dev, my_info->buttons, my_info->x.min, my_info->x.max, |
| 331 |
+ my_info->y.min, my_info->y.max); |
| 332 |
+ my_info->fd = fd; |
| 333 |
+ if (JoystickShift) { |
| 334 |
+ my_info->joy = js_count + 1; |
| 335 |
+ } else |
| 336 |
+ my_info->joy = js_count; |
| 337 |
+ return (0); |
| 338 |
+} |
| 339 |
+ |
| 340 |
+void |
| 341 |
+InitJoysticks() { |
| 342 |
+ int i, fd; |
| 343 |
+ |
| 344 |
+ for (i = 0; i < (sizeof(js_device) / sizeof(js_device[0])); i++) { |
| 345 |
+ fd = open(js_device[i], O_RDONLY | O_NONBLOCK); |
| 346 |
+ if (fd == -1) |
| 347 |
+ continue; |
| 348 |
+ if (InitJoystickFd(fd, js_device[i]) == 0) |
| 349 |
+ js_count++; |
| 350 |
+ else |
| 351 |
+ close(fd); |
| 352 |
+ } |
| 353 |
+} |
| 354 |
+ |
| 355 |
+void |
| 356 |
+ReadJoysticks() { |
| 357 |
+ struct hid_item *h; |
| 358 |
+ int page, usage, njoy, joy; |
| 359 |
+ |
| 360 |
+ for (njoy = 0; njoy < js_count; njoy++) { |
| 361 |
+ struct js_info *jsi; |
| 362 |
+ |
| 363 |
+ jsi = &js_info[njoy]; |
| 364 |
+ joy = jsi->joy; |
| 365 |
+ |
| 366 |
+ if (read(jsi->fd, jsi->buf, jsi->reportlen) != jsi->reportlen) |
| 367 |
+ continue; |
| 368 |
+ for (h = jsi->hids; h != NULL; h = h->next) { |
| 369 |
+ int d; |
| 370 |
+ |
| 371 |
+ d = hid_get_data(jsi->buf + jsi->offset, h); |
| 372 |
+ page = HID_PAGE(h->usage); |
| 373 |
+ usage = HID_USAGE(h->usage); |
| 374 |
+ |
| 375 |
+ switch (page) { |
| 376 |
+ case HUP_GENERIC_DESKTOP: |
| 377 |
+ switch (usage) { |
| 378 |
+ case HUG_X: |
| 379 |
+ case HUG_RX: |
| 380 |
+ if (d < jsi->x.lower_third) { |
| 381 |
+ joypads[joy] |= SNES_LEFT_MASK; |
| 382 |
+ joypads[joy] &= ~SNES_RIGHT_MASK; |
| 383 |
+ } else if (d < jsi->x.higher_third) { |
| 384 |
+ joypads[joy] &= ~SNES_LEFT_MASK; |
| 385 |
+ joypads[joy] &= ~SNES_RIGHT_MASK; |
| 386 |
+ } else { |
| 387 |
+ joypads[joy] &= ~SNES_LEFT_MASK; |
| 388 |
+ joypads[joy] |= SNES_RIGHT_MASK; |
| 389 |
+ } |
| 390 |
+ break; |
| 391 |
+ case HUG_Y: |
| 392 |
+ case HUG_RY: |
| 393 |
+ if (d < jsi->y.lower_third) { |
| 394 |
+ joypads[joy] |= SNES_UP_MASK; |
| 395 |
+ joypads[joy] &= ~SNES_DOWN_MASK; |
| 396 |
+ } else if (d < jsi->y.higher_third) { |
| 397 |
+ joypads[joy] &= ~SNES_UP_MASK; |
| 398 |
+ joypads[joy] &= ~SNES_DOWN_MASK; |
| 399 |
+ } else { |
| 400 |
+ joypads[joy] &= ~SNES_UP_MASK; |
| 401 |
+ joypads[joy] |= SNES_DOWN_MASK; |
| 402 |
+ } |
| 403 |
+ break; |
| 404 |
+ } |
| 405 |
+ break; |
| 406 |
+ case HUP_BUTTON: |
| 407 |
+ if (d) |
| 408 |
+ joypads[joy] |= js_map_button[njoy][usage - 1]; |
| 409 |
+ else |
| 410 |
+ joypads[joy] &= ~js_map_button[njoy][usage - 1]; |
| 411 |
+ break; |
| 412 |
+ } |
| 413 |
+ } |
| 414 |
+ } |
| 415 |
+} |
| 416 |
+#endif // defined(__FreeBSD__) |
| 417 |
#endif // defined (JOYSTICK_SUPPORT) |
| 418 |
|
| 419 |
START_EXTERN_C |
| 420 |
@@ -1716,7 +2002,7 @@ |
| 421 |
} |
| 422 |
#endif |
| 423 |
|
| 424 |
-#if !defined(NOSOUND) && defined(__linux) |
| 425 |
+#if !defined(NOSOUND) && (defined(__linux) || defined(__FreeBSD__)) |
| 426 |
static int Rates[8] = |
| 427 |
{ |
| 428 |
0, 8000, 11025, 16000, 22050, 32000, 44100, 48000 |
| 429 |
@@ -1847,7 +2133,7 @@ |
| 430 |
#endif |
| 431 |
|
| 432 |
|
| 433 |
-#if !defined(NOSOUND) && (defined (__linux) || defined (__sun)) |
| 434 |
+#if !defined(NOSOUND) && (defined (__linux) || defined (__sun) || defined(__FreeBSD__)) |
| 435 |
void S9xUnixProcessSound (void) |
| 436 |
{ |
| 437 |
} |
| 438 |
@@ -1890,7 +2176,7 @@ |
| 439 |
} |
| 440 |
#endif |
| 441 |
|
| 442 |
-#if !defined(NOSOUND) && (defined (__linux) || defined (__sun)) |
| 443 |
+#if !defined(NOSOUND) && (defined (__linux) || defined (__sun) || defined(__FreeBSD__)) |
| 444 |
void S9xGenerateSound () |
| 445 |
{ |
| 446 |
/* Linux and Sun versions */ |
| 447 |
@@ -1985,7 +2271,7 @@ |
| 448 |
/* If not, this will be called by timer */ |
| 449 |
if (Settings.Mute) |
| 450 |
return NULL; |
| 451 |
-#ifdef __linux |
| 452 |
+#if defined(__linux) || defined(__FreeBSD__) |
| 453 |
audio_buf_info info; |
| 454 |
|
| 455 |
if (!Settings.ThreadSound && |