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

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.

References haptic, i, NULL, 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_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_NumHaptics, and SDL_Quit.

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  name = argv[1];
58  if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
59  SDL_Log("USAGE: %s [device]\n"
60  "If device is a two-digit number it'll use it as an index, otherwise\n"
61  "it'll use it as if it were part of the device's name.\n",
62  argv[0]);
63  return 0;
64  }
65 
66  i = strlen(name);
67  if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
68  index = atoi(name);
69  name = NULL;
70  }
71  }
72 
73  /* Initialize the force feedbackness */
76  SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
77  if (SDL_NumHaptics() > 0) {
78  /* We'll just use index or the first force feedback device found */
79  if (name == NULL) {
80  i = (index != -1) ? index : 0;
81  }
82  /* Try to find matching device */
83  else {
84  for (i = 0; i < SDL_NumHaptics(); i++) {
85  if (strstr(SDL_HapticName(i), name) != NULL)
86  break;
87  }
88 
89  if (i >= SDL_NumHaptics()) {
90  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
91  name);
92  return 1;
93  }
94  }
95 
97  if (haptic == NULL) {
98  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
99  SDL_GetError());
100  return 1;
101  }
102  SDL_Log("Device: %s\n", SDL_HapticName(i));
103  } else {
104  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
105  return 1;
106  }
107 
108  /* We only want force feedback errors. */
109  SDL_ClearError();
110 
112  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!\n");
113  return 1;
114  }
115  if (SDL_HapticRumbleInit(haptic) != 0) {
116  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s\n", SDL_GetError());
117  return 1;
118  }
119  SDL_Log("Playing 2 second rumble at 0.5 magnitude.\n");
120  if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
121  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
122  return 1;
123  }
124  SDL_Delay(2000);
125  SDL_Log("Stopping rumble.\n");
127  SDL_Delay(2000);
128  SDL_Log("Playing 2 second rumble at 0.3 magnitude.\n");
129  if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
130  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
131  return 1;
132  }
133  SDL_Delay(2000);
134 
135  /* Quit */
136  if (haptic != NULL)
138  SDL_Quit();
139 
140  return 0;
141 }
#define SDL_ClearError
#define SDL_GetError
#define SDL_INIT_JOYSTICK
Definition: SDL.h:78
#define SDL_HapticRumbleStop
#define SDL_HapticOpen
GLfloat f
GLuint const GLchar * name
#define SDL_HapticClose
#define SDL_LogError
#define SDL_Log
#define SDL_HapticName
#define SDL_Quit
GLuint index
#define SDL_Delay
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:42
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:143
#define SDL_INIT_TIMER
Definition: SDL.h:75
static SDL_Haptic * haptic
Definition: testrumble.c:36
#define SDL_HapticRumbleSupported
#define SDL_NumHaptics
#define SDL_HapticRumbleInit
#define SDL_Init
#define SDL_INIT_HAPTIC
Definition: SDL.h:79
#define SDL_INIT_VIDEO
Definition: SDL.h:77
#define SDL_HapticRumblePlay

Variable Documentation

SDL_Haptic* haptic
static

Definition at line 36 of file testrumble.c.

Referenced by main().