/// <summary> /// Process HID events. /// </summary> /// <param name="aSender"></param> /// <param name="aHidEvent"></param> public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) { if (aHidEvent.IsStray) { //Stray event just ignore it return; } if (this.InvokeRequired) { //Not in the proper thread, invoke ourselves. //Repeat events usually come from another thread. OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe); this.Invoke(d, new object[] { aSender, aHidEvent }); } else { //We are in the proper thread listViewEvents.Items.Insert(0, aHidEvent.ToListViewItem()); if (aHidEvent.Device != null) { toolStripStatusLabelDevice.Text = aHidEvent.Device.FriendlyName; } richTextBoxLogs.AppendText(aHidEvent.ToLog()); } }
private void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) { if (aHidEvent.IsStray) { // Stray event just ignore it return; } if (this.InvokeRequired) { // Not in the proper thread, invoke ourselves. // Repeat events usually come from another thread. OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe); this.Invoke(d, new object[] { aSender, aHidEvent }); } else { if (HidEvent != null) { foreach (ushort usage in aHidEvent.Usages) { String key = "HID " + usage.ToString("X4"); HidEvent(aSender, key); } } } }
/// <summary> /// Here we receive HID events from our HID library. /// Used when recording during edition mode. /// </summary> /// <param name="aSender"></param> /// <param name="aHidEvent"></param> public virtual void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent) { var instancePath = Device.CurrentItem; if (CurrentState != State.Edit || aHidEvent.IsMouse || aHidEvent.IsButtonUp || !aHidEvent.IsValid || aHidEvent.IsBackground || aHidEvent.IsRepeat || aHidEvent.IsStray ) { return; } PrivateCopy(aHidEvent); // //Tell observer the object itself changed OnPropertyChanged("Brief"); // Check if our device changed if (!instancePath.Equals(Device.CurrentItem)) { // Only trigger that one if our device actually changed, we could find ourself in endless loops with HID axis otherwise somehow OnPropertyChanged("Device"); } }
public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) { if (aHidEvent.IsStray) { //Stray event just ignore it return; } if (this.InvokeRequired) { //Not in the proper thread, invoke ourselves. //Repeat events usually come from another thread. OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe); this.Invoke(d, new object[] { aSender, aHidEvent }); } else { //We are in the proper thread Trace.WriteLine(aHidEvent.ToLog().Replace(Environment.NewLine, "")); if (((aHidEvent.IsButtonDown) & (aHidEvent.ToString().Contains("HookSwitch (0x0020)") | aHidEvent.ToString().Contains("LineBusyTone (0x0097)")))) { string ST = CallApi("GET", "astproxy/extension/" + Ext, ""); dynamic data = JObject.Parse(ST); string Status = (string)data.status; Trace.WriteLine("Button click: " + Status + " with " + Ext.ToString()); string Ans, Hang; switch (Status) { case "ringing": if (Type == "webrtc") { Ans = CallApi("POST", "astproxy/answer_webrtc", "{\"endpointId\":\"" + Ext + "\",\"endpointType\":\"extension\"}"); } else { Ans = CallApi("POST", "astproxy/answer", "{\"endpointId\":\"" + Ext + "\",\"endpointType\":\"extension\"}"); } break; case "busy": string Conv = ""; foreach (JProperty Convx in data.conversations.Properties()) { Conv = (string)Convx.Name.ToString(); } Hang = CallApi("POST", "astproxy/hangup", "{\"convid\":\"" + Conv + "\",\"endpointId\":\"" + Ext + "\"}"); break; case "online": break; case "offline": break; } } } }
/// <summary> /// Here we receive HID events from our HID library. /// </summary> /// <param name="aSender"></param> /// <param name="aHidEvent"></param> public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent) { if (CurrentState != State.Edit || aHidEvent.IsMouse || aHidEvent.IsButtonUp || !aHidEvent.IsValid || aHidEvent.IsBackground || aHidEvent.IsRepeat || aHidEvent.IsStray) { return; } PrivateCopy(aHidEvent); // //Tell observer the object itself changed OnPropertyChanged("Brief"); }
/// <summary> /// Here we receive HID events from our HID library. /// </summary> /// <param name="aSender"></param> /// <param name="aHidEvent"></param> public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) { if (aHidEvent.IsStray || !aHidEvent.IsValid) { //Stray event just ignore it return; } if (this.InvokeRequired) { //Not in the proper thread, invoke ourselves OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe); this.Invoke(d, new object[] { aSender, aHidEvent }); } else { //Trigger corresponding EAR event if any EventHid e = new EventHid(); e.Copy(aHidEvent); Properties.Settings.Default.EarManager.TriggerEvents(e); } }
/// <summary> /// Here we receive HID events from our HID library. /// </summary> /// <param name="aSender"></param> /// <param name="aHidEvent"></param> public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent) { Log.Write("MPTray: HandleHidEvent"); if (aHidEvent.IsStray) { //Stray event just ignore it return; } //We are in the proper thread if (!(aHidEvent.Usages.Count > 0 && aHidEvent.UsagePage == (ushort)Hid.UsagePage.WindowsMediaCenterRemoteControl && aHidEvent.Usages[0] == (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart)) //&& aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer //&& aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier) { //Discard anything but the Green Start Button return; } DoStart(); }
/// <summary> /// This is just for recording in edit mode so performance is not an issue here. /// </summary> /// <param name="aSender"></param> /// <param name="aHidEvent"></param> public override void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent) { var instancePath = Device.CurrentItem; if (CurrentState != State.Edit || (!aHidEvent.Device.IsGamePad && !aHidEvent.Device.IsJoystick) || !aHidEvent.IsValid || aHidEvent.IsBackground || aHidEvent.IsRepeat || aHidEvent.IsStray ) { return; } // Make sure we know that device var device = GetDevice(aHidEvent.Device.InstancePath); if (device == null) { return; } // Dictionary <int, Axis> axes = null; if (!AxesByDevice.ContainsKey(device.InstancePath)) { // Create a collection of axis for that device then axes = new Dictionary <int, Axis>(); AxesByDevice[device.InstancePath] = axes; } else { // We already have an axis collection for that device, just fetch it then axes = AxesByDevice[device.InstancePath]; } // TODO: Only copy it if an axis is moving a lot //For each axis on that device foreach (KeyValuePair <HIDP_VALUE_CAPS, uint> entry in aHidEvent.UsageValues) { // Skip if this usage value is not a axis if (!SharpDisplayManager.Axis.IsAxis(entry.Key)) { continue; } var axis = new Axis(entry.Key); axis.Value = (int)entry.Value; //Check if we already know that device if (axes.ContainsKey(axis.Id)) { // Axis already in our collection, check it's value // TODO: review and improve those math? // Should we not use the rest position value instead of the first one we captured? If yes how do we get it? if (Math.Abs(axis.Value - axes[axis.Id].Value) > Math.Abs((axis.Max - axis.Min) / 4)) { // Value moved enough take on that device and axis then PrivateCopy(aHidEvent); // Most sets the device for us // Check if our device changed if (!instancePath.Equals(Device.CurrentItem)) { // Only trigger that one if our device actually changed, we could find ourself in endless loops with HID axis otherwise somehow OnPropertyChanged("Device"); } if (Axis.CurrentItem != axis.FullName) { Axis.CurrentItem = axis.FullName; OnPropertyChanged("Axis"); } //Object description may have changed OnPropertyChanged("Brief"); } } else { // First time we meet that axis just keep track of it and its rest value then axes[axis.Id] = axis; } } }
unsafe public void OnHidEvent(object aSender, SharpLib.Hid.Event aHidEvent) { if (aHidEvent.IsStray) { //Stray event just ignore it return; } // Oculus Rift S Touch Controller report starts with 0x67 if (aHidEvent.InputReport[0] != 0x67) { return; //Console.WriteLine(aHidEvent.InputReportString()); } var size = aHidEvent.InputReport.Length; if (size < 62) { return; } iMutex.WaitOne(); // Parse our raw input report into our controller report class. Utils.ParseControllerInputReport(aHidEvent.InputReport, iCtrlReport); // Check if we have a valid device ID if (iCtrlReport.device_id != 0) { // Check if we already meat this device ControllerState state; int position = Console.WindowTop + iControllers.Count * 20; if (!iControllers.TryGetValue(iCtrlReport.device_id, out state)) { // That's a new device, create a state object for it then and add it to our collection state = new ControllerState(); state.device_id = iCtrlReport.device_id; iControllers.Add(iCtrlReport.device_id, state); //Console.WriteLine("Found new device: 0x" + report.device_id.ToString("X")); iPosition.Add(iCtrlReport.device_id, position); } // Now that we have our state object for this device, we need to update it with the incoming input report Utils.UpdateControllerState(ref state, iCtrlReport); } // Do not update our prints too often to avoid lags bool updatePrints = iStopWatch.ElapsedMilliseconds >= 100; foreach (var ctrl in iControllers.Values) { // Quick and dirty mapping to keyboard input for MWO if (ctrl.device_id == 0xCE156735BF2F1676) // // Right controller { var position = iPosition[ctrl.device_id]; if (updatePrints) { // Print our controller states in our console Console.SetCursorPosition(Console.WindowLeft, position); //Console.Clear(); string blank = " "; Console.WriteLine(ctrl.Dump()); if (isDownForward) { Console.WriteLine("Up"); } else { Console.WriteLine(blank); } if (isDownBackward) { Console.WriteLine("Down"); } else { Console.WriteLine(blank); } if (isDownLeft) { Console.WriteLine("Left"); } else { Console.WriteLine(blank); } if (isDownRight) { Console.WriteLine("Right"); } else { Console.WriteLine(blank); } iStopWatch.Restart(); } // Throttle if (ctrl.joystick_y > 10000) { // Moving forward if (!isDownForward) { isDownForward = true; isDownBackward = false; // iInputSim.Keyboard.KeyDown(iUp); iInputSim.Keyboard.KeyUp(iDown); } } else if (ctrl.joystick_y < -10000) { // Moving backward if (!isDownBackward) { isDownForward = false; isDownBackward = true; // iInputSim.Keyboard.KeyDown(iDown); iInputSim.Keyboard.KeyUp(iUp); } } else { if (isDownForward || isDownBackward) { isDownForward = false; isDownBackward = false; // iInputSim.Keyboard.KeyUp(iDown); iInputSim.Keyboard.KeyUp(iUp); } } // Rotation if (ctrl.joystick_x > 15000) { // Moving right if (!isDownRight) { isDownLeft = false; isDownRight = true; // iInputSim.Keyboard.KeyDown(iRight); iInputSim.Keyboard.KeyUp(iLeft); } } else if (ctrl.joystick_x < -15000) { // Moving left if (!isDownLeft) { // We used those to measure our input lag by comparing the time the event is sent here and the time it is received in HidDemo //Console.WriteLine("\nLeft down: " + aHidEvent.Time.ToString("HH:mm:ss:fff")); isDownLeft = true; isDownRight = false; // iInputSim.Keyboard.KeyDown(iLeft); iInputSim.Keyboard.KeyUp(iRight); } } else { if (isDownRight) { // Use that to debug input lag //Console.WriteLine("Right up: " + aHidEvent.Time.ToString("HH:mm:ss:fff")); isDownRight = false; iInputSim.Keyboard.KeyUp(iRight); } if (isDownLeft) { // Use that to debug input lag //Console.WriteLine("Left up: " + aHidEvent.Time.ToString("HH:mm:ss:fff")); isDownLeft = false; iInputSim.Keyboard.KeyUp(iLeft); } } //Target if (ctrl.trigger < 3072) { if (!isDownTrigger) { isDownTrigger = true; iInputSim.Keyboard.KeyDown(VirtualKeyCode.VK_R); } } else if (isDownTrigger) { isDownTrigger = false; iInputSim.Keyboard.KeyUp(VirtualKeyCode.VK_R); } // Push-to-talk if (ctrl.grip < 512) { if (!isDownGrip) { //Console.WriteLine("down "); isDownGrip = true; iInputSim.Keyboard.KeyDown(VirtualKeyCode.CAPITAL); } } else if (isDownGrip) { //Console.WriteLine("up "); isDownGrip = false; iInputSim.Keyboard.KeyUp(VirtualKeyCode.CAPITAL); } // Third weapon if (ctrl.ButtonStick()) { if (!isDownButtonStick) { //Console.WriteLine("down "); isDownButtonStick = true; iInputSim.Keyboard.KeyDown(VirtualKeyCode.VK_3); } } else if (isDownButtonStick) { //Console.WriteLine("up "); isDownButtonStick = false; iInputSim.Keyboard.KeyUp(VirtualKeyCode.VK_3); } // Battlegrid if (ctrl.ButtonMenu()) { if (!isDownButtonMenu) { //Console.WriteLine("down "); isDownButtonMenu = true; iInputSim.Keyboard.KeyDown(VirtualKeyCode.VK_B); } } else if (isDownButtonMenu) { //Console.WriteLine("up "); isDownButtonMenu = false; iInputSim.Keyboard.KeyUp(VirtualKeyCode.VK_B); } } } iMutex.ReleaseMutex(); }