Ejemplo n.º 1
0
uint32_t JoystickOSX::process_joysticks(uint32_t p_last_id) {
	poll_joysticks();

	for (int i = 0; i < device_list.size(); i++) {
		joystick &joy = device_list[i];

		for (int j = 0; j < joy.axis_elements.size(); j++) {
			rec_element &elem = joy.axis_elements[j];
			int value = joy.get_hid_element_state(&elem);
			p_last_id = input->joy_axis(p_last_id, joy.id, j, axis_correct(value, elem.min, elem.max));
		}
		for (int j = 0; j < joy.button_elements.size(); j++) {
			int value = joy.get_hid_element_state(&joy.button_elements[j]);
			p_last_id = input->joy_button(p_last_id, joy.id, j, (value >= 1));
		}
		for (int j = 0; j < joy.hat_elements.size(); j++) {
			rec_element &elem = joy.hat_elements[j];
			int value = joy.get_hid_element_state(&elem);
			int hat_value = process_hat_value(elem.min, elem.max, value);
			p_last_id = input->joy_hat(p_last_id, joy.id, hat_value);
		}

		if (joy.ffservice) {
			uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
			if (timestamp > joy.ff_timestamp) {
				Vector2 strength = input->get_joy_vibration_strength(joy.id);
				float duration = input->get_joy_vibration_duration(joy.id);
				if (strength.x == 0 && strength.y == 0) {
					joystick_vibration_stop(joy.id, timestamp);
				} else {
					float gain = MAX(strength.x, strength.y);
					joystick_vibration_start(joy.id, gain, duration, timestamp);
				}
			}
		}
	}
	return p_last_id;
}
uint32_t joystick_linux::process_joysticks(uint32_t p_event_id) {

	if (joy_mutex->try_lock() != OK) {
		return p_event_id;
	}
	for (int i=0; i<JOYSTICKS_MAX; i++) {

		if (joysticks[i].fd == -1) continue;

		input_event events[32];
		Joystick* joy = &joysticks[i];

		int len;

		while ((len = read(joy->fd, events, (sizeof events))) > 0) {
			len /= sizeof(events[0]);
			for (int j = 0; j < len; j++) {

				input_event &ev = events[j];
				switch (ev.type) {
				case EV_KEY:
					p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value);
					break;

				case EV_ABS:

					switch (ev.code) {
					case ABS_HAT0X:
						if (ev.value != 0) {
							if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_LEFT;
							else              joy->dpad |= InputDefault::HAT_MASK_RIGHT;
						}
						else joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT);

						p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
						break;

					case ABS_HAT0Y:
						if (ev.value != 0) {
							if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_UP;
							else              joy->dpad |= InputDefault::HAT_MASK_DOWN;
						}
						else joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN);

						p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
						break;

					default:
						if (joy->abs_map[ev.code] != -1 && joy->abs_info[ev.code]) {
							InputDefault::JoyAxis value = axis_correct(joy->abs_info[ev.code], ev.value);
							joy->curr_axis[joy->abs_map[ev.code]] = value;
						}
						break;
					}
					break;
				}
			}
		}
		for (int j = 0; j < MAX_ABS; j++) {
			int index = joy->abs_map[j];
			if (index != -1) {
				p_event_id = input->joy_axis(p_event_id, i, index, joy->curr_axis[index]);
			}
		}
	}
	joy_mutex->unlock();
	return p_event_id;
}
Ejemplo n.º 3
0
uint32_t joystick_linux::process_joysticks(uint32_t p_event_id) {

	if (joy_mutex->try_lock() != OK) {
		return p_event_id;
	}
	for (int i=0; i<JOYSTICKS_MAX; i++) {

		if (joysticks[i].fd == -1) continue;

		input_event ev;
		Joystick* joy = &joysticks[i];
		libevdev* dev = joy->dev;
		int rc = 1;

		rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);

		if (rc < 0 && rc != -EAGAIN) {
			continue;
		}

		while (rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS) {

			switch (ev.type) {
			case EV_KEY:
				p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value);
				break;

			case EV_ABS:

				switch (ev.code) {
				case ABS_HAT0X:
					if (ev.value != 0) {
						if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_LEFT;
						else              joy->dpad |= InputDefault::HAT_MASK_RIGHT;

					}
					else joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT);

					p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
					break;

				case ABS_HAT0Y:
					if (ev.value != 0) {
						if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_UP;
						else              joy->dpad |= InputDefault::HAT_MASK_DOWN;
					}
					else joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN);

					p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
					break;

				default:
					if (joy->abs_map[ev.code] != -1) {
						InputDefault::JoyAxis value = axis_correct(libevdev_get_abs_info(dev, ev.code), ev.value);
						joy->curr_axis[joy->abs_map[ev.code]] = value;
					}
					break;
				}
				break;
			}
			rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
		}
		for (int j = 0; j < MAX_ABS; j++) {
			int index = joy->abs_map[j];
			if (index != -1) {
				p_event_id = input->joy_axis(p_event_id, i, index, joy->curr_axis[index]);
			}
		}
	}
	joy_mutex->unlock();
	return p_event_id;
}
Ejemplo n.º 4
0
void JoypadLinux::process_joypads() {

	if (joy_mutex->try_lock() != OK) {
		return;
	}
	for (int i = 0; i < JOYPADS_MAX; i++) {

		if (joypads[i].fd == -1) continue;

		input_event events[32];
		Joypad *joy = &joypads[i];

		int len;

		while ((len = read(joy->fd, events, (sizeof events))) > 0) {
			len /= sizeof(events[0]);
			for (int j = 0; j < len; j++) {

				input_event &ev = events[j];

				// ev may be tainted and out of MAX_KEY range, which will cause
				// joy->key_map[ev.code] to crash
				if (ev.code < 0 || ev.code >= MAX_KEY)
					return;

				switch (ev.type) {
					case EV_KEY:
						input->joy_button(i, joy->key_map[ev.code], ev.value);
						break;

					case EV_ABS:

						switch (ev.code) {
							case ABS_HAT0X:
								if (ev.value != 0) {
									if (ev.value < 0)
										joy->dpad |= InputDefault::HAT_MASK_LEFT;
									else
										joy->dpad |= InputDefault::HAT_MASK_RIGHT;
								} else
									joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT);

								input->joy_hat(i, joy->dpad);
								break;

							case ABS_HAT0Y:
								if (ev.value != 0) {
									if (ev.value < 0)
										joy->dpad |= InputDefault::HAT_MASK_UP;
									else
										joy->dpad |= InputDefault::HAT_MASK_DOWN;
								} else
									joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN);

								input->joy_hat(i, joy->dpad);
								break;

							default:
								if (joy->abs_map[ev.code] != -1 && joy->abs_info[ev.code]) {
									InputDefault::JoyAxis value = axis_correct(joy->abs_info[ev.code], ev.value);
									joy->curr_axis[joy->abs_map[ev.code]] = value;
								}
								break;
						}
						break;
				}
			}
		}
		for (int j = 0; j < MAX_ABS; j++) {
			int index = joy->abs_map[j];
			if (index != -1) {
				input->joy_axis(i, index, joy->curr_axis[index]);
			}
		}
		if (len == 0 || (len < 0 && errno != EAGAIN)) {
			close_joypad(i);
		};

		if (joy->force_feedback) {
			uint64_t timestamp = input->get_joy_vibration_timestamp(i);
			if (timestamp > joy->ff_effect_timestamp) {
				Vector2 strength = input->get_joy_vibration_strength(i);
				float duration = input->get_joy_vibration_duration(i);
				if (strength.x == 0 && strength.y == 0) {
					joypad_vibration_stop(i, timestamp);
				} else {
					joypad_vibration_start(i, strength.x, strength.y, duration, timestamp);
				}
			}
		}
	}
	joy_mutex->unlock();
}
Ejemplo n.º 5
0
unsigned int joystick_windows::process_joysticks(unsigned int p_last_id) {

	HRESULT hr;

	for (int i = 0; i < XUSER_MAX_COUNT; i++) {

		xinput_gamepad &joy = x_joysticks[i];
		if (!joy.attached) {
			continue;
		}
		ZeroMemory(&joy.state, sizeof(XINPUT_STATE));

		xinput_get_state(i, &joy.state);
		if (joy.state.dwPacketNumber != joy.last_packet) {

			int button_mask = XINPUT_GAMEPAD_DPAD_UP;
			for (int i = 0; i <= 16; i++) {

				p_last_id = input->joy_button(p_last_id, joy.id, i, joy.state.Gamepad.wButtons & button_mask);
				button_mask = button_mask * 2;
			}

			p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_0,  axis_correct(joy.state.Gamepad.sThumbLX, true));
			p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_1,  axis_correct(joy.state.Gamepad.sThumbLY, true, false, true));
			p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_2,  axis_correct(joy.state.Gamepad.sThumbRX, true));
			p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_3,  axis_correct(joy.state.Gamepad.sThumbRY, true, false, true));
			p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_4,  axis_correct(joy.state.Gamepad.bLeftTrigger, true, true));
			p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_5,  axis_correct(joy.state.Gamepad.bRightTrigger, true, true));
			joy.last_packet = joy.state.dwPacketNumber;
		}
		uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
		if (timestamp > joy.ff_timestamp) {
			Vector2 strength = input->get_joy_vibration_strength(joy.id);
			float duration = input->get_joy_vibration_duration(joy.id);
			if (strength.x == 0 && strength.y == 0) {
				joystick_vibration_stop_xinput(i, timestamp);
			} else {
				joystick_vibration_start_xinput(i, strength.x, strength.y, duration, timestamp);
			}
		} else if (joy.vibrating && joy.ff_end_timestamp != 0) {
			uint64_t current_time = OS::get_singleton()->get_ticks_usec();
			if (current_time >= joy.ff_end_timestamp)
				joystick_vibration_stop_xinput(i, current_time);
		}
	}

	for (int i = 0; i < JOYSTICKS_MAX; i++) {

		dinput_gamepad* joy = &d_joysticks[i];

		if (!joy->attached)
			continue;

		DIJOYSTATE2 js;
		hr = joy->di_joy->Poll();
		if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
			IDirectInputDevice8_Acquire(joy->di_joy);
			joy->di_joy->Poll();
		}
		if (FAILED(hr = joy->di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js))) {

			//printf("failed to read joy #%d\n", i);
			continue;
		}

		p_last_id = post_hat(p_last_id, joy->id, js.rgdwPOV[0]);

		for (int j = 0; j < 128; j++) {

			if (js.rgbButtons[j] & 0x80) {

				if (!joy->last_buttons[j]) {

					p_last_id = input->joy_button(p_last_id, joy->id, j, true);
					joy->last_buttons[j] = true;
				}
			}
			else {

				if (joy->last_buttons[j]) {

					p_last_id = input->joy_button(p_last_id, joy->id, j, false);
					joy->last_buttons[j] = false;
				}
			}
		}

		// on mingw, these constants are not constants
		int count = 6;
		int axes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ };
		int values[] = { js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz };

		for (int j = 0; j < joy->joy_axis.size(); j++) {

			for (int k=0; k<count; k++) {
				if (joy->joy_axis[j] == axes[k]) {
					p_last_id = input->joy_axis(p_last_id, joy->id, j, axis_correct(values[k]));
					break;
				};
			};
		};
	}
	return p_last_id;
}