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