src/joystick/bsd/SDL_sysjoystick.c
author Ozkan Sezer <sezeroz@gmail.com>
Thu, 01 Nov 2018 12:35:00 +0300
changeset 12378 61b267339239
parent 12359 691c32a30fb9
child 12503 806492103856
permissions -rw-r--r--
fix NetBSD C90 build failure

src/vendor/SDL2/src/joystick/bsd/SDL_sysjoystick.c:353:5: error:
ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

reported by 'bch' at https://discourse.libsdl.org/t/25231
slouken@278
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@278
     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@278
     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@278
    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@278
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@278
    22
slouken@1635
    23
#ifdef SDL_JOYSTICK_USBHID
slouken@1635
    24
slouken@278
    25
/*
slouken@278
    26
 * Joystick driver for the uhid(4) interface found in OpenBSD,
slouken@278
    27
 * NetBSD and FreeBSD.
slouken@278
    28
 *
slouken@278
    29
 * Maintainer: <vedge at csoft.org>
slouken@278
    30
 */
slouken@278
    31
slouken@552
    32
#include <sys/param.h>
slouken@552
    33
slouken@278
    34
#include <unistd.h>
slouken@278
    35
#include <fcntl.h>
slouken@278
    36
#include <errno.h>
slouken@278
    37
slouken@3367
    38
#ifndef __FreeBSD_kernel_version
slouken@3367
    39
#define __FreeBSD_kernel_version __FreeBSD_version
slouken@3367
    40
#endif
slouken@3367
    41
slouken@403
    42
#if defined(HAVE_USB_H)
slouken@403
    43
#include <usb.h>
slouken@403
    44
#endif
slouken@1565
    45
#ifdef __DragonFly__
slouken@1565
    46
#include <bus/usb/usb.h>
slouken@1565
    47
#include <bus/usb/usbhid.h>
slouken@1565
    48
#else
slouken@278
    49
#include <dev/usb/usb.h>
slouken@278
    50
#include <dev/usb/usbhid.h>
slouken@1565
    51
#endif
slouken@358
    52
slouken@381
    53
#if defined(HAVE_USBHID_H)
slouken@381
    54
#include <usbhid.h>
slouken@381
    55
#elif defined(HAVE_LIBUSB_H)
slouken@381
    56
#include <libusb.h>
slouken@381
    57
#elif defined(HAVE_LIBUSBHID_H)
slouken@381
    58
#include <libusbhid.h>
slouken@358
    59
#endif
slouken@278
    60
slouken@4944
    61
#if defined(__FREEBSD__) || defined(__FreeBSD_kernel__)
slouken@1565
    62
#ifndef __DragonFly__
slouken@611
    63
#include <osreldate.h>
slouken@1565
    64
#endif
icculus@6080
    65
#if __FreeBSD_kernel_version > 800063
icculus@6080
    66
#include <dev/usb/usb_ioctl.h>
icculus@6080
    67
#endif
slouken@776
    68
#include <sys/joystick.h>
slouken@776
    69
#endif
slouken@776
    70
slouken@1565
    71
#if SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
slouken@776
    72
#include <machine/joystick.h>
slouken@611
    73
#endif
slouken@611
    74
slouken@278
    75
#include "SDL_joystick.h"
slouken@1361
    76
#include "../SDL_sysjoystick.h"
slouken@1361
    77
#include "../SDL_joystick_c.h"
slouken@278
    78
icculus@9986
    79
#define MAX_UHID_JOYS   64
slouken@7191
    80
#define MAX_JOY_JOYS    2
slouken@7191
    81
#define MAX_JOYS    (MAX_UHID_JOYS + MAX_JOY_JOYS)
slouken@278
    82
slouken@3385
    83
slouken@1895
    84
struct report
slouken@1895
    85
{
slouken@6963
    86
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000)
slouken@6963
    87
    void *buf; /* Buffer */
slouken@6963
    88
#elif defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063)
icculus@6080
    89
    struct usb_gen_descriptor *buf; /* Buffer */
icculus@6080
    90
#else
slouken@1895
    91
    struct usb_ctl_report *buf; /* Buffer */
icculus@6080
    92
#endif
slouken@1895
    93
    size_t size;                /* Buffer size */
slouken@1895
    94
    int rid;                    /* Report ID */
slouken@1895
    95
    enum
slouken@1895
    96
    {
slouken@1895
    97
        SREPORT_UNINIT,
slouken@1895
    98
        SREPORT_CLEAN,
slouken@1895
    99
        SREPORT_DIRTY
slouken@1895
   100
    } status;
slouken@278
   101
};
slouken@278
   102
slouken@1895
   103
static struct
slouken@1895
   104
{
slouken@1895
   105
    int uhid_report;
slouken@1895
   106
    hid_kind_t kind;
slouken@1895
   107
    const char *name;
slouken@278
   108
} const repinfo[] = {
slouken@1895
   109
    {UHID_INPUT_REPORT, hid_input, "input"},
slouken@1895
   110
    {UHID_OUTPUT_REPORT, hid_output, "output"},
slouken@1895
   111
    {UHID_FEATURE_REPORT, hid_feature, "feature"}
slouken@278
   112
};
slouken@307
   113
slouken@1895
   114
enum
slouken@1895
   115
{
slouken@1895
   116
    REPORT_INPUT = 0,
slouken@1895
   117
    REPORT_OUTPUT = 1,
slouken@1895
   118
    REPORT_FEATURE = 2
slouken@307
   119
};
slouken@307
   120
slouken@1895
   121
enum
slouken@1895
   122
{
slouken@1895
   123
    JOYAXE_X,
slouken@1895
   124
    JOYAXE_Y,
slouken@1895
   125
    JOYAXE_Z,
slouken@1895
   126
    JOYAXE_SLIDER,
slouken@1895
   127
    JOYAXE_WHEEL,
slouken@1895
   128
    JOYAXE_RX,
slouken@1895
   129
    JOYAXE_RY,
slouken@1895
   130
    JOYAXE_RZ,
slouken@1895
   131
    JOYAXE_count
slouken@307
   132
};
slouken@278
   133
slouken@1895
   134
struct joystick_hwdata
slouken@1895
   135
{
slouken@1895
   136
    int fd;
slouken@1895
   137
    char *path;
slouken@1895
   138
    enum
slouken@1895
   139
    {
slouken@1895
   140
        BSDJOY_UHID,            /* uhid(4) */
slouken@1895
   141
        BSDJOY_JOY              /* joy(4) */
slouken@1895
   142
    } type;
slouken@1895
   143
    struct report_desc *repdesc;
slouken@1895
   144
    struct report inreport;
slouken@1895
   145
    int axis_map[JOYAXE_count]; /* map present JOYAXE_* to 0,1,.. */
slouken@278
   146
};
slouken@278
   147
slouken@278
   148
static char *joynames[MAX_JOYS];
slouken@278
   149
static char *joydevnames[MAX_JOYS];
slouken@278
   150
slouken@1895
   151
static int report_alloc(struct report *, struct report_desc *, int);
slouken@1895
   152
static void report_free(struct report *);
slouken@278
   153
slouken@6963
   154
#if defined(USBHID_UCR_DATA) || (defined(__FreeBSD_kernel__) && __FreeBSD_kernel_version <= 800063)
slouken@381
   155
#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
slouken@6963
   156
#elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000))
slouken@6963
   157
#define REP_BUF_DATA(rep) ((rep)->buf)
icculus@6080
   158
#elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063))
icculus@6080
   159
#define REP_BUF_DATA(rep) ((rep)->buf->ugd_data)
slouken@381
   160
#else
slouken@381
   161
#define REP_BUF_DATA(rep) ((rep)->buf->data)
slouken@381
   162
#endif
slouken@381
   163
icculus@12105
   164
static int numjoysticks = 0;
slouken@6697
   165
icculus@12106
   166
static int BSD_JoystickOpen(SDL_Joystick * joy, int device_index);
icculus@12106
   167
static void BSD_JoystickClose(SDL_Joystick * joy);
icculus@12106
   168
icculus@12105
   169
static int
icculus@12105
   170
BSD_JoystickInit(void)
slouken@278
   171
{
slouken@1895
   172
    char s[16];
slouken@1895
   173
    int i, fd;
slouken@278
   174
icculus@12105
   175
    numjoysticks = 0;
slouken@278
   176
slouken@1895
   177
    SDL_memset(joynames, 0, sizeof(joynames));
slouken@1895
   178
    SDL_memset(joydevnames, 0, sizeof(joydevnames));
slouken@278
   179
slouken@1895
   180
    for (i = 0; i < MAX_UHID_JOYS; i++) {
slouken@1895
   181
        SDL_Joystick nj;
slouken@544
   182
slouken@1895
   183
        SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
slouken@544
   184
icculus@12105
   185
        joynames[numjoysticks] = SDL_strdup(s);
slouken@544
   186
icculus@12105
   187
        if (BSD_JoystickOpen(&nj, numjoysticks) == 0) {
icculus@12105
   188
            BSD_JoystickClose(&nj);
icculus@12105
   189
            numjoysticks++;
slouken@1895
   190
        } else {
icculus@12105
   191
            SDL_free(joynames[numjoysticks]);
icculus@12105
   192
            joynames[numjoysticks] = NULL;
slouken@1895
   193
        }
slouken@1895
   194
    }
slouken@1895
   195
    for (i = 0; i < MAX_JOY_JOYS; i++) {
slouken@1895
   196
        SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
slouken@1895
   197
        fd = open(s, O_RDONLY);
slouken@1895
   198
        if (fd != -1) {
icculus@12105
   199
            joynames[numjoysticks++] = SDL_strdup(s);
slouken@1895
   200
            close(fd);
slouken@1895
   201
        }
slouken@1895
   202
    }
slouken@278
   203
slouken@1895
   204
    /* Read the default USB HID usage table. */
slouken@1895
   205
    hid_init(NULL);
slouken@278
   206
icculus@12105
   207
    return (numjoysticks);
slouken@278
   208
}
slouken@278
   209
icculus@12105
   210
static int
icculus@12105
   211
BSD_JoystickGetCount(void)
slouken@6707
   212
{
icculus@12105
   213
    return numjoysticks;
slouken@6707
   214
}
slouken@6707
   215
icculus@12105
   216
static void
icculus@12105
   217
BSD_JoystickDetect(void)
slouken@6707
   218
{
slouken@6707
   219
}
slouken@6707
   220
icculus@12105
   221
static const char *
icculus@12105
   222
BSD_JoystickGetDeviceName(int device_index)
slouken@278
   223
{
slouken@6707
   224
    if (joydevnames[device_index] != NULL) {
slouken@6707
   225
        return (joydevnames[device_index]);
slouken@1895
   226
    }
slouken@6707
   227
    return (joynames[device_index]);
slouken@6707
   228
}
slouken@6707
   229
slouken@12359
   230
static int
slouken@12359
   231
BSD_JoystickGetDevicePlayerIndex(int device_index)
slouken@12359
   232
{
slouken@12359
   233
    return -1;
slouken@12359
   234
}
slouken@12359
   235
slouken@6707
   236
/* Function to perform the mapping from device index to the instance id for this index */
icculus@12105
   237
static SDL_JoystickID
icculus@12105
   238
BSD_JoystickGetDeviceInstanceID(int device_index)
slouken@6707
   239
{
slouken@6707
   240
    return device_index;
slouken@278
   241
}
slouken@278
   242
slouken@632
   243
static int
slouken@632
   244
usage_to_joyaxe(unsigned usage)
slouken@632
   245
{
slouken@632
   246
    int joyaxe;
slouken@632
   247
    switch (usage) {
slouken@632
   248
    case HUG_X:
slouken@1895
   249
        joyaxe = JOYAXE_X;
slouken@1895
   250
        break;
slouken@632
   251
    case HUG_Y:
slouken@1895
   252
        joyaxe = JOYAXE_Y;
slouken@1895
   253
        break;
slouken@632
   254
    case HUG_Z:
slouken@1895
   255
        joyaxe = JOYAXE_Z;
slouken@1895
   256
        break;
slouken@632
   257
    case HUG_SLIDER:
slouken@1895
   258
        joyaxe = JOYAXE_SLIDER;
slouken@1895
   259
        break;
slouken@632
   260
    case HUG_WHEEL:
slouken@1895
   261
        joyaxe = JOYAXE_WHEEL;
slouken@1895
   262
        break;
slouken@632
   263
    case HUG_RX:
slouken@1895
   264
        joyaxe = JOYAXE_RX;
slouken@1895
   265
        break;
slouken@632
   266
    case HUG_RY:
slouken@1895
   267
        joyaxe = JOYAXE_RY;
slouken@1895
   268
        break;
slouken@632
   269
    case HUG_RZ:
slouken@1895
   270
        joyaxe = JOYAXE_RZ;
slouken@1895
   271
        break;
slouken@632
   272
    default:
slouken@1895
   273
        joyaxe = -1;
slouken@632
   274
    }
slouken@1895
   275
    return joyaxe;
slouken@632
   276
}
slouken@632
   277
slouken@632
   278
static unsigned
slouken@632
   279
hatval_to_sdl(Sint32 hatval)
slouken@632
   280
{
slouken@632
   281
    static const unsigned hat_dir_map[8] = {
slouken@1895
   282
        SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN,
slouken@1895
   283
        SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
slouken@632
   284
    };
slouken@632
   285
    unsigned result;
slouken@1895
   286
    if ((hatval & 7) == hatval)
slouken@1895
   287
        result = hat_dir_map[hatval];
slouken@1895
   288
    else
slouken@1895
   289
        result = SDL_HAT_CENTERED;
slouken@632
   290
    return result;
slouken@632
   291
}
slouken@632
   292
slouken@632
   293
icculus@12105
   294
static int
icculus@12105
   295
BSD_JoystickOpen(SDL_Joystick * joy, int device_index)
slouken@278
   296
{
slouken@6697
   297
    char *path = joynames[device_index];
slouken@1895
   298
    struct joystick_hwdata *hw;
slouken@1895
   299
    struct hid_item hitem;
slouken@1895
   300
    struct hid_data *hdata;
icculus@9986
   301
    struct report *rep = NULL;
sezeroz@12378
   302
#if defined(__NetBSD__)
sezeroz@12378
   303
    usb_device_descriptor_t udd;
sezeroz@12378
   304
    struct usb_string_desc usd;
sezeroz@12378
   305
#endif
slouken@1895
   306
    int fd;
slouken@1895
   307
    int i;
slouken@278
   308
slouken@1895
   309
    fd = open(path, O_RDONLY);
slouken@1895
   310
    if (fd == -1) {
icculus@7037
   311
        return SDL_SetError("%s: %s", path, strerror(errno));
slouken@1895
   312
    }
slouken@278
   313
slouken@6698
   314
    joy->instance_id = device_index;
slouken@1895
   315
    hw = (struct joystick_hwdata *)
slouken@1895
   316
        SDL_malloc(sizeof(struct joystick_hwdata));
slouken@1895
   317
    if (hw == NULL) {
slouken@1895
   318
        close(fd);
icculus@7037
   319
        return SDL_OutOfMemory();
slouken@1895
   320
    }
slouken@1895
   321
    joy->hwdata = hw;
slouken@1895
   322
    hw->fd = fd;
philipp@10187
   323
    hw->path = SDL_strdup(path);
slouken@1895
   324
    if (!SDL_strncmp(path, "/dev/joy", 8)) {
slouken@1895
   325
        hw->type = BSDJOY_JOY;
slouken@1895
   326
        joy->naxes = 2;
slouken@1895
   327
        joy->nbuttons = 2;
slouken@1895
   328
        joy->nhats = 0;
slouken@1895
   329
        joy->nballs = 0;
philipp@10187
   330
        joydevnames[device_index] = SDL_strdup("Gameport joystick");
slouken@1895
   331
        goto usbend;
slouken@1895
   332
    } else {
slouken@1895
   333
        hw->type = BSDJOY_UHID;
slouken@1895
   334
    }
slouken@776
   335
slouken@1895
   336
    {
slouken@1895
   337
        int ax;
slouken@1895
   338
        for (ax = 0; ax < JOYAXE_count; ax++)
slouken@1895
   339
            hw->axis_map[ax] = -1;
slouken@1895
   340
    }
slouken@1895
   341
    hw->repdesc = hid_get_report_desc(fd);
slouken@1895
   342
    if (hw->repdesc == NULL) {
slouken@1895
   343
        SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
slouken@1895
   344
                     strerror(errno));
slouken@1895
   345
        goto usberr;
slouken@1895
   346
    }
icculus@3703
   347
    rep = &hw->inreport;
slouken@4537
   348
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063) || defined(__FreeBSD_kernel__)
slouken@3385
   349
    rep->rid = hid_get_report_id(fd);
slouken@3385
   350
    if (rep->rid < 0) {
slouken@3385
   351
#else
slouken@1895
   352
    if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
slouken@3385
   353
#endif
slouken@1895
   354
        rep->rid = -1;          /* XXX */
slouken@1895
   355
    }
icculus@9986
   356
#if defined(__NetBSD__)
icculus@9986
   357
    if (ioctl(fd, USB_GET_DEVICE_DESC, &udd) == -1)
icculus@9986
   358
        goto desc_failed;
icculus@9986
   359
icculus@9986
   360
    /* Get default language */
icculus@9986
   361
    usd.usd_string_index = USB_LANGUAGE_TABLE;
icculus@9986
   362
    usd.usd_language_id = 0;
icculus@9986
   363
    if (ioctl(fd, USB_GET_STRING_DESC, &usd) == -1 || usd.usd_desc.bLength < 4) {
icculus@9986
   364
        usd.usd_language_id = 0;
icculus@9986
   365
    } else {
icculus@9986
   366
        usd.usd_language_id = UGETW(usd.usd_desc.bString[0]);
icculus@9986
   367
    }
icculus@9986
   368
icculus@9986
   369
    usd.usd_string_index = udd.iProduct;
icculus@9986
   370
    if (ioctl(fd, USB_GET_STRING_DESC, &usd) == 0) {
icculus@9986
   371
        char str[128];
icculus@9986
   372
        char *new_name = NULL;
icculus@9986
   373
        int i;
icculus@9986
   374
        for (i = 0; i < (usd.usd_desc.bLength >> 1) - 1 && i < sizeof(str) - 1; i++) {
icculus@9986
   375
            str[i] = UGETW(usd.usd_desc.bString[i]);
icculus@9986
   376
        }
icculus@9986
   377
        str[i] = '\0';
icculus@9986
   378
        asprintf(&new_name, "%s @ %s", str, path);
icculus@9986
   379
        if (new_name != NULL) {
icculus@12105
   380
            SDL_free(joydevnames[numjoysticks]);
icculus@12105
   381
            joydevnames[numjoysticks] = new_name;
icculus@9986
   382
        }
icculus@9986
   383
    }
icculus@9986
   384
desc_failed:
icculus@9986
   385
#endif
slouken@1895
   386
    if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
slouken@1895
   387
        goto usberr;
slouken@1895
   388
    }
slouken@1895
   389
    if (rep->size <= 0) {
slouken@1895
   390
        SDL_SetError("%s: Input report descriptor has invalid length",
slouken@1895
   391
                     hw->path);
slouken@1895
   392
        goto usberr;
slouken@1895
   393
    }
slouken@4537
   394
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
slouken@1895
   395
    hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
slouken@1895
   396
#else
slouken@1895
   397
    hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
slouken@1895
   398
#endif
slouken@1895
   399
    if (hdata == NULL) {
slouken@1895
   400
        SDL_SetError("%s: Cannot start HID parser", hw->path);
slouken@1895
   401
        goto usberr;
slouken@1895
   402
    }
slouken@1895
   403
    joy->naxes = 0;
slouken@1895
   404
    joy->nbuttons = 0;
slouken@1895
   405
    joy->nhats = 0;
slouken@1895
   406
    joy->nballs = 0;
slouken@1895
   407
    for (i = 0; i < JOYAXE_count; i++)
slouken@1895
   408
        hw->axis_map[i] = -1;
slouken@278
   409
slouken@1895
   410
    while (hid_get_item(hdata, &hitem) > 0) {
slouken@1895
   411
        char *sp;
slouken@1895
   412
        const char *s;
slouken@278
   413
slouken@1895
   414
        switch (hitem.kind) {
slouken@1895
   415
        case hid_collection:
slouken@1895
   416
            switch (HID_PAGE(hitem.usage)) {
slouken@1895
   417
            case HUP_GENERIC_DESKTOP:
slouken@1895
   418
                switch (HID_USAGE(hitem.usage)) {
slouken@1895
   419
                case HUG_JOYSTICK:
slouken@1895
   420
                case HUG_GAME_PAD:
slouken@1895
   421
                    s = hid_usage_in_page(hitem.usage);
slouken@1895
   422
                    sp = SDL_malloc(SDL_strlen(s) + 5);
slouken@1895
   423
                    SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)",
slouken@6700
   424
                                 s, device_index);
slouken@6700
   425
                    joydevnames[device_index] = sp;
slouken@1895
   426
                }
slouken@1895
   427
            }
slouken@1895
   428
            break;
slouken@1895
   429
        case hid_input:
slouken@1895
   430
            switch (HID_PAGE(hitem.usage)) {
slouken@1895
   431
            case HUP_GENERIC_DESKTOP:
slouken@1895
   432
                {
slouken@1895
   433
                    unsigned usage = HID_USAGE(hitem.usage);
slouken@1895
   434
                    int joyaxe = usage_to_joyaxe(usage);
slouken@1895
   435
                    if (joyaxe >= 0) {
slouken@1895
   436
                        hw->axis_map[joyaxe] = 1;
slouken@1895
   437
                    } else if (usage == HUG_HAT_SWITCH) {
slouken@1895
   438
                        joy->nhats++;
slouken@1895
   439
                    }
slouken@1895
   440
                    break;
slouken@1895
   441
                }
slouken@1895
   442
            case HUP_BUTTON:
slouken@1895
   443
                joy->nbuttons++;
slouken@1895
   444
                break;
slouken@1895
   445
            default:
slouken@1895
   446
                break;
slouken@1895
   447
            }
slouken@1895
   448
            break;
slouken@1895
   449
        default:
slouken@1895
   450
            break;
slouken@1895
   451
        }
slouken@1895
   452
    }
slouken@1895
   453
    hid_end_parse(hdata);
slouken@1895
   454
    for (i = 0; i < JOYAXE_count; i++)
slouken@1895
   455
        if (hw->axis_map[i] > 0)
slouken@1895
   456
            hw->axis_map[i] = joy->naxes++;
slouken@278
   457
icculus@9986
   458
    if (joy->naxes == 0 && joy->nbuttons == 0 && joy->nhats == 0 && joy->nballs == 0) {
icculus@9986
   459
        SDL_SetError("%s: Not a joystick, ignoring", hw->path);
icculus@9986
   460
        goto usberr;
icculus@9986
   461
    }
icculus@9986
   462
slouken@1895
   463
  usbend:
slouken@1895
   464
    /* The poll blocks the event thread. */
slouken@1895
   465
    fcntl(fd, F_SETFL, O_NONBLOCK);
icculus@9986
   466
#ifdef __NetBSD__
icculus@9986
   467
    /* Flush pending events */
icculus@9986
   468
    if (rep) {
icculus@9986
   469
        while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size)
icculus@9986
   470
            ;
icculus@9986
   471
    }
icculus@9986
   472
#endif
slouken@278
   473
slouken@1895
   474
    return (0);
slouken@1895
   475
  usberr:
slouken@1895
   476
    close(hw->fd);
slouken@1895
   477
    SDL_free(hw->path);
slouken@1895
   478
    SDL_free(hw);
slouken@1895
   479
    return (-1);
slouken@278
   480
}
slouken@278
   481
icculus@12105
   482
static void
icculus@12105
   483
BSD_JoystickUpdate(SDL_Joystick * joy)
slouken@278
   484
{
slouken@1895
   485
    struct hid_item hitem;
slouken@1895
   486
    struct hid_data *hdata;
slouken@1895
   487
    struct report *rep;
slouken@1895
   488
    int nbutton, naxe = -1;
slouken@1895
   489
    Sint32 v;
slouken@776
   490
slouken@4537
   491
#if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H || defined(__FreeBSD_kernel__)
slouken@1895
   492
    struct joystick gameport;
slouken@1895
   493
    static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
slouken@1895
   494
slouken@1895
   495
    if (joy->hwdata->type == BSDJOY_JOY) {
slouken@7733
   496
        while (read(joy->hwdata->fd, &gameport, sizeof gameport) == sizeof gameport) {
slouken@7733
   497
            if (abs(x - gameport.x) > 8) {
slouken@7733
   498
                x = gameport.x;
slouken@7733
   499
                if (x < xmin) {
slouken@7733
   500
                    xmin = x;
slouken@7733
   501
                }
slouken@7733
   502
                if (x > xmax) {
slouken@7733
   503
                    xmax = x;
slouken@7733
   504
                }
slouken@7733
   505
                if (xmin == xmax) {
slouken@7733
   506
                    xmin--;
slouken@7733
   507
                    xmax++;
slouken@7733
   508
                }
slouken@7733
   509
                v = (Sint32) x;
slouken@7733
   510
                v -= (xmax + xmin + 1) / 2;
slouken@7733
   511
                v *= 32768 / ((xmax - xmin + 1) / 2);
slouken@7733
   512
                SDL_PrivateJoystickAxis(joy, 0, v);
slouken@1895
   513
            }
slouken@7733
   514
            if (abs(y - gameport.y) > 8) {
slouken@7733
   515
                y = gameport.y;
slouken@7733
   516
                if (y < ymin) {
slouken@7733
   517
                    ymin = y;
slouken@7733
   518
                }
slouken@7733
   519
                if (y > ymax) {
slouken@7733
   520
                    ymax = y;
slouken@7733
   521
                }
slouken@7733
   522
                if (ymin == ymax) {
slouken@7733
   523
                    ymin--;
slouken@7733
   524
                    ymax++;
slouken@7733
   525
                }
slouken@7733
   526
                v = (Sint32) y;
slouken@7733
   527
                v -= (ymax + ymin + 1) / 2;
slouken@7733
   528
                v *= 32768 / ((ymax - ymin + 1) / 2);
slouken@7733
   529
                SDL_PrivateJoystickAxis(joy, 1, v);
slouken@1895
   530
            }
slouken@10745
   531
            SDL_PrivateJoystickButton(joy, 0, gameport.b1);
slouken@10745
   532
            SDL_PrivateJoystickButton(joy, 1, gameport.b2);
slouken@1895
   533
        }
slouken@1895
   534
        return;
slouken@1895
   535
    }
slouken@1565
   536
#endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
slouken@381
   537
slouken@1895
   538
    rep = &joy->hwdata->inreport;
slouken@1895
   539
slouken@7733
   540
    while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size) {
slouken@4537
   541
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
slouken@7733
   542
        hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
slouken@407
   543
#else
slouken@7733
   544
        hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
slouken@407
   545
#endif
slouken@7733
   546
        if (hdata == NULL) {
slouken@7733
   547
            /*fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);*/
slouken@7733
   548
            continue;
slouken@7733
   549
        }
slouken@278
   550
slouken@7733
   551
        for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
slouken@7733
   552
            switch (hitem.kind) {
slouken@7733
   553
            case hid_input:
slouken@7733
   554
                switch (HID_PAGE(hitem.usage)) {
slouken@7733
   555
                case HUP_GENERIC_DESKTOP:
slouken@7733
   556
                    {
slouken@7733
   557
                        unsigned usage = HID_USAGE(hitem.usage);
slouken@7733
   558
                        int joyaxe = usage_to_joyaxe(usage);
slouken@7733
   559
                        if (joyaxe >= 0) {
slouken@7733
   560
                            naxe = joy->hwdata->axis_map[joyaxe];
slouken@7733
   561
                            /* scaleaxe */
slouken@7733
   562
                            v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
slouken@7733
   563
                            v -= (hitem.logical_maximum +
slouken@7733
   564
                                  hitem.logical_minimum + 1) / 2;
slouken@7733
   565
                            v *= 32768 /
slouken@7733
   566
                                ((hitem.logical_maximum -
slouken@7733
   567
                                  hitem.logical_minimum + 1) / 2);
slouken@10745
   568
                            SDL_PrivateJoystickAxis(joy, naxe, v);
slouken@7733
   569
                        } else if (usage == HUG_HAT_SWITCH) {
slouken@7733
   570
                            v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
slouken@7733
   571
                            SDL_PrivateJoystickHat(joy, 0,
slouken@7733
   572
                                                   hatval_to_sdl(v) -
slouken@7733
   573
                                                   hitem.logical_minimum);
slouken@1895
   574
                        }
slouken@7733
   575
                        break;
slouken@1895
   576
                    }
slouken@7733
   577
                case HUP_BUTTON:
slouken@7733
   578
                    v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
slouken@10745
   579
                    SDL_PrivateJoystickButton(joy, nbutton, v);
slouken@7733
   580
                    nbutton++;
slouken@1895
   581
                    break;
slouken@7733
   582
                default:
slouken@7733
   583
                    continue;
slouken@1895
   584
                }
slouken@1895
   585
                break;
slouken@1895
   586
            default:
slouken@7733
   587
                break;
slouken@1895
   588
            }
slouken@1895
   589
        }
slouken@7733
   590
        hid_end_parse(hdata);
slouken@1895
   591
    }
slouken@278
   592
}
slouken@278
   593
slouken@278
   594
/* Function to close a joystick after use */
icculus@12105
   595
static void
icculus@12105
   596
BSD_JoystickClose(SDL_Joystick * joy)
slouken@278
   597
{
slouken@1895
   598
    if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
slouken@1895
   599
        report_free(&joy->hwdata->inreport);
slouken@1895
   600
        hid_dispose_report_desc(joy->hwdata->repdesc);
slouken@1895
   601
    }
slouken@1895
   602
    close(joy->hwdata->fd);
slouken@1895
   603
    SDL_free(joy->hwdata->path);
slouken@1895
   604
    SDL_free(joy->hwdata);
slouken@278
   605
}
slouken@278
   606
icculus@12105
   607
static void
icculus@12105
   608
BSD_JoystickQuit(void)
slouken@278
   609
{
slouken@1895
   610
    int i;
slouken@278
   611
slouken@1895
   612
    for (i = 0; i < MAX_JOYS; i++) {
slouken@7719
   613
        SDL_free(joynames[i]);
slouken@7719
   614
        SDL_free(joydevnames[i]);
slouken@1895
   615
    }
slouken@278
   616
slouken@1895
   617
    return;
slouken@278
   618
}
slouken@278
   619
icculus@12105
   620
static SDL_JoystickGUID
icculus@12105
   621
BSD_JoystickGetDeviceGUID( int device_index )
slouken@6697
   622
{
slouken@6744
   623
    SDL_JoystickGUID guid;
slouken@7191
   624
    /* the GUID is just the first 16 chars of the name for now */
icculus@12106
   625
    const char *name = BSD_JoystickGetDeviceName( device_index );
slouken@6697
   626
    SDL_zero( guid );
slouken@6697
   627
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6697
   628
    return guid;
slouken@6697
   629
}
slouken@6697
   630
slouken@278
   631
static int
slouken@278
   632
report_alloc(struct report *r, struct report_desc *rd, int repind)
slouken@278
   633
{
slouken@1895
   634
    int len;
slouken@278
   635
slouken@1565
   636
#ifdef __DragonFly__
slouken@1895
   637
    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@1565
   638
#elif __FREEBSD__
slouken@3366
   639
# if (__FreeBSD_kernel_version >= 460000) || defined(__FreeBSD_kernel__)
slouken@3366
   640
#  if (__FreeBSD_kernel_version <= 500111)
slouken@1895
   641
    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@611
   642
#  else
slouken@1895
   643
    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@611
   644
#  endif
slouken@552
   645
# else
slouken@1895
   646
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@1565
   647
# endif
slouken@552
   648
#else
slouken@552
   649
# ifdef USBHID_NEW
slouken@1895
   650
    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@1565
   651
# else
slouken@1895
   652
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@552
   653
# endif
slouken@552
   654
#endif
slouken@552
   655
slouken@1895
   656
    if (len < 0) {
icculus@7037
   657
        return SDL_SetError("Negative HID report size");
slouken@1895
   658
    }
slouken@1895
   659
    r->size = len;
slouken@278
   660
slouken@1895
   661
    if (r->size > 0) {
slouken@6963
   662
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000)
slouken@6963
   663
        r->buf = SDL_malloc(r->size);
slouken@6963
   664
#else
slouken@1895
   665
        r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
slouken@1895
   666
                            r->size);
slouken@6963
   667
#endif
slouken@1895
   668
        if (r->buf == NULL) {
icculus@7037
   669
            return SDL_OutOfMemory();
slouken@1895
   670
        }
slouken@1895
   671
    } else {
slouken@1895
   672
        r->buf = NULL;
slouken@1895
   673
    }
slouken@278
   674
slouken@1895
   675
    r->status = SREPORT_CLEAN;
icculus@7037
   676
    return 0;
slouken@278
   677
}
slouken@278
   678
slouken@278
   679
static void
slouken@278
   680
report_free(struct report *r)
slouken@278
   681
{
slouken@7719
   682
    SDL_free(r->buf);
slouken@1895
   683
    r->status = SREPORT_UNINIT;
slouken@278
   684
}
slouken@278
   685
icculus@12105
   686
static int
icculus@12105
   687
BSD_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
icculus@12105
   688
{
icculus@12105
   689
    return SDL_Unsupported();
icculus@12105
   690
}
icculus@12105
   691
icculus@12105
   692
SDL_JoystickDriver SDL_BSD_JoystickDriver =
icculus@12105
   693
{
icculus@12105
   694
    BSD_JoystickInit,
icculus@12105
   695
    BSD_JoystickGetCount,
icculus@12105
   696
    BSD_JoystickDetect,
icculus@12105
   697
    BSD_JoystickGetDeviceName,
slouken@12359
   698
    BSD_JoystickGetDevicePlayerIndex,
icculus@12105
   699
    BSD_JoystickGetDeviceGUID,
icculus@12105
   700
    BSD_JoystickGetDeviceInstanceID,
icculus@12105
   701
    BSD_JoystickOpen,
icculus@12105
   702
    BSD_JoystickRumble,
icculus@12105
   703
    BSD_JoystickUpdate,
icculus@12105
   704
    BSD_JoystickClose,
icculus@12105
   705
    BSD_JoystickQuit,
icculus@12105
   706
};
icculus@12105
   707
slouken@1635
   708
#endif /* SDL_JOYSTICK_USBHID */
slouken@6697
   709
slouken@1895
   710
/* vi: set ts=4 sw=4 expandtab: */