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