src/joystick/linux/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 01 May 2006 06:40:00 +0000
changeset 1752 97f20f2a1d85
parent 1635 92947e3a18db
child 1821 2f381b48b05c
permissions -rw-r--r--
SDL_sysjoystick.c:983: warning: 'return' with a value, in function returning void
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1635
    24
#ifdef SDL_JOYSTICK_LINUX
slouken@1635
    25
slouken@0
    26
/* This is the system specific header for the SDL joystick API */
slouken@0
    27
slouken@0
    28
#include <sys/stat.h>
slouken@0
    29
#include <unistd.h>
slouken@0
    30
#include <fcntl.h>
slouken@0
    31
#include <sys/ioctl.h>
slouken@0
    32
#include <limits.h>		/* For the definition of PATH_MAX */
slouken@377
    33
#ifdef __arm__
slouken@377
    34
#include <linux/limits.h> /* Arm cross-compiler needs this */
slouken@377
    35
#endif
slouken@0
    36
#include <linux/joystick.h>
slouken@1361
    37
#if SDL_INPUT_LINUXEV
slouken@0
    38
#include <linux/input.h>
slouken@0
    39
#endif
slouken@0
    40
slouken@0
    41
#include "SDL_joystick.h"
slouken@1361
    42
#include "../SDL_sysjoystick.h"
slouken@1361
    43
#include "../SDL_joystick_c.h"
slouken@0
    44
slouken@554
    45
/* Special joystick configurations */
slouken@554
    46
static struct {
slouken@554
    47
	const char *name;
slouken@554
    48
	int naxes;
slouken@554
    49
	int nhats;
slouken@554
    50
	int nballs;
slouken@554
    51
} special_joysticks[] = {
slouken@554
    52
	{ "MadCatz Panther XL", 3, 2, 1 }, /* We don't handle rudder (axis 8) */
slouken@554
    53
	{ "SideWinder Precision Pro", 4, 1, 0 },
slouken@554
    54
	{ "SideWinder 3D Pro", 4, 1, 0 },
slouken@554
    55
	{ "Microsoft SideWinder 3D Pro", 4, 1, 0 },
slouken@554
    56
	{ "Microsoft SideWinder Dual Strike USB version 1.0", 2, 1, 0 },
slouken@554
    57
	{ "WingMan Interceptor", 3, 3, 0 },
slouken@554
    58
	{ "WingMan Extreme Digital 3D", 4, 1, 0 },
slouken@554
    59
	{ "Microsoft SideWinder Precision 2 Joystick", 4, 1, 0 },
slouken@554
    60
	{ "Logitech Inc. WingMan Extreme Digital 3D", 4, 1, 0 },
slouken@554
    61
	{ "Saitek Saitek X45", 6, 1, 0 }
slouken@0
    62
};
slouken@0
    63
slouken@892
    64
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
    65
slouken@1551
    66
/*
slouken@1551
    67
   Some USB HIDs show up as a single joystick even though they actually
slouken@1551
    68
   control 2 or more joysticks.
slouken@1551
    69
*/
slouken@1551
    70
/*
slouken@1551
    71
   This code handles the MP-8800 (Quad) and MP-8866 (Dual), which can
slouken@1551
    72
   be identified by their transparent blue design. It's quite trivial
slouken@1551
    73
   to add other joysticks with similar quirky behavior.
slouken@1551
    74
   -id
slouken@1551
    75
*/
slouken@1551
    76
slouken@1551
    77
struct joystick_logical_mapping {
slouken@892
    78
        int njoy;
slouken@892
    79
        int nthing;
slouken@1551
    80
};
slouken@892
    81
slouken@1551
    82
/*
slouken@1551
    83
   {logical joy, logical axis},
slouken@1551
    84
   {logical joy, logical hat},
slouken@1551
    85
   {logical joy, logical ball},
slouken@1551
    86
   {logical joy, logical button}
slouken@1551
    87
*/
slouken@1551
    88
slouken@1551
    89
static struct joystick_logical_mapping mp88xx_1_logical_axismap[] = {
slouken@1551
    90
   {0,0},{0,1},{0,2},{0,3},{0,4},{0,5}
slouken@1551
    91
};
slouken@1551
    92
static struct joystick_logical_mapping mp88xx_1_logical_buttonmap[] = {
slouken@1551
    93
   {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11}
slouken@1551
    94
};
slouken@892
    95
slouken@1551
    96
static struct joystick_logical_mapping mp88xx_2_logical_axismap[] = {
slouken@1551
    97
   {0,0},{0,1},{0,2},{1,0},{1,1},{0,3},
slouken@1551
    98
   {1,2},{1,3},{0,4},{0,5},{1,4},{1,5}
slouken@1551
    99
};
slouken@1551
   100
static struct joystick_logical_mapping mp88xx_2_logical_buttonmap[] = {
slouken@1551
   101
   {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11},
slouken@1551
   102
   {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11}
slouken@1551
   103
};
slouken@892
   104
slouken@1551
   105
static struct joystick_logical_mapping mp88xx_3_logical_axismap[] = {
slouken@1551
   106
   {0,0},{0,1},{0,2},{1,0},{1,1},{0,3},
slouken@1551
   107
   {1,2},{1,3},{2,0},{2,1},{2,2},{2,3},
slouken@1551
   108
   {0,4},{0,5},{1,4},{1,5},{2,4},{2,5}
slouken@1551
   109
};
slouken@1551
   110
static struct joystick_logical_mapping mp88xx_3_logical_buttonmap[] = {
slouken@1551
   111
   {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11},
slouken@1551
   112
   {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11},
slouken@1551
   113
   {2,0},{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},{2,7},{2,8},{2,9},{2,10},{2,11}
slouken@1551
   114
};
slouken@1551
   115
slouken@1551
   116
static struct joystick_logical_mapping mp88xx_4_logical_axismap[] = {
slouken@1551
   117
   {0,0},{0,1},{0,2},{1,0},{1,1},{0,3},
slouken@1551
   118
   {1,2},{1,3},{2,0},{2,1},{2,2},{2,3},
slouken@1551
   119
   {3,0},{3,1},{3,2},{3,3},{0,4},{0,5},
slouken@1551
   120
   {1,4},{1,5},{2,4},{2,5},{3,4},{3,5}
slouken@1551
   121
};
slouken@1551
   122
static struct joystick_logical_mapping mp88xx_4_logical_buttonmap[] = {
slouken@892
   123
   {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11},
slouken@892
   124
   {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11},
slouken@892
   125
   {2,0},{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},{2,7},{2,8},{2,9},{2,10},{2,11},
slouken@1305
   126
   {3,0},{3,1},{3,2},{3,3},{3,4},{3,5},{3,6},{3,7},{3,8},{3,9},{3,10},{3,11}
slouken@892
   127
};
slouken@892
   128
slouken@1551
   129
struct joystick_logical_layout {
slouken@892
   130
        int naxes;
slouken@892
   131
        int nhats;
slouken@892
   132
        int nballs;
slouken@892
   133
        int nbuttons;
slouken@1551
   134
};
slouken@1551
   135
slouken@1551
   136
static struct joystick_logical_layout mp88xx_1_logical_layout[] = {
slouken@1551
   137
        {6, 0, 0, 12}
slouken@1551
   138
};
slouken@1551
   139
static struct joystick_logical_layout mp88xx_2_logical_layout[] = {
slouken@1551
   140
        {6, 0, 0, 12},
slouken@1551
   141
        {6, 0, 0, 12}
slouken@1551
   142
};
slouken@1551
   143
static struct joystick_logical_layout mp88xx_3_logical_layout[] = {
slouken@1551
   144
        {6, 0, 0, 12},
slouken@1551
   145
        {6, 0, 0, 12},
slouken@1551
   146
        {6, 0, 0, 12}
slouken@1551
   147
};
slouken@1551
   148
static struct joystick_logical_layout mp88xx_4_logical_layout[] = {
slouken@1551
   149
        {6, 0, 0, 12},
slouken@1551
   150
        {6, 0, 0, 12},
slouken@1551
   151
        {6, 0, 0, 12},
slouken@1551
   152
        {6, 0, 0, 12}
slouken@892
   153
};
slouken@892
   154
slouken@892
   155
/*
slouken@1551
   156
   This array sets up a means of mapping a single physical joystick to
slouken@1551
   157
   multiple logical joysticks. (djm)
slouken@892
   158
                                                                                
slouken@892
   159
   njoys
slouken@892
   160
        the number of logical joysticks
slouken@892
   161
                                                                                
slouken@892
   162
   layouts
slouken@892
   163
        an array of layout structures, one to describe each logical joystick
slouken@892
   164
                                                                                
slouken@892
   165
   axes, hats, balls, buttons
slouken@892
   166
        arrays that map a physical thingy to a logical thingy
slouken@892
   167
 */
slouken@1551
   168
struct joystick_logicalmap {
slouken@892
   169
        const char *name;
slouken@1551
   170
	int nbuttons;
slouken@892
   171
        int njoys;
slouken@1551
   172
        struct joystick_logical_layout *layout;
slouken@1551
   173
        struct joystick_logical_mapping *axismap;
slouken@1551
   174
        struct joystick_logical_mapping *hatmap;
slouken@1551
   175
        struct joystick_logical_mapping *ballmap;
slouken@1551
   176
        struct joystick_logical_mapping *buttonmap;
slouken@1551
   177
};
slouken@892
   178
slouken@1551
   179
static struct joystick_logicalmap joystick_logicalmap[] = {
slouken@1551
   180
        {
slouken@1551
   181
		"WiseGroup.,Ltd MP-8866 Dual USB Joypad",
slouken@1551
   182
		12,
slouken@1551
   183
		1,
slouken@1551
   184
		mp88xx_1_logical_layout,
slouken@1551
   185
        	mp88xx_1_logical_axismap,
slouken@1551
   186
		NULL,
slouken@1551
   187
		NULL,
slouken@1551
   188
        	mp88xx_1_logical_buttonmap
slouken@1551
   189
	},
slouken@1551
   190
        {
slouken@1551
   191
		"WiseGroup.,Ltd MP-8866 Dual USB Joypad",
slouken@1551
   192
		24,
slouken@1551
   193
		2,
slouken@1551
   194
		mp88xx_2_logical_layout,
slouken@1551
   195
        	mp88xx_2_logical_axismap,
slouken@1551
   196
		NULL,
slouken@1551
   197
		NULL,
slouken@1551
   198
        	mp88xx_2_logical_buttonmap
slouken@1551
   199
	},
slouken@1551
   200
        {
slouken@1551
   201
		"WiseGroup.,Ltd MP-8800 Quad USB Joypad",
slouken@1551
   202
		12,
slouken@1551
   203
		1,
slouken@1551
   204
		mp88xx_1_logical_layout,
slouken@1551
   205
        	mp88xx_1_logical_axismap,
slouken@1551
   206
		NULL,
slouken@1551
   207
		NULL,
slouken@1551
   208
        	mp88xx_1_logical_buttonmap
slouken@1551
   209
	},
slouken@1551
   210
        {
slouken@1551
   211
		"WiseGroup.,Ltd MP-8800 Quad USB Joypad",
slouken@1551
   212
		24,
slouken@1551
   213
		2,
slouken@1551
   214
		mp88xx_2_logical_layout,
slouken@1551
   215
        	mp88xx_2_logical_axismap,
slouken@1551
   216
		NULL,
slouken@1551
   217
		NULL,
slouken@1551
   218
        	mp88xx_2_logical_buttonmap
slouken@1551
   219
	},
slouken@1551
   220
        {
slouken@1551
   221
		"WiseGroup.,Ltd MP-8800 Quad USB Joypad",
slouken@1551
   222
		36,
slouken@1551
   223
		3,
slouken@1551
   224
		mp88xx_3_logical_layout,
slouken@1551
   225
        	mp88xx_3_logical_axismap,
slouken@1551
   226
		NULL,
slouken@1551
   227
		NULL,
slouken@1551
   228
        	mp88xx_3_logical_buttonmap
slouken@1551
   229
	},
slouken@1551
   230
        {
slouken@1551
   231
		"WiseGroup.,Ltd MP-8800 Quad USB Joypad",
slouken@1551
   232
		48,
slouken@1551
   233
		4,
slouken@1551
   234
		mp88xx_4_logical_layout,
slouken@1551
   235
        	mp88xx_4_logical_axismap,
slouken@1551
   236
		NULL,
slouken@1551
   237
		NULL,
slouken@1551
   238
        	mp88xx_4_logical_buttonmap
slouken@1551
   239
	}
slouken@892
   240
};
slouken@892
   241
slouken@892
   242
/* find the head of a linked list, given a point in it
slouken@892
   243
 */
slouken@892
   244
#define SDL_joylist_head(i, start)\
slouken@892
   245
        for(i = start; SDL_joylist[i].fname == NULL;) i = SDL_joylist[i].prev;
slouken@892
   246
slouken@892
   247
#define SDL_logical_joydecl(d) d
slouken@892
   248
slouken@892
   249
slouken@892
   250
#else
slouken@892
   251
slouken@892
   252
#define SDL_logical_joydecl(d)
slouken@892
   253
slouken@892
   254
#endif /* USE_LOGICAL_JOYSTICKS */
slouken@892
   255
slouken@0
   256
/* The maximum number of joysticks we'll detect */
slouken@0
   257
#define MAX_JOYSTICKS	32
slouken@0
   258
slouken@0
   259
/* A list of available joysticks */
slouken@892
   260
static struct
slouken@892
   261
{
slouken@892
   262
        char* fname;
slouken@892
   263
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   264
        SDL_Joystick* joy;
slouken@892
   265
        struct joystick_logicalmap* map;
slouken@892
   266
        int prev;
slouken@892
   267
        int next;
slouken@892
   268
        int logicalno;
slouken@892
   269
#endif /* USE_LOGICAL_JOYSTICKS */
slouken@892
   270
} SDL_joylist[MAX_JOYSTICKS];
slouken@892
   271
slouken@0
   272
slouken@0
   273
/* The private structure used to keep track of a joystick */
slouken@0
   274
struct joystick_hwdata {
slouken@0
   275
	int fd;
slouken@0
   276
	/* The current linux joystick driver maps hats to two axes */
slouken@0
   277
	struct hwdata_hat {
slouken@0
   278
		int axis[2];
slouken@0
   279
	} *hats;
slouken@0
   280
	/* The current linux joystick driver maps balls to two axes */
slouken@0
   281
	struct hwdata_ball {
slouken@0
   282
		int axis[2];
slouken@0
   283
	} *balls;
slouken@0
   284
slouken@0
   285
	/* Support for the Linux 2.4 unified input interface */
slouken@1361
   286
#if SDL_INPUT_LINUXEV
slouken@0
   287
	SDL_bool is_hid;
slouken@0
   288
	Uint8 key_map[KEY_MAX-BTN_MISC];
slouken@0
   289
	Uint8 abs_map[ABS_MAX];
slouken@0
   290
	struct axis_correct {
slouken@0
   291
		int used;
slouken@0
   292
		int coef[3];
slouken@0
   293
	} abs_correct[ABS_MAX];
slouken@0
   294
#endif
slouken@0
   295
};
slouken@0
   296
slouken@892
   297
slouken@892
   298
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   299
slouken@892
   300
static int CountLogicalJoysticks(int max)
slouken@892
   301
{
slouken@892
   302
   register int i, j, k, ret, prev;
slouken@892
   303
   const char* name;
slouken@1551
   304
   int nbuttons, fd;
slouken@1551
   305
   unsigned char n;
slouken@892
   306
slouken@892
   307
   ret = 0;
slouken@892
   308
slouken@892
   309
   for(i = 0; i < max; i++) {
slouken@892
   310
      name = SDL_SYS_JoystickName(i);
slouken@1551
   311
	
slouken@1551
   312
      fd = open(SDL_joylist[i].fname, O_RDONLY, 0);
slouken@1551
   313
      if ( fd >= 0 ) {
slouken@1551
   314
	 if ( ioctl(fd, JSIOCGBUTTONS, &n) < 0 ) {
slouken@1551
   315
	    nbuttons = -1;
slouken@1551
   316
	 } else {
slouken@1551
   317
            nbuttons = n;
slouken@1551
   318
	 }
slouken@1551
   319
	 close(fd);
slouken@1551
   320
      }
slouken@1551
   321
      else {
slouken@1551
   322
	 nbuttons=-1;
slouken@1551
   323
      }
slouken@892
   324
slouken@892
   325
      if (name) {
slouken@1379
   326
         for(j = 0; j < SDL_arraysize(joystick_logicalmap); j++) {
slouken@1551
   327
            if (!SDL_strcmp(name, joystick_logicalmap[j].name) && (nbuttons==-1 || nbuttons==joystick_logicalmap[j].nbuttons)) {
slouken@892
   328
               prev = i;
slouken@1551
   329
               SDL_joylist[prev].map = &(joystick_logicalmap[j]);
slouken@892
   330
slouken@892
   331
               for(k = 1; k < joystick_logicalmap[j].njoys; k++) {
slouken@892
   332
                  SDL_joylist[prev].next = max + ret;
slouken@1551
   333
                  SDL_joylist[max+ret].prev = prev;
slouken@1551
   334
		  
slouken@892
   335
                  prev = max + ret;
slouken@892
   336
                  SDL_joylist[prev].logicalno = k;
slouken@1551
   337
                  SDL_joylist[prev].map = &(joystick_logicalmap[j]);
slouken@892
   338
                  ret++;
slouken@892
   339
               }
slouken@892
   340
slouken@892
   341
               break;
slouken@892
   342
            }
slouken@892
   343
         }
slouken@892
   344
      }
slouken@892
   345
   }
slouken@892
   346
slouken@892
   347
   return ret;
slouken@892
   348
}
slouken@892
   349
slouken@892
   350
static void LogicalSuffix(int logicalno, char* namebuf, int len)
slouken@892
   351
{
slouken@892
   352
   register int slen;
slouken@892
   353
   const static char suffixs[] =
slouken@892
   354
      "01020304050607080910111213141516171819"
slouken@892
   355
      "20212223242526272829303132";
slouken@892
   356
   const char* suffix;
slouken@1336
   357
   slen = SDL_strlen(namebuf);
slouken@892
   358
   suffix = NULL;
slouken@892
   359
slouken@892
   360
   if (logicalno*2<sizeof(suffixs))
slouken@892
   361
      suffix = suffixs + (logicalno*2);
slouken@892
   362
slouken@892
   363
   if (slen + 4 < len && suffix) {
slouken@892
   364
      namebuf[slen++] = ' ';
slouken@892
   365
      namebuf[slen++] = '#';
slouken@892
   366
      namebuf[slen++] = suffix[0];
slouken@892
   367
      namebuf[slen++] = suffix[1];
slouken@892
   368
      namebuf[slen++] = 0;
slouken@892
   369
   }
slouken@892
   370
}
slouken@892
   371
slouken@892
   372
#endif /* USE_LOGICAL_JOYSTICKS */
slouken@892
   373
slouken@1361
   374
#if SDL_INPUT_LINUXEV
slouken@0
   375
#define test_bit(nr, addr) \
slouken@0
   376
	(((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
slouken@0
   377
slouken@0
   378
static int EV_IsJoystick(int fd)
slouken@0
   379
{
slouken@0
   380
	unsigned long evbit[40];
slouken@0
   381
	unsigned long keybit[40];
slouken@0
   382
	unsigned long absbit[40];
slouken@0
   383
slouken@0
   384
	if ( (ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
slouken@0
   385
	     (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
slouken@0
   386
	     (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0) ) {
slouken@0
   387
		return(0);
slouken@0
   388
	}
slouken@0
   389
	if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
slouken@0
   390
	      test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit) &&
slouken@0
   391
	     (test_bit(BTN_TRIGGER, keybit) || test_bit(BTN_A, keybit) || test_bit(BTN_1, keybit)))) return 0;
slouken@0
   392
	return(1);
slouken@0
   393
}
slouken@0
   394
slouken@1361
   395
#endif /* SDL_INPUT_LINUXEV */
slouken@0
   396
slouken@0
   397
/* Function to scan the system for joysticks */
slouken@0
   398
int SDL_SYS_JoystickInit(void)
slouken@0
   399
{
slouken@0
   400
	/* The base path of the joystick devices */
slouken@245
   401
	const char *joydev_pattern[] = {
slouken@1361
   402
#if SDL_INPUT_LINUXEV
slouken@245
   403
		"/dev/input/event%d",
slouken@211
   404
#endif
slouken@554
   405
		"/dev/input/js%d",
slouken@554
   406
		"/dev/js%d"
slouken@0
   407
	};
slouken@0
   408
	int numjoysticks;
slouken@554
   409
	int i, j;
slouken@0
   410
	int fd;
slouken@0
   411
	char path[PATH_MAX];
slouken@0
   412
	dev_t dev_nums[MAX_JOYSTICKS];  /* major/minor device numbers */
slouken@0
   413
	struct stat sb;
slouken@0
   414
	int n, duplicate;
slouken@0
   415
slouken@0
   416
	numjoysticks = 0;
slouken@0
   417
slouken@0
   418
	/* First see if the user specified a joystick to use */
slouken@1336
   419
	if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) {
slouken@1379
   420
		SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
slouken@0
   421
		if ( stat(path, &sb) == 0 ) {
slouken@0
   422
			fd = open(path, O_RDONLY, 0);
slouken@0
   423
			if ( fd >= 0 ) {
slouken@0
   424
				/* Assume the user knows what they're doing. */
slouken@1379
   425
				SDL_joylist[numjoysticks].fname = SDL_strdup(path);
slouken@892
   426
				if ( SDL_joylist[numjoysticks].fname ) {
slouken@0
   427
					dev_nums[numjoysticks] = sb.st_rdev;
slouken@0
   428
					++numjoysticks;
slouken@0
   429
				}
slouken@0
   430
				close(fd);
slouken@0
   431
			}
slouken@0
   432
		}
slouken@0
   433
	}
slouken@554
   434
slouken@1379
   435
	for ( i=0; i<SDL_arraysize(joydev_pattern); ++i ) {
slouken@554
   436
		for ( j=0; j < MAX_JOYSTICKS; ++j ) {
slouken@1338
   437
			SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j);
slouken@0
   438
slouken@0
   439
			/* rcg06302000 replaced access(F_OK) call with stat().
slouken@0
   440
			 * stat() will fail if the file doesn't exist, so it's
slouken@0
   441
			 * equivalent behaviour.
slouken@0
   442
			 */
slouken@0
   443
			if ( stat(path, &sb) == 0 ) {
slouken@0
   444
				/* Check to make sure it's not already in list.
slouken@0
   445
				 * This happens when we see a stick via symlink.
slouken@0
   446
				 */
slouken@0
   447
				duplicate = 0;
slouken@0
   448
				for (n=0; (n<numjoysticks) && !duplicate; ++n) {
slouken@0
   449
					if ( sb.st_rdev == dev_nums[n] ) {
slouken@0
   450
						duplicate = 1;
slouken@0
   451
					}
slouken@0
   452
				}
slouken@0
   453
				if (duplicate) {
slouken@0
   454
					continue;
slouken@0
   455
				}
slouken@0
   456
slouken@0
   457
				fd = open(path, O_RDONLY, 0);
slouken@0
   458
				if ( fd < 0 ) {
slouken@0
   459
					continue;
slouken@0
   460
				}
slouken@1361
   461
#if SDL_INPUT_LINUXEV
slouken@0
   462
#ifdef DEBUG_INPUT_EVENTS
slouken@0
   463
				printf("Checking %s\n", path);
slouken@0
   464
#endif
slouken@554
   465
				if ( (i == 0) && ! EV_IsJoystick(fd) ) {
slouken@0
   466
					close(fd);
slouken@0
   467
					continue;
slouken@0
   468
				}
slouken@0
   469
#endif
slouken@0
   470
				close(fd);
slouken@0
   471
slouken@0
   472
				/* We're fine, add this joystick */
slouken@1379
   473
				SDL_joylist[numjoysticks].fname = SDL_strdup(path);
slouken@892
   474
				if ( SDL_joylist[numjoysticks].fname ) {
slouken@0
   475
					dev_nums[numjoysticks] = sb.st_rdev;
slouken@0
   476
					++numjoysticks;
slouken@0
   477
				}
slouken@554
   478
			} else
slouken@554
   479
				break;
slouken@0
   480
		}
slouken@554
   481
slouken@1361
   482
#if SDL_INPUT_LINUXEV
slouken@554
   483
		/* This is a special case...
slouken@554
   484
		   If the event devices are valid then the joystick devices
slouken@554
   485
		   will be duplicates but without extra information about their
slouken@554
   486
		   hats or balls. Unfortunately, the event devices can't
slouken@554
   487
		   currently be calibrated, so it's a win-lose situation.
slouken@554
   488
		   So : /dev/input/eventX = /dev/input/jsY = /dev/jsY
slouken@554
   489
		*/
slouken@554
   490
		if ( (i == 0) && (numjoysticks > 0) )
slouken@554
   491
			break;
slouken@554
   492
#endif
slouken@0
   493
	}
slouken@892
   494
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   495
	numjoysticks += CountLogicalJoysticks(numjoysticks);
slouken@892
   496
#endif
slouken@554
   497
slouken@0
   498
	return(numjoysticks);
slouken@0
   499
}
slouken@0
   500
slouken@0
   501
/* Function to get the device-dependent name of a joystick */
slouken@0
   502
const char *SDL_SYS_JoystickName(int index)
slouken@0
   503
{
slouken@0
   504
	int fd;
slouken@0
   505
	static char namebuf[128];
slouken@0
   506
	char *name;
slouken@892
   507
	SDL_logical_joydecl(int oindex = index);
slouken@0
   508
slouken@892
   509
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   510
	SDL_joylist_head(index, index);
slouken@892
   511
#endif
slouken@0
   512
	name = NULL;
slouken@892
   513
	fd = open(SDL_joylist[index].fname, O_RDONLY, 0);
slouken@0
   514
	if ( fd >= 0 ) {
slouken@0
   515
		if ( 
slouken@1361
   516
#if SDL_INPUT_LINUXEV
slouken@0
   517
		     (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) &&
slouken@0
   518
#endif
slouken@0
   519
		     (ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0) ) {
slouken@892
   520
			name = SDL_joylist[index].fname;
slouken@0
   521
		} else {
slouken@0
   522
			name = namebuf;
slouken@0
   523
		}
slouken@0
   524
		close(fd);
slouken@892
   525
slouken@1551
   526
slouken@892
   527
#ifndef NO_LOGICAL_JOYSTICKS
slouken@1551
   528
		if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next || index!=oindex)
slouken@1551
   529
		{
slouken@892
   530
       		   LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128);
slouken@1551
   531
		}
slouken@892
   532
#endif
slouken@0
   533
	}
slouken@0
   534
	return name;
slouken@0
   535
}
slouken@0
   536
slouken@0
   537
static int allocate_hatdata(SDL_Joystick *joystick)
slouken@0
   538
{
slouken@0
   539
	int i;
slouken@0
   540
slouken@1336
   541
	joystick->hwdata->hats = (struct hwdata_hat *)SDL_malloc(
slouken@0
   542
		joystick->nhats * sizeof(struct hwdata_hat));
slouken@0
   543
	if ( joystick->hwdata->hats == NULL ) {
slouken@0
   544
		return(-1);
slouken@0
   545
	}
slouken@0
   546
	for ( i=0; i<joystick->nhats; ++i ) {
slouken@0
   547
		joystick->hwdata->hats[i].axis[0] = 1;
slouken@0
   548
		joystick->hwdata->hats[i].axis[1] = 1;
slouken@0
   549
	}
slouken@0
   550
	return(0);
slouken@0
   551
}
slouken@0
   552
slouken@0
   553
static int allocate_balldata(SDL_Joystick *joystick)
slouken@0
   554
{
slouken@0
   555
	int i;
slouken@0
   556
slouken@1336
   557
	joystick->hwdata->balls = (struct hwdata_ball *)SDL_malloc(
slouken@0
   558
		joystick->nballs * sizeof(struct hwdata_ball));
slouken@0
   559
	if ( joystick->hwdata->balls == NULL ) {
slouken@0
   560
		return(-1);
slouken@0
   561
	}
slouken@0
   562
	for ( i=0; i<joystick->nballs; ++i ) {
slouken@0
   563
		joystick->hwdata->balls[i].axis[0] = 0;
slouken@0
   564
		joystick->hwdata->balls[i].axis[1] = 0;
slouken@0
   565
	}
slouken@0
   566
	return(0);
slouken@0
   567
}
slouken@0
   568
slouken@554
   569
static SDL_bool JS_ConfigJoystick(SDL_Joystick *joystick, int fd)
slouken@0
   570
{
slouken@0
   571
	SDL_bool handled;
slouken@554
   572
	unsigned char n;
slouken@554
   573
	int old_axes, tmp_naxes, tmp_nhats, tmp_nballs;
slouken@554
   574
	const char *name;
slouken@554
   575
	char *env, env_name[128];
slouken@554
   576
	int i;
slouken@554
   577
slouken@554
   578
	handled = SDL_FALSE;
slouken@0
   579
slouken@554
   580
	/* Default joystick device settings */
slouken@554
   581
	if ( ioctl(fd, JSIOCGAXES, &n) < 0 ) {
slouken@554
   582
		joystick->naxes = 2;
slouken@554
   583
	} else {
slouken@554
   584
		joystick->naxes = n;
slouken@554
   585
	}
slouken@554
   586
	if ( ioctl(fd, JSIOCGBUTTONS, &n) < 0 ) {
slouken@554
   587
		joystick->nbuttons = 2;
slouken@554
   588
	} else {
slouken@554
   589
		joystick->nbuttons = n;
slouken@554
   590
	}
slouken@554
   591
slouken@554
   592
	name = SDL_SYS_JoystickName(joystick->index);
slouken@554
   593
	old_axes = joystick->naxes;
slouken@554
   594
slouken@554
   595
	/* Generic analog joystick support */
slouken@1336
   596
	if ( SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat") ) {
slouken@1336
   597
		if ( SDL_sscanf(name,"Analog %d-axis %*d-button %d-hat",
slouken@554
   598
			&tmp_naxes, &tmp_nhats) == 2 ) {
slouken@554
   599
slouken@554
   600
			joystick->naxes = tmp_naxes;
slouken@554
   601
			joystick->nhats = tmp_nhats;
slouken@554
   602
slouken@554
   603
			handled = SDL_TRUE;
slouken@554
   604
		}
slouken@0
   605
	}
slouken@554
   606
slouken@554
   607
	/* Special joystick support */
slouken@1379
   608
	for ( i=0; i < SDL_arraysize(special_joysticks); ++i ) {
slouken@1336
   609
		if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) {
slouken@554
   610
slouken@554
   611
			joystick->naxes = special_joysticks[i].naxes;
slouken@554
   612
			joystick->nhats = special_joysticks[i].nhats;
slouken@554
   613
			joystick->nballs = special_joysticks[i].nballs;
slouken@554
   614
slouken@554
   615
			handled = SDL_TRUE;
slouken@554
   616
			break;
slouken@554
   617
		}
slouken@0
   618
	}
slouken@554
   619
slouken@554
   620
	/* User environment joystick support */
slouken@1336
   621
	if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) {
slouken@1379
   622
		*env_name = '\0';
slouken@1336
   623
		if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 )
slouken@1336
   624
			env += SDL_strlen(env_name)+2;
slouken@1336
   625
		else if ( SDL_sscanf(env, "%s", env_name) == 1 )
slouken@1336
   626
			env += SDL_strlen(env_name);
slouken@554
   627
slouken@1336
   628
		if ( SDL_strcmp(name, env_name) == 0 ) {
slouken@0
   629
slouken@1336
   630
			if ( SDL_sscanf(env, "%d %d %d", &tmp_naxes, &tmp_nhats,
slouken@554
   631
				&tmp_nballs) == 3 ) {
slouken@554
   632
slouken@554
   633
				joystick->naxes = tmp_naxes;
slouken@554
   634
				joystick->nhats = tmp_nhats;
slouken@554
   635
				joystick->nballs = tmp_nballs;
slouken@554
   636
slouken@554
   637
				handled = SDL_TRUE;
slouken@554
   638
			}
slouken@554
   639
		}
slouken@554
   640
	}
slouken@554
   641
slouken@554
   642
	/* Remap hats and balls */
slouken@554
   643
	if (handled) {
slouken@0
   644
		if ( joystick->nhats > 0 ) {
slouken@589
   645
			if ( allocate_hatdata(joystick) < 0 ) {
slouken@589
   646
				joystick->nhats = 0;
slouken@0
   647
			}
slouken@0
   648
		}
slouken@0
   649
		if ( joystick->nballs > 0 ) {
slouken@0
   650
			if ( allocate_balldata(joystick) < 0 ) {
slouken@0
   651
				joystick->nballs = 0;
slouken@0
   652
			}
slouken@0
   653
		}
slouken@0
   654
	}
slouken@554
   655
slouken@0
   656
	return(handled);
slouken@0
   657
}
slouken@0
   658
slouken@1361
   659
#if SDL_INPUT_LINUXEV
slouken@0
   660
slouken@0
   661
static SDL_bool EV_ConfigJoystick(SDL_Joystick *joystick, int fd)
slouken@0
   662
{
slouken@871
   663
	int i, t;
slouken@0
   664
	unsigned long keybit[40];
slouken@0
   665
	unsigned long absbit[40];
slouken@0
   666
	unsigned long relbit[40];
slouken@0
   667
slouken@0
   668
	/* See if this device uses the new unified event API */
slouken@0
   669
	if ( (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
slouken@0
   670
	     (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
slouken@0
   671
	     (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0) ) {
slouken@0
   672
		joystick->hwdata->is_hid = SDL_TRUE;
slouken@0
   673
slouken@0
   674
		/* Get the number of buttons, axes, and other thingamajigs */
slouken@0
   675
		for ( i=BTN_JOYSTICK; i < KEY_MAX; ++i ) {
slouken@0
   676
			if ( test_bit(i, keybit) ) {
slouken@0
   677
#ifdef DEBUG_INPUT_EVENTS
slouken@0
   678
				printf("Joystick has button: 0x%x\n", i);
slouken@0
   679
#endif
slouken@0
   680
				joystick->hwdata->key_map[i-BTN_MISC] =
slouken@0
   681
						joystick->nbuttons;
slouken@0
   682
				++joystick->nbuttons;
slouken@0
   683
			}
slouken@0
   684
		}
slouken@0
   685
		for ( i=BTN_MISC; i < BTN_JOYSTICK; ++i ) {
slouken@0
   686
			if ( test_bit(i, keybit) ) {
slouken@0
   687
#ifdef DEBUG_INPUT_EVENTS
slouken@0
   688
				printf("Joystick has button: 0x%x\n", i);
slouken@0
   689
#endif
slouken@0
   690
				joystick->hwdata->key_map[i-BTN_MISC] =
slouken@0
   691
						joystick->nbuttons;
slouken@0
   692
				++joystick->nbuttons;
slouken@0
   693
			}
slouken@0
   694
		}
slouken@0
   695
		for ( i=0; i<ABS_MAX; ++i ) {
slouken@0
   696
			/* Skip hats */
slouken@0
   697
			if ( i == ABS_HAT0X ) {
slouken@0
   698
				i = ABS_HAT3Y;
slouken@0
   699
				continue;
slouken@0
   700
			}
slouken@0
   701
			if ( test_bit(i, absbit) ) {
slouken@0
   702
				int values[5];
slouken@0
   703
slouken@896
   704
				if ( ioctl(fd, EVIOCGABS(i), values) < 0 )
slouken@896
   705
					continue;
slouken@0
   706
#ifdef DEBUG_INPUT_EVENTS
slouken@0
   707
				printf("Joystick has absolute axis: %x\n", i);
slouken@0
   708
				printf("Values = { %d, %d, %d, %d, %d }\n",
slouken@0
   709
					values[0], values[1],
slouken@0
   710
					values[2], values[3], values[4]);
slouken@0
   711
#endif /* DEBUG_INPUT_EVENTS */
slouken@0
   712
				joystick->hwdata->abs_map[i] = joystick->naxes;
slouken@0
   713
				if ( values[1] == values[2] ) {
slouken@0
   714
				    joystick->hwdata->abs_correct[i].used = 0;
slouken@0
   715
				} else {
slouken@0
   716
				    joystick->hwdata->abs_correct[i].used = 1;
slouken@872
   717
				    joystick->hwdata->abs_correct[i].coef[0] =
slouken@872
   718
					(values[2] + values[1]) / 2 - values[4];
slouken@872
   719
				    joystick->hwdata->abs_correct[i].coef[1] =
slouken@872
   720
					(values[2] + values[1]) / 2 + values[4];
slouken@871
   721
				    t = ((values[2] - values[1]) / 2 - 2 * values[4]);
slouken@871
   722
				    if ( t != 0 ) {
slouken@871
   723
					joystick->hwdata->abs_correct[i].coef[2] = (1 << 29) / t;
slouken@872
   724
				    } else {
slouken@872
   725
					joystick->hwdata->abs_correct[i].coef[2] = 0;
slouken@871
   726
				    }
slouken@0
   727
				}
slouken@0
   728
				++joystick->naxes;
slouken@0
   729
			}
slouken@0
   730
		}
slouken@0
   731
		for ( i=ABS_HAT0X; i <= ABS_HAT3Y; i += 2 ) {
slouken@0
   732
			if ( test_bit(i, absbit) || test_bit(i+1, absbit) ) {
slouken@0
   733
#ifdef DEBUG_INPUT_EVENTS
slouken@0
   734
				printf("Joystick has hat %d\n",(i-ABS_HAT0X)/2);
slouken@0
   735
#endif
slouken@0
   736
				++joystick->nhats;
slouken@0
   737
			}
slouken@0
   738
		}
slouken@0
   739
		if ( test_bit(REL_X, relbit) || test_bit(REL_Y, relbit) ) {
slouken@0
   740
			++joystick->nballs;
slouken@0
   741
		}
slouken@0
   742
slouken@0
   743
		/* Allocate data to keep track of these thingamajigs */
slouken@0
   744
		if ( joystick->nhats > 0 ) {
slouken@0
   745
			if ( allocate_hatdata(joystick) < 0 ) {
slouken@0
   746
				joystick->nhats = 0;
slouken@0
   747
			}
slouken@0
   748
		}
slouken@0
   749
		if ( joystick->nballs > 0 ) {
slouken@0
   750
			if ( allocate_balldata(joystick) < 0 ) {
slouken@0
   751
				joystick->nballs = 0;
slouken@0
   752
			}
slouken@0
   753
		}
slouken@0
   754
	}
slouken@0
   755
	return(joystick->hwdata->is_hid);
slouken@0
   756
}
slouken@0
   757
slouken@1361
   758
#endif /* SDL_INPUT_LINUXEV */
slouken@0
   759
slouken@892
   760
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   761
static void ConfigLogicalJoystick(SDL_Joystick *joystick)
slouken@892
   762
{
slouken@892
   763
        struct joystick_logical_layout* layout;
slouken@892
   764
                                                                                
slouken@1551
   765
        layout = SDL_joylist[joystick->index].map->layout +
slouken@892
   766
                SDL_joylist[joystick->index].logicalno;
slouken@892
   767
                                                                                
slouken@892
   768
        joystick->nbuttons = layout->nbuttons;
slouken@892
   769
        joystick->nhats = layout->nhats;
slouken@892
   770
        joystick->naxes = layout->naxes;
slouken@892
   771
        joystick->nballs = layout->nballs;
slouken@892
   772
}
slouken@892
   773
#endif
slouken@892
   774
slouken@892
   775
slouken@0
   776
/* Function to open a joystick for use.
slouken@0
   777
   The joystick to open is specified by the index field of the joystick.
slouken@0
   778
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@0
   779
   It returns 0, or -1 if there is an error.
slouken@0
   780
 */
slouken@0
   781
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@0
   782
{
slouken@0
   783
	int fd;
slouken@892
   784
	SDL_logical_joydecl(int realindex);
slouken@892
   785
	SDL_logical_joydecl(SDL_Joystick *realjoy = NULL);
slouken@0
   786
slouken@0
   787
	/* Open the joystick and set the joystick file descriptor */
slouken@892
   788
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   789
	if (SDL_joylist[joystick->index].fname == NULL) {
slouken@892
   790
		SDL_joylist_head(realindex, joystick->index);
slouken@892
   791
		realjoy = SDL_JoystickOpen(realindex);
slouken@892
   792
slouken@892
   793
		if (realjoy == NULL)
slouken@892
   794
			return(-1);
slouken@892
   795
                                                                                
slouken@892
   796
		fd = realjoy->hwdata->fd;
slouken@892
   797
slouken@892
   798
	} else {
slouken@892
   799
		fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
slouken@892
   800
	}
slouken@892
   801
	SDL_joylist[joystick->index].joy = joystick;
slouken@892
   802
#else
slouken@892
   803
	fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
slouken@892
   804
#endif
slouken@892
   805
slouken@0
   806
	if ( fd < 0 ) {
slouken@0
   807
		SDL_SetError("Unable to open %s\n",
slouken@0
   808
		             SDL_joylist[joystick->index]);
slouken@0
   809
		return(-1);
slouken@0
   810
	}
slouken@0
   811
	joystick->hwdata = (struct joystick_hwdata *)
slouken@1336
   812
	                   SDL_malloc(sizeof(*joystick->hwdata));
slouken@0
   813
	if ( joystick->hwdata == NULL ) {
slouken@0
   814
		SDL_OutOfMemory();
slouken@0
   815
		close(fd);
slouken@0
   816
		return(-1);
slouken@0
   817
	}
slouken@1336
   818
	SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
slouken@0
   819
	joystick->hwdata->fd = fd;
slouken@0
   820
slouken@0
   821
	/* Set the joystick to non-blocking read mode */
slouken@0
   822
	fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@0
   823
slouken@0
   824
	/* Get the number of buttons and axes on the joystick */
slouken@892
   825
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   826
	if (realjoy)
slouken@892
   827
		ConfigLogicalJoystick(joystick);
slouken@892
   828
	else
slouken@892
   829
#endif
slouken@1361
   830
#if SDL_INPUT_LINUXEV
slouken@0
   831
	if ( ! EV_ConfigJoystick(joystick, fd) )
slouken@0
   832
#endif
slouken@554
   833
		JS_ConfigJoystick(joystick, fd);
slouken@554
   834
slouken@0
   835
	return(0);
slouken@0
   836
}
slouken@0
   837
slouken@892
   838
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   839
slouken@892
   840
static SDL_Joystick* FindLogicalJoystick(
slouken@1551
   841
   SDL_Joystick *joystick, struct joystick_logical_mapping* v)
slouken@892
   842
{
slouken@892
   843
        SDL_Joystick *logicaljoy;
slouken@892
   844
        register int i;
slouken@892
   845
slouken@892
   846
        i = joystick->index;
slouken@892
   847
        logicaljoy = NULL;
slouken@892
   848
slouken@892
   849
        /* get the fake joystick that will receive the event
slouken@892
   850
         */
slouken@892
   851
        for(;;) {
slouken@892
   852
slouken@892
   853
           if (SDL_joylist[i].logicalno == v->njoy) {
slouken@892
   854
              logicaljoy = SDL_joylist[i].joy;
slouken@892
   855
              break;
slouken@892
   856
           }
slouken@892
   857
slouken@892
   858
           if (SDL_joylist[i].next == 0)
slouken@892
   859
              break;
slouken@892
   860
slouken@892
   861
           i = SDL_joylist[i].next;
slouken@892
   862
slouken@892
   863
        }
slouken@892
   864
slouken@892
   865
        return logicaljoy;
slouken@892
   866
}
slouken@892
   867
slouken@892
   868
static int LogicalJoystickButton(
slouken@892
   869
   SDL_Joystick *joystick, Uint8 button, Uint8 state){
slouken@1551
   870
        struct joystick_logical_mapping* buttons;
slouken@892
   871
        SDL_Joystick *logicaljoy = NULL;
slouken@892
   872
slouken@892
   873
        /* if there's no map then this is just a regular joystick
slouken@892
   874
         */
slouken@892
   875
        if (SDL_joylist[joystick->index].map == NULL)
slouken@892
   876
           return 0;
slouken@892
   877
slouken@892
   878
        /* get the logical joystick that will receive the event
slouken@892
   879
         */
slouken@1551
   880
        buttons = SDL_joylist[joystick->index].map->buttonmap+button;
slouken@892
   881
        logicaljoy = FindLogicalJoystick(joystick, buttons);
slouken@892
   882
slouken@892
   883
        if (logicaljoy == NULL)
slouken@892
   884
           return 1;
slouken@892
   885
slouken@892
   886
        SDL_PrivateJoystickButton(logicaljoy, buttons->nthing, state);
slouken@892
   887
slouken@892
   888
        return 1;
slouken@892
   889
}
slouken@892
   890
slouken@892
   891
static int LogicalJoystickAxis(
slouken@892
   892
	SDL_Joystick *joystick, Uint8 axis, Sint16 value)
slouken@892
   893
{
slouken@1551
   894
        struct joystick_logical_mapping* axes;
slouken@892
   895
        SDL_Joystick *logicaljoy = NULL;
slouken@892
   896
slouken@892
   897
        /* if there's no map then this is just a regular joystick
slouken@892
   898
         */
slouken@892
   899
        if (SDL_joylist[joystick->index].map == NULL)
slouken@892
   900
           return 0;
slouken@892
   901
slouken@892
   902
        /* get the logical joystick that will receive the event
slouken@892
   903
         */
slouken@1551
   904
        axes = SDL_joylist[joystick->index].map->axismap+axis;
slouken@892
   905
        logicaljoy = FindLogicalJoystick(joystick, axes);
slouken@892
   906
slouken@892
   907
        if (logicaljoy == NULL)
slouken@892
   908
           return 1;
slouken@892
   909
slouken@892
   910
        SDL_PrivateJoystickAxis(logicaljoy, axes->nthing, value);
slouken@892
   911
slouken@892
   912
        return 1;
slouken@892
   913
}
slouken@892
   914
#endif /* USE_LOGICAL_JOYSTICKS */
slouken@892
   915
slouken@0
   916
static __inline__
slouken@0
   917
void HandleHat(SDL_Joystick *stick, Uint8 hat, int axis, int value)
slouken@0
   918
{
slouken@0
   919
	struct hwdata_hat *the_hat;
slouken@0
   920
	const Uint8 position_map[3][3] = {
slouken@0
   921
		{ SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP },
slouken@0
   922
		{ SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT },
slouken@0
   923
		{ SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN }
slouken@0
   924
	};
slouken@892
   925
	SDL_logical_joydecl(SDL_Joystick *logicaljoy = NULL);
slouken@1551
   926
	SDL_logical_joydecl(struct joystick_logical_mapping* hats = NULL);
slouken@0
   927
slouken@0
   928
	the_hat = &stick->hwdata->hats[hat];
slouken@0
   929
	if ( value < 0 ) {
slouken@0
   930
		value = 0;
slouken@0
   931
	} else
slouken@0
   932
	if ( value == 0 ) {
slouken@0
   933
		value = 1;
slouken@0
   934
	} else
slouken@0
   935
	if ( value > 0 ) {
slouken@0
   936
		value = 2;
slouken@0
   937
	}
slouken@0
   938
	if ( value != the_hat->axis[axis] ) {
slouken@0
   939
		the_hat->axis[axis] = value;
slouken@892
   940
slouken@892
   941
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   942
		/* if there's no map then this is just a regular joystick
slouken@892
   943
		*/
slouken@892
   944
		if (SDL_joylist[stick->index].map != NULL) {
slouken@892
   945
slouken@892
   946
			/* get the fake joystick that will receive the event
slouken@892
   947
			*/
slouken@1551
   948
			hats = SDL_joylist[stick->index].map->hatmap+hat;
slouken@892
   949
			logicaljoy = FindLogicalJoystick(stick, hats);
slouken@892
   950
		}
slouken@892
   951
slouken@892
   952
		if (logicaljoy) {
slouken@892
   953
			stick = logicaljoy;
slouken@892
   954
			hat = hats->nthing;
slouken@892
   955
		}
slouken@892
   956
#endif /* USE_LOGICAL_JOYSTICKS */
slouken@892
   957
slouken@0
   958
		SDL_PrivateJoystickHat(stick, hat,
slouken@0
   959
			position_map[the_hat->axis[1]][the_hat->axis[0]]);
slouken@0
   960
	}
slouken@0
   961
}
slouken@0
   962
slouken@0
   963
static __inline__
slouken@0
   964
void HandleBall(SDL_Joystick *stick, Uint8 ball, int axis, int value)
slouken@0
   965
{
slouken@0
   966
	stick->hwdata->balls[ball].axis[axis] += value;
slouken@0
   967
}
slouken@0
   968
slouken@0
   969
/* Function to update the state of a joystick - called as a device poll.
slouken@0
   970
 * This function shouldn't update the joystick structure directly,
slouken@0
   971
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@0
   972
 * and update joystick device state.
slouken@0
   973
 */
slouken@0
   974
static __inline__ void JS_HandleEvents(SDL_Joystick *joystick)
slouken@0
   975
{
slouken@0
   976
	struct js_event events[32];
slouken@0
   977
	int i, len;
slouken@0
   978
	Uint8 other_axis;
slouken@0
   979
slouken@892
   980
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   981
	if (SDL_joylist[joystick->index].fname == NULL) {
slouken@892
   982
		SDL_joylist_head(i, joystick->index);
slouken@1752
   983
		JS_HandleEvents(SDL_joylist[i].joy);
slouken@1752
   984
		return;
slouken@892
   985
	}
slouken@892
   986
#endif
slouken@892
   987
slouken@0
   988
	while ((len=read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
slouken@0
   989
		len /= sizeof(events[0]);
slouken@0
   990
		for ( i=0; i<len; ++i ) {
slouken@0
   991
			switch (events[i].type & ~JS_EVENT_INIT) {
slouken@0
   992
			    case JS_EVENT_AXIS:
slouken@0
   993
				if ( events[i].number < joystick->naxes ) {
slouken@892
   994
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
   995
					if (!LogicalJoystickAxis(joystick,
slouken@892
   996
				           events[i].number, events[i].value))
slouken@892
   997
#endif
slouken@0
   998
					SDL_PrivateJoystickAxis(joystick,
slouken@0
   999
				           events[i].number, events[i].value);
slouken@0
  1000
					break;
slouken@0
  1001
				}
slouken@0
  1002
				events[i].number -= joystick->naxes;
slouken@589
  1003
				other_axis = (events[i].number / 2);
slouken@589
  1004
				if ( other_axis < joystick->nhats ) {
slouken@589
  1005
					HandleHat(joystick, other_axis,
slouken@589
  1006
						events[i].number%2,
slouken@589
  1007
						events[i].value);
slouken@589
  1008
					break;
slouken@0
  1009
				}
slouken@0
  1010
				events[i].number -= joystick->nhats*2;
slouken@0
  1011
				other_axis = (events[i].number / 2);
slouken@0
  1012
				if ( other_axis < joystick->nballs ) {
slouken@0
  1013
					HandleBall(joystick, other_axis,
slouken@0
  1014
						events[i].number%2,
slouken@0
  1015
						events[i].value);
slouken@0
  1016
					break;
slouken@0
  1017
				}
slouken@0
  1018
				break;
slouken@0
  1019
			    case JS_EVENT_BUTTON:
slouken@892
  1020
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
  1021
				if (!LogicalJoystickButton(joystick,
slouken@892
  1022
				           events[i].number, events[i].value))
slouken@892
  1023
#endif
slouken@0
  1024
				SDL_PrivateJoystickButton(joystick,
slouken@0
  1025
				           events[i].number, events[i].value);
slouken@0
  1026
				break;
slouken@0
  1027
			    default:
slouken@0
  1028
				/* ?? */
slouken@0
  1029
				break;
slouken@0
  1030
			}
slouken@0
  1031
		}
slouken@0
  1032
	}
slouken@0
  1033
}
slouken@1361
  1034
#if SDL_INPUT_LINUXEV
slouken@0
  1035
static __inline__ int EV_AxisCorrect(SDL_Joystick *joystick, int which, int value)
slouken@0
  1036
{
slouken@0
  1037
	struct axis_correct *correct;
slouken@0
  1038
slouken@0
  1039
	correct = &joystick->hwdata->abs_correct[which];
slouken@0
  1040
	if ( correct->used ) {
slouken@0
  1041
		if ( value > correct->coef[0] ) {
slouken@0
  1042
			if ( value < correct->coef[1] ) {
slouken@0
  1043
				return 0;
slouken@0
  1044
			}
slouken@0
  1045
			value -= correct->coef[1];
slouken@0
  1046
		} else {
slouken@0
  1047
			value -= correct->coef[0];
slouken@0
  1048
		}
slouken@0
  1049
		value *= correct->coef[2];
slouken@0
  1050
		value >>= 14;
slouken@0
  1051
	}
slouken@554
  1052
slouken@0
  1053
	/* Clamp and return */
slouken@1275
  1054
	if ( value < -32768 ) return -32768;
slouken@554
  1055
	if ( value >  32767 ) return  32767;
slouken@554
  1056
slouken@0
  1057
	return value;
slouken@0
  1058
}
slouken@0
  1059
slouken@0
  1060
static __inline__ void EV_HandleEvents(SDL_Joystick *joystick)
slouken@0
  1061
{
slouken@0
  1062
	struct input_event events[32];
slouken@0
  1063
	int i, len;
slouken@0
  1064
	int code;
slouken@0
  1065
slouken@892
  1066
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
  1067
	if (SDL_joylist[joystick->index].fname == NULL) {
slouken@892
  1068
		SDL_joylist_head(i, joystick->index);
slouken@892
  1069
		return EV_HandleEvents(SDL_joylist[i].joy);
slouken@892
  1070
	}
slouken@892
  1071
#endif
slouken@892
  1072
slouken@0
  1073
	while ((len=read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
slouken@0
  1074
		len /= sizeof(events[0]);
slouken@0
  1075
		for ( i=0; i<len; ++i ) {
slouken@0
  1076
			code = events[i].code;
slouken@0
  1077
			switch (events[i].type) {
slouken@0
  1078
			    case EV_KEY:
slouken@0
  1079
				if ( code >= BTN_MISC ) {
slouken@0
  1080
					code -= BTN_MISC;
slouken@892
  1081
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
  1082
					if (!LogicalJoystickButton(joystick,
slouken@892
  1083
				           joystick->hwdata->key_map[code],
slouken@892
  1084
					   events[i].value))
slouken@892
  1085
#endif
slouken@0
  1086
					SDL_PrivateJoystickButton(joystick,
slouken@0
  1087
				           joystick->hwdata->key_map[code],
slouken@0
  1088
					   events[i].value);
slouken@0
  1089
				}
slouken@0
  1090
				break;
slouken@0
  1091
			    case EV_ABS:
slouken@0
  1092
				switch (code) {
slouken@0
  1093
				    case ABS_HAT0X:
slouken@0
  1094
				    case ABS_HAT0Y:
slouken@0
  1095
				    case ABS_HAT1X:
slouken@0
  1096
				    case ABS_HAT1Y:
slouken@0
  1097
				    case ABS_HAT2X:
slouken@0
  1098
				    case ABS_HAT2Y:
slouken@0
  1099
				    case ABS_HAT3X:
slouken@0
  1100
				    case ABS_HAT3Y:
slouken@0
  1101
					code -= ABS_HAT0X;
slouken@0
  1102
					HandleHat(joystick, code/2, code%2,
slouken@0
  1103
							events[i].value);
slouken@0
  1104
					break;
slouken@0
  1105
				    default:
slouken@0
  1106
					events[i].value = EV_AxisCorrect(joystick, code, events[i].value);
slouken@892
  1107
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
  1108
					if (!LogicalJoystickAxis(joystick,
slouken@892
  1109
				           joystick->hwdata->abs_map[code],
slouken@892
  1110
					   events[i].value))
slouken@892
  1111
#endif
slouken@0
  1112
					SDL_PrivateJoystickAxis(joystick,
slouken@0
  1113
				           joystick->hwdata->abs_map[code],
slouken@0
  1114
					   events[i].value);
slouken@0
  1115
					break;
slouken@0
  1116
				}
slouken@0
  1117
				break;
slouken@0
  1118
			    case EV_REL:
slouken@0
  1119
				switch (code) {
slouken@0
  1120
				    case REL_X:
slouken@0
  1121
				    case REL_Y:
slouken@0
  1122
					code -= REL_X;
slouken@0
  1123
					HandleBall(joystick, code/2, code%2,
slouken@0
  1124
							events[i].value);
slouken@0
  1125
					break;
slouken@0
  1126
				    default:
slouken@0
  1127
					break;
slouken@0
  1128
				}
slouken@0
  1129
				break;
slouken@0
  1130
			    default:
slouken@0
  1131
				break;
slouken@0
  1132
			}
slouken@0
  1133
		}
slouken@0
  1134
	}
slouken@0
  1135
}
slouken@1361
  1136
#endif /* SDL_INPUT_LINUXEV */
slouken@0
  1137
slouken@0
  1138
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@0
  1139
{
slouken@0
  1140
	int i;
slouken@0
  1141
	
slouken@1361
  1142
#if SDL_INPUT_LINUXEV
slouken@0
  1143
	if ( joystick->hwdata->is_hid )
slouken@0
  1144
		EV_HandleEvents(joystick);
slouken@0
  1145
	else
slouken@0
  1146
#endif
slouken@0
  1147
		JS_HandleEvents(joystick);
slouken@0
  1148
slouken@0
  1149
	/* Deliver ball motion updates */
slouken@0
  1150
	for ( i=0; i<joystick->nballs; ++i ) {
slouken@0
  1151
		int xrel, yrel;
slouken@0
  1152
slouken@0
  1153
		xrel = joystick->hwdata->balls[i].axis[0];
slouken@0
  1154
		yrel = joystick->hwdata->balls[i].axis[1];
slouken@0
  1155
		if ( xrel || yrel ) {
slouken@0
  1156
			joystick->hwdata->balls[i].axis[0] = 0;
slouken@0
  1157
			joystick->hwdata->balls[i].axis[1] = 0;
slouken@0
  1158
			SDL_PrivateJoystickBall(joystick, (Uint8)i, xrel, yrel);
slouken@0
  1159
		}
slouken@0
  1160
	}
slouken@0
  1161
}
slouken@0
  1162
slouken@0
  1163
/* Function to close a joystick after use */
slouken@0
  1164
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@0
  1165
{
slouken@892
  1166
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
  1167
	register int i;
slouken@892
  1168
	if (SDL_joylist[joystick->index].fname == NULL) {
slouken@892
  1169
		SDL_joylist_head(i, joystick->index);
slouken@892
  1170
		SDL_JoystickClose(SDL_joylist[i].joy);
slouken@892
  1171
	}
slouken@892
  1172
#endif
slouken@892
  1173
slouken@0
  1174
	if ( joystick->hwdata ) {
slouken@892
  1175
#ifndef NO_LOGICAL_JOYSTICKS
slouken@892
  1176
		if (SDL_joylist[joystick->index].fname != NULL)
slouken@892
  1177
#endif
slouken@0
  1178
		close(joystick->hwdata->fd);
slouken@0
  1179
		if ( joystick->hwdata->hats ) {
slouken@1336
  1180
			SDL_free(joystick->hwdata->hats);
slouken@0
  1181
		}
slouken@0
  1182
		if ( joystick->hwdata->balls ) {
slouken@1336
  1183
			SDL_free(joystick->hwdata->balls);
slouken@0
  1184
		}
slouken@1336
  1185
		SDL_free(joystick->hwdata);
slouken@0
  1186
		joystick->hwdata = NULL;
slouken@0
  1187
	}
slouken@0
  1188
}
slouken@0
  1189
slouken@0
  1190
/* Function to perform any system-specific joystick related cleanup */
slouken@0
  1191
void SDL_SYS_JoystickQuit(void)
slouken@0
  1192
{
slouken@0
  1193
	int i;
slouken@0
  1194
slouken@892
  1195
	for ( i=0; SDL_joylist[i].fname; ++i ) {
slouken@1336
  1196
		SDL_free(SDL_joylist[i].fname);
slouken@0
  1197
	}
slouken@892
  1198
	SDL_joylist[0].fname = NULL;
slouken@0
  1199
}
slouken@0
  1200
slouken@1635
  1201
#endif /* SDL_JOYSTICK_LINUX */