Debian Codegraph @

test/testrumble.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
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 "SDL.h"
29
30 #ifndef SDL_HAPTIC_DISABLED
31
32 static SDL_Haptic *haptic;
33
34 /**
35 * @brief The entry point of this force feedback demo.
36 * @param[in] argc Number of arguments.
37 * @param[in] argv Array of argc arguments.
38 */
39 int main(int argc, char **argv)
40 {
41 int i;
42 char *name;
43 int index;
44
45 /* Enable standard application logging */
46 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
47
48 name = NULL;
49 index = -1;
50 if (argc > 1) {
51 size_t l;
52 name = argv[1];
53 if ((SDL_strcmp(name, "--help") == 0) || (SDL_strcmp(name, "-h") == 0)) {
54 SDL_Log("USAGE: %s [device]\n"
55 "If device is a two-digit number it'll use it as an index, otherwise\n"
56 "it'll use it as if it were part of the device's name.\n",
57 argv[0]);
58 return 0;
59 }
60
61 l = SDL_strlen(name);
62 if ((l < 3) && SDL_isdigit(name[0]) && ((l == 1) || SDL_isdigit(name[1]))) {
63 index = SDL_atoi(name);
64 name = NULL;
65 }
66 }
67
68 /* Initialize the force feedbackness */
69 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
70 SDL_INIT_HAPTIC);
71 SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
72 if (SDL_NumHaptics() > 0) {
73 /* We'll just use index or the first force feedback device found */
74 if (!name) {
75 i = (index != -1) ? index : 0;
76 }
77 /* Try to find matching device */
78 else {
79 for (i = 0; i < SDL_NumHaptics(); i++) {
80 if (SDL_strstr(SDL_HapticName(i), name) != NULL) {
81 break;
82 }
83 }
84
85 if (i >= SDL_NumHaptics()) {
86 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
87 name);
88 return 1;
89 }
90 }
91
92 haptic = SDL_HapticOpen(i);
93 if (!haptic) {
94 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
95 SDL_GetError());
96 return 1;
97 }
98 SDL_Log("Device: %s\n", SDL_HapticName(i));
99 } else {
100 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
101 return 1;
102 }
103
104 /* We only want force feedback errors. */
105 SDL_ClearError();
106
107 if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
108 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!\n");
109 return 1;
110 }
111 if (SDL_HapticRumbleInit(haptic) != 0) {
112 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s\n", SDL_GetError());
113 return 1;
114 }
115 SDL_Log("Playing 2 second rumble at 0.5 magnitude.\n");
116 if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
117 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError());
118 return 1;
119 }
120 SDL_Delay(2000);
121 SDL_Log("Stopping rumble.\n");
122 SDL_HapticRumbleStop(haptic);
123 SDL_Delay(2000);
124 SDL_Log("Playing 2 second rumble at 0.3 magnitude.\n");
125 if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
126 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError());
127 return 1;
128 }
129 SDL_Delay(2000);
130
131 /* Quit */
132 if (haptic) {
133 SDL_HapticClose(haptic);
134 }
135 SDL_Quit();
136
137 return 0;
138 }
139
140 #else
141
142 int
143 main(int argc, char *argv[])
144 {
145 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
146 return 1;
147 }
148
149 #endif