Adding missing entries in consumer usage table.
Adding magic MyTv code in MCE usage table as handled by MP.
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4 using System.Diagnostics;
6 using Microsoft.Win32.SafeHandles;
13 namespace Devices.RemoteControl
16 public enum InputDevice
24 public enum RemoteControlButton
81 #region RemoteControlEventArgs
83 public class RemoteControlEventArgs : EventArgs
85 RemoteControlButton _rcb;
87 WindowsMediaCenterRemoteControl iMceButton;
88 ConsumerControl iConsumerControl;
90 public RemoteControlEventArgs(RemoteControlButton rcb, InputDevice device)
98 public RemoteControlEventArgs(ConsumerControl aConsumerControl, InputDevice device)
102 iConsumerControl = aConsumerControl;
107 public RemoteControlEventArgs(WindowsMediaCenterRemoteControl mce, InputDevice device)
115 private void SetNullButtons()
117 iConsumerControl = ConsumerControl.Null;
118 iMceButton = WindowsMediaCenterRemoteControl.Null;
119 _rcb = RemoteControlButton.Unknown;
122 public RemoteControlEventArgs()
124 iMceButton = WindowsMediaCenterRemoteControl.Null;
125 _rcb = RemoteControlButton.Unknown;
126 _device = InputDevice.Key;
129 public RemoteControlButton Button
132 set { _rcb = value; }
135 public WindowsMediaCenterRemoteControl MceButton
137 get { return iMceButton; }
138 set { iMceButton = value; }
141 public ConsumerControl ConsumerControl
143 get { return iConsumerControl; }
144 set { iConsumerControl = value; }
147 public InputDevice Device
149 get { return _device; }
150 set { _device = value; }
154 #endregion RemoteControlEventArgs
157 public sealed class RemoteControlDevice
159 public delegate bool RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
160 public event RemoteControlDeviceEventHandler ButtonPressed;
163 /// Return true if the usage was processed.
165 /// <param name="aUsage"></param>
166 /// <returns></returns>
167 public delegate bool HidUsageHandler(ushort aUsage);
169 Hid.HidHandler iHidHandler;
172 //-------------------------------------------------------------
174 //-------------------------------------------------------------
176 public RemoteControlDevice(IntPtr aHWND)
178 // Register the input device to receive the commands from the
179 // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
180 // for the vendor defined usage page.
182 RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[4];
185 rid[i].usUsagePage = (ushort)Hid.UsagePage.WindowsMediaCenterRemoteControl;
186 rid[i].usUsage = (ushort)Hid.UsageCollectionWindowsMediaCenter.WindowsMediaCenterRemoteControl;
187 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
188 rid[i].hwndTarget = aHWND;
191 rid[i].usUsagePage = (ushort)Hid.UsagePage.Consumer;
192 rid[i].usUsage = (ushort)Hid.UsageCollectionConsumer.ConsumerControl;
193 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
194 rid[i].hwndTarget = aHWND;
197 rid[i].usUsagePage = (ushort)Hid.UsagePage.Consumer;
198 rid[i].usUsage = (ushort)Hid.UsageCollectionConsumer.Selection;
199 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
200 rid[i].hwndTarget = aHWND;
203 rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
204 rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.SystemControl;
205 rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
206 rid[i].hwndTarget = aHWND;
209 //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
210 //rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.Keyboard;
211 //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
212 //rid[i].hwndTarget = aHWND;
215 //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
216 //rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.Mouse;
217 //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
218 //rid[i].hwndTarget = aHWND;
221 iHidHandler = new Hid.HidHandler(rid);
222 if (!iHidHandler.IsRegistered)
224 Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
226 iHidHandler.OnHidEvent += HandleHidEvent;
230 //-------------------------------------------------------------
232 //-------------------------------------------------------------
234 public void ProcessMessage(Message message)
238 case Const.WM_KEYDOWN:
239 ProcessKeyDown(message.WParam);
242 //Returning zero means we processed that message.
243 message.Result = new IntPtr(0);
244 ProcessInputCommand(ref message);
251 //-------------------------------------------------------------
253 //-------------------------------------------------------------
255 private void ProcessKeyDown(IntPtr wParam)
257 RemoteControlButton rcb = RemoteControlButton.Unknown;
259 switch (wParam.ToInt32())
261 case (int)Keys.Escape:
262 rcb = RemoteControlButton.Clear;
265 rcb = RemoteControlButton.Up;
268 rcb = RemoteControlButton.Down;
271 rcb = RemoteControlButton.Left;
273 case (int)Keys.Right:
274 rcb = RemoteControlButton.Right;
276 case (int)Keys.Enter:
277 rcb = RemoteControlButton.Enter;
280 rcb = RemoteControlButton.Digit0;
283 rcb = RemoteControlButton.Digit1;
286 rcb = RemoteControlButton.Digit2;
289 rcb = RemoteControlButton.Digit3;
292 rcb = RemoteControlButton.Digit4;
295 rcb = RemoteControlButton.Digit5;
298 rcb = RemoteControlButton.Digit6;
301 rcb = RemoteControlButton.Digit7;
304 rcb = RemoteControlButton.Digit8;
307 rcb = RemoteControlButton.Digit9;
311 if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
313 Debug.WriteLine("KeyDown: " + rcb.ToString());
314 this.ButtonPressed(this, new RemoteControlEventArgs(rcb, InputDevice.Key));
322 /// <param name="aUsage"></param>
323 private bool HidConsumerDeviceHandler(ushort aUsage)
331 if (Enum.IsDefined(typeof(ConsumerControl), aUsage) && aUsage != 0) //Our button is a known consumer control
333 if (this.ButtonPressed != null)
335 return this.ButtonPressed(this, new RemoteControlEventArgs((ConsumerControl)aUsage, InputDevice.OEM));
341 Debug.WriteLine("Unknown Consumer Control!");
349 /// <param name="aUsage"></param>
350 private bool HidMceRemoteHandler(ushort aUsage)
359 if (Enum.IsDefined(typeof(WindowsMediaCenterRemoteControl), aUsage) && aUsage != 0) //Our button is a known MCE button
361 if (this.ButtonPressed != null)
363 return this.ButtonPressed(this, new RemoteControlEventArgs((WindowsMediaCenterRemoteControl)aUsage, InputDevice.OEM));
369 Debug.WriteLine("Unknown MCE button!");
377 /// <param name="message"></param>
378 private void ProcessInputCommand(ref Message message)
380 //We received a WM_INPUT message
381 Debug.WriteLine("================WM_INPUT================");
383 iHidHandler.ProcessInput(message);
390 /// <param name="aSender"></param>
391 /// <param name="aHidEvent"></param>
392 void HandleHidEvent(object aSender, Hid.HidEvent aHidEvent)
394 HidUsageHandler usagePageHandler = null;
396 //Check if this an MCE remote HID message
397 if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.WindowsMediaCenterRemoteControl && aHidEvent.UsageCollection == (ushort)Hid.UsageCollectionWindowsMediaCenter.WindowsMediaCenterRemoteControl)
399 usagePageHandler = HidMceRemoteHandler;
401 //Check if this is a consumer control HID message
402 else if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer && aHidEvent.UsageCollection == (ushort)Hid.UsageCollectionConsumer.ConsumerControl)
404 usagePageHandler = HidConsumerDeviceHandler;
406 //Unknown HID message
409 Debug.WriteLine("Unknown HID message.");
413 foreach (ushort usage in aHidEvent.Usages)
415 usagePageHandler(usage);