SDL  2.0
testrumble.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2020 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 Copyright (c) 2011, Edgar Simo Serra
14 All rights reserved.
15 
16 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
17 
18  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
19  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
20  * Neither the name of the Simple Directmedia Layer (SDL) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24 
25 /*
26  * includes
27  */
28 #include <stdlib.h>
29 #include <string.h> /* strstr */
30 #include <ctype.h> /* isdigit */
31 
32 #include "SDL.h"
33 
34 #ifndef SDL_HAPTIC_DISABLED
35 
36 static SDL_Haptic *haptic;
37 
38 
39 /**
40  * @brief The entry point of this force feedback demo.
41  * @param[in] argc Number of arguments.
42  * @param[in] argv Array of argc arguments.
43  */
44 int
45 main(int argc, char **argv)
46 {
47  int i;
48  char *name;
49  int index;
50 
51  /* Enable standard application logging */
53 
54  name = NULL;
55  index = -1;
56  if (argc > 1) {
57  size_t l;
58  name = argv[1];
59  if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
60  SDL_Log("USAGE: %s [device]\n"
61  "If device is a two-digit number it'll use it as an index, otherwise\n"
62  "it'll use it as if it were part of the device's name.\n",
63  argv[0]);
64  return 0;
65  }
66 
67  l = SDL_strlen(name);
68  if ((l < 3) && SDL_isdigit(name[0]) && ((l == 1) || SDL_isdigit(name[1]))) {
69  index = SDL_atoi(name);
70  name = NULL;
71  }
72  }
73 
74  /* Initialize the force feedbackness */
77  SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
78  if (SDL_NumHaptics() > 0) {
79  /* We'll just use index or the first force feedback device found */
80  if (name == NULL) {
81  i = (index != -1) ? index : 0;
82  }
83  /* Try to find matching device */
84  else {
85  for (i = 0; i < SDL_NumHaptics(); i++) {
86  if (strstr(SDL_HapticName(i), name) != NULL)
87  break;
88  }
89 
90  if (i >= SDL_NumHaptics()) {
91  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
92  name);
93  return 1;
94  }
95  }
96 
98  if (haptic == NULL) {
99  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
100  SDL_GetError());
101  return 1;
102  }
103  SDL_Log("Device: %s\n", SDL_HapticName(i));
104  } else {
105  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
106  return 1;
107  }
108 
109  /* We only want force feedback errors. */
110  SDL_ClearError();
111 
113  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!\n");
114  return 1;
115  }
116  if (SDL_HapticRumbleInit(haptic) != 0) {
117  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s\n", SDL_GetError());
118  return 1;
119  }
120  SDL_Log("Playing 2 second rumble at 0.5 magnitude.\n");
121  if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
122  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
123  return 1;
124  }
125  SDL_Delay(2000);
126  SDL_Log("Stopping rumble.\n");
128  SDL_Delay(2000);
129  SDL_Log("Playing 2 second rumble at 0.3 magnitude.\n");
130  if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
131  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
132  return 1;
133  }
134  SDL_Delay(2000);
135 
136  /* Quit */
137  if (haptic != NULL)
139  SDL_Quit();
140 
141  return 0;
142 }
143 
144 #else
145 
146 int
147 main(int argc, char *argv[])
148 {
149  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
150  return 1;
151 }
152 
153 #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_ClearError
#define SDL_ClearError
Definition: SDL_dynapi_overrides.h:114
SDL_HapticRumbleStop
#define SDL_HapticRumbleStop
Definition: SDL_dynapi_overrides.h:188
NULL
#define NULL
Definition: begin_code.h:167
SDL_INIT_JOYSTICK
#define SDL_INIT_JOYSTICK
Definition: SDL.h:81
SDL_HapticOpen
#define SDL_HapticOpen
Definition: SDL_dynapi_overrides.h:161
main
int main(int argc, char **argv)
The entry point of this force feedback demo.
Definition: testrumble.c:45
index
GLuint index
Definition: SDL_opengl_glext.h:663
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_HapticClose
#define SDL_HapticClose
Definition: SDL_dynapi_overrides.h:168
SDL_HapticName
#define SDL_HapticName
Definition: SDL_dynapi_overrides.h:160
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
f
GLfloat f
Definition: SDL_opengl_glext.h:1873
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:663
SDL_isdigit
#define SDL_isdigit
Definition: SDL_dynapi_overrides.h:382
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
haptic
static SDL_Haptic * haptic
Definition: testrumble.c:36
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
SDL_INIT_HAPTIC
#define SDL_INIT_HAPTIC
Definition: SDL.h:82
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:410
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:80
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
l
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 int int return Display Window Cursor return Display Window return Display Drawable GC int int unsigned int unsigned int return Display Drawable GC int int _Xconst char int return Display Drawable GC int int unsigned int unsigned int return Display return Display Cursor return Display GC return XModifierKeymap return char Display Window int return Display return Display int int int return Display long XVisualInfo int return Display Window Atom long long Bool Atom Atom int unsigned long unsigned long unsigned char * l)
Definition: SDL_x11sym.h:80
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_INIT_TIMER
#define SDL_INIT_TIMER
Definition: SDL.h:78
SDL_HapticRumbleInit
#define SDL_HapticRumbleInit
Definition: SDL_dynapi_overrides.h:186
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:393
SDL_HapticRumbleSupported
#define SDL_HapticRumbleSupported
Definition: SDL_dynapi_overrides.h:185
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