MainForm.cs
author StephaneLenclud
Sun, 15 Mar 2015 21:26:51 +0100
changeset 82 312160defeac
parent 81 baabcd5cdf8c
child 83 2d5955694057
permissions -rw-r--r--
Publishing on NuGet.
     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             RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5];
    73 
    74             int i = 0;
    75             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
    76             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
    77             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    78             rid[i].hwndTarget = Handle;
    79 
    80             i++;
    81             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
    82             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
    83             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    84             rid[i].hwndTarget = Handle;
    85 
    86             i++;
    87             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
    88             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
    89             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    90             rid[i].hwndTarget = Handle;
    91 
    92             i++;
    93             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
    94             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
    95             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    96             rid[i].hwndTarget = Handle;
    97 
    98             i++;
    99             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   100             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
   101             rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   102             rid[i].hwndTarget = Handle;
   103 
   104             //i++;
   105             //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   106             //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
   107             //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   108             //rid[i].hwndTarget = Handle;
   109 
   110             //i++;
   111             //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
   112             //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
   113             //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   114             //rid[i].hwndTarget = aHWND;
   115 
   116 
   117             iHidHandler = new SharpLib.Hid.Handler(rid);
   118             if (!iHidHandler.IsRegistered)
   119             {
   120                 Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
   121             }
   122             iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
   123 	    }
   124 
   125         public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
   126         {
   127             if (aHidEvent.IsStray)
   128             {
   129                 //Stray event just ignore it
   130                 return;
   131             }
   132 
   133             if (this.InvokeRequired)
   134             {
   135                 //Not in the proper thread, invoke ourselves
   136                 OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
   137                 this.Invoke(d, new object[] { aSender, aHidEvent });
   138             }
   139             else
   140             {
   141                 //We are in the proper thread
   142                 listViewEvents.Items.Insert(0, aHidEvent.ToListViewItem());
   143                 toolStripStatusLabelDevice.Text = aHidEvent.Device.FriendlyName;
   144             }
   145         }
   146 
   147 		protected override void WndProc(ref Message message)
   148 		{
   149             switch (message.Msg)
   150             {
   151                 case Const.WM_KEYDOWN:
   152                     //ProcessKeyDown(message.WParam);
   153                     break;
   154                 case Const.WM_INPUT:
   155                     //Returning zero means we processed that message.
   156                     message.Result = new IntPtr(0);
   157                     iHidHandler.ProcessInput(ref message);
   158                     break;
   159             }
   160             //Is that needed? Check the docs.
   161 			base.WndProc(ref message);
   162 		}
   163 
   164 		private void buttonClear_Click(object sender, EventArgs e)
   165 		{
   166 			listViewEvents.Items.Clear();
   167 		}
   168 
   169         private void buttonTreeViewCollapseAll_Click(object sender, EventArgs e)
   170         {
   171             treeViewDevices.CollapseAll();            
   172         }
   173 
   174         private void buttonTreeViewExpandAll_Click(object sender, EventArgs e)
   175         {
   176             treeViewDevices.ExpandAll();
   177         }
   178 
   179         private void buttonRefresh_Click(object sender, EventArgs e)
   180         {
   181             treeViewDevices.Nodes.Clear();
   182             SharpLib.Win32.RawInput.PopulateDeviceList(treeViewDevices);
   183         }
   184 
   185 	}
   186 }