src/joystick/linux/SDL_sysjoystick.c
author Ozkan Sezer <sezeroz@gmail.com>
Tue, 07 Aug 2018 10:10:02 +0300
branchSDL-1.2
changeset 12076 3e05d58dc84f
parent 6325 9cff1c7c0fff
permissions -rw-r--r--
linux/SDL_sysjoystick.c: Fix bug #3193:

Protect against any axis that isn't reported by EVIOCGBIT but still
sends EV_ABS events; patch from <maxxus@gmail.com>. Original issue
and description:

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