Atlas - testhotplug.c

Home / ext / SDL / test Lines: 1 | Size: 6562 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely. 11*/ 12 13/* Simple program to test the SDL joystick hotplugging */ 14 15#include <stdlib.h> 16 17#include <SDL3/SDL.h> 18#include <SDL3/SDL_main.h> 19#include <SDL3/SDL_test.h> 20 21int main(int argc, char *argv[]) 22{ 23 int num_keyboards = 0; 24 int num_mice = 0; 25 int num_joysticks = 0; 26 SDL_Joystick *joystick = NULL; 27 SDL_Haptic *haptic = NULL; 28 SDL_JoystickID instance = 0; 29 bool keepGoing = true; 30 int i; 31 bool enable_haptic = true; 32 Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; 33 SDLTest_CommonState *state; 34 35 /* Initialize test framework */ 36 state = SDLTest_CommonCreateState(argv, 0); 37 if (!state) { 38 return 1; 39 } 40 41 /* Parse commandline */ 42 for (i = 1; i < argc;) { 43 int consumed; 44 45 consumed = SDLTest_CommonArg(state, i); 46 if (!consumed) { 47 if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) { 48 enable_haptic = false; 49 consumed = 1; 50 } 51 } 52 if (consumed <= 0) { 53 static const char *options[] = { "[--nohaptic]", NULL }; 54 SDLTest_CommonLogUsage(state, argv[0], options); 55 exit(1); 56 } 57 58 i += consumed; 59 } 60 61 if (enable_haptic) { 62 init_subsystems |= SDL_INIT_HAPTIC; 63 } 64 65 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); 66 67 /* Initialize SDL (Note: video is required to start event loop) */ 68 if (!SDL_Init(init_subsystems)) { 69 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); 70 exit(1); 71 } 72 73 /* 74 //SDL_CreateWindow("Dummy", 128, 128, 0); 75 */ 76 77 SDL_free(SDL_GetKeyboards(&num_keyboards)); 78 SDL_Log("There are %d keyboards at startup", num_keyboards); 79 80 SDL_free(SDL_GetMice(&num_mice)); 81 SDL_Log("There are %d mice at startup", num_mice); 82 83 SDL_free(SDL_GetJoysticks(&num_joysticks)); 84 SDL_Log("There are %d joysticks at startup", num_joysticks); 85 86 if (enable_haptic) { 87 int num_haptics; 88 SDL_free(SDL_GetHaptics(&num_haptics)); 89 SDL_Log("There are %d haptic devices at startup", num_haptics); 90 } 91 92 while (keepGoing) { 93 SDL_Event event; 94 while (SDL_PollEvent(&event)) { 95 switch (event.type) { 96 case SDL_EVENT_QUIT: 97 keepGoing = false; 98 break; 99 case SDL_EVENT_KEY_DOWN: 100 if (event.key.scancode == SDL_SCANCODE_ESCAPE) { 101 keepGoing = false; 102 } 103 break; 104 case SDL_EVENT_KEYBOARD_ADDED: 105 SDL_Log("Keyboard '%s' added : %" SDL_PRIu32, SDL_GetKeyboardNameForID(event.kdevice.which), event.kdevice.which); 106 break; 107 case SDL_EVENT_KEYBOARD_REMOVED: 108 SDL_Log("Keyboard removed: %" SDL_PRIu32, event.kdevice.which); 109 break; 110 case SDL_EVENT_MOUSE_ADDED: 111 SDL_Log("Mouse '%s' added : %" SDL_PRIu32, SDL_GetMouseNameForID(event.mdevice.which), event.mdevice.which); 112 break; 113 case SDL_EVENT_MOUSE_REMOVED: 114 SDL_Log("Mouse removed: %" SDL_PRIu32, event.mdevice.which); 115 break; 116 case SDL_EVENT_JOYSTICK_ADDED: 117 if (joystick) { 118 SDL_Log("Only one joystick supported by this test"); 119 } else { 120 joystick = SDL_OpenJoystick(event.jdevice.which); 121 instance = event.jdevice.which; 122 SDL_Log("Joy Added : %" SDL_PRIu32 " : %s", event.jdevice.which, SDL_GetJoystickName(joystick)); 123 if (enable_haptic) { 124 if (SDL_IsJoystickHaptic(joystick)) { 125 haptic = SDL_OpenHapticFromJoystick(joystick); 126 if (haptic) { 127 SDL_Log("Joy Haptic Opened"); 128 if (!SDL_InitHapticRumble(haptic)) { 129 SDL_Log("Could not init Rumble!: %s", SDL_GetError()); 130 SDL_CloseHaptic(haptic); 131 haptic = NULL; 132 } 133 } else { 134 SDL_Log("Joy haptic open FAILED!: %s", SDL_GetError()); 135 } 136 } else { 137 SDL_Log("No haptic found"); 138 } 139 } 140 } 141 break; 142 case SDL_EVENT_JOYSTICK_REMOVED: 143 if (instance == event.jdevice.which) { 144 SDL_Log("Joy Removed: %" SDL_PRIs32, event.jdevice.which); 145 instance = 0; 146 if (enable_haptic && haptic) { 147 SDL_CloseHaptic(haptic); 148 haptic = NULL; 149 } 150 SDL_CloseJoystick(joystick); 151 joystick = NULL; 152 } else { 153 SDL_Log("Unknown joystick disconnected"); 154 } 155 break; 156 case SDL_EVENT_JOYSTICK_AXIS_MOTION: 157 /* 158 // SDL_Log("Axis Move: %d", event.jaxis.axis); 159 */ 160 if (enable_haptic) { 161 SDL_PlayHapticRumble(haptic, 0.25, 250); 162 } 163 break; 164 case SDL_EVENT_JOYSTICK_BUTTON_DOWN: 165 SDL_Log("Button Press: %d", event.jbutton.button); 166 if (enable_haptic && haptic) { 167 SDL_PlayHapticRumble(haptic, 0.25, 250); 168 } 169 if (event.jbutton.button == 0) { 170 SDL_Log("Exiting due to button press of button 0"); 171 keepGoing = false; 172 } 173 break; 174 case SDL_EVENT_JOYSTICK_BUTTON_UP: 175 SDL_Log("Button Release: %d", event.jbutton.button); 176 break; 177 default: 178 break; 179 } 180 } 181 } 182 183 SDL_Quit(); 184 SDLTest_CommonDestroyState(state); 185 186 return 0; 187} 188
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.