RemoteControlDevice.cs
changeset 15 2044181ade12
parent 14 0d9f479cbbf7
child 16 9a3e77655031
     1.1 --- a/RemoteControlDevice.cs	Mon Nov 24 16:46:26 2014 +0100
     1.2 +++ b/RemoteControlDevice.cs	Mon Nov 24 18:20:45 2014 +0100
     1.3 @@ -126,45 +126,17 @@
     1.4  
     1.5  	public sealed class RemoteControlDevice
     1.6  	{
     1.7 -		private const int WM_KEYDOWN	= 0x0100;
     1.8 -		private const int WM_APPCOMMAND	= 0x0319;
     1.9 -		private const int WM_INPUT		= 0x00FF;
    1.10 -
    1.11 -		private const int APPCOMMAND_BROWSER_BACKWARD   = 1;
    1.12 -		private const int APPCOMMAND_VOLUME_MUTE        = 8;
    1.13 -		private const int APPCOMMAND_VOLUME_DOWN        = 9;
    1.14 -		private const int APPCOMMAND_VOLUME_UP          = 10;
    1.15 -		private const int APPCOMMAND_MEDIA_NEXTTRACK    = 11;
    1.16 -		private const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12;
    1.17 -		private const int APPCOMMAND_MEDIA_STOP         = 13;
    1.18 -		private const int APPCOMMAND_MEDIA_PLAY_PAUSE   = 14;
    1.19 -		private const int APPCOMMAND_MEDIA_PLAY         = 46;
    1.20 -		private const int APPCOMMAND_MEDIA_PAUSE        = 47;
    1.21 -		private const int APPCOMMAND_MEDIA_RECORD       = 48;
    1.22 -		private const int APPCOMMAND_MEDIA_FAST_FORWARD = 49;
    1.23 -		private const int APPCOMMAND_MEDIA_REWIND       = 50;
    1.24 -		private const int APPCOMMAND_MEDIA_CHANNEL_UP   = 51;
    1.25 -		private const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52;
    1.26 -
    1.27 -		private const int FAPPCOMMAND_MASK				= 0xF000;
    1.28 -		private const int FAPPCOMMAND_MOUSE				= 0x8000;
    1.29 -		private const int FAPPCOMMAND_KEY				= 0;
    1.30 -		private const int FAPPCOMMAND_OEM				= 0x1000;
    1.31 -
    1.32 -
    1.33 -
    1.34  		public delegate void RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
    1.35  		public event RemoteControlDeviceEventHandler ButtonPressed;
    1.36  
    1.37          public delegate void HidUsageHandler(ushort aUsage);
    1.38          
    1.39  
    1.40 -
    1.41  		//-------------------------------------------------------------
    1.42  		// constructors
    1.43  		//-------------------------------------------------------------
    1.44  
    1.45 -		public RemoteControlDevice()
    1.46 +		public RemoteControlDevice(IntPtr aHWND)
    1.47  		{
    1.48  			// Register the input device to receive the commands from the
    1.49  			// remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
    1.50 @@ -174,22 +146,22 @@
    1.51  
    1.52  			rid[0].usUsagePage = 0xFFBC;
    1.53  			rid[0].usUsage = 0x88;
    1.54 -			rid[0].dwFlags = 0;
    1.55 +            rid[0].dwFlags = Const.RIDEV_EXINPUTSINK;
    1.56 +            rid[0].hwndTarget = aHWND;
    1.57  
    1.58  			rid[1].usUsagePage = 0x0C;
    1.59  			rid[1].usUsage = 0x01;
    1.60 -			rid[1].dwFlags = 0;
    1.61 +            rid[1].dwFlags = Const.RIDEV_EXINPUTSINK;
    1.62 +            rid[1].hwndTarget = aHWND;
    1.63  
    1.64  			rid[2].usUsagePage = 0x0C;
    1.65  			rid[2].usUsage = 0x80;
    1.66 -			rid[2].dwFlags = 0;
    1.67 +            rid[2].dwFlags = Const.RIDEV_EXINPUTSINK;
    1.68 +            rid[2].hwndTarget = aHWND;
    1.69  
    1.70 -			if (!Function.RegisterRawInputDevices(rid,
    1.71 -				(uint) rid.Length,
    1.72 -				(uint) Marshal.SizeOf(rid[0]))
    1.73 -				)
    1.74 +			if (!Function.RegisterRawInputDevices(rid,(uint) rid.Length,(uint) Marshal.SizeOf(rid[0])))
    1.75  			{
    1.76 -				throw new ApplicationException("Failed to register raw input devices.");
    1.77 +                throw new ApplicationException("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
    1.78  			}
    1.79  		}
    1.80  
    1.81 @@ -200,19 +172,15 @@
    1.82  
    1.83  		public void ProcessMessage(Message message)
    1.84  		{
    1.85 -		int param;
    1.86 -
    1.87  			switch (message.Msg)
    1.88  			{
    1.89 -				case WM_KEYDOWN:
    1.90 -					param = message.WParam.ToInt32();
    1.91 -					ProcessKeyDown(param);
    1.92 +				case Const.WM_KEYDOWN:
    1.93 +                    ProcessKeyDown(message.WParam);
    1.94  					break;
    1.95 -				case WM_APPCOMMAND:
    1.96 -					param = message.LParam.ToInt32();
    1.97 -					ProcessAppCommand(param);
    1.98 +                case Const.WM_APPCOMMAND:
    1.99 +					ProcessAppCommand(message.LParam);
   1.100  					break;
   1.101 -				case WM_INPUT:
   1.102 +                case Const.WM_INPUT:
   1.103  					ProcessInputCommand(ref message);
   1.104                      message.Result = new IntPtr(0);
   1.105  					break;
   1.106 @@ -225,11 +193,11 @@
   1.107  		// methods (helpers)
   1.108  		//-------------------------------------------------------------
   1.109  
   1.110 -		private void ProcessKeyDown(int param)
   1.111 +		private void ProcessKeyDown(IntPtr wParam)
   1.112  		{
   1.113  			RemoteControlButton rcb = RemoteControlButton.Unknown;
   1.114  
   1.115 -			switch (param)
   1.116 +            switch (wParam.ToInt32())
   1.117  			{
   1.118  				case (int) Keys.Escape:
   1.119  					rcb = RemoteControlButton.Clear;
   1.120 @@ -282,67 +250,68 @@
   1.121  			}
   1.122  
   1.123  			if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
   1.124 -				this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(param)));
   1.125 +				this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(wParam)));
   1.126  		}
   1.127  
   1.128  
   1.129 -		private void ProcessAppCommand(int param)
   1.130 +		private void ProcessAppCommand(IntPtr lParam)
   1.131  		{
   1.132  			RemoteControlButton rcb = RemoteControlButton.Unknown;
   1.133  
   1.134 -			int cmd	= (int) (((ushort) (param >> 16)) & ~FAPPCOMMAND_MASK);
   1.135 +            int cmd = Macro.GET_APPCOMMAND_LPARAM(lParam);
   1.136 +                //(int) (((ushort) (param >> 16)) & ~Const.FAPPCOMMAND_MASK);
   1.137  
   1.138  			switch (cmd)
   1.139  			{
   1.140 -				case APPCOMMAND_BROWSER_BACKWARD:
   1.141 +                case Const.APPCOMMAND_BROWSER_BACKWARD:
   1.142  					rcb = RemoteControlButton.Back;
   1.143  					break;
   1.144 -				case APPCOMMAND_MEDIA_CHANNEL_DOWN:
   1.145 +                case Const.APPCOMMAND_MEDIA_CHANNEL_DOWN:
   1.146  					rcb = RemoteControlButton.ChannelDown;
   1.147  					break;
   1.148 -				case APPCOMMAND_MEDIA_CHANNEL_UP:
   1.149 +                case Const.APPCOMMAND_MEDIA_CHANNEL_UP:
   1.150  					rcb = RemoteControlButton.ChannelUp;
   1.151  					break;
   1.152 -				case APPCOMMAND_MEDIA_FAST_FORWARD:
   1.153 +                case Const.APPCOMMAND_MEDIA_FAST_FORWARD:
   1.154  					rcb = RemoteControlButton.FastForward;
   1.155  					break;
   1.156 -				case APPCOMMAND_VOLUME_MUTE:
   1.157 +                case Const.APPCOMMAND_VOLUME_MUTE:
   1.158  					rcb = RemoteControlButton.VolumeMute;
   1.159  					break;
   1.160 -				case APPCOMMAND_MEDIA_PAUSE:
   1.161 +                case Const.APPCOMMAND_MEDIA_PAUSE:
   1.162  					rcb = RemoteControlButton.Pause;
   1.163  					break;
   1.164 -				case APPCOMMAND_MEDIA_PLAY:
   1.165 +                case Const.APPCOMMAND_MEDIA_PLAY:
   1.166  					rcb = RemoteControlButton.Play;
   1.167  					break;
   1.168 -                case APPCOMMAND_MEDIA_PLAY_PAUSE:
   1.169 +                case Const.APPCOMMAND_MEDIA_PLAY_PAUSE:
   1.170                      rcb = RemoteControlButton.PlayPause;
   1.171                      break;
   1.172 -				case APPCOMMAND_MEDIA_RECORD:
   1.173 +                case Const.APPCOMMAND_MEDIA_RECORD:
   1.174  					rcb = RemoteControlButton.Record;
   1.175  					break;
   1.176 -				case APPCOMMAND_MEDIA_PREVIOUSTRACK:
   1.177 +                case Const.APPCOMMAND_MEDIA_PREVIOUSTRACK:
   1.178  					rcb = RemoteControlButton.PreviousTrack;
   1.179  					break;
   1.180 -				case APPCOMMAND_MEDIA_REWIND:
   1.181 +                case Const.APPCOMMAND_MEDIA_REWIND:
   1.182  					rcb = RemoteControlButton.Rewind;
   1.183  					break;
   1.184 -				case APPCOMMAND_MEDIA_NEXTTRACK:
   1.185 +                case Const.APPCOMMAND_MEDIA_NEXTTRACK:
   1.186  					rcb = RemoteControlButton.NextTrack;
   1.187  					break;
   1.188 -				case APPCOMMAND_MEDIA_STOP:
   1.189 +                case Const.APPCOMMAND_MEDIA_STOP:
   1.190  					rcb = RemoteControlButton.Stop;
   1.191  					break;
   1.192 -				case APPCOMMAND_VOLUME_DOWN:
   1.193 +                case Const.APPCOMMAND_VOLUME_DOWN:
   1.194  					rcb = RemoteControlButton.VolumeDown;
   1.195  					break;
   1.196 -				case APPCOMMAND_VOLUME_UP:
   1.197 +                case Const.APPCOMMAND_VOLUME_UP:
   1.198  					rcb = RemoteControlButton.VolumeUp;
   1.199  					break;
   1.200  			}
   1.201  
   1.202  			if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
   1.203 -				this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(param)));
   1.204 +                this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(lParam)));
   1.205  		}
   1.206  
   1.207          /// <summary>
   1.208 @@ -413,8 +382,18 @@
   1.209  
   1.210  		private void ProcessInputCommand(ref Message message)
   1.211  		{
   1.212 +            //We received a WM_INPUT message
   1.213              Debug.WriteLine("================WM_INPUT================");
   1.214  
   1.215 +            //Check if we received this message while in background or foreground
   1.216 +            if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUT)
   1.217 +            {
   1.218 +                Debug.WriteLine("================FOREGROUND");
   1.219 +            }
   1.220 +            else if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUTSINK)
   1.221 +            {
   1.222 +                Debug.WriteLine("================BACKGROUND");
   1.223 +            }
   1.224  
   1.225              //Declare a pointer
   1.226              IntPtr rawInputBuffer = IntPtr.Zero;
   1.227 @@ -535,16 +514,16 @@
   1.228  		}
   1.229  
   1.230  
   1.231 -		private InputDevice GetDevice(int param)
   1.232 +		private InputDevice GetDevice(IntPtr lParam)
   1.233  		{
   1.234  			InputDevice inputDevice;
   1.235  
   1.236 -			switch ((int) (((ushort) (param >> 16)) & FAPPCOMMAND_MASK))
   1.237 +            switch (Macro.GET_DEVICE_LPARAM(lParam))
   1.238  			{
   1.239 -				case FAPPCOMMAND_OEM:
   1.240 +				case Const.FAPPCOMMAND_OEM:
   1.241  					inputDevice = InputDevice.OEM;
   1.242  					break;
   1.243 -				case FAPPCOMMAND_MOUSE:
   1.244 +				case Const.FAPPCOMMAND_MOUSE:
   1.245  					inputDevice = InputDevice.Mouse;
   1.246  					break;
   1.247  				default: