include/SDL_joystick.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Mar 2017 16:09:16 -0800
changeset 10934 380eae4f5059
parent 10870 b158d18e06d0
child 11581 49393a9dffe4
permissions -rw-r--r--
Added an API to get the joystick instance ID before opening the device: SDL_JoystickGetDeviceInstanceID()
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@3407
   109
/**
slouken@6690
   110
 *  Count the number of joysticks attached to the system right now
slouken@0
   111
 */
slouken@337
   112
extern DECLSPEC int SDLCALL SDL_NumJoysticks(void);
slouken@0
   113
slouken@3407
   114
/**
slouken@3407
   115
 *  Get the implementation dependent name of a joystick.
slouken@3407
   116
 *  This can be called before any joysticks are opened.
slouken@3407
   117
 *  If no name can be found, this function returns NULL.
slouken@0
   118
 */
slouken@6690
   119
extern DECLSPEC const char *SDLCALL SDL_JoystickNameForIndex(int device_index);
slouken@0
   120
slouken@3407
   121
/**
slouken@10595
   122
 *  Return the GUID for the joystick at this index
slouken@10595
   123
 *  This can be called before any joysticks are opened.
slouken@10595
   124
 */
slouken@10595
   125
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetDeviceGUID(int device_index);
slouken@10595
   126
slouken@10595
   127
/**
slouken@10595
   128
 *  Get the USB vendor ID of a joystick, if available.
slouken@10595
   129
 *  This can be called before any joysticks are opened.
slouken@10595
   130
 *  If the vendor ID isn't available this function returns 0.
slouken@10595
   131
 */
slouken@10595
   132
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceVendor(int device_index);
slouken@10595
   133
slouken@10595
   134
/**
slouken@10595
   135
 *  Get the USB product ID of a joystick, if available.
slouken@10595
   136
 *  This can be called before any joysticks are opened.
slouken@10595
   137
 *  If the product ID isn't available this function returns 0.
slouken@10595
   138
 */
slouken@10595
   139
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceProduct(int device_index);
slouken@10595
   140
slouken@10595
   141
/**
slouken@10595
   142
 *  Get the product version of a joystick, if available.
slouken@10595
   143
 *  This can be called before any joysticks are opened.
slouken@10595
   144
 *  If the product version isn't available this function returns 0.
slouken@10595
   145
 */
slouken@10595
   146
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceProductVersion(int device_index);
slouken@10595
   147
slouken@10595
   148
/**
slouken@10855
   149
 *  Get the type of a joystick, if available.
slouken@10855
   150
 *  This can be called before any joysticks are opened.
slouken@10855
   151
 */
slouken@10855
   152
extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetDeviceType(int device_index);
slouken@10855
   153
slouken@10855
   154
/**
slouken@10934
   155
 *  Get the instance ID of a joystick.
slouken@10934
   156
 *  This can be called before any joysticks are opened.
slouken@10934
   157
 *  If the index is out of range, this function will return -1.
slouken@10934
   158
 */
slouken@10934
   159
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickGetDeviceInstanceID(int device_index);
slouken@10934
   160
slouken@10934
   161
/**
slouken@7191
   162
 *  Open a joystick for use.
philipp@9227
   163
 *  The index passed as an argument refers to the N'th joystick on the system.
philipp@9569
   164
 *  This index is not the value which will identify this joystick in future
philipp@9569
   165
 *  joystick events.  The joystick's instance id (::SDL_JoystickID) will be used
philipp@9569
   166
 *  there instead.
slouken@7191
   167
 *
slouken@3407
   168
 *  \return A joystick identifier, or NULL if an error occurred.
slouken@0
   169
 */
slouken@1895
   170
extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index);
slouken@0
   171
slouken@3407
   172
/**
icculus@9916
   173
 * Return the SDL_Joystick associated with an instance id.
icculus@9916
   174
 */
icculus@9916
   175
extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickFromInstanceID(SDL_JoystickID joyid);
icculus@9916
   176
icculus@9916
   177
/**
slouken@6690
   178
 *  Return the name for this currently opened joystick.
slouken@6690
   179
 *  If no name can be found, this function returns NULL.
slouken@0
   180
 */
slouken@6690
   181
extern DECLSPEC const char *SDLCALL SDL_JoystickName(SDL_Joystick * joystick);
slouken@7191
   182
slouken@6690
   183
/**
slouken@6690
   184
 *  Return the GUID for this opened joystick
slouken@6690
   185
 */
slouken@6738
   186
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUID(SDL_Joystick * joystick);
slouken@6690
   187
slouken@6690
   188
/**
slouken@10595
   189
 *  Get the USB vendor ID of an opened joystick, if available.
slouken@10595
   190
 *  If the vendor ID isn't available this function returns 0.
slouken@10595
   191
 */
slouken@10595
   192
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetVendor(SDL_Joystick * joystick);
slouken@10595
   193
slouken@10595
   194
/**
slouken@10595
   195
 *  Get the USB product ID of an opened joystick, if available.
slouken@10595
   196
 *  If the product ID isn't available this function returns 0.
slouken@10595
   197
 */
slouken@10595
   198
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetProduct(SDL_Joystick * joystick);
slouken@10595
   199
slouken@10595
   200
/**
slouken@10595
   201
 *  Get the product version of an opened joystick, if available.
slouken@10595
   202
 *  If the product version isn't available this function returns 0.
slouken@10595
   203
 */
slouken@10595
   204
extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetProductVersion(SDL_Joystick * joystick);
slouken@10595
   205
slouken@10595
   206
/**
slouken@10855
   207
 *  Get the type of an opened joystick.
slouken@10855
   208
 */
slouken@10855
   209
extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetType(SDL_Joystick * joystick);
slouken@10855
   210
slouken@10855
   211
/**
icculus@6746
   212
 *  Return a string representation for this guid. pszGUID must point to at least 33 bytes
icculus@6746
   213
 *  (32 for the string plus a NULL terminator).
slouken@6690
   214
 */
icculus@8028
   215
extern DECLSPEC void SDLCALL SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
slouken@6690
   216
slouken@6690
   217
/**
slouken@10595
   218
 *  Convert a string into a joystick guid
slouken@6690
   219
 */
slouken@6738
   220
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID);
slouken@6690
   221
slouken@6707
   222
/**
slouken@6707
   223
 *  Returns SDL_TRUE if the joystick has been opened and currently connected, or SDL_FALSE if it has not.
slouken@6707
   224
 */
slouken@6707
   225
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAttached(SDL_Joystick * joystick);
slouken@6690
   226
slouken@6690
   227
/**
slouken@6974
   228
 *  Get the instance ID of an opened joystick or -1 if the joystick is invalid.
slouken@0
   229
 */
slouken@6690
   230
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickInstanceID(SDL_Joystick * joystick);
slouken@0
   231
slouken@3407
   232
/**
slouken@3407
   233
 *  Get the number of general axis controls on a joystick.
slouken@0
   234
 */
slouken@1895
   235
extern DECLSPEC int SDLCALL SDL_JoystickNumAxes(SDL_Joystick * joystick);
slouken@0
   236
slouken@3407
   237
/**
slouken@3407
   238
 *  Get the number of trackballs on a joystick.
slouken@7191
   239
 *
slouken@3407
   240
 *  Joystick trackballs have only relative motion events associated
slouken@3407
   241
 *  with them and their state cannot be polled.
slouken@0
   242
 */
slouken@1895
   243
extern DECLSPEC int SDLCALL SDL_JoystickNumBalls(SDL_Joystick * joystick);
slouken@0
   244
slouken@3407
   245
/**
slouken@3407
   246
 *  Get the number of POV hats on a joystick.
slouken@0
   247
 */
slouken@1895
   248
extern DECLSPEC int SDLCALL SDL_JoystickNumHats(SDL_Joystick * joystick);
slouken@0
   249
slouken@3407
   250
/**
slouken@3407
   251
 *  Get the number of buttons on a joystick.
slouken@0
   252
 */
slouken@1895
   253
extern DECLSPEC int SDLCALL SDL_JoystickNumButtons(SDL_Joystick * joystick);
slouken@0
   254
slouken@3407
   255
/**
slouken@3407
   256
 *  Update the current state of the open joysticks.
slouken@7191
   257
 *
slouken@3407
   258
 *  This is called automatically by the event loop if any joystick
slouken@3407
   259
 *  events are enabled.
slouken@0
   260
 */
slouken@337
   261
extern DECLSPEC void SDLCALL SDL_JoystickUpdate(void);
slouken@0
   262
slouken@3407
   263
/**
slouken@3407
   264
 *  Enable/disable joystick event polling.
slouken@7191
   265
 *
slouken@3407
   266
 *  If joystick events are disabled, you must call SDL_JoystickUpdate()
slouken@3407
   267
 *  yourself and check the state of the joystick when you want joystick
slouken@3407
   268
 *  information.
slouken@7191
   269
 *
slouken@3407
   270
 *  The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
slouken@0
   271
 */
slouken@337
   272
extern DECLSPEC int SDLCALL SDL_JoystickEventState(int state);
slouken@0
   273
slouken@10724
   274
#define SDL_JOYSTICK_AXIS_MAX   32767
slouken@10724
   275
#define SDL_JOYSTICK_AXIS_MIN   -32768
slouken@3407
   276
/**
slouken@3407
   277
 *  Get the current state of an axis control on a joystick.
slouken@7191
   278
 *
slouken@3407
   279
 *  The state is a value ranging from -32768 to 32767.
slouken@7191
   280
 *
slouken@3407
   281
 *  The axis indices start at index 0.
slouken@0
   282
 */
slouken@1895
   283
extern DECLSPEC Sint16 SDLCALL SDL_JoystickGetAxis(SDL_Joystick * joystick,
slouken@1895
   284
                                                   int axis);
slouken@0
   285
slouken@3407
   286
/**
slouken@10752
   287
 *  Get the initial state of an axis control on a joystick.
slouken@10752
   288
 *
slouken@10752
   289
 *  The state is a value ranging from -32768 to 32767.
slouken@10752
   290
 *
slouken@10752
   291
 *  The axis indices start at index 0.
slouken@10752
   292
 *
slouken@10752
   293
 *  \return SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
slouken@10752
   294
 */
slouken@10752
   295
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick,
slouken@10752
   296
                                                   int axis, Sint16 *state);
slouken@10752
   297
slouken@10752
   298
/**
slouken@3407
   299
 *  \name Hat positions
slouken@0
   300
 */
gabomdq@7678
   301
/* @{ */
slouken@7191
   302
#define SDL_HAT_CENTERED    0x00
philipp@9806
   303
#define SDL_HAT_UP          0x01
slouken@7191
   304
#define SDL_HAT_RIGHT       0x02
slouken@7191
   305
#define SDL_HAT_DOWN        0x04
slouken@7191
   306
#define SDL_HAT_LEFT        0x08
slouken@7191
   307
#define SDL_HAT_RIGHTUP     (SDL_HAT_RIGHT|SDL_HAT_UP)
slouken@7191
   308
#define SDL_HAT_RIGHTDOWN   (SDL_HAT_RIGHT|SDL_HAT_DOWN)
slouken@7191
   309
#define SDL_HAT_LEFTUP      (SDL_HAT_LEFT|SDL_HAT_UP)
slouken@7191
   310
#define SDL_HAT_LEFTDOWN    (SDL_HAT_LEFT|SDL_HAT_DOWN)
gabomdq@7678
   311
/* @} */
slouken@3407
   312
slouken@3407
   313
/**
slouken@3407
   314
 *  Get the current state of a POV hat on a joystick.
slouken@3407
   315
 *
slouken@3407
   316
 *  The hat indices start at index 0.
slouken@7191
   317
 *
slouken@3407
   318
 *  \return The return value is one of the following positions:
slouken@3407
   319
 *           - ::SDL_HAT_CENTERED
slouken@3407
   320
 *           - ::SDL_HAT_UP
slouken@3407
   321
 *           - ::SDL_HAT_RIGHT
slouken@3407
   322
 *           - ::SDL_HAT_DOWN
slouken@3407
   323
 *           - ::SDL_HAT_LEFT
slouken@3407
   324
 *           - ::SDL_HAT_RIGHTUP
slouken@3407
   325
 *           - ::SDL_HAT_RIGHTDOWN
slouken@3407
   326
 *           - ::SDL_HAT_LEFTUP
slouken@3407
   327
 *           - ::SDL_HAT_LEFTDOWN
slouken@0
   328
 */
slouken@1895
   329
extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetHat(SDL_Joystick * joystick,
slouken@1895
   330
                                                 int hat);
slouken@0
   331
slouken@3407
   332
/**
slouken@3407
   333
 *  Get the ball axis change since the last poll.
slouken@7191
   334
 *
slouken@3407
   335
 *  \return 0, or -1 if you passed it invalid parameters.
slouken@7191
   336
 *
slouken@3407
   337
 *  The ball indices start at index 0.
slouken@0
   338
 */
slouken@1895
   339
extern DECLSPEC int SDLCALL SDL_JoystickGetBall(SDL_Joystick * joystick,
slouken@1895
   340
                                                int ball, int *dx, int *dy);
slouken@0
   341
slouken@3407
   342
/**
slouken@3407
   343
 *  Get the current state of a button on a joystick.
slouken@7191
   344
 *
slouken@3407
   345
 *  The button indices start at index 0.
slouken@0
   346
 */
slouken@1895
   347
extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick * joystick,
slouken@1895
   348
                                                    int button);
slouken@0
   349
slouken@3407
   350
/**
slouken@3407
   351
 *  Close a joystick previously opened with SDL_JoystickOpen().
slouken@0
   352
 */
slouken@1895
   353
extern DECLSPEC void SDLCALL SDL_JoystickClose(SDL_Joystick * joystick);
slouken@0
   354
slouken@9884
   355
/**
icculus@9917
   356
 *  Return the battery level of this joystick
icculus@9917
   357
 */
slouken@9884
   358
extern DECLSPEC SDL_JoystickPowerLevel SDLCALL SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick);
slouken@9884
   359
slouken@0
   360
/* Ends C function definitions when using C++ */
slouken@0
   361
#ifdef __cplusplus
slouken@0
   362
}
slouken@0
   363
#endif
slouken@0
   364
#include "close_code.h"
slouken@0
   365
slouken@10638
   366
#endif /* SDL_joystick_h_ */
slouken@1895
   367
slouken@1895
   368
/* vi: set ts=4 sw=4 expandtab: */