public void UpdateJoystickState() { JoystickState joystickState = new JoystickState(); foreach (VjoyController controller in this.controllers) { joystickState.Add(controller.GetJoystickState()); } joystickState.Clamp(); uint buttons1 = 0; for (int i = 0; i < 32; i++) { if (joystickState.buttons[i]) { buttons1 |= (uint)1 << i; } } uint buttons2 = 0; for (int i = 0; i < 32; i++) { if (joystickState.buttons[32 + i]) { buttons2 |= (uint)1 << i; } } uint buttons3 = 0; for (int i = 0; i < 32; i++) { if (joystickState.buttons[64 + i]) { buttons3 |= (uint)1 << i; } } uint buttons4 = 0; for (int i = 0; i < 32; i++) { if (joystickState.buttons[96 + i]) { buttons4 |= (uint)1 << i; } } vJoy.JoystickState iReport = new vJoy.JoystickState { bDevice = (byte)this.vjoyDeviceId, bHats = 0xFFFFFFFF, bHatsEx1 = 0xFFFFFFFF, bHatsEx2 = 0xFFFFFFFF, bHatsEx3 = 0xFFFFFFFF, AxisX = (int)((joystickState.axis_x + 1) / 2 * 0x8000), AxisY = (int)((joystickState.axis_y + 1) / 2 * 0x8000), AxisZ = (int)((joystickState.axis_z + 1) / 2 * 0x8000), AxisXRot = (int)((joystickState.axis_x_rot + 1) / 2 * 0x8000), AxisYRot = (int)((joystickState.axis_y_rot + 1) / 2 * 0x8000), AxisZRot = (int)((joystickState.axis_z_rot + 1) / 2 * 0x8000), Slider = (int)((joystickState.slider1 + 1) / 2 * 0x8000), Dial = (int)((joystickState.slider2 + 1) / 2 * 0x8000), Buttons = buttons1, ButtonsEx1 = buttons2, ButtonsEx2 = buttons3, ButtonsEx3 = buttons4 }; lock (this.vjoy) { this.vjoy.UpdateVJD(this.vjoyDeviceId, ref iReport); } }
public void ProcessPacket(byte[] data) { if (data.Length < 3) { return; } if (data.Length == 3) { for (int controllerId = 0; controllerId < 16; controllerId++) { this.controllers[controllerId].GetJoystickState().Reset(); } return; } int pos = 3; int segments_header = DecodeUInt16(data[pos], data[pos + 1]); pos += 2; for (int controllerId = 0; controllerId < 16; controllerId++) { if ((segments_header & 1 << controllerId) == 0) { continue; } JoystickState joystickState = this.controllers[controllerId].GetJoystickState(); joystickState.Reset(); int axis_header = data[pos++]; if ((axis_header & 1 << 0) > 0) { joystickState.axis_x = DecodeInt7(data[pos++]) / 127f; } if ((axis_header & 1 << 1) > 0) { joystickState.axis_y = DecodeInt7(data[pos++]) / 127f; } if ((axis_header & 1 << 2) > 0) { joystickState.axis_z = DecodeInt7(data[pos++]) / 127f; } if ((axis_header & 1 << 3) > 0) { joystickState.axis_x_rot = DecodeInt7(data[pos++]) / 127f; } if ((axis_header & 1 << 4) > 0) { joystickState.axis_y_rot = DecodeInt7(data[pos++]) / 127f; } if ((axis_header & 1 << 5) > 0) { joystickState.axis_z_rot = DecodeInt7(data[pos++]) / 127f; } if ((axis_header & 1 << 6) > 0) { joystickState.slider1 = DecodeInt7(data[pos++]) / 127f; } if ((axis_header & 1 << 7) > 0) { joystickState.slider2 = DecodeInt7(data[pos++]) / 127f; } //Log(string.Format("x: {0} y: {1}", axis_x, axis_y)); int buttons_header = DecodeUInt16(data[pos], data[pos + 1]); pos += 2; if (buttons_header == 0) { int button_idx; while ((button_idx = data[pos++]) != 255) { joystickState.buttons[button_idx] = true; } } else { for (int button_group_idx = 0; button_group_idx < 16; button_group_idx++) { if ((buttons_header & 1 << button_group_idx) == 0) { continue; } int button_group = data[pos++]; for (int button_idx = 0; button_idx < 8; button_idx++) { if ((button_group & 1 << button_idx) > 0) { joystickState.buttons[8 * button_group_idx + button_idx] = true; } } } } } }