src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 06 Sep 2013 20:54:14 -0700
changeset 7733 957d7137867c
parent 7719 31b5f9ff36ca
child 8093 b43765095a6f
permissions -rw-r--r--
Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD kikuchan Some joysticks with high sampling rate need to be read() more fast, otherwise it delay user inputs due to internal queue. Especially, an app that issues SDL_PollEvent() not so frequent
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6744
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    21
#include "SDL_config.h"
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1565
diff changeset
    23
#ifdef SDL_JOYSTICK_USBHID
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1565
diff changeset
    24
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
/*
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
 * Joystick driver for the uhid(4) interface found in OpenBSD,
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
 * NetBSD and FreeBSD.
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
 *
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
 * Maintainer: <vedge at csoft.org>
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
 */
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
    32
#include <sys/param.h>
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
    33
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#include <unistd.h>
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
#include <fcntl.h>
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#include <errno.h>
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
3367
7a7a61a1484d Whoops, FreeBSD 6.2 doesn't define __FreeBSD_kernel_version
Sam Lantinga <slouken@libsdl.org>
parents: 3366
diff changeset
    38
#ifndef __FreeBSD_kernel_version
7a7a61a1484d Whoops, FreeBSD 6.2 doesn't define __FreeBSD_kernel_version
Sam Lantinga <slouken@libsdl.org>
parents: 3366
diff changeset
    39
#define __FreeBSD_kernel_version __FreeBSD_version
7a7a61a1484d Whoops, FreeBSD 6.2 doesn't define __FreeBSD_kernel_version
Sam Lantinga <slouken@libsdl.org>
parents: 3366
diff changeset
    40
#endif
7a7a61a1484d Whoops, FreeBSD 6.2 doesn't define __FreeBSD_kernel_version
Sam Lantinga <slouken@libsdl.org>
parents: 3366
diff changeset
    41
403
8d431937739d *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 402
diff changeset
    42
#if defined(HAVE_USB_H)
8d431937739d *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 402
diff changeset
    43
#include <usb.h>
8d431937739d *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 402
diff changeset
    44
#endif
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    45
#ifdef __DragonFly__
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    46
#include <bus/usb/usb.h>
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    47
#include <bus/usb/usbhid.h>
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    48
#else
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
#include <dev/usb/usb.h>
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
#include <dev/usb/usbhid.h>
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    51
#endif
358
1f148809d972 Wilbern Cobb fixed joystick code on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 307
diff changeset
    52
381
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
    53
#if defined(HAVE_USBHID_H)
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
    54
#include <usbhid.h>
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
    55
#elif defined(HAVE_LIBUSB_H)
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
    56
#include <libusb.h>
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
    57
#elif defined(HAVE_LIBUSBHID_H)
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
    58
#include <libusbhid.h>
358
1f148809d972 Wilbern Cobb fixed joystick code on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 307
diff changeset
    59
#endif
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
4944
921637675f2c I am using SDL 1.3 on FreeBSD. I found two problems.
Sam Lantinga <slouken@libsdl.org>
parents: 4537
diff changeset
    61
#if defined(__FREEBSD__) || defined(__FreeBSD_kernel__)
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    62
#ifndef __DragonFly__
611
0a2ef9e8ca41 Date: Wed, 9 Apr 2003 01:03:25 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 552
diff changeset
    63
#include <osreldate.h>
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    64
#endif
6080
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    65
#if __FreeBSD_kernel_version > 800063
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    66
#include <dev/usb/usb_ioctl.h>
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    67
#endif
776
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    68
#include <sys/joystick.h>
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    69
#endif
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    70
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
    71
#if SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
776
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    72
#include <machine/joystick.h>
611
0a2ef9e8ca41 Date: Wed, 9 Apr 2003 01:03:25 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 552
diff changeset
    73
#endif
0a2ef9e8ca41 Date: Wed, 9 Apr 2003 01:03:25 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 552
diff changeset
    74
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
#include "SDL_joystick.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    76
#include "../SDL_sysjoystick.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    77
#include "../SDL_joystick_c.h"
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
    79
#define MAX_UHID_JOYS   16
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
    80
#define MAX_JOY_JOYS    2
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
    81
#define MAX_JOYS    (MAX_UHID_JOYS + MAX_JOY_JOYS)
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
3385
45d7f0f70b27 Joystick patch from FreeBSD ports system
Sam Lantinga <slouken@libsdl.org>
parents: 3367
diff changeset
    83
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    84
struct report
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    85
{
6963
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    86
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000)
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    87
    void *buf; /* Buffer */
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    88
#elif defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063)
6080
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    89
    struct usb_gen_descriptor *buf; /* Buffer */
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    90
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    91
    struct usb_ctl_report *buf; /* Buffer */
6080
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    92
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    93
    size_t size;                /* Buffer size */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    94
    int rid;                    /* Report ID */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    95
    enum
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    96
    {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    97
        SREPORT_UNINIT,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    98
        SREPORT_CLEAN,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    99
        SREPORT_DIRTY
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   100
    } status;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
};
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   103
static struct
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   104
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   105
    int uhid_report;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   106
    hid_kind_t kind;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   107
    const char *name;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
} const repinfo[] = {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   109
    {UHID_INPUT_REPORT, hid_input, "input"},
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   110
    {UHID_OUTPUT_REPORT, hid_output, "output"},
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   111
    {UHID_FEATURE_REPORT, hid_feature, "feature"}
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
};
307
0185452e9f83 This properly scales axes, and adds support for sliders/wheels
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   113
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   114
enum
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   115
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   116
    REPORT_INPUT = 0,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   117
    REPORT_OUTPUT = 1,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   118
    REPORT_FEATURE = 2
307
0185452e9f83 This properly scales axes, and adds support for sliders/wheels
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   119
};
0185452e9f83 This properly scales axes, and adds support for sliders/wheels
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   120
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   121
enum
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   122
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   123
    JOYAXE_X,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   124
    JOYAXE_Y,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   125
    JOYAXE_Z,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   126
    JOYAXE_SLIDER,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   127
    JOYAXE_WHEEL,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   128
    JOYAXE_RX,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   129
    JOYAXE_RY,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   130
    JOYAXE_RZ,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   131
    JOYAXE_count
307
0185452e9f83 This properly scales axes, and adds support for sliders/wheels
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   132
};
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   134
struct joystick_hwdata
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   135
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   136
    int fd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   137
    char *path;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   138
    enum
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   139
    {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   140
        BSDJOY_UHID,            /* uhid(4) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   141
        BSDJOY_JOY              /* joy(4) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   142
    } type;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   143
    struct report_desc *repdesc;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   144
    struct report inreport;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   145
    int axis_map[JOYAXE_count]; /* map present JOYAXE_* to 0,1,.. */
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
};
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
static char *joynames[MAX_JOYS];
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
static char *joydevnames[MAX_JOYS];
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   151
static int report_alloc(struct report *, struct report_desc *, int);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   152
static void report_free(struct report *);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
6963
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   154
#if defined(USBHID_UCR_DATA) || (defined(__FreeBSD_kernel__) && __FreeBSD_kernel_version <= 800063)
381
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
   155
#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
6963
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   156
#elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000))
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   157
#define REP_BUF_DATA(rep) ((rep)->buf)
6080
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   158
#elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063))
eb0e11b096ab Patched to compile on newer FreeBSD releases.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   159
#define REP_BUF_DATA(rep) ((rep)->buf->ugd_data)
381
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
   160
#else
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
   161
#define REP_BUF_DATA(rep) ((rep)->buf->data)
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
   162
#endif
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
   163
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   164
static int SDL_SYS_numjoysticks = 0;
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   165
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
int
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
SDL_SYS_JoystickInit(void)
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   169
    char s[16];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   170
    int i, fd;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   172
    SDL_SYS_numjoysticks = 0;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   174
    SDL_memset(joynames, 0, sizeof(joynames));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   175
    SDL_memset(joydevnames, 0, sizeof(joydevnames));
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   177
    for (i = 0; i < MAX_UHID_JOYS; i++) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   178
        SDL_Joystick nj;
544
2d7373ffd131 Fixed bsd joystick detection ... again (thanks Wilbern)
Sam Lantinga <slouken@libsdl.org>
parents: 503
diff changeset
   179
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   180
        SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
544
2d7373ffd131 Fixed bsd joystick detection ... again (thanks Wilbern)
Sam Lantinga <slouken@libsdl.org>
parents: 503
diff changeset
   181
6700
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   182
        joynames[SDL_SYS_numjoysticks] = strdup(s);
544
2d7373ffd131 Fixed bsd joystick detection ... again (thanks Wilbern)
Sam Lantinga <slouken@libsdl.org>
parents: 503
diff changeset
   183
6700
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   184
        if (SDL_SYS_JoystickOpen(&nj, SDL_SYS_numjoysticks) == 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   185
            SDL_SYS_JoystickClose(&nj);
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   186
            SDL_SYS_numjoysticks++;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   187
        } else {
6700
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   188
            SDL_free(joynames[SDL_SYS_numjoysticks]);
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   189
            joynames[SDL_SYS_numjoysticks] = NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   190
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   191
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   192
    for (i = 0; i < MAX_JOY_JOYS; i++) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   193
        SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   194
        fd = open(s, O_RDONLY);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   195
        if (fd != -1) {
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   196
            joynames[SDL_SYS_numjoysticks++] = strdup(s);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   197
            close(fd);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   198
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   199
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   201
    /* Read the default USB HID usage table. */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   202
    hid_init(NULL);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   204
    return (SDL_SYS_numjoysticks);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   207
int SDL_SYS_NumJoysticks()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   208
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   209
    return SDL_SYS_numjoysticks;
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   210
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   211
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   212
void SDL_SYS_JoystickDetect()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   213
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   214
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   215
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   216
SDL_bool SDL_SYS_JoystickNeedsPolling()
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
{
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   218
    return SDL_FALSE;
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   219
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   220
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   221
const char *
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   222
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   223
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   224
    if (joydevnames[device_index] != NULL) {
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   225
        return (joydevnames[device_index]);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   226
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   227
    return (joynames[device_index]);
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   228
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   229
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   230
/* Function to perform the mapping from device index to the instance id for this index */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   231
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   232
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   233
    return device_index;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   236
static int
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   237
usage_to_joyaxe(unsigned usage)
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   238
{
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   239
    int joyaxe;
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   240
    switch (usage) {
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   241
    case HUG_X:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   242
        joyaxe = JOYAXE_X;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   243
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   244
    case HUG_Y:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   245
        joyaxe = JOYAXE_Y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   246
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   247
    case HUG_Z:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   248
        joyaxe = JOYAXE_Z;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   249
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   250
    case HUG_SLIDER:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   251
        joyaxe = JOYAXE_SLIDER;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   252
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   253
    case HUG_WHEEL:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   254
        joyaxe = JOYAXE_WHEEL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   255
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   256
    case HUG_RX:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   257
        joyaxe = JOYAXE_RX;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   258
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   259
    case HUG_RY:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   260
        joyaxe = JOYAXE_RY;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   261
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   262
    case HUG_RZ:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   263
        joyaxe = JOYAXE_RZ;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   264
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   265
    default:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   266
        joyaxe = -1;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   267
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   268
    return joyaxe;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   269
}
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   270
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   271
static unsigned
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   272
hatval_to_sdl(Sint32 hatval)
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   273
{
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   274
    static const unsigned hat_dir_map[8] = {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   275
        SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   276
        SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   277
    };
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   278
    unsigned result;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   279
    if ((hatval & 7) == hatval)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   280
        result = hat_dir_map[hatval];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   281
    else
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   282
        result = SDL_HAT_CENTERED;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   283
    return result;
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   284
}
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   285
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   286
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
int
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   288
SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
{
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   290
    char *path = joynames[device_index];
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   291
    struct joystick_hwdata *hw;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   292
    struct hid_item hitem;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   293
    struct hid_data *hdata;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   294
    struct report *rep;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   295
    int fd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   296
    int i;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   298
    fd = open(path, O_RDONLY);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   299
    if (fd == -1) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   300
        return SDL_SetError("%s: %s", path, strerror(errno));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   301
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
6698
28ab2ef7bfc9 Completed adding new hotplug stubs for the joystick implementations
Sam Lantinga <slouken@libsdl.org>
parents: 6697
diff changeset
   303
    joy->instance_id = device_index;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   304
    hw = (struct joystick_hwdata *)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   305
        SDL_malloc(sizeof(struct joystick_hwdata));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   306
    if (hw == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   307
        close(fd);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   308
        return SDL_OutOfMemory();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   309
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   310
    joy->hwdata = hw;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   311
    hw->fd = fd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   312
    hw->path = strdup(path);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   313
    if (!SDL_strncmp(path, "/dev/joy", 8)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   314
        hw->type = BSDJOY_JOY;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   315
        joy->naxes = 2;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   316
        joy->nbuttons = 2;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   317
        joy->nhats = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   318
        joy->nballs = 0;
6700
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   319
        joydevnames[device_index] = strdup("Gameport joystick");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   320
        goto usbend;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   321
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   322
        hw->type = BSDJOY_UHID;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   323
    }
776
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   324
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   325
    {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   326
        int ax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   327
        for (ax = 0; ax < JOYAXE_count; ax++)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   328
            hw->axis_map[ax] = -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   329
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   330
    hw->repdesc = hid_get_report_desc(fd);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   331
    if (hw->repdesc == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   332
        SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   333
                     strerror(errno));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   334
        goto usberr;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   335
    }
3703
df65ae8f32e8 Merged r5547:5548 from branches/SDL-1.2: FreeBSD joystick uninit'd var fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   336
    rep = &hw->inreport;
4537
cd344ebc14e9 Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 3703
diff changeset
   337
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063) || defined(__FreeBSD_kernel__)
3385
45d7f0f70b27 Joystick patch from FreeBSD ports system
Sam Lantinga <slouken@libsdl.org>
parents: 3367
diff changeset
   338
    rep->rid = hid_get_report_id(fd);
45d7f0f70b27 Joystick patch from FreeBSD ports system
Sam Lantinga <slouken@libsdl.org>
parents: 3367
diff changeset
   339
    if (rep->rid < 0) {
45d7f0f70b27 Joystick patch from FreeBSD ports system
Sam Lantinga <slouken@libsdl.org>
parents: 3367
diff changeset
   340
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   341
    if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
3385
45d7f0f70b27 Joystick patch from FreeBSD ports system
Sam Lantinga <slouken@libsdl.org>
parents: 3367
diff changeset
   342
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   343
        rep->rid = -1;          /* XXX */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   344
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   345
    if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   346
        goto usberr;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   347
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   348
    if (rep->size <= 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   349
        SDL_SetError("%s: Input report descriptor has invalid length",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   350
                     hw->path);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   351
        goto usberr;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   352
    }
4537
cd344ebc14e9 Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 3703
diff changeset
   353
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   354
    hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
407
173909e17b7f API changes on OpenBSD and FreeBSD...
Sam Lantinga <slouken@libsdl.org>
parents: 405
diff changeset
   355
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   356
    hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
407
173909e17b7f API changes on OpenBSD and FreeBSD...
Sam Lantinga <slouken@libsdl.org>
parents: 405
diff changeset
   357
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   358
    if (hdata == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   359
        SDL_SetError("%s: Cannot start HID parser", hw->path);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   360
        goto usberr;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   361
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   362
    joy->naxes = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   363
    joy->nbuttons = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   364
    joy->nhats = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   365
    joy->nballs = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   366
    for (i = 0; i < JOYAXE_count; i++)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   367
        hw->axis_map[i] = -1;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   369
    while (hid_get_item(hdata, &hitem) > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   370
        char *sp;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   371
        const char *s;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   372
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   373
        switch (hitem.kind) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   374
        case hid_collection:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   375
            switch (HID_PAGE(hitem.usage)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   376
            case HUP_GENERIC_DESKTOP:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   377
                switch (HID_USAGE(hitem.usage)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   378
                case HUG_JOYSTICK:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   379
                case HUG_GAME_PAD:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   380
                    s = hid_usage_in_page(hitem.usage);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   381
                    sp = SDL_malloc(SDL_strlen(s) + 5);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   382
                    SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)",
6700
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   383
                                 s, device_index);
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   384
                    joydevnames[device_index] = sp;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   385
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   386
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   387
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   388
        case hid_input:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   389
            switch (HID_PAGE(hitem.usage)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   390
            case HUP_GENERIC_DESKTOP:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   391
                {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   392
                    unsigned usage = HID_USAGE(hitem.usage);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   393
                    int joyaxe = usage_to_joyaxe(usage);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   394
                    if (joyaxe >= 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   395
                        hw->axis_map[joyaxe] = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   396
                    } else if (usage == HUG_HAT_SWITCH) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   397
                        joy->nhats++;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   398
                    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   399
                    break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   400
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   401
            case HUP_BUTTON:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   402
                joy->nbuttons++;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   403
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   404
            default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   405
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   406
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   407
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   408
        default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   409
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   410
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   411
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   412
    hid_end_parse(hdata);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   413
    for (i = 0; i < JOYAXE_count; i++)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   414
        if (hw->axis_map[i] > 0)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   415
            hw->axis_map[i] = joy->naxes++;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   416
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   417
  usbend:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   418
    /* The poll blocks the event thread. */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   419
    fcntl(fd, F_SETFL, O_NONBLOCK);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   420
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   421
    return (0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   422
  usberr:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   423
    close(hw->fd);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   424
    SDL_free(hw->path);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   425
    SDL_free(hw);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   426
    return (-1);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   428
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   429
/* Function to determine is this joystick is attached to the system right now */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   430
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   431
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   432
    return SDL_TRUE;
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   433
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   434
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   436
SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   438
    struct hid_item hitem;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   439
    struct hid_data *hdata;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   440
    struct report *rep;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   441
    int nbutton, naxe = -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   442
    Sint32 v;
776
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   443
4537
cd344ebc14e9 Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 3703
diff changeset
   444
#if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H || defined(__FreeBSD_kernel__)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   445
    struct joystick gameport;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   446
    static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   447
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   448
    if (joy->hwdata->type == BSDJOY_JOY) {
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   449
        while (read(joy->hwdata->fd, &gameport, sizeof gameport) == sizeof gameport) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   450
            if (abs(x - gameport.x) > 8) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   451
                x = gameport.x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   452
                if (x < xmin) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   453
                    xmin = x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   454
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   455
                if (x > xmax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   456
                    xmax = x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   457
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   458
                if (xmin == xmax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   459
                    xmin--;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   460
                    xmax++;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   461
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   462
                v = (Sint32) x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   463
                v -= (xmax + xmin + 1) / 2;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   464
                v *= 32768 / ((xmax - xmin + 1) / 2);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   465
                SDL_PrivateJoystickAxis(joy, 0, v);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   466
            }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   467
            if (abs(y - gameport.y) > 8) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   468
                y = gameport.y;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   469
                if (y < ymin) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   470
                    ymin = y;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   471
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   472
                if (y > ymax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   473
                    ymax = y;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   474
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   475
                if (ymin == ymax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   476
                    ymin--;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   477
                    ymax++;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   478
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   479
                v = (Sint32) y;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   480
                v -= (ymax + ymin + 1) / 2;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   481
                v *= 32768 / ((ymax - ymin + 1) / 2);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   482
                SDL_PrivateJoystickAxis(joy, 1, v);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   483
            }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   484
            if (gameport.b1 != joy->buttons[0]) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   485
                SDL_PrivateJoystickButton(joy, 0, gameport.b1);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   486
            }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   487
            if (gameport.b2 != joy->buttons[1]) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   488
                SDL_PrivateJoystickButton(joy, 1, gameport.b2);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   489
            }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   490
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   491
        return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   492
    }
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   493
#endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   494
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   495
    rep = &joy->hwdata->inreport;
381
bc1401311390 Wilbern Cobb submitted a fix for building BSD joystick support that should work on all BSD flavors.
Sam Lantinga <slouken@libsdl.org>
parents: 358
diff changeset
   496
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   497
    while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size) {
4537
cd344ebc14e9 Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 3703
diff changeset
   498
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   499
        hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
407
173909e17b7f API changes on OpenBSD and FreeBSD...
Sam Lantinga <slouken@libsdl.org>
parents: 405
diff changeset
   500
#else
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   501
        hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
407
173909e17b7f API changes on OpenBSD and FreeBSD...
Sam Lantinga <slouken@libsdl.org>
parents: 405
diff changeset
   502
#endif
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   503
        if (hdata == NULL) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   504
            /*fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);*/
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   505
            continue;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   506
        }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   507
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   508
        for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   509
            switch (hitem.kind) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   510
            case hid_input:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   511
                switch (HID_PAGE(hitem.usage)) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   512
                case HUP_GENERIC_DESKTOP:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   513
                    {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   514
                        unsigned usage = HID_USAGE(hitem.usage);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   515
                        int joyaxe = usage_to_joyaxe(usage);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   516
                        if (joyaxe >= 0) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   517
                            naxe = joy->hwdata->axis_map[joyaxe];
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   518
                            /* scaleaxe */
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   519
                            v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   520
                            v -= (hitem.logical_maximum +
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   521
                                  hitem.logical_minimum + 1) / 2;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   522
                            v *= 32768 /
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   523
                                ((hitem.logical_maximum -
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   524
                                  hitem.logical_minimum + 1) / 2);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   525
                            if (v != joy->axes[naxe]) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   526
                                SDL_PrivateJoystickAxis(joy, naxe, v);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   527
                            }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   528
                        } else if (usage == HUG_HAT_SWITCH) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   529
                            v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   530
                            SDL_PrivateJoystickHat(joy, 0,
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   531
                                                   hatval_to_sdl(v) -
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   532
                                                   hitem.logical_minimum);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   533
                        }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   534
                        break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   535
                    }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   536
                case HUP_BUTTON:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   537
                    v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   538
                    if (joy->buttons[nbutton] != v) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   539
                        SDL_PrivateJoystickButton(joy, nbutton, v);
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   540
                    }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   541
                    nbutton++;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   542
                    break;
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   543
                default:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   544
                    continue;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   545
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   546
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   547
            default:
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   548
                break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   549
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   550
        }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   551
        hid_end_parse(hdata);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   552
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   553
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   554
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   555
/* Function to close a joystick after use */
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   556
void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   557
SDL_SYS_JoystickClose(SDL_Joystick * joy)
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   558
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   559
    if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   560
        report_free(&joy->hwdata->inreport);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   561
        hid_dispose_report_desc(joy->hwdata->repdesc);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   562
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   563
    close(joy->hwdata->fd);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   564
    SDL_free(joy->hwdata->path);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   565
    SDL_free(joy->hwdata);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   566
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   567
    return;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   568
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   569
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   570
void
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   571
SDL_SYS_JoystickQuit(void)
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   572
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   573
    int i;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   574
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   575
    for (i = 0; i < MAX_JOYS; i++) {
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   576
        SDL_free(joynames[i]);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   577
        SDL_free(joydevnames[i]);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   578
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   580
    return;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   583
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   584
{
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   585
    SDL_JoystickGUID guid;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   586
    /* the GUID is just the first 16 chars of the name for now */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   587
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   588
    SDL_zero( guid );
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   589
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   590
    return guid;
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   591
}
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   592
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   593
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   594
{
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   595
    SDL_JoystickGUID guid;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   596
    /* the GUID is just the first 16 chars of the name for now */
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   597
    const char *name = joystick->name;
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   598
    SDL_zero( guid );
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   599
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   600
    return guid;
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   601
}
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   602
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   603
static int
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   604
report_alloc(struct report *r, struct report_desc *rd, int repind)
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   606
    int len;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   608
#ifdef __DragonFly__
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   609
    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   610
#elif __FREEBSD__
3366
8cd9ed45c020 Adapted from Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   611
# if (__FreeBSD_kernel_version >= 460000) || defined(__FreeBSD_kernel__)
8cd9ed45c020 Adapted from Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   612
#  if (__FreeBSD_kernel_version <= 500111)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   613
    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
611
0a2ef9e8ca41 Date: Wed, 9 Apr 2003 01:03:25 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 552
diff changeset
   614
#  else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   615
    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
611
0a2ef9e8ca41 Date: Wed, 9 Apr 2003 01:03:25 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 552
diff changeset
   616
#  endif
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   617
# else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   618
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   619
# endif
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   620
#else
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   621
# ifdef USBHID_NEW
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   622
    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   623
# else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   624
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   625
# endif
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   626
#endif
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   627
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   628
    if (len < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   629
        return SDL_SetError("Negative HID report size");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   630
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   631
    r->size = len;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   632
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   633
    if (r->size > 0) {
6963
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   634
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000)
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   635
        r->buf = SDL_malloc(r->size);
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   636
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   637
        r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   638
                            r->size);
6963
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   639
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   640
        if (r->buf == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   641
            return SDL_OutOfMemory();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   642
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   643
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   644
        r->buf = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   645
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   646
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   647
    r->status = SREPORT_CLEAN;
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   648
    return 0;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   649
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   650
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   651
static void
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   652
report_free(struct report *r)
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   653
{
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   654
    SDL_free(r->buf);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   655
    r->status = SREPORT_UNINIT;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   656
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   657
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1565
diff changeset
   658
#endif /* SDL_JOYSTICK_USBHID */
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   659
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   660
/* vi: set ts=4 sw=4 expandtab: */