Cleaning up app command stuff.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
8 namespace Devices.RemoteControl
11 public enum InputDevice
19 public enum RemoteControlButton
76 #region RemoteControlEventArgs
78 public class RemoteControlEventArgs : EventArgs
80 RemoteControlButton _rcb;
83 ConsumerControl iConsumerControl;
85 public RemoteControlEventArgs(RemoteControlButton rcb, InputDevice device)
93 public RemoteControlEventArgs(ConsumerControl aConsumerControl, InputDevice device)
97 iConsumerControl = aConsumerControl;
102 public RemoteControlEventArgs(MceButton mce, InputDevice device)
110 private void SetNullButtons()
112 iConsumerControl = ConsumerControl.Null;
113 iMceButton = MceButton.Null;
114 _rcb = RemoteControlButton.Unknown;
117 public RemoteControlEventArgs()
119 iMceButton = MceButton.Null;
120 _rcb = RemoteControlButton.Unknown;
121 _device = InputDevice.Key;
124 public RemoteControlButton Button
127 set { _rcb = value; }
130 public MceButton MceButton
132 get { return iMceButton; }
133 set { iMceButton = value; }
136 public ConsumerControl ConsumerControl
138 get { return iConsumerControl; }
139 set { iConsumerControl = value; }
142 public InputDevice Device
144 get { return _device; }
145 set { _device = value; }
149 #endregion RemoteControlEventArgs
152 public sealed class RemoteControlDevice
154 public delegate bool RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
155 public event RemoteControlDeviceEventHandler ButtonPressed;
158 /// Return true if the usage was processed.
160 /// <param name="aUsage"></param>
161 /// <returns></returns>
162 public delegate bool HidUsageHandler(ushort aUsage);
165 //-------------------------------------------------------------
167 //-------------------------------------------------------------
169 public RemoteControlDevice(IntPtr aHWND)
171 // Register the input device to receive the commands from the
172 // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
173 // for the vendor defined usage page.
175 RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[3];
177 rid[0].usUsagePage = 0xFFBC;
178 rid[0].usUsage = 0x88;
179 rid[0].dwFlags = Const.RIDEV_EXINPUTSINK;
180 rid[0].hwndTarget = aHWND;
182 rid[1].usUsagePage = 0x0C;
183 rid[1].usUsage = 0x01;
184 rid[1].dwFlags = Const.RIDEV_EXINPUTSINK;
185 rid[1].hwndTarget = aHWND;
187 rid[2].usUsagePage = 0x0C;
188 rid[2].usUsage = 0x80;
189 rid[2].dwFlags = Const.RIDEV_EXINPUTSINK;
190 rid[2].hwndTarget = aHWND;
192 if (!Function.RegisterRawInputDevices(rid,(uint) rid.Length,(uint) Marshal.SizeOf(rid[0])))
194 throw new ApplicationException("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
199 //-------------------------------------------------------------
201 //-------------------------------------------------------------
203 public void ProcessMessage(Message message)
207 case Const.WM_KEYDOWN:
208 ProcessKeyDown(message.WParam);
211 //Returning zero means we processed that message.
212 message.Result = new IntPtr(0);
213 ProcessInputCommand(ref message);
220 //-------------------------------------------------------------
222 //-------------------------------------------------------------
224 private void ProcessKeyDown(IntPtr wParam)
226 RemoteControlButton rcb = RemoteControlButton.Unknown;
228 switch (wParam.ToInt32())
230 case (int) Keys.Escape:
231 rcb = RemoteControlButton.Clear;
233 case (int) Keys.Down:
234 rcb = RemoteControlButton.Down;
236 case (int) Keys.Left:
237 rcb = RemoteControlButton.Left;
240 rcb = RemoteControlButton.Digit0;
243 rcb = RemoteControlButton.Digit1;
246 rcb = RemoteControlButton.Digit2;
249 rcb = RemoteControlButton.Digit3;
252 rcb = RemoteControlButton.Digit4;
255 rcb = RemoteControlButton.Digit5;
258 rcb = RemoteControlButton.Digit6;
261 rcb = RemoteControlButton.Digit7;
264 rcb = RemoteControlButton.Digit8;
267 rcb = RemoteControlButton.Digit9;
269 case (int) Keys.Enter:
270 rcb = RemoteControlButton.Enter;
272 case (int) Keys.Right:
273 rcb = RemoteControlButton.Right;
276 rcb = RemoteControlButton.Up;
280 if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
281 this.ButtonPressed(this, new RemoteControlEventArgs(rcb, InputDevice.Key));
288 /// <param name="aUsage"></param>
289 private bool HidConsumerDeviceHandler(ushort aUsage)
297 if (Enum.IsDefined(typeof(ConsumerControl), aUsage) && aUsage != 0) //Our button is a known consumer control
299 if (this.ButtonPressed != null)
301 return this.ButtonPressed(this, new RemoteControlEventArgs((ConsumerControl)aUsage, InputDevice.OEM));
307 Debug.WriteLine("Unknown Consumer Control!");
315 /// <param name="aUsage"></param>
316 private bool HidMceRemoteHandler(ushort aUsage)
325 if (Enum.IsDefined(typeof(MceButton), aUsage) && aUsage != 0) //Our button is a known MCE button
327 if (this.ButtonPressed != null)
329 return this.ButtonPressed(this, new RemoteControlEventArgs((MceButton)aUsage, InputDevice.OEM));
335 Debug.WriteLine("Unknown MCE button!");
341 private void ProcessInputCommand(ref Message message)
343 //We received a WM_INPUT message
344 Debug.WriteLine("================WM_INPUT================");
346 //Check if we received this message while in background or foreground
347 if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUT)
349 Debug.WriteLine("================FOREGROUND");
351 else if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUTSINK)
353 Debug.WriteLine("================BACKGROUND");
356 //Declare some pointers
357 IntPtr rawInputBuffer = IntPtr.Zero;
358 //My understanding is that this is basically our HID descriptor
359 IntPtr preParsedData = IntPtr.Zero;
364 RAWINPUT rawInput = new RAWINPUT();
365 if (!RawInput.GetRawInputData(message.LParam, ref rawInput, ref rawInputBuffer))
371 RID_DEVICE_INFO deviceInfo = new RID_DEVICE_INFO();
372 if (!RawInput.GetDeviceInfo(rawInput.header.hDevice, ref deviceInfo))
378 if (rawInput.header.dwType == Const.RIM_TYPEHID) //Check that our raw input is HID
380 Debug.WriteLine("WM_INPUT source device is HID.");
381 //Get Usage Page and Usage
382 Debug.WriteLine("Usage Page: 0x" + deviceInfo.hid.usUsagePage.ToString("X4") + " Usage ID: 0x" + deviceInfo.hid.usUsage.ToString("X4"));
385 preParsedData = RawInput.GetPreParsedData(rawInput.header.hDevice);
388 HidUsageHandler usagePageHandler=null;
390 //Check if this an MCE remote HID message
391 if (deviceInfo.hid.usUsagePage == (ushort)Hid.UsagePage.MceRemote && deviceInfo.hid.usUsage == (ushort)Hid.UsageId.MceRemoteUsage)
393 usagePageHandler = HidMceRemoteHandler;
395 //Check if this is a consumer control HID message
396 else if (deviceInfo.hid.usUsagePage == (ushort)Hid.UsagePage.Consumer && deviceInfo.hid.usUsage == (ushort)Hid.UsageId.ConsumerControl)
398 usagePageHandler = HidConsumerDeviceHandler;
400 //Unknown HID message
403 Debug.WriteLine("Unknown HID message.");
407 if (!(rawInput.hid.dwSizeHid > 1 //Make sure our HID msg size more than 1. In fact the first ushort is irrelevant to us for now
408 && rawInput.hid.dwCount > 0)) //Check that we have at least one HID msg
414 //Allocate a buffer for one HID input
415 byte[] hidInputReport = new byte[rawInput.hid.dwSizeHid];
417 Debug.WriteLine("Raw input contains " + rawInput.hid.dwCount + " HID input report(s)");
419 //For each HID input report in our raw input
420 for (int i = 0; i < rawInput.hid.dwCount; i++)
422 //Compute the address from which to copy our HID input
423 int hidInputOffset = 0;
426 byte* source = (byte*)rawInputBuffer;
427 source += sizeof(RAWINPUTHEADER) + sizeof(RAWHID) + (rawInput.hid.dwSizeHid * i);
428 hidInputOffset = (int)source;
431 //Copy HID input into our buffer
432 Marshal.Copy(new IntPtr(hidInputOffset), hidInputReport, 0, (int)rawInput.hid.dwSizeHid);
434 //Print HID input report in our debug output
435 string hidDump = "HID input report: ";
436 foreach (byte b in hidInputReport)
438 hidDump += b.ToString("X2");
440 Debug.WriteLine(hidDump);
443 uint usageCount = 1; //Assuming a single usage per input report. Is that correct?
444 Win32.USAGE_AND_PAGE[] usages = new Win32.USAGE_AND_PAGE[usageCount];
445 Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, hidInputReport, (uint)hidInputReport.Length);
446 if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
448 Debug.WriteLine("Could not parse HID data!");
452 Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
453 Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
454 //Call on our Usage Page handler
455 usagePageHandler(usages[0].Usage);
460 else if (rawInput.header.dwType == Const.RIM_TYPEMOUSE)
462 Debug.WriteLine("WM_INPUT source device is Mouse.");
463 // do mouse handling...
465 else if (rawInput.header.dwType == Const.RIM_TYPEKEYBOARD)
467 Debug.WriteLine("WM_INPUT source device is Keyboard.");
468 // do keyboard handling...
474 //Always executed when leaving our try block
475 Marshal.FreeHGlobal(rawInputBuffer);
476 Marshal.FreeHGlobal(preParsedData);