Browse Source

Added an example of setting PS5 controller audio routing

Closes https://github.com/libsdl-org/SDL/pull/9661
Sam Lantinga 10 months ago
parent
commit
8aa7910184
1 changed files with 49 additions and 4 deletions
  1. 49 4
      test/testcontroller.c

+ 49 - 4
test/testcontroller.c

@@ -60,6 +60,7 @@ typedef struct
     char *mapping;
     SDL_bool has_bindings;
 
+    int audio_route;
     int trigger_effect;
 } Controller;
 
@@ -191,6 +192,44 @@ typedef struct
     Uint8 ucLedBlue;                  /* 46 */
 } DS5EffectsState_t;
 
+static void CyclePS5AudioRoute(Controller *device)
+{
+    DS5EffectsState_t state;
+
+    device->audio_route = (device->audio_route + 1) % 4;
+
+    SDL_zero(state);
+    switch (device->audio_route) {
+    case 0:
+        /* Audio disabled */
+        state.ucEnableBits1 |= (0x80 | 0x20 | 0x10); /* Modify audio route and speaker / headphone volume */
+        state.ucSpeakerVolume = 0;                   /* Maximum volume */
+        state.ucHeadphoneVolume = 0;                 /* Maximum volume */
+        state.ucAudioEnableBits = 0x00;              /* Output to headphones */
+        break;
+    case 1:
+        /* Headphones */
+        state.ucEnableBits1 |= (0x80 | 0x10); /* Modify audio route and headphone volume */
+        state.ucHeadphoneVolume = 100;        /* Maximum volume */
+        state.ucAudioEnableBits = 0x00;       /* Output to headphones */
+        break;
+    case 2:
+        /* Speaker */
+        state.ucEnableBits1 |= (0x80 | 0x20); /* Modify audio route and speaker volume */
+        state.ucSpeakerVolume = 100;          /* Maximum volume */
+        state.ucAudioEnableBits = 0x30;       /* Output to headphones */
+        break;
+    case 3:
+        /* Both */
+        state.ucEnableBits1 |= (0x80 | 0x20 | 0x10); /* Modify audio route and speaker / headphone volume */
+        state.ucSpeakerVolume = 100;                 /* Maximum volume */
+        state.ucHeadphoneVolume = 100;               /* Maximum volume */
+        state.ucAudioEnableBits = 0x20;              /* Output to both speaker and headphones */
+        break;
+    }
+    SDL_SendGamepadEffect(device->gamepad, &state, sizeof(state));
+}
+
 static void CyclePS5TriggerEffect(Controller *device)
 {
     DS5EffectsState_t state;
@@ -1715,11 +1754,17 @@ static void loop(void *arg)
 #endif /* VERBOSE_BUTTONS */
 
             if (display_mode == CONTROLLER_MODE_TESTING) {
-                /* Cycle PS5 trigger effects when the microphone button is pressed */
                 if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN &&
-                    controller && SDL_GetGamepadType(controller->gamepad) == SDL_GAMEPAD_TYPE_PS5 &&
-                    event.gbutton.button == SDL_GAMEPAD_BUTTON_MISC1) {
-                    CyclePS5TriggerEffect(controller);
+                    controller && SDL_GetGamepadType(controller->gamepad) == SDL_GAMEPAD_TYPE_PS5) {
+                    /* Cycle PS5 audio routing when the microphone button is pressed */
+                    if (event.gbutton.button == SDL_GAMEPAD_BUTTON_MISC1) {
+                        CyclePS5AudioRoute(controller);
+                    }
+
+                    /* Cycle PS5 trigger effects when the triangle button is pressed */
+                    if (event.gbutton.button == SDL_GAMEPAD_BUTTON_NORTH) {
+                        CyclePS5TriggerEffect(controller);
+                    }
                 }
             }
             break;