src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1635 92947e3a18db
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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