src/haptic/SDL_haptic.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Jan 2019 22:01:14 -0800
changeset 12503 806492103856
parent 12462 b57435750c5b
child 13422 fd6a12de91c7
permissions -rw-r--r--
Updated copyright for 2019
slouken@2713
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@2713
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@2713
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@2713
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2713
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@2713
    22
slouken@2713
    23
#include "SDL_syshaptic.h"
slouken@4472
    24
#include "SDL_haptic_c.h"
slouken@2713
    25
#include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
icculus@7621
    26
#include "SDL_assert.h"
slouken@2713
    27
slouken@10610
    28
/* Global for SDL_windowshaptic.c */
sylvain@12462
    29
#if (defined(SDL_HAPTIC_DINPUT) && SDL_HAPTIC_DINPUT) || (defined(SDL_HAPTIC_XINPUT) && SDL_HAPTIC_XINPUT)
slouken@10610
    30
SDL_Haptic *SDL_haptics = NULL;
sylvain@12462
    31
#else 
sylvain@12462
    32
static SDL_Haptic *SDL_haptics = NULL;
sylvain@12462
    33
#endif
slouken@2713
    34
slouken@2713
    35
/*
slouken@2713
    36
 * Initializes the Haptic devices.
slouken@2713
    37
 */
slouken@2713
    38
int
slouken@2713
    39
SDL_HapticInit(void)
slouken@2713
    40
{
slouken@2713
    41
    int status;
slouken@2713
    42
slouken@2713
    43
    status = SDL_SYS_HapticInit();
slouken@2713
    44
    if (status >= 0) {
slouken@2713
    45
        status = 0;
slouken@2713
    46
    }
slouken@2713
    47
slouken@2713
    48
    return status;
slouken@2713
    49
}
slouken@2713
    50
slouken@2713
    51
slouken@2713
    52
/*
slouken@2713
    53
 * Checks to see if the haptic device is valid
slouken@2713
    54
 */
slouken@2713
    55
static int
slouken@2713
    56
ValidHaptic(SDL_Haptic * haptic)
slouken@2713
    57
{
slouken@2713
    58
    int valid;
urkle@8170
    59
    SDL_Haptic *hapticlist;
slouken@2713
    60
slouken@2713
    61
    valid = 0;
bobbens@3080
    62
    if (haptic != NULL) {
urkle@8170
    63
        hapticlist = SDL_haptics;
urkle@8170
    64
        while ( hapticlist )
urkle@8170
    65
        {
urkle@8170
    66
            if (hapticlist == haptic) {
bobbens@3080
    67
                valid = 1;
bobbens@3080
    68
                break;
bobbens@3080
    69
            }
urkle@8170
    70
            hapticlist = hapticlist->next;
slouken@2713
    71
        }
slouken@2713
    72
    }
slouken@2713
    73
bobbens@3080
    74
    /* Create the error here. */
bobbens@3080
    75
    if (valid == 0) {
bobbens@3080
    76
        SDL_SetError("Haptic: Invalid haptic device identifier");
bobbens@3080
    77
    }
bobbens@3080
    78
slouken@2713
    79
    return valid;
slouken@2713
    80
}
slouken@2713
    81
slouken@2713
    82
slouken@2713
    83
/*
slouken@2713
    84
 * Returns the number of available devices.
slouken@2713
    85
 */
slouken@2713
    86
int
slouken@2713
    87
SDL_NumHaptics(void)
slouken@2713
    88
{
urkle@8170
    89
    return SDL_SYS_NumHaptics();
slouken@2713
    90
}
slouken@2713
    91
slouken@2713
    92
slouken@2713
    93
/*
slouken@2713
    94
 * Gets the name of a Haptic device by index.
slouken@2713
    95
 */
slouken@2713
    96
const char *
slouken@2713
    97
SDL_HapticName(int device_index)
slouken@2713
    98
{
urkle@8170
    99
    if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
slouken@2713
   100
        SDL_SetError("Haptic: There are %d haptic devices available",
urkle@8170
   101
                     SDL_NumHaptics());
slouken@2713
   102
        return NULL;
slouken@2713
   103
    }
slouken@2713
   104
    return SDL_SYS_HapticName(device_index);
slouken@2713
   105
}
slouken@2713
   106
slouken@2713
   107
slouken@2713
   108
/*
slouken@2713
   109
 * Opens a Haptic device.
slouken@2713
   110
 */
slouken@2713
   111
SDL_Haptic *
slouken@2713
   112
SDL_HapticOpen(int device_index)
slouken@2713
   113
{
slouken@2713
   114
    SDL_Haptic *haptic;
urkle@8170
   115
    SDL_Haptic *hapticlist;
slouken@2713
   116
urkle@8170
   117
    if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
slouken@2713
   118
        SDL_SetError("Haptic: There are %d haptic devices available",
urkle@8170
   119
                     SDL_NumHaptics());
slouken@2713
   120
        return NULL;
slouken@2713
   121
    }
slouken@2713
   122
urkle@8170
   123
    hapticlist = SDL_haptics;
urkle@8170
   124
    /* If the haptic is already open, return it
urkle@8170
   125
    * TODO: Should we create haptic instance IDs like the Joystick API?
urkle@8170
   126
    */
urkle@8170
   127
    while ( hapticlist )
urkle@8170
   128
    {
urkle@8170
   129
        if (device_index == hapticlist->index) {
urkle@8170
   130
            haptic = hapticlist;
slouken@2713
   131
            ++haptic->ref_count;
slouken@2713
   132
            return haptic;
slouken@2713
   133
        }
urkle@8170
   134
        hapticlist = hapticlist->next;
slouken@2713
   135
    }
slouken@2713
   136
slouken@2713
   137
    /* Create the haptic device */
slouken@2713
   138
    haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
slouken@2713
   139
    if (haptic == NULL) {
slouken@2713
   140
        SDL_OutOfMemory();
slouken@2713
   141
        return NULL;
slouken@2713
   142
    }
slouken@2713
   143
slouken@2713
   144
    /* Initialize the haptic device */
slouken@2713
   145
    SDL_memset(haptic, 0, (sizeof *haptic));
slouken@5360
   146
    haptic->rumble_id = -1;
slouken@2713
   147
    haptic->index = device_index;
slouken@2713
   148
    if (SDL_SYS_HapticOpen(haptic) < 0) {
slouken@2713
   149
        SDL_free(haptic);
slouken@2713
   150
        return NULL;
slouken@2713
   151
    }
slouken@2713
   152
slouken@7524
   153
    /* Add haptic to list */
slouken@7524
   154
    ++haptic->ref_count;
urkle@8170
   155
    /* Link the haptic in the list */
urkle@8170
   156
    haptic->next = SDL_haptics;
urkle@8170
   157
    SDL_haptics = haptic;
slouken@7524
   158
slouken@2713
   159
    /* Disable autocenter and set gain to max. */
slouken@2713
   160
    if (haptic->supported & SDL_HAPTIC_GAIN)
slouken@2713
   161
        SDL_HapticSetGain(haptic, 100);
slouken@2713
   162
    if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
slouken@2713
   163
        SDL_HapticSetAutocenter(haptic, 0);
slouken@2713
   164
slouken@2713
   165
    return haptic;
slouken@2713
   166
}
slouken@2713
   167
slouken@2713
   168
slouken@2713
   169
/*
slouken@2713
   170
 * Returns 1 if the device has been opened.
slouken@2713
   171
 */
slouken@2713
   172
int
slouken@2713
   173
SDL_HapticOpened(int device_index)
slouken@2713
   174
{
urkle@8170
   175
    int opened;
urkle@8170
   176
    SDL_Haptic *hapticlist;
slouken@2713
   177
slouken@7524
   178
    /* Make sure it's valid. */
urkle@8170
   179
    if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
slouken@7524
   180
        SDL_SetError("Haptic: There are %d haptic devices available",
urkle@8170
   181
                     SDL_NumHaptics());
philipp@7533
   182
        return 0;
slouken@7524
   183
    }
slouken@7524
   184
slouken@2713
   185
    opened = 0;
urkle@8170
   186
    hapticlist = SDL_haptics;
urkle@8170
   187
    /* TODO Should this use an instance ID? */
urkle@8170
   188
    while ( hapticlist )
urkle@8170
   189
    {
urkle@8170
   190
        if (hapticlist->index == (Uint8) device_index) {
slouken@2713
   191
            opened = 1;
slouken@2713
   192
            break;
slouken@2713
   193
        }
urkle@8170
   194
        hapticlist = hapticlist->next;
slouken@2713
   195
    }
slouken@2713
   196
    return opened;
slouken@2713
   197
}
slouken@2713
   198
slouken@2713
   199
slouken@2713
   200
/*
slouken@2713
   201
 * Returns the index to a haptic device.
slouken@2713
   202
 */
slouken@2713
   203
int
slouken@2713
   204
SDL_HapticIndex(SDL_Haptic * haptic)
slouken@2713
   205
{
slouken@2713
   206
    if (!ValidHaptic(haptic)) {
slouken@2713
   207
        return -1;
slouken@2713
   208
    }
slouken@2713
   209
slouken@2713
   210
    return haptic->index;
slouken@2713
   211
}
slouken@2713
   212
slouken@2713
   213
slouken@2713
   214
/*
slouken@2713
   215
 * Returns SDL_TRUE if mouse is haptic, SDL_FALSE if it isn't.
slouken@2713
   216
 */
slouken@2713
   217
int
slouken@2713
   218
SDL_MouseIsHaptic(void)
slouken@2713
   219
{
slouken@2713
   220
    if (SDL_SYS_HapticMouse() < 0)
slouken@2713
   221
        return SDL_FALSE;
slouken@2713
   222
    return SDL_TRUE;
slouken@2713
   223
}
slouken@2713
   224
slouken@2713
   225
slouken@2713
   226
/*
slouken@2713
   227
 * Returns the haptic device if mouse is haptic or NULL elsewise.
slouken@2713
   228
 */
slouken@2713
   229
SDL_Haptic *
slouken@2713
   230
SDL_HapticOpenFromMouse(void)
slouken@2713
   231
{
slouken@2713
   232
    int device_index;
slouken@2713
   233
slouken@2713
   234
    device_index = SDL_SYS_HapticMouse();
slouken@2713
   235
slouken@2713
   236
    if (device_index < 0) {
slouken@2713
   237
        SDL_SetError("Haptic: Mouse isn't a haptic device.");
slouken@2713
   238
        return NULL;
slouken@2713
   239
    }
slouken@2713
   240
slouken@2713
   241
    return SDL_HapticOpen(device_index);
slouken@2713
   242
}
slouken@2713
   243
slouken@2713
   244
slouken@2713
   245
/*
slouken@2713
   246
 * Returns SDL_TRUE if joystick has haptic features.
slouken@2713
   247
 */
slouken@2713
   248
int
slouken@2713
   249
SDL_JoystickIsHaptic(SDL_Joystick * joystick)
slouken@2713
   250
{
slouken@2713
   251
    int ret;
slouken@2713
   252
slouken@2713
   253
    /* Must be a valid joystick */
slouken@6690
   254
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@2713
   255
        return -1;
slouken@2713
   256
    }
slouken@2713
   257
slouken@2713
   258
    ret = SDL_SYS_JoystickIsHaptic(joystick);
slouken@2713
   259
slouken@2713
   260
    if (ret > 0)
slouken@2713
   261
        return SDL_TRUE;
slouken@2713
   262
    else if (ret == 0)
slouken@2713
   263
        return SDL_FALSE;
slouken@2713
   264
    else
slouken@2713
   265
        return -1;
slouken@2713
   266
}
slouken@2713
   267
slouken@2713
   268
slouken@2713
   269
/*
slouken@2713
   270
 * Opens a haptic device from a joystick.
slouken@2713
   271
 */
slouken@2713
   272
SDL_Haptic *
slouken@2713
   273
SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
slouken@2713
   274
{
slouken@2713
   275
    SDL_Haptic *haptic;
urkle@8170
   276
    SDL_Haptic *hapticlist;
slouken@2713
   277
slouken@7524
   278
    /* Make sure there is room. */
urkle@8170
   279
    if (SDL_NumHaptics() <= 0) {
slouken@7524
   280
        SDL_SetError("Haptic: There are %d haptic devices available",
urkle@8170
   281
                     SDL_NumHaptics());
slouken@7524
   282
        return NULL;
slouken@7524
   283
    }
slouken@7524
   284
slouken@2713
   285
    /* Must be a valid joystick */
slouken@6690
   286
    if (!SDL_PrivateJoystickValid(joystick)) {
bobbens@3232
   287
        SDL_SetError("Haptic: Joystick isn't valid.");
slouken@2713
   288
        return NULL;
slouken@2713
   289
    }
slouken@2713
   290
slouken@2713
   291
    /* Joystick must be haptic */
slouken@2713
   292
    if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
bobbens@3232
   293
        SDL_SetError("Haptic: Joystick isn't a haptic device.");
slouken@2713
   294
        return NULL;
slouken@2713
   295
    }
slouken@2713
   296
urkle@8170
   297
    hapticlist = SDL_haptics;
slouken@2713
   298
    /* Check to see if joystick's haptic is already open */
urkle@8170
   299
    while ( hapticlist )
urkle@8170
   300
    {
urkle@8170
   301
        if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
urkle@8170
   302
            haptic = hapticlist;
slouken@2713
   303
            ++haptic->ref_count;
slouken@2713
   304
            return haptic;
slouken@2713
   305
        }
urkle@8170
   306
        hapticlist = hapticlist->next;
slouken@2713
   307
    }
slouken@2713
   308
slouken@2713
   309
    /* Create the haptic device */
slouken@2713
   310
    haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
slouken@2713
   311
    if (haptic == NULL) {
slouken@2713
   312
        SDL_OutOfMemory();
slouken@2713
   313
        return NULL;
slouken@2713
   314
    }
slouken@2713
   315
slouken@2713
   316
    /* Initialize the haptic device */
slouken@2713
   317
    SDL_memset(haptic, 0, sizeof(SDL_Haptic));
slouken@5360
   318
    haptic->rumble_id = -1;
slouken@2713
   319
    if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
slouken@11238
   320
        SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
slouken@2713
   321
        SDL_free(haptic);
slouken@2713
   322
        return NULL;
slouken@2713
   323
    }
slouken@2713
   324
slouken@2713
   325
    /* Add haptic to list */
slouken@7524
   326
    ++haptic->ref_count;
urkle@8170
   327
    /* Link the haptic in the list */
urkle@8170
   328
    haptic->next = SDL_haptics;
urkle@8170
   329
    SDL_haptics = haptic;
slouken@2713
   330
slouken@2713
   331
    return haptic;
slouken@2713
   332
}
slouken@2713
   333
slouken@2713
   334
slouken@2713
   335
/*
slouken@2713
   336
 * Closes a SDL_Haptic device.
slouken@2713
   337
 */
slouken@2713
   338
void
slouken@2713
   339
SDL_HapticClose(SDL_Haptic * haptic)
slouken@2713
   340
{
slouken@2713
   341
    int i;
urkle@8170
   342
    SDL_Haptic *hapticlist;
urkle@8170
   343
    SDL_Haptic *hapticlistprev;
slouken@2713
   344
slouken@2713
   345
    /* Must be valid */
slouken@2713
   346
    if (!ValidHaptic(haptic)) {
slouken@2713
   347
        return;
slouken@2713
   348
    }
slouken@2713
   349
slouken@2713
   350
    /* Check if it's still in use */
slouken@8974
   351
    if (--haptic->ref_count > 0) {
slouken@2713
   352
        return;
slouken@2713
   353
    }
slouken@2713
   354
slouken@2713
   355
    /* Close it, properly removing effects if needed */
slouken@2713
   356
    for (i = 0; i < haptic->neffects; i++) {
slouken@2713
   357
        if (haptic->effects[i].hweffect != NULL) {
slouken@2713
   358
            SDL_HapticDestroyEffect(haptic, i);
slouken@2713
   359
        }
slouken@2713
   360
    }
slouken@2713
   361
    SDL_SYS_HapticClose(haptic);
slouken@2713
   362
slouken@2713
   363
    /* Remove from the list */
urkle@8170
   364
    hapticlist = SDL_haptics;
urkle@8170
   365
    hapticlistprev = NULL;
urkle@8170
   366
    while ( hapticlist )
urkle@8170
   367
    {
urkle@8170
   368
        if (haptic == hapticlist)
urkle@8170
   369
        {
urkle@8170
   370
            if ( hapticlistprev )
urkle@8170
   371
            {
urkle@8170
   372
                /* unlink this entry */
urkle@8170
   373
                hapticlistprev->next = hapticlist->next;
urkle@8170
   374
            }
urkle@8170
   375
            else
urkle@8170
   376
            {
urkle@8170
   377
                SDL_haptics = haptic->next;
urkle@8170
   378
            }
urkle@8170
   379
slouken@2713
   380
            break;
slouken@2713
   381
        }
urkle@8170
   382
        hapticlistprev = hapticlist;
urkle@8170
   383
        hapticlist = hapticlist->next;
slouken@2713
   384
    }
slouken@2713
   385
slouken@2713
   386
    /* Free */
slouken@2713
   387
    SDL_free(haptic);
slouken@2713
   388
}
slouken@2713
   389
slouken@2713
   390
/*
slouken@2713
   391
 * Cleans up after the subsystem.
slouken@2713
   392
 */
slouken@2713
   393
void
slouken@2713
   394
SDL_HapticQuit(void)
slouken@2713
   395
{
slouken@12304
   396
    while (SDL_haptics) {
slouken@12304
   397
        SDL_HapticClose(SDL_haptics);
slouken@12304
   398
    }
slouken@12304
   399
slouken@2713
   400
    SDL_SYS_HapticQuit();
slouken@2713
   401
}
slouken@2713
   402
slouken@2713
   403
/*
slouken@2713
   404
 * Returns the number of effects a haptic device has.
slouken@2713
   405
 */
slouken@2713
   406
int
slouken@2713
   407
SDL_HapticNumEffects(SDL_Haptic * haptic)
slouken@2713
   408
{
slouken@2713
   409
    if (!ValidHaptic(haptic)) {
slouken@2713
   410
        return -1;
slouken@2713
   411
    }
slouken@2713
   412
slouken@2713
   413
    return haptic->neffects;
slouken@2713
   414
}
slouken@2713
   415
slouken@2713
   416
slouken@2713
   417
/*
slouken@2713
   418
 * Returns the number of effects a haptic device can play.
slouken@2713
   419
 */
slouken@2713
   420
int
slouken@2713
   421
SDL_HapticNumEffectsPlaying(SDL_Haptic * haptic)
slouken@2713
   422
{
slouken@2713
   423
    if (!ValidHaptic(haptic)) {
slouken@2713
   424
        return -1;
slouken@2713
   425
    }
slouken@2713
   426
slouken@2713
   427
    return haptic->nplaying;
slouken@2713
   428
}
slouken@2713
   429
slouken@2713
   430
slouken@2713
   431
/*
slouken@2713
   432
 * Returns supported effects by the device.
slouken@2713
   433
 */
slouken@2713
   434
unsigned int
slouken@2713
   435
SDL_HapticQuery(SDL_Haptic * haptic)
slouken@2713
   436
{
slouken@2713
   437
    if (!ValidHaptic(haptic)) {
philipp@7131
   438
        return 0; /* same as if no effects were supported */
slouken@2713
   439
    }
slouken@2713
   440
slouken@2713
   441
    return haptic->supported;
slouken@2713
   442
}
slouken@2713
   443
slouken@2713
   444
slouken@2713
   445
/*
slouken@2713
   446
 * Returns the number of axis on the device.
slouken@2713
   447
 */
slouken@2713
   448
int
slouken@2713
   449
SDL_HapticNumAxes(SDL_Haptic * haptic)
slouken@2713
   450
{
slouken@2713
   451
    if (!ValidHaptic(haptic)) {
slouken@2713
   452
        return -1;
slouken@2713
   453
    }
slouken@2713
   454
slouken@2713
   455
    return haptic->naxes;
slouken@2713
   456
}
slouken@2713
   457
slouken@2713
   458
/*
slouken@2713
   459
 * Checks to see if the device can support the effect.
slouken@2713
   460
 */
slouken@2713
   461
int
slouken@2713
   462
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
slouken@2713
   463
{
slouken@2713
   464
    if (!ValidHaptic(haptic)) {
slouken@2713
   465
        return -1;
slouken@2713
   466
    }
slouken@2713
   467
slouken@2713
   468
    if ((haptic->supported & effect->type) != 0)
slouken@2713
   469
        return SDL_TRUE;
slouken@2713
   470
    return SDL_FALSE;
slouken@2713
   471
}
slouken@2713
   472
slouken@2713
   473
/*
slouken@2713
   474
 * Creates a new haptic effect.
slouken@2713
   475
 */
slouken@2713
   476
int
slouken@2713
   477
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
slouken@2713
   478
{
slouken@2713
   479
    int i;
slouken@2713
   480
slouken@2713
   481
    /* Check for device validity. */
slouken@2713
   482
    if (!ValidHaptic(haptic)) {
slouken@2713
   483
        return -1;
slouken@2713
   484
    }
slouken@2713
   485
slouken@2713
   486
    /* Check to see if effect is supported */
slouken@2713
   487
    if (SDL_HapticEffectSupported(haptic, effect) == SDL_FALSE) {
icculus@7037
   488
        return SDL_SetError("Haptic: Effect not supported by haptic device.");
slouken@2713
   489
    }
slouken@2713
   490
slouken@2713
   491
    /* See if there's a free slot */
slouken@2713
   492
    for (i = 0; i < haptic->neffects; i++) {
slouken@2713
   493
        if (haptic->effects[i].hweffect == NULL) {
slouken@2713
   494
slouken@2713
   495
            /* Now let the backend create the real effect */
slouken@2713
   496
            if (SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect)
slouken@2713
   497
                != 0) {
slouken@2713
   498
                return -1;      /* Backend failed to create effect */
slouken@2713
   499
            }
slouken@2713
   500
slouken@2713
   501
            SDL_memcpy(&haptic->effects[i].effect, effect,
slouken@2713
   502
                       sizeof(SDL_HapticEffect));
slouken@2713
   503
            return i;
slouken@2713
   504
        }
slouken@2713
   505
    }
slouken@2713
   506
icculus@7037
   507
    return SDL_SetError("Haptic: Device has no free space left.");
slouken@2713
   508
}
slouken@2713
   509
slouken@2713
   510
/*
slouken@2713
   511
 * Checks to see if an effect is valid.
slouken@2713
   512
 */
slouken@2713
   513
static int
slouken@2713
   514
ValidEffect(SDL_Haptic * haptic, int effect)
slouken@2713
   515
{
slouken@2713
   516
    if ((effect < 0) || (effect >= haptic->neffects)) {
slouken@2713
   517
        SDL_SetError("Haptic: Invalid effect identifier.");
slouken@2713
   518
        return 0;
slouken@2713
   519
    }
slouken@2713
   520
    return 1;
slouken@2713
   521
}
slouken@2713
   522
slouken@2713
   523
/*
slouken@2713
   524
 * Updates an effect.
slouken@2713
   525
 */
slouken@2713
   526
int
slouken@2713
   527
SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect,
slouken@2713
   528
                       SDL_HapticEffect * data)
slouken@2713
   529
{
slouken@2713
   530
    if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
slouken@2713
   531
        return -1;
slouken@2713
   532
    }
slouken@2713
   533
slouken@2713
   534
    /* Can't change type dynamically. */
slouken@2713
   535
    if (data->type != haptic->effects[effect].effect.type) {
icculus@7037
   536
        return SDL_SetError("Haptic: Updating effect type is illegal.");
slouken@2713
   537
    }
slouken@2713
   538
slouken@2713
   539
    /* Updates the effect */
slouken@2713
   540
    if (SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data) <
slouken@2713
   541
        0) {
slouken@2713
   542
        return -1;
slouken@2713
   543
    }
slouken@2713
   544
slouken@2713
   545
    SDL_memcpy(&haptic->effects[effect].effect, data,
slouken@2713
   546
               sizeof(SDL_HapticEffect));
icculus@7536
   547
    return 0;
slouken@2713
   548
}
slouken@2713
   549
slouken@2713
   550
slouken@2713
   551
/*
slouken@2713
   552
 * Runs the haptic effect on the device.
slouken@2713
   553
 */
slouken@2713
   554
int
slouken@2713
   555
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect, Uint32 iterations)
slouken@2713
   556
{
slouken@2713
   557
    if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
slouken@2713
   558
        return -1;
slouken@2713
   559
    }
slouken@2713
   560
slouken@2713
   561
    /* Run the effect */
slouken@2713
   562
    if (SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations)
slouken@2713
   563
        < 0) {
slouken@2713
   564
        return -1;
slouken@2713
   565
    }
slouken@2713
   566
slouken@2713
   567
    return 0;
slouken@2713
   568
}
slouken@2713
   569
slouken@2713
   570
/*
slouken@2713
   571
 * Stops the haptic effect on the device.
slouken@2713
   572
 */
slouken@2713
   573
int
slouken@2713
   574
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
slouken@2713
   575
{
slouken@2713
   576
    if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
slouken@2713
   577
        return -1;
slouken@2713
   578
    }
slouken@2713
   579
slouken@2713
   580
    /* Stop the effect */
slouken@2713
   581
    if (SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect]) < 0) {
slouken@2713
   582
        return -1;
slouken@2713
   583
    }
slouken@2713
   584
slouken@2713
   585
    return 0;
slouken@2713
   586
}
slouken@2713
   587
slouken@2713
   588
/*
slouken@2713
   589
 * Gets rid of a haptic effect.
slouken@2713
   590
 */
slouken@2713
   591
void
slouken@2713
   592
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
slouken@2713
   593
{
slouken@2713
   594
    if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
slouken@2713
   595
        return;
slouken@2713
   596
    }
slouken@2713
   597
slouken@2713
   598
    /* Not allocated */
slouken@2713
   599
    if (haptic->effects[effect].hweffect == NULL) {
slouken@2713
   600
        return;
slouken@2713
   601
    }
slouken@2713
   602
slouken@2713
   603
    SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
slouken@2713
   604
}
slouken@2713
   605
slouken@2713
   606
/*
slouken@2713
   607
 * Gets the status of a haptic effect.
slouken@2713
   608
 */
slouken@2713
   609
int
slouken@2713
   610
SDL_HapticGetEffectStatus(SDL_Haptic * haptic, int effect)
slouken@2713
   611
{
slouken@2713
   612
    if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
slouken@2713
   613
        return -1;
slouken@2713
   614
    }
slouken@2713
   615
slouken@2713
   616
    if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
icculus@7037
   617
        return SDL_SetError("Haptic: Device does not support status queries.");
slouken@2713
   618
    }
slouken@2713
   619
slouken@2713
   620
    return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
slouken@2713
   621
}
slouken@2713
   622
slouken@2713
   623
/*
slouken@2713
   624
 * Sets the global gain of the device.
slouken@2713
   625
 */
slouken@2713
   626
int
slouken@2713
   627
SDL_HapticSetGain(SDL_Haptic * haptic, int gain)
slouken@2713
   628
{
slouken@2713
   629
    const char *env;
slouken@2713
   630
    int real_gain, max_gain;
slouken@2713
   631
slouken@2713
   632
    if (!ValidHaptic(haptic)) {
slouken@2713
   633
        return -1;
slouken@2713
   634
    }
slouken@2713
   635
slouken@2713
   636
    if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
icculus@7037
   637
        return SDL_SetError("Haptic: Device does not support setting gain.");
slouken@2713
   638
    }
slouken@2713
   639
slouken@2713
   640
    if ((gain < 0) || (gain > 100)) {
icculus@7037
   641
        return SDL_SetError("Haptic: Gain must be between 0 and 100.");
slouken@2713
   642
    }
slouken@2713
   643
slouken@2713
   644
    /* We use the envvar to get the maximum gain. */
slouken@2713
   645
    env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
slouken@2713
   646
    if (env != NULL) {
slouken@2713
   647
        max_gain = SDL_atoi(env);
slouken@2713
   648
slouken@2713
   649
        /* Check for sanity. */
slouken@2713
   650
        if (max_gain < 0)
slouken@2713
   651
            max_gain = 0;
slouken@2713
   652
        else if (max_gain > 100)
slouken@2713
   653
            max_gain = 100;
slouken@2713
   654
slouken@2713
   655
        /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
slouken@2713
   656
        real_gain = (gain * max_gain) / 100;
slouken@2713
   657
    } else {
slouken@2713
   658
        real_gain = gain;
slouken@2713
   659
    }
slouken@2713
   660
slouken@2713
   661
    if (SDL_SYS_HapticSetGain(haptic, real_gain) < 0) {
slouken@2713
   662
        return -1;
slouken@2713
   663
    }
slouken@2713
   664
slouken@2713
   665
    return 0;
slouken@2713
   666
}
slouken@2713
   667
slouken@2713
   668
/*
slouken@2713
   669
 * Makes the device autocenter, 0 disables.
slouken@2713
   670
 */
slouken@2713
   671
int
slouken@2713
   672
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
slouken@2713
   673
{
slouken@2713
   674
    if (!ValidHaptic(haptic)) {
slouken@2713
   675
        return -1;
slouken@2713
   676
    }
slouken@2713
   677
slouken@2713
   678
    if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
icculus@7037
   679
        return SDL_SetError("Haptic: Device does not support setting autocenter.");
slouken@2713
   680
    }
slouken@2713
   681
slouken@2713
   682
    if ((autocenter < 0) || (autocenter > 100)) {
icculus@7037
   683
        return SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
slouken@2713
   684
    }
slouken@2713
   685
slouken@2713
   686
    if (SDL_SYS_HapticSetAutocenter(haptic, autocenter) < 0) {
slouken@2713
   687
        return -1;
slouken@2713
   688
    }
slouken@2713
   689
slouken@2713
   690
    return 0;
slouken@2713
   691
}
slouken@2713
   692
slouken@2713
   693
/*
slouken@2713
   694
 * Pauses the haptic device.
slouken@2713
   695
 */
slouken@2713
   696
int
slouken@2713
   697
SDL_HapticPause(SDL_Haptic * haptic)
slouken@2713
   698
{
slouken@2713
   699
    if (!ValidHaptic(haptic)) {
slouken@2713
   700
        return -1;
slouken@2713
   701
    }
slouken@2713
   702
slouken@2713
   703
    if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
icculus@7037
   704
        return SDL_SetError("Haptic: Device does not support setting pausing.");
slouken@2713
   705
    }
slouken@2713
   706
slouken@2713
   707
    return SDL_SYS_HapticPause(haptic);
slouken@2713
   708
}
slouken@2713
   709
slouken@2713
   710
/*
slouken@2713
   711
 * Unpauses the haptic device.
slouken@2713
   712
 */
slouken@2713
   713
int
slouken@2713
   714
SDL_HapticUnpause(SDL_Haptic * haptic)
slouken@2713
   715
{
slouken@2713
   716
    if (!ValidHaptic(haptic)) {
slouken@2713
   717
        return -1;
slouken@2713
   718
    }
slouken@2713
   719
slouken@2713
   720
    if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
slouken@2713
   721
        return 0;               /* Not going to be paused, so we pretend it's unpaused. */
slouken@2713
   722
    }
slouken@2713
   723
slouken@2713
   724
    return SDL_SYS_HapticUnpause(haptic);
slouken@2713
   725
}
slouken@2713
   726
slouken@2713
   727
/*
slouken@2713
   728
 * Stops all the currently playing effects.
slouken@2713
   729
 */
slouken@2713
   730
int
slouken@2713
   731
SDL_HapticStopAll(SDL_Haptic * haptic)
slouken@2713
   732
{
slouken@2713
   733
    if (!ValidHaptic(haptic)) {
slouken@2713
   734
        return -1;
slouken@2713
   735
    }
slouken@2713
   736
slouken@2713
   737
    return SDL_SYS_HapticStopAll(haptic);
slouken@2713
   738
}
slouken@5360
   739
slouken@5360
   740
/*
slouken@5360
   741
 * Checks to see if rumble is supported.
slouken@5360
   742
 */
slouken@5360
   743
int
slouken@5360
   744
SDL_HapticRumbleSupported(SDL_Haptic * haptic)
slouken@5360
   745
{
slouken@5360
   746
    if (!ValidHaptic(haptic)) {
slouken@5360
   747
        return -1;
slouken@5360
   748
    }
slouken@5360
   749
icculus@7621
   750
    /* Most things can use SINE, but XInput only has LEFTRIGHT. */
icculus@7621
   751
    return ((haptic->supported & (SDL_HAPTIC_SINE|SDL_HAPTIC_LEFTRIGHT)) != 0);
slouken@5360
   752
}
slouken@5360
   753
slouken@5360
   754
/*
slouken@5360
   755
 * Initializes the haptic device for simple rumble playback.
slouken@5360
   756
 */
slouken@5360
   757
int
slouken@5360
   758
SDL_HapticRumbleInit(SDL_Haptic * haptic)
slouken@5360
   759
{
icculus@7621
   760
    SDL_HapticEffect *efx = &haptic->rumble_effect;
icculus@7621
   761
slouken@5360
   762
    if (!ValidHaptic(haptic)) {
slouken@5360
   763
        return -1;
slouken@5360
   764
    }
slouken@5360
   765
slouken@5360
   766
    /* Already allocated. */
slouken@5360
   767
    if (haptic->rumble_id >= 0) {
slouken@5360
   768
        return 0;
slouken@5360
   769
    }
slouken@5360
   770
icculus@7621
   771
    SDL_zerop(efx);
icculus@7621
   772
    if (haptic->supported & SDL_HAPTIC_SINE) {
icculus@7621
   773
        efx->type = SDL_HAPTIC_SINE;
slouken@12060
   774
        efx->periodic.direction.type = SDL_HAPTIC_CARTESIAN;
icculus@7621
   775
        efx->periodic.period = 1000;
icculus@7621
   776
        efx->periodic.magnitude = 0x4000;
icculus@7621
   777
        efx->periodic.length = 5000;
icculus@7621
   778
        efx->periodic.attack_length = 0;
icculus@7621
   779
        efx->periodic.fade_length = 0;
icculus@7621
   780
    } else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) {  /* XInput? */
icculus@7621
   781
        efx->type = SDL_HAPTIC_LEFTRIGHT;
icculus@7621
   782
        efx->leftright.length = 5000;
icculus@7621
   783
        efx->leftright.large_magnitude = 0x4000;
icculus@7621
   784
        efx->leftright.small_magnitude = 0x4000;
icculus@7621
   785
    } else {
icculus@7621
   786
        return SDL_SetError("Device doesn't support rumble");
icculus@7621
   787
    }
icculus@7621
   788
slouken@5360
   789
    haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
slouken@5360
   790
    if (haptic->rumble_id >= 0) {
slouken@5360
   791
        return 0;
slouken@5360
   792
    }
slouken@5360
   793
    return -1;
slouken@5360
   794
}
slouken@5360
   795
slouken@5360
   796
/*
slouken@5360
   797
 * Runs simple rumble on a haptic device
slouken@5360
   798
 */
slouken@5360
   799
int
slouken@5360
   800
SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
slouken@5360
   801
{
icculus@7621
   802
    SDL_HapticEffect *efx;
icculus@7621
   803
    Sint16 magnitude;
slouken@5360
   804
slouken@5360
   805
    if (!ValidHaptic(haptic)) {
slouken@5360
   806
        return -1;
slouken@5360
   807
    }
slouken@5360
   808
slouken@5360
   809
    if (haptic->rumble_id < 0) {
icculus@7037
   810
        return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
slouken@5360
   811
    }
slouken@5360
   812
slouken@5360
   813
    /* Clamp strength. */
slouken@5360
   814
    if (strength > 1.0f) {
slouken@5360
   815
        strength = 1.0f;
icculus@7621
   816
    } else if (strength < 0.0f) {
slouken@5360
   817
        strength = 0.0f;
slouken@5360
   818
    }
icculus@7621
   819
    magnitude = (Sint16)(32767.0f*strength);
slouken@5360
   820
icculus@7621
   821
    efx = &haptic->rumble_effect;
icculus@7621
   822
    if (efx->type == SDL_HAPTIC_SINE) {
icculus@7621
   823
        efx->periodic.magnitude = magnitude;
icculus@7621
   824
        efx->periodic.length = length;
icculus@7621
   825
    } else if (efx->type == SDL_HAPTIC_LEFTRIGHT) {
icculus@7621
   826
        efx->leftright.small_magnitude = efx->leftright.large_magnitude = magnitude;
icculus@7621
   827
        efx->leftright.length = length;
icculus@7621
   828
    } else {
icculus@7621
   829
        SDL_assert(0 && "This should have been caught elsewhere");
icculus@7621
   830
    }
icculus@7621
   831
philipp@7531
   832
    if (SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect) < 0) {
philipp@7531
   833
        return -1;
slouken@7524
   834
    }
slouken@5360
   835
slouken@5360
   836
    return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
slouken@5360
   837
}
slouken@5360
   838
slouken@5360
   839
/*
slouken@5360
   840
 * Stops the simple rumble on a haptic device.
slouken@5360
   841
 */
slouken@5360
   842
int
slouken@5360
   843
SDL_HapticRumbleStop(SDL_Haptic * haptic)
slouken@5360
   844
{
slouken@5360
   845
    if (!ValidHaptic(haptic)) {
slouken@5360
   846
        return -1;
slouken@5360
   847
    }
slouken@5360
   848
slouken@5360
   849
    if (haptic->rumble_id < 0) {
icculus@7037
   850
        return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
slouken@5360
   851
    }
slouken@5360
   852
slouken@5360
   853
    return SDL_HapticStopEffect(haptic, haptic->rumble_id);
slouken@5360
   854
}
slouken@5360
   855
slouken@9740
   856
/* vi: set ts=4 sw=4 expandtab: */