RemoteControlDevice.cs
changeset 9 94850bfc12b5
parent 8 0d0c62b1df48
child 10 17f8421146ba
     1.1 --- a/RemoteControlDevice.cs	Thu Nov 06 21:57:53 2014 +0100
     1.2 +++ b/RemoteControlDevice.cs	Fri Nov 07 20:49:51 2014 +0100
     1.3 @@ -3,13 +3,11 @@
     1.4  using System.Runtime.InteropServices;
     1.5  using System.Diagnostics;
     1.6  using Hid.UsageTables;
     1.7 +using Win32;
     1.8  
     1.9  namespace Devices.RemoteControl
    1.10  {
    1.11  
    1.12 - 
    1.13 -
    1.14 -
    1.15  	public enum InputDevice
    1.16  	{
    1.17  		Key,
    1.18 @@ -127,159 +125,6 @@
    1.19  
    1.20  	public sealed class RemoteControlDevice
    1.21  	{
    1.22 -
    1.23 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
    1.24 -		internal struct RAWINPUTDEVICE
    1.25 -		{
    1.26 -			[MarshalAs(UnmanagedType.U2)]
    1.27 -			public ushort usUsagePage;
    1.28 -			[MarshalAs(UnmanagedType.U2)]
    1.29 -			public ushort usUsage;
    1.30 -			[MarshalAs(UnmanagedType.U4)]
    1.31 -			public int	 dwFlags;
    1.32 -			public IntPtr hwndTarget;
    1.33 -		}
    1.34 -
    1.35 -
    1.36 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
    1.37 -		internal struct RAWINPUTHEADER
    1.38 -		{
    1.39 -			[MarshalAs(UnmanagedType.U4)]
    1.40 -			public int dwType;
    1.41 -			[MarshalAs(UnmanagedType.U4)]
    1.42 -			public int dwSize;
    1.43 -            public IntPtr hDevice;
    1.44 -			[MarshalAs(UnmanagedType.U4)]
    1.45 -			public int wParam;
    1.46 -		}
    1.47 -
    1.48 -
    1.49 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
    1.50 -		internal struct RAWHID
    1.51 -		{
    1.52 -			[MarshalAs(UnmanagedType.U4)]
    1.53 -			public int dwSizeHid;
    1.54 -			[MarshalAs(UnmanagedType.U4)]
    1.55 -			public int dwCount;
    1.56 -            //
    1.57 -            //BYTE  bRawData[1];
    1.58 -		}
    1.59 -
    1.60 -
    1.61 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
    1.62 -		internal struct BUTTONSSTR
    1.63 -		{
    1.64 -			[MarshalAs(UnmanagedType.U2)]
    1.65 -			public ushort usButtonFlags;
    1.66 -			[MarshalAs(UnmanagedType.U2)]
    1.67 -			public ushort usButtonData;
    1.68 -		}
    1.69 -
    1.70 -
    1.71 -        [StructLayout(LayoutKind.Explicit, Pack = 1)]
    1.72 -		internal struct RAWMOUSE
    1.73 -		{
    1.74 -			[MarshalAs(UnmanagedType.U2)]
    1.75 -			[FieldOffset (0)] public ushort usFlags;
    1.76 -			[MarshalAs(UnmanagedType.U4)]
    1.77 -			[FieldOffset (4)] public uint ulButtons;
    1.78 -			[FieldOffset (4)] public BUTTONSSTR buttonsStr;
    1.79 -			[MarshalAs(UnmanagedType.U4)]
    1.80 -			[FieldOffset (8)] public uint ulRawButtons;
    1.81 -            [MarshalAs(UnmanagedType.U4)]
    1.82 -            [FieldOffset (12)] public int lLastX;
    1.83 -            [MarshalAs(UnmanagedType.U4)]
    1.84 -            [FieldOffset (16)] public int lLastY;
    1.85 -			[MarshalAs(UnmanagedType.U4)]
    1.86 -			[FieldOffset (20)] public uint ulExtraInformation;
    1.87 -		}
    1.88 -
    1.89 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
    1.90 -		internal struct RAWKEYBOARD
    1.91 -		{
    1.92 -			[MarshalAs(UnmanagedType.U2)]
    1.93 -			public ushort MakeCode;
    1.94 -			[MarshalAs(UnmanagedType.U2)]
    1.95 -			public ushort Flags;
    1.96 -			[MarshalAs(UnmanagedType.U2)]
    1.97 -			public ushort Reserved;
    1.98 -			[MarshalAs(UnmanagedType.U2)]
    1.99 -			public ushort VKey;
   1.100 -			[MarshalAs(UnmanagedType.U4)]
   1.101 -			public uint Message;
   1.102 -			[MarshalAs(UnmanagedType.U4)]
   1.103 -			public uint ExtraInformation;
   1.104 -		}
   1.105 -
   1.106 -
   1.107 -		[StructLayout(LayoutKind.Explicit, Pack=1)]
   1.108 -		internal struct RAWINPUT
   1.109 -		{
   1.110 -			[FieldOffset  (0)] public RAWINPUTHEADER header;
   1.111 -			[FieldOffset (16)] public RAWMOUSE mouse;
   1.112 -			[FieldOffset (16)] public RAWKEYBOARD keyboard;
   1.113 -			[FieldOffset (16)] public RAWHID hid;
   1.114 -		}
   1.115 -
   1.116 -
   1.117 -        [StructLayout(LayoutKind.Sequential, Pack=1)]
   1.118 -        internal struct RID_DEVICE_INFO_MOUSE
   1.119 -		{
   1.120 -            public uint dwId;
   1.121 -            public uint dwNumberOfButtons;
   1.122 -            public uint dwSampleRate;
   1.123 -            public bool fHasHorizontalWheel;
   1.124 -		}
   1.125 -
   1.126 -
   1.127 -        [StructLayout(LayoutKind.Sequential, Pack=1)]
   1.128 -        internal struct RID_DEVICE_INFO_KEYBOARD
   1.129 -		{
   1.130 -            public uint dwType;
   1.131 -            public uint dwSubType;
   1.132 -            public uint dwKeyboardMode;
   1.133 -            public uint dwNumberOfFunctionKeys;
   1.134 -            public uint dwNumberOfIndicators;
   1.135 -            public uint dwNumberOfKeysTotal;
   1.136 -        }
   1.137 -
   1.138 -        [StructLayout(LayoutKind.Sequential, Pack=1)]
   1.139 -        internal struct RID_DEVICE_INFO_HID
   1.140 -		{
   1.141 -            public uint dwVendorId;
   1.142 -            public uint dwProductId;
   1.143 -            public uint dwVersionNumber;
   1.144 -            public ushort usUsagePage;
   1.145 -            public ushort usUsage;
   1.146 -        }
   1.147 -
   1.148 -        [StructLayout(LayoutKind.Explicit, Pack=1)]
   1.149 -        internal struct RID_DEVICE_INFO
   1.150 -		{
   1.151 -            [FieldOffset(0)]
   1.152 -            public uint cbSize;
   1.153 -            [FieldOffset(4)]
   1.154 -            public uint dwType;
   1.155 -            [FieldOffset(8)]
   1.156 -            public RID_DEVICE_INFO_MOUSE mouse;
   1.157 -            [FieldOffset(8)]
   1.158 -            public RID_DEVICE_INFO_KEYBOARD keyboard;
   1.159 -            [FieldOffset(8)]
   1.160 -            public RID_DEVICE_INFO_HID hid;
   1.161 -		}
   1.162 -
   1.163 -
   1.164 -
   1.165 -		[DllImport("User32.dll")]
   1.166 -		extern static bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint uiNumDevices, uint cbSize);
   1.167 -
   1.168 -		[DllImport("User32.dll")]
   1.169 -		extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
   1.170 -
   1.171 -   		[DllImport("User32.dll", SetLastError=true)]
   1.172 -		extern static int GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
   1.173 -
   1.174 -
   1.175  		private const int WM_KEYDOWN	= 0x0100;
   1.176  		private const int WM_APPCOMMAND	= 0x0319;
   1.177  		private const int WM_INPUT		= 0x00FF;
   1.178 @@ -308,35 +153,6 @@
   1.179  		private const int FAPPCOMMAND_KEY				= 0;
   1.180  		private const int FAPPCOMMAND_OEM				= 0x1000;
   1.181  
   1.182 -        /// <summary>
   1.183 -        /// GetRawInputDeviceInfo pData points to a string that contains the device name.
   1.184 -        /// </summary>
   1.185 -        public const uint RIDI_DEVICENAME = 0x20000007;
   1.186 -        /// <summary>
   1.187 -        /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
   1.188 -        /// </summary>
   1.189 -        public const uint RIDI_DEVICEINFO = 0x2000000b;
   1.190 -        /// <summary>
   1.191 -        /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
   1.192 -        /// </summary>
   1.193 -        public const uint RIDI_PREPARSEDDATA = 0x20000005;
   1.194 -
   1.195 -
   1.196 -        /// <summary>
   1.197 -        /// Data comes from a mouse.
   1.198 -        /// </summary>
   1.199 -        public const uint RIM_TYPEMOUSE = 0;
   1.200 -        /// <summary>
   1.201 -        /// Data comes from a keyboard.
   1.202 -        /// </summary>
   1.203 -        public const uint RIM_TYPEKEYBOARD = 1;
   1.204 -        /// <summary>
   1.205 -        /// Data comes from an HID that is not a keyboard or a mouse.
   1.206 -        /// </summary>
   1.207 -        public const uint RIM_TYPEHID = 2;
   1.208 -
   1.209 -
   1.210 -
   1.211  
   1.212  
   1.213  		public delegate void RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
   1.214 @@ -367,7 +183,7 @@
   1.215  			rid[2].usUsage = 0x80;
   1.216  			rid[2].dwFlags = 0;
   1.217  
   1.218 -			if (!RegisterRawInputDevices(rid,
   1.219 +			if (!Function.RegisterRawInputDevices(rid,
   1.220  				(uint) rid.Length,
   1.221  				(uint) Marshal.SizeOf(rid[0]))
   1.222  				)
   1.223 @@ -538,7 +354,7 @@
   1.224              uint sizeOfHeader=(uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
   1.225  
   1.226              //Get the size of our raw input data.
   1.227 -			GetRawInputData(message.LParam,	RID_INPUT, IntPtr.Zero,	ref dwSize,	sizeOfHeader);
   1.228 +			Win32.Function.GetRawInputData(message.LParam,	RID_INPUT, IntPtr.Zero,	ref dwSize,	sizeOfHeader);
   1.229  
   1.230              //Allocate a large enough buffer
   1.231  			IntPtr rawInputBuffer = Marshal.AllocHGlobal((int) dwSize);
   1.232 @@ -548,7 +364,7 @@
   1.233  					return;
   1.234  
   1.235                  //Now read our RAWINPUT data
   1.236 -				if (GetRawInputData(message.LParam,	RID_INPUT, rawInputBuffer, ref dwSize, (uint) Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
   1.237 +                if (Win32.Function.GetRawInputData(message.LParam, RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
   1.238  				{
   1.239  					return;
   1.240  				}
   1.241 @@ -560,7 +376,7 @@
   1.242                  uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
   1.243                  IntPtr deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
   1.244  
   1.245 -                int res = GetRawInputDeviceInfo(raw.header.hDevice, RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
   1.246 +                int res = Win32.Function.GetRawInputDeviceInfo(raw.header.hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
   1.247                  if (res <= 0)
   1.248                  {
   1.249                      Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
   1.250 @@ -573,13 +389,13 @@
   1.251                  //Check type of input device and quite if we don't like it
   1.252                  switch (deviceInfo.dwType)
   1.253                  {
   1.254 -                    case RIM_TYPEHID:
   1.255 +                    case Const.RIM_TYPEHID:
   1.256                          Debug.WriteLine("WM_INPUT source device is HID.");
   1.257                          break;
   1.258 -                    case RIM_TYPEMOUSE:
   1.259 +                    case Const.RIM_TYPEMOUSE:
   1.260                          Debug.WriteLine("WM_INPUT source device is Mouse.");
   1.261                          return;
   1.262 -                    case RIM_TYPEKEYBOARD:
   1.263 +                    case Const.RIM_TYPEKEYBOARD:
   1.264                          Debug.WriteLine("WM_INPUT source device is Keyboard.");
   1.265                          return;
   1.266                      default:
   1.267 @@ -591,7 +407,7 @@
   1.268                  Debug.WriteLine("Usage Page: 0x" + deviceInfo.hid.usUsagePage.ToString("X4") + " Usage: 0x" + deviceInfo.hid.usUsage.ToString("X4"));
   1.269  
   1.270                  //Check that our raw input is HID
   1.271 -                if (raw.header.dwType == RIM_TYPEHID && raw.hid.dwSizeHid>0)
   1.272 +                if (raw.header.dwType == Const.RIM_TYPEHID && raw.hid.dwSizeHid>0)
   1.273  				{
   1.274                      //Allocate a buffer for one HID message
   1.275  					byte[] bRawData = new byte[raw.hid.dwSizeHid];
   1.276 @@ -633,11 +449,11 @@
   1.277                          }
   1.278                      }
   1.279  				}
   1.280 -				else if(raw.header.dwType == RIM_TYPEMOUSE)
   1.281 +				else if(raw.header.dwType == Const.RIM_TYPEMOUSE)
   1.282  				{
   1.283  					// do mouse handling...
   1.284  				}
   1.285 -				else if(raw.header.dwType == RIM_TYPEKEYBOARD)
   1.286 +				else if(raw.header.dwType == Const.RIM_TYPEKEYBOARD)
   1.287  				{
   1.288  					// do keyboard handling...
   1.289  				}