include/SDL_joystick.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 10 Oct 2017 11:10:15 -0700
changeset 11581 49393a9dffe4
parent 10934 380eae4f5059
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Exposed the joystick locking functions for multi-threaded access to the joystick API
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@0
     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@0
     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@0
    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@0
    20
*/
slouken@0
    21
slouken@1895
    22
/**
slouken@3407
    23
 *  \file SDL_joystick.h
slouken@7191
    24
 *
slouken@3407
    25
 *  Include file for SDL joystick event handling
slouken@6690
    26
 *
philipp@10213
    27
 * The term "device_index" identifies currently plugged in joystick devices between 0 and SDL_NumJoysticks(), with the exact joystick
slouken@6690
    28
 *   behind a device_index changing as joysticks are plugged and unplugged.
slouken@6690
    29
 *
slouken@6690
    30
 * The term "instance_id" is the current instantiation of a joystick device in the system, if the joystick is removed and then re-inserted
slouken@6690
    31
 *   then it will get a new instance_id, instance_id's are monotonically increasing identifiers of a joystick plugged in.
slouken@6690
    32
 *
slouken@7191
    33
 * The term JoystickGUID is a stable 128-bit identifier for a joystick device that does not change over time, it identifies class of
slouken@6690
    34
 *   the device (a X360 wired controller for example). This identifier is platform dependent.
slouken@6690
    35
 *
slouken@6690
    36
 *
slouken@1895
    37
 */
slouken@0
    38
slouken@10638
    39
#ifndef SDL_joystick_h_
slouken@10638
    40
#define SDL_joystick_h_
slouken@0
    41
slouken@1356
    42
#include "SDL_stdinc.h"
slouken@1358
    43
#include "SDL_error.h"
slouken@0
    44
slouken@0
    45
#include "begin_code.h"
slouken@0
    46
/* Set up for C function definitions, even when using C++ */
slouken@0
    47
#ifdef __cplusplus
slouken@0
    48
extern "C" {
slouken@0
    49
#endif
slouken@0
    50
slouken@3407
    51
/**
slouken@3407
    52
 *  \file SDL_joystick.h
slouken@3407
    53
 *
slouken@3407
    54
 *  In order to use these functions, SDL_Init() must have been called
slouken@3407
    55
 *  with the ::SDL_INIT_JOYSTICK flag.  This causes SDL to scan the system
slouken@3407
    56
 *  for joysticks, and load appropriate drivers.
slouken@7341
    57
 *
slouken@7341
    58
 *  If you would like to receive joystick updates while the application
slouken@7341
    59
 *  is in the background, you should set the following hint before calling
slouken@7341
    60
 *  SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
slouken@3407
    61
 */
slouken@0
    62
slouken@10636
    63
/**
slouken@10636
    64
 * The joystick structure used to identify an SDL joystick
slouken@10636
    65
 */
slouken@0
    66
struct _SDL_Joystick;
slouken@0
    67
typedef struct _SDL_Joystick SDL_Joystick;
slouken@0
    68
slouken@6707
    69
/* A structure that encodes the stable unique id for a joystick device */
slouken@6707
    70
typedef struct {
slouken@7191
    71
    Uint8 data[16];
slouken@6738
    72
} SDL_JoystickGUID;
slouken@6707
    73
slouken@10934
    74
/**
slouken@10934
    75
 * This is a unique ID for a joystick for the time it is connected to the system,
slouken@10934
    76
 * and is never reused for the lifetime of the application. If the joystick is
slouken@10934
    77
 * disconnected and reconnected, it will get a new ID.
slouken@10934
    78
 *
slouken@10934
    79
 * The ID value starts at 0 and increments from there. The value -1 is an invalid ID.
slouken@10934
    80
 */
slouken@6974
    81
typedef Sint32 SDL_JoystickID;
slouken@6707
    82
slouken@9884
    83
typedef enum
slouken@9884
    84
{
slouken@10855
    85
    SDL_JOYSTICK_TYPE_UNKNOWN,
slouken@10855
    86
    SDL_JOYSTICK_TYPE_GAMECONTROLLER,
slouken@10855
    87
    SDL_JOYSTICK_TYPE_WHEEL,
slouken@10855
    88
    SDL_JOYSTICK_TYPE_ARCADE_STICK,
slouken@10855
    89
    SDL_JOYSTICK_TYPE_FLIGHT_STICK,
slouken@10855
    90
    SDL_JOYSTICK_TYPE_DANCE_PAD,
slouken@10855
    91
    SDL_JOYSTICK_TYPE_GUITAR,
slouken@10855
    92
    SDL_JOYSTICK_TYPE_DRUM_KIT,
slouken@10868
    93
    SDL_JOYSTICK_TYPE_ARCADE_PAD,
slouken@10870
    94
    SDL_JOYSTICK_TYPE_THROTTLE
slouken@10855
    95
} SDL_JoystickType;
slouken@10855
    96
slouken@10855
    97
typedef enum
slouken@10855
    98
{
slouken@9884
    99
    SDL_JOYSTICK_POWER_UNKNOWN = -1,
slouken@9884
   100
    SDL_JOYSTICK_POWER_EMPTY,
slouken@9884
   101
    SDL_JOYSTICK_POWER_LOW,
slouken@9884
   102
    SDL_JOYSTICK_POWER_MEDIUM,
slouken@9884
   103
    SDL_JOYSTICK_POWER_FULL,
slouken@9884
   104
    SDL_JOYSTICK_POWER_WIRED,
slouken@9884
   105
    SDL_JOYSTICK_POWER_MAX
slouken@9884
   106
} SDL_JoystickPowerLevel;
slouken@6707
   107
slouken@0
   108
/* Function prototypes */
slouken@11581
   109
slouken@11581
   110
/**
slouken@11581
   111
 * Locking for multi-threaded access to the joystick API
slouken@11581
   112
 *
slouken@11581
   113
 * If you are using the joystick API or handling events from multiple threads
slouken@11581
   114
 * you should use these locking functions to protect access to the joysticks.
slouken@11581
   115
 *
slouken@11581
   116
 * In particular, you are guaranteed that the joystick list won't change, so
slouken@11581
   117
 * the API functions that take a joystick index will be valid, and joystick
slouken@11581
   118
 * and game controller events will not be delivered.
slouken@11581
   119
 */
slouken@11581
   120
extern DECLSPEC void SDLCALL SDL_LockJoysticks(void);
slouken@11581
   121
extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void);
slouken@11581
   122
slouken@3407
   123
/**
slouken@6690
   124
 *  Count the number of joysticks attached to the system right now
slouken@0
   125
 */
slouken@337
   126
extern DECLSPEC int SDLCALL SDL_NumJoysticks(void);
slouken@0
   127
slouken@3407
   128
/**
slouken@3407
   129
 *  Get the implementation dependent name of a joystick.
slouken@3407
   130
 *  This can be called before any joysticks are opened.
slouken@3407
   131
 *  If no name can be found, this function returns NULL.
slouken@0
   132
 */
slouken@6690
   133
extern DECLSPEC const char *SDLCALL SDL_JoystickNameForIndex(int device_index);
slouken@0
   134
slouken@3407
   135
/**
slouken@10595
   136
 *  Return the GUID for the joystick at this index
slouken@10595
   137
 *  This can be called before any joysticks are opened.
slouken@10595
   138
 */
slouken@10595
   139
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetDeviceGUID(int device_index);
slouken@10595
   140
slouken@10595
   141
/**
slouken@10595
   142
 *  Get the USB vendor ID of a joystick, if available.
slouken@10595
   143
 *  This can be called before any joysticks are opened.
slouken@10595
   144
 *  If the vendor ID isn't available this function returns 0.
slouken@10595
   145
 */
slouken@10595
   146
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceVendor(int device_index);
slouken@10595
   147
slouken@10595
   148
/**
slouken@10595
   149
 *  Get the USB product ID of a joystick, if available.
slouken@10595
   150
 *  This can be called before any joysticks are opened.
slouken@10595
   151
 *  If the product ID isn't available this function returns 0.
slouken@10595
   152
 */
slouken@10595
   153
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceProduct(int device_index);
slouken@10595
   154
slouken@10595
   155
/**
slouken@10595
   156
 *  Get the product version of a joystick, if available.
slouken@10595
   157
 *  This can be called before any joysticks are opened.
slouken@10595
   158
 *  If the product version isn't available this function returns 0.
slouken@10595
   159
 */
slouken@10595
   160
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceProductVersion(int device_index);
slouken@10595
   161
slouken@10595
   162
/**
slouken@10855
   163
 *  Get the type of a joystick, if available.
slouken@10855
   164
 *  This can be called before any joysticks are opened.
slouken@10855
   165
 */
slouken@10855
   166
extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetDeviceType(int device_index);
slouken@10855
   167
slouken@10855
   168
/**
slouken@10934
   169
 *  Get the instance ID of a joystick.
slouken@10934
   170
 *  This can be called before any joysticks are opened.
slouken@10934
   171
 *  If the index is out of range, this function will return -1.
slouken@10934
   172
 */
slouken@10934
   173
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickGetDeviceInstanceID(int device_index);
slouken@10934
   174
slouken@10934
   175
/**
slouken@7191
   176
 *  Open a joystick for use.
philipp@9227
   177
 *  The index passed as an argument refers to the N'th joystick on the system.
philipp@9569
   178
 *  This index is not the value which will identify this joystick in future
philipp@9569
   179
 *  joystick events.  The joystick's instance id (::SDL_JoystickID) will be used
philipp@9569
   180
 *  there instead.
slouken@7191
   181
 *
slouken@3407
   182
 *  \return A joystick identifier, or NULL if an error occurred.
slouken@0
   183
 */
slouken@1895
   184
extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index);
slouken@0
   185
slouken@3407
   186
/**
icculus@9916
   187
 * Return the SDL_Joystick associated with an instance id.
icculus@9916
   188
 */
icculus@9916
   189
extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickFromInstanceID(SDL_JoystickID joyid);
icculus@9916
   190
icculus@9916
   191
/**
slouken@6690
   192
 *  Return the name for this currently opened joystick.
slouken@6690
   193
 *  If no name can be found, this function returns NULL.
slouken@0
   194
 */
slouken@6690
   195
extern DECLSPEC const char *SDLCALL SDL_JoystickName(SDL_Joystick * joystick);
slouken@7191
   196
slouken@6690
   197
/**
slouken@6690
   198
 *  Return the GUID for this opened joystick
slouken@6690
   199
 */
slouken@6738
   200
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUID(SDL_Joystick * joystick);
slouken@6690
   201
slouken@6690
   202
/**
slouken@10595
   203
 *  Get the USB vendor ID of an opened joystick, if available.
slouken@10595
   204
 *  If the vendor ID isn't available this function returns 0.
slouken@10595
   205
 */
slouken@10595
   206
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetVendor(SDL_Joystick * joystick);
slouken@10595
   207
slouken@10595
   208
/**
slouken@10595
   209
 *  Get the USB product ID of an opened joystick, if available.
slouken@10595
   210
 *  If the product ID isn't available this function returns 0.
slouken@10595
   211
 */
slouken@10595
   212
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetProduct(SDL_Joystick * joystick);
slouken@10595
   213
slouken@10595
   214
/**
slouken@10595
   215
 *  Get the product version of an opened joystick, if available.
slouken@10595
   216
 *  If the product version isn't available this function returns 0.
slouken@10595
   217
 */
slouken@10595
   218
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetProductVersion(SDL_Joystick * joystick);
slouken@10595
   219
slouken@10595
   220
/**
slouken@10855
   221
 *  Get the type of an opened joystick.
slouken@10855
   222
 */
slouken@10855
   223
extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetType(SDL_Joystick * joystick);
slouken@10855
   224
slouken@10855
   225
/**
icculus@6746
   226
 *  Return a string representation for this guid. pszGUID must point to at least 33 bytes
icculus@6746
   227
 *  (32 for the string plus a NULL terminator).
slouken@6690
   228
 */
icculus@8028
   229
extern DECLSPEC void SDLCALL SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
slouken@6690
   230
slouken@6690
   231
/**
slouken@10595
   232
 *  Convert a string into a joystick guid
slouken@6690
   233
 */
slouken@6738
   234
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID);
slouken@6690
   235
slouken@6707
   236
/**
slouken@6707
   237
 *  Returns SDL_TRUE if the joystick has been opened and currently connected, or SDL_FALSE if it has not.
slouken@6707
   238
 */
slouken@6707
   239
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAttached(SDL_Joystick * joystick);
slouken@6690
   240
slouken@6690
   241
/**
slouken@6974
   242
 *  Get the instance ID of an opened joystick or -1 if the joystick is invalid.
slouken@0
   243
 */
slouken@6690
   244
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickInstanceID(SDL_Joystick * joystick);
slouken@0
   245
slouken@3407
   246
/**
slouken@3407
   247
 *  Get the number of general axis controls on a joystick.
slouken@0
   248
 */
slouken@1895
   249
extern DECLSPEC int SDLCALL SDL_JoystickNumAxes(SDL_Joystick * joystick);
slouken@0
   250
slouken@3407
   251
/**
slouken@3407
   252
 *  Get the number of trackballs on a joystick.
slouken@7191
   253
 *
slouken@3407
   254
 *  Joystick trackballs have only relative motion events associated
slouken@3407
   255
 *  with them and their state cannot be polled.
slouken@0
   256
 */
slouken@1895
   257
extern DECLSPEC int SDLCALL SDL_JoystickNumBalls(SDL_Joystick * joystick);
slouken@0
   258
slouken@3407
   259
/**
slouken@3407
   260
 *  Get the number of POV hats on a joystick.
slouken@0
   261
 */
slouken@1895
   262
extern DECLSPEC int SDLCALL SDL_JoystickNumHats(SDL_Joystick * joystick);
slouken@0
   263
slouken@3407
   264
/**
slouken@3407
   265
 *  Get the number of buttons on a joystick.
slouken@0
   266
 */
slouken@1895
   267
extern DECLSPEC int SDLCALL SDL_JoystickNumButtons(SDL_Joystick * joystick);
slouken@0
   268
slouken@3407
   269
/**
slouken@3407
   270
 *  Update the current state of the open joysticks.
slouken@7191
   271
 *
slouken@3407
   272
 *  This is called automatically by the event loop if any joystick
slouken@3407
   273
 *  events are enabled.
slouken@0
   274
 */
slouken@337
   275
extern DECLSPEC void SDLCALL SDL_JoystickUpdate(void);
slouken@0
   276
slouken@3407
   277
/**
slouken@3407
   278
 *  Enable/disable joystick event polling.
slouken@7191
   279
 *
slouken@3407
   280
 *  If joystick events are disabled, you must call SDL_JoystickUpdate()
slouken@3407
   281
 *  yourself and check the state of the joystick when you want joystick
slouken@3407
   282
 *  information.
slouken@7191
   283
 *
slouken@3407
   284
 *  The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
slouken@0
   285
 */
slouken@337
   286
extern DECLSPEC int SDLCALL SDL_JoystickEventState(int state);
slouken@0
   287
slouken@10724
   288
#define SDL_JOYSTICK_AXIS_MAX   32767
slouken@10724
   289
#define SDL_JOYSTICK_AXIS_MIN   -32768
slouken@3407
   290
/**
slouken@3407
   291
 *  Get the current state of an axis control on a joystick.
slouken@7191
   292
 *
slouken@3407
   293
 *  The state is a value ranging from -32768 to 32767.
slouken@7191
   294
 *
slouken@3407
   295
 *  The axis indices start at index 0.
slouken@0
   296
 */
slouken@1895
   297
extern DECLSPEC Sint16 SDLCALL SDL_JoystickGetAxis(SDL_Joystick * joystick,
slouken@1895
   298
                                                   int axis);
slouken@0
   299
slouken@3407
   300
/**
slouken@10752
   301
 *  Get the initial state of an axis control on a joystick.
slouken@10752
   302
 *
slouken@10752
   303
 *  The state is a value ranging from -32768 to 32767.
slouken@10752
   304
 *
slouken@10752
   305
 *  The axis indices start at index 0.
slouken@10752
   306
 *
slouken@10752
   307
 *  \return SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
slouken@10752
   308
 */
slouken@10752
   309
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick,
slouken@10752
   310
                                                   int axis, Sint16 *state);
slouken@10752
   311
slouken@10752
   312
/**
slouken@3407
   313
 *  \name Hat positions
slouken@0
   314
 */
gabomdq@7678
   315
/* @{ */
slouken@7191
   316
#define SDL_HAT_CENTERED    0x00
philipp@9806
   317
#define SDL_HAT_UP          0x01
slouken@7191
   318
#define SDL_HAT_RIGHT       0x02
slouken@7191
   319
#define SDL_HAT_DOWN        0x04
slouken@7191
   320
#define SDL_HAT_LEFT        0x08
slouken@7191
   321
#define SDL_HAT_RIGHTUP     (SDL_HAT_RIGHT|SDL_HAT_UP)
slouken@7191
   322
#define SDL_HAT_RIGHTDOWN   (SDL_HAT_RIGHT|SDL_HAT_DOWN)
slouken@7191
   323
#define SDL_HAT_LEFTUP      (SDL_HAT_LEFT|SDL_HAT_UP)
slouken@7191
   324
#define SDL_HAT_LEFTDOWN    (SDL_HAT_LEFT|SDL_HAT_DOWN)
gabomdq@7678
   325
/* @} */
slouken@3407
   326
slouken@3407
   327
/**
slouken@3407
   328
 *  Get the current state of a POV hat on a joystick.
slouken@3407
   329
 *
slouken@3407
   330
 *  The hat indices start at index 0.
slouken@7191
   331
 *
slouken@3407
   332
 *  \return The return value is one of the following positions:
slouken@3407
   333
 *           - ::SDL_HAT_CENTERED
slouken@3407
   334
 *           - ::SDL_HAT_UP
slouken@3407
   335
 *           - ::SDL_HAT_RIGHT
slouken@3407
   336
 *           - ::SDL_HAT_DOWN
slouken@3407
   337
 *           - ::SDL_HAT_LEFT
slouken@3407
   338
 *           - ::SDL_HAT_RIGHTUP
slouken@3407
   339
 *           - ::SDL_HAT_RIGHTDOWN
slouken@3407
   340
 *           - ::SDL_HAT_LEFTUP
slouken@3407
   341
 *           - ::SDL_HAT_LEFTDOWN
slouken@0
   342
 */
slouken@1895
   343
extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetHat(SDL_Joystick * joystick,
slouken@1895
   344
                                                 int hat);
slouken@0
   345
slouken@3407
   346
/**
slouken@3407
   347
 *  Get the ball axis change since the last poll.
slouken@7191
   348
 *
slouken@3407
   349
 *  \return 0, or -1 if you passed it invalid parameters.
slouken@7191
   350
 *
slouken@3407
   351
 *  The ball indices start at index 0.
slouken@0
   352
 */
slouken@1895
   353
extern DECLSPEC int SDLCALL SDL_JoystickGetBall(SDL_Joystick * joystick,
slouken@1895
   354
                                                int ball, int *dx, int *dy);
slouken@0
   355
slouken@3407
   356
/**
slouken@3407
   357
 *  Get the current state of a button on a joystick.
slouken@7191
   358
 *
slouken@3407
   359
 *  The button indices start at index 0.
slouken@0
   360
 */
slouken@1895
   361
extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick * joystick,
slouken@1895
   362
                                                    int button);
slouken@0
   363
slouken@3407
   364
/**
slouken@3407
   365
 *  Close a joystick previously opened with SDL_JoystickOpen().
slouken@0
   366
 */
slouken@1895
   367
extern DECLSPEC void SDLCALL SDL_JoystickClose(SDL_Joystick * joystick);
slouken@0
   368
slouken@9884
   369
/**
icculus@9917
   370
 *  Return the battery level of this joystick
icculus@9917
   371
 */
slouken@9884
   372
extern DECLSPEC SDL_JoystickPowerLevel SDLCALL SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick);
slouken@9884
   373
slouken@0
   374
/* Ends C function definitions when using C++ */
slouken@0
   375
#ifdef __cplusplus
slouken@0
   376
}
slouken@0
   377
#endif
slouken@0
   378
#include "close_code.h"
slouken@0
   379
slouken@10638
   380
#endif /* SDL_joystick_h_ */
slouken@1895
   381
slouken@1895
   382
/* vi: set ts=4 sw=4 expandtab: */