src/joystick/bsd/SDL_sysjoystick.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Tue, 28 Jun 2016 21:08:23 +0200
changeset 10187 1f0f8c95531d
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
Fixed freeing strings created by strdup() with SDL_free(). This only worked on platforms where SDL_free() wraps free().
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
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9986
diff changeset
     3
  Copyright (C) 1997-2016 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
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7733
diff changeset
    21
#include "../../SDL_internal.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
9986
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
    79
#define MAX_UHID_JOYS   64
7191
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
10187
1f0f8c95531d Fixed freeing strings created by strdup() with SDL_free().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   182
        joynames[SDL_SYS_numjoysticks] = SDL_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) {
10187
1f0f8c95531d Fixed freeing strings created by strdup() with SDL_free().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   196
            joynames[SDL_SYS_numjoysticks++] = SDL_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
const char *
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   217
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   218
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   219
    if (joydevnames[device_index] != NULL) {
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   220
        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
   221
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   222
    return (joynames[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
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   225
/* 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
   226
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   227
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   228
    return device_index;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   231
static int
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   232
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
   233
{
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   234
    int joyaxe;
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   235
    switch (usage) {
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   236
    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
   237
        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
   238
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   239
    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
   240
        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
   241
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   242
    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
   243
        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
   244
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   245
    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
   246
        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
   247
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   248
    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
   249
        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
   250
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   251
    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
   252
        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
   253
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   254
    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
   255
        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
   256
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   257
    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
   258
        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
   259
        break;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   260
    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
   261
        joyaxe = -1;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   262
    }
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
    return joyaxe;
632
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   264
}
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   265
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   266
static unsigned
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   267
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
   268
{
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   269
    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
   270
        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
   271
        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
   272
    };
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   273
    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
   274
    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
   275
        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
   276
    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
   277
        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
   278
    return result;
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   279
}
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   280
85e104fe14c2 Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
Sam Lantinga <slouken@libsdl.org>
parents: 611
diff changeset
   281
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
int
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   283
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
   284
{
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   285
    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
   286
    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
   287
    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
   288
    struct hid_data *hdata;
9986
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   289
    struct report *rep = 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
   290
    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
   291
    int i;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
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
   293
    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
   294
    if (fd == -1) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   295
        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
   296
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
6698
28ab2ef7bfc9 Completed adding new hotplug stubs for the joystick implementations
Sam Lantinga <slouken@libsdl.org>
parents: 6697
diff changeset
   298
    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
   299
    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
   300
        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
   301
    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
   302
        close(fd);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   303
        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
   304
    }
c121d94672cb SDL 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
    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
   306
    hw->fd = fd;
10187
1f0f8c95531d Fixed freeing strings created by strdup() with SDL_free().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   307
    hw->path = SDL_strdup(path);
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
   308
    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
   309
        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
   310
        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
   311
        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
   312
        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
   313
        joy->nballs = 0;
10187
1f0f8c95531d Fixed freeing strings created by strdup() with SDL_free().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   314
        joydevnames[device_index] = SDL_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
   315
        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
   316
    } 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
   317
        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
   318
    }
776
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   319
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
    {
c121d94672cb SDL 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
        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
   322
        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
   323
            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
   324
    }
c121d94672cb SDL 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
    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
   326
    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
   327
        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
   328
                     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
   329
        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
   330
    }
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
   331
    rep = &hw->inreport;
4537
cd344ebc14e9 Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 3703
diff changeset
   332
#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
   333
    rep->rid = hid_get_report_id(fd);
45d7f0f70b27 Joystick patch from FreeBSD ports system
Sam Lantinga <slouken@libsdl.org>
parents: 3367
diff changeset
   334
    if (rep->rid < 0) {
45d7f0f70b27 Joystick patch from FreeBSD ports system
Sam Lantinga <slouken@libsdl.org>
parents: 3367
diff changeset
   335
#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
   336
    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
   337
#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
   338
        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
   339
    }
9986
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   340
#if defined(__NetBSD__)
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   341
    usb_device_descriptor_t udd;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   342
    struct usb_string_desc usd;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   343
    if (ioctl(fd, USB_GET_DEVICE_DESC, &udd) == -1)
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   344
        goto desc_failed;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   345
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   346
    /* Get default language */
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   347
    usd.usd_string_index = USB_LANGUAGE_TABLE;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   348
    usd.usd_language_id = 0;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   349
    if (ioctl(fd, USB_GET_STRING_DESC, &usd) == -1 || usd.usd_desc.bLength < 4) {
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   350
        usd.usd_language_id = 0;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   351
    } else {
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   352
        usd.usd_language_id = UGETW(usd.usd_desc.bString[0]);
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   353
    }
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   354
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   355
    usd.usd_string_index = udd.iProduct;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   356
    if (ioctl(fd, USB_GET_STRING_DESC, &usd) == 0) {
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   357
        char str[128];
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   358
        char *new_name = NULL;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   359
        int i;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   360
        for (i = 0; i < (usd.usd_desc.bLength >> 1) - 1 && i < sizeof(str) - 1; i++) {
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   361
            str[i] = UGETW(usd.usd_desc.bString[i]);
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   362
        }
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   363
        str[i] = '\0';
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   364
        asprintf(&new_name, "%s @ %s", str, path);
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   365
        if (new_name != NULL) {
10187
1f0f8c95531d Fixed freeing strings created by strdup() with SDL_free().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   366
            SDL_free(joydevnames[SDL_SYS_numjoysticks]);
9986
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   367
            joydevnames[SDL_SYS_numjoysticks] = new_name;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   368
        }
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   369
    }
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   370
desc_failed:
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   371
#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
   372
    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
   373
        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
   374
    }
c121d94672cb SDL 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
    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
   376
        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
   377
                     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
   378
        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
   379
    }
4537
cd344ebc14e9 Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 3703
diff changeset
   380
#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
   381
    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
   382
#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
   383
    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
   384
#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
   385
    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
   386
        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
   387
        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
   388
    }
c121d94672cb SDL 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
    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
   390
    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
   391
    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
   392
    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
   393
    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
   394
        hw->axis_map[i] = -1;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
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
   396
    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
   397
        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
   398
        const char *s;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   399
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
   400
        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
   401
        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
   402
            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
   403
            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
   404
                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
   405
                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
   406
                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
   407
                    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
   408
                    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
   409
                    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
   410
                                 s, device_index);
c8f3d3f0251e Hopefully fixed BSD joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   411
                    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
   412
                }
c121d94672cb SDL 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
            }
c121d94672cb SDL 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
            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
   415
        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
   416
            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
   417
            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
   418
                {
c121d94672cb SDL 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
                    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
   420
                    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
   421
                    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
   422
                        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
   423
                    } 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
   424
                        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
   425
                    }
c121d94672cb SDL 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
                    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
   427
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   428
            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
   429
                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
   430
                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
   431
            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
   432
                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
   433
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   434
            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
   435
        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
   436
            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
   437
        }
c121d94672cb SDL 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
    }
c121d94672cb SDL 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
    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
   440
    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
   441
        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
   442
            hw->axis_map[i] = joy->naxes++;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   443
9986
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   444
    if (joy->naxes == 0 && joy->nbuttons == 0 && joy->nhats == 0 && joy->nballs == 0) {
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   445
        SDL_SetError("%s: Not a joystick, ignoring", hw->path);
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   446
        goto usberr;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   447
    }
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   448
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
   449
  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
   450
    /* 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
   451
    fcntl(fd, F_SETFL, O_NONBLOCK);
9986
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   452
#ifdef __NetBSD__
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   453
    /* Flush pending events */
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   454
    if (rep) {
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   455
        while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size)
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   456
            ;
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   457
    }
081fbd89a347 NetBSD: improved joystick support (thanks, Thomas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   458
#endif
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   459
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
   460
    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
   461
  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
   462
    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
   463
    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
   464
    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
   465
    return (-1);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   466
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   467
9561
d8ad01792399 Fixed typo in internal joystick documentation comments.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9433
diff changeset
   468
/* Function to determine if this joystick is attached to the system right now */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   469
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   470
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   471
    return SDL_TRUE;
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   472
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6700
diff changeset
   473
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   474
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
   475
SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   476
{
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
   477
    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
   478
    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
   479
    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
   480
    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
   481
    Sint32 v;
776
18922ae3ee07 Added support for /dev/joy* on Free/Net/OpenBSD (thanks Christian!)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   482
4537
cd344ebc14e9 Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 3703
diff changeset
   483
#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
   484
    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
   485
    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
   486
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   487
    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
   488
        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
   489
            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
   490
                x = gameport.x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   491
                if (x < xmin) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   492
                    xmin = x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   493
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   494
                if (x > xmax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   495
                    xmax = x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   496
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   497
                if (xmin == xmax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   498
                    xmin--;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   499
                    xmax++;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   500
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   501
                v = (Sint32) x;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   502
                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
   503
                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
   504
                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
   505
            }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   506
            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
   507
                y = gameport.y;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   508
                if (y < ymin) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   509
                    ymin = y;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   510
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   511
                if (y > ymax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   512
                    ymax = y;
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
                if (ymin == ymax) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   515
                    ymin--;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   516
                    ymax++;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   517
                }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   518
                v = (Sint32) y;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   519
                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
   520
                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
   521
                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
   522
            }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   523
            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
   524
                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
   525
            }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   526
            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
   527
                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
   528
            }
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
   529
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   530
        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
   531
    }
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   532
#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
   533
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   534
    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
   535
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   536
    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
   537
#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
   538
        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
   539
#else
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   540
        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
   541
#endif
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   542
        if (hdata == NULL) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   543
            /*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
   544
            continue;
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   545
        }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   546
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   547
        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
   548
            switch (hitem.kind) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   549
            case hid_input:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   550
                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
   551
                case HUP_GENERIC_DESKTOP:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   552
                    {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   553
                        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
   554
                        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
   555
                        if (joyaxe >= 0) {
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   556
                            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
   557
                            /* scaleaxe */
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   558
                            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
   559
                            v -= (hitem.logical_maximum +
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   560
                                  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
   561
                            v *= 32768 /
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   562
                                ((hitem.logical_maximum -
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   563
                                  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
   564
                            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
   565
                                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
   566
                            }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   567
                        } 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
   568
                            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
   569
                            SDL_PrivateJoystickHat(joy, 0,
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   570
                                                   hatval_to_sdl(v) -
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   571
                                                   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
   572
                        }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   573
                        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
   574
                    }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   575
                case HUP_BUTTON:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   576
                    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
   577
                    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
   578
                        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
   579
                    }
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   580
                    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
   581
                    break;
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   582
                default:
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   583
                    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
   584
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   585
                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
   586
            default:
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   587
                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
   588
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   589
        }
7733
957d7137867c Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   590
        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
   591
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   592
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   593
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   594
/* Function to close a joystick after use */
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   595
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
   596
SDL_SYS_JoystickClose(SDL_Joystick * joy)
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   597
{
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
   598
    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
   599
        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
   600
        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
   601
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   602
    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
   603
    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
   604
    SDL_free(joy->hwdata);
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   606
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
void
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   608
SDL_SYS_JoystickQuit(void)
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   609
{
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
   610
    int i;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   611
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
   612
    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
   613
        SDL_free(joynames[i]);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   614
        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
   615
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   616
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
   617
    return;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   618
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   619
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   620
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
   621
{
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   622
    SDL_JoystickGUID guid;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   623
    /* 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
   624
    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
   625
    SDL_zero( guid );
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   626
    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
   627
    return guid;
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   628
}
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   629
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   630
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
   631
{
6744
4ac2bb6dc80c Fixed joystick GUID renaming for other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   632
    SDL_JoystickGUID guid;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   633
    /* 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
   634
    const char *name = joystick->name;
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   635
    SDL_zero( guid );
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   636
    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
   637
    return guid;
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   638
}
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   639
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
static int
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   641
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
   642
{
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
   643
    int len;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   644
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   645
#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
   646
    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   647
#elif __FREEBSD__
3366
8cd9ed45c020 Adapted from Debian patch: 215_kfreebsd_gnu.diff
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   648
# 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
   649
#  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
   650
    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
   651
#  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
   652
    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
   653
#  endif
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   654
# 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
   655
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
1565
57431b199aed Fixed bug #52
Sam Lantinga <slouken@libsdl.org>
parents: 1559
diff changeset
   656
# endif
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   657
#else
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   658
# 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
   659
    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   660
# 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
   661
    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
552
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   662
# endif
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   663
#endif
639d58d32471 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 544
diff changeset
   664
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
   665
    if (len < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   666
        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
   667
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   668
    r->size = len;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   669
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
   670
    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
   671
#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
   672
        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
   673
#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
   674
        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
   675
                            r->size);
6963
4658b1101200 Fixed bug 1553 - Fix USB joystick input for FreeBSD 9.0+
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   676
#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
   677
        if (r->buf == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   678
            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
   679
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   680
    } 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
   681
        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
   682
    }
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   683
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
   684
    r->status = SREPORT_CLEAN;
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6963
diff changeset
   685
    return 0;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   686
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   687
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   688
static void
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   689
report_free(struct report *r)
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   690
{
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   691
    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
   692
    r->status = SREPORT_UNINIT;
278
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   693
}
dcd9f7b50a1c Added support for joysticks on *BSD (thanks Wilbern!)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   694
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1565
diff changeset
   695
#endif /* SDL_JOYSTICK_USBHID */
6697
dbda91031456 Updated BSD joystick code for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 6401
diff changeset
   696
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
   697
/* vi: set ts=4 sw=4 expandtab: */