Debian Codegraph @

test/testhotplug.c

raw
1 /*
2 Copyright (C) 1997-2026 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 <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "SDL.h"
20
21 #if !defined SDL_JOYSTICK_DISABLED && !defined SDL_HAPTIC_DISABLED
22
23 int main(int argc, char *argv[])
24 {
25 SDL_Joystick *joystick = NULL;
26 SDL_Haptic *haptic = NULL;
27 SDL_JoystickID instance = -1;
28 SDL_bool keepGoing = SDL_TRUE;
29 int i;
30 SDL_bool enable_haptic = SDL_TRUE;
31 Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK;
32
33 for (i = 1; i < argc; ++i) {
34 if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) {
35 enable_haptic = SDL_FALSE;
36 }
37 }
38
39 if (enable_haptic) {
40 init_subsystems |= SDL_INIT_HAPTIC;
41 }
42
43 /* Enable standard application logging */
44 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
45
46 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
47
48 /* Initialize SDL (Note: video is required to start event loop) */
49 if (SDL_Init(init_subsystems) < 0) {
50 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
51 exit(1);
52 }
53
54 /*
55 //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
56 */
57
58 SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
59 if (enable_haptic) {
60 SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
61 }
62
63 while (keepGoing) {
64 SDL_Event event;
65 while (SDL_PollEvent(&event)) {
66 switch (event.type) {
67 case SDL_QUIT:
68 keepGoing = SDL_FALSE;
69 break;
70 case SDL_JOYDEVICEADDED:
71 if (joystick) {
72 SDL_Log("Only one joystick supported by this test\n");
73 } else {
74 joystick = SDL_JoystickOpen(event.jdevice.which);
75 instance = SDL_JoystickInstanceID(joystick);
76 SDL_Log("Joy Added : %" SDL_PRIs32 " : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
77 if (enable_haptic) {
78 if (SDL_JoystickIsHaptic(joystick)) {
79 haptic = SDL_HapticOpenFromJoystick(joystick);
80 if (haptic) {
81 SDL_Log("Joy Haptic Opened\n");
82 if (SDL_HapticRumbleInit(haptic) != 0) {
83 SDL_Log("Could not init Rumble!: %s\n", SDL_GetError());
84 SDL_HapticClose(haptic);
85 haptic = NULL;
86 }
87 } else {
88 SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError());
89 }
90 } else {
91 SDL_Log("No haptic found\n");
92 }
93 }
94 }
95 break;
96 case SDL_JOYDEVICEREMOVED:
97 if (instance == event.jdevice.which) {
98 SDL_Log("Joy Removed: %" SDL_PRIs32 "\n", event.jdevice.which);
99 instance = -1;
100 if (enable_haptic && haptic) {
101 SDL_HapticClose(haptic);
102 haptic = NULL;
103 }
104 SDL_JoystickClose(joystick);
105 joystick = NULL;
106 } else {
107 SDL_Log("Unknown joystick diconnected\n");
108 }
109 break;
110 case SDL_JOYAXISMOTION:
111 /*
112 // SDL_Log("Axis Move: %d\n", event.jaxis.axis);
113 */
114 if (enable_haptic) {
115 SDL_HapticRumblePlay(haptic, 0.25, 250);
116 }
117 break;
118 case SDL_JOYBUTTONDOWN:
119 SDL_Log("Button Press: %d\n", event.jbutton.button);
120 if (enable_haptic && haptic) {
121 SDL_HapticRumblePlay(haptic, 0.25, 250);
122 }
123 if (event.jbutton.button == 0) {
124 SDL_Log("Exiting due to button press of button 0\n");
125 keepGoing = SDL_FALSE;
126 }
127 break;
128 case SDL_JOYBUTTONUP:
129 SDL_Log("Button Release: %d\n", event.jbutton.button);
130 break;
131 }
132 }
133 }
134
135 SDL_Quit();
136
137 return 0;
138 }
139 #else
140
141 int
142 main(int argc, char *argv[])
143 {
144 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick and haptic support.\n");
145 return 1;
146 }
147
148 #endif