Example #1
0
/*
 * Add or update an entry into the Mappings Database
 */
int
SDL_GameControllerAddMapping(const char *mappingString)
{
    char *pchGUID;
    char *pchName;
    char *pchMapping;
    SDL_JoystickGUID jGUID;
    ControllerMapping_t *pControllerMapping;
    SDL_bool is_xinput_mapping = SDL_FALSE;

    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
    if (!pchGUID) {
        return SDL_SetError("Couldn't parse GUID from %s", mappingString);
    }
    if (!SDL_strcasecmp(pchGUID, "xinput")) {
        is_xinput_mapping = SDL_TRUE;
    }
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
    SDL_free(pchGUID);

    pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
    if (!pchName) {
        return SDL_SetError("Couldn't parse name from %s", mappingString);
    }

    pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
    if (!pchMapping) {
        SDL_free(pchName);
        return SDL_SetError("Couldn't parse %s", mappingString);
    }

    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);

    if (pControllerMapping) {
        /* Update existing mapping */
        SDL_free(pControllerMapping->name);
        pControllerMapping->name = pchName;
        SDL_free(pControllerMapping->mapping);
        pControllerMapping->mapping = pchMapping;
        /* refresh open controllers */
        SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
        return 0;
    } else {
        pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
        if (!pControllerMapping) {
            SDL_free(pchName);
            SDL_free(pchMapping);
            return SDL_OutOfMemory();
        }
        if (is_xinput_mapping) {
            s_pXInputMapping = pControllerMapping;
        }
        pControllerMapping->guid = jGUID;
        pControllerMapping->name = pchName;
        pControllerMapping->mapping = pchMapping;
        pControllerMapping->next = s_pSupportedControllers;
        s_pSupportedControllers = pControllerMapping;
        return 1;
    }
}
Example #2
0
static mrb_value
mrb_sdl2_joystick_get_guid_from_string(mrb_state *mrb, mrb_value self)
{
  mrb_value name;
  SDL_JoystickGUID result;
  mrb_get_args(mrb, "S", &name);
  result = SDL_JoystickGetGUIDFromString(RSTRING_PTR(name));

  return mrb_fixnum_value(*result.data);
}
Example #3
0
bool Joystick::Mapping::populateFromDict(QVariantMap deviceinfo)
{
    bool ret = InputDeviceMapping::populateFromDict(deviceinfo);
    if (!ret)
        return ret;

    QByteArray guid = deviceinfo["guid"].toString().toLatin1();
    joystick_guid = SDL_JoystickGetGUIDFromString(guid.constData());
    return ret;
}
Example #4
0
bool Joystick::Mapping::populateFromSettings(QSettings &settings)
{
    bool ret = InputDeviceMapping::populateFromSettings(settings);
    if (!ret)
        return ret;

    QVariant guid_ = settings.value("sdl_joystick_guid");
    if (guid_.isValid()) {
        QByteArray guid = guid_.toString().toLatin1();
        joystick_guid = SDL_JoystickGetGUIDFromString(guid.constData());
    }
    return ret;
}
Example #5
0
/*
 * Add or update an entry into the Mappings Database with a priority
 */
static int
SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_ControllerMappingPriority priority)
{
    char *pchGUID;
    SDL_JoystickGUID jGUID;
    SDL_bool is_xinput_mapping = SDL_FALSE;
    SDL_bool is_emscripten_mapping = SDL_FALSE;
    SDL_bool existing = SDL_FALSE;
    ControllerMapping_t *pControllerMapping;

    if (!mappingString) {
        return SDL_InvalidParamError("mappingString");
    }

    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
    if (!pchGUID) {
        return SDL_SetError("Couldn't parse GUID from %s", mappingString);
    }
    if (!SDL_strcasecmp(pchGUID, "xinput")) {
        is_xinput_mapping = SDL_TRUE;
    }
    if (!SDL_strcasecmp(pchGUID, "emscripten")) {
        is_emscripten_mapping = SDL_TRUE;
    }
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
    SDL_free(pchGUID);

    pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing, priority);
    if (!pControllerMapping) {
        return -1;
    }

    if (existing) {
        return 0;
    } else {
        if (is_xinput_mapping) {
            s_pXInputMapping = pControllerMapping;
        }
        if (is_emscripten_mapping) {
            s_pEmscriptenMapping = pControllerMapping;
        }
        return 1;
    }
}
static int ReadMappingsString(const char *s)
{
#define READ_TOKEN(buf, p, end)\
	if (end == NULL)\
	{\
		strcpy(buf, p);\
		p = NULL;\
	}\
	else\
	{\
		strncpy(buf, p, end - p);\
		buf[end - p] = '\0';\
		p = end + 1;\
	}

	// Read compiled string button names into memory
	// Search for a matching GUID + joystickName in the db
	int read = 0;
	for (const char *cur = s; cur;)
	{
		const char *nl = strchr(cur, '\n');
		char line[2048];
		READ_TOKEN(line, cur, nl);

		char buf[256];

		// Check for the platform string
		sprintf(buf, "platform:%s", SDL_GetPlatform());
		if (strstr(line, buf) == NULL) continue;

#define STR_NOT_EQ(expected, actualP, actualEnd)\
	strlen(expected) != (actualEnd) - (actualP) || \
	strncmp(expected, actualP, (actualEnd) - (actualP)) != 0
		const char *curL = line;
		// Ignore hash comments
		if (*curL == '#') continue;

		const char *nextComma;
		JoystickButtonNames j = jDefault;

		// Read GUID
		nextComma = strchr(curL, ',');
		if (nextComma == NULL || cur == nextComma) continue;
		READ_TOKEN(buf, curL, nextComma);
		j.guid = SDL_JoystickGetGUIDFromString(buf);

		// Read joystick name
		nextComma = strchr(curL, ',');
		if (nextComma == NULL || curL == nextComma) continue;
		READ_TOKEN(j.joystickName, curL, nextComma);

		// Check if GUID+joystick name already exists
		bool exists = false;
		for (int i = 0; i < nJBN; i++)
		{
			const JoystickButtonNames *jp = jbn + i;
			if (memcmp(&jp->guid, &j.guid, sizeof j.guid) == 0 &&
				strcmp(jp->joystickName, j.joystickName) == 0)
			{
				exists = true;
				break;
			}
		}
		if (exists) continue;

		// Read name and colors
		for (;; curL = nextComma + 1)
		{
			nextComma = strchr(curL, ',');
			if (nextComma == NULL) break;

			const char *nextColon;

			nextColon = strchr(curL, ':');
			if (nextColon == NULL || curL == nextColon) continue;
			READ_TOKEN(buf, curL, nextColon);
			const SDL_GameControllerButton button =
				SDL_GameControllerGetButtonFromString(buf);
			const SDL_GameControllerAxis axis =
				SDL_GameControllerGetAxisFromString(buf);
			char *name;
			SDL_Color *color;
			if (button != SDL_CONTROLLER_BUTTON_INVALID)
			{
				name = j.buttonNames[(int)button];
				color = &j.buttonColors[(int)button];
			}
			else if (axis != SDL_CONTROLLER_AXIS_INVALID)
			{
				name = j.axisNames[(int)axis];
				color = &j.axisColors[(int)axis];
			}
			else
			{
				continue;
			}
			// Read the real button/axis name
			nextColon = strchr(curL, ':');
			if (nextColon == NULL) continue;
			READ_TOKEN(name, curL, nextColon);
			// R
			nextColon = strchr(curL, ':');
			if (nextColon == NULL) continue;
			READ_TOKEN(buf, curL, nextColon);
			color->r = (Uint8)atoi(buf);
			// G
			nextColon = strchr(curL, ':');
			if (nextColon == NULL) continue;
			READ_TOKEN(buf, curL, nextColon);
			color->g = (Uint8)atoi(buf);
			// B
			READ_TOKEN(buf, curL, nextComma);
			color->b = (Uint8)atoi(buf);

			color->a = 255;
		}
		nJBN++;
		read++;
		jbn = SDL_realloc(jbn, nJBN * sizeof *jbn);
		memcpy(jbn + nJBN - 1, &j, sizeof j);
	}
	return read;
}