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