Ejemplo n.º 1
0
/*
 * Get the implementation dependent name of a joystick
 */
const char *SDL_JoystickName(int device_index)
{
	if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) {
		SDL_SetError("There are %d joysticks available",
		             SDL_numjoysticks);
		return(NULL);
	}
	return(SDL_SYS_JoystickName(device_index));
}
Ejemplo n.º 2
0
static int
CountLogicalJoysticks(int max)
{
    register int i, j, k, ret, prev;
    const char *name;
    int nbuttons, fd;
    unsigned char n;

    ret = 0;

    for (i = 0; i < max; i++) {
        name = SDL_SYS_JoystickName(i);

        fd = open(SDL_joylist[i].fname, O_RDONLY, 0);
        if (fd >= 0) {
            if (ioctl(fd, JSIOCGBUTTONS, &n) < 0) {
                nbuttons = -1;
            } else {
                nbuttons = n;
            }
            close(fd);
        } else {
            nbuttons = -1;
        }

        if (name) {
            for (j = 0; j < SDL_arraysize(joystick_logicalmap); j++) {
                if (!SDL_strcmp(name, joystick_logicalmap[j].name)
                    && (nbuttons == -1
                        || nbuttons == joystick_logicalmap[j].nbuttons)) {
                    prev = i;
                    SDL_joylist[prev].map = &(joystick_logicalmap[j]);

                    for (k = 1; k < joystick_logicalmap[j].njoys; k++) {
                        SDL_joylist[prev].next = max + ret;
                        SDL_joylist[max + ret].prev = prev;

                        prev = max + ret;
                        SDL_joylist[prev].logicalno = k;
                        SDL_joylist[prev].map = &(joystick_logicalmap[j]);
                        ret++;
                    }

                    break;
                }
            }
        }
    }

    return ret;
}
Ejemplo n.º 3
0
static SDL_bool JS_ConfigJoystick(SDL_Joystick *joystick, int fd)
{
	SDL_bool handled;
	unsigned char n;
	int tmp_naxes, tmp_nhats, tmp_nballs;
	const char *name;
	char *env, env_name[128];
	int i;

	handled = SDL_FALSE;

	/* Default joystick device settings */
	if ( ioctl(fd, JSIOCGAXES, &n) < 0 ) {
		joystick->naxes = 2;
	} else {
		joystick->naxes = n;
	}
	if ( ioctl(fd, JSIOCGBUTTONS, &n) < 0 ) {
		joystick->nbuttons = 2;
	} else {
		joystick->nbuttons = n;
	}

	name = SDL_SYS_JoystickName(joystick->index);

	/* Generic analog joystick support */
	if ( SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat") ) {
		if ( SDL_sscanf(name,"Analog %d-axis %*d-button %d-hat",
			&tmp_naxes, &tmp_nhats) == 2 ) {

			joystick->naxes = tmp_naxes;
			joystick->nhats = tmp_nhats;

			handled = SDL_TRUE;
		}
	}

	/* Special joystick support */
	for ( i=0; i < SDL_arraysize(special_joysticks); ++i ) {
		if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) {

			joystick->naxes = special_joysticks[i].naxes;
			joystick->nhats = special_joysticks[i].nhats;
			joystick->nballs = special_joysticks[i].nballs;

			handled = SDL_TRUE;
			break;
		}
	}

	/* User environment joystick support */
	if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) {
		*env_name = '\0';
		if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 )
			env += SDL_strlen(env_name)+2;
		else if ( SDL_sscanf(env, "%s", env_name) == 1 )
			env += SDL_strlen(env_name);

		if ( SDL_strcmp(name, env_name) == 0 ) {

			if ( SDL_sscanf(env, "%d %d %d", &tmp_naxes, &tmp_nhats,
				&tmp_nballs) == 3 ) {

				joystick->naxes = tmp_naxes;
				joystick->nhats = tmp_nhats;
				joystick->nballs = tmp_nballs;

				handled = SDL_TRUE;
			}
		}
	}

	/* Remap hats and balls */
	if (handled) {
		if ( joystick->nhats > 0 ) {
			if ( allocate_hatdata(joystick) < 0 ) {
				joystick->nhats = 0;
			}
		}
		if ( joystick->nballs > 0 ) {
			if ( allocate_balldata(joystick) < 0 ) {
				joystick->nballs = 0;
			}
		}
	}

	return(handled);
}
Ejemplo n.º 4
0
/* Function to open a joystick for use.
   The joystick to open is specified by the index field of the joystick.
   This should fill the nbuttons and naxes fields of the joystick structure.
   It returns 0, or -1 if there is an error.
 */
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
{
    int     index;
    UInt32  count, gotCount, count2;
    long    numAxis, numButtons, numHats, numBalls;

    count = kMaxReferences;
    count2 = 0;
    numAxis = numButtons = numHats = numBalls = 0;

    index = joystick->index;

    /* allocate memory for system specific hardware data */
    joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
    if (joystick->hwdata == NULL)
    {
		SDL_OutOfMemory();
		return(-1);
    }
    SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
    SDL_strlcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index), SDL_arraysize(joystick->hwdata->name));
    joystick->name = joystick->hwdata->name;

    ISpElementList_ExtractByKind(
        SYS_Elements[index],
        kISpElementKind_Axis,
        count,
        &gotCount,
        joystick->hwdata->refs);

    numAxis = gotCount;
    count -= gotCount;
    count2 += gotCount;

    ISpElementList_ExtractByKind(
        SYS_Elements[index],
        kISpElementKind_DPad,
        count,
        &gotCount,
        &(joystick->hwdata->refs[count2]));

    numHats = gotCount;
    count -= gotCount;
    count2 += gotCount;

    ISpElementList_ExtractByKind(
        SYS_Elements[index],
        kISpElementKind_Button,
        count,
        &gotCount,
        &(joystick->hwdata->refs[count2]));

    numButtons = gotCount;
    count -= gotCount;
    count2 += gotCount;

    joystick->naxes = numAxis;
    joystick->nhats = numHats;
    joystick->nballs = numBalls;
    joystick->nbuttons = numButtons;

    ISpDevices_Activate(
        1,
        &SYS_Joysticks[index]);

    return 0;
}