summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testrumble.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testrumble.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testrumble.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testrumble.c164
1 files changed, 164 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testrumble.c b/src/contrib/SDL-3.2.20/test/testrumble.c
new file mode 100644
index 0000000..df35e0b
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testrumble.c
@@ -0,0 +1,164 @@
1/*
2 Copyright (C) 1997-2025 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/*
13Copyright (c) 2011, Edgar Simo Serra
14All rights reserved.
15
16Redistribution 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
22THIS 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#include <SDL3/SDL.h>
26#include <SDL3/SDL_main.h>
27#include <SDL3/SDL_test.h>
28
29static SDL_Haptic *haptic;
30
31/**
32 * The entry point of this force feedback demo.
33 * \param[in] argc Number of arguments.
34 * \param[in] argv Array of argc arguments.
35 */
36int main(int argc, char **argv)
37{
38 int i;
39 char *name = NULL;
40 int index;
41 SDLTest_CommonState *state;
42 SDL_HapticID *haptics;
43 int num_haptics;
44
45 /* Initialize test framework */
46 state = SDLTest_CommonCreateState(argv, 0);
47 if (!state) {
48 return 1;
49 }
50
51 name = NULL;
52 index = -1;
53
54 /* Parse commandline */
55 for (i = 1; i < argc;) {
56 int consumed;
57
58 consumed = SDLTest_CommonArg(state, i);
59 if (!consumed) {
60 if (!name && index < 0) {
61 size_t l;
62
63 l = SDL_strlen(argv[i]);
64 if ((l < 3) && SDL_isdigit(argv[i][0]) && ((l == 1) || SDL_isdigit(argv[i][1]))) {
65 index = SDL_atoi(argv[i]);
66 } else {
67 name = argv[i];
68 }
69 consumed = 1;
70 }
71 }
72 if (consumed <= 0) {
73 static const char *options[] = { "[device]", NULL };
74 SDLTest_CommonLogUsage(state, argv[0], options);
75 SDL_Log("%s", "");
76 SDL_Log("If device is a two-digit number it'll use it as an index, otherwise\n"
77 "it'll use it as if it were part of the device's name.");
78 return 1;
79 }
80
81 i += consumed;
82 }
83
84 /* Initialize the force feedbackness */
85 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
86 haptics = SDL_GetHaptics(&num_haptics);
87 SDL_Log("%d Haptic devices detected.", num_haptics);
88 if (num_haptics == 0) {
89 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!");
90 SDL_free(haptics);
91 return 1;
92 }
93
94 /* We'll just use index or the first force feedback device found */
95 if (!name) {
96 i = (index != -1) ? index : 0;
97
98 if (i >= num_haptics) {
99 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.");
100 SDL_free(haptics);
101 return 1;
102 }
103 }
104 /* Try to find matching device */
105 else {
106 for (i = 0; i < num_haptics; i++) {
107 if (SDL_strstr(SDL_GetHapticNameForID(haptics[i]), name) != NULL) {
108 break;
109 }
110 }
111
112 if (i >= num_haptics) {
113 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.", name);
114 SDL_free(haptics);
115 return 1;
116 }
117 }
118
119 haptic = SDL_OpenHaptic(haptics[i]);
120 if (!haptic) {
121 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s", SDL_GetError());
122 SDL_free(haptics);
123 return 1;
124 }
125 SDL_Log("Device: %s", SDL_GetHapticName(haptic));
126 SDL_free(haptics);
127
128 /* We only want force feedback errors. */
129 SDL_ClearError();
130
131 if (!SDL_HapticRumbleSupported(haptic)) {
132 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!");
133 return 1;
134 }
135 if (!SDL_InitHapticRumble(haptic)) {
136 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s", SDL_GetError());
137 return 1;
138 }
139 SDL_Log("Playing 2 second rumble at 0.5 magnitude.");
140 if (!SDL_PlayHapticRumble(haptic, 0.5, 5000)) {
141 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s", SDL_GetError());
142 return 1;
143 }
144 SDL_Delay(2000);
145 SDL_Log("Stopping rumble.");
146 SDL_StopHapticRumble(haptic);
147 SDL_Delay(2000);
148 SDL_Log("Playing 2 second rumble at 0.3 magnitude.");
149 if (!SDL_PlayHapticRumble(haptic, 0.3f, 5000)) {
150 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s", SDL_GetError());
151 return 1;
152 }
153 SDL_Delay(2000);
154
155 /* Quit */
156 if (haptic) {
157 SDL_CloseHaptic(haptic);
158 }
159
160 SDL_Quit();
161 SDLTest_CommonDestroyState(state);
162
163 return 0;
164}