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