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