src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 22:27:49 -0800
changeset 6698 28ab2ef7bfc9
parent 6697 dbda91031456
child 6700 c8f3d3f0251e
permissions -rw-r--r--
Completed adding new hotplug stubs for the joystick implementations
slouken@278
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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
*/
slouken@1402
    21
#include "SDL_config.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
slouken@6401
    79
#define MAX_UHID_JOYS	16
slouken@278
    80
#define MAX_JOY_JOYS	2
slouken@278
    81
#define MAX_JOYS	(MAX_UHID_JOYS + MAX_JOY_JOYS)
slouken@278
    82
slouken@3385
    83
slouken@1895
    84
struct report
slouken@1895
    85
{
icculus@6080
    86
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063)
icculus@6080
    87
    struct usb_gen_descriptor *buf; /* Buffer */
icculus@6080
    88
#else
slouken@1895
    89
    struct usb_ctl_report *buf; /* Buffer */
icculus@6080
    90
#endif
slouken@1895
    91
    size_t size;                /* Buffer size */
slouken@1895
    92
    int rid;                    /* Report ID */
slouken@1895
    93
    enum
slouken@1895
    94
    {
slouken@1895
    95
        SREPORT_UNINIT,
slouken@1895
    96
        SREPORT_CLEAN,
slouken@1895
    97
        SREPORT_DIRTY
slouken@1895
    98
    } status;
slouken@278
    99
};
slouken@278
   100
slouken@1895
   101
static struct
slouken@1895
   102
{
slouken@1895
   103
    int uhid_report;
slouken@1895
   104
    hid_kind_t kind;
slouken@1895
   105
    const char *name;
slouken@278
   106
} const repinfo[] = {
slouken@1895
   107
    {UHID_INPUT_REPORT, hid_input, "input"},
slouken@1895
   108
    {UHID_OUTPUT_REPORT, hid_output, "output"},
slouken@1895
   109
    {UHID_FEATURE_REPORT, hid_feature, "feature"}
slouken@278
   110
};
slouken@307
   111
slouken@1895
   112
enum
slouken@1895
   113
{
slouken@1895
   114
    REPORT_INPUT = 0,
slouken@1895
   115
    REPORT_OUTPUT = 1,
slouken@1895
   116
    REPORT_FEATURE = 2
slouken@307
   117
};
slouken@307
   118
slouken@1895
   119
enum
slouken@1895
   120
{
slouken@1895
   121
    JOYAXE_X,
slouken@1895
   122
    JOYAXE_Y,
slouken@1895
   123
    JOYAXE_Z,
slouken@1895
   124
    JOYAXE_SLIDER,
slouken@1895
   125
    JOYAXE_WHEEL,
slouken@1895
   126
    JOYAXE_RX,
slouken@1895
   127
    JOYAXE_RY,
slouken@1895
   128
    JOYAXE_RZ,
slouken@1895
   129
    JOYAXE_count
slouken@307
   130
};
slouken@278
   131
slouken@1895
   132
struct joystick_hwdata
slouken@1895
   133
{
slouken@1895
   134
    int fd;
slouken@1895
   135
    char *path;
slouken@1895
   136
    enum
slouken@1895
   137
    {
slouken@1895
   138
        BSDJOY_UHID,            /* uhid(4) */
slouken@1895
   139
        BSDJOY_JOY              /* joy(4) */
slouken@1895
   140
    } type;
slouken@1895
   141
    struct report_desc *repdesc;
slouken@1895
   142
    struct report inreport;
slouken@1895
   143
    int axis_map[JOYAXE_count]; /* map present JOYAXE_* to 0,1,.. */
slouken@278
   144
};
slouken@278
   145
slouken@278
   146
static char *joynames[MAX_JOYS];
slouken@278
   147
static char *joydevnames[MAX_JOYS];
slouken@278
   148
slouken@1895
   149
static int report_alloc(struct report *, struct report_desc *, int);
slouken@1895
   150
static void report_free(struct report *);
slouken@278
   151
slouken@6290
   152
#if defined(USBHID_UCR_DATA)
slouken@381
   153
#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
icculus@6080
   154
#elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063))
icculus@6080
   155
#define REP_BUF_DATA(rep) ((rep)->buf->ugd_data)
slouken@381
   156
#else
slouken@381
   157
#define REP_BUF_DATA(rep) ((rep)->buf->data)
slouken@381
   158
#endif
slouken@381
   159
slouken@6697
   160
int SDL_SYS_numjoysticks = 0;
slouken@6697
   161
slouken@278
   162
int
slouken@278
   163
SDL_SYS_JoystickInit(void)
slouken@278
   164
{
slouken@1895
   165
    char s[16];
slouken@1895
   166
    int i, fd;
slouken@278
   167
slouken@6697
   168
    SDL_SYS_numjoysticks = 0;
slouken@278
   169
slouken@1895
   170
    SDL_memset(joynames, 0, sizeof(joynames));
slouken@1895
   171
    SDL_memset(joydevnames, 0, sizeof(joydevnames));
slouken@278
   172
slouken@1895
   173
    for (i = 0; i < MAX_UHID_JOYS; i++) {
slouken@1895
   174
        SDL_Joystick nj;
slouken@544
   175
slouken@1895
   176
        SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
slouken@544
   177
slouken@6697
   178
        nj.index = SDL_SYS_numjoysticks;
slouken@1895
   179
        joynames[nj.index] = strdup(s);
slouken@544
   180
slouken@6697
   181
        if (SDL_SYS_JoystickOpen(&nj, nj.index) == 0) {
slouken@1895
   182
            SDL_SYS_JoystickClose(&nj);
slouken@6697
   183
            SDL_SYS_numjoysticks++;
slouken@1895
   184
        } else {
slouken@1895
   185
            SDL_free(joynames[nj.index]);
slouken@1895
   186
            joynames[nj.index] = NULL;
slouken@1895
   187
        }
slouken@1895
   188
    }
slouken@1895
   189
    for (i = 0; i < MAX_JOY_JOYS; i++) {
slouken@1895
   190
        SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
slouken@1895
   191
        fd = open(s, O_RDONLY);
slouken@1895
   192
        if (fd != -1) {
slouken@6697
   193
            joynames[SDL_SYS_numjoysticks++] = strdup(s);
slouken@1895
   194
            close(fd);
slouken@1895
   195
        }
slouken@1895
   196
    }
slouken@278
   197
slouken@1895
   198
    /* Read the default USB HID usage table. */
slouken@1895
   199
    hid_init(NULL);
slouken@278
   200
slouken@6697
   201
    return (SDL_SYS_numjoysticks);
slouken@278
   202
}
slouken@278
   203
slouken@278
   204
const char *
slouken@6697
   205
SDL_SYS_JoystickNameForIndex(int index)
slouken@278
   206
{
slouken@1895
   207
    if (joydevnames[index] != NULL) {
slouken@1895
   208
        return (joydevnames[index]);
slouken@1895
   209
    }
slouken@1895
   210
    return (joynames[index]);
slouken@278
   211
}
slouken@278
   212
slouken@632
   213
static int
slouken@632
   214
usage_to_joyaxe(unsigned usage)
slouken@632
   215
{
slouken@632
   216
    int joyaxe;
slouken@632
   217
    switch (usage) {
slouken@632
   218
    case HUG_X:
slouken@1895
   219
        joyaxe = JOYAXE_X;
slouken@1895
   220
        break;
slouken@632
   221
    case HUG_Y:
slouken@1895
   222
        joyaxe = JOYAXE_Y;
slouken@1895
   223
        break;
slouken@632
   224
    case HUG_Z:
slouken@1895
   225
        joyaxe = JOYAXE_Z;
slouken@1895
   226
        break;
slouken@632
   227
    case HUG_SLIDER:
slouken@1895
   228
        joyaxe = JOYAXE_SLIDER;
slouken@1895
   229
        break;
slouken@632
   230
    case HUG_WHEEL:
slouken@1895
   231
        joyaxe = JOYAXE_WHEEL;
slouken@1895
   232
        break;
slouken@632
   233
    case HUG_RX:
slouken@1895
   234
        joyaxe = JOYAXE_RX;
slouken@1895
   235
        break;
slouken@632
   236
    case HUG_RY:
slouken@1895
   237
        joyaxe = JOYAXE_RY;
slouken@1895
   238
        break;
slouken@632
   239
    case HUG_RZ:
slouken@1895
   240
        joyaxe = JOYAXE_RZ;
slouken@1895
   241
        break;
slouken@632
   242
    default:
slouken@1895
   243
        joyaxe = -1;
slouken@632
   244
    }
slouken@1895
   245
    return joyaxe;
slouken@632
   246
}
slouken@632
   247
slouken@632
   248
static unsigned
slouken@632
   249
hatval_to_sdl(Sint32 hatval)
slouken@632
   250
{
slouken@632
   251
    static const unsigned hat_dir_map[8] = {
slouken@1895
   252
        SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN,
slouken@1895
   253
        SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
slouken@632
   254
    };
slouken@632
   255
    unsigned result;
slouken@1895
   256
    if ((hatval & 7) == hatval)
slouken@1895
   257
        result = hat_dir_map[hatval];
slouken@1895
   258
    else
slouken@1895
   259
        result = SDL_HAT_CENTERED;
slouken@632
   260
    return result;
slouken@632
   261
}
slouken@632
   262
slouken@632
   263
slouken@278
   264
int
slouken@6697
   265
SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
slouken@278
   266
{
slouken@6697
   267
    char *path = joynames[device_index];
slouken@1895
   268
    struct joystick_hwdata *hw;
slouken@1895
   269
    struct hid_item hitem;
slouken@1895
   270
    struct hid_data *hdata;
slouken@1895
   271
    struct report *rep;
slouken@1895
   272
    int fd;
slouken@1895
   273
    int i;
slouken@278
   274
slouken@1895
   275
    fd = open(path, O_RDONLY);
slouken@1895
   276
    if (fd == -1) {
slouken@1895
   277
        SDL_SetError("%s: %s", path, strerror(errno));
slouken@1895
   278
        return (-1);
slouken@1895
   279
    }
slouken@278
   280
slouken@6698
   281
    joy->instance_id = device_index;
slouken@1895
   282
    hw = (struct joystick_hwdata *)
slouken@1895
   283
        SDL_malloc(sizeof(struct joystick_hwdata));
slouken@1895
   284
    if (hw == NULL) {
slouken@1895
   285
        SDL_OutOfMemory();
slouken@1895
   286
        close(fd);
slouken@1895
   287
        return (-1);
slouken@1895
   288
    }
slouken@1895
   289
    joy->hwdata = hw;
slouken@1895
   290
    hw->fd = fd;
slouken@1895
   291
    hw->path = strdup(path);
slouken@1895
   292
    if (!SDL_strncmp(path, "/dev/joy", 8)) {
slouken@1895
   293
        hw->type = BSDJOY_JOY;
slouken@1895
   294
        joy->naxes = 2;
slouken@1895
   295
        joy->nbuttons = 2;
slouken@1895
   296
        joy->nhats = 0;
slouken@1895
   297
        joy->nballs = 0;
slouken@1895
   298
        joydevnames[joy->index] = strdup("Gameport joystick");
slouken@1895
   299
        goto usbend;
slouken@1895
   300
    } else {
slouken@1895
   301
        hw->type = BSDJOY_UHID;
slouken@1895
   302
    }
slouken@776
   303
slouken@1895
   304
    {
slouken@1895
   305
        int ax;
slouken@1895
   306
        for (ax = 0; ax < JOYAXE_count; ax++)
slouken@1895
   307
            hw->axis_map[ax] = -1;
slouken@1895
   308
    }
slouken@1895
   309
    hw->repdesc = hid_get_report_desc(fd);
slouken@1895
   310
    if (hw->repdesc == NULL) {
slouken@1895
   311
        SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
slouken@1895
   312
                     strerror(errno));
slouken@1895
   313
        goto usberr;
slouken@1895
   314
    }
icculus@3703
   315
    rep = &hw->inreport;
slouken@4537
   316
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063) || defined(__FreeBSD_kernel__)
slouken@3385
   317
    rep->rid = hid_get_report_id(fd);
slouken@3385
   318
    if (rep->rid < 0) {
slouken@3385
   319
#else
slouken@1895
   320
    if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
slouken@3385
   321
#endif
slouken@1895
   322
        rep->rid = -1;          /* XXX */
slouken@1895
   323
    }
slouken@1895
   324
    if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
slouken@1895
   325
        goto usberr;
slouken@1895
   326
    }
slouken@1895
   327
    if (rep->size <= 0) {
slouken@1895
   328
        SDL_SetError("%s: Input report descriptor has invalid length",
slouken@1895
   329
                     hw->path);
slouken@1895
   330
        goto usberr;
slouken@1895
   331
    }
slouken@4537
   332
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
slouken@1895
   333
    hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
slouken@1895
   334
#else
slouken@1895
   335
    hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
slouken@1895
   336
#endif
slouken@1895
   337
    if (hdata == NULL) {
slouken@1895
   338
        SDL_SetError("%s: Cannot start HID parser", hw->path);
slouken@1895
   339
        goto usberr;
slouken@1895
   340
    }
slouken@1895
   341
    joy->naxes = 0;
slouken@1895
   342
    joy->nbuttons = 0;
slouken@1895
   343
    joy->nhats = 0;
slouken@1895
   344
    joy->nballs = 0;
slouken@1895
   345
    for (i = 0; i < JOYAXE_count; i++)
slouken@1895
   346
        hw->axis_map[i] = -1;
slouken@278
   347
slouken@1895
   348
    while (hid_get_item(hdata, &hitem) > 0) {
slouken@1895
   349
        char *sp;
slouken@1895
   350
        const char *s;
slouken@278
   351
slouken@1895
   352
        switch (hitem.kind) {
slouken@1895
   353
        case hid_collection:
slouken@1895
   354
            switch (HID_PAGE(hitem.usage)) {
slouken@1895
   355
            case HUP_GENERIC_DESKTOP:
slouken@1895
   356
                switch (HID_USAGE(hitem.usage)) {
slouken@1895
   357
                case HUG_JOYSTICK:
slouken@1895
   358
                case HUG_GAME_PAD:
slouken@1895
   359
                    s = hid_usage_in_page(hitem.usage);
slouken@1895
   360
                    sp = SDL_malloc(SDL_strlen(s) + 5);
slouken@1895
   361
                    SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)",
slouken@1895
   362
                                 s, joy->index);
slouken@1895
   363
                    joydevnames[joy->index] = sp;
slouken@1895
   364
                }
slouken@1895
   365
            }
slouken@1895
   366
            break;
slouken@1895
   367
        case hid_input:
slouken@1895
   368
            switch (HID_PAGE(hitem.usage)) {
slouken@1895
   369
            case HUP_GENERIC_DESKTOP:
slouken@1895
   370
                {
slouken@1895
   371
                    unsigned usage = HID_USAGE(hitem.usage);
slouken@1895
   372
                    int joyaxe = usage_to_joyaxe(usage);
slouken@1895
   373
                    if (joyaxe >= 0) {
slouken@1895
   374
                        hw->axis_map[joyaxe] = 1;
slouken@1895
   375
                    } else if (usage == HUG_HAT_SWITCH) {
slouken@1895
   376
                        joy->nhats++;
slouken@1895
   377
                    }
slouken@1895
   378
                    break;
slouken@1895
   379
                }
slouken@1895
   380
            case HUP_BUTTON:
slouken@1895
   381
                joy->nbuttons++;
slouken@1895
   382
                break;
slouken@1895
   383
            default:
slouken@1895
   384
                break;
slouken@1895
   385
            }
slouken@1895
   386
            break;
slouken@1895
   387
        default:
slouken@1895
   388
            break;
slouken@1895
   389
        }
slouken@1895
   390
    }
slouken@1895
   391
    hid_end_parse(hdata);
slouken@1895
   392
    for (i = 0; i < JOYAXE_count; i++)
slouken@1895
   393
        if (hw->axis_map[i] > 0)
slouken@1895
   394
            hw->axis_map[i] = joy->naxes++;
slouken@278
   395
slouken@1895
   396
  usbend:
slouken@1895
   397
    /* The poll blocks the event thread. */
slouken@1895
   398
    fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@278
   399
slouken@1895
   400
    return (0);
slouken@1895
   401
  usberr:
slouken@1895
   402
    close(hw->fd);
slouken@1895
   403
    SDL_free(hw->path);
slouken@1895
   404
    SDL_free(hw);
slouken@1895
   405
    return (-1);
slouken@278
   406
}
slouken@278
   407
slouken@278
   408
void
slouken@1895
   409
SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
slouken@278
   410
{
slouken@1895
   411
    struct hid_item hitem;
slouken@1895
   412
    struct hid_data *hdata;
slouken@1895
   413
    struct report *rep;
slouken@1895
   414
    int nbutton, naxe = -1;
slouken@1895
   415
    Sint32 v;
slouken@776
   416
slouken@4537
   417
#if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H || defined(__FreeBSD_kernel__)
slouken@1895
   418
    struct joystick gameport;
slouken@1895
   419
    static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
slouken@1895
   420
slouken@1895
   421
    if (joy->hwdata->type == BSDJOY_JOY) {
slouken@1895
   422
        if (read(joy->hwdata->fd, &gameport, sizeof gameport) !=
slouken@1895
   423
            sizeof gameport)
slouken@1895
   424
            return;
slouken@1895
   425
        if (abs(x - gameport.x) > 8) {
slouken@1895
   426
            x = gameport.x;
slouken@1895
   427
            if (x < xmin) {
slouken@1895
   428
                xmin = x;
slouken@1895
   429
            }
slouken@1895
   430
            if (x > xmax) {
slouken@1895
   431
                xmax = x;
slouken@1895
   432
            }
slouken@1895
   433
            if (xmin == xmax) {
slouken@1895
   434
                xmin--;
slouken@1895
   435
                xmax++;
slouken@1895
   436
            }
slouken@1895
   437
            v = (Sint32) x;
slouken@1895
   438
            v -= (xmax + xmin + 1) / 2;
slouken@1895
   439
            v *= 32768 / ((xmax - xmin + 1) / 2);
slouken@1895
   440
            SDL_PrivateJoystickAxis(joy, 0, v);
slouken@1895
   441
        }
slouken@1895
   442
        if (abs(y - gameport.y) > 8) {
slouken@1895
   443
            y = gameport.y;
slouken@1895
   444
            if (y < ymin) {
slouken@1895
   445
                ymin = y;
slouken@1895
   446
            }
slouken@1895
   447
            if (y > ymax) {
slouken@1895
   448
                ymax = y;
slouken@1895
   449
            }
slouken@1895
   450
            if (ymin == ymax) {
slouken@1895
   451
                ymin--;
slouken@1895
   452
                ymax++;
slouken@1895
   453
            }
slouken@1895
   454
            v = (Sint32) y;
slouken@1895
   455
            v -= (ymax + ymin + 1) / 2;
slouken@1895
   456
            v *= 32768 / ((ymax - ymin + 1) / 2);
slouken@1895
   457
            SDL_PrivateJoystickAxis(joy, 1, v);
slouken@1895
   458
        }
slouken@1895
   459
        if (gameport.b1 != joy->buttons[0]) {
slouken@1895
   460
            SDL_PrivateJoystickButton(joy, 0, gameport.b1);
slouken@1895
   461
        }
slouken@1895
   462
        if (gameport.b2 != joy->buttons[1]) {
slouken@1895
   463
            SDL_PrivateJoystickButton(joy, 1, gameport.b2);
slouken@1895
   464
        }
slouken@1895
   465
        return;
slouken@1895
   466
    }
slouken@1565
   467
#endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
slouken@381
   468
slouken@1895
   469
    rep = &joy->hwdata->inreport;
slouken@1895
   470
slouken@1895
   471
    if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
slouken@1895
   472
        return;
slouken@1895
   473
    }
slouken@4537
   474
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
slouken@1895
   475
    hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
slouken@407
   476
#else
slouken@1895
   477
    hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
slouken@407
   478
#endif
slouken@1895
   479
    if (hdata == NULL) {
slouken@1895
   480
        fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);
slouken@1895
   481
        return;
slouken@1895
   482
    }
slouken@278
   483
slouken@1895
   484
    for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
slouken@1895
   485
        switch (hitem.kind) {
slouken@1895
   486
        case hid_input:
slouken@1895
   487
            switch (HID_PAGE(hitem.usage)) {
slouken@1895
   488
            case HUP_GENERIC_DESKTOP:
slouken@1895
   489
                {
slouken@1895
   490
                    unsigned usage = HID_USAGE(hitem.usage);
slouken@1895
   491
                    int joyaxe = usage_to_joyaxe(usage);
slouken@1895
   492
                    if (joyaxe >= 0) {
slouken@1895
   493
                        naxe = joy->hwdata->axis_map[joyaxe];
slouken@1895
   494
                        /* scaleaxe */
slouken@1895
   495
                        v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
slouken@1895
   496
                        v -= (hitem.logical_maximum +
slouken@1895
   497
                              hitem.logical_minimum + 1) / 2;
slouken@1895
   498
                        v *= 32768 /
slouken@1895
   499
                            ((hitem.logical_maximum -
slouken@1895
   500
                              hitem.logical_minimum + 1) / 2);
slouken@1895
   501
                        if (v != joy->axes[naxe]) {
slouken@1895
   502
                            SDL_PrivateJoystickAxis(joy, naxe, v);
slouken@1895
   503
                        }
slouken@1895
   504
                    } else if (usage == HUG_HAT_SWITCH) {
slouken@1895
   505
                        v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
slouken@1895
   506
                        SDL_PrivateJoystickHat(joy, 0,
slouken@1895
   507
                                               hatval_to_sdl(v) -
slouken@1895
   508
                                               hitem.logical_minimum);
slouken@1895
   509
                    }
slouken@1895
   510
                    break;
slouken@1895
   511
                }
slouken@1895
   512
            case HUP_BUTTON:
slouken@1895
   513
                v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
slouken@1895
   514
                if (joy->buttons[nbutton] != v) {
slouken@1895
   515
                    SDL_PrivateJoystickButton(joy, nbutton, v);
slouken@1895
   516
                }
slouken@1895
   517
                nbutton++;
slouken@1895
   518
                break;
slouken@1895
   519
            default:
slouken@1895
   520
                continue;
slouken@1895
   521
            }
slouken@1895
   522
            break;
slouken@1895
   523
        default:
slouken@1895
   524
            break;
slouken@1895
   525
        }
slouken@1895
   526
    }
slouken@1895
   527
    hid_end_parse(hdata);
slouken@278
   528
slouken@1895
   529
    return;
slouken@278
   530
}
slouken@278
   531
slouken@278
   532
/* Function to close a joystick after use */
slouken@278
   533
void
slouken@1895
   534
SDL_SYS_JoystickClose(SDL_Joystick * joy)
slouken@278
   535
{
slouken@1895
   536
    if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
slouken@1895
   537
        report_free(&joy->hwdata->inreport);
slouken@1895
   538
        hid_dispose_report_desc(joy->hwdata->repdesc);
slouken@1895
   539
    }
slouken@1895
   540
    close(joy->hwdata->fd);
slouken@1895
   541
    SDL_free(joy->hwdata->path);
slouken@1895
   542
    SDL_free(joy->hwdata);
slouken@278
   543
slouken@1895
   544
    return;
slouken@278
   545
}
slouken@278
   546
slouken@278
   547
void
slouken@278
   548
SDL_SYS_JoystickQuit(void)
slouken@278
   549
{
slouken@1895
   550
    int i;
slouken@278
   551
slouken@1895
   552
    for (i = 0; i < MAX_JOYS; i++) {
slouken@1895
   553
        if (joynames[i] != NULL)
slouken@1895
   554
            SDL_free(joynames[i]);
slouken@1895
   555
        if (joydevnames[i] != NULL)
slouken@1895
   556
            SDL_free(joydevnames[i]);
slouken@1895
   557
    }
slouken@278
   558
slouken@1895
   559
    return;
slouken@278
   560
}
slouken@278
   561
slouken@6697
   562
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6697
   563
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
slouken@6697
   564
{
slouken@6697
   565
    return index;
slouken@6697
   566
}
slouken@6697
   567
slouken@6697
   568
/* Function to determine is this joystick is attached to the system right now */
slouken@6697
   569
int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6697
   570
{
slouken@6697
   571
    return 1;
slouken@6697
   572
}
slouken@6697
   573
slouken@6697
   574
int SDL_SYS_NumJoysticks()
slouken@6697
   575
{
slouken@6697
   576
    return SDL_SYS_numjoysticks;
slouken@6697
   577
}
slouken@6697
   578
slouken@6697
   579
int SDL_SYS_JoystickNeedsPolling()
slouken@6697
   580
{
slouken@6697
   581
    return 0;
slouken@6697
   582
}
slouken@6697
   583
slouken@6697
   584
void SDL_SYS_JoystickDetect()
slouken@6697
   585
{
slouken@6697
   586
}
slouken@6697
   587
slouken@6697
   588
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
slouken@6697
   589
{
slouken@6697
   590
    JoystickGUID guid;
slouken@6697
   591
    // the GUID is just the first 16 chars of the name for now
slouken@6697
   592
    const char *name = SDL_SYS_JoystickNameForIndex( device_index );
slouken@6697
   593
    SDL_zero( guid );
slouken@6697
   594
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6697
   595
    return guid;
slouken@6697
   596
}
slouken@6697
   597
slouken@6697
   598
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
slouken@6697
   599
{
slouken@6697
   600
    JoystickGUID guid;
slouken@6697
   601
    // the GUID is just the first 16 chars of the name for now
slouken@6697
   602
    const char *name = joystick->name;
slouken@6697
   603
    SDL_zero( guid );
slouken@6697
   604
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6697
   605
    return guid;
slouken@6697
   606
}
slouken@6697
   607
slouken@278
   608
static int
slouken@278
   609
report_alloc(struct report *r, struct report_desc *rd, int repind)
slouken@278
   610
{
slouken@1895
   611
    int len;
slouken@278
   612
slouken@1565
   613
#ifdef __DragonFly__
slouken@1895
   614
    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@1565
   615
#elif __FREEBSD__
slouken@3366
   616
# if (__FreeBSD_kernel_version >= 460000) || defined(__FreeBSD_kernel__)
slouken@3366
   617
#  if (__FreeBSD_kernel_version <= 500111)
slouken@1895
   618
    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@611
   619
#  else
slouken@1895
   620
    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@611
   621
#  endif
slouken@552
   622
# else
slouken@1895
   623
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@1565
   624
# endif
slouken@552
   625
#else
slouken@552
   626
# ifdef USBHID_NEW
slouken@1895
   627
    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@1565
   628
# else
slouken@1895
   629
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@552
   630
# endif
slouken@552
   631
#endif
slouken@552
   632
slouken@1895
   633
    if (len < 0) {
slouken@1895
   634
        SDL_SetError("Negative HID report size");
slouken@1895
   635
        return (-1);
slouken@1895
   636
    }
slouken@1895
   637
    r->size = len;
slouken@278
   638
slouken@1895
   639
    if (r->size > 0) {
slouken@1895
   640
        r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
slouken@1895
   641
                            r->size);
slouken@1895
   642
        if (r->buf == NULL) {
slouken@1895
   643
            SDL_OutOfMemory();
slouken@1895
   644
            return (-1);
slouken@1895
   645
        }
slouken@1895
   646
    } else {
slouken@1895
   647
        r->buf = NULL;
slouken@1895
   648
    }
slouken@278
   649
slouken@1895
   650
    r->status = SREPORT_CLEAN;
slouken@1895
   651
    return (0);
slouken@278
   652
}
slouken@278
   653
slouken@278
   654
static void
slouken@278
   655
report_free(struct report *r)
slouken@278
   656
{
slouken@1895
   657
    if (r->buf != NULL) {
slouken@1895
   658
        SDL_free(r->buf);
slouken@1895
   659
    }
slouken@1895
   660
    r->status = SREPORT_UNINIT;
slouken@278
   661
}
slouken@278
   662
slouken@1635
   663
#endif /* SDL_JOYSTICK_USBHID */
slouken@6697
   664
slouken@1895
   665
/* vi: set ts=4 sw=4 expandtab: */