~ubuntu-branches/ubuntu/trusty/libsdl2/trusty-proposed

« back to all changes in this revision

Viewing changes to test/testhaptic.c

  • Committer: Package Import Robot
  • Author(s): Manuel A. Fernandez Montecelo
  • Date: 2013-12-28 12:31:19 UTC
  • mto: (7.1.3 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20131228123119-wehupm72qsjvh6vz
Tags: upstream-2.0.1+dfsg1
ImportĀ upstreamĀ versionĀ 2.0.1+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 * includes
27
27
 */
28
28
#include <stdlib.h>
29
 
#include <stdio.h>              /* printf */
30
29
#include <string.h>             /* strstr */
31
30
#include <ctype.h>              /* isdigit */
32
31
 
62
61
    int nefx;
63
62
    unsigned int supported;
64
63
 
 
64
    /* Enable standard application logging */
 
65
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
66
 
65
67
    name = NULL;
66
68
    index = -1;
67
69
    if (argc > 1) {
68
70
        name = argv[1];
69
71
        if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
70
 
            printf("USAGE: %s [device]\n"
 
72
            SDL_Log("USAGE: %s [device]\n"
71
73
                   "If device is a two-digit number it'll use it as an index, otherwise\n"
72
74
                   "it'll use it as if it were part of the device's name.\n",
73
75
                   argv[0]);
84
86
    /* Initialize the force feedbackness */
85
87
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
86
88
             SDL_INIT_HAPTIC);
87
 
    printf("%d Haptic devices detected.\n", SDL_NumHaptics());
 
89
    SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
88
90
    if (SDL_NumHaptics() > 0) {
89
91
        /* We'll just use index or the first force feedback device found */
90
92
        if (name == NULL) {
98
100
            }
99
101
 
100
102
            if (i >= SDL_NumHaptics()) {
101
 
                printf("Unable to find device matching '%s', aborting.\n",
 
103
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
102
104
                       name);
103
105
                return 1;
104
106
            }
106
108
 
107
109
        haptic = SDL_HapticOpen(i);
108
110
        if (haptic == NULL) {
109
 
            printf("Unable to create the haptic device: %s\n",
 
111
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
110
112
                   SDL_GetError());
111
113
            return 1;
112
114
        }
113
 
        printf("Device: %s\n", SDL_HapticName(i));
 
115
        SDL_Log("Device: %s\n", SDL_HapticName(i));
114
116
        HapticPrintSupported(haptic);
115
117
    } else {
116
 
        printf("No Haptic devices found!\n");
 
118
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
117
119
        return 1;
118
120
    }
119
121
 
125
127
    nefx = 0;
126
128
    supported = SDL_HapticQuery(haptic);
127
129
 
128
 
    printf("\nUploading effects\n");
 
130
    SDL_Log("\nUploading effects\n");
129
131
    /* First we'll try a SINE effect. */
130
132
    if (supported & SDL_HAPTIC_SINE) {
131
 
        printf("   effect %d: Sine Wave\n", nefx);
 
133
        SDL_Log("   effect %d: Sine Wave\n", nefx);
132
134
        efx[nefx].type = SDL_HAPTIC_SINE;
133
135
        efx[nefx].periodic.period = 1000;
134
136
        efx[nefx].periodic.magnitude = 0x4000;
137
139
        efx[nefx].periodic.fade_length = 1000;
138
140
        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
139
141
        if (id[nefx] < 0) {
140
 
            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
 
142
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
141
143
            abort_execution();
142
144
        }
143
145
        nefx++;
144
146
    }
145
147
    /* Now we'll try a SAWTOOTHUP */
146
148
    if (supported & SDL_HAPTIC_SAWTOOTHUP) {
147
 
        printf("   effect %d: Sawtooth Up\n", nefx);
 
149
        SDL_Log("   effect %d: Sawtooth Up\n", nefx);
148
150
        efx[nefx].type = SDL_HAPTIC_SAWTOOTHUP;
149
151
        efx[nefx].periodic.period = 500;
150
152
        efx[nefx].periodic.magnitude = 0x5000;
153
155
        efx[nefx].periodic.fade_length = 1000;
154
156
        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
155
157
        if (id[nefx] < 0) {
156
 
            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
 
158
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
157
159
            abort_execution();
158
160
        }
159
161
        nefx++;
160
162
    }
161
163
    /* Now the classical constant effect. */
162
164
    if (supported & SDL_HAPTIC_CONSTANT) {
163
 
        printf("   effect %d: Constant Force\n", nefx);
 
165
        SDL_Log("   effect %d: Constant Force\n", nefx);
164
166
        efx[nefx].type = SDL_HAPTIC_CONSTANT;
165
167
        efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
166
168
        efx[nefx].constant.direction.dir[0] = 20000;    /* Force comes from the south-west. */
170
172
        efx[nefx].constant.fade_length = 1000;
171
173
        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
172
174
        if (id[nefx] < 0) {
173
 
            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
 
175
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
174
176
            abort_execution();
175
177
        }
176
178
        nefx++;
177
179
    }
178
180
    /* The cute spring effect. */
179
181
    if (supported & SDL_HAPTIC_SPRING) {
180
 
        printf("   effect %d: Condition Spring\n", nefx);
 
182
        SDL_Log("   effect %d: Condition Spring\n", nefx);
181
183
        efx[nefx].type = SDL_HAPTIC_SPRING;
182
184
        efx[nefx].condition.length = 5000;
183
185
        for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
189
191
        }
190
192
        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
191
193
        if (id[nefx] < 0) {
192
 
            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
 
194
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
193
195
            abort_execution();
194
196
        }
195
197
        nefx++;
196
198
    }
197
199
    /* The pretty awesome inertia effect. */
198
200
    if (supported & SDL_HAPTIC_INERTIA) {
199
 
        printf("   effect %d: Condition Inertia\n", nefx);
 
201
        SDL_Log("   effect %d: Condition Inertia\n", nefx);
200
202
        efx[nefx].type = SDL_HAPTIC_SPRING;
201
203
        efx[nefx].condition.length = 5000;
202
204
        for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
207
209
        }
208
210
        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
209
211
        if (id[nefx] < 0) {
210
 
            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
 
212
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
211
213
            abort_execution();
212
214
        }
213
215
        nefx++;
215
217
 
216
218
    /* Finally we'll try a left/right effect. */
217
219
    if (supported & SDL_HAPTIC_LEFTRIGHT) {
218
 
        printf("   effect %d: Left/Right\n", nefx);
 
220
        SDL_Log("   effect %d: Left/Right\n", nefx);
219
221
        efx[nefx].type = SDL_HAPTIC_LEFTRIGHT;
220
222
        efx[nefx].leftright.length = 5000;
221
223
        efx[nefx].leftright.large_magnitude = 0x3000;
222
224
        efx[nefx].leftright.small_magnitude = 0xFFFF;
223
225
        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
224
226
        if (id[nefx] < 0) {
225
 
            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
 
227
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
226
228
            abort_execution();
227
229
        }
228
230
        nefx++;
229
231
    }
230
232
 
231
233
 
232
 
    printf
 
234
    SDL_Log
233
235
        ("\nNow playing effects for 5 seconds each with 1 second delay between\n");
234
236
    for (i = 0; i < nefx; i++) {
235
 
        printf("   Playing effect %d\n", i);
 
237
        SDL_Log("   Playing effect %d\n", i);
236
238
        SDL_HapticRunEffect(haptic, id[i], 1);
237
239
        SDL_Delay(6000);        /* Effects only have length 5000 */
238
240
    }
252
254
static void
253
255
abort_execution(void)
254
256
{
255
 
    printf("\nAborting program execution.\n");
 
257
    SDL_Log("\nAborting program execution.\n");
256
258
 
257
259
    SDL_HapticClose(haptic);
258
260
    SDL_Quit();
270
272
    unsigned int supported;
271
273
 
272
274
    supported = SDL_HapticQuery(haptic);
273
 
    printf("   Supported effects [%d effects, %d playing]:\n",
 
275
    SDL_Log("   Supported effects [%d effects, %d playing]:\n",
274
276
           SDL_HapticNumEffects(haptic), SDL_HapticNumEffectsPlaying(haptic));
275
277
    if (supported & SDL_HAPTIC_CONSTANT)
276
 
        printf("      constant\n");
 
278
        SDL_Log("      constant\n");
277
279
    if (supported & SDL_HAPTIC_SINE)
278
 
        printf("      sine\n");
 
280
        SDL_Log("      sine\n");
279
281
    /* !!! FIXME: put this back when we have more bits in 2.1 */
280
 
    /*if (supported & SDL_HAPTIC_SQUARE)
281
 
        printf("      square\n");*/
 
282
    /* if (supported & SDL_HAPTIC_SQUARE)
 
283
        SDL_Log("      square\n"); */
282
284
    if (supported & SDL_HAPTIC_TRIANGLE)
283
 
        printf("      triangle\n");
 
285
        SDL_Log("      triangle\n");
284
286
    if (supported & SDL_HAPTIC_SAWTOOTHUP)
285
 
        printf("      sawtoothup\n");
 
287
        SDL_Log("      sawtoothup\n");
286
288
    if (supported & SDL_HAPTIC_SAWTOOTHDOWN)
287
 
        printf("      sawtoothdown\n");
 
289
        SDL_Log("      sawtoothdown\n");
288
290
    if (supported & SDL_HAPTIC_RAMP)
289
 
        printf("      ramp\n");
 
291
        SDL_Log("      ramp\n");
290
292
    if (supported & SDL_HAPTIC_FRICTION)
291
 
        printf("      friction\n");
 
293
        SDL_Log("      friction\n");
292
294
    if (supported & SDL_HAPTIC_SPRING)
293
 
        printf("      spring\n");
 
295
        SDL_Log("      spring\n");
294
296
    if (supported & SDL_HAPTIC_DAMPER)
295
 
        printf("      damper\n");
 
297
        SDL_Log("      damper\n");
296
298
    if (supported & SDL_HAPTIC_INERTIA)
297
 
        printf("      intertia\n");
 
299
        SDL_Log("      inertia\n");
298
300
    if (supported & SDL_HAPTIC_CUSTOM)
299
 
        printf("      custom\n");
 
301
        SDL_Log("      custom\n");
300
302
    if (supported & SDL_HAPTIC_LEFTRIGHT)
301
 
        printf("      left/right\n");
302
 
    printf("   Supported capabilities:\n");
 
303
        SDL_Log("      left/right\n");
 
304
    SDL_Log("   Supported capabilities:\n");
303
305
    if (supported & SDL_HAPTIC_GAIN)
304
 
        printf("      gain\n");
 
306
        SDL_Log("      gain\n");
305
307
    if (supported & SDL_HAPTIC_AUTOCENTER)
306
 
        printf("      autocenter\n");
 
308
        SDL_Log("      autocenter\n");
307
309
    if (supported & SDL_HAPTIC_STATUS)
308
 
        printf("      status\n");
 
310
        SDL_Log("      status\n");
309
311
}
310
312
 
311
313
#else
313
315
int
314
316
main(int argc, char *argv[])
315
317
{
316
 
    fprintf(stderr, "SDL compiled without Haptic support.\n");
 
318
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
317
319
    exit(1);
318
320
}
319
321