Added support for the EVORETRO GameCube Adapter in PC mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index e67bd57..02b2f3b 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -574,7 +574,8 @@ static ControllerMapping_t *SDL_CreateMappingForHIDAPIController(SDL_JoystickGUI
SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
- if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) {
+ if ((vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) ||
+ (vendor == USB_VENDOR_SHENZHEN && product == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER)) {
/* GameCube driver has 12 buttons and 6 axes */
SDL_strlcat(mapping_string, "a:b0,b:b1,dpdown:b6,dpleft:b4,dpright:b5,dpup:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b9,righttrigger:a5,rightx:a2,righty:a3,start:b8,x:b2,y:b3,", sizeof(mapping_string));
} else {
diff --git a/src/joystick/hidapi/SDL_hidapi_gamecube.c b/src/joystick/hidapi/SDL_hidapi_gamecube.c
index 51e8ba9..91ca67a 100644
--- a/src/joystick/hidapi/SDL_hidapi_gamecube.c
+++ b/src/joystick/hidapi/SDL_hidapi_gamecube.c
@@ -37,9 +37,13 @@
#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
+/* Define this if you want to log all packets from the controller */
+/*#define DEBUG_GAMECUBE_PROTOCOL*/
+
#define MAX_CONTROLLERS 4
typedef struct {
+ SDL_bool pc_mode;
SDL_JoystickID joysticks[MAX_CONTROLLERS];
Uint8 wireless[MAX_CONTROLLERS];
Uint8 min_axis[MAX_CONTROLLERS*SDL_CONTROLLER_AXIS_MAX];
@@ -58,6 +62,10 @@ HIDAPI_DriverGameCube_IsSupportedDevice(const char *name, SDL_GameControllerType
/* Nintendo Co., Ltd. Wii U GameCube Controller Adapter */
return SDL_TRUE;
}
+ if (vendor_id == USB_VENDOR_SHENZHEN && product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER) {
+ /* EVORETRO GameCube Controller Adapter */
+ return SDL_TRUE;
+ }
return SDL_FALSE;
}
@@ -86,7 +94,7 @@ static float fsel(float fComparand, float fValGE, float fLT)
static float RemapVal(float val, float A, float B, float C, float D)
{
if (A == B) {
- return fsel(val - B , D , C);
+ return fsel(val - B, D, C);
}
if (val < A) {
val = A;
@@ -154,40 +162,54 @@ HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
ctx->joysticks[3] = -1;
ctx->rumble[0] = rumbleMagic;
- /* This is all that's needed to initialize the device. Really! */
- if (hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {
- SDL_SetError("Couldn't initialize WUP-028");
- goto error;
+ if (device->vendor_id != USB_VENDOR_NINTENDO) {
+ ctx->pc_mode = SDL_TRUE;
}
- /* Wait for the adapter to initialize */
- SDL_Delay(10);
-
- /* Add all the applicable joysticks */
- while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
- if (size < 37 || packet[0] != 0x21) {
- continue; /* Nothing to do yet...? */
+ if (ctx->pc_mode) {
+ for (i = 0; i < MAX_CONTROLLERS; ++i) {
+ ResetAxisRange(ctx, i);
+ HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
+ }
+ } else {
+ /* This is all that's needed to initialize the device. Really! */
+ if (hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {
+ SDL_SetError("Couldn't initialize WUP-028");
+ goto error;
}
- /* Go through all 4 slots */
- curSlot = packet + 1;
- for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
- ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
+ /* Wait for the adapter to initialize */
+ SDL_Delay(10);
- /* Only allow rumble if the adapter's second USB cable is connected */
- ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
+ /* Add all the applicable joysticks */
+ while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
+#ifdef DEBUG_GAMECUBE_PROTOCOL
+ HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
+#endif
+ if (size < 37 || packet[0] != 0x21) {
+ continue; /* Nothing to do yet...? */
+ }
- if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
- if (ctx->joysticks[i] == -1) {
- ResetAxisRange(ctx, i);
- HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
- }
- } else {
- if (ctx->joysticks[i] != -1) {
- HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
- ctx->joysticks[i] = -1;
+ /* Go through all 4 slots */
+ curSlot = packet + 1;
+ for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
+ ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
+
+ /* Only allow rumble if the adapter's second USB cable is connected */
+ ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
+
+ if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
+ if (ctx->joysticks[i] == -1) {
+ ResetAxisRange(ctx, i);
+ HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
+ }
+ } else {
+ if (ctx->joysticks[i] != -1) {
+ HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
+ ctx->joysticks[i] = -1;
+ }
+ continue;
}
- continue;
}
}
}
@@ -233,90 +255,163 @@ HIDAPI_DriverGameCube_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_Joysti
{
}
-static SDL_bool
-HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)
+static void
+HIDAPI_DriverGameCube_HandleJoystickPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)
+{
+ SDL_Joystick *joystick;
+ Uint8 i, v;
+ Sint16 axis_value;
+
+ if (size != 10) {
+ return; /* How do we handle this packet? */
+ }
+
+ i = packet[0] - 1;
+ if (i >= MAX_CONTROLLERS) {
+ return; /* How do we handle this packet? */
+ }
+
+ joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
+
+ #define READ_BUTTON(off, flag, button) \
+ SDL_PrivateJoystickButton( \
+ joystick, \
+ RemapButton(ctx, button), \
+ (packet[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
+ );
+ READ_BUTTON(1, 0x02, 0) /* A */
+ READ_BUTTON(1, 0x04, 1) /* B */
+ READ_BUTTON(1, 0x01, 2) /* X */
+ READ_BUTTON(1, 0x08, 3) /* Y */
+ READ_BUTTON(2, 0x80, 4) /* DPAD_LEFT */
+ READ_BUTTON(2, 0x20, 5) /* DPAD_RIGHT */
+ READ_BUTTON(2, 0x40, 6) /* DPAD_DOWN */
+ READ_BUTTON(2, 0x10, 7) /* DPAD_UP */
+ READ_BUTTON(2, 0x02, 8) /* START */
+ READ_BUTTON(1, 0x80, 9) /* RIGHTSHOULDER */
+ /* These two buttons are for the bottoms of the analog triggers.
+ * More than likely, you're going to want to read the axes instead!
+ * -flibit
+ */
+ READ_BUTTON(1, 0x20, 10) /* TRIGGERRIGHT */
+ READ_BUTTON(1, 0x10, 11) /* TRIGGERLEFT */
+ #undef READ_BUTTON
+
+ #define READ_AXIS(off, axis, invert) \
+ v = invert ? (0xff - packet[off]) : packet[off]; \
+ if (v < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = v; \
+ if (v > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = v; \
+ axis_value = (Sint16)RemapVal(v, ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \
+ SDL_PrivateJoystickAxis( \
+ joystick, \
+ axis, axis_value \
+ );
+ READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX, 0)
+ READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY, 0)
+ READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTX, 1)
+ READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTY, 1)
+ READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0)
+ READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0)
+ #undef READ_AXIS
+}
+
+static void
+HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)
{
- SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
SDL_Joystick *joystick;
- Uint8 packet[37];
Uint8 *curSlot;
Uint8 i;
Sint16 axis_value;
- int size;
- /* Read input packet */
- while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
- if (size < 37 || packet[0] != 0x21) {
- continue; /* Nothing to do right now...? */
- }
+ if (size < 37 || packet[0] != 0x21) {
+ return; /* Nothing to do right now...? */
+ }
- /* Go through all 4 slots */
- curSlot = packet + 1;
- for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
- ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
+ /* Go through all 4 slots */
+ curSlot = packet + 1;
+ for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
+ ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
- /* Only allow rumble if the adapter's second USB cable is connected */
- ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
+ /* Only allow rumble if the adapter's second USB cable is connected */
+ ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
- if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
- if (ctx->joysticks[i] == -1) {
- ResetAxisRange(ctx, i);
- HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
- }
- joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
+ if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
+ if (ctx->joysticks[i] == -1) {
+ ResetAxisRange(ctx, i);
+ HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
+ }
+ joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
- /* Hasn't been opened yet, skip */
- if (joystick == NULL) {
- continue;
- }
- } else {
- if (ctx->joysticks[i] != -1) {
- HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
- ctx->joysticks[i] = -1;
- }
+ /* Hasn't been opened yet, skip */
+ if (joystick == NULL) {
continue;
}
+ } else {
+ if (ctx->joysticks[i] != -1) {
+ HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
+ ctx->joysticks[i] = -1;
+ }
+ continue;
+ }
- #define READ_BUTTON(off, flag, button) \
- SDL_PrivateJoystickButton( \
- joystick, \
- RemapButton(ctx, button), \
- (curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
- );
- READ_BUTTON(1, 0x01, 0) /* A */
- READ_BUTTON(1, 0x04, 1) /* B */
- READ_BUTTON(1, 0x02, 2) /* X */
- READ_BUTTON(1, 0x08, 3) /* Y */
- READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */
- READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */
- READ_BUTTON(1, 0x40, 6) /* DPAD_DOWN */
- READ_BUTTON(1, 0x80, 7) /* DPAD_UP */
- READ_BUTTON(2, 0x01, 8) /* START */
- READ_BUTTON(2, 0x02, 9) /* RIGHTSHOULDER */
- /* These two buttons are for the bottoms of the analog triggers.
- * More than likely, you're going to want to read the axes instead!
- * -flibit
- */
- READ_BUTTON(2, 0x04, 10) /* TRIGGERRIGHT */
- READ_BUTTON(2, 0x08, 11) /* TRIGGERLEFT */
- #undef READ_BUTTON
-
- #define READ_AXIS(off, axis) \
- if (axis < SDL_CONTROLLER_AXIS_TRIGGERLEFT) \
- if (curSlot[off] < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
- if (curSlot[off] > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
- axis_value = (Sint16)(RemapVal(curSlot[off], ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16)); \
- SDL_PrivateJoystickAxis( \
- joystick, \
- axis, axis_value \
- );
- READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX)
- READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY)
- READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTX)
- READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTY)
- READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT)
- READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
- #undef READ_AXIS
+ #define READ_BUTTON(off, flag, button) \
+ SDL_PrivateJoystickButton( \
+ joystick, \
+ RemapButton(ctx, button), \
+ (curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
+ );
+ READ_BUTTON(1, 0x01, 0) /* A */
+ READ_BUTTON(1, 0x04, 1) /* B */
+ READ_BUTTON(1, 0x02, 2) /* X */
+ READ_BUTTON(1, 0x08, 3) /* Y */
+ READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */
+ READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */
+ READ_BUTTON(1, 0x40, 6) /* DPAD_DOWN */
+ READ_BUTTON(1, 0x80, 7) /* DPAD_UP */
+ READ_BUTTON(2, 0x01, 8) /* START */
+ READ_BUTTON(2, 0x02, 9) /* RIGHTSHOULDER */
+ /* These two buttons are for the bottoms of the analog triggers.
+ * More than likely, you're going to want to read the axes instead!
+ * -flibit
+ */
+ READ_BUTTON(2, 0x04, 10) /* TRIGGERRIGHT */
+ READ_BUTTON(2, 0x08, 11) /* TRIGGERLEFT */
+ #undef READ_BUTTON
+
+ #define READ_AXIS(off, axis) \
+ if (curSlot[off] < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
+ if (curSlot[off] > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
+ axis_value = (Sint16)(RemapVal(curSlot[off], ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16)); \
+ SDL_PrivateJoystickAxis( \
+ joystick, \
+ axis, axis_value \
+ );
+ READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX)
+ READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY)
+ READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTX)
+ READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTY)
+ READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT)
+ READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
+ #undef READ_AXIS
+ }
+}
+
+static SDL_bool
+HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)
+{
+ SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
+ Uint8 packet[USB_PACKET_LENGTH];
+ int size;
+
+ /* Read input packet */
+ while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
+#ifdef DEBUG_GAMECUBE_PROTOCOL
+ //HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
+#endif
+ if (ctx->pc_mode) {
+ HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, packet, size);
+ } else {
+ HIDAPI_DriverGameCube_HandleNintendoPacket(device, ctx, packet, size);
}
}
@@ -351,6 +446,11 @@ HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *jo
{
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
Uint8 i, val;
+
+ if (ctx->pc_mode) {
+ return SDL_Unsupported();
+ }
+
for (i = 0; i < MAX_CONTROLLERS; i += 1) {
if (joystick->instance_id == ctx->joysticks[i]) {
if (ctx->wireless[i]) {
diff --git a/src/joystick/usb_ids.h b/src/joystick/usb_ids.h
index 4d452a4..ed7c640 100644
--- a/src/joystick/usb_ids.h
+++ b/src/joystick/usb_ids.h
@@ -31,10 +31,12 @@
#define USB_VENDOR_NVIDIA 0x0955
#define USB_VENDOR_PDP 0x0e6f
#define USB_VENDOR_POWERA 0x24c6
-#define USB_VENDOR_SONY 0x054c
#define USB_VENDOR_RAZER 0x1532
+#define USB_VENDOR_SHENZHEN 0x0079
+#define USB_VENDOR_SONY 0x054c
#define USB_VENDOR_VALVE 0x28de
+#define USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER 0x1846
#define USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER 0x0337
#define USB_PRODUCT_NINTENDO_SWITCH_PRO 0x2009
#define USB_PRODUCT_RAZER_PANTHERA 0x0401