2 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple program to test the SDL joystick hotplugging */
21 #if !defined SDL_JOYSTICK_DISABLED && !defined SDL_HAPTIC_DISABLED
24 main(int argc, char *argv[])
26 SDL_Joystick *joystick = NULL;
27 SDL_Haptic *haptic = NULL;
28 SDL_JoystickID instance = -1;
29 SDL_bool keepGoing = SDL_TRUE;
31 SDL_bool enable_haptic = SDL_TRUE;
32 Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK;
34 for (i = 1; i < argc; ++i) {
35 if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) {
36 enable_haptic = SDL_FALSE;
41 init_subsystems |= SDL_INIT_HAPTIC;
44 /* Enable standard application logging */
45 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
47 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
49 /* Initialize SDL (Note: video is required to start event loop) */
50 if (SDL_Init(init_subsystems) < 0) {
51 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
56 //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
59 SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
61 SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
66 while(SDL_PollEvent(&event))
71 keepGoing = SDL_FALSE;
73 case SDL_JOYDEVICEADDED:
76 SDL_Log("Only one joystick supported by this test\n");
80 joystick = SDL_JoystickOpen(event.jdevice.which);
81 instance = SDL_JoystickInstanceID(joystick);
82 SDL_Log("Joy Added : %d : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
85 if (SDL_JoystickIsHaptic(joystick))
87 haptic = SDL_HapticOpenFromJoystick(joystick);
90 SDL_Log("Joy Haptic Opened\n");
91 if (SDL_HapticRumbleInit( haptic ) != 0)
93 SDL_Log("Could not init Rumble!: %s\n", SDL_GetError());
94 SDL_HapticClose(haptic);
98 SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError());
103 SDL_Log("No haptic found\n");
108 case SDL_JOYDEVICEREMOVED:
109 if (instance == event.jdevice.which)
111 SDL_Log("Joy Removed: %d\n", event.jdevice.which);
113 if(enable_haptic && haptic)
115 SDL_HapticClose(haptic);
118 SDL_JoystickClose(joystick);
121 SDL_Log("Unknown joystick diconnected\n");
124 case SDL_JOYAXISMOTION:
126 // SDL_Log("Axis Move: %d\n", event.jaxis.axis);
129 SDL_HapticRumblePlay(haptic, 0.25, 250);
131 case SDL_JOYBUTTONDOWN:
132 SDL_Log("Button Press: %d\n", event.jbutton.button);
133 if(enable_haptic && haptic)
135 SDL_HapticRumblePlay(haptic, 0.25, 250);
137 if (event.jbutton.button == 0) {
138 SDL_Log("Exiting due to button press of button 0\n");
139 keepGoing = SDL_FALSE;
142 case SDL_JOYBUTTONUP:
143 SDL_Log("Button Release: %d\n", event.jbutton.button);
156 main(int argc, char *argv[])
158 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick and haptic support.\n");