SDL 2.0
testrumble.c File Reference
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "SDL.h"
+ Include dependency graph for testrumble.c:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 The entry point of this force feedback demo. More...
 

Variables

static SDL_Haptic * haptic
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

The entry point of this force feedback demo.

Parameters
[in]argcNumber of arguments.
[in]argvArray of argc arguments.

Definition at line 45 of file testrumble.c.

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]))) {
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. */
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}
#define SDL_INIT_TIMER
Definition: SDL.h:77
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
#define SDL_INIT_HAPTIC
Definition: SDL.h:81
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_HapticRumbleSupported
#define SDL_GetError
#define SDL_isdigit
#define SDL_HapticRumbleStop
#define SDL_strlen
#define SDL_HapticRumbleInit
#define SDL_LogSetPriority
#define SDL_ClearError
#define SDL_HapticRumblePlay
#define SDL_LogError
#define SDL_HapticName
#define SDL_HapticClose
#define SDL_Delay
#define SDL_Quit
#define SDL_Init
#define SDL_atoi
#define SDL_Log
#define SDL_HapticOpen
#define SDL_NumHaptics
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLuint index
GLuint const GLchar * name
@ SDL_FALSE
Definition: SDL_stdinc.h:163
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
#define NULL
Definition: begin_code.h:167
static SDL_Haptic * haptic
Definition: testrumble.c:36

References haptic, i, NULL, SDL_atoi, SDL_ClearError, SDL_Delay, SDL_FALSE, SDL_GetError, SDL_HapticClose, SDL_HapticName, SDL_HapticOpen, SDL_HapticRumbleInit, SDL_HapticRumblePlay, SDL_HapticRumbleStop, SDL_HapticRumbleSupported, SDL_Init, SDL_INIT_HAPTIC, SDL_INIT_JOYSTICK, SDL_INIT_TIMER, SDL_INIT_VIDEO, SDL_isdigit, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_NumHaptics, SDL_Quit, and SDL_strlen.

Variable Documentation

◆ haptic

SDL_Haptic* haptic
static

Definition at line 36 of file testrumble.c.

Referenced by main().