src/joystick/SDL_sysjoystick.h
author Sam Lantinga
Fri, 27 Nov 2020 18:57:42 -0800
changeset 14413 0ba75e4da3e1
parent 14316 5d6b8455e720
permissions -rw-r--r--
Remember to close the game controller when we're done with it
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 #ifndef SDL_sysjoystick_h_
    24 #define SDL_sysjoystick_h_
    25 
    26 /* This is the system specific header for the SDL joystick API */
    27 
    28 #include "SDL_joystick.h"
    29 #include "SDL_joystick_c.h"
    30 
    31 /* The SDL joystick structure */
    32 typedef struct _SDL_JoystickAxisInfo
    33 {
    34     Sint16 initial_value;       /* Initial axis state */
    35     Sint16 value;               /* Current axis state */
    36     Sint16 zero;                /* Zero point on the axis (-32768 for triggers) */
    37     SDL_bool has_initial_value; /* Whether we've seen a value on the axis yet */
    38     SDL_bool has_second_value;  /* Whether we've seen a second value on the axis yet */
    39     SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */
    40 } SDL_JoystickAxisInfo;
    41 
    42 typedef struct _SDL_JoystickTouchpadFingerInfo
    43 {
    44     Uint8 state;
    45     float x;
    46     float y;
    47     float pressure;
    48 } SDL_JoystickTouchpadFingerInfo;
    49 
    50 typedef struct _SDL_JoystickTouchpadInfo
    51 {
    52     int nfingers;
    53     SDL_JoystickTouchpadFingerInfo *fingers;
    54 } SDL_JoystickTouchpadInfo;
    55 
    56 typedef struct _SDL_JoystickSensorInfo
    57 {
    58     SDL_SensorType type;
    59     SDL_bool enabled;
    60     float data[3];      /* If this needs to expand, update SDL_ControllerSensorEvent */
    61 } SDL_JoystickSensorInfo;
    62 
    63 struct _SDL_Joystick
    64 {
    65     SDL_JoystickID instance_id; /* Device instance, monotonically increasing from 0 */
    66     char *name;                 /* Joystick name - system dependent */
    67     char *serial;               /* Joystick serial */
    68     SDL_JoystickGUID guid;      /* Joystick guid */
    69 
    70     int naxes;                  /* Number of axis controls on the joystick */
    71     SDL_JoystickAxisInfo *axes;
    72 
    73     int nhats;                  /* Number of hats on the joystick */
    74     Uint8 *hats;                /* Current hat states */
    75 
    76     int nballs;                 /* Number of trackballs on the joystick */
    77     struct balldelta {
    78         int dx;
    79         int dy;
    80     } *balls;                   /* Current ball motion deltas */
    81 
    82     int nbuttons;               /* Number of buttons on the joystick */
    83     Uint8 *buttons;             /* Current button states */
    84 
    85     int ntouchpads;             /* Number of touchpads on the joystick */
    86     SDL_JoystickTouchpadInfo *touchpads;    /* Current touchpad states */
    87 
    88     int nsensors;               /* Number of sensors on the joystick */
    89     int nsensors_enabled;
    90     SDL_JoystickSensorInfo *sensors;
    91 
    92     Uint16 low_frequency_rumble;
    93     Uint16 high_frequency_rumble;
    94     Uint32 rumble_expiration;
    95 
    96     Uint16 left_trigger_rumble;
    97     Uint16 right_trigger_rumble;
    98     Uint32 trigger_rumble_expiration;
    99 
   100     Uint8 led_red;
   101     Uint8 led_green;
   102     Uint8 led_blue;
   103 
   104     SDL_bool attached;
   105     SDL_bool is_game_controller;
   106     SDL_bool delayed_guide_button; /* SDL_TRUE if this device has the guide button event delayed */
   107     SDL_JoystickPowerLevel epowerlevel; /* power level of this joystick, SDL_JOYSTICK_POWER_UNKNOWN if not supported */
   108 
   109     struct _SDL_JoystickDriver *driver;
   110 
   111     struct joystick_hwdata *hwdata;     /* Driver dependent information */
   112 
   113     int ref_count;              /* Reference count for multiple opens */
   114 
   115     struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */
   116 };
   117 
   118 /* Device bus definitions */
   119 #define SDL_HARDWARE_BUS_USB        0x03
   120 #define SDL_HARDWARE_BUS_BLUETOOTH  0x05
   121 
   122 /* Macro to combine a USB vendor ID and product ID into a single Uint32 value */
   123 #define MAKE_VIDPID(VID, PID)   (((Uint32)(VID))<<16|(PID))
   124 
   125 typedef struct _SDL_JoystickDriver
   126 {
   127     /* Function to scan the system for joysticks.
   128      * Joystick 0 should be the system default joystick.
   129      * This function should return 0, or -1 on an unrecoverable error.
   130      */
   131     int (*Init)(void);
   132 
   133     /* Function to return the number of joystick devices plugged in right now */
   134     int (*GetCount)(void);
   135 
   136     /* Function to cause any queued joystick insertions to be processed */
   137     void (*Detect)(void);
   138 
   139     /* Function to get the device-dependent name of a joystick */
   140     const char *(*GetDeviceName)(int device_index);
   141 
   142     /* Function to get the player index of a joystick */
   143     int (*GetDevicePlayerIndex)(int device_index);
   144 
   145     /* Function to get the player index of a joystick */
   146     void (*SetDevicePlayerIndex)(int device_index, int player_index);
   147 
   148     /* Function to return the stable GUID for a plugged in device */
   149     SDL_JoystickGUID (*GetDeviceGUID)(int device_index);
   150 
   151     /* Function to get the current instance id of the joystick located at device_index */
   152     SDL_JoystickID (*GetDeviceInstanceID)(int device_index);
   153 
   154     /* Function to open a joystick for use.
   155        The joystick to open is specified by the device index.
   156        This should fill the nbuttons and naxes fields of the joystick structure.
   157        It returns 0, or -1 if there is an error.
   158      */
   159     int (*Open)(SDL_Joystick *joystick, int device_index);
   160 
   161     /* Rumble functionality */
   162     int (*Rumble)(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
   163     int (*RumbleTriggers)(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
   164 
   165     /* LED functionality */
   166     SDL_bool (*HasLED)(SDL_Joystick *joystick);
   167     int (*SetLED)(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
   168 
   169     /* Sensor functionality */
   170     int (*SetSensorsEnabled)(SDL_Joystick *joystick, SDL_bool enabled);
   171 
   172     /* Function to update the state of a joystick - called as a device poll.
   173      * This function shouldn't update the joystick structure directly,
   174      * but instead should call SDL_PrivateJoystick*() to deliver events
   175      * and update joystick device state.
   176      */
   177     void (*Update)(SDL_Joystick *joystick);
   178 
   179     /* Function to close a joystick after use */
   180     void (*Close)(SDL_Joystick *joystick);
   181 
   182     /* Function to perform any system-specific joystick related cleanup */
   183     void (*Quit)(void);
   184 
   185     /* Function to get the autodetected controller mapping; returns false if there isn't any. */
   186     SDL_bool (*GetGamepadMapping)(int device_index, SDL_GamepadMapping * out);
   187 
   188 } SDL_JoystickDriver;
   189 
   190 /* Windows and Mac OSX has a limit of MAX_DWORD / 1000, Linux kernel has a limit of 0xFFFF */
   191 #define SDL_MAX_RUMBLE_DURATION_MS  0xFFFF
   192 
   193 /* The available joystick drivers */
   194 extern SDL_JoystickDriver SDL_ANDROID_JoystickDriver;
   195 extern SDL_JoystickDriver SDL_BSD_JoystickDriver;
   196 extern SDL_JoystickDriver SDL_DARWIN_JoystickDriver;
   197 extern SDL_JoystickDriver SDL_DUMMY_JoystickDriver;
   198 extern SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver;
   199 extern SDL_JoystickDriver SDL_HAIKU_JoystickDriver;
   200 extern SDL_JoystickDriver SDL_HIDAPI_JoystickDriver;
   201 extern SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver;
   202 extern SDL_JoystickDriver SDL_IOS_JoystickDriver;
   203 extern SDL_JoystickDriver SDL_LINUX_JoystickDriver;
   204 extern SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver;
   205 extern SDL_JoystickDriver SDL_WGI_JoystickDriver;
   206 extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver;
   207 
   208 #endif /* SDL_sysjoystick_h_ */
   209 
   210 /* vi: set ts=4 sw=4 expandtab: */