RemoteControlDevice.cs
author sl
Tue, 04 Nov 2014 19:49:34 +0100
changeset 2 505a13a09336
parent 0 82a61d3d2706
child 3 db8e6a25d6bc
permissions -rw-r--r--
Removing x64 configuration as our interop it's not working anyway.
Better debug logs with padding.
sl@0
     1
using System;
sl@0
     2
using System.Windows.Forms;
sl@0
     3
using System.Runtime.InteropServices;
sl@0
     4
using System.Diagnostics;
sl@0
     5
sl@0
     6
sl@0
     7
namespace BruceThomas.Devices.RemoteControl
sl@0
     8
{
sl@0
     9
	public enum InputDevice
sl@0
    10
	{
sl@0
    11
		Key,
sl@0
    12
		Mouse,
sl@0
    13
		OEM
sl@0
    14
	}
sl@0
    15
sl@0
    16
sl@0
    17
	public enum RemoteControlButton
sl@0
    18
	{
sl@0
    19
		Clear,
sl@0
    20
		Down,
sl@0
    21
		Left,
sl@0
    22
		Digit0,
sl@0
    23
		Digit1,
sl@0
    24
		Digit2,
sl@0
    25
		Digit3,
sl@0
    26
		Digit4,
sl@0
    27
		Digit5,
sl@0
    28
		Digit6,
sl@0
    29
		Digit7,
sl@0
    30
		Digit8,
sl@0
    31
		Digit9,
sl@0
    32
		Enter,
sl@0
    33
		Right,
sl@0
    34
		Up,
sl@0
    35
sl@0
    36
		Back,
sl@0
    37
		ChannelDown,
sl@0
    38
		ChannelUp,
sl@0
    39
		FastForward,
sl@0
    40
		VolumeMute,
sl@0
    41
		Pause,
sl@0
    42
		Play,
sl@0
    43
        PlayPause,
sl@0
    44
		Record,
sl@0
    45
		PreviousTrack,
sl@0
    46
		Rewind,
sl@0
    47
		NextTrack,
sl@0
    48
		Stop,
sl@0
    49
		VolumeDown,
sl@0
    50
		VolumeUp,
sl@0
    51
sl@0
    52
		RecordedTV,
sl@0
    53
		Guide,
sl@0
    54
		LiveTV,
sl@0
    55
		Details,
sl@0
    56
		DVDMenu,
sl@0
    57
		DVDAngle,
sl@0
    58
		DVDAudio,
sl@0
    59
		DVDSubtitle,
sl@0
    60
		MyMusic,
sl@0
    61
		MyPictures,
sl@0
    62
		MyVideos,
sl@0
    63
		MyTV,
sl@0
    64
		OEM1,
sl@0
    65
		OEM2,
sl@0
    66
		StandBy,
sl@0
    67
		TVJump,
sl@0
    68
sl@0
    69
		Unknown
sl@0
    70
	}
sl@0
    71
sl@0
    72
sl@0
    73
	#region RemoteControlEventArgs
sl@0
    74
sl@0
    75
	public class RemoteControlEventArgs : EventArgs
sl@0
    76
	{
sl@0
    77
		RemoteControlButton _rcb;
sl@0
    78
		InputDevice _device;
sl@0
    79
sl@0
    80
		public RemoteControlEventArgs(RemoteControlButton rcb, InputDevice device)
sl@0
    81
		{
sl@0
    82
			_rcb = rcb;
sl@0
    83
			_device = device;
sl@0
    84
		}
sl@0
    85
sl@0
    86
sl@0
    87
		public RemoteControlEventArgs()
sl@0
    88
		{
sl@0
    89
			_rcb = RemoteControlButton.Unknown;
sl@0
    90
			_device = InputDevice.Key;
sl@0
    91
		}
sl@0
    92
sl@0
    93
sl@0
    94
		public RemoteControlButton Button
sl@0
    95
		{
sl@0
    96
			get { return _rcb;  }
sl@0
    97
			set { _rcb = value; }
sl@0
    98
		}
sl@0
    99
sl@0
   100
		public InputDevice Device
sl@0
   101
		{
sl@0
   102
			get { return _device;  }
sl@0
   103
			set { _device = value; }
sl@0
   104
		}
sl@0
   105
	}
sl@0
   106
sl@0
   107
	#endregion RemoteControlEventArgs
sl@0
   108
sl@0
   109
sl@0
   110
	public sealed class RemoteControlDevice
sl@0
   111
	{
sl@0
   112
sl@0
   113
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@0
   114
		internal struct RAWINPUTDEVICE
sl@0
   115
		{
sl@0
   116
			[MarshalAs(UnmanagedType.U2)]
sl@0
   117
			public ushort usUsagePage;
sl@0
   118
			[MarshalAs(UnmanagedType.U2)]
sl@0
   119
			public ushort usUsage;
sl@0
   120
			[MarshalAs(UnmanagedType.U4)]
sl@0
   121
			public int	 dwFlags;
sl@0
   122
			public IntPtr hwndTarget;
sl@0
   123
		}
sl@0
   124
sl@0
   125
sl@0
   126
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@0
   127
		internal struct RAWINPUTHEADER
sl@0
   128
		{
sl@0
   129
			[MarshalAs(UnmanagedType.U4)]
sl@0
   130
			public int dwType;
sl@0
   131
			[MarshalAs(UnmanagedType.U4)]
sl@0
   132
			public int dwSize;
sl@0
   133
            public IntPtr hDevice;
sl@0
   134
			[MarshalAs(UnmanagedType.U4)]
sl@0
   135
			public int wParam;
sl@0
   136
		}
sl@0
   137
sl@0
   138
sl@0
   139
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@0
   140
		internal struct RAWHID
sl@0
   141
		{
sl@0
   142
			[MarshalAs(UnmanagedType.U4)]
sl@0
   143
			public int dwSizeHid;
sl@0
   144
			[MarshalAs(UnmanagedType.U4)]
sl@0
   145
			public int dwCount;
sl@0
   146
            //
sl@0
   147
            //BYTE  bRawData[1];
sl@0
   148
		}
sl@0
   149
sl@0
   150
sl@0
   151
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@0
   152
		internal struct BUTTONSSTR
sl@0
   153
		{
sl@0
   154
			[MarshalAs(UnmanagedType.U2)]
sl@0
   155
			public ushort usButtonFlags;
sl@0
   156
			[MarshalAs(UnmanagedType.U2)]
sl@0
   157
			public ushort usButtonData;
sl@0
   158
		}
sl@0
   159
sl@0
   160
sl@0
   161
        [StructLayout(LayoutKind.Explicit, Pack = 1)]
sl@0
   162
		internal struct RAWMOUSE
sl@0
   163
		{
sl@0
   164
			[MarshalAs(UnmanagedType.U2)]
sl@0
   165
			[FieldOffset (0)] public ushort usFlags;
sl@0
   166
			[MarshalAs(UnmanagedType.U4)]
sl@0
   167
			[FieldOffset (4)] public uint ulButtons;
sl@0
   168
			[FieldOffset (4)] public BUTTONSSTR buttonsStr;
sl@0
   169
			[MarshalAs(UnmanagedType.U4)]
sl@0
   170
			[FieldOffset (8)] public uint ulRawButtons;
sl@0
   171
            [MarshalAs(UnmanagedType.U4)]
sl@0
   172
            [FieldOffset (12)] public int lLastX;
sl@0
   173
            [MarshalAs(UnmanagedType.U4)]
sl@0
   174
            [FieldOffset (16)] public int lLastY;
sl@0
   175
			[MarshalAs(UnmanagedType.U4)]
sl@0
   176
			[FieldOffset (20)] public uint ulExtraInformation;
sl@0
   177
		}
sl@0
   178
sl@0
   179
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@0
   180
		internal struct RAWKEYBOARD
sl@0
   181
		{
sl@0
   182
			[MarshalAs(UnmanagedType.U2)]
sl@0
   183
			public ushort MakeCode;
sl@0
   184
			[MarshalAs(UnmanagedType.U2)]
sl@0
   185
			public ushort Flags;
sl@0
   186
			[MarshalAs(UnmanagedType.U2)]
sl@0
   187
			public ushort Reserved;
sl@0
   188
			[MarshalAs(UnmanagedType.U2)]
sl@0
   189
			public ushort VKey;
sl@0
   190
			[MarshalAs(UnmanagedType.U4)]
sl@0
   191
			public uint Message;
sl@0
   192
			[MarshalAs(UnmanagedType.U4)]
sl@0
   193
			public uint ExtraInformation;
sl@0
   194
		}
sl@0
   195
sl@0
   196
sl@0
   197
		[StructLayout(LayoutKind.Explicit, Pack=1)]
sl@0
   198
		internal struct RAWINPUT
sl@0
   199
		{
sl@0
   200
			[FieldOffset  (0)] public RAWINPUTHEADER header;
sl@0
   201
			[FieldOffset (16)] public RAWMOUSE mouse;
sl@0
   202
			[FieldOffset (16)] public RAWKEYBOARD keyboard;
sl@0
   203
			[FieldOffset (16)] public RAWHID hid;
sl@0
   204
		}
sl@0
   205
sl@0
   206
sl@0
   207
		[DllImport("User32.dll")]
sl@0
   208
		extern static bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint uiNumDevices, uint cbSize);
sl@0
   209
sl@0
   210
		[DllImport("User32.dll")]
sl@0
   211
		extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
sl@0
   212
sl@0
   213
sl@0
   214
		private const int WM_KEYDOWN	= 0x0100;
sl@0
   215
		private const int WM_APPCOMMAND	= 0x0319;
sl@0
   216
		private const int WM_INPUT		= 0x00FF;
sl@0
   217
sl@0
   218
		private const int APPCOMMAND_BROWSER_BACKWARD   = 1;
sl@0
   219
		private const int APPCOMMAND_VOLUME_MUTE        = 8;
sl@0
   220
		private const int APPCOMMAND_VOLUME_DOWN        = 9;
sl@0
   221
		private const int APPCOMMAND_VOLUME_UP          = 10;
sl@0
   222
		private const int APPCOMMAND_MEDIA_NEXTTRACK    = 11;
sl@0
   223
		private const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12;
sl@0
   224
		private const int APPCOMMAND_MEDIA_STOP         = 13;
sl@0
   225
		private const int APPCOMMAND_MEDIA_PLAY_PAUSE   = 14;
sl@0
   226
		private const int APPCOMMAND_MEDIA_PLAY         = 46;
sl@0
   227
		private const int APPCOMMAND_MEDIA_PAUSE        = 47;
sl@0
   228
		private const int APPCOMMAND_MEDIA_RECORD       = 48;
sl@0
   229
		private const int APPCOMMAND_MEDIA_FAST_FORWARD = 49;
sl@0
   230
		private const int APPCOMMAND_MEDIA_REWIND       = 50;
sl@0
   231
		private const int APPCOMMAND_MEDIA_CHANNEL_UP   = 51;
sl@0
   232
		private const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52;
sl@0
   233
sl@0
   234
		private const int RAWINPUT_DETAILS				= 0x209;
sl@0
   235
		private const int RAWINPUT_GUIDE				= 0x8D;
sl@0
   236
		private const int RAWINPUT_TVJUMP				= 0x25;
sl@0
   237
		private const int RAWINPUT_STANDBY				= 0x82;
sl@0
   238
		private const int RAWINPUT_OEM1					= 0x80;
sl@0
   239
		private const int RAWINPUT_OEM2					= 0x81;
sl@0
   240
		private const int RAWINPUT_MYTV					= 0x46;
sl@0
   241
		private const int RAWINPUT_MYVIDEOS				= 0x4A;
sl@0
   242
		private const int RAWINPUT_MYPICTURES			= 0x49;
sl@0
   243
		private const int RAWINPUT_MYMUSIC				= 0x47;
sl@0
   244
		private const int RAWINPUT_RECORDEDTV			= 0x48;
sl@0
   245
		private const int RAWINPUT_DVDANGLE				= 0x4B;
sl@0
   246
		private const int RAWINPUT_DVDAUDIO				= 0x4C;
sl@0
   247
		private const int RAWINPUT_DVDMENU				= 0x24;
sl@0
   248
		private const int RAWINPUT_DVDSUBTITLE			= 0x4D;
sl@0
   249
sl@0
   250
		private const int RIM_TYPEMOUSE					= 0;
sl@0
   251
		private const int RIM_TYPEKEYBOARD				= 1;
sl@0
   252
		private const int RIM_TYPEHID					= 2;
sl@0
   253
sl@0
   254
		private const int RID_INPUT						= 0x10000003;
sl@0
   255
		private const int RID_HEADER					= 0x10000005;
sl@0
   256
sl@0
   257
		private const int FAPPCOMMAND_MASK				= 0xF000;
sl@0
   258
		private const int FAPPCOMMAND_MOUSE				= 0x8000;
sl@0
   259
		private const int FAPPCOMMAND_KEY				= 0;
sl@0
   260
		private const int FAPPCOMMAND_OEM				= 0x1000;
sl@0
   261
sl@0
   262
		public delegate void RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
sl@0
   263
		public event RemoteControlDeviceEventHandler ButtonPressed;
sl@0
   264
sl@0
   265
sl@0
   266
		//-------------------------------------------------------------
sl@0
   267
		// constructors
sl@0
   268
		//-------------------------------------------------------------
sl@0
   269
sl@0
   270
		public RemoteControlDevice()
sl@0
   271
		{
sl@0
   272
			// Register the input device to receive the commands from the
sl@0
   273
			// remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
sl@0
   274
			// for the vendor defined usage page.
sl@0
   275
sl@0
   276
			RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[3];
sl@0
   277
sl@0
   278
			rid[0].usUsagePage = 0xFFBC;
sl@0
   279
			rid[0].usUsage = 0x88;
sl@0
   280
			rid[0].dwFlags = 0;
sl@0
   281
sl@0
   282
			rid[1].usUsagePage = 0x0C;
sl@0
   283
			rid[1].usUsage = 0x01;
sl@0
   284
			rid[1].dwFlags = 0;
sl@0
   285
sl@0
   286
			rid[2].usUsagePage = 0x0C;
sl@0
   287
			rid[2].usUsage = 0x80;
sl@0
   288
			rid[2].dwFlags = 0;
sl@0
   289
sl@0
   290
			if (!RegisterRawInputDevices(rid,
sl@0
   291
				(uint) rid.Length,
sl@0
   292
				(uint) Marshal.SizeOf(rid[0]))
sl@0
   293
				)
sl@0
   294
			{
sl@0
   295
				throw new ApplicationException("Failed to register raw input devices.");
sl@0
   296
			}
sl@0
   297
		}
sl@0
   298
sl@0
   299
sl@0
   300
		//-------------------------------------------------------------
sl@0
   301
		// methods
sl@0
   302
		//-------------------------------------------------------------
sl@0
   303
sl@0
   304
		public void ProcessMessage(Message message)
sl@0
   305
		{
sl@0
   306
			int param;
sl@0
   307
sl@0
   308
			switch (message.Msg)
sl@0
   309
			{
sl@0
   310
				case WM_KEYDOWN:
sl@0
   311
					param = message.WParam.ToInt32();
sl@0
   312
					ProcessKeyDown(param);
sl@0
   313
					break;
sl@0
   314
				case WM_APPCOMMAND:
sl@0
   315
					param = message.LParam.ToInt32();
sl@0
   316
					ProcessAppCommand(param);
sl@0
   317
					break;
sl@0
   318
				case WM_INPUT:
sl@0
   319
					ProcessInputCommand(ref message);
sl@0
   320
                    message.Result = new IntPtr(0);
sl@0
   321
					break;
sl@0
   322
			}
sl@0
   323
sl@0
   324
		}
sl@0
   325
sl@0
   326
sl@0
   327
		//-------------------------------------------------------------
sl@0
   328
		// methods (helpers)
sl@0
   329
		//-------------------------------------------------------------
sl@0
   330
sl@0
   331
		private void ProcessKeyDown(int param)
sl@0
   332
		{
sl@0
   333
			RemoteControlButton rcb = RemoteControlButton.Unknown;
sl@0
   334
sl@0
   335
			switch (param)
sl@0
   336
			{
sl@0
   337
				case (int) Keys.Escape:
sl@0
   338
					rcb = RemoteControlButton.Clear;
sl@0
   339
					break;
sl@0
   340
				case (int) Keys.Down:
sl@0
   341
					rcb = RemoteControlButton.Down;
sl@0
   342
					break;
sl@0
   343
				case (int) Keys.Left:
sl@0
   344
					rcb = RemoteControlButton.Left;
sl@0
   345
					break;
sl@0
   346
				case (int) Keys.D0:
sl@0
   347
					rcb = RemoteControlButton.Digit0;
sl@0
   348
					break;
sl@0
   349
				case (int) Keys.D1:
sl@0
   350
					rcb = RemoteControlButton.Digit1;
sl@0
   351
					break;
sl@0
   352
				case (int) Keys.D2:
sl@0
   353
					rcb = RemoteControlButton.Digit2;
sl@0
   354
					break;
sl@0
   355
				case (int) Keys.D3:
sl@0
   356
					rcb = RemoteControlButton.Digit3;
sl@0
   357
					break;
sl@0
   358
				case (int) Keys.D4:
sl@0
   359
					rcb = RemoteControlButton.Digit4;
sl@0
   360
					break;
sl@0
   361
				case (int) Keys.D5:
sl@0
   362
					rcb = RemoteControlButton.Digit5;
sl@0
   363
					break;
sl@0
   364
				case (int) Keys.D6:
sl@0
   365
					rcb = RemoteControlButton.Digit6;
sl@0
   366
					break;
sl@0
   367
				case (int) Keys.D7:
sl@0
   368
					rcb = RemoteControlButton.Digit7;
sl@0
   369
					break;
sl@0
   370
				case (int) Keys.D8:
sl@0
   371
					rcb = RemoteControlButton.Digit8;
sl@0
   372
					break;
sl@0
   373
				case (int) Keys.D9:
sl@0
   374
					rcb = RemoteControlButton.Digit9;
sl@0
   375
					break;
sl@0
   376
				case (int) Keys.Enter:
sl@0
   377
					rcb = RemoteControlButton.Enter;
sl@0
   378
					break;
sl@0
   379
				case (int) Keys.Right:
sl@0
   380
					rcb = RemoteControlButton.Right;
sl@0
   381
					break;
sl@0
   382
				case (int) Keys.Up:
sl@0
   383
					rcb = RemoteControlButton.Up;
sl@0
   384
					break;
sl@0
   385
			}
sl@0
   386
sl@0
   387
			if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
sl@0
   388
				this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(param)));
sl@0
   389
		}
sl@0
   390
sl@0
   391
sl@0
   392
		private void ProcessAppCommand(int param)
sl@0
   393
		{
sl@0
   394
			RemoteControlButton rcb = RemoteControlButton.Unknown;
sl@0
   395
sl@0
   396
			int cmd	= (int) (((ushort) (param >> 16)) & ~FAPPCOMMAND_MASK);
sl@0
   397
sl@0
   398
			switch (cmd)
sl@0
   399
			{
sl@0
   400
				case APPCOMMAND_BROWSER_BACKWARD:
sl@0
   401
					rcb = RemoteControlButton.Back;
sl@0
   402
					break;
sl@0
   403
				case APPCOMMAND_MEDIA_CHANNEL_DOWN:
sl@0
   404
					rcb = RemoteControlButton.ChannelDown;
sl@0
   405
					break;
sl@0
   406
				case APPCOMMAND_MEDIA_CHANNEL_UP:
sl@0
   407
					rcb = RemoteControlButton.ChannelUp;
sl@0
   408
					break;
sl@0
   409
				case APPCOMMAND_MEDIA_FAST_FORWARD:
sl@0
   410
					rcb = RemoteControlButton.FastForward;
sl@0
   411
					break;
sl@0
   412
				case APPCOMMAND_VOLUME_MUTE:
sl@0
   413
					rcb = RemoteControlButton.VolumeMute;
sl@0
   414
					break;
sl@0
   415
				case APPCOMMAND_MEDIA_PAUSE:
sl@0
   416
					rcb = RemoteControlButton.Pause;
sl@0
   417
					break;
sl@0
   418
				case APPCOMMAND_MEDIA_PLAY:
sl@0
   419
					rcb = RemoteControlButton.Play;
sl@0
   420
					break;
sl@0
   421
                case APPCOMMAND_MEDIA_PLAY_PAUSE:
sl@0
   422
                    rcb = RemoteControlButton.PlayPause;
sl@0
   423
                    break;
sl@0
   424
				case APPCOMMAND_MEDIA_RECORD:
sl@0
   425
					rcb = RemoteControlButton.Record;
sl@0
   426
					break;
sl@0
   427
				case APPCOMMAND_MEDIA_PREVIOUSTRACK:
sl@0
   428
					rcb = RemoteControlButton.PreviousTrack;
sl@0
   429
					break;
sl@0
   430
				case APPCOMMAND_MEDIA_REWIND:
sl@0
   431
					rcb = RemoteControlButton.Rewind;
sl@0
   432
					break;
sl@0
   433
				case APPCOMMAND_MEDIA_NEXTTRACK:
sl@0
   434
					rcb = RemoteControlButton.NextTrack;
sl@0
   435
					break;
sl@0
   436
				case APPCOMMAND_MEDIA_STOP:
sl@0
   437
					rcb = RemoteControlButton.Stop;
sl@0
   438
					break;
sl@0
   439
				case APPCOMMAND_VOLUME_DOWN:
sl@0
   440
					rcb = RemoteControlButton.VolumeDown;
sl@0
   441
					break;
sl@0
   442
				case APPCOMMAND_VOLUME_UP:
sl@0
   443
					rcb = RemoteControlButton.VolumeUp;
sl@0
   444
					break;
sl@0
   445
			}
sl@0
   446
sl@0
   447
			if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
sl@0
   448
				this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(param)));
sl@0
   449
		}
sl@0
   450
sl@0
   451
sl@0
   452
		private void ProcessInputCommand(ref Message message)
sl@0
   453
		{
sl@0
   454
			RemoteControlButton rcb = RemoteControlButton.Unknown;
sl@0
   455
			uint dwSize = 0;
sl@0
   456
sl@0
   457
            uint sizeOfHeader=(uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
sl@0
   458
sl@0
   459
            //Get the size of our raw input data.
sl@0
   460
			GetRawInputData(message.LParam,	RID_INPUT, IntPtr.Zero,	ref dwSize,	sizeOfHeader);
sl@0
   461
sl@0
   462
            //Allocate a large enough buffer
sl@0
   463
			IntPtr buffer = Marshal.AllocHGlobal((int) dwSize);
sl@0
   464
			try
sl@0
   465
			{
sl@0
   466
				if(buffer == IntPtr.Zero)
sl@0
   467
					return;
sl@0
   468
sl@0
   469
                //Now read our RAWINPUT data
sl@0
   470
				if (GetRawInputData(message.LParam,	RID_INPUT, buffer, ref dwSize, (uint) Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
sl@0
   471
				{
sl@0
   472
					return;
sl@0
   473
				}
sl@0
   474
sl@0
   475
                //Cast our buffer
sl@0
   476
                RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT));
sl@0
   477
sl@0
   478
                //Check that our raw input is HID
sl@0
   479
                if (raw.header.dwType == RIM_TYPEHID && raw.hid.dwSizeHid>0)
sl@0
   480
				{
sl@0
   481
                    //Allocate a buffer for one HID message
sl@0
   482
					byte[] bRawData = new byte[raw.hid.dwSizeHid];
sl@0
   483
sl@0
   484
                    //Compute the address from which to copy our HID message
sl@0
   485
                    int pRawData = 0;
sl@0
   486
                    unsafe
sl@0
   487
                    {
sl@0
   488
                        byte* source = (byte*)buffer;
sl@0
   489
                        source += sizeof(RAWINPUTHEADER) + sizeof(RAWHID);
sl@0
   490
                        //source += 1;
sl@0
   491
                        pRawData = (int)source;
sl@0
   492
                    }
sl@0
   493
                    //int pRawData = buffer.ToUint32() + Marshal.SizeOf(typeof(RAWINPUT)) + 1;
sl@0
   494
sl@0
   495
					//Marshal.Copy(new IntPtr(pRawData), bRawData, 0, raw.hid.dwSizeHid - 1);
sl@0
   496
                    Marshal.Copy(new IntPtr(pRawData), bRawData, 0, raw.hid.dwSizeHid);
sl@0
   497
					//int rawData = bRawData[0] | bRawData[1] << 8;
sl@0
   498
                    int rawData = bRawData[1]; //Get button code
sl@2
   499
                    Debug.WriteLine("HID " + raw.hid.dwCount + "/" + raw.hid.dwSizeHid + ":" + bRawData[0].ToString("X2") + bRawData[1].ToString("X2"));
sl@0
   500
sl@0
   501
					switch (rawData)
sl@0
   502
					{
sl@0
   503
						case RAWINPUT_DETAILS:
sl@0
   504
							rcb = RemoteControlButton.Details;
sl@0
   505
							break;
sl@0
   506
						case RAWINPUT_GUIDE:
sl@0
   507
							rcb = RemoteControlButton.Guide;
sl@0
   508
							break;
sl@0
   509
						case RAWINPUT_TVJUMP:
sl@0
   510
							rcb = RemoteControlButton.TVJump;
sl@0
   511
							break;
sl@0
   512
						case RAWINPUT_STANDBY:
sl@0
   513
							rcb = RemoteControlButton.StandBy;
sl@0
   514
							break;
sl@0
   515
						case RAWINPUT_OEM1:
sl@0
   516
							rcb = RemoteControlButton.OEM1;
sl@0
   517
							break;
sl@0
   518
						case RAWINPUT_OEM2:
sl@0
   519
							rcb = RemoteControlButton.OEM2;
sl@0
   520
							break;
sl@0
   521
						case RAWINPUT_MYTV:
sl@0
   522
							rcb = RemoteControlButton.MyTV;
sl@0
   523
							break;
sl@0
   524
						case RAWINPUT_MYVIDEOS:
sl@0
   525
							rcb = RemoteControlButton.MyVideos;
sl@0
   526
							break;
sl@0
   527
						case RAWINPUT_MYPICTURES:
sl@0
   528
							rcb = RemoteControlButton.MyPictures;
sl@0
   529
							break;
sl@0
   530
						case RAWINPUT_MYMUSIC:
sl@0
   531
							rcb = RemoteControlButton.MyMusic;
sl@0
   532
							break;
sl@0
   533
						case RAWINPUT_RECORDEDTV:
sl@0
   534
							rcb = RemoteControlButton.RecordedTV;
sl@0
   535
							break;
sl@0
   536
						case RAWINPUT_DVDANGLE:
sl@0
   537
							rcb = RemoteControlButton.DVDAngle;
sl@0
   538
							break;
sl@0
   539
						case RAWINPUT_DVDAUDIO:
sl@0
   540
							rcb = RemoteControlButton.DVDAudio;
sl@0
   541
							break;
sl@0
   542
						case RAWINPUT_DVDMENU:
sl@0
   543
							rcb = RemoteControlButton.DVDMenu;
sl@0
   544
							break;
sl@0
   545
						case RAWINPUT_DVDSUBTITLE:
sl@0
   546
							rcb = RemoteControlButton.DVDSubtitle;
sl@0
   547
							break;
sl@0
   548
					}
sl@0
   549
sl@0
   550
					if (rcb != RemoteControlButton.Unknown && this.ButtonPressed != null)
sl@0
   551
						this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(message.LParam.ToInt32())));
sl@0
   552
				}
sl@0
   553
				else if(raw.header.dwType == RIM_TYPEMOUSE)
sl@0
   554
				{
sl@0
   555
					// do mouse handling...
sl@0
   556
				}
sl@0
   557
				else if(raw.header.dwType == RIM_TYPEKEYBOARD)
sl@0
   558
				{
sl@0
   559
					// do keyboard handling...
sl@0
   560
				}
sl@0
   561
			}
sl@0
   562
			finally
sl@0
   563
			{
sl@0
   564
				Marshal.FreeHGlobal(buffer);
sl@0
   565
			}
sl@0
   566
		}
sl@0
   567
sl@0
   568
sl@0
   569
		private InputDevice GetDevice(int param)
sl@0
   570
		{
sl@0
   571
			InputDevice inputDevice;
sl@0
   572
sl@0
   573
			switch ((int) (((ushort) (param >> 16)) & FAPPCOMMAND_MASK))
sl@0
   574
			{
sl@0
   575
				case FAPPCOMMAND_OEM:
sl@0
   576
					inputDevice = InputDevice.OEM;
sl@0
   577
					break;
sl@0
   578
				case FAPPCOMMAND_MOUSE:
sl@0
   579
					inputDevice = InputDevice.Mouse;
sl@0
   580
					break;
sl@0
   581
				default:
sl@0
   582
					inputDevice = InputDevice.Key;
sl@0
   583
					break;
sl@0
   584
			}
sl@0
   585
sl@0
   586
			return inputDevice;
sl@0
   587
		}
sl@0
   588
	}
sl@0
   589
}