include/SDL_joystick.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 30 Sep 2015 15:39:30 -0700
changeset 9884 26b595dea221
parent 9806 2deeef7b793c
child 9916 9429bc102632
permissions -rw-r--r--
SDL - added new SDL_JoystickCurrentPowerLevel() API that returns the battery level of the selected joystick. Currently only implemented for XInput devices, other platforms are a TODO.

CR: Sam
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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
 *
slouken@6690
    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@0
    39
#ifndef _SDL_joystick_h
slouken@0
    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@0
    63
/* The joystick structure used to identify an SDL joystick */
slouken@0
    64
struct _SDL_Joystick;
slouken@0
    65
typedef struct _SDL_Joystick SDL_Joystick;
slouken@0
    66
slouken@6707
    67
/* A structure that encodes the stable unique id for a joystick device */
slouken@6707
    68
typedef struct {
slouken@7191
    69
    Uint8 data[16];
slouken@6738
    70
} SDL_JoystickGUID;
slouken@6707
    71
slouken@6974
    72
typedef Sint32 SDL_JoystickID;
slouken@6707
    73
slouken@9884
    74
typedef enum
slouken@9884
    75
{
slouken@9884
    76
    SDL_JOYSTICK_POWER_UNKNOWN = -1,
slouken@9884
    77
    SDL_JOYSTICK_POWER_EMPTY,
slouken@9884
    78
    SDL_JOYSTICK_POWER_LOW,
slouken@9884
    79
    SDL_JOYSTICK_POWER_MEDIUM,
slouken@9884
    80
    SDL_JOYSTICK_POWER_FULL,
slouken@9884
    81
    SDL_JOYSTICK_POWER_WIRED,
slouken@9884
    82
    SDL_JOYSTICK_POWER_MAX
slouken@9884
    83
} SDL_JoystickPowerLevel;
slouken@6707
    84
slouken@0
    85
/* Function prototypes */
slouken@3407
    86
/**
slouken@6690
    87
 *  Count the number of joysticks attached to the system right now
slouken@0
    88
 */
slouken@337
    89
extern DECLSPEC int SDLCALL SDL_NumJoysticks(void);
slouken@0
    90
slouken@3407
    91
/**
slouken@3407
    92
 *  Get the implementation dependent name of a joystick.
slouken@3407
    93
 *  This can be called before any joysticks are opened.
slouken@3407
    94
 *  If no name can be found, this function returns NULL.
slouken@0
    95
 */
slouken@6690
    96
extern DECLSPEC const char *SDLCALL SDL_JoystickNameForIndex(int device_index);
slouken@0
    97
slouken@3407
    98
/**
slouken@7191
    99
 *  Open a joystick for use.
philipp@9227
   100
 *  The index passed as an argument refers to the N'th joystick on the system.
philipp@9569
   101
 *  This index is not the value which will identify this joystick in future
philipp@9569
   102
 *  joystick events.  The joystick's instance id (::SDL_JoystickID) will be used
philipp@9569
   103
 *  there instead.
slouken@7191
   104
 *
slouken@3407
   105
 *  \return A joystick identifier, or NULL if an error occurred.
slouken@0
   106
 */
slouken@1895
   107
extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index);
slouken@0
   108
slouken@3407
   109
/**
slouken@6690
   110
 *  Return the name for this currently opened joystick.
slouken@6690
   111
 *  If no name can be found, this function returns NULL.
slouken@0
   112
 */
slouken@6690
   113
extern DECLSPEC const char *SDLCALL SDL_JoystickName(SDL_Joystick * joystick);
slouken@7191
   114
slouken@6690
   115
/**
slouken@6690
   116
 *  Return the GUID for the joystick at this index
slouken@6690
   117
 */
slouken@6738
   118
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetDeviceGUID(int device_index);
slouken@6690
   119
slouken@6690
   120
/**
slouken@6690
   121
 *  Return the GUID for this opened joystick
slouken@6690
   122
 */
slouken@6738
   123
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUID(SDL_Joystick * joystick);
slouken@6690
   124
slouken@6690
   125
/**
icculus@6746
   126
 *  Return a string representation for this guid. pszGUID must point to at least 33 bytes
icculus@6746
   127
 *  (32 for the string plus a NULL terminator).
slouken@6690
   128
 */
icculus@8028
   129
extern DECLSPEC void SDLCALL SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
slouken@6690
   130
slouken@6690
   131
/**
slouken@6690
   132
 *  convert a string into a joystick formatted guid
slouken@6690
   133
 */
slouken@6738
   134
extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID);
slouken@6690
   135
slouken@6707
   136
/**
slouken@6707
   137
 *  Returns SDL_TRUE if the joystick has been opened and currently connected, or SDL_FALSE if it has not.
slouken@6707
   138
 */
slouken@6707
   139
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAttached(SDL_Joystick * joystick);
slouken@6690
   140
slouken@6690
   141
/**
slouken@6974
   142
 *  Get the instance ID of an opened joystick or -1 if the joystick is invalid.
slouken@0
   143
 */
slouken@6690
   144
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickInstanceID(SDL_Joystick * joystick);
slouken@0
   145
slouken@3407
   146
/**
slouken@3407
   147
 *  Get the number of general axis controls on a joystick.
slouken@0
   148
 */
slouken@1895
   149
extern DECLSPEC int SDLCALL SDL_JoystickNumAxes(SDL_Joystick * joystick);
slouken@0
   150
slouken@3407
   151
/**
slouken@3407
   152
 *  Get the number of trackballs on a joystick.
slouken@7191
   153
 *
slouken@3407
   154
 *  Joystick trackballs have only relative motion events associated
slouken@3407
   155
 *  with them and their state cannot be polled.
slouken@0
   156
 */
slouken@1895
   157
extern DECLSPEC int SDLCALL SDL_JoystickNumBalls(SDL_Joystick * joystick);
slouken@0
   158
slouken@3407
   159
/**
slouken@3407
   160
 *  Get the number of POV hats on a joystick.
slouken@0
   161
 */
slouken@1895
   162
extern DECLSPEC int SDLCALL SDL_JoystickNumHats(SDL_Joystick * joystick);
slouken@0
   163
slouken@3407
   164
/**
slouken@3407
   165
 *  Get the number of buttons on a joystick.
slouken@0
   166
 */
slouken@1895
   167
extern DECLSPEC int SDLCALL SDL_JoystickNumButtons(SDL_Joystick * joystick);
slouken@0
   168
slouken@3407
   169
/**
slouken@3407
   170
 *  Update the current state of the open joysticks.
slouken@7191
   171
 *
slouken@3407
   172
 *  This is called automatically by the event loop if any joystick
slouken@3407
   173
 *  events are enabled.
slouken@0
   174
 */
slouken@337
   175
extern DECLSPEC void SDLCALL SDL_JoystickUpdate(void);
slouken@0
   176
slouken@3407
   177
/**
slouken@3407
   178
 *  Enable/disable joystick event polling.
slouken@7191
   179
 *
slouken@3407
   180
 *  If joystick events are disabled, you must call SDL_JoystickUpdate()
slouken@3407
   181
 *  yourself and check the state of the joystick when you want joystick
slouken@3407
   182
 *  information.
slouken@7191
   183
 *
slouken@3407
   184
 *  The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
slouken@0
   185
 */
slouken@337
   186
extern DECLSPEC int SDLCALL SDL_JoystickEventState(int state);
slouken@0
   187
slouken@3407
   188
/**
slouken@3407
   189
 *  Get the current state of an axis control on a joystick.
slouken@7191
   190
 *
slouken@3407
   191
 *  The state is a value ranging from -32768 to 32767.
slouken@7191
   192
 *
slouken@3407
   193
 *  The axis indices start at index 0.
slouken@0
   194
 */
slouken@1895
   195
extern DECLSPEC Sint16 SDLCALL SDL_JoystickGetAxis(SDL_Joystick * joystick,
slouken@1895
   196
                                                   int axis);
slouken@0
   197
slouken@3407
   198
/**
slouken@3407
   199
 *  \name Hat positions
slouken@0
   200
 */
gabomdq@7678
   201
/* @{ */
slouken@7191
   202
#define SDL_HAT_CENTERED    0x00
philipp@9806
   203
#define SDL_HAT_UP          0x01
slouken@7191
   204
#define SDL_HAT_RIGHT       0x02
slouken@7191
   205
#define SDL_HAT_DOWN        0x04
slouken@7191
   206
#define SDL_HAT_LEFT        0x08
slouken@7191
   207
#define SDL_HAT_RIGHTUP     (SDL_HAT_RIGHT|SDL_HAT_UP)
slouken@7191
   208
#define SDL_HAT_RIGHTDOWN   (SDL_HAT_RIGHT|SDL_HAT_DOWN)
slouken@7191
   209
#define SDL_HAT_LEFTUP      (SDL_HAT_LEFT|SDL_HAT_UP)
slouken@7191
   210
#define SDL_HAT_LEFTDOWN    (SDL_HAT_LEFT|SDL_HAT_DOWN)
gabomdq@7678
   211
/* @} */
slouken@3407
   212
slouken@3407
   213
/**
slouken@3407
   214
 *  Get the current state of a POV hat on a joystick.
slouken@3407
   215
 *
slouken@3407
   216
 *  The hat indices start at index 0.
slouken@7191
   217
 *
slouken@3407
   218
 *  \return The return value is one of the following positions:
slouken@3407
   219
 *           - ::SDL_HAT_CENTERED
slouken@3407
   220
 *           - ::SDL_HAT_UP
slouken@3407
   221
 *           - ::SDL_HAT_RIGHT
slouken@3407
   222
 *           - ::SDL_HAT_DOWN
slouken@3407
   223
 *           - ::SDL_HAT_LEFT
slouken@3407
   224
 *           - ::SDL_HAT_RIGHTUP
slouken@3407
   225
 *           - ::SDL_HAT_RIGHTDOWN
slouken@3407
   226
 *           - ::SDL_HAT_LEFTUP
slouken@3407
   227
 *           - ::SDL_HAT_LEFTDOWN
slouken@0
   228
 */
slouken@1895
   229
extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetHat(SDL_Joystick * joystick,
slouken@1895
   230
                                                 int hat);
slouken@0
   231
slouken@3407
   232
/**
slouken@3407
   233
 *  Get the ball axis change since the last poll.
slouken@7191
   234
 *
slouken@3407
   235
 *  \return 0, or -1 if you passed it invalid parameters.
slouken@7191
   236
 *
slouken@3407
   237
 *  The ball indices start at index 0.
slouken@0
   238
 */
slouken@1895
   239
extern DECLSPEC int SDLCALL SDL_JoystickGetBall(SDL_Joystick * joystick,
slouken@1895
   240
                                                int ball, int *dx, int *dy);
slouken@0
   241
slouken@3407
   242
/**
slouken@3407
   243
 *  Get the current state of a button on a joystick.
slouken@7191
   244
 *
slouken@3407
   245
 *  The button indices start at index 0.
slouken@0
   246
 */
slouken@1895
   247
extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick * joystick,
slouken@1895
   248
                                                    int button);
slouken@0
   249
slouken@3407
   250
/**
slouken@3407
   251
 *  Close a joystick previously opened with SDL_JoystickOpen().
slouken@0
   252
 */
slouken@1895
   253
extern DECLSPEC void SDLCALL SDL_JoystickClose(SDL_Joystick * joystick);
slouken@0
   254
slouken@9884
   255
/**
slouken@9884
   256
*  Return the battery level of this joystick
slouken@9884
   257
*/
slouken@9884
   258
extern DECLSPEC SDL_JoystickPowerLevel SDLCALL SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick);
slouken@9884
   259
slouken@0
   260
slouken@0
   261
/* Ends C function definitions when using C++ */
slouken@0
   262
#ifdef __cplusplus
slouken@0
   263
}
slouken@0
   264
#endif
slouken@0
   265
#include "close_code.h"
slouken@0
   266
slouken@0
   267
#endif /* _SDL_joystick_h */
slouken@1895
   268
slouken@1895
   269
/* vi: set ts=4 sw=4 expandtab: */