summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testhotplug.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testhotplug.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testhotplug.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testhotplug.c182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testhotplug.c b/src/contrib/SDL-3.2.20/test/testhotplug.c
new file mode 100644
index 0000000..f050ade
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testhotplug.c
@@ -0,0 +1,182 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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_KEYBOARD_ADDED:
100 SDL_Log("Keyboard '%s' added : %" SDL_PRIu32, SDL_GetKeyboardNameForID(event.kdevice.which), event.kdevice.which);
101 break;
102 case SDL_EVENT_KEYBOARD_REMOVED:
103 SDL_Log("Keyboard removed: %" SDL_PRIu32, event.kdevice.which);
104 break;
105 case SDL_EVENT_MOUSE_ADDED:
106 SDL_Log("Mouse '%s' added : %" SDL_PRIu32, SDL_GetMouseNameForID(event.mdevice.which), event.mdevice.which);
107 break;
108 case SDL_EVENT_MOUSE_REMOVED:
109 SDL_Log("Mouse removed: %" SDL_PRIu32, event.mdevice.which);
110 break;
111 case SDL_EVENT_JOYSTICK_ADDED:
112 if (joystick) {
113 SDL_Log("Only one joystick supported by this test");
114 } else {
115 joystick = SDL_OpenJoystick(event.jdevice.which);
116 instance = event.jdevice.which;
117 SDL_Log("Joy Added : %" SDL_PRIu32 " : %s", event.jdevice.which, SDL_GetJoystickName(joystick));
118 if (enable_haptic) {
119 if (SDL_IsJoystickHaptic(joystick)) {
120 haptic = SDL_OpenHapticFromJoystick(joystick);
121 if (haptic) {
122 SDL_Log("Joy Haptic Opened");
123 if (!SDL_InitHapticRumble(haptic)) {
124 SDL_Log("Could not init Rumble!: %s", SDL_GetError());
125 SDL_CloseHaptic(haptic);
126 haptic = NULL;
127 }
128 } else {
129 SDL_Log("Joy haptic open FAILED!: %s", SDL_GetError());
130 }
131 } else {
132 SDL_Log("No haptic found");
133 }
134 }
135 }
136 break;
137 case SDL_EVENT_JOYSTICK_REMOVED:
138 if (instance == event.jdevice.which) {
139 SDL_Log("Joy Removed: %" SDL_PRIs32, event.jdevice.which);
140 instance = 0;
141 if (enable_haptic && haptic) {
142 SDL_CloseHaptic(haptic);
143 haptic = NULL;
144 }
145 SDL_CloseJoystick(joystick);
146 joystick = NULL;
147 } else {
148 SDL_Log("Unknown joystick disconnected");
149 }
150 break;
151 case SDL_EVENT_JOYSTICK_AXIS_MOTION:
152 /*
153 // SDL_Log("Axis Move: %d", event.jaxis.axis);
154 */
155 if (enable_haptic) {
156 SDL_PlayHapticRumble(haptic, 0.25, 250);
157 }
158 break;
159 case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
160 SDL_Log("Button Press: %d", event.jbutton.button);
161 if (enable_haptic && haptic) {
162 SDL_PlayHapticRumble(haptic, 0.25, 250);
163 }
164 if (event.jbutton.button == 0) {
165 SDL_Log("Exiting due to button press of button 0");
166 keepGoing = false;
167 }
168 break;
169 case SDL_EVENT_JOYSTICK_BUTTON_UP:
170 SDL_Log("Button Release: %d", event.jbutton.button);
171 break;
172 default:
173 break;
174 }
175 }
176 }
177
178 SDL_Quit();
179 SDLTest_CommonDestroyState(state);
180
181 return 0;
182}