diff --git a/src/i_system.h b/src/i_system.h index 644e74774..362329fa3 100644 --- a/src/i_system.h +++ b/src/i_system.h @@ -184,9 +184,13 @@ void I_JoyScale4(void); // Called by D_SRB2Main. +/** \brief to startup a joystick +*/ +void I_InitJoystick(UINT8 index); + /** \brief to startup the first joystick */ -void I_InitJoystick(void); +void I_InitJoystick1(void); /** \brief to startup the second joystick */ @@ -318,19 +322,7 @@ const char *I_LocateWad(void); /** \brief First Joystick's events */ -void I_GetJoystickEvents(void); - -/** \brief Second Joystick's events -*/ -void I_GetJoystick2Events(void); - -/** \brief Third Joystick's events -*/ -void I_GetJoystick3Events(void); - -/** \brief Fourth Joystick's events -*/ -void I_GetJoystick4Events(void); +void I_GetJoystickEvents(UINT8 index); /** \brief Mouses events */ diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 62629efdf..e20737412 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -220,41 +220,11 @@ static void JoyReset(SDLJoyInfo_t *JoySet) /** \brief First joystick up and running */ -static INT32 joystick_started = 0; +static INT32 joystick_started[MAXSPLITSCREENPLAYERS] = {0,0,0,0}; /** \brief SDL info about joystick 1 */ -SDLJoyInfo_t JoyInfo; - - -/** \brief Second joystick up and running -*/ -static INT32 joystick2_started = 0; - -/** \brief SDL info about joystick 2 -*/ -SDLJoyInfo_t JoyInfo2; - -/** \brief Third joystick up and running -*/ -static INT32 joystick3_started = 0; - -/** \brief SDL info about joystick 3 -*/ -SDLJoyInfo_t JoyInfo3; - -/** \brief Fourth joystick up and running -*/ -static INT32 joystick4_started = 0; - -/** \brief SDL info about joystick 4 -*/ -SDLJoyInfo_t JoyInfo4; - -#ifdef HAVE_TERMIOS -static INT32 fdmouse2 = -1; -static INT32 mouse2_started = 0; -#endif +SDLJoyInfo_t JoyInfo[MAXSPLITSCREENPLAYERS]; SDL_bool consolevent = SDL_FALSE; SDL_bool framebuffer = SDL_FALSE; @@ -871,26 +841,26 @@ INT32 I_GetKey (void) // void I_JoyScale(void) { - Joystick.bGamepadStyle = cv_joyscale.value==0; - JoyInfo.scale = Joystick.bGamepadStyle?1:cv_joyscale.value; + Joystick[0].bGamepadStyle = cv_joyscale[0].value==0; + JoyInfo[0].scale = Joystick[0].bGamepadStyle?1:cv_joyscale[0].value; } void I_JoyScale2(void) { - Joystick2.bGamepadStyle = cv_joyscale2.value==0; - JoyInfo2.scale = Joystick2.bGamepadStyle?1:cv_joyscale2.value; + Joystick[1].bGamepadStyle = cv_joyscale[1].value==0; + JoyInfo[1].scale = Joystick[1].bGamepadStyle?1:cv_joyscale[1].value; } void I_JoyScale3(void) { - Joystick3.bGamepadStyle = cv_joyscale3.value==0; - JoyInfo3.scale = Joystick3.bGamepadStyle?1:cv_joyscale3.value; + Joystick[2].bGamepadStyle = cv_joyscale[2].value==0; + JoyInfo[2].scale = Joystick[2].bGamepadStyle?1:cv_joyscale[2].value; } void I_JoyScale4(void) { - Joystick4.bGamepadStyle = cv_joyscale4.value==0; - JoyInfo4.scale = Joystick4.bGamepadStyle?1:cv_joyscale4.value; + Joystick[3].bGamepadStyle = cv_joyscale[3].value==0; + JoyInfo[3].scale = Joystick[3].bGamepadStyle?1:cv_joyscale[1].value; } // Cheat to get the device index for a joystick handle @@ -903,226 +873,96 @@ INT32 I_GetJoystickDeviceIndex(SDL_Joystick *dev) SDL_Joystick *test = SDL_JoystickOpen(i); if (test && test == dev) return i; - else if (JoyInfo.dev != test && JoyInfo2.dev != test && JoyInfo3.dev != test && JoyInfo4.dev != test) - SDL_JoystickClose(test); + else + { + UINT8 j; + + for (j = 0; j < MAXSPLITSCREENPLAYERS; j++) + { + if (JoyInfo[j].dev == test) + break; + } + + if (j == MAXSPLITSCREENPLAYERS) + SDL_JoystickClose(test); + } } return -1; } -// Misleading function: updates device indices for all players BUT the one specified. -// Necessary for SDL_JOYDEVICEADDED events -void I_UpdateJoystickDeviceIndices(INT32 player) +void I_UpdateJoystickDeviceIndex(UINT8 player) { - if (player != 1) // This is a fucking mess. - { - ////////////////////////////// - // update joystick 1's device index - ////////////////////////////// + /////////////////////////////////////////////// + // update this joystick's device index (wow) // + /////////////////////////////////////////////// - if (JoyInfo.dev) - cv_usejoystick.value = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; - // is cv_usejoystick used? - else if (// don't check JoyInfo or cv_usejoystick; we're currently operating on those - atoi(cv_usejoystick.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick2.value - && atoi(cv_usejoystick.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick3.value - && atoi(cv_usejoystick.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick4.value) - cv_usejoystick.value = atoi(cv_usejoystick.string); - // is cv_usejoystick2 used? - else if ( // don't check JoyInfo or cv_usejoystick; we're currently operating on those - atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick2.value - && atoi(cv_usejoystick2.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick3.value - && atoi(cv_usejoystick2.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick4.value) - cv_usejoystick.value = atoi(cv_usejoystick2.string); - // is cv_usejoystick3 used? - else if (// don't check JoyInfo or cv_usejoystick; we're currently operating on those - atoi(cv_usejoystick3.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick2.value - && atoi(cv_usejoystick3.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick3.value - && atoi(cv_usejoystick3.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick4.value) - cv_usejoystick.value = atoi(cv_usejoystick3.string); - // is cv_usejoystick4 used? - else if (// don't check JoyInfo or cv_usejoystick; we're currently operating on those - atoi(cv_usejoystick4.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick2.value - && atoi(cv_usejoystick4.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick3.value - && atoi(cv_usejoystick4.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick4.value) - cv_usejoystick.value = atoi(cv_usejoystick4.string); - else // we tried... - cv_usejoystick.value = 0; + if (JoyInfo[player].dev) + { + cv_usejoystick[player].value = I_GetJoystickDeviceIndex(JoyInfo[player].dev) + 1; } - - if (player != 2) + else { - ////////////////////////////// - // update joystick 2's device index - ////////////////////////////// + UINT8 joystickID, compareJoystick; - if (JoyInfo2.dev) - cv_usejoystick2.value = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; - // is cv_usejoystick2 used? - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick.value - // don't check JoyInfo2 or cv_usejoystick2; we're currently operating on those - && atoi(cv_usejoystick2.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick3.value - && atoi(cv_usejoystick2.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick4.value) - cv_usejoystick2.value = atoi(cv_usejoystick2.string); - // is cv_usejoystick used? - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick.value - // don't check JoyInfo2 or cv_usejoystick2; we're currently operating on those - && atoi(cv_usejoystick.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick3.value - && atoi(cv_usejoystick.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick4.value) - cv_usejoystick2.value = atoi(cv_usejoystick.string); - // is cv_usejoystick3 used? - else if (atoi(cv_usejoystick3.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick.value - // don't check JoyInfo2 or cv_usejoystick2; we're currently operating on those - && atoi(cv_usejoystick3.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick3.value - && atoi(cv_usejoystick3.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick4.value) - cv_usejoystick2.value = atoi(cv_usejoystick3.string); - // is cv_usejoystick4 used? - else if (atoi(cv_usejoystick4.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick.value - // don't check JoyInfo2 or cv_usejoystick2; we're currently operating on those - && atoi(cv_usejoystick4.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick3.value - && atoi(cv_usejoystick4.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick4.value) - cv_usejoystick2.value = atoi(cv_usejoystick4.string); - else // we tried... - cv_usejoystick2.value = 0; - } + for (joystickID = 0; joystickID < MAXSPLITSCREENPLAYERS; joystickID++) + { + // is this cv_usejoystick used? + const INT32 value = atoi(cv_usejoystick[joystickID].string); - if (player != 3) - { - ////////////////////////////// - // update joystick 3's device index - ////////////////////////////// + for (compareJoystick = 0; compareJoystick < MAXSPLITSCREENPLAYERS; compareJoystick++) + { + if (compareJoystick == player) + continue; - if (JoyInfo3.dev) - cv_usejoystick3.value = I_GetJoystickDeviceIndex(JoyInfo3.dev) + 1; - // is cv_usejoystick3 used? - else if (atoi(cv_usejoystick3.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick.value - && atoi(cv_usejoystick3.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick2.value - // don't check JoyInfo3 or cv_usejoystick3; we're currently operating on those - && atoi(cv_usejoystick3.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick4.value) - cv_usejoystick3.value = atoi(cv_usejoystick3.string); - // is cv_usejoystick used? - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick.value - && atoi(cv_usejoystick.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick2.value - // don't check JoyInfo3 or cv_usejoystick3; we're currently operating on those - && atoi(cv_usejoystick.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick4.value) - cv_usejoystick3.value = atoi(cv_usejoystick.string); - // is cv_usejoystick2 used? - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick.value - && atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick2.value - // don't check JoyInfo3 or cv_usejoystick3; we're currently operating on those - && atoi(cv_usejoystick2.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick4.value) - cv_usejoystick3.value = atoi(cv_usejoystick2.string); - // is cv_usejoystick4 used? - else if (atoi(cv_usejoystick4.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick.value - && atoi(cv_usejoystick4.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick2.value - // don't check JoyInfo3 or cv_usejoystick3; we're currently operating on those - && atoi(cv_usejoystick4.string) != JoyInfo4.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick4.value) - cv_usejoystick3.value = atoi(cv_usejoystick4.string); - else // we tried... - cv_usejoystick3.value = 0; - } + if (value == JoyInfo[compareJoystick].oldjoy || value == cv_usejoystick[compareJoystick].value) + break; + } - if (player != 4) - { - ////////////////////////////// - // update joystick 4's device index - ////////////////////////////// + if (compareJoystick == MAXSPLITSCREENPLAYERS) + { + // We DID make it through the whole loop, so we can use this one! + cv_usejoystick[player].value = value; + break; + } + } - if (JoyInfo4.dev) - cv_usejoystick4.value = I_GetJoystickDeviceIndex(JoyInfo4.dev) + 1; - // is cv_usejoystick4 used? - else if (atoi(cv_usejoystick4.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick.value - && atoi(cv_usejoystick4.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick2.value - && atoi(cv_usejoystick4.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick4.string) != cv_usejoystick3.value) - // don't check JoyInfo4 or cv_usejoystick4; we're currently operating on those - cv_usejoystick4.value = atoi(cv_usejoystick4.string); - // is cv_usejoystick used? - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick.value - && atoi(cv_usejoystick.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick2.value - && atoi(cv_usejoystick.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick3.value) - // don't check JoyInfo4 or cv_usejoystick4; we're currently operating on those - cv_usejoystick4.value = atoi(cv_usejoystick.string); - // is cv_usejoystick2 used? - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick.value - && atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick2.value - && atoi(cv_usejoystick2.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick3.value) - // don't check JoyInfo4 or cv_usejoystick4; we're currently operating on those - cv_usejoystick4.value = atoi(cv_usejoystick2.string); - // is cv_usejoystick3 used? - else if (atoi(cv_usejoystick3.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick.value - && atoi(cv_usejoystick3.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick2.value - && atoi(cv_usejoystick3.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick3.string) != cv_usejoystick3.value) - // don't check JoyInfo4 or cv_usejoystick4; we're currently operating on those - cv_usejoystick4.value = atoi(cv_usejoystick3.string); - else // we tried... - cv_usejoystick4.value = 0; + if (joystickID == MAXSPLITSCREENPLAYERS) + { + // We DID NOT make it through the whole loop, so we can't assign this joystick to anything. + // When you try your best, but you don't succeed... + cv_usejoystick[player].value = 0; + } } } -/** \brief Joystick 1 buttons states +// Misleading function: updates device indices for all players BUT the one specified. +// Necessary for SDL_JOYDEVICEADDED events +void I_UpdateJoystickDeviceIndices(UINT8 excludePlayer) +{ + UINT8 player; + + for (player = 0; player < MAXSPLITSCREENPLAYERS; player++) + { + if (player == excludePlayer) + continue; + + I_UpdateJoystickDeviceIndex(player); + } +} + +/** \brief Joystick buttons states */ -static UINT64 lastjoybuttons = 0; +static UINT64 lastjoybuttons[MAXSPLITSCREENPLAYERS] = {0,0,0,0}; -/** \brief Joystick 1 hats state +/** \brief Joystick hats state */ -static UINT64 lastjoyhats = 0; - -/** \brief Shuts down joystick 1 - +static UINT64 lastjoyhats[MAXSPLITSCREENPLAYERS] = {0,0,0,0}; +/** \brief Shuts down joystick \return void - - */ -void I_ShutdownJoystick(void) +void I_ShutdownJoystick(UINT8 index) { INT32 i; event_t event; @@ -1130,7 +970,7 @@ void I_ShutdownJoystick(void) event.data2 = 0; event.data3 = 0; - lastjoybuttons = lastjoyhats = 0; + lastjoybuttons[index] = lastjoyhats[index] = 0; // emulate the up of all joystick buttons for (i=0;i= 0; i--) + for (i = JoyInfo[index].buttons - 1; i >= 0; i--) { joybuttons <<= 1; - if (SDL_JoystickGetButton(JoyInfo.dev,i)) + if (SDL_JoystickGetButton(JoyInfo[index].dev,i)) joybuttons |= 1; } - if (joybuttons != lastjoybuttons) + if (joybuttons != lastjoybuttons[index]) { INT64 j = 1; // keep only bits that changed since last time - INT64 newbuttons = joybuttons ^ lastjoybuttons; - lastjoybuttons = joybuttons; + INT64 newbuttons = joybuttons ^ lastjoybuttons[index]; + lastjoybuttons[index] = joybuttons; for (i = 0; i < JOYBUTTONS; i++, j <<= 1) { @@ -1206,9 +1046,9 @@ void I_GetJoystickEvents(void) } #endif - for (i = JoyInfo.hats - 1; i >= 0; i--) + for (i = JoyInfo[index].hats - 1; i >= 0; i--) { - Uint8 hat = SDL_JoystickGetHat(JoyInfo.dev, i); + Uint8 hat = SDL_JoystickGetHat(JoyInfo[index].dev, i); if (hat & SDL_HAT_UP ) joyhats|=(UINT64)0x1<<(0 + 4*i); if (hat & SDL_HAT_DOWN ) joyhats|=(UINT64)0x1<<(1 + 4*i); @@ -1216,11 +1056,11 @@ void I_GetJoystickEvents(void) if (hat & SDL_HAT_RIGHT) joyhats|=(UINT64)0x1<<(3 + 4*i); } - if (joyhats != lastjoyhats) + if (joyhats != lastjoyhats[index]) { INT64 j = 1; // keep only bits that changed since last time - INT64 newhats = joyhats ^ lastjoyhats; - lastjoyhats = joyhats; + INT64 newhats = joyhats ^ lastjoyhats[index]; + lastjoyhats[index] = joyhats; for (i = 0; i < JOYHATS*4; i++, j <<= 1) { @@ -1243,11 +1083,11 @@ void I_GetJoystickEvents(void) for (i = JOYAXISSET - 1; i >= 0; i--) { event.data1 = i; - if (i*2 + 1 <= JoyInfo.axises) - axisx = SDL_JoystickGetAxis(JoyInfo.dev, i*2 + 0); + if (i*2 + 1 <= JoyInfo[index].axises) + axisx = SDL_JoystickGetAxis(JoyInfo[index].dev, i*2 + 0); else axisx = 0; - if (i*2 + 2 <= JoyInfo.axises) - axisy = SDL_JoystickGetAxis(JoyInfo.dev, i*2 + 1); + if (i*2 + 2 <= JoyInfo[index].axises) + axisy = SDL_JoystickGetAxis(JoyInfo[index].dev, i*2 + 1); else axisy = 0; @@ -1256,7 +1096,7 @@ void I_GetJoystickEvents(void) axisy = axisy/32; - if (Joystick.bGamepadStyle) + if (Joystick[index].bGamepadStyle) { // gamepad control type, on or off, live or die if (axisx < -(JOYAXISRANGE/2)) @@ -1273,8 +1113,8 @@ void I_GetJoystickEvents(void) else { - axisx = JoyInfo.scale?((axisx/JoyInfo.scale)*JoyInfo.scale):axisx; - axisy = JoyInfo.scale?((axisy/JoyInfo.scale)*JoyInfo.scale):axisy; + axisx = JoyInfo[index].scale?((axisx/JoyInfo[index].scale)*JoyInfo[index].scale):axisx; + axisy = JoyInfo[index].scale?((axisy/JoyInfo[index].scale)*JoyInfo[index].scale):axisy; #ifdef SDL_JDEADZONE if (-SDL_JDEADZONE <= axisx && axisx <= SDL_JDEADZONE) axisx = 0; @@ -1298,7 +1138,7 @@ void I_GetJoystickEvents(void) */ -static int joy_open(int joyindex) +static int joy_open(int playerIndex, int joyIndex) { SDL_Joystick *newdev = NULL; int num_joy = 0; @@ -1309,7 +1149,7 @@ static int joy_open(int joyindex) return -1; } - if (joyindex <= 0) + if (joyIndex <= 0) return -1; num_joy = SDL_NumJoysticks(); @@ -1320,7 +1160,7 @@ static int joy_open(int joyindex) return -1; } - newdev = SDL_JoystickOpen(joyindex-1); + newdev = SDL_JoystickOpen(joyIndex-1); // Handle the edge case where the device <-> joystick index assignment can change due to hotplugging // This indexing is SDL's responsibility and there's not much we can do about it. @@ -1332,871 +1172,65 @@ static int joy_open(int joyindex) // 4. Unplug Controller B -> Index 0 inactive, Index 1 closed // 5. Plug Controller B -> Index 0 opened // 6. Plug Controller A -> Index 0 REPLACED, opened as Controller A; Index 1 is now Controller B - if (JoyInfo.dev) + if (JoyInfo[playerIndex].dev) { - if (JoyInfo.dev == newdev // same device, nothing to do - || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo.dev))) // we failed, but already have a working device - return JoyInfo.axises; + if (JoyInfo[playerIndex].dev == newdev // same device, nothing to do + || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo[playerIndex].dev))) // we failed, but already have a working device + { + return JoyInfo[playerIndex].axises; + } + // Else, we're changing devices, so send neutral joy events - CONS_Debug(DBG_GAMELOGIC, "Joystick1 device is changing; resetting events...\n"); - I_ShutdownJoystick(); + CONS_Debug(DBG_GAMELOGIC, "Joystick%d device is changing; resetting events...\n", playerIndex+1); + I_ShutdownJoystick(playerIndex); } - JoyInfo.dev = newdev; + JoyInfo[playerIndex].dev = newdev; - if (JoyInfo.dev == NULL) + if (JoyInfo[playerIndex].dev == NULL) { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick1: Couldn't open device - %s\n"), SDL_GetError()); + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick%d: Couldn't open device - %s\n"), playerIndex+1, SDL_GetError()); return -1; } else { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick1: %s\n"), SDL_JoystickName(JoyInfo.dev)); - JoyInfo.axises = SDL_JoystickNumAxes(JoyInfo.dev); - if (JoyInfo.axises > JOYAXISSET*2) - JoyInfo.axises = JOYAXISSET*2; - /* if (joyaxes<2) + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick%d: %s\n"), playerIndex+1, SDL_JoystickName(JoyInfo[playerIndex].dev)); + + JoyInfo[playerIndex].axises = SDL_JoystickNumAxes(JoyInfo[playerIndex].dev); + if (JoyInfo[playerIndex].axises > JOYAXISSET*2) + JoyInfo[playerIndex].axises = JOYAXISSET*2; + + /* + if (joyaxes<2) { I_OutputMsg("Not enought axes?\n"); return 0; - }*/ - - JoyInfo.buttons = SDL_JoystickNumButtons(JoyInfo.dev); - if (JoyInfo.buttons > JOYBUTTONS) - JoyInfo.buttons = JOYBUTTONS; - - JoyInfo.hats = SDL_JoystickNumHats(JoyInfo.dev); - if (JoyInfo.hats > JOYHATS) - JoyInfo.hats = JOYHATS; - - JoyInfo.balls = SDL_JoystickNumBalls(JoyInfo.dev); - - //Joystick.bGamepadStyle = !stricmp(SDL_JoystickName(JoyInfo.dev), "pad"); - - return JoyInfo.axises; - } -} - -//Joystick2 - -/** \brief Joystick 2 buttons states -*/ -static UINT64 lastjoy2buttons = 0; - -/** \brief Joystick 2 hats state -*/ -static UINT64 lastjoy2hats = 0; - -/** \brief Shuts down joystick 2 - - - \return void -*/ -void I_ShutdownJoystick2(void) -{ - INT32 i; - event_t event; - event.type = ev_keyup; - event.data2 = 0; - event.data3 = 0; - - lastjoy2buttons = lastjoy2hats = 0; - - // emulate the up of all joystick buttons - for (i = 0; i < JOYBUTTONS; i++) - { - event.data1 = KEY_2JOY1 + i; - D_PostEvent(&event); - } - - // emulate the up of all joystick hats - for (i = 0; i < JOYHATS*4; i++) - { - event.data1 = KEY_2HAT1 + i; - D_PostEvent(&event); - } - - // reset joystick position - event.type = ev_joystick2; - for (i = 0; i < JOYAXISSET; i++) - { - event.data1 = i; - D_PostEvent(&event); - } - - joystick2_started = 0; - JoyReset(&JoyInfo2); - - // don't shut down the subsystem here, because hotplugging -} - -void I_GetJoystick2Events(void) -{ - static event_t event = {0,0,0,0}; - INT32 i = 0; - UINT64 joyhats = 0; -#if 0 - INT64 joybuttons = 0; - INT32 axisx, axisy; -#endif - - if (!joystick2_started) - return; - - if (!JoyInfo2.dev) //I_ShutdownJoystick2(); - return; - - -#if 0 - //faB: look for as much buttons as g_input code supports, - // we don't use the others - for (i = JoyInfo2.buttons - 1; i >= 0; i--) - { - joybuttons <<= 1; - if (SDL_JoystickGetButton(JoyInfo2.dev,i)) - joybuttons |= 1; - } - - if (joybuttons != lastjoy2buttons) - { - INT64 j = 1; // keep only bits that changed since last time - INT64 newbuttons = joybuttons ^ lastjoy2buttons; - lastjoy2buttons = joybuttons; - - for (i = 0; i < JOYBUTTONS; i++, j <<= 1) - { - if (newbuttons & j) // button changed state? - { - if (joybuttons & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_2JOY1 + i; - D_PostEvent(&event); - } } - } -#endif + */ - for (i = JoyInfo2.hats - 1; i >= 0; i--) - { - Uint8 hat = SDL_JoystickGetHat(JoyInfo2.dev, i); + JoyInfo[playerIndex].buttons = SDL_JoystickNumButtons(JoyInfo[playerIndex].dev); + if (JoyInfo[playerIndex].buttons > JOYBUTTONS) + JoyInfo[playerIndex].buttons = JOYBUTTONS; - if (hat & SDL_HAT_UP ) joyhats|=(UINT64)0x1<<(0 + 4*i); - if (hat & SDL_HAT_DOWN ) joyhats|=(UINT64)0x1<<(1 + 4*i); - if (hat & SDL_HAT_LEFT ) joyhats|=(UINT64)0x1<<(2 + 4*i); - if (hat & SDL_HAT_RIGHT) joyhats|=(UINT64)0x1<<(3 + 4*i); - } + JoyInfo[playerIndex].hats = SDL_JoystickNumHats(JoyInfo[playerIndex].dev); + if (JoyInfo[playerIndex].hats > JOYHATS) + JoyInfo[playerIndex].hats = JOYHATS; - if (joyhats != lastjoy2hats) - { - INT64 j = 1; // keep only bits that changed since last time - INT64 newhats = joyhats ^ lastjoy2hats; - lastjoy2hats = joyhats; + JoyInfo[playerIndex].balls = SDL_JoystickNumBalls(JoyInfo[playerIndex].dev); - for (i = 0; i < JOYHATS*4; i++, j <<= 1) - { - if (newhats & j) // hat changed state? - { - if (joyhats & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_2HAT1 + i; - D_PostEvent(&event); - } - } - } + //JoyInfo[playerIndex].bGamepadStyle = !stricmp(SDL_JoystickName(JoyInfo[playerIndex].dev), "pad"); -#if 0 - // send joystick axis positions - event.type = ev_joystick2; - - for (i = JOYAXISSET - 1; i >= 0; i--) - { - event.data1 = i; - if (i*2 + 1 <= JoyInfo2.axises) - axisx = SDL_JoystickGetAxis(JoyInfo2.dev, i*2 + 0); - else axisx = 0; - if (i*2 + 2 <= JoyInfo2.axises) - axisy = SDL_JoystickGetAxis(JoyInfo2.dev, i*2 + 1); - else axisy = 0; - - // -32768 to 32767 - axisx = axisx/32; - axisy = axisy/32; - - if (Joystick2.bGamepadStyle) - { - // gamepad control type, on or off, live or die - if (axisx < -(JOYAXISRANGE/2)) - event.data2 = -1; - else if (axisx > (JOYAXISRANGE/2)) - event.data2 = 1; - else - event.data2 = 0; - if (axisy < -(JOYAXISRANGE/2)) - event.data3 = -1; - else if (axisy > (JOYAXISRANGE/2)) - event.data3 = 1; - else - event.data3 = 0; - } - else - { - - axisx = JoyInfo2.scale?((axisx/JoyInfo2.scale)*JoyInfo2.scale):axisx; - axisy = JoyInfo2.scale?((axisy/JoyInfo2.scale)*JoyInfo2.scale):axisy; - -#ifdef SDL_JDEADZONE - if (-SDL_JDEADZONE <= axisx && axisx <= SDL_JDEADZONE) axisx = 0; - if (-SDL_JDEADZONE <= axisy && axisy <= SDL_JDEADZONE) axisy = 0; -#endif - - // analog control style , just send the raw data - event.data2 = axisx; // x axis - event.data3 = axisy; // y axis - } - D_PostEvent(&event); - } -#endif -} - -/** \brief Open joystick handle - - \param fname name of joystick - - \return axises - - -*/ -static int joy_open2(int joyindex) -{ - SDL_Joystick *newdev = NULL; - int num_joy = 0; - - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - CONS_Printf(M_GetText("Joystick subsystem not started\n")); - return -1; - } - - if (joyindex <= 0) - return -1; - - num_joy = SDL_NumJoysticks(); - - if (num_joy == 0) - { - CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); - return -1; - } - - newdev = SDL_JoystickOpen(joyindex-1); - - // Handle the edge case where the device <-> joystick index assignment can change due to hotplugging - // This indexing is SDL's responsibility and there's not much we can do about it. - // - // Example: - // 1. Plug Controller A -> Index 0 opened - // 2. Plug Controller B -> Index 1 opened - // 3. Unplug Controller A -> Index 0 closed, Index 1 active - // 4. Unplug Controller B -> Index 0 inactive, Index 1 closed - // 5. Plug Controller B -> Index 0 opened - // 6. Plug Controller A -> Index 0 REPLACED, opened as Controller A; Index 1 is now Controller B - if (JoyInfo2.dev) - { - if (JoyInfo2.dev == newdev // same device, nothing to do - || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo2.dev))) // we failed, but already have a working device - return JoyInfo.axises; - // Else, we're changing devices, so send neutral joy events - CONS_Debug(DBG_GAMELOGIC, "Joystick2 device is changing; resetting events...\n"); - I_ShutdownJoystick2(); - } - - JoyInfo2.dev = newdev; - - if (JoyInfo2.dev == NULL) - { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick2: couldn't open device - %s\n"), SDL_GetError()); - return -1; - } - else - { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick2: %s\n"), SDL_JoystickName(JoyInfo2.dev)); - JoyInfo2.axises = SDL_JoystickNumAxes(JoyInfo2.dev); - if (JoyInfo2.axises > JOYAXISSET*2) - JoyInfo2.axises = JOYAXISSET*2; -/* if (joyaxes<2) - { - I_OutputMsg("Not enought axes?\n"); - return 0; - }*/ - - JoyInfo2.buttons = SDL_JoystickNumButtons(JoyInfo2.dev); - if (JoyInfo2.buttons > JOYBUTTONS) - JoyInfo2.buttons = JOYBUTTONS; - - JoyInfo2.hats = SDL_JoystickNumHats(JoyInfo2.dev); - if (JoyInfo2.hats > JOYHATS) - JoyInfo2.hats = JOYHATS; - - JoyInfo2.balls = SDL_JoystickNumBalls(JoyInfo2.dev); - - //Joystick.bGamepadStyle = !stricmp(SDL_JoystickName(JoyInfo2.dev), "pad"); - - return JoyInfo2.axises; - } -} - -//Joystick3 - -/** \brief Joystick 3 buttons states -*/ -static UINT64 lastjoy3buttons = 0; - -/** \brief Joystick 3 hats state -*/ -static UINT64 lastjoy3hats = 0; - -/** \brief Shuts down joystick 3 - - -\return void -*/ -void I_ShutdownJoystick3(void) -{ - INT32 i; - event_t event; - event.type = ev_keyup; - event.data2 = 0; - event.data3 = 0; - - lastjoy3buttons = lastjoy3hats = 0; - - // emulate the up of all joystick buttons - for (i = 0; i < JOYBUTTONS; i++) - { - event.data1 = KEY_3JOY1 + i; - D_PostEvent(&event); - } - - // emulate the up of all joystick hats - for (i = 0; i < JOYHATS*4; i++) - { - event.data1 = KEY_3HAT1 + i; - D_PostEvent(&event); - } - - // reset joystick position - event.type = ev_joystick3; - for (i = 0; i < JOYAXISSET; i++) - { - event.data1 = i; - D_PostEvent(&event); - } - - joystick3_started = 0; - JoyReset(&JoyInfo3); - - // don't shutdown the subsystem here, because hotplugging -} - -void I_GetJoystick3Events(void) -{ - static event_t event = {0,0,0,0}; - INT32 i = 0; - UINT64 joyhats = 0; -#if 0 - INT64 joybuttons = 0; -#endif - INT32 axisx, axisy; - - if (!joystick3_started) - return; - - if (!JoyInfo3.dev) //I_ShutdownJoystick3(); - return; - - -#if 0 - //faB: look for as much buttons as g_input code supports, - // we don't use the others - for (i = JoyInfo3.buttons - 1; i >= 0; i--) - { - joybuttons <<= 1; - if (SDL_JoystickGetButton(JoyInfo3.dev,i)) - joybuttons |= 1; - } - - if (joybuttons != lastjoy3buttons) - { - INT64 j = 1; // keep only bits that changed since last time - INT64 newbuttons = joybuttons ^ lastjoy3buttons; - lastjoy3buttons = joybuttons; - - for (i = 0; i < JOYBUTTONS; i++, j <<= 1) - { - if (newbuttons & j) // button changed state? - { - if (joybuttons & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_3JOY1 + i; - D_PostEvent(&event); - } - } - } -#endif - - for (i = JoyInfo3.hats - 1; i >= 0; i--) - { - Uint8 hat = SDL_JoystickGetHat(JoyInfo3.dev, i); - - if (hat & SDL_HAT_UP ) joyhats|=(UINT64)0x1<<(0 + 4*i); - if (hat & SDL_HAT_DOWN ) joyhats|=(UINT64)0x1<<(1 + 4*i); - if (hat & SDL_HAT_LEFT ) joyhats|=(UINT64)0x1<<(2 + 4*i); - if (hat & SDL_HAT_RIGHT) joyhats|=(UINT64)0x1<<(3 + 4*i); - } - - if (joyhats != lastjoy3hats) - { - INT64 j = 1; // keep only bits that changed since last time - INT64 newhats = joyhats ^ lastjoy3hats; - lastjoy3hats = joyhats; - - for (i = 0; i < JOYHATS*4; i++, j <<= 1) - { - if (newhats & j) // hat changed state? - { - if (joyhats & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_3HAT1 + i; - D_PostEvent(&event); - } - } - } - - // send joystick axis positions - event.type = ev_joystick3; - - for (i = JOYAXISSET - 1; i >= 0; i--) - { - event.data1 = i; - if (i*2 + 1 <= JoyInfo3.axises) - axisx = SDL_JoystickGetAxis(JoyInfo3.dev, i*2 + 0); - else axisx = 0; - if (i*2 + 2 <= JoyInfo3.axises) - axisy = SDL_JoystickGetAxis(JoyInfo3.dev, i*2 + 1); - else axisy = 0; - - // -32768 to 32767 - axisx = axisx/32; - axisy = axisy/32; - - if (Joystick3.bGamepadStyle) - { - // gamepad control type, on or off, live or die - if (axisx < -(JOYAXISRANGE/2)) - event.data2 = -1; - else if (axisx > (JOYAXISRANGE/2)) - event.data2 = 1; - else - event.data2 = 0; - if (axisy < -(JOYAXISRANGE/2)) - event.data3 = -1; - else if (axisy > (JOYAXISRANGE/2)) - event.data3 = 1; - else - event.data3 = 0; - } - else - { - - axisx = JoyInfo3.scale?((axisx/JoyInfo3.scale)*JoyInfo3.scale):axisx; - axisy = JoyInfo3.scale?((axisy/JoyInfo3.scale)*JoyInfo3.scale):axisy; - -#ifdef SDL_JDEADZONE - if (-SDL_JDEADZONE <= axisx && axisx <= SDL_JDEADZONE) axisx = 0; - if (-SDL_JDEADZONE <= axisy && axisy <= SDL_JDEADZONE) axisy = 0; -#endif - - // analog control style , just send the raw data - event.data2 = axisx; // x axis - event.data3 = axisy; // y axis - } - D_PostEvent(&event); - } - -} - -/** \brief Open joystick handle - - \param fname name of joystick - - \return axises - - -*/ -static int joy_open3(int joyindex) -{ - SDL_Joystick *newdev = NULL; - int num_joy = 0; - - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - CONS_Printf(M_GetText("Joystick subsystem not started\n")); - return -1; - } - - if (joyindex <= 0) - return -1; - - num_joy = SDL_NumJoysticks(); - - if (num_joy == 0) - { - CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); - return -1; - } - - newdev = SDL_JoystickOpen(joyindex - 1); - - // Handle the edge case where the device <-> joystick index assignment can change due to hotplugging - // This indexing is SDL's responsibility and there's not much we can do about it. - // - // Example: - // 1. Plug Controller A -> Index 0 opened - // 2. Plug Controller B -> Index 1 opened - // 3. Unplug Controller A -> Index 0 closed, Index 1 active - // 4. Unplug Controller B -> Index 0 inactive, Index 1 closed - // 5. Plug Controller B -> Index 0 opened - // 6. Plug Controller A -> Index 0 REPLACED, opened as Controller A; Index 1 is now Controller B - if (JoyInfo3.dev) - { - if (JoyInfo3.dev == newdev // same device, nothing to do - || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo3.dev))) // we failed, but already have a working device - return JoyInfo.axises; - // Else, we're changing devices, so send neutral joy events - CONS_Debug(DBG_GAMELOGIC, "Joystick3 device is changing; resetting events...\n"); - I_ShutdownJoystick3(); - } - - JoyInfo3.dev = newdev; - - if (JoyInfo3.dev == NULL) - { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick3: couldn't open device - %s\n"), SDL_GetError()); - return -1; - } - else - { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick3: %s\n"), SDL_JoystickName(JoyInfo3.dev)); - JoyInfo3.axises = SDL_JoystickNumAxes(JoyInfo3.dev); - if (JoyInfo3.axises > JOYAXISSET * 2) - JoyInfo3.axises = JOYAXISSET * 2; - /* if (joyaxes<2) - { - I_OutputMsg("Not enought axes?\n"); - return 0; - }*/ - - JoyInfo3.buttons = SDL_JoystickNumButtons(JoyInfo3.dev); - if (JoyInfo3.buttons > JOYBUTTONS) - JoyInfo3.buttons = JOYBUTTONS; - - JoyInfo3.hats = SDL_JoystickNumHats(JoyInfo3.dev); - if (JoyInfo3.hats > JOYHATS) - JoyInfo3.hats = JOYHATS; - - JoyInfo3.balls = SDL_JoystickNumBalls(JoyInfo3.dev); - - //Joystick.bGamepadStyle = !stricmp(SDL_JoystickName(JoyInfo3.dev), "pad"); - - return JoyInfo3.axises; - } -} - -//Joystick4 - -/** \brief Joystick 4 buttons states -*/ -static UINT64 lastjoy4buttons = 0; - -/** \brief Joystick 4 hats state -*/ -static UINT64 lastjoy4hats = 0; - -/** \brief Shuts down joystick 4 - - -\return void -*/ -void I_ShutdownJoystick4(void) -{ - INT32 i; - event_t event; - event.type = ev_keyup; - event.data2 = 0; - event.data3 = 0; - - lastjoy4buttons = lastjoy4hats = 0; - - // emulate the up of all joystick buttons - for (i = 0; i < JOYBUTTONS; i++) - { - event.data1 = KEY_4JOY1 + i; - D_PostEvent(&event); - } - - // emulate the up of all joystick hats - for (i = 0; i < JOYHATS*4; i++) - { - event.data1 = KEY_4HAT1 + i; - D_PostEvent(&event); - } - - // reset joystick position - event.type = ev_joystick4; - for (i = 0; i < JOYAXISSET; i++) - { - event.data1 = i; - D_PostEvent(&event); - } - - joystick4_started = 0; - JoyReset(&JoyInfo4); - - // don't shutdown the subsystem here, because hotplugging -} - -void I_GetJoystick4Events(void) -{ - static event_t event = {0,0,0,0}; - INT32 i = 0; - UINT64 joyhats = 0; -#if 0 - INT64 joybuttons = 0; -#endif - INT32 axisx, axisy; - - if (!joystick4_started) - return; - - if (!JoyInfo4.dev) //I_ShutdownJoystick4(); - return; - - -#if 0 - //faB: look for as much buttons as g_input code supports, - // we don't use the others - for (i = JoyInfo4.buttons - 1; i >= 0; i--) - { - joybuttons <<= 1; - if (SDL_JoystickGetButton(JoyInfo4.dev,i)) - joybuttons |= 1; - } - - if (joybuttons != lastjoy4buttons) - { - INT64 j = 1; // keep only bits that changed since last time - INT64 newbuttons = joybuttons ^ lastjoy4buttons; - lastjoy4buttons = joybuttons; - - for (i = 0; i < JOYBUTTONS; i++, j <<= 1) - { - if (newbuttons & j) // button changed state? - { - if (joybuttons & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_4JOY1 + i; - D_PostEvent(&event); - } - } - } -#endif - - for (i = JoyInfo4.hats - 1; i >= 0; i--) - { - Uint8 hat = SDL_JoystickGetHat(JoyInfo4.dev, i); - - if (hat & SDL_HAT_UP ) joyhats|=(UINT64)0x1<<(0 + 4*i); - if (hat & SDL_HAT_DOWN ) joyhats|=(UINT64)0x1<<(1 + 4*i); - if (hat & SDL_HAT_LEFT ) joyhats|=(UINT64)0x1<<(2 + 4*i); - if (hat & SDL_HAT_RIGHT) joyhats|=(UINT64)0x1<<(3 + 4*i); - } - - if (joyhats != lastjoy4hats) - { - INT64 j = 1; // keep only bits that changed since last time - INT64 newhats = joyhats ^ lastjoy4hats; - lastjoy4hats = joyhats; - - for (i = 0; i < JOYHATS*4; i++, j <<= 1) - { - if (newhats & j) // hat changed state? - { - if (joyhats & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_4HAT1 + i; - D_PostEvent(&event); - } - } - } - - // send joystick axis positions - event.type = ev_joystick4; - - for (i = JOYAXISSET - 1; i >= 0; i--) - { - event.data1 = i; - if (i*2 + 1 <= JoyInfo4.axises) - axisx = SDL_JoystickGetAxis(JoyInfo4.dev, i*2 + 0); - else axisx = 0; - if (i*2 + 2 <= JoyInfo4.axises) - axisy = SDL_JoystickGetAxis(JoyInfo4.dev, i*2 + 1); - else axisy = 0; - - // -32768 to 32767 - axisx = axisx/32; - axisy = axisy/32; - - if (Joystick4.bGamepadStyle) - { - // gamepad control type, on or off, live or die - if (axisx < -(JOYAXISRANGE/2)) - event.data2 = -1; - else if (axisx > (JOYAXISRANGE/2)) - event.data2 = 1; - else - event.data2 = 0; - if (axisy < -(JOYAXISRANGE/2)) - event.data3 = -1; - else if (axisy > (JOYAXISRANGE/2)) - event.data3 = 1; - else - event.data3 = 0; - } - else - { - - axisx = JoyInfo4.scale?((axisx/JoyInfo4.scale)*JoyInfo4.scale):axisx; - axisy = JoyInfo4.scale?((axisy/JoyInfo4.scale)*JoyInfo4.scale):axisy; - -#ifdef SDL_JDEADZONE - if (-SDL_JDEADZONE <= axisx && axisx <= SDL_JDEADZONE) axisx = 0; - if (-SDL_JDEADZONE <= axisy && axisy <= SDL_JDEADZONE) axisy = 0; -#endif - - // analog control style , just send the raw data - event.data2 = axisx; // x axis - event.data3 = axisy; // y axis - } - D_PostEvent(&event); - } - -} - -/** \brief Open joystick handle - - \param fname name of joystick - - \return axises - - -*/ -static int joy_open4(int joyindex) -{ - SDL_Joystick *newdev = NULL; - int num_joy = 0; - - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - CONS_Printf(M_GetText("Joystick subsystem not started\n")); - return -1; - } - - if (joyindex <= 0) - return -1; - - num_joy = SDL_NumJoysticks(); - - if (num_joy == 0) - { - CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); - return -1; - } - - newdev = SDL_JoystickOpen(joyindex - 1); - - // Handle the edge case where the device <-> joystick index assignment can change due to hotplugging - // This indexing is SDL's responsibility and there's not much we can do about it. - // - // Example: - // 1. Plug Controller A -> Index 0 opened - // 2. Plug Controller B -> Index 1 opened - // 3. Unplug Controller A -> Index 0 closed, Index 1 active - // 4. Unplug Controller B -> Index 0 inactive, Index 1 closed - // 5. Plug Controller B -> Index 0 opened - // 6. Plug Controller A -> Index 0 REPLACED, opened as Controller A; Index 1 is now Controller B - if (JoyInfo4.dev) - { - if (JoyInfo4.dev == newdev // same device, nothing to do - || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo4.dev))) // we failed, but already have a working device - return JoyInfo.axises; - // Else, we're changing devices, so send neutral joy events - CONS_Debug(DBG_GAMELOGIC, "Joystick4 device is changing; resetting events...\n"); - I_ShutdownJoystick4(); - } - - JoyInfo4.dev = newdev; - - if (JoyInfo4.dev == NULL) - { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick4: couldn't open device - %s\n"), SDL_GetError()); - return -1; - } - else - { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick4: %s\n"), SDL_JoystickName(JoyInfo4.dev)); - JoyInfo4.axises = SDL_JoystickNumAxes(JoyInfo4.dev); - if (JoyInfo4.axises > JOYAXISSET * 2) - JoyInfo4.axises = JOYAXISSET * 2; - /* if (joyaxes<2) - { - I_OutputMsg("Not enought axes?\n"); - return 0; - }*/ - - JoyInfo4.buttons = SDL_JoystickNumButtons(JoyInfo4.dev); - if (JoyInfo4.buttons > JOYBUTTONS) - JoyInfo4.buttons = JOYBUTTONS; - - JoyInfo4.hats = SDL_JoystickNumHats(JoyInfo4.dev); - if (JoyInfo4.hats > JOYHATS) - JoyInfo4.hats = JOYHATS; - - JoyInfo4.balls = SDL_JoystickNumBalls(JoyInfo4.dev); - - //Joystick.bGamepadStyle = !stricmp(SDL_JoystickName(JoyInfo4.dev), "pad"); - - return JoyInfo4.axises; + return JoyInfo[playerIndex].axises; } } // // I_InitJoystick // -void I_InitJoystick(void) +void I_InitJoystick(UINT8 index) { SDL_Joystick *newjoy = NULL; + UINT8 i; //I_ShutdownJoystick(); //SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); @@ -2220,169 +1254,80 @@ void I_InitJoystick(void) } } - if (cv_usejoystick.value) - newjoy = SDL_JoystickOpen(cv_usejoystick.value-1); + if (cv_usejoystick[index].value) + newjoy = SDL_JoystickOpen(cv_usejoystick[index].value-1); - if (newjoy && (JoyInfo2.dev == newjoy || JoyInfo3.dev == newjoy || JoyInfo4.dev == newjoy)) // don't override an active device - cv_usejoystick.value = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; - else if (newjoy && joy_open(cv_usejoystick.value) != -1) + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (i == index) + continue; + + if (JoyInfo[i].dev == newjoy) + break; + } + + if (newjoy && i < MAXSPLITSCREENPLAYERS) // don't override an active device + { + cv_usejoystick[index].value = I_GetJoystickDeviceIndex(JoyInfo[index].dev) + 1; + } + else if (newjoy && joy_open(index, cv_usejoystick[index].value) != -1) { // SDL's device indexes are unstable, so cv_usejoystick may not match // the actual device index. So let's cheat a bit and find the device's current index. - JoyInfo.oldjoy = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; - joystick_started = 1; + JoyInfo[index].oldjoy = I_GetJoystickDeviceIndex(JoyInfo[index].dev) + 1; + joystick_started[index] = 1; } else { - if (JoyInfo.oldjoy) - I_ShutdownJoystick(); - cv_usejoystick.value = 0; - joystick_started = 0; + if (JoyInfo[index].oldjoy) + I_ShutdownJoystick(index); + cv_usejoystick[index].value = 0; + joystick_started[index] = 0; } - if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy && JoyInfo3.dev != newjoy && JoyInfo4.dev != newjoy) + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (JoyInfo[i].dev == newjoy) + break; + } + + if (i == MAXSPLITSCREENPLAYERS) + { + // Joystick didn't end up being used SDL_JoystickClose(newjoy); + } +} + +void I_InitJoystick1(void) +{ + I_InitJoystick(0); } void I_InitJoystick2(void) { - SDL_Joystick *newjoy = NULL; - - //I_ShutdownJoystick2(); - //SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); - if (M_CheckParm("-nojoy")) - return; - - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - CONS_Printf("I_InitJoystick2()...\n"); - if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) - { - CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); - return; - } - } - - if (cv_usejoystick2.value) - newjoy = SDL_JoystickOpen(cv_usejoystick2.value-1); - - if (newjoy && (JoyInfo.dev == newjoy || JoyInfo3.dev == newjoy || JoyInfo4.dev == newjoy)) // don't override an active device - cv_usejoystick2.value = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; - else if (newjoy && joy_open2(cv_usejoystick2.value) != -1) - { - // SDL's device indexes are unstable, so cv_usejoystick may not match - // the actual device index. So let's cheat a bit and find the device's current index. - JoyInfo2.oldjoy = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; - joystick2_started = 1; - } - else - { - if (JoyInfo2.oldjoy) - I_ShutdownJoystick2(); - cv_usejoystick2.value = 0; - joystick2_started = 0; - } - - if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy && JoyInfo3.dev != newjoy && JoyInfo4.dev != newjoy) - SDL_JoystickClose(newjoy); + I_InitJoystick(1); } void I_InitJoystick3(void) { - SDL_Joystick *newjoy = NULL; - - //I_ShutdownJoystick3(); - //SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); - if (M_CheckParm("-nojoy")) - return; - - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - CONS_Printf("I_InitJoystick3()...\n"); - if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) - { - CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); - return; - } - } - - if (cv_usejoystick3.value) - newjoy = SDL_JoystickOpen(cv_usejoystick3.value - 1); - - if (newjoy && (JoyInfo.dev == newjoy || JoyInfo2.dev == newjoy || JoyInfo4.dev == newjoy)) // don't override an active device - cv_usejoystick3.value = I_GetJoystickDeviceIndex(JoyInfo3.dev) + 1; - else if (newjoy && joy_open3(cv_usejoystick3.value) != -1) - { - // SDL's device indexes are unstable, so cv_usejoystick may not match - // the actual device index. So let's cheat a bit and find the device's current index. - JoyInfo3.oldjoy = I_GetJoystickDeviceIndex(JoyInfo3.dev) + 1; - joystick3_started = 1; - } - else - { - if (JoyInfo3.oldjoy) - I_ShutdownJoystick3(); - cv_usejoystick3.value = 0; - joystick3_started = 0; - } - - if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy && JoyInfo3.dev != newjoy && JoyInfo4.dev != newjoy) - SDL_JoystickClose(newjoy); + I_InitJoystick(2); } void I_InitJoystick4(void) { - SDL_Joystick *newjoy = NULL; - - //I_ShutdownJoystick4(); - //SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); - if (M_CheckParm("-nojoy")) - return; - - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - CONS_Printf("I_InitJoystick4()...\n"); - if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) - { - CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); - return; - } - } - - if (cv_usejoystick4.value) - newjoy = SDL_JoystickOpen(cv_usejoystick4.value - 1); - - if (newjoy && (JoyInfo.dev == newjoy || JoyInfo2.dev == newjoy || JoyInfo4.dev == newjoy)) // don't override an active device - cv_usejoystick4.value = I_GetJoystickDeviceIndex(JoyInfo4.dev) + 1; - else if (newjoy && joy_open4(cv_usejoystick4.value) != -1) - { - // SDL's device indexes are unstable, so cv_usejoystick may not match - // the actual device index. So let's cheat a bit and find the device's current index. - JoyInfo4.oldjoy = I_GetJoystickDeviceIndex(JoyInfo4.dev) + 1; - joystick4_started = 1; - } - else - { - if (JoyInfo4.oldjoy) - I_ShutdownJoystick4(); - cv_usejoystick4.value = 0; - joystick4_started = 0; - } - - if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy && JoyInfo3.dev != newjoy && JoyInfo4.dev != newjoy) - SDL_JoystickClose(newjoy); + I_InitJoystick(3); } - static void I_ShutdownInput(void) { + UINT8 i; + // Yes, the name is misleading: these send neutral events to // clean up the unplugged joystick's input // Note these methods are internal to this file, not called elsewhere. - I_ShutdownJoystick(); - I_ShutdownJoystick2(); - I_ShutdownJoystick3(); - I_ShutdownJoystick4(); + + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + I_ShutdownJoystick(i); if (SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) { @@ -2544,336 +1489,7 @@ void I_UpdateMumble(const mobj_t *mobj, const listener_t listener) #undef WINMUMBLE #endif // NOMUMBLE -#ifdef HAVE_TERMIOS - -void I_GetMouseEvents(void) -{ - static UINT8 mdata[5]; - static INT32 i = 0,om2b = 0; - INT32 di, j, mlp, button; - event_t event; - const INT32 mswap[8] = {0, 4, 1, 5, 2, 6, 3, 7}; - - if (!mouse2_started) return; - for (mlp = 0; mlp < 20; mlp++) - { - for (; i < 5; i++) - { - di = read(fdmouse2, mdata+i, 1); - if (di == -1) return; - } - if ((mdata[0] & 0xf8) != 0x80) - { - for (j = 1; j < 5; j++) - if ((mdata[j] & 0xf8) == 0x80) - for (i = 0; i < 5-j; i++) // shift - mdata[i] = mdata[i+j]; - if (i < 5) continue; - } - else - { - button = mswap[~mdata[0] & 0x07]; - for (j = 0; j < MOUSEBUTTONS; j++) - { - if (om2b & (1<> 4); - } - else if (bytenum == 3) - { - dx = (char)((combytes[0] & 3) << 6); - dy = (char)((combytes[0] & 12) << 4); - dx = (char)(dx + combytes[1]); - dy = (char)(dy + combytes[2]); - handlermouse2x+= dx; - handlermouse2y+= dy; - } - else if (bytenum == 4) // fourth UINT8 (logitech mouses) - { - if (buffer[i] & 32) - handlermouse2buttons |= 4; - else - handlermouse2buttons &= ~4; - } - } -} - -void I_GetMouseEvents(void) -{ - static UINT8 lastbuttons2 = 0; //mouse movement - event_t event; - - if (mouse2filehandle == INVALID_HANDLE_VALUE) - return; - - I_PoolMouse2(); - // post key event for buttons - if (handlermouse2buttons != lastbuttons2) - { - INT32 i, j = 1, k; - k = (handlermouse2buttons ^ lastbuttons2); // only changed bit to 1 - lastbuttons2 = (UINT8)handlermouse2buttons; - - for (i = 0; i < MOUSEBUTTONS; i++, j <<= 1) - if (k & j) - { - if (handlermouse2buttons & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_2MOUSE1+i; - D_PostEvent(&event); - } - } - - if (handlermouse2x != 0 || handlermouse2y != 0) - { - event.type = ev_mouse2; - event.data1 = 0; -// event.data1 = buttons; // not needed - event.data2 = handlermouse2x << 1; - event.data3 = -handlermouse2y << 1; - handlermouse2x = 0; - handlermouse2y = 0; - - D_PostEvent(&event); - } -} -#else -void I_GetMouseEvents(void){}; -#endif - -// -// I_StartupMouse2 -// -void I_StartupMouse2(void) -{ -#ifdef HAVE_TERMIOS - struct termios m2tio; - size_t i; - INT32 dtr = -1, rts = -1;; - I_ShutdownMouse2(); - if (cv_usemouse2.value == 0) return; - if ((fdmouse2 = open(cv_mouse2port.string, O_RDONLY|O_NONBLOCK|O_NOCTTY)) == -1) - { - CONS_Printf(M_GetText("Error opening %s!\n"), cv_mouse2port.string); - return; - } - tcflush(fdmouse2, TCIOFLUSH); - m2tio.c_iflag = IGNBRK; - m2tio.c_oflag = 0; - m2tio.c_cflag = CREAD|CLOCAL|HUPCL|CS8|CSTOPB|B1200; - m2tio.c_lflag = 0; - m2tio.c_cc[VTIME] = 0; - m2tio.c_cc[VMIN] = 1; - tcsetattr(fdmouse2, TCSANOW, &m2tio); - for (i = 0; i < strlen(cv_mouse2opt.string); i++) - { - if (toupper(cv_mouse2opt.string[i]) == 'D') - { - if (cv_mouse2opt.string[i+1] == '-') - dtr = 0; - else - dtr = 1; - } - if (toupper(cv_mouse2opt.string[i]) == 'R') - { - if (cv_mouse2opt.string[i+1] == '-') - rts = 0; - else - rts = 1; - } - if (dtr != -1 || rts != -1) - { - INT32 c; - if (!ioctl(fdmouse2, TIOCMGET, &c)) - { - if (!dtr) - c &= ~TIOCM_DTR; - else if (dtr > 0) - c |= TIOCM_DTR; - } - if (!rts) - c &= ~TIOCM_RTS; - else if (rts > 0) - c |= TIOCM_RTS; - ioctl(fdmouse2, TIOCMSET, &c); - } - } - mouse2_started = 1; - I_AddExitFunc(I_ShutdownMouse2); -#elif defined (_WIN32) - DCB dcb; - - if (mouse2filehandle != INVALID_HANDLE_VALUE) - I_ShutdownMouse2(); - - if (cv_usemouse2.value == 0) - return; - - if (mouse2filehandle == INVALID_HANDLE_VALUE) - { - // COM file handle - mouse2filehandle = CreateFileA(cv_mouse2port.string, GENERIC_READ | GENERIC_WRITE, - 0, // exclusive access - NULL, // no security attrs - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); - if (mouse2filehandle == INVALID_HANDLE_VALUE) - { - INT32 e = GetLastError(); - if (e == 5) - CONS_Alert(CONS_ERROR, M_GetText("Can't open %s: Access denied\n"), cv_mouse2port.string); - else - CONS_Alert(CONS_ERROR, M_GetText("Can't open %s: error %d\n"), cv_mouse2port.string, e); - return; - } - } - - // getevent when somthing happens - //SetCommMask(mouse2filehandle, EV_RXCHAR); - - // buffers - SetupComm(mouse2filehandle, MOUSECOMBUFFERSIZE, MOUSECOMBUFFERSIZE); - - // purge buffers - PurgeComm(mouse2filehandle, PURGE_TXABORT | PURGE_RXABORT - | PURGE_TXCLEAR | PURGE_RXCLEAR); - - // setup port to 1200 7N1 - dcb.DCBlength = sizeof (DCB); - - GetCommState(mouse2filehandle, &dcb); - - dcb.BaudRate = CBR_1200; - dcb.ByteSize = 7; - dcb.Parity = NOPARITY; - dcb.StopBits = ONESTOPBIT; - - dcb.fDtrControl = DTR_CONTROL_ENABLE; - dcb.fRtsControl = RTS_CONTROL_ENABLE; - - dcb.fBinary = TRUE; - dcb.fParity = TRUE; - - SetCommState(mouse2filehandle, &dcb); - I_AddExitFunc(I_ShutdownMouse2); -#endif -} +#endif // HAVE_SDL // // I_Tactile @@ -2906,40 +1522,38 @@ void I_Tactile4(FFType pFFType, const JoyFF_t *FFEffect) (void)FFEffect; } +static ticcmd_t emptycmd[MAXSPLITSCREENPLAYERS]; + /** \brief empty ticcmd for player 1 */ -static ticcmd_t emptycmd; ticcmd_t *I_BaseTiccmd(void) { - return &emptycmd; + return &emptycmd[0]; } /** \brief empty ticcmd for player 2 */ -static ticcmd_t emptycmd2; ticcmd_t *I_BaseTiccmd2(void) { - return &emptycmd2; + return &emptycmd[1]; } /** \brief empty ticcmd for player 3 */ -static ticcmd_t emptycmd3; ticcmd_t *I_BaseTiccmd3(void) { - return &emptycmd3; + return &emptycmd[2]; } /** \brief empty ticcmd for player 4 */ -static ticcmd_t emptycmd4; ticcmd_t *I_BaseTiccmd4(void) { - return &emptycmd4; + return &emptycmd[3]; } #if defined (_WIN32) @@ -3208,7 +1822,7 @@ void I_Quit(void) if (Playing()) K_PlayerForfeit(consoleplayer, true); - G_SaveGameData(false); // Tails 12-08-2002 + G_SaveGameData(); // Tails 12-08-2002 //added:16-02-98: when recording a demo, should exit using 'q' key, // but sometimes we forget and use 'F10'.. so save here too. @@ -3292,7 +1906,7 @@ void I_Error(const char *error, ...) if (errorcount == 8) { M_SaveConfig(NULL); - G_SaveGameData(false); + G_SaveGameData(); } if (errorcount > 20) { @@ -3324,7 +1938,7 @@ void I_Error(const char *error, ...) #ifndef NONET D_SaveBan(); // save the ban list #endif - G_SaveGameData(false); // Tails 12-08-2002 + G_SaveGameData(); // Tails 12-08-2002 /* Prevent segmentation fault if testers go to Record Attack... */ #ifndef TESTERS diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 2de0c43bb..bece95f88 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -528,7 +528,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) INT32 raxis = axis/32; if (which == ev_joystick) { - if (Joystick.bGamepadStyle) + if (Joystick[0].bGamepadStyle) { // gamepad control type, on or off, live or die if (raxis < -(JOYAXISRANGE/2)) @@ -540,7 +540,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) } else { - raxis = JoyInfo.scale!=1?((raxis/JoyInfo.scale)*JoyInfo.scale):raxis; + raxis = JoyInfo[0].scale!=1?((raxis/JoyInfo[0].scale)*JoyInfo[0].scale):raxis; #ifdef SDL_JDEADZONE if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE) @@ -550,7 +550,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) } else if (which == ev_joystick2) { - if (Joystick2.bGamepadStyle) + if (Joystick[1].bGamepadStyle) { // gamepad control type, on or off, live or die if (raxis < -(JOYAXISRANGE/2)) @@ -561,7 +561,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) } else { - raxis = JoyInfo2.scale!=1?((raxis/JoyInfo2.scale)*JoyInfo2.scale):raxis; + raxis = JoyInfo[1].scale!=1?((raxis/JoyInfo[1].scale)*JoyInfo[1].scale):raxis; #ifdef SDL_JDEADZONE if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE) @@ -571,7 +571,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) } else if (which == ev_joystick3) { - if (Joystick3.bGamepadStyle) + if (Joystick[2].bGamepadStyle) { // gamepad control type, on or off, live or die if (raxis < -(JOYAXISRANGE/2)) @@ -582,7 +582,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) } else { - raxis = JoyInfo3.scale!=1?((raxis/JoyInfo3.scale)*JoyInfo3.scale):raxis; + raxis = JoyInfo[2].scale!=1?((raxis/JoyInfo[2].scale)*JoyInfo[2].scale):raxis; #ifdef SDL_JDEADZONE if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE) @@ -592,7 +592,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) } else if (which == ev_joystick4) { - if (Joystick4.bGamepadStyle) + if (Joystick[3].bGamepadStyle) { // gamepad control type, on or off, live or die if (raxis < -(JOYAXISRANGE/2)) @@ -603,7 +603,7 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) } else { - raxis = JoyInfo4.scale!=1?((raxis/JoyInfo4.scale)*JoyInfo4.scale):raxis; + raxis = JoyInfo[3].scale!=1?((raxis/JoyInfo[3].scale)*JoyInfo[3].scale):raxis; #ifdef SDL_JDEADZONE if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE) @@ -823,13 +823,12 @@ static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) static void Impl_HandleJoystickAxisEvent(SDL_JoyAxisEvent evt) { event_t event; - SDL_JoystickID joyid[4]; + SDL_JoystickID joyid[MAXSPLITSCREENPLAYERS]; + UINT8 i; // Determine the Joystick IDs for each current open joystick - joyid[0] = SDL_JoystickInstanceID(JoyInfo.dev); - joyid[1] = SDL_JoystickInstanceID(JoyInfo2.dev); - joyid[2] = SDL_JoystickInstanceID(JoyInfo3.dev); - joyid[3] = SDL_JoystickInstanceID(JoyInfo4.dev); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + joyid[i] = SDL_JoystickInstanceID(JoyInfo[i].dev); evt.axis++; event.data1 = event.data2 = event.data3 = INT32_MAX; @@ -873,11 +872,12 @@ static void Impl_HandleJoystickAxisEvent(SDL_JoyAxisEvent evt) static void Impl_HandleJoystickHatEvent(SDL_JoyHatEvent evt) { event_t event; - SDL_JoystickID joyid[2]; + SDL_JoystickID joyid[MAXSPLITSCREENPLAYERS]; + UINT8 i; // Determine the Joystick IDs for each current open joystick - joyid[0] = SDL_JoystickInstanceID(JoyInfo.dev); - joyid[1] = SDL_JoystickInstanceID(JoyInfo2.dev); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + joyid[i] = SDL_JoystickInstanceID(JoyInfo[i].dev); if (evt.hat >= JOYHATS) return; // ignore hats with too high an index @@ -890,6 +890,14 @@ static void Impl_HandleJoystickHatEvent(SDL_JoyHatEvent evt) { event.data1 = KEY_2HAT1 + (evt.hat*4); } + else if (evt.which == joyid[2]) + { + event.data1 = KEY_3HAT1 + (evt.hat*4); + } + else if (evt.which == joyid[3]) + { + event.data1 = KEY_4HAT1 + (evt.hat*4); + } else return; // NOTE: UNFINISHED @@ -899,13 +907,12 @@ static void Impl_HandleJoystickHatEvent(SDL_JoyHatEvent evt) static void Impl_HandleJoystickButtonEvent(SDL_JoyButtonEvent evt, Uint32 type) { event_t event; - SDL_JoystickID joyid[4]; + SDL_JoystickID joyid[MAXSPLITSCREENPLAYERS]; + UINT8 i; // Determine the Joystick IDs for each current open joystick - joyid[0] = SDL_JoystickInstanceID(JoyInfo.dev); - joyid[1] = SDL_JoystickInstanceID(JoyInfo2.dev); - joyid[2] = SDL_JoystickInstanceID(JoyInfo3.dev); - joyid[3] = SDL_JoystickInstanceID(JoyInfo4.dev); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + joyid[i] = SDL_JoystickInstanceID(JoyInfo[i].dev); if (evt.which == joyid[0]) { @@ -948,10 +955,13 @@ static void Impl_HandleJoystickButtonEvent(SDL_JoyButtonEvent evt, Uint32 type) void I_GetEvent(void) { SDL_Event evt; + // We only want the first motion event, // otherwise we'll end up catching the warp back to center. //int mouseMotionOnce = 0; + UINT8 i; + if (!graphics_started) { return; @@ -1016,77 +1026,42 @@ void I_GetEvent(void) // * BUT: If that default index is being occupied, use ANOTHER cv_usejoystick's default value! //////////////////////////////////////////////////////////// - ////////////////////////////// - // PLAYER 1 - ////////////////////////////// - - if (newjoy && (!JoyInfo.dev || !SDL_JoystickGetAttached(JoyInfo.dev)) - && JoyInfo2.dev != newjoy && JoyInfo3.dev != newjoy && JoyInfo4.dev != newjoy) // don't override a currently active device + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - cv_usejoystick.value = evt.jdevice.which + 1; - I_UpdateJoystickDeviceIndices(1); - } + if (newjoy && (!JoyInfo[i].dev || !SDL_JoystickGetAttached(JoyInfo[i].dev))) + { + UINT8 j; - ////////////////////////////// - // PLAYER 2 - ////////////////////////////// + for (j = 0; j < MAXSPLITSCREENPLAYERS; j++) + { + if (i == j) + continue; - else if (newjoy && (!JoyInfo2.dev || !SDL_JoystickGetAttached(JoyInfo2.dev)) - && JoyInfo.dev != newjoy && JoyInfo3.dev != newjoy && JoyInfo4.dev != newjoy) // don't override a currently active device - { - cv_usejoystick2.value = evt.jdevice.which + 1; - I_UpdateJoystickDeviceIndices(2); - } + if (JoyInfo[j].dev == newjoy) + break; + } - ////////////////////////////// - // PLAYER 3 - ////////////////////////////// - - else if (newjoy && (!JoyInfo3.dev || !SDL_JoystickGetAttached(JoyInfo3.dev)) - && JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy && JoyInfo4.dev != newjoy) // don't override a currently active device - { - cv_usejoystick3.value = evt.jdevice.which + 1; - I_UpdateJoystickDeviceIndices(3); - } - - ////////////////////////////// - // PLAYER 4 - ////////////////////////////// - - else if (newjoy && (!JoyInfo4.dev || !SDL_JoystickGetAttached(JoyInfo4.dev)) - && JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy && JoyInfo3.dev != newjoy) // don't override a currently active device - { - cv_usejoystick4.value = evt.jdevice.which + 1; - I_UpdateJoystickDeviceIndices(4); + if (j == MAXSPLITSCREENPLAYERS) + { + // ensures we aren't overriding a currently active device + cv_usejoystick[i].value = evt.jdevice.which + 1; + I_UpdateJoystickDeviceIndices(0); + } + } } //////////////////////////////////////////////////////////// // Was cv_usejoystick disabled in settings? //////////////////////////////////////////////////////////// - if (!strcmp(cv_usejoystick.string, "0") || !cv_usejoystick.value) - cv_usejoystick.value = 0; - else if (atoi(cv_usejoystick.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick, cv_usejoystick.value); - - if (!strcmp(cv_usejoystick2.string, "0") || !cv_usejoystick2.value) - cv_usejoystick2.value = 0; - else if (atoi(cv_usejoystick2.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick2.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); - - if (!strcmp(cv_usejoystick3.string, "0") || !cv_usejoystick3.value) - cv_usejoystick3.value = 0; - else if (atoi(cv_usejoystick3.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick3.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick3, cv_usejoystick3.value); - - if (!strcmp(cv_usejoystick4.string, "0") || !cv_usejoystick4.value) - cv_usejoystick4.value = 0; - else if (atoi(cv_usejoystick4.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick4.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick4, cv_usejoystick4.value); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (!strcmp(cv_usejoystick[i].string, "0") || !cv_usejoystick[i].value) + cv_usejoystick[i].value = 0; + else if (atoi(cv_usejoystick[i].string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys + && cv_usejoystick[i].value) // update the cvar ONLY if a device exists + CV_SetValue(&cv_usejoystick[i], cv_usejoystick[i].value); + } //////////////////////////////////////////////////////////// // Update all joysticks' init states @@ -1095,23 +1070,25 @@ void I_GetEvent(void) // if the device is already active, this should do nothing, effectively. //////////////////////////////////////////////////////////// - I_InitJoystick(); - I_InitJoystick2(); - I_InitJoystick3(); - I_InitJoystick4(); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + I_InitJoystick(i); //////////////////////////////////////////////////////////// - CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); - CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); - CONS_Debug(DBG_GAMELOGIC, "Joystick3 device index: %d\n", JoyInfo3.oldjoy); - CONS_Debug(DBG_GAMELOGIC, "Joystick4 device index: %d\n", JoyInfo4.oldjoy); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + CONS_Debug(DBG_GAMELOGIC, "Joystick%d device index: %d\n", i+1, JoyInfo[i].oldjoy); // update the menu if (currentMenu == &OP_JoystickSetDef) M_SetupJoystickMenu(0); - if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy && JoyInfo3.dev != newjoy && JoyInfo4.dev != newjoy) + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (JoyInfo[i].dev == newjoy) + break; + } + + if (i == MAXSPLITSCREENPLAYERS) SDL_JoystickClose(newjoy); } break; @@ -1119,28 +1096,13 @@ void I_GetEvent(void) //////////////////////////////////////////////////////////// case SDL_JOYDEVICEREMOVED: - if (JoyInfo.dev && !SDL_JoystickGetAttached(JoyInfo.dev)) + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - CONS_Debug(DBG_GAMELOGIC, "Joystick1 removed, device index: %d\n", JoyInfo.oldjoy); - I_ShutdownJoystick(); - } - - if (JoyInfo2.dev && !SDL_JoystickGetAttached(JoyInfo2.dev)) - { - CONS_Debug(DBG_GAMELOGIC, "Joystick2 removed, device index: %d\n", JoyInfo2.oldjoy); - I_ShutdownJoystick2(); - } - - if (JoyInfo3.dev && !SDL_JoystickGetAttached(JoyInfo3.dev)) - { - CONS_Debug(DBG_GAMELOGIC, "Joystick3 removed, device index: %d\n", JoyInfo3.oldjoy); - I_ShutdownJoystick3(); - } - - if (JoyInfo4.dev && !SDL_JoystickGetAttached(JoyInfo4.dev)) - { - CONS_Debug(DBG_GAMELOGIC, "Joystick4 removed, device index: %d\n", JoyInfo4.oldjoy); - I_ShutdownJoystick4(); + if (JoyInfo[i].dev && !SDL_JoystickGetAttached(JoyInfo[i].dev)) + { + CONS_Debug(DBG_GAMELOGIC, "Joystick%d removed, device index: %d\n", i+1, JoyInfo[i].oldjoy); + I_ShutdownJoystick(i); + } } //////////////////////////////////////////////////////////// @@ -1149,124 +1111,32 @@ void I_GetEvent(void) // * BUT: If that default index is being occupied, use ANOTHER cv_usejoystick's default value! //////////////////////////////////////////////////////////// - if (JoyInfo.dev) - cv_usejoystick.value = JoyInfo.oldjoy = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; - else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo4.oldjoy) - cv_usejoystick.value = atoi(cv_usejoystick.string); - else if (atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo4.oldjoy) - cv_usejoystick.value = atoi(cv_usejoystick2.string); - else if (atoi(cv_usejoystick3.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo4.oldjoy) - cv_usejoystick.value = atoi(cv_usejoystick3.string); - else if (atoi(cv_usejoystick4.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo4.oldjoy) - cv_usejoystick.value = atoi(cv_usejoystick4.string); - else // we tried... - cv_usejoystick.value = 0; - - if (JoyInfo2.dev) - cv_usejoystick2.value = JoyInfo2.oldjoy = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo4.oldjoy) - cv_usejoystick2.value = atoi(cv_usejoystick.string); - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo4.oldjoy) - cv_usejoystick2.value = atoi(cv_usejoystick2.string); - else if (atoi(cv_usejoystick3.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo4.oldjoy) - cv_usejoystick2.value = atoi(cv_usejoystick3.string); - else if (atoi(cv_usejoystick4.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo3.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo4.oldjoy) - cv_usejoystick2.value = atoi(cv_usejoystick4.string); - else // we tried... - cv_usejoystick2.value = 0; - - if (JoyInfo3.dev) - cv_usejoystick3.value = JoyInfo3.oldjoy = I_GetJoystickDeviceIndex(JoyInfo3.dev) + 1; - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo4.oldjoy) - cv_usejoystick3.value = atoi(cv_usejoystick.string); - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo4.oldjoy) - cv_usejoystick3.value = atoi(cv_usejoystick2.string); - else if (atoi(cv_usejoystick3.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo4.oldjoy) - cv_usejoystick3.value = atoi(cv_usejoystick3.string); - else if (atoi(cv_usejoystick4.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo4.oldjoy) - cv_usejoystick3.value = atoi(cv_usejoystick4.string); - else // we tried... - cv_usejoystick3.value = 0; - - if (JoyInfo4.dev) - cv_usejoystick4.value = JoyInfo4.oldjoy = I_GetJoystickDeviceIndex(JoyInfo4.dev) + 1; - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick.string) != JoyInfo3.oldjoy) - cv_usejoystick4.value = atoi(cv_usejoystick.string); - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick2.string) != JoyInfo3.oldjoy) - cv_usejoystick4.value = atoi(cv_usejoystick2.string); - else if (atoi(cv_usejoystick3.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick3.string) != JoyInfo3.oldjoy) - cv_usejoystick4.value = atoi(cv_usejoystick3.string); - else if (atoi(cv_usejoystick4.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick4.string) != JoyInfo3.oldjoy) - cv_usejoystick4.value = atoi(cv_usejoystick4.string); - else // we tried... - cv_usejoystick4.value = 0; + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + I_UpdateJoystickDeviceIndex(i); + } //////////////////////////////////////////////////////////// // Was cv_usejoystick disabled in settings? //////////////////////////////////////////////////////////// - if (!strcmp(cv_usejoystick.string, "0")) - cv_usejoystick.value = 0; - else if (atoi(cv_usejoystick.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick, cv_usejoystick.value); - - if (!strcmp(cv_usejoystick2.string, "0")) - cv_usejoystick2.value = 0; - else if (atoi(cv_usejoystick2.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick2.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); - - if (!strcmp(cv_usejoystick3.string, "0")) - cv_usejoystick3.value = 0; - else if (atoi(cv_usejoystick3.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick3.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick3, cv_usejoystick3.value); - - if (!strcmp(cv_usejoystick4.string, "0")) - cv_usejoystick4.value = 0; - else if (atoi(cv_usejoystick4.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys - && cv_usejoystick4.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick4, cv_usejoystick4.value); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (!strcmp(cv_usejoystick[i].string, "0")) + { + cv_usejoystick[i].value = 0; + } + else if (atoi(cv_usejoystick[i].string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys + && cv_usejoystick[i].value) // update the cvar ONLY if a device exists + { + CV_SetValue(&cv_usejoystick[i], cv_usejoystick[i].value); + } + } //////////////////////////////////////////////////////////// - CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); - CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); - CONS_Debug(DBG_GAMELOGIC, "Joystick3 device index: %d\n", JoyInfo3.oldjoy); - CONS_Debug(DBG_GAMELOGIC, "Joystick4 device index: %d\n", JoyInfo4.oldjoy); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + CONS_Debug(DBG_GAMELOGIC, "Joystick%d device index: %d\n", i+1, JoyInfo[i].oldjoy); // update the menu if (currentMenu == &OP_JoystickSetDef) @@ -1324,16 +1194,15 @@ void I_StartupMouse(void) void I_OsPolling(void) { SDL_Keymod mod; + UINT8 i; if (consolevent) I_GetConsoleEvents(); if (SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) { SDL_JoystickUpdate(); - I_GetJoystickEvents(); - I_GetJoystick2Events(); - I_GetJoystick3Events(); - I_GetJoystick4Events(); + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + I_GetJoystickEvents(i); } I_GetMouseEvents(); diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index aa9574d41..6294c5c33 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -156,26 +156,11 @@ boolean OglSdlSurface(INT32 w, INT32 h) { INT32 cbpp = cv_scr_depth.value < 16 ? 16 : cv_scr_depth.value; static boolean first_init = false; - const char *gllogdir = NULL; oglflags = 0; if (!first_init) { - if (!gllogstream) - { - gllogdir = D_Home(); - -#ifdef DEBUG_TO_FILE -#ifdef DEFAULTDIR - if (gllogdir) - gllogstream = fopen(va("%s/"DEFAULTDIR"/ogllog.txt",gllogdir), "wt"); - else -#endif - gllogstream = fopen("./ogllog.txt", "wt"); -#endif - } - gl_version = pglGetString(GL_VERSION); gl_renderer = pglGetString(GL_RENDERER); gl_extensions = pglGetString(GL_EXTENSIONS); diff --git a/src/sdl/sdlmain.h b/src/sdl/sdlmain.h index 0c545b51a..39c099a2b 100644 --- a/src/sdl/sdlmain.h +++ b/src/sdl/sdlmain.h @@ -23,6 +23,7 @@ extern SDL_bool consolevent; extern SDL_bool framebuffer; #include "../m_fixed.h" +#include "../doomdef.h" // SDL2 stub macro #ifdef _MSC_VER @@ -57,40 +58,26 @@ typedef struct SDLJoyInfo_s } SDLJoyInfo_t; -/** \brief SDL info about joystick 1 +/** \brief SDL info about joysticks */ -extern SDLJoyInfo_t JoyInfo; +extern SDLJoyInfo_t JoyInfo[MAXSPLITSCREENPLAYERS]; /** \brief joystick axis deadzone */ #define SDL_JDEADZONE 153 #undef SDL_JDEADZONE -/** \brief SDL inof about joystick 2 -*/ -extern SDLJoyInfo_t JoyInfo2; - -/** \brief SDL inof about joystick 3 -*/ -extern SDLJoyInfo_t JoyInfo3; - -/** \brief SDL inof about joystick 4 -*/ -extern SDLJoyInfo_t JoyInfo4; - void I_GetConsoleEvents(void); // So we can call this from i_video event loop -void I_ShutdownJoystick(void); -void I_ShutdownJoystick2(void); -void I_ShutdownJoystick3(void); -void I_ShutdownJoystick4(void); +void I_ShutdownJoystick(UINT8 index); // Cheat to get the device index for a joystick handle INT32 I_GetJoystickDeviceIndex(SDL_Joystick *dev); // Quick thing to make SDL_JOYDEVICEADDED events less of an abomination -void I_UpdateJoystickDeviceIndices(INT32 player); +void I_UpdateJoystickDeviceIndex(UINT8 player); +void I_UpdateJoystickDeviceIndices(UINT8 excludePlayer); void I_GetConsoleEvents(void);