src/joystick/hidapi/SDL_hidapi_switch.c
changeset 12641 64597a7e8771
parent 12503 806492103856
child 12787 0411f841b035
equal deleted inserted replaced
12640:b708d2c3fe08 12641:64597a7e8771
   181     Uint8 rgucProprietaryData[ k_unSwitchOutputPacketDataLength - 1 - 1 ];
   181     Uint8 rgucProprietaryData[ k_unSwitchOutputPacketDataLength - 1 - 1 ];
   182 } SwitchProprietaryOutputPacket_t;
   182 } SwitchProprietaryOutputPacket_t;
   183 #pragma pack()
   183 #pragma pack()
   184 
   184 
   185 typedef struct {
   185 typedef struct {
       
   186     SDL_JoystickID joystickID;
   186     hid_device *dev;
   187     hid_device *dev;
   187     SDL_bool m_bIsUsingBluetooth;
   188     SDL_bool m_bIsUsingBluetooth;
   188     Uint8 m_nCommandNumber;
   189     Uint8 m_nCommandNumber;
   189     SwitchCommonOutputPacket_t m_RumblePacket;
   190     SwitchCommonOutputPacket_t m_RumblePacket;
   190     Uint32 m_nRumbleExpiration;
   191     Uint32 m_nRumbleExpiration;
   568 {
   569 {
   569     return ApplyStickCalibrationCentered(ctx, nStick, nAxis, sRawValue, ctx->m_StickCalData[nStick].axis[nAxis].sCenter);
   570     return ApplyStickCalibrationCentered(ctx, nStick, nAxis, sRawValue, ctx->m_StickCalData[nStick].axis[nAxis].sCenter);
   570 }
   571 }
   571 
   572 
   572 static SDL_bool
   573 static SDL_bool
   573 HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
   574 HIDAPI_DriverSwitch_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks)
   574 {
   575 {
   575     SDL_DriverSwitch_Context *ctx;
   576     SDL_DriverSwitch_Context *ctx;
   576     Uint8 input_mode;
   577     Uint8 input_mode;
   577 
   578 
   578     ctx = (SDL_DriverSwitch_Context *)SDL_calloc(1, sizeof(*ctx));
   579     ctx = (SDL_DriverSwitch_Context *)SDL_calloc(1, sizeof(*ctx));
   579     if (!ctx) {
   580     if (!ctx) {
   580         SDL_OutOfMemory();
   581         SDL_OutOfMemory();
   581         return SDL_FALSE;
   582         return SDL_FALSE;
   582     }
   583     }
   583     ctx->dev = dev;
   584     ctx->dev = context->device;
   584 
   585 
   585     *context = ctx;
   586     context->context = ctx;
   586 
   587 
   587     /* Initialize rumble data */
   588     /* Initialize rumble data */
   588     SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]);
   589     SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]);
   589     SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[1]);
   590     SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[1]);
   590 
   591 
   625             SDL_free(ctx);
   626             SDL_free(ctx);
   626             return SDL_FALSE;
   627             return SDL_FALSE;
   627         }
   628         }
   628     }
   629     }
   629 
   630 
       
   631     ctx->joystickID = SDL_GetNextJoystickInstanceID();
       
   632     *num_joysticks += 1;
       
   633     SDL_PrivateJoystickAdded(ctx->joystickID);
       
   634 
       
   635     return SDL_TRUE;
       
   636 }
       
   637 
       
   638 static SDL_bool
       
   639 HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick)
       
   640 {
       
   641     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context;
       
   642 
   630     /* Set the LED state */
   643     /* Set the LED state */
   631     SetHomeLED(ctx, 100);
   644     SetHomeLED(ctx, 100);
   632     SetSlotLED(ctx, (joystick->instance_id % 4));
   645     SetSlotLED(ctx, (joystick->instance_id % 4));
   633 
   646 
   634     /* Initialize the joystick capabilities */
   647     /* Initialize the joystick capabilities */
   638 
   651 
   639     return SDL_TRUE;
   652     return SDL_TRUE;
   640 }
   653 }
   641 
   654 
   642 static int
   655 static int
   643 HIDAPI_DriverSwitch_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
   656 HIDAPI_DriverSwitch_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
   644 {
   657 {
   645     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context;
   658     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context;
   646 
   659 
   647     /* Experimentally determined rumble values. These will only matter on some controllers as tested ones
   660     /* Experimentally determined rumble values. These will only matter on some controllers as tested ones
   648      * seem to disregard these and just use any non-zero rumble values as a binary flag for constant rumble
   661      * seem to disregard these and just use any non-zero rumble values as a binary flag for constant rumble
   649      *
   662      *
   650      * More information about these values can be found here:
   663      * More information about these values can be found here:
   845 
   858 
   846     ctx->m_lastFullState = *packet;
   859     ctx->m_lastFullState = *packet;
   847 }
   860 }
   848 
   861 
   849 static SDL_bool
   862 static SDL_bool
   850 HIDAPI_DriverSwitch_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
   863 HIDAPI_DriverSwitch_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks)
   851 {
   864 {
   852     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context;
   865     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context;
       
   866     SDL_Joystick *joystick = SDL_JoystickFromInstanceID(ctx->joystickID);
   853     int size;
   867     int size;
       
   868 
       
   869     if (joystick == NULL) {
       
   870         return SDL_TRUE; /* Nothing to do right now! */
       
   871     }
   854 
   872 
   855     while ((size = ReadInput(ctx)) > 0) {
   873     while ((size = ReadInput(ctx)) > 0) {
   856         switch (ctx->m_rgucReadBuffer[0]) {
   874         switch (ctx->m_rgucReadBuffer[0]) {
   857         case k_eSwitchInputReportIDs_SimpleControllerState:
   875         case k_eSwitchInputReportIDs_SimpleControllerState:
   858             HandleSimpleControllerState(joystick, ctx, (SwitchSimpleStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
   876             HandleSimpleControllerState(joystick, ctx, (SwitchSimpleStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
   866     }
   884     }
   867 
   885 
   868     if (ctx->m_nRumbleExpiration) {
   886     if (ctx->m_nRumbleExpiration) {
   869         Uint32 now = SDL_GetTicks();
   887         Uint32 now = SDL_GetTicks();
   870         if (SDL_TICKS_PASSED(now, ctx->m_nRumbleExpiration)) {
   888         if (SDL_TICKS_PASSED(now, ctx->m_nRumbleExpiration)) {
   871             HIDAPI_DriverSwitch_Rumble(joystick, dev, context, 0, 0, 0);
   889             HIDAPI_DriverSwitch_Rumble(context, joystick, 0, 0, 0);
   872         }
   890         }
   873     }
   891     }
   874 
   892 
   875     return (size >= 0);
   893     return (size >= 0);
   876 }
   894 }
   877 
   895 
   878 static void
   896 static void
   879 HIDAPI_DriverSwitch_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
   897 HIDAPI_DriverSwitch_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks)
   880 {
   898 {
   881     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context;
   899     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context;
   882 
   900 
   883     /* Restore simple input mode for other applications */
   901     /* Restore simple input mode for other applications */
   884     SetInputMode(ctx, k_eSwitchInputReportIDs_SimpleControllerState);
   902     SetInputMode(ctx, k_eSwitchInputReportIDs_SimpleControllerState);
   885 
   903 
   886     SDL_free(context);
   904     *num_joysticks -= 1;
       
   905     if (send_event) {
       
   906         SDL_PrivateJoystickRemoved(ctx->joystickID);
       
   907     }
       
   908     SDL_free(context->context);
       
   909 }
       
   910 
       
   911 static int
       
   912 HIDAPI_DriverSwitch_NumJoysticks(SDL_HIDAPI_DriverData *context)
       
   913 {
       
   914     return 1;
       
   915 }
       
   916 
       
   917 static SDL_JoystickID
       
   918 HIDAPI_DriverSwitch_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
       
   919 {
       
   920     SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context;
       
   921     return ctx->joystickID;
   887 }
   922 }
   888 
   923 
   889 SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch =
   924 SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch =
   890 {
   925 {
   891     SDL_HINT_JOYSTICK_HIDAPI_SWITCH,
   926     SDL_HINT_JOYSTICK_HIDAPI_SWITCH,
   892     SDL_TRUE,
   927     SDL_TRUE,
   893     HIDAPI_DriverSwitch_IsSupportedDevice,
   928     HIDAPI_DriverSwitch_IsSupportedDevice,
   894     HIDAPI_DriverSwitch_GetDeviceName,
   929     HIDAPI_DriverSwitch_GetDeviceName,
   895     HIDAPI_DriverSwitch_Init,
   930     HIDAPI_DriverSwitch_InitDriver,
   896     HIDAPI_DriverSwitch_Rumble,
   931     HIDAPI_DriverSwitch_QuitDriver,
   897     HIDAPI_DriverSwitch_Update,
   932     HIDAPI_DriverSwitch_UpdateDriver,
   898     HIDAPI_DriverSwitch_Quit
   933     HIDAPI_DriverSwitch_NumJoysticks,
       
   934     HIDAPI_DriverSwitch_InstanceIDForIndex,
       
   935     HIDAPI_DriverSwitch_OpenJoystick,
       
   936     HIDAPI_DriverSwitch_Rumble
   899 };
   937 };
   900 
   938 
   901 #endif /* SDL_JOYSTICK_HIDAPI_SWITCH */
   939 #endif /* SDL_JOYSTICK_HIDAPI_SWITCH */
   902 
   940 
   903 #endif /* SDL_JOYSTICK_HIDAPI */
   941 #endif /* SDL_JOYSTICK_HIDAPI */