src/joystick/hidapi/SDL_hidapi_ps4.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 29 Aug 2018 20:55:02 -0700
changeset 12165 c7e321efcbab
parent 12163 dd056c76593a
child 12168 a5459597367f
permissions -rw-r--r--
Don't show the Razer Raiju sound interface as a game controller
slouken@12088
     1
/*
slouken@12088
     2
  Simple DirectMedia Layer
slouken@12088
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@12088
     4
slouken@12088
     5
  This software is provided 'as-is', without any express or implied
slouken@12088
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@12088
     7
  arising from the use of this software.
slouken@12088
     8
slouken@12088
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@12088
    10
  including commercial applications, and to alter it and redistribute it
slouken@12088
    11
  freely, subject to the following restrictions:
slouken@12088
    12
slouken@12088
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@12088
    14
     claim that you wrote the original software. If you use this software
slouken@12088
    15
     in a product, an acknowledgment in the product documentation would be
slouken@12088
    16
     appreciated but is not required.
slouken@12088
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@12088
    18
     misrepresented as being the original software.
slouken@12088
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@12088
    20
*/
slouken@12097
    21
/* This driver supports both simplified reports and the extended input reports enabled by Steam.
slouken@12097
    22
   Code and logic contributed by Valve Corporation under the SDL zlib license.
slouken@12097
    23
*/
slouken@12088
    24
#include "../../SDL_internal.h"
slouken@12088
    25
slouken@12088
    26
#ifdef SDL_JOYSTICK_HIDAPI
slouken@12088
    27
slouken@12088
    28
#include "SDL_hints.h"
slouken@12088
    29
#include "SDL_log.h"
slouken@12088
    30
#include "SDL_events.h"
slouken@12088
    31
#include "SDL_timer.h"
slouken@12088
    32
#include "SDL_joystick.h"
slouken@12088
    33
#include "SDL_gamecontroller.h"
slouken@12088
    34
#include "../SDL_sysjoystick.h"
slouken@12088
    35
#include "SDL_hidapijoystick_c.h"
slouken@12088
    36
slouken@12088
    37
slouken@12088
    38
#ifdef SDL_JOYSTICK_HIDAPI_PS4
slouken@12088
    39
slouken@12088
    40
#define SONY_USB_VID        0x054C
slouken@12088
    41
#define SONY_DS4_PID        0x05C4
slouken@12088
    42
#define SONY_DS4_DONGLE_PID 0x0BA0
slouken@12088
    43
#define SONY_DS4_SLIM_PID   0x09CC
slouken@12088
    44
slouken@12088
    45
#define USB_PACKET_LENGTH   64
slouken@12088
    46
slouken@12088
    47
#define VOLUME_CHECK_INTERVAL_MS    (10 * 1000)
slouken@12088
    48
slouken@12088
    49
typedef enum
slouken@12088
    50
{
slouken@12088
    51
    k_EPS4ReportIdUsbState = 1,
slouken@12088
    52
    k_EPS4ReportIdUsbEffects = 5,
slouken@12088
    53
    k_EPS4ReportIdBluetoothState = 17,
slouken@12088
    54
    k_EPS4ReportIdBluetoothEffects = 17,
slouken@12088
    55
    k_EPS4ReportIdDisconnectMessage = 226,
slouken@12088
    56
} EPS4ReportId;
slouken@12088
    57
slouken@12088
    58
typedef enum 
slouken@12088
    59
{
slouken@12088
    60
    k_ePS4FeatureReportIdGyroCalibration_USB = 0x02,
slouken@12088
    61
    k_ePS4FeatureReportIdGyroCalibration_BT = 0x05,
slouken@12088
    62
    k_ePS4FeatureReportIdSerialNumber = 0x12,
slouken@12088
    63
} EPS4FeatureReportID;
slouken@12088
    64
slouken@12088
    65
typedef struct
slouken@12088
    66
{
slouken@12088
    67
    Uint8 ucLeftJoystickX;
slouken@12088
    68
    Uint8 ucLeftJoystickY;
slouken@12088
    69
    Uint8 ucRightJoystickX;
slouken@12088
    70
    Uint8 ucRightJoystickY;
slouken@12088
    71
    Uint8 rgucButtonsHatAndCounter[ 3 ];
slouken@12088
    72
    Uint8 ucTriggerLeft;
slouken@12088
    73
    Uint8 ucTriggerRight;
slouken@12088
    74
    Uint8 _rgucPad0[ 3 ];
slouken@12088
    75
    Sint16 sGyroX;
slouken@12088
    76
    Sint16 sGyroY;
slouken@12088
    77
    Sint16 sGyroZ;
slouken@12088
    78
    Sint16 sAccelX;
slouken@12088
    79
    Sint16 sAccelY;
slouken@12088
    80
    Sint16 sAccelZ;
slouken@12088
    81
    Uint8 _rgucPad1[ 5 ];
slouken@12088
    82
    Uint8 ucBatteryLevel;
slouken@12088
    83
    Uint8 _rgucPad2[ 4 ];
slouken@12088
    84
    Uint8 ucTrackpadCounter1;
slouken@12088
    85
    Uint8 rgucTrackpadData1[ 3 ];
slouken@12088
    86
    Uint8 ucTrackpadCounter2;
slouken@12088
    87
    Uint8 rgucTrackpadData2[ 3 ];
slouken@12088
    88
} PS4StatePacket_t;
slouken@12088
    89
slouken@12088
    90
typedef struct
slouken@12088
    91
{
slouken@12088
    92
    Uint8 ucRumbleRight;
slouken@12088
    93
    Uint8 ucRumbleLeft;
slouken@12088
    94
    Uint8 ucLedRed;
slouken@12088
    95
    Uint8 ucLedGreen;
slouken@12088
    96
    Uint8 ucLedBlue;
slouken@12088
    97
    Uint8 ucLedDelayOn;
slouken@12088
    98
    Uint8 ucLedDelayOff;
slouken@12088
    99
    Uint8 _rgucPad0[ 8 ];
slouken@12088
   100
    Uint8 ucVolumeLeft;
slouken@12088
   101
    Uint8 ucVolumeRight;
slouken@12088
   102
    Uint8 ucVolumeMic;
slouken@12088
   103
    Uint8 ucVolumeSpeaker;
slouken@12088
   104
} DS4EffectsState_t;
slouken@12088
   105
slouken@12088
   106
typedef struct {
slouken@12088
   107
    SDL_bool is_dongle;
slouken@12088
   108
    SDL_bool is_bluetooth;
slouken@12088
   109
    SDL_bool audio_supported;
slouken@12163
   110
    SDL_bool rumble_supported;
slouken@12088
   111
    Uint8 volume;
slouken@12088
   112
    Uint32 last_volume_check;
slouken@12088
   113
    Uint32 rumble_expiration;
slouken@12088
   114
    PS4StatePacket_t last_state;
slouken@12088
   115
} SDL_DriverPS4_Context;
slouken@12088
   116
slouken@12088
   117
slouken@12088
   118
/* Public domain CRC implementation adapted from:
slouken@12088
   119
   http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
slouken@12088
   120
*/
slouken@12088
   121
static Uint32 crc32_for_byte(Uint32 r)
slouken@12088
   122
{
slouken@12088
   123
    int i;
slouken@12088
   124
    for(i = 0; i < 8; ++i) {
slouken@12088
   125
        r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1;
slouken@12088
   126
    }
slouken@12088
   127
    return r ^ (Uint32)0xFF000000L;
slouken@12088
   128
}
slouken@12088
   129
slouken@12088
   130
static Uint32 crc32(Uint32 crc, const void *data, int count)
slouken@12088
   131
{
slouken@12088
   132
    int i;
slouken@12088
   133
    for(i = 0; i < count; ++i) {
slouken@12088
   134
        crc = crc32_for_byte((Uint8)crc ^ ((const Uint8*)data)[i]) ^ crc >> 8;
slouken@12088
   135
    }
slouken@12088
   136
    return crc;
slouken@12088
   137
}
slouken@12088
   138
slouken@12088
   139
#ifdef __WIN32__
slouken@12088
   140
#include "../../core/windows/SDL_windows.h"
slouken@12088
   141
sezeroz@12120
   142
#ifndef NTDDI_VISTA
sezeroz@12120
   143
#define NTDDI_VISTA    0x06000000
sezeroz@12120
   144
#endif
sezeroz@12120
   145
#ifndef _WIN32_WINNT_VISTA
sezeroz@12120
   146
#define _WIN32_WINNT_VISTA 0x0600
sezeroz@12120
   147
#endif
sezeroz@12120
   148
slouken@12088
   149
/* Define Vista for the Audio related includes below to work */
slouken@12088
   150
#undef NTDDI_VERSION
slouken@12088
   151
#define NTDDI_VERSION NTDDI_VISTA
slouken@12088
   152
#undef _WIN32_WINNT
slouken@12088
   153
#define _WIN32_WINNT _WIN32_WINNT_VISTA
slouken@12088
   154
#define COBJMACROS
sezeroz@12101
   155
#include <mmdeviceapi.h>
sezeroz@12101
   156
#include <audioclient.h>
sezeroz@12101
   157
#include <endpointvolume.h>
slouken@12088
   158
slouken@12088
   159
#undef DEFINE_GUID
slouken@12088
   160
#define DEFINE_GUID(n,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) static const GUID n = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}
slouken@12088
   161
DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xBCDE0395, 0xE52F, 0x467C, 0x8E, 0x3D, 0xC4, 0x57, 0x92, 0x91, 0x69, 0x2E);
slouken@12088
   162
DEFINE_GUID(IID_IMMDeviceEnumerator, 0xA95664D2, 0x9614, 0x4F35, 0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36, 0x17, 0xE6);
slouken@12088
   163
DEFINE_GUID(IID_IAudioEndpointVolume, 0x5CDF2C82, 0x841E, 0x4546, 0x97, 0x22, 0x0C, 0xF7, 0x40, 0x78, 0x22, 0x9A);
slouken@12088
   164
#endif
slouken@12088
   165
slouken@12088
   166
slouken@12088
   167
slouken@12088
   168
static float GetSystemVolume(void)
slouken@12088
   169
{
slouken@12088
   170
    float volume = -1.0f;    /* Return this if we can't get system volume */
slouken@12088
   171
slouken@12088
   172
#ifdef __WIN32__
slouken@12088
   173
    HRESULT hr = WIN_CoInitialize();
slouken@12088
   174
    if (SUCCEEDED(hr)) {
slouken@12088
   175
        IMMDeviceEnumerator *pEnumerator;
slouken@12088
   176
slouken@12088
   177
        /* This should gracefully fail on XP and succeed on everything Vista and above */
slouken@12088
   178
        hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &IID_IMMDeviceEnumerator, (LPVOID*)&pEnumerator);
slouken@12088
   179
        if (SUCCEEDED(hr)) {
slouken@12088
   180
            IMMDevice *pDevice;
slouken@12088
   181
slouken@12088
   182
            hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(pEnumerator, eRender, eConsole, &pDevice);
slouken@12088
   183
            if (SUCCEEDED(hr)) {
slouken@12088
   184
                IAudioEndpointVolume *pEndpointVolume;
slouken@12088
   185
slouken@12088
   186
                hr = IMMDevice_Activate(pDevice, &IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (LPVOID*)&pEndpointVolume);
slouken@12088
   187
                if (SUCCEEDED(hr)) {
slouken@12088
   188
                    IAudioEndpointVolume_GetMasterVolumeLevelScalar(pEndpointVolume, &volume);
slouken@12088
   189
                    IUnknown_Release(pEndpointVolume);
slouken@12088
   190
                }
slouken@12088
   191
                IUnknown_Release(pDevice);
slouken@12088
   192
            }
slouken@12088
   193
            IUnknown_Release(pEnumerator);
slouken@12088
   194
        }
slouken@12088
   195
        WIN_CoUninitialize();
slouken@12088
   196
    }
slouken@12088
   197
#endif /* __WIN32__ */
slouken@12088
   198
slouken@12088
   199
    return volume;
slouken@12088
   200
}
slouken@12088
   201
slouken@12088
   202
static uint8_t GetPlaystationVolumeFromFloat(float fVolume)
slouken@12088
   203
{
slouken@12088
   204
    const int k_nVolumeFitRatio = 15;
slouken@12088
   205
    const int k_nVolumeFitOffset = 9;
slouken@12088
   206
    float fVolLog;
slouken@12088
   207
slouken@12088
   208
    if (fVolume > 1.0f || fVolume < 0.0f) {
slouken@12088
   209
        fVolume = 0.30f;
slouken@12088
   210
    }
slouken@12088
   211
    fVolLog = SDL_logf(fVolume * 100);
slouken@12088
   212
slouken@12088
   213
    return (Uint8)((fVolLog * k_nVolumeFitRatio) + k_nVolumeFitOffset);
slouken@12088
   214
}
slouken@12088
   215
slouken@12088
   216
static SDL_bool
slouken@12115
   217
HIDAPI_DriverPS4_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, Uint16 usage_page, Uint16 usage)
slouken@12088
   218
{
slouken@12165
   219
    /* The Revolution Pro Controller and Razer Raiju expose multiple interfaces on Windows */
slouken@12088
   220
    const Uint16 NACON_USB_VID = 0x146b;
slouken@12165
   221
    const Uint16 RAZER_USB_VID = 0x1532;
slouken@12165
   222
    if ((vendor_id == NACON_USB_VID || vendor_id == RAZER_USB_VID) &&
slouken@12165
   223
        (usage_page != 0 && usage_page != 1)) {
slouken@12088
   224
        return SDL_FALSE;
slouken@12088
   225
    }
slouken@12088
   226
slouken@12088
   227
    return SDL_IsJoystickPS4(vendor_id, product_id);
slouken@12088
   228
}
slouken@12088
   229
slouken@12088
   230
static const char *
slouken@12088
   231
HIDAPI_DriverPS4_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
slouken@12088
   232
{
slouken@12088
   233
    if (vendor_id == SONY_USB_VID) {
slouken@12088
   234
        return "PS4 Controller";
slouken@12088
   235
    }
slouken@12088
   236
    return NULL;
slouken@12088
   237
}
slouken@12088
   238
slouken@12088
   239
static SDL_bool ReadFeatureReport(hid_device *dev, Uint8 report_id, Uint8 *data, size_t size)
slouken@12088
   240
{
slouken@12088
   241
    Uint8 report[USB_PACKET_LENGTH + 1];
slouken@12088
   242
slouken@12088
   243
    SDL_memset(report, 0, sizeof(report));
slouken@12088
   244
    report[0] = report_id;
slouken@12088
   245
    if (hid_get_feature_report(dev, report, sizeof(report)) < 0) {
slouken@12088
   246
        return SDL_FALSE;
slouken@12088
   247
    }
slouken@12088
   248
    SDL_memcpy(data, report, SDL_min(size, sizeof(report)));
slouken@12088
   249
    return SDL_TRUE;
slouken@12088
   250
}
slouken@12088
   251
slouken@12088
   252
static SDL_bool CheckUSBConnected(hid_device *dev)
slouken@12088
   253
{
slouken@12088
   254
    int i;
slouken@12088
   255
    Uint8 data[16];
slouken@12088
   256
slouken@12088
   257
    /* This will fail if we're on Bluetooth */
slouken@12088
   258
    if (ReadFeatureReport(dev, k_ePS4FeatureReportIdSerialNumber, data, sizeof(data))) {
slouken@12088
   259
        for (i = 0; i < sizeof(data); ++i) {
slouken@12088
   260
            if (data[i] != 0x00) {
slouken@12088
   261
                return SDL_TRUE;
slouken@12088
   262
            }
slouken@12088
   263
        }
slouken@12088
   264
        /* Maybe the dongle without a connected controller? */
slouken@12088
   265
    }
slouken@12088
   266
    return SDL_FALSE;
slouken@12088
   267
}
slouken@12088
   268
slouken@12088
   269
static int HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
slouken@12088
   270
slouken@12088
   271
static SDL_bool
slouken@12088
   272
HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
slouken@12088
   273
{
slouken@12088
   274
    SDL_DriverPS4_Context *ctx;
slouken@12088
   275
slouken@12088
   276
    ctx = (SDL_DriverPS4_Context *)SDL_calloc(1, sizeof(*ctx));
slouken@12088
   277
    if (!ctx) {
slouken@12088
   278
        SDL_OutOfMemory();
slouken@12088
   279
        return SDL_FALSE;
slouken@12088
   280
    }
slouken@12088
   281
    *context = ctx;
slouken@12088
   282
slouken@12088
   283
    /* Check for type of connection */
slouken@12088
   284
    ctx->is_dongle = (vendor_id == SONY_USB_VID && product_id == SONY_DS4_DONGLE_PID);
slouken@12088
   285
    if (ctx->is_dongle) {
slouken@12088
   286
        ctx->is_bluetooth = SDL_FALSE;
slouken@12088
   287
    } else if (vendor_id == SONY_USB_VID) {
slouken@12088
   288
        ctx->is_bluetooth = !CheckUSBConnected(dev);
slouken@12088
   289
    } else {
slouken@12088
   290
        /* Third party controllers appear to all be wired */
slouken@12088
   291
        ctx->is_bluetooth = SDL_FALSE;
slouken@12088
   292
    }
slouken@12088
   293
#ifdef DEBUG_PS4
slouken@12088
   294
    SDL_Log("PS4 dongle = %s, bluetooth = %s\n", ctx->is_dongle ? "TRUE" : "FALSE", ctx->is_bluetooth ? "TRUE" : "FALSE");
slouken@12088
   295
#endif
slouken@12088
   296
slouken@12088
   297
    /* Check to see if audio is supported */
slouken@12088
   298
    if (vendor_id == SONY_USB_VID &&
slouken@12088
   299
        (product_id == SONY_DS4_SLIM_PID || product_id == SONY_DS4_DONGLE_PID )) {
slouken@12088
   300
        ctx->audio_supported = SDL_TRUE;
slouken@12088
   301
    }
slouken@12088
   302
slouken@12163
   303
    if (ctx->is_bluetooth) {
slouken@12163
   304
        ctx->rumble_supported = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_FALSE);
slouken@12163
   305
    } else {
slouken@12163
   306
        ctx->rumble_supported = SDL_TRUE;
slouken@12163
   307
    }
slouken@12163
   308
slouken@12088
   309
    /* Initialize LED and effect state */
slouken@12088
   310
    HIDAPI_DriverPS4_Rumble(joystick, dev, ctx, 0, 0, 0);
slouken@12088
   311
slouken@12088
   312
    /* Initialize the joystick capabilities */
slouken@12088
   313
    joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
slouken@12088
   314
    joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
slouken@12088
   315
    joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
slouken@12088
   316
slouken@12088
   317
    return SDL_TRUE;
slouken@12088
   318
}
slouken@12088
   319
slouken@12088
   320
static int
slouken@12088
   321
HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
slouken@12088
   322
{
slouken@12088
   323
    SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context;
slouken@12088
   324
    DS4EffectsState_t *effects;
slouken@12088
   325
    Uint8 data[78];
slouken@12088
   326
    int report_size, offset;
slouken@12088
   327
slouken@12163
   328
    if (!ctx->rumble_supported) {
slouken@12163
   329
        return SDL_Unsupported();
slouken@12163
   330
    }
slouken@12163
   331
slouken@12088
   332
    /* In order to send rumble, we have to send a complete effect packet */
slouken@12088
   333
    SDL_memset(data, 0, sizeof(data));
slouken@12088
   334
slouken@12088
   335
    if (ctx->is_bluetooth) {
slouken@12088
   336
        data[0] = k_EPS4ReportIdBluetoothEffects;
slouken@12088
   337
        data[1] = 0xC0 | 0x04;  /* Magic value HID + CRC, also sets interval to 4ms for samples */
slouken@12088
   338
        data[3] = 0x03;  /* 0x1 is rumble, 0x2 is lightbar, 0x4 is the blink interval */
slouken@12088
   339
slouken@12088
   340
        report_size = 78;
slouken@12088
   341
        offset = 6;
slouken@12088
   342
    } else {
slouken@12088
   343
        data[0] = k_EPS4ReportIdUsbEffects;
slouken@12088
   344
        data[1] = 0x07;  /* Magic value */
slouken@12088
   345
slouken@12088
   346
        report_size = 32;
slouken@12088
   347
        offset = 4;
slouken@12088
   348
    }
slouken@12088
   349
    effects = (DS4EffectsState_t *)&data[offset];
slouken@12088
   350
slouken@12088
   351
    effects->ucRumbleLeft = (low_frequency_rumble >> 8);
slouken@12088
   352
    effects->ucRumbleRight = (high_frequency_rumble >> 8);
slouken@12088
   353
slouken@12088
   354
    effects->ucLedRed = 0;
slouken@12088
   355
    effects->ucLedGreen = 0;
slouken@12088
   356
    effects->ucLedBlue = 80;
slouken@12088
   357
slouken@12088
   358
    if (ctx->audio_supported) {
slouken@12088
   359
        Uint32 now = SDL_GetTicks();
slouken@12088
   360
        if (!ctx->last_volume_check ||
slouken@12088
   361
            SDL_TICKS_PASSED(now, ctx->last_volume_check + VOLUME_CHECK_INTERVAL_MS)) {
slouken@12088
   362
            ctx->volume = GetPlaystationVolumeFromFloat(GetSystemVolume());
slouken@12088
   363
            ctx->last_volume_check = now;
slouken@12088
   364
        }
slouken@12088
   365
slouken@12088
   366
        effects->ucVolumeRight = ctx->volume;
slouken@12088
   367
        effects->ucVolumeLeft = ctx->volume;
slouken@12088
   368
        effects->ucVolumeSpeaker = ctx->volume;
slouken@12088
   369
        effects->ucVolumeMic = 0xFF;
slouken@12088
   370
    }
slouken@12088
   371
slouken@12088
   372
    if (ctx->is_bluetooth) {
slouken@12088
   373
        /* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
slouken@12088
   374
        Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
slouken@12088
   375
        Uint32 unCRC;
slouken@12088
   376
        unCRC = crc32(0, &ubHdr, 1);
slouken@12088
   377
        unCRC = crc32(unCRC, data, (Uint32)(report_size - sizeof(unCRC)));
slouken@12088
   378
        SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
slouken@12088
   379
    }
slouken@12088
   380
slouken@12088
   381
    if (hid_write(dev, data, report_size) != report_size) {
slouken@12088
   382
        return SDL_SetError("Couldn't send rumble packet");
slouken@12088
   383
    }
slouken@12088
   384
slouken@12088
   385
    if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
slouken@12088
   386
        ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
slouken@12088
   387
    } else {
slouken@12088
   388
        ctx->rumble_expiration = 0;
slouken@12088
   389
    }
slouken@12088
   390
    return 0;
slouken@12088
   391
}
slouken@12088
   392
slouken@12088
   393
static void
slouken@12088
   394
HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS4_Context *ctx, PS4StatePacket_t *packet)
slouken@12088
   395
{
slouken@12088
   396
    Sint16 axis;
slouken@12088
   397
slouken@12088
   398
    if (ctx->last_state.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
slouken@12088
   399
        {
slouken@12088
   400
            Uint8 data = (packet->rgucButtonsHatAndCounter[0] >> 4);
slouken@12088
   401
slouken@12088
   402
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   403
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   404
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   405
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   406
        }
slouken@12088
   407
        {
slouken@12088
   408
            Uint8 data = (packet->rgucButtonsHatAndCounter[0] & 0x0F);
slouken@12088
   409
            SDL_bool dpad_up = SDL_FALSE;
slouken@12088
   410
            SDL_bool dpad_down = SDL_FALSE;
slouken@12088
   411
            SDL_bool dpad_left = SDL_FALSE;
slouken@12088
   412
            SDL_bool dpad_right = SDL_FALSE;
slouken@12088
   413
slouken@12088
   414
            switch (data) {
slouken@12088
   415
            case 0:
slouken@12088
   416
                dpad_up = SDL_TRUE;
slouken@12088
   417
                break;
slouken@12088
   418
            case 1:
slouken@12088
   419
                dpad_up = SDL_TRUE;
slouken@12088
   420
                dpad_right = SDL_TRUE;
slouken@12088
   421
                break;
slouken@12088
   422
            case 2:
slouken@12088
   423
                dpad_right = SDL_TRUE;
slouken@12088
   424
                break;
slouken@12088
   425
            case 3:
slouken@12088
   426
                dpad_right = SDL_TRUE;
slouken@12088
   427
                dpad_down = SDL_TRUE;
slouken@12088
   428
                break;
slouken@12088
   429
            case 4:
slouken@12088
   430
                dpad_down = SDL_TRUE;
slouken@12088
   431
                break;
slouken@12088
   432
            case 5:
slouken@12088
   433
                dpad_left = SDL_TRUE;
slouken@12088
   434
                dpad_down = SDL_TRUE;
slouken@12088
   435
                break;
slouken@12088
   436
            case 6:
slouken@12088
   437
                dpad_left = SDL_TRUE;
slouken@12088
   438
                break;
slouken@12088
   439
            case 7:
slouken@12088
   440
                dpad_up = SDL_TRUE;
slouken@12088
   441
                dpad_left = SDL_TRUE;
slouken@12088
   442
                break;
slouken@12088
   443
            default:
slouken@12088
   444
                break;
slouken@12088
   445
            }
slouken@12088
   446
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
slouken@12088
   447
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
slouken@12088
   448
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
slouken@12088
   449
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
slouken@12088
   450
        }
slouken@12088
   451
    }
slouken@12088
   452
slouken@12088
   453
    if (ctx->last_state.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
slouken@12088
   454
        Uint8 data = packet->rgucButtonsHatAndCounter[1];
slouken@12088
   455
slouken@12088
   456
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   457
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   458
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   459
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   460
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   461
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   462
    }
slouken@12088
   463
slouken@12088
   464
    if (ctx->last_state.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
slouken@12088
   465
        Uint8 data = (packet->rgucButtonsHatAndCounter[2] & 0x03);
slouken@12088
   466
slouken@12088
   467
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   468
    }
slouken@12088
   469
slouken@12088
   470
    axis = ((int)packet->ucTriggerLeft * 257) - 32768;
slouken@12088
   471
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
slouken@12088
   472
    axis = ((int)packet->ucTriggerRight * 257) - 32768;
slouken@12088
   473
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
slouken@12088
   474
    axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
slouken@12088
   475
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
slouken@12088
   476
    axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
slouken@12088
   477
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
slouken@12088
   478
    axis = ((int)packet->ucRightJoystickX * 257) - 32768;
slouken@12088
   479
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
slouken@12088
   480
    axis = ((int)packet->ucRightJoystickY * 257) - 32768;
slouken@12088
   481
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
slouken@12088
   482
slouken@12088
   483
    if (packet->ucBatteryLevel & 0x10) {
slouken@12088
   484
        joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
slouken@12088
   485
    } else {
slouken@12088
   486
        /* Battery level ranges from 0 to 10 */
slouken@12088
   487
        int level = (packet->ucBatteryLevel & 0xF);
slouken@12088
   488
        if (level == 0) {
slouken@12088
   489
            joystick->epowerlevel = SDL_JOYSTICK_POWER_EMPTY;
slouken@12088
   490
        } else if (level <= 2) {
slouken@12088
   491
            joystick->epowerlevel = SDL_JOYSTICK_POWER_LOW;
slouken@12088
   492
        } else if (level <= 7) {
slouken@12088
   493
            joystick->epowerlevel = SDL_JOYSTICK_POWER_MEDIUM;
slouken@12088
   494
        } else {
slouken@12088
   495
            joystick->epowerlevel = SDL_JOYSTICK_POWER_FULL;
slouken@12088
   496
        }
slouken@12088
   497
    }
slouken@12088
   498
slouken@12088
   499
    SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
slouken@12088
   500
}
slouken@12088
   501
slouken@12111
   502
static SDL_bool
slouken@12088
   503
HIDAPI_DriverPS4_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
slouken@12088
   504
{
slouken@12088
   505
    SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context;
slouken@12088
   506
    Uint8 data[USB_PACKET_LENGTH];
slouken@12088
   507
    int size;
slouken@12088
   508
slouken@12088
   509
    while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
slouken@12088
   510
        switch (data[0]) {
slouken@12088
   511
        case k_EPS4ReportIdUsbState:
slouken@12088
   512
            HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[1]);
slouken@12088
   513
            break;
slouken@12088
   514
        case k_EPS4ReportIdBluetoothState:
slouken@12088
   515
            /* Bluetooth state packets have two additional bytes at the beginning */
slouken@12088
   516
            HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[3]);
slouken@12088
   517
            break;
slouken@12088
   518
        default:
slouken@12088
   519
#ifdef DEBUG_JOYSTICK
slouken@12088
   520
            SDL_Log("Unknown PS4 packet: 0x%.2x\n", data[0]);
slouken@12088
   521
#endif
slouken@12088
   522
            break;
slouken@12088
   523
        }
slouken@12088
   524
    }
slouken@12088
   525
slouken@12088
   526
    if (ctx->rumble_expiration) {
slouken@12088
   527
        Uint32 now = SDL_GetTicks();
slouken@12088
   528
        if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
slouken@12088
   529
            HIDAPI_DriverPS4_Rumble(joystick, dev, context, 0, 0, 0);
slouken@12088
   530
        }
slouken@12088
   531
    }
slouken@12111
   532
slouken@12163
   533
    return (size >= 0);
slouken@12088
   534
}
slouken@12088
   535
slouken@12088
   536
static void
slouken@12088
   537
HIDAPI_DriverPS4_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
slouken@12088
   538
{
slouken@12088
   539
    SDL_free(context);
slouken@12088
   540
}
slouken@12088
   541
slouken@12088
   542
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4 =
slouken@12088
   543
{
slouken@12088
   544
    SDL_HINT_JOYSTICK_HIDAPI_PS4,
slouken@12088
   545
    SDL_TRUE,
slouken@12088
   546
    HIDAPI_DriverPS4_IsSupportedDevice,
slouken@12088
   547
    HIDAPI_DriverPS4_GetDeviceName,
slouken@12088
   548
    HIDAPI_DriverPS4_Init,
slouken@12088
   549
    HIDAPI_DriverPS4_Rumble,
slouken@12088
   550
    HIDAPI_DriverPS4_Update,
slouken@12088
   551
    HIDAPI_DriverPS4_Quit
slouken@12088
   552
};
slouken@12088
   553
slouken@12088
   554
#endif /* SDL_JOYSTICK_HIDAPI_PS4 */
slouken@12088
   555
slouken@12088
   556
#endif /* SDL_JOYSTICK_HIDAPI */
slouken@12088
   557
slouken@12088
   558
/* vi: set ts=4 sw=4 expandtab: */