MainForm.cs
author StephaneLenclud
Wed, 18 Mar 2015 07:46:39 +0100
changeset 88 3429909219c8
parent 82 312160defeac
permissions -rw-r--r--
Fixing typo in NuGet package description.
     1 //
     2 // Copyright (C) 2014-2015 Stéphane Lenclud.
     3 //
     4 // This file is part of SharpLibHid.
     5 //
     6 // SharpDisplayManager is free software: you can redistribute it and/or modify
     7 // it under the terms of the GNU General Public License as published by
     8 // the Free Software Foundation, either version 3 of the License, or
     9 // (at your option) any later version.
    10 //
    11 // SharpDisplayManager is distributed in the hope that it will be useful,
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 // GNU General Public License for more details.
    15 //
    16 // You should have received a copy of the GNU General Public License
    17 // along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    18 //
    19 
    20 using System;
    21 using System.Drawing;
    22 using System.Collections;
    23 using System.ComponentModel;
    24 using System.Windows.Forms;
    25 using System.Data;
    26 using System.Diagnostics;
    27 using System.Runtime.InteropServices;
    28 using Hid = SharpLib.Hid;
    29 using SharpLib.Win32;
    30 
    31 namespace HidDemo
    32 {
    33 	/// <summary>
    34 	/// MainForm for our HID demo.
    35 	/// </summary>
    36 	public partial class MainForm : System.Windows.Forms.Form
    37 	{
    38         private Hid.Handler iHidHandler;
    39 
    40         public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent);
    41 
    42 		public MainForm()
    43 		{
    44 			// Required for Windows Form Designer support
    45 			InitializeComponent();           
    46 		}
    47 
    48 
    49 		/// <summary>
    50 		/// The main entry point for the application.
    51 		/// </summary>
    52 		[STAThread]
    53 		static void Main()
    54 		{
    55             Application.EnableVisualStyles();
    56 			Application.Run(new MainForm());
    57 		}
    58 
    59         private void MainForm_Load(object sender, System.EventArgs e)
    60         {
    61             RegisterHidDevices();
    62             //Create our list of HID devices
    63             SharpLib.Win32.RawInput.PopulateDeviceList(treeViewDevices);
    64 		}
    65 
    66 	    void RegisterHidDevices()
    67 	    {           
    68             // Register the input device to receive the commands from the
    69             // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
    70             // for the vendor defined usage page.
    71 
    72             if (iHidHandler!=null)
    73             {
    74                 //First de-register
    75                 iHidHandler.Dispose();
    76                 iHidHandler = null;
    77             }
    78 
    79 
    80             RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5];
    81 
    82             int i = 0;
    83             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
    84             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
    85             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    86             rid[i].hwndTarget = Handle;
    87 
    88             i++;
    89             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
    90             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
    91             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    92             rid[i].hwndTarget = Handle;
    93 
    94             i++;
    95             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
    96             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
    97             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    98             rid[i].hwndTarget = Handle;
    99 
   100             i++;
   101             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   102             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
   103             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   104             rid[i].hwndTarget = Handle;
   105 
   106             i++;
   107             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   108             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
   109             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   110             rid[i].hwndTarget = Handle;
   111 
   112             //i++;
   113             //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   114             //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
   115             //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   116             //rid[i].hwndTarget = Handle;
   117 
   118             //i++;
   119             //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
   120             //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
   121             //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   122             //rid[i].hwndTarget = aHWND;
   123 
   124 
   125             iHidHandler = new SharpLib.Hid.Handler(rid, checkBoxRepeat.Checked);
   126             if (!iHidHandler.IsRegistered)
   127             {
   128                 Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
   129             }
   130             iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
   131 	    }
   132 
   133         public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
   134         {
   135             if (aHidEvent.IsStray)
   136             {
   137                 //Stray event just ignore it
   138                 return;
   139             }
   140 
   141             if (this.InvokeRequired)
   142             {
   143                 //Not in the proper thread, invoke ourselves
   144                 OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
   145                 this.Invoke(d, new object[] { aSender, aHidEvent });
   146             }
   147             else
   148             {
   149                 //We are in the proper thread
   150                 listViewEvents.Items.Insert(0, aHidEvent.ToListViewItem());
   151                 toolStripStatusLabelDevice.Text = aHidEvent.Device.FriendlyName;
   152             }
   153         }
   154 
   155 		protected override void WndProc(ref Message message)
   156 		{
   157             switch (message.Msg)
   158             {
   159                 case Const.WM_KEYDOWN:
   160                     //ProcessKeyDown(message.WParam);
   161                     break;
   162                 case Const.WM_INPUT:
   163                     //Returning zero means we processed that message.
   164                     message.Result = new IntPtr(0);
   165                     iHidHandler.ProcessInput(ref message);
   166                     break;
   167             }
   168             //Is that needed? Check the docs.
   169 			base.WndProc(ref message);
   170 		}
   171 
   172 		private void buttonClear_Click(object sender, EventArgs e)
   173 		{
   174 			listViewEvents.Items.Clear();
   175 		}
   176 
   177         private void buttonTreeViewCollapseAll_Click(object sender, EventArgs e)
   178         {
   179             treeViewDevices.CollapseAll();            
   180         }
   181 
   182         private void buttonTreeViewExpandAll_Click(object sender, EventArgs e)
   183         {
   184             treeViewDevices.ExpandAll();
   185         }
   186 
   187         private void buttonRefresh_Click(object sender, EventArgs e)
   188         {
   189             treeViewDevices.Nodes.Clear();
   190             SharpLib.Win32.RawInput.PopulateDeviceList(treeViewDevices);
   191         }
   192 
   193         private void checkBoxRepeat_CheckedChanged(object sender, EventArgs e)
   194         {
   195             RegisterHidDevices();
   196         }
   197 
   198 	}
   199 }