-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathACDPropertyPage.cpp
executable file
·377 lines (305 loc) · 10.7 KB
/
ACDPropertyPage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (C) 2005 Laurent Morichetti
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// ACDPropertyPage.cpp : implementation file
//
#include "stdafx.h"
#include "ACDExt.h"
#include "ACDPropertyPage.h"
#include "ACDOptionsDlg.h"
#include "ACDUtil.h"
#include <dbt.h>
// CACDPropertyPage dialog
IMPLEMENT_DYNAMIC (CACDPropertyPage, CPropertyPage)
CACDPropertyPage::CACDPropertyPage ()
: CPropertyPage (CACDPropertyPage::IDD), m_pCurrentVirtualCP (NULL), m_hDevNotify(NULL)
{
m_psp.dwFlags |= PSP_USEICONID | PSP_USEREFPARENT;
m_psp.pszIcon = MAKEINTRESOURCE (IDI_ICON);
m_psp.pcRefParent = (UINT*) &(AfxGetModuleState ()->m_nObjectCount);
CACDVirtualCP::FindKnownVirtualCPs (m_VirtualControlPanels);
}
CACDPropertyPage::~CACDPropertyPage ()
{
for (INT_PTR i = 0; i < m_VirtualControlPanels.GetCount (); ++i)
delete m_VirtualControlPanels.ElementAt (i);
}
void
CACDPropertyPage::PostNcDestroy ()
{
CPropertyPage::PostNcDestroy ();
UnregisterDeviceNotification (m_hDevNotify);
delete this;
}
void
CACDPropertyPage::UpdateBrightnessEdit ()
{
// get the slider position.
int pos = m_cBrightnessSlider.GetPos ();
// set the spinner text.
char buffer[4];
_snprintf_s(buffer, sizeof (buffer), "%d", pos * 100 / 255);
buffer [sizeof (buffer) - 1] = '\0';
m_cBrightnessEdit.SetWindowText (buffer);
}
void
CACDPropertyPage::UpdateControls() {
CACDHidDevice& Device = m_pCurrentVirtualCP->GetDevice ();
USES_CONVERSION;
char buffer [128];
// set the slider position.
int pos = m_pCurrentVirtualCP->GetBrightness ();
m_cBrightnessSlider.SetPos (pos);
// set the spinner text.
UpdateBrightnessEdit ();
// set the device description.
if (!Device.GetProductString (buffer, sizeof (buffer)))
GetDlgItem (IDC_DESCRIPTION)->SetWindowText ("(Not Available)");
else
GetDlgItem (IDC_DESCRIPTION)->SetWindowText (W2T ((LPCWSTR)buffer));
// set the firmware revision.
int nVersion = Device.GetDeviceVersionNumber();
_snprintf_s(buffer, sizeof (buffer), "%x", nVersion);
buffer [sizeof (buffer) - 1] = '\0';
GetDlgItem (IDC_FIRMWARE_VERSION)->SetWindowText (buffer);
// set the manufacturing date.
EDID_STRUCT edid;
Device.GetEDID (&edid);
_snprintf_s(buffer, sizeof (buffer), "%d, ISO week %d",
edid.ProductIdentification.bManufacturingYear + 1990,
edid.ProductIdentification.bManufacturingWeek);
buffer [sizeof (buffer) - 1] = '\0';
GetDlgItem (IDC_MANUFACTURING_DATE)->SetWindowText (buffer);
}
void
CACDPropertyPage::InitializeControls() {
BOOL bShow = m_VirtualControlPanels.GetCount () != 0;
for (CWnd* child = GetWindow(GW_CHILD); child != NULL; child = child->GetNextWindow())
child->ShowWindow(bShow);
switch (m_VirtualControlPanels.GetCount ()) {
case 0:
// no ACD found
GetDlgItem (IDC_VIRTUAL_CONTROL)->SetWindowText ("None present");
GetDlgItem (IDC_STATIC_VCP)->ShowWindow (SW_SHOW);
GetDlgItem (IDC_STATIC_VCP_ICON)->ShowWindow (SW_SHOW);
GetDlgItem (IDC_VIRTUAL_CONTROL)->ShowWindow (SW_SHOW);
return;
case 1:
// one ACD connected
m_pCurrentVirtualCP = m_VirtualControlPanels.ElementAt (0);
m_cVirtualControlList.ShowWindow (SW_HIDE);
GetDlgItem (IDC_VIRTUAL_CONTROL)->SetWindowText (
m_pCurrentVirtualCP->GetDeviceName ());
break;
default:
// multiple ACDs connected
for (INT_PTR i = 0; i < m_VirtualControlPanels.GetCount (); ++i) {
CACDVirtualCP* pVCP = m_VirtualControlPanels.ElementAt (i);
int pos = m_cVirtualControlList.AddString (pVCP->GetDeviceName ());
if (pos >= 0)
m_cVirtualControlList.SetItemDataPtr (pos, pVCP);
}
// select the first item in the list.
m_pCurrentVirtualCP = (CACDVirtualCP*)
m_cVirtualControlList.GetItemDataPtr (0);
m_cVirtualControlList.SetCurSel (0);
GetDlgItem (IDC_VIRTUAL_CONTROL)->ShowWindow (SW_HIDE);
break;
}
UpdateControls ();
}
BOOL
CACDPropertyPage::OnInitDialog() {
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
CPropertyPage::OnInitDialog();
// register the HID device notification.
ZeroMemory (&NotificationFilter, sizeof (NotificationFilter));
NotificationFilter.dbcc_size = sizeof (DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
HidD_GetHidGuid (&NotificationFilter.dbcc_classguid);
m_hDevNotify = RegisterDeviceNotification (
m_hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
// initialize the brightness slider range.
m_cBrightnessSlider.SetRange (0, 255);
// we only need to type 0-100, limit edit text to 3 chars.
m_cBrightnessEdit.SetLimitText (3);
InitializeControls ();
if (m_VirtualControlPanels.GetCount () > 0)
SetTimer (ACD_SETTINGS_TIMER, 500, 0);
return TRUE;
}
BOOL
CACDPropertyPage::OnApply() {
BOOL status = CPropertyPage::OnApply ();
if (!status)
return status;
// enumerate the virtual control panels and apply individual settings.
for (INT_PTR i = 0; i < m_VirtualControlPanels.GetCount (); ++i)
m_VirtualControlPanels.ElementAt(i)->Apply ();
return TRUE;
}
void
CACDPropertyPage::OnReset ()
{
CPropertyPage::OnReset ();
// reset settings on all virtual control panels.
for (INT_PTR i = 0; i < m_VirtualControlPanels.GetCount (); ++i)
m_VirtualControlPanels.ElementAt (i)->Reset ();
}
void
CACDPropertyPage::SetModified (BOOL bChanged)
{
if (bChanged) {
CPropertyPage::SetModified(TRUE);
} else {
// if any of the VCPs have modified settings, we can't clear the
// modified flags.
for (INT_PTR i = 0; i < m_VirtualControlPanels.GetCount (); ++i)
bChanged |= m_VirtualControlPanels.ElementAt (i)->Modified ();
CPropertyPage::SetModified (bChanged);
}
}
void
CACDPropertyPage::DoDataExchange (CDataExchange* pDX)
{
CPropertyPage::DoDataExchange (pDX);
DDX_Control (pDX, IDC_BRIGHTNESS_SLIDER, m_cBrightnessSlider);
DDX_Control (pDX, IDC_BRIGHTNESS_EDIT, m_cBrightnessEdit);
DDX_Control (pDX, IDC_VIRTUAL_CONTROL_LIST, m_cVirtualControlList);
}
BEGIN_MESSAGE_MAP (CACDPropertyPage, CPropertyPage)
ON_WM_DEVICECHANGE ()
ON_WM_HSCROLL ()
ON_WM_TIMER ()
ON_BN_CLICKED (IDC_PROPERTIES_BUTTON, OnBnClickedPropertiesButton)
ON_BN_CLICKED (IDC_OPTIONS_BUTTON, OnBnClickedOptionsButton)
ON_LBN_SELCHANGE (IDC_VIRTUAL_CONTROL_LIST, OnLbnSelchangeVirtualControlList)
ON_EN_CHANGE (IDC_BRIGHTNESS_EDIT, OnEnChangeBrightnessEdit)
END_MESSAGE_MAP ()
//
// CACDPropertyPage message handlers
//
void CACDPropertyPage::OnLbnSelchangeVirtualControlList()
{
int index = m_cVirtualControlList.GetCurSel ();
m_pCurrentVirtualCP = (CACDVirtualCP*)
m_cVirtualControlList.GetItemDataPtr (index);
UpdateControls ();
}
void
CACDPropertyPage::OnHScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if ((CSliderCtrl*) pScrollBar == &m_cBrightnessSlider) {
int pos = m_cBrightnessSlider.GetPos ();
m_pCurrentVirtualCP->SetBrightness (pos);
UpdateBrightnessEdit ();
SetModified (TRUE);
}
else
CPropertyPage::OnHScroll (nSBCode, nPos, pScrollBar);
}
void
CACDPropertyPage::OnTimer (UINT nIDEvent)
{
if (nIDEvent == ACD_SETTINGS_TIMER) {
if (!m_pCurrentVirtualCP->CheckBrightness ()) {
SetModified (FALSE);
if (m_pCurrentVirtualCP->Reset ())
UpdateControls ();
}
}
else
CPropertyPage::OnTimer (nIDEvent);
}
void
CACDPropertyPage::OnBnClickedPropertiesButton ()
{
typedef void (_stdcall *LPFNDLLFUNC) (HWND, HINSTANCE, LPCSTR, int);
HINSTANCE hDLL = LoadLibrary (_TEXT ("DEVMGR.DLL"));
if (hDLL == NULL)
return;
LPFNDLLFUNC DeviceProperties = (LPFNDLLFUNC) GetProcAddress(
hDLL, "DevicePropertiesA");
if (DeviceProperties)
DeviceProperties (m_hWnd, 0, m_pCurrentVirtualCP->GetDeviceID (), 0);
FreeLibrary (hDLL);
}
void
CACDPropertyPage::OnBnClickedOptionsButton ()
{
CACDOptionsDialog dlg (m_VirtualControlPanels, this);
if (dlg.DoModal () != IDOK)
return;
UCHAR bFlags, bMask;
ACDUtil::GetFlagsFromPrefs (bFlags, bMask);
for (INT_PTR i = 0; i < m_VirtualControlPanels.GetCount (); ++i) {
CACDHidDevice& Device = m_VirtualControlPanels.ElementAt (i)
->GetDevice ();
if (Device.IsAluminumCinemaDisplay ())
Device.SetFlags (Device.GetFlags () & ~bMask | bFlags);
}
}
void
CACDPropertyPage::OnEnChangeBrightnessEdit ()
{
if (!::IsWindow(m_cBrightnessEdit.m_hWnd /* not initialized */)
|| GetFocus () != &m_cBrightnessEdit /* no focus */)
return;
char buffer [4]; // text limit is set to 3 chars
m_cBrightnessEdit.GetWindowText (buffer, sizeof (buffer));
buffer [sizeof (buffer) - 1] = '\0';
int value = atoi (buffer);
// check for out-of-range.
if (value < 0 || value > 100) {
value = min (0, max (value, 100));
_snprintf_s(buffer, sizeof (buffer), "%d", value);
buffer [sizeof (buffer) - 1] = '\0';
m_cBrightnessEdit.SetWindowText (buffer);
}
if (m_cBrightnessSlider.GetPos () * 100 / 255 == value)
// does not result in a brightness change.
return;
// convert to absolute [0-255]
int brightness = value * 255 / 100;
// ajust so that (brightness * 100 / 255) * 255 / 100 == brightness.
while ((brightness * 100 / 255) != value) ++brightness;
m_cBrightnessSlider.SetPos (brightness);
m_pCurrentVirtualCP->SetBrightness (brightness);
SetModified (TRUE);
}
BOOL
CACDPropertyPage::OnDeviceChange (UINT nEventType, DWORD_PTR dwData)
{
PDEV_BROADCAST_HDR pdb = (PDEV_BROADCAST_HDR) dwData;
switch (nEventType) {
case DBT_DEVICEARRIVAL:
case DBT_DEVICEREMOVECOMPLETE:
if (pdb->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE)
break;
KillTimer (ACD_SETTINGS_TIMER);
for (INT_PTR i = 0; i < m_VirtualControlPanels.GetCount (); ++i)
delete m_VirtualControlPanels.ElementAt (i);
m_VirtualControlPanels.RemoveAll ();
CACDVirtualCP::FindKnownVirtualCPs (m_VirtualControlPanels);
InitializeControls ();
SetModified (FALSE);
if (m_VirtualControlPanels.GetCount () > 0)
SetTimer (ACD_SETTINGS_TIMER, 500, 0);
break;
default:
break;
}
return CPropertyPage::OnDeviceChange (nEventType, dwData);
}