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] | argc | Number of arguments.
  [in] | argv | Array of argc arguments.

Definition at line 45 of file testrumble.c.

{
int i;
char *name;
int index;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
name = NULL;
index = -1;
if (argc > 1) {
size_t l;
name = argv[1];
if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
SDL_Log("USAGE: %s [device]\n"
"If device is a two-digit number it'll use it as an index, otherwise\n"
"it'll use it as if it were part of the device's name.\n",
argv[0]);
return 0;
}
l = SDL_strlen(name);
if ((l < 3) && SDL_isdigit(name[0]) && ((l == 1) || SDL_isdigit(name[1]))) {
index = SDL_atoi(name);
name = NULL;
}
}
/* Initialize the force feedbackness */
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
SDL_INIT_HAPTIC);
SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
if (SDL_NumHaptics() > 0) {
/* We'll just use index or the first force feedback device found */
if (name == NULL) {
i = (index != -1) ? index : 0;
}
/* Try to find matching device */
else {
for (i = 0; i < SDL_NumHaptics(); i++) {
if (strstr(SDL_HapticName(i), name) != NULL)
break;
}
if (i >= SDL_NumHaptics()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
name);
return 1;
}
}
haptic = SDL_HapticOpen(i);
if (haptic == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
SDL_GetError());
return 1;
}
SDL_Log("Device: %s\n", SDL_HapticName(i));
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
return 1;
}
/* We only want force feedback errors. */
SDL_ClearError();
if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!\n");
return 1;
}
if (SDL_HapticRumbleInit(haptic) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s\n", SDL_GetError());
return 1;
}
SDL_Log("Playing 2 second rumble at 0.5 magnitude.\n");
if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
return 1;
}
SDL_Delay(2000);
SDL_Log("Stopping rumble.\n");
SDL_HapticRumbleStop(haptic);
SDL_Delay(2000);
SDL_Log("Playing 2 second rumble at 0.3 magnitude.\n");
if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
return 1;
}
SDL_Delay(2000);
/* Quit */
if (haptic != NULL)
SDL_HapticClose(haptic);
SDL_Quit();
return 0;
}

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().

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
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
NULL
#define NULL
Definition: begin_code.h:167
SDL_INIT_JOYSTICK
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
SDL_HapticOpen
#define SDL_HapticOpen
Definition: SDL_dynapi_overrides.h:161
index
GLuint index
Definition: SDL_opengl_glext.h:660
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
f
GLfloat f
Definition: SDL_opengl_glext.h:1870
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
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:81
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:410
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_INIT_TIMER
#define SDL_INIT_TIMER
Definition: SDL.h:77
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