SDL  2.0
testhotplug.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2018 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 <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
24 main(int argc, char *argv[])
25 {
26  SDL_Joystick *joystick = NULL;
27  SDL_Haptic *haptic = NULL;
28  SDL_JoystickID instance = -1;
29  SDL_bool keepGoing = SDL_TRUE;
30  int i;
31  SDL_bool enable_haptic = SDL_TRUE;
32  Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK;
33 
34  for (i = 1; i < argc; ++i) {
35  if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) {
36  enable_haptic = SDL_FALSE;
37  }
38  }
39 
40  if(enable_haptic) {
41  init_subsystems |= SDL_INIT_HAPTIC;
42  }
43 
44  /* Enable standard application logging */
46 
48 
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());
52  exit(1);
53  }
54 
55  /*
56  //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
57  */
58 
59  SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
60  if (enable_haptic)
61  SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
62 
63  while(keepGoing)
64  {
66  while(SDL_PollEvent(&event))
67  {
68  switch(event.type)
69  {
70  case SDL_QUIT:
71  keepGoing = SDL_FALSE;
72  break;
73  case SDL_JOYDEVICEADDED:
74  if (joystick != NULL)
75  {
76  SDL_Log("Only one joystick supported by this test\n");
77  }
78  else
79  {
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));
83  if (enable_haptic)
84  {
85  if (SDL_JoystickIsHaptic(joystick))
86  {
88  if (haptic)
89  {
90  SDL_Log("Joy Haptic Opened\n");
91  if (SDL_HapticRumbleInit( haptic ) != 0)
92  {
93  SDL_Log("Could not init Rumble!: %s\n", SDL_GetError());
95  haptic = NULL;
96  }
97  } else {
98  SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError());
99  }
100  }
101  else
102  {
103  SDL_Log("No haptic found\n");
104  }
105  }
106  }
107  break;
109  if (instance == event.jdevice.which)
110  {
111  SDL_Log("Joy Removed: %d\n", event.jdevice.which);
112  instance = -1;
113  if(enable_haptic && haptic)
114  {
116  haptic = NULL;
117  }
118  SDL_JoystickClose(joystick);
119  joystick = NULL;
120  } else {
121  SDL_Log("Unknown joystick diconnected\n");
122  }
123  break;
124  case SDL_JOYAXISMOTION:
125 /*
126 // SDL_Log("Axis Move: %d\n", event.jaxis.axis);
127 */
128  if (enable_haptic)
129  SDL_HapticRumblePlay(haptic, 0.25, 250);
130  break;
131  case SDL_JOYBUTTONDOWN:
132  SDL_Log("Button Press: %d\n", event.jbutton.button);
133  if(enable_haptic && haptic)
134  {
135  SDL_HapticRumblePlay(haptic, 0.25, 250);
136  }
137  if (event.jbutton.button == 0) {
138  SDL_Log("Exiting due to button press of button 0\n");
139  keepGoing = SDL_FALSE;
140  }
141  break;
142  case SDL_JOYBUTTONUP:
143  SDL_Log("Button Release: %d\n", event.jbutton.button);
144  break;
145  }
146  }
147  }
148 
149  SDL_Quit();
150 
151  return 0;
152 }
153 #else
154 
155 int
156 main(int argc, char *argv[])
157 {
158  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick and haptic support.\n");
159  return 1;
160 }
161 
162 #endif
SDL.h
SDL_HapticRumblePlay
#define SDL_HapticRumblePlay
Definition: SDL_dynapi_overrides.h:187
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
#define SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
A variable that lets you enable joystick (and gamecontroller) events even when your app is in the bac...
Definition: SDL_hints.h:476
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_JoystickClose
#define SDL_JoystickClose
Definition: SDL_dynapi_overrides.h:215
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_HapticOpenFromJoystick
#define SDL_HapticOpenFromJoystick
Definition: SDL_dynapi_overrides.h:167
NULL
#define NULL
Definition: begin_code.h:164
SDL_JoystickID
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81
SDL_INIT_JOYSTICK
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
SDL_NumJoysticks
#define SDL_NumJoysticks
Definition: SDL_dynapi_overrides.h:195
main
int main(int argc, char *argv[])
Definition: testhotplug.c:24
SDL_JoystickInstanceID
#define SDL_JoystickInstanceID
Definition: SDL_dynapi_overrides.h:204
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_JOYDEVICEREMOVED
@ SDL_JOYDEVICEREMOVED
Definition: SDL_events.h:117
SDL_JoystickOpen
#define SDL_JoystickOpen
Definition: SDL_dynapi_overrides.h:197
SDL_JoystickName
#define SDL_JoystickName
Definition: SDL_dynapi_overrides.h:198
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_HapticClose
#define SDL_HapticClose
Definition: SDL_dynapi_overrides.h:168
SDL_strcasecmp
#define SDL_strcasecmp
Definition: SDL_dynapi_overrides.h:419
SDL_JoystickIsHaptic
#define SDL_JoystickIsHaptic
Definition: SDL_dynapi_overrides.h:166
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_JOYAXISMOTION
@ SDL_JOYAXISMOTION
Definition: SDL_events.h:111
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_INIT_HAPTIC
#define SDL_INIT_HAPTIC
Definition: SDL.h:81
haptic
static SDL_Haptic * haptic
Definition: testhaptic.c:25
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:79
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_JOYBUTTONUP
@ SDL_JOYBUTTONUP
Definition: SDL_events.h:115
SDL_HapticRumbleInit
#define SDL_HapticRumbleInit
Definition: SDL_dynapi_overrides.h:186
SDL_SetHint
#define SDL_SetHint
Definition: SDL_dynapi_overrides.h:190
SDL_JOYDEVICEADDED
@ SDL_JOYDEVICEADDED
Definition: SDL_events.h:116
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_Event
General event structure.
Definition: SDL_events.h:558
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_JOYBUTTONDOWN
@ SDL_JOYBUTTONDOWN
Definition: SDL_events.h:114
SDL_NumHaptics
#define SDL_NumHaptics
Definition: SDL_dynapi_overrides.h:159
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50