Exemplo n.º 1
0
adv_error joystickb_event_init(int joystickb_id)
{
	unsigned i;
	adv_bool eacces = 0;
	struct event_location map[EVENT_JOYSTICK_DEVICE_MAX];
	unsigned mac;

	log_std(("josytickb:event: joystickb_event_init(id:%d)\n", joystickb_id));

	log_std(("joystickb:event: opening joystick from 0 to %d\n", EVENT_JOYSTICK_DEVICE_MAX));

	event_state.counter = 0;

	mac = event_locate(map, EVENT_JOYSTICK_DEVICE_MAX, &eacces);

	event_state.mac = 0;
	for(i=0;i<mac;++i) {
		int f;

		if (event_state.mac >= EVENT_JOYSTICK_MAX)
			continue;

		f = event_open(map[i].file, event_state.map[event_state.mac].evtype_bitmask, sizeof(event_state.map[event_state.mac].evtype_bitmask));
		if (f == -1) {
			if (errno == EACCES) {
				eacces = 1;
			}
			continue;
		}

		if (!event_is_joystick(f, event_state.map[event_state.mac].evtype_bitmask)) {
			log_std(("joystickb:event: not a joystick on device %s\n", map[i].file));
			event_close(f);
			continue;
		}

		if (joystickb_setup(&event_state.map[event_state.mac], f) != 0) {
			event_close(f);
			continue;
		}

		++event_state.mac;
	}

	if (!event_state.mac) {
		if (eacces)
			error_set("No joystick found. Check the /dev/input/event* permissions.\n");
		else
			error_set("No joystick found.\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 2
0
adv_error joystickb_raw_init(int joystickb_id)
{
	unsigned i;
	adv_bool eacces = 0;

	log_std(("josytickb:raw: joystickb_raw_init(id:%d)\n", joystickb_id));

	log_std(("joystickb:raw: opening joystick from 0 to %d\n", RAW_JOYSTICK_DEVICE_MAX));

	raw_state.mac = 0;
	for (i = 0; i < RAW_JOYSTICK_DEVICE_MAX; ++i) {
		int f;
		int version;
		char file[128];

		if (raw_state.mac >= RAW_JOYSTICK_MAX)
			continue;

		snprintf(file, sizeof(file), "/dev/js%d", i);
		f = open(file, O_RDONLY | O_NONBLOCK);
		if (f == -1) {
			if (errno != ENODEV) {
				log_std(("joystickb:raw: error opening device %s, errno %d (%s)\n", file, errno, strerror(errno)));
			}
			if (errno == EACCES) {
				eacces = 1;
			}

			snprintf(file, sizeof(file), "/dev/input/js%d", i);
			f = open(file, O_RDONLY | O_NONBLOCK);
		}
		if (f == -1) {
			if (errno != ENODEV) {
				log_std(("joystickb:raw: error opening device %s, errno %d (%s)\n", file, errno, strerror(errno)));
			}
			continue;
		}

		log_std(("joystickb:raw: open device %s\n", file));

		if (ioctl(f, JSIOCGVERSION, &version) < 0) {
			log_std(("ERROR:joystickb:raw: ioctl(JSIOCVERSION) failed\n"));
			close(f);
			continue;
		}

		joystickb_log(f);

		if (joystickb_setup(&raw_state.map[raw_state.mac], f) != 0) {
			close(f);
			continue;
		}

		++raw_state.mac;
	}

	if (!raw_state.mac) {
		if (eacces)
			error_set("No joystick found. Check the /dev/js* and /dev/input/js* permissions.\n");
		else
			error_set("No joystick found.\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 3
0
adv_error joystickb_lgrawinput_init(int joystickb_id)
{
#if defined(USE_SDL) && SDL_MAJOR_VERSION != 1
	error_set("Incompatible with SDL2.\n");
	return -1;
#else
	unsigned i;
	HMODULE h;
	UINT n;
	UINT size;
	RAWINPUTDEVICELIST* l;

	log_std(("joystickb:lgrawinput: joystickb_lgrawinput_init(id:%d)\n", joystickb_id));

	h = GetModuleHandle("user32.dll");
	if (!h) {
		error_set("Error loading the user32 library.\n");
		return -1;
	}

	RegisterRawInputDevices_ptr = (RegisterRawInputDevices_type)GetProcAddress(h, "RegisterRawInputDevices");
	GetRawInputDeviceList_ptr = (GetRawInputDeviceList_type)GetProcAddress(h, "GetRawInputDeviceList");
	GetRawInputDeviceInfoA_ptr = (GetRawInputDeviceInfoA_type)GetProcAddress(h, "GetRawInputDeviceInfoA");
	GetRawInputData_ptr = (GetRawInputData_type)GetProcAddress(h, "GetRawInputData");

	if (!RegisterRawInputDevices_ptr || !GetRawInputDeviceList_ptr || !GetRawInputDeviceInfoA_ptr || !GetRawInputData_ptr) {
		error_set("Raw input devices not supported on your system.\n");
		return -1;
	}

	if (GetRawInputDeviceList_ptr(NULL, &n, sizeof(RAWINPUTDEVICELIST)) != 0) {
		error_set("Error getting the number of raw devices.\n");
		return -1;
	}

	if (n == 0) {
		error_set("No input device found.\n");
		return -1;
	}

	size = n * sizeof(RAWINPUTDEVICELIST);
	l = malloc(size);

	n = GetRawInputDeviceList_ptr(l, &size, sizeof(RAWINPUTDEVICELIST));
	if (n == -1) {
		free(l);
		error_set("Error getting the list of raw devices.\n");
		return -1;
	}

	log_std(("joystickb:lgrawinput: GetRawInputDeviceList() -> %d\n", (unsigned)n));

	raw_state.mac = 0;
	for (i = 0; i < n; ++i) {
		if (raw_state.mac < RAW_MOUSE_MAX) {
			UINT size;
			unsigned vid, pid, rev;
			struct raw_context* context = &raw_state.map[raw_state.mac].context;

			size = sizeof(RID_DEVICE_INFO);
			context->info.cbSize = sizeof(RID_DEVICE_INFO);
			if (GetRawInputDeviceInfoA_ptr(l[i].hDevice, RIDI_DEVICEINFO, &context->info, &size) == -1) {
				continue;
			}

			size = sizeof(context->name);
			if (GetRawInputDeviceInfoA_ptr(l[i].hDevice, RIDI_DEVICENAME, context->name, &size) < 0) {
				continue;
			}

			/* Get the VID/PID */
			if (GetRawInputDeviceHIDInfo(context->name, &vid, &pid, &rev) < 0) {
				/* on error use fake value, for not HID devices it's ok to fail */
				vid = 0;
				pid = 0;
				rev = 0;
			}

			log_std(("joystickb:lgrawinput: GetRawInputDeviceInfo(%d) -> type:%d,vid:%04x,pid:%04x,rev:%04x,%s\n", i, (unsigned)context->info.dwType, vid, pid, rev, context->name));

			/* only mouse type lightgun are recognized */
			if (context->info.dwType != RIM_TYPEMOUSE) {
				continue;
			}

			if (
				/* SMOG Lightgun (http://lightgun.splinder.com/) */
				(vid == 0x0b9a && pid == 0x016a)
				/* Acts Labs Lightgun (http://www.act-labs.com/) */
				|| (vid == 0x061c && pid == 0xa800)
				|| (vid == 0x061c && pid == 0xa700)
			) {
				/* recognized */
			} else {
				continue;
			}

			raw_state.map[raw_state.mac].context.h = l[i].hDevice;

			log_std(("joystickb:lgrawinput: lightgun id:%d,vid:%04x,pid:%04x,rev:%04x,buttons:%d,samplerate:%d\n", (unsigned)context->info.mouse.dwId, vid, pid, rev, (unsigned)context->info.mouse.dwNumberOfButtons, (unsigned)context->info.mouse.dwSampleRate));

			joystickb_setup(&raw_state.map[raw_state.mac], context->info.mouse.dwNumberOfButtons);

			++raw_state.mac;
		}
	}

	free(l);

	if (raw_state.mac == 0) {
		error_set("No lightgun found.\n");
		return -1;
	}

	qsort(raw_state.map, raw_state.mac, sizeof(raw_state.map[0]), joystickb_compare);

	return 0;
#endif
}