src/joystick/windows/SDL_mmjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Feb 2011 17:17:21 -0800
changeset 5328 f34a5f9ce9f6
parent 5262 b530ef003506
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed bug 1122 (spinlock fails to compile with -march=armv4t)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
     3
    Copyright (C) 1997-2011 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1135
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1135
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1135
diff changeset
    13
    Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1135
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1135
diff changeset
    16
    License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1135
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
    Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 165
diff changeset
    20
    slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    21
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    22
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    24
#ifdef SDL_JOYSTICK_WINMM
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    25
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
/* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
    28
#include "../../core/windows/SDL_windows.h"
1358
c71e05b4dc2e More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    29
#include <mmsystem.h>
c71e05b4dc2e More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    30
#include <regstr.h>
c71e05b4dc2e More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    31
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    32
#include "SDL_events.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#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
    34
#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
    35
#include "../SDL_joystick_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
531
8450e66651ea Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    37
#define MAX_JOYSTICKS	16
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    38
#define MAX_AXES	6       /* each joystick can have up to 6 axes */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    39
#define MAX_BUTTONS	32      /* and 32 buttons                      */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    41
#define AXIS_MAX	32767   /* maximum value for axis coordinate */
834
d37179d10ccc *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    42
/* limit axis to 256 possible positions to filter out noise */
d37179d10ccc *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    43
#define JOY_AXIS_THRESHOLD      (((AXIS_MAX)-(AXIS_MIN))/256)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    44
#define JOY_BUTTON_FLAG(n)	(1<<n)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    45
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    46
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    47
/* array to hold joystick ID values */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    48
static UINT SYS_JoystickID[MAX_JOYSTICKS];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    49
static JOYCAPS SYS_Joystick[MAX_JOYSTICKS];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    50
static char *SYS_JoystickName[MAX_JOYSTICKS];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    51
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    52
/* The private structure used to keep track of a joystick */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    53
struct joystick_hwdata
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    55
    /* joystick ID */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    56
    UINT id;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    57
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    58
    /* values used to translate device-specific coordinates into
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    59
       SDL-standard ranges */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    60
    struct _transaxis
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    61
    {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    62
        int offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    63
        float scale;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    64
    } transaxis[6];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    65
};
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    66
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    67
/* Convert a Windows Multimedia API return code to a text message */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    68
static void SetMMerror(char *function, int code);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    69
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    71
static char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    72
GetJoystickName(int index, const char *szRegKey)
937
1e6366bde299 Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 834
diff changeset
    73
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    74
    /* added 7/24/2004 by Eckhard Stolberg */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    75
    /*
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    76
       see if there is a joystick for the current
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    77
       index (1-16) listed in the registry
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    78
     */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    79
    char *name = NULL;
2176
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    80
    HKEY hTopKey;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    81
    HKEY hKey;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    82
    DWORD regsize;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
    83
    LONG regresult;
2176
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    84
    char regkey[256];
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    85
    char regvalue[256];
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    86
    char regname[256];
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    87
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    88
    SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    89
                 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    90
    hTopKey = HKEY_LOCAL_MACHINE;
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    91
    regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    92
    if (regresult != ERROR_SUCCESS) {
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    93
        hTopKey = HKEY_CURRENT_USER;
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    94
        regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    95
    }
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    96
    if (regresult != ERROR_SUCCESS) {
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    97
        return NULL;
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
    98
    }
937
1e6366bde299 Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 834
diff changeset
    99
2176
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   100
    /* find the registry key name for the joystick's properties */
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   101
    regsize = sizeof(regname);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   102
    SDL_snprintf(regvalue, SDL_arraysize(regvalue), "Joystick%d%s", index + 1,
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   103
                 REGSTR_VAL_JOYOEMNAME);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   104
    regresult =
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   105
        RegQueryValueExA(hKey, regvalue, 0, 0, (LPBYTE) regname, &regsize);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   106
    RegCloseKey(hKey);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   107
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   108
    if (regresult != ERROR_SUCCESS) {
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   109
        return NULL;
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   110
    }
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   111
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   112
    /* open that registry key */
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   113
    SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s", REGSTR_PATH_JOYOEM,
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   114
                 regname);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   115
    regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   116
    if (regresult != ERROR_SUCCESS) {
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   117
        return NULL;
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   118
    }
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   119
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   120
    /* find the size for the OEM name text */
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   121
    regsize = sizeof(regvalue);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   122
    regresult =
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   123
        RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0, NULL, &regsize);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   124
    if (regresult == ERROR_SUCCESS) {
2176
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   125
        /* allocate enough memory for the OEM name text ... */
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   126
        name = (char *) SDL_malloc(regsize);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   127
        if (name) {
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   128
            /* ... and read it from the registry */
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   129
            regresult = RegQueryValueExA(hKey,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   130
                                         REGSTR_VAL_JOYOEMNAME, 0, 0,
2176
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   131
                                         (LPBYTE) name, &regsize);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   132
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   133
    }
2176
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   134
    RegCloseKey(hKey);
cbe06fa842cd Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
Sam Lantinga <slouken@libsdl.org>
parents: 2061
diff changeset
   135
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   136
    return (name);
937
1e6366bde299 Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 834
diff changeset
   137
}
1e6366bde299 Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 834
diff changeset
   138
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   139
/* Function to scan the system for joysticks.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   140
 * This function should set SDL_numjoysticks to the number of available
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   141
 * joysticks.  Joystick 0 should be the system default joystick.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
 * It should return 0, or -1 on an unrecoverable fatal error.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   143
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   144
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   145
SDL_SYS_JoystickInit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   146
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   147
    int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   148
    int maxdevs;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   149
    int numdevs;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   150
    JOYINFOEX joyinfo;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   151
    JOYCAPS joycaps;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   152
    MMRESULT result;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   154
    /* Reset the joystick ID & name mapping tables */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   155
    for (i = 0; i < MAX_JOYSTICKS; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   156
        SYS_JoystickID[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: 1748
diff changeset
   157
        SYS_JoystickName[i] = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   158
    }
531
8450e66651ea Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   159
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   160
    /* Loop over all potential joystick devices */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   161
    numdevs = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   162
    maxdevs = joyGetNumDevs();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   163
    for (i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   164
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   165
        joyinfo.dwSize = sizeof(joyinfo);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   166
        joyinfo.dwFlags = JOY_RETURNALL;
2061
6051761b5934 Win32 joystick fix, ported from 1.2 branch r2887:2888. (Thanks, Ondrej!)
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   167
        result = joyGetPosEx(i, &joyinfo);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   168
        if (result == JOYERR_NOERROR) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   169
            result = joyGetDevCaps(i, &joycaps, sizeof(joycaps));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   170
            if (result == JOYERR_NOERROR) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   171
                SYS_JoystickID[numdevs] = i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   172
                SYS_Joystick[numdevs] = joycaps;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   173
                SYS_JoystickName[numdevs] =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   174
                    GetJoystickName(i, joycaps.szRegKey);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   175
                numdevs++;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   176
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   177
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   178
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   179
    return (numdevs);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   180
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   181
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   182
/* Function to get the device-dependent name of a 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: 1748
diff changeset
   183
const char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   184
SDL_SYS_JoystickName(int index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   185
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   186
    if (SYS_JoystickName[index] != NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   187
        return (SYS_JoystickName[index]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   188
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   189
        return (SYS_Joystick[index].szPname);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   190
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   191
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   192
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   193
/* Function to open a joystick for use.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   194
   The joystick to open is specified by the index field of the joystick.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   195
   This should fill the nbuttons and naxes fields of the joystick structure.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   196
   It returns 0, or -1 if there is an error.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   197
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   198
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   199
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 1748
diff changeset
   201
    int index, i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   202
    int caps_flags[MAX_AXES - 2] =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   203
        { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV };
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   204
    int axis_min[MAX_AXES], axis_max[MAX_AXES];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   205
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   206
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   207
    /* shortcut */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   208
    index = joystick->index;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   209
    axis_min[0] = SYS_Joystick[index].wXmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   210
    axis_max[0] = SYS_Joystick[index].wXmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   211
    axis_min[1] = SYS_Joystick[index].wYmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   212
    axis_max[1] = SYS_Joystick[index].wYmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   213
    axis_min[2] = SYS_Joystick[index].wZmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   214
    axis_max[2] = SYS_Joystick[index].wZmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   215
    axis_min[3] = SYS_Joystick[index].wRmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   216
    axis_max[3] = SYS_Joystick[index].wRmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   217
    axis_min[4] = SYS_Joystick[index].wUmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   218
    axis_max[4] = SYS_Joystick[index].wUmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   219
    axis_min[5] = SYS_Joystick[index].wVmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   220
    axis_max[5] = SYS_Joystick[index].wVmax;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   222
    /* allocate memory for system specific hardware data */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   223
    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: 1748
diff changeset
   224
        (struct joystick_hwdata *) SDL_malloc(sizeof(*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: 1748
diff changeset
   225
    if (joystick->hwdata == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   226
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   227
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   228
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   229
    SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   230
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   231
    /* set hardware data */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   232
    joystick->hwdata->id = SYS_JoystickID[index];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   233
    for (i = 0; i < MAX_AXES; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   234
        if ((i < 2) || (SYS_Joystick[index].wCaps & caps_flags[i - 2])) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   235
            joystick->hwdata->transaxis[i].offset = AXIS_MIN - axis_min[i];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   236
            joystick->hwdata->transaxis[i].scale =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   237
                (float) (AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   238
        } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   239
            joystick->hwdata->transaxis[i].offset = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   240
            joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   241
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   242
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   243
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   244
    /* fill nbuttons, naxes, and nhats fields */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   245
    joystick->nbuttons = SYS_Joystick[index].wNumButtons;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   246
    joystick->naxes = SYS_Joystick[index].wNumAxes;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   247
    if (SYS_Joystick[index].wCaps & JOYCAPS_HASPOV) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   248
        joystick->nhats = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   249
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   250
        joystick->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: 1748
diff changeset
   251
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   252
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   253
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   254
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   255
static Uint8
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   256
TranslatePOV(DWORD value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   257
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   258
    Uint8 pos;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   259
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   260
    pos = SDL_HAT_CENTERED;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   261
    if (value != JOY_POVCENTERED) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   262
        if ((value > JOY_POVLEFT) || (value < JOY_POVRIGHT)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   263
            pos |= SDL_HAT_UP;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   264
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   265
        if ((value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   266
            pos |= SDL_HAT_RIGHT;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   267
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   268
        if ((value > JOY_POVRIGHT) && (value < JOY_POVLEFT)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   269
            pos |= SDL_HAT_DOWN;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   270
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   271
        if (value > JOY_POVBACKWARD) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   272
            pos |= SDL_HAT_LEFT;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   273
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   274
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   275
    return (pos);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   276
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   277
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   278
/* Function to update the state of a joystick - called as a device poll.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   279
 * This function shouldn't update the joystick structure directly,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   280
 * but instead should call SDL_PrivateJoystick*() to deliver events
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   281
 * and update joystick device state.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   282
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   283
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   284
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   286
    MMRESULT result;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   287
    int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   288
    DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   289
        JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   290
    };
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   291
    DWORD pos[MAX_AXES];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   292
    struct _transaxis *transaxis;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   293
    int value, change;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   294
    JOYINFOEX joyinfo;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   295
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   296
    joyinfo.dwSize = sizeof(joyinfo);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   297
    joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   298
    if (!joystick->hats) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   299
        joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   300
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   301
    result = joyGetPosEx(joystick->hwdata->id, &joyinfo);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   302
    if (result != JOYERR_NOERROR) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   303
        SetMMerror("joyGetPosEx", result);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   304
        return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   305
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   306
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   307
    /* joystick motion events */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   308
    pos[0] = joyinfo.dwXpos;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   309
    pos[1] = joyinfo.dwYpos;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   310
    pos[2] = joyinfo.dwZpos;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   311
    pos[3] = joyinfo.dwRpos;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   312
    pos[4] = joyinfo.dwUpos;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   313
    pos[5] = joyinfo.dwVpos;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   314
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   315
    transaxis = joystick->hwdata->transaxis;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   316
    for (i = 0; i < joystick->naxes; i++) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   317
        if (joyinfo.dwFlags & flags[i]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   318
            value =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   319
                (int) (((float) pos[i] +
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   320
                        transaxis[i].offset) * transaxis[i].scale);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   321
            change = (value - joystick->axes[i]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   322
            if ((change < -JOY_AXIS_THRESHOLD)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   323
                || (change > JOY_AXIS_THRESHOLD)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   324
                SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   325
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   326
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   327
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   328
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   329
    /* joystick button events */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   330
    if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   331
        for (i = 0; i < joystick->nbuttons; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   332
            if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   333
                if (!joystick->buttons[i]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   334
                    SDL_PrivateJoystickButton(joystick, (Uint8) i,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   335
                                              SDL_PRESSED);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   336
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   337
            } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   338
                if (joystick->buttons[i]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   339
                    SDL_PrivateJoystickButton(joystick, (Uint8) i,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   340
                                              SDL_RELEASED);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   341
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   342
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   343
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   344
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   345
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   346
    /* joystick hat events */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   347
    if (joyinfo.dwFlags & JOY_RETURNPOV) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   348
        Uint8 pos;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   349
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   350
        pos = TranslatePOV(joyinfo.dwPOV);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   351
        if (pos != joystick->hats[0]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   352
            SDL_PrivateJoystickHat(joystick, 0, pos);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   353
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   354
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   355
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   356
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   357
/* Function to close a joystick after use */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   358
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   359
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   360
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   361
    if (joystick->hwdata != NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   362
        /* free system specific hardware data */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   363
        SDL_free(joystick->hwdata);
3610
3c1b39d0d1a6 Merged r4121:4122 from branches/SDL-1.2: win32 joystick double-free fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   364
        joystick->hwdata = 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: 1748
diff changeset
   365
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   366
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   367
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   368
/* Function to perform any system-specific joystick related cleanup */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   369
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   370
SDL_SYS_JoystickQuit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   371
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   372
    int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   373
    for (i = 0; i < MAX_JOYSTICKS; i++) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   374
        if (SYS_JoystickName[i] != NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   375
            SDL_free(SYS_JoystickName[i]);
3610
3c1b39d0d1a6 Merged r4121:4122 from branches/SDL-1.2: win32 joystick double-free fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   376
            SYS_JoystickName[i] = 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: 1748
diff changeset
   377
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   378
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   379
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   380
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   381
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   382
/* implementation functions */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   383
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   384
SetMMerror(char *function, int code)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   385
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   386
    static char *error;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   387
    static char errbuf[1024];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   388
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   389
    errbuf[0] = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   390
    switch (code) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   391
    case MMSYSERR_NODRIVER:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   392
        error = "Joystick driver not present";
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   393
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   394
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   395
    case MMSYSERR_INVALPARAM:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   396
    case JOYERR_PARMS:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   397
        error = "Invalid parameter(s)";
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   398
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   399
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   400
    case MMSYSERR_BADDEVICEID:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   401
        error = "Bad device ID";
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   402
        break;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   403
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   404
    case JOYERR_UNPLUGGED:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   405
        error = "Joystick not attached";
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   406
        break;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   407
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   408
    case JOYERR_NOCANDO:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   409
        error = "Can't capture joystick input";
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   410
        break;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   411
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   412
    default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   413
        SDL_snprintf(errbuf, SDL_arraysize(errbuf),
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   414
                     "%s: Unknown Multimedia system error: 0x%x",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   415
                     function, code);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   416
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   417
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   418
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   419
    if (!errbuf[0]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   420
        SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   421
                     error);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   422
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   423
    SDL_SetError("%s", errbuf);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   424
}
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
   425
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
   426
#endif /* SDL_JOYSTICK_WINMM */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1748
diff changeset
   427
/* vi: set ts=4 sw=4 expandtab: */