private void CaptureThread() { while (true) { try { if (!this.isCapturing) { return; } if (gamepad.Acquire().IsFailure) { return; } if (gamepad.Poll().IsFailure) { return; } if (SlimDX.Result.Last.IsFailure) { return; } state = gamepad.GetCurrentState(); bool[] buttons = state.GetButtons(); //for (int i = 0; i < buttons.Length; i++) // if (buttons[i]) // label2.Text = i.ToString(); } catch (Exception ex) { } if (OnStateUpdated != null) { OnStateUpdated(state); } try { gamepad.Unacquire(); } catch (Exception ex) { } Thread.Sleep(50); } }
/// <summary> /// Callback, получающий информацию об изменениях в состоянии контролов джойстика /// </summary> private void GetJoystickData() { while (true) { try { lock (_threadLock) { if (_quitThread) { break; } } if (!_joystickEvent.WaitOne(30)) { continue; } _joystick.Poll(); var state = _joystick.GetCurrentState(); var buttons = state.GetButtons(); lock (_dumpObjectLocker) { for (uint i = 0; i < _buttons.Length; i++) { if ((buttons[i] == _buttons[i]) && _dumpMode == false) { continue; } var ev = new ButtonEvent { Hardware = new ControlProcessorHardware { ModuleType = HardwareModuleType.Button, MotherBoardId = GetJoystickGuid(), ModuleId = (uint)JoystickModule.Button, ControlId = i }, IsPressed = buttons[i] }; _buttons[i] = buttons[i]; _incomingEvents.Enqueue(ev); } // ControlId начинается с 1 var axisIndex = 1; if (_axis.Length >= axisIndex) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], state.X); } if (_axis.Length >= axisIndex) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], state.Y); } if (_axis.Length >= axisIndex) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], state.Z); } if (_axis.Length >= axisIndex) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], state.RotationX); } if (_axis.Length >= axisIndex) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], state.RotationY); } if (_axis.Length >= axisIndex) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], state.RotationZ); } var extAxis = state.GetSliders(); if (_axis.Length >= axisIndex && extAxis.Length >= 1) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], extAxis[0]); } if (_axis.Length >= axisIndex && extAxis.Length >= 2) { AddAxisEvent((uint)axisIndex, _axis[axisIndex++], extAxis[1]); } _dumpMode = false; } } catch (Exception) { } } }
/// <summary> /// handles all events that are trigged from joystick. /// </summary> private void pollingClient() { joystickDevice.Poll(); JoystickState state = joystickDevice.GetCurrentState(); if (previousState.X != state.X && !(state.X == beforePreviousState.X && previousState.X == beforeBeforePreviousState.X)) { int status = calculateStateValue(axisX, state.X); onAxisX(status); log("pollingClient", "onAxisX(" + status + ")"); } if (previousState.Y != state.Y && !(state.Y == beforePreviousState.Y && previousState.Y == beforeBeforePreviousState.Y)) { int status = calculateStateValue(axisY, state.Y); onAxisY(status); log("pollingClient", "onAxisY(" + status + ")"); } if (previousState.Z != state.Z && !(state.Z == beforePreviousState.Z && previousState.Z == beforeBeforePreviousState.Z)) { int status = calculateStateValue(axisZ, state.Z); onAxisZ(status); log("pollingClient", "onAxisZ(" + status + ")"); } /* Rotation */ if (previousState.RotationX != state.RotationX && !(state.RotationX == beforePreviousState.RotationX && previousState.RotationX == beforeBeforePreviousState.RotationX)) { int status = calculateStateValue(axisRx, state.RotationX); onAxisRx(status); log("pollingClient", "onAxisRx(" + status + ")"); } if (previousState.RotationY != state.RotationY && !(state.RotationY == beforePreviousState.RotationY && previousState.RotationY == beforeBeforePreviousState.RotationY)) { int status = calculateStateValue(axisRy, state.RotationY); onAxisRy(status); log("pollingClient", "onAxisRy(" + status + ")"); } if (previousState.RotationZ != state.RotationZ && !(state.RotationZ == beforePreviousState.RotationZ && previousState.RotationZ == beforeBeforePreviousState.RotationZ)) { int status = calculateStateValue(axisRz, state.RotationZ); onAxisRz(status); log("pollingClient", "onAxisRz(" + status + ")"); } /* Extra axis */ int[] axis = state.GetSliders(); int[] previousAxis = previousState.GetSliders(); int[] beforePreviousAxis = beforePreviousState.GetSliders(); int[] beforeBeforePreviousAxis = beforeBeforePreviousState.GetSliders(); for (int i = 0, c = axis.Length; i < c; i++) { if (previousAxis[i] != axis[i] && !(axis[i] == beforePreviousAxis[i] && previousAxis[i] == beforeBeforePreviousAxis[i])) { int status = calculateStateValue(axisExtra, axis[i]); onAxisExtra(i, status); log("pollingClient", "onAxisExtra(" + i + "," + status + ")"); } } /* All Buttons */ bool[] buttons = state.GetButtons(); bool[] previousButtons = previousState.GetButtons(); for (int i = 0, c = buttons.Length; i < c; i++) { if (previousButtons[i] != buttons[i]) { onButton(i, buttons[i]); log("pollingClient", "onButton(" + i + "," + buttons[i] + ")"); } } int[] pointOfViews = state.GetPointOfViewControllers(); int[] previousPointOfViews = previousState.GetPointOfViewControllers(); for (int i = 0, c = pointOfViews.Length; i < c; i++) { if (previousPointOfViews[i] != pointOfViews[i]) { onPov(i, pointOfViews[i]); log("pollingClient", "onPov(" + i + "," + pointOfViews[i] + ")"); } } beforeBeforePreviousState = beforePreviousState; beforePreviousState = previousState; previousState = state; }