forked from deskflow/deskflow
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathMSWindowsScreen.cpp
1957 lines (1700 loc) · 55.2 KB
/
MSWindowsScreen.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* barrier -- mouse and keyboard sharing utility
* Copyright (C) 2018 Debauchee Open Source Group
* Copyright (C) 2012-2016 Symless Ltd.
* Copyright (C) 2002 Chris Schoeneman
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package 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, see <http://www.gnu.org/licenses/>.
*/
#include "platform/MSWindowsScreen.h"
#include "platform/MSWindowsDropTarget.h"
#include "client/Client.h"
#include "platform/MSWindowsClipboard.h"
#include "platform/MSWindowsDesks.h"
#include "platform/MSWindowsEventQueueBuffer.h"
#include "platform/MSWindowsKeyState.h"
#include "platform/MSWindowsScreenSaver.h"
#include "barrier/Clipboard.h"
#include "barrier/KeyMap.h"
#include "barrier/XScreen.h"
#include "barrier/App.h"
#include "barrier/ArgsBase.h"
#include "barrier/ClientApp.h"
#include "mt/Lock.h"
#include "mt/Thread.h"
#include "arch/win32/ArchMiscWindows.h"
#include "arch/Arch.h"
#include "base/Log.h"
#include "base/IEventQueue.h"
#include "base/TMethodEventJob.h"
#include <string.h>
#include <Shlobj.h>
#include <comutil.h>
#include <algorithm>
//
// add backwards compatible multihead support (and suppress bogus warning).
// this isn't supported on MinGW yet AFAICT.
//
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4706) // assignment within conditional
#define COMPILE_MULTIMON_STUBS
#include <multimon.h>
#pragma warning(pop)
#endif
// X button stuff
#if !defined(WM_XBUTTONDOWN)
#define WM_XBUTTONDOWN 0x020B
#define WM_XBUTTONUP 0x020C
#define WM_XBUTTONDBLCLK 0x020D
#define WM_NCXBUTTONDOWN 0x00AB
#define WM_NCXBUTTONUP 0x00AC
#define WM_NCXBUTTONDBLCLK 0x00AD
#define MOUSEEVENTF_XDOWN 0x0080
#define MOUSEEVENTF_XUP 0x0100
#define XBUTTON1 0x0001
#define XBUTTON2 0x0002
#endif
#if !defined(VK_XBUTTON1)
#define VK_XBUTTON1 0x05
#define VK_XBUTTON2 0x06
#endif
// WM_POWERBROADCAST stuff
#if !defined(PBT_APMRESUMEAUTOMATIC)
#define PBT_APMRESUMEAUTOMATIC 0x0012
#endif
//
// MSWindowsScreen
//
HINSTANCE MSWindowsScreen::s_windowInstance = NULL;
MSWindowsScreen* MSWindowsScreen::s_screen = NULL;
MSWindowsScreen::MSWindowsScreen(
bool isPrimary,
bool noHooks,
bool stopOnDeskSwitch,
IEventQueue* events) :
PlatformScreen(events),
m_isPrimary(isPrimary),
m_noHooks(noHooks),
m_isOnScreen(m_isPrimary),
m_class(0),
m_x(0), m_y(0),
m_w(0), m_h(0),
m_xCenter(0), m_yCenter(0),
m_multimon(false),
m_xCursor(0), m_yCursor(0),
m_sequenceNumber(0),
m_mark(0),
m_markReceived(0),
m_fixTimer(NULL),
m_keyLayout(NULL),
m_screensaver(NULL),
m_screensaverNotify(false),
m_screensaverActive(false),
m_window(NULL),
m_nextClipboardWindow(NULL),
m_ownClipboard(false),
m_desks(NULL),
m_keyState(NULL),
m_hasMouse(GetSystemMetrics(SM_MOUSEPRESENT) != 0),
m_showingMouse(false),
m_events(events),
m_dropWindow(NULL),
m_dropWindowSize(20)
{
assert(s_windowInstance != NULL);
assert(s_screen == NULL);
s_screen = this;
try {
m_screensaver = new MSWindowsScreenSaver();
m_desks = new MSWindowsDesks(
m_isPrimary,
m_noHooks,
m_screensaver,
m_events,
[this]() { updateKeysCB(); },
stopOnDeskSwitch);
m_keyState = new MSWindowsKeyState(m_desks, getEventTarget(), m_events);
updateScreenShape();
m_class = createWindowClass();
m_window = createWindow(m_class, "Barrier");
forceShowCursor();
LOG((CLOG_DEBUG "screen shape: %d,%d %dx%d %s", m_x, m_y, m_w, m_h, m_multimon ? "(multi-monitor)" : ""));
LOG((CLOG_DEBUG "window is 0x%08x", m_window));
OleInitialize(0);
m_dropWindow = createDropWindow(m_class, "DropWindow");
m_dropTarget = new MSWindowsDropTarget();
RegisterDragDrop(m_dropWindow, m_dropTarget);
}
catch (...) {
delete m_keyState;
delete m_desks;
delete m_screensaver;
destroyWindow(m_window);
destroyClass(m_class);
s_screen = NULL;
throw;
}
// install event handlers
m_events->adoptHandler(Event::kSystem, m_events->getSystemTarget(),
new TMethodEventJob<MSWindowsScreen>(this,
&MSWindowsScreen::handleSystemEvent));
// install the platform event queue
m_events->adoptBuffer(new MSWindowsEventQueueBuffer(m_events));
}
MSWindowsScreen::~MSWindowsScreen()
{
assert(s_screen != NULL);
disable();
m_events->adoptBuffer(NULL);
m_events->removeHandler(Event::kSystem, m_events->getSystemTarget());
delete m_keyState;
delete m_desks;
delete m_screensaver;
destroyWindow(m_window);
destroyClass(m_class);
RevokeDragDrop(m_dropWindow);
m_dropTarget->Release();
OleUninitialize();
destroyWindow(m_dropWindow);
s_screen = NULL;
}
void
MSWindowsScreen::init(HINSTANCE windowInstance)
{
assert(s_windowInstance == NULL);
assert(windowInstance != NULL);
s_windowInstance = windowInstance;
}
HINSTANCE
MSWindowsScreen::getWindowInstance()
{
return s_windowInstance;
}
void
MSWindowsScreen::enable()
{
assert(m_isOnScreen == m_isPrimary);
// we need to poll some things to fix them
m_fixTimer = m_events->newTimer(1.0, NULL);
m_events->adoptHandler(Event::kTimer, m_fixTimer,
new TMethodEventJob<MSWindowsScreen>(this,
&MSWindowsScreen::handleFixes));
// install our clipboard snooper
m_nextClipboardWindow = SetClipboardViewer(m_window);
// track the active desk and (re)install the hooks
m_desks->enable();
if (m_isPrimary) {
// set jump zones
m_hook.setZone(m_x, m_y, m_w, m_h, getJumpZoneSize());
// watch jump zones
m_hook.setMode(kHOOK_WATCH_JUMP_ZONE);
}
else {
// prevent the system from entering power saving modes. if
// it did we'd be forced to disconnect from the server and
// the server would not be able to wake us up.
ArchMiscWindows::addBusyState(ArchMiscWindows::kSYSTEM);
}
}
void
MSWindowsScreen::disable()
{
// stop tracking the active desk
m_desks->disable();
if (m_isPrimary) {
// disable hooks
m_hook.setMode(kHOOK_DISABLE);
// enable special key sequences on win95 family
enableSpecialKeys(true);
}
else {
// allow the system to enter power saving mode
ArchMiscWindows::removeBusyState(ArchMiscWindows::kSYSTEM |
ArchMiscWindows::kDISPLAY);
}
// tell key state
m_keyState->disable();
// stop snooping the clipboard
ChangeClipboardChain(m_window, m_nextClipboardWindow);
m_nextClipboardWindow = NULL;
// uninstall fix timer
if (m_fixTimer != NULL) {
m_events->removeHandler(Event::kTimer, m_fixTimer);
m_events->deleteTimer(m_fixTimer);
m_fixTimer = NULL;
}
m_isOnScreen = m_isPrimary;
forceShowCursor();
}
void
MSWindowsScreen::enter()
{
m_desks->enter();
if (m_isPrimary) {
// enable special key sequences on win95 family
enableSpecialKeys(true);
// watch jump zones
m_hook.setMode(kHOOK_WATCH_JUMP_ZONE);
// all messages prior to now are invalid
nextMark();
m_primaryKeyDownList.clear();
}
else {
// Entering a secondary screen. Ensure that no screensaver is active
// and that the screen is not in powersave mode.
ArchMiscWindows::wakeupDisplay();
if (m_screensaver != NULL && m_screensaverActive)
{
m_screensaver->deactivate();
m_screensaverActive = 0;
}
}
// now on screen
m_isOnScreen = true;
forceShowCursor();
}
bool
MSWindowsScreen::leave()
{
// get keyboard layout of foreground window. we'll use this
// keyboard layout for translating keys sent to clients.
HWND window = GetForegroundWindow();
DWORD thread = GetWindowThreadProcessId(window, NULL);
m_keyLayout = GetKeyboardLayout(thread);
// tell the key mapper about the keyboard layout
m_keyState->setKeyLayout(m_keyLayout);
// tell desk that we're leaving and tell it the keyboard layout
m_desks->leave(m_keyLayout);
if (m_isPrimary) {
// warp to center
LOG((CLOG_DEBUG1 "warping cursor to center: %+d, %+d", m_xCenter, m_yCenter));
warpCursor(m_xCenter, m_yCenter);
// disable special key sequences on win95 family
enableSpecialKeys(false);
// all messages prior to now are invalid
nextMark();
// remember the modifier state. this is the modifier state
// reflected in the internal keyboard state.
m_keyState->saveModifiers();
m_hook.setMode(kHOOK_RELAY_EVENTS);
m_primaryKeyDownList.clear();
for (KeyButton i = 0; i < IKeyState::kNumButtons; ++i) {
if (m_keyState->isKeyDown(i)) {
m_primaryKeyDownList.push_back(i);
LOG((CLOG_DEBUG1 "key button %d is down before leaving to another screen", i));
}
}
}
// now off screen
m_isOnScreen = false;
forceShowCursor();
if (isDraggingStarted() && !m_isPrimary) {
m_sendDragThread = new Thread([this](){ send_drag_thread(); });
}
return true;
}
void MSWindowsScreen::send_drag_thread()
{
std::string& draggingFilename = getDraggingFilename();
size_t size = draggingFilename.size();
if (draggingFilename.empty() == false) {
ClientApp& app = ClientApp::instance();
Client* client = app.getClientPtr();
UInt32 fileCount = 1;
LOG((CLOG_DEBUG "send dragging info to server: %s", draggingFilename.c_str()));
client->sendDragInfo(fileCount, draggingFilename, size);
LOG((CLOG_DEBUG "send dragging file to server"));
client->sendFileToServer(draggingFilename.c_str());
}
m_draggingStarted = false;
}
bool
MSWindowsScreen::setClipboard(ClipboardID, const IClipboard* src)
{
MSWindowsClipboard dst(m_window);
if (src != NULL) {
// save clipboard data
return Clipboard::copy(&dst, src);
}
else {
// assert clipboard ownership
if (!dst.open(0)) {
return false;
}
dst.empty();
dst.close();
return true;
}
}
void
MSWindowsScreen::checkClipboards()
{
// if we think we own the clipboard but we don't then somebody
// grabbed the clipboard on this screen without us knowing.
// tell the server that this screen grabbed the clipboard.
//
// this works around bugs in the clipboard viewer chain.
// sometimes NT will simply never send WM_DRAWCLIPBOARD
// messages for no apparent reason and rebooting fixes the
// problem. since we don't want a broken clipboard until the
// next reboot we do this double check. clipboard ownership
// won't be reflected on other screens until we leave but at
// least the clipboard itself will work.
if (m_ownClipboard && !MSWindowsClipboard::isOwnedByBarrier()) {
LOG((CLOG_DEBUG "clipboard changed: lost ownership and no notification received"));
m_ownClipboard = false;
sendClipboardEvent(m_events->forClipboard().clipboardGrabbed(), kClipboardClipboard);
sendClipboardEvent(m_events->forClipboard().clipboardGrabbed(), kClipboardSelection);
}
}
void
MSWindowsScreen::openScreensaver(bool notify)
{
assert(m_screensaver != NULL);
m_screensaverNotify = notify;
if (m_screensaverNotify) {
m_desks->installScreensaverHooks(true);
}
else if (m_screensaver) {
m_screensaver->disable();
}
}
void
MSWindowsScreen::closeScreensaver()
{
if (m_screensaver != NULL) {
if (m_screensaverNotify) {
m_desks->installScreensaverHooks(false);
}
else {
m_screensaver->enable();
}
}
m_screensaverNotify = false;
}
void
MSWindowsScreen::screensaver(bool activate)
{
assert(m_screensaver != NULL);
if (m_screensaver==NULL) return;
if (activate) {
m_screensaver->activate();
}
else {
m_screensaver->deactivate();
}
}
void
MSWindowsScreen::resetOptions()
{
m_desks->resetOptions();
}
void
MSWindowsScreen::setOptions(const OptionsList& options)
{
m_desks->setOptions(options);
}
void
MSWindowsScreen::setSequenceNumber(UInt32 seqNum)
{
m_sequenceNumber = seqNum;
}
bool
MSWindowsScreen::isPrimary() const
{
return m_isPrimary;
}
void*
MSWindowsScreen::getEventTarget() const
{
return const_cast<MSWindowsScreen*>(this);
}
bool
MSWindowsScreen::getClipboard(ClipboardID, IClipboard* dst) const
{
MSWindowsClipboard src(m_window);
Clipboard::copy(dst, &src);
return true;
}
void
MSWindowsScreen::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
{
assert(m_class != 0);
x = m_x;
y = m_y;
w = m_w;
h = m_h;
}
void
MSWindowsScreen::getCursorPos(SInt32& x, SInt32& y) const
{
m_desks->getCursorPos(x, y);
}
void
MSWindowsScreen::reconfigure(UInt32 activeSides)
{
assert(m_isPrimary);
LOG((CLOG_DEBUG "active sides: %x", activeSides));
m_hook.setSides(activeSides);
}
void
MSWindowsScreen::warpCursor(SInt32 x, SInt32 y)
{
// warp mouse
warpCursorNoFlush(x, y);
// remove all input events before and including warp
MSG msg;
while (PeekMessage(&msg, NULL, BARRIER_MSG_INPUT_FIRST,
BARRIER_MSG_INPUT_LAST, PM_REMOVE)) {
// do nothing
}
// save position to compute delta of next motion
saveMousePosition(x, y);
}
void MSWindowsScreen::saveMousePosition(SInt32 x, SInt32 y) {
m_xCursor = x;
m_yCursor = y;
LOG((CLOG_DEBUG5 "saved mouse position for next delta: %+d,%+d", x,y));
}
UInt32
MSWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask)
{
// only allow certain modifiers
if ((mask & ~(KeyModifierShift | KeyModifierControl |
KeyModifierAlt | KeyModifierSuper)) != 0) {
// this should be a warning, but this can confuse users,
// as this warning happens almost always.
LOG((CLOG_DEBUG "could not map hotkey id=%04x mask=%04x", key, mask));
return 0;
}
// fail if no keys
if (key == kKeyNone && mask == 0) {
return 0;
}
// convert to win32
UINT modifiers = 0;
if ((mask & KeyModifierShift) != 0) {
modifiers |= MOD_SHIFT;
}
if ((mask & KeyModifierControl) != 0) {
modifiers |= MOD_CONTROL;
}
if ((mask & KeyModifierAlt) != 0) {
modifiers |= MOD_ALT;
}
if ((mask & KeyModifierSuper) != 0) {
modifiers |= MOD_WIN;
}
UINT vk = m_keyState->mapKeyToVirtualKey(key);
if (key != kKeyNone && vk == 0) {
// can't map key
// this should be a warning, but this can confuse users,
// as this warning happens almost always.
LOG((CLOG_DEBUG "could not map hotkey id=%04x mask=%04x", key, mask));
return 0;
}
// choose hotkey id
UInt32 id;
if (!m_oldHotKeyIDs.empty()) {
id = m_oldHotKeyIDs.back();
m_oldHotKeyIDs.pop_back();
}
else {
//id = m_hotKeys.size() + 1;
id = (UInt32)m_hotKeys.size() + 1;
}
// if this hot key has modifiers only then we'll handle it specially
bool err;
if (key == kKeyNone) {
// check if already registered
err = (m_hotKeyToIDMap.count(HotKeyItem(vk, modifiers)) > 0);
}
else {
// register with OS
err = (RegisterHotKey(NULL, id, modifiers, vk) == 0);
}
if (!err) {
m_hotKeys.insert(std::make_pair(id, HotKeyItem(vk, modifiers)));
m_hotKeyToIDMap[HotKeyItem(vk, modifiers)] = id;
}
else {
m_oldHotKeyIDs.push_back(id);
m_hotKeys.erase(id);
LOG((CLOG_WARN "failed to register hotkey %s (id=%04x mask=%04x)", barrier::KeyMap::formatKey(key, mask).c_str(), key, mask));
return 0;
}
LOG((CLOG_DEBUG "registered hotkey %s (id=%04x mask=%04x) as id=%d", barrier::KeyMap::formatKey(key, mask).c_str(), key, mask, id));
return id;
}
void
MSWindowsScreen::unregisterHotKey(UInt32 id)
{
// look up hotkey
HotKeyMap::iterator i = m_hotKeys.find(id);
if (i == m_hotKeys.end()) {
return;
}
// unregister with OS
bool err;
if (i->second.getVirtualKey() != 0) {
err = !UnregisterHotKey(NULL, id);
}
else {
err = false;
}
if (err) {
LOG((CLOG_WARN "failed to unregister hotkey id=%d", id));
}
else {
LOG((CLOG_DEBUG "unregistered hotkey id=%d", id));
}
// discard hot key from map and record old id for reuse
m_hotKeyToIDMap.erase(i->second);
m_hotKeys.erase(i);
m_oldHotKeyIDs.push_back(id);
}
void
MSWindowsScreen::fakeInputBegin()
{
assert(m_isPrimary);
if (!m_isOnScreen) {
m_keyState->useSavedModifiers(true);
}
m_desks->fakeInputBegin();
}
void
MSWindowsScreen::fakeInputEnd()
{
assert(m_isPrimary);
m_desks->fakeInputEnd();
if (!m_isOnScreen) {
m_keyState->useSavedModifiers(false);
}
}
SInt32
MSWindowsScreen::getJumpZoneSize() const
{
return 1;
}
bool
MSWindowsScreen::isAnyMouseButtonDown(UInt32& buttonID) const
{
static const char* buttonToName[] = {
"<invalid>",
"Left Button",
"Middle Button",
"Right Button",
"X Button 1",
"X Button 2"
};
for (UInt32 i = 1; i < sizeof(m_buttons) / sizeof(m_buttons[0]); ++i) {
if (m_buttons[i]) {
buttonID = i;
LOG((CLOG_DEBUG "locked by \"%s\"", buttonToName[i]));
return true;
}
}
return false;
}
void
MSWindowsScreen::getCursorCenter(SInt32& x, SInt32& y) const
{
x = m_xCenter;
y = m_yCenter;
}
void
MSWindowsScreen::fakeMouseButton(ButtonID id, bool press)
{
m_desks->fakeMouseButton(id, press);
if (id == kButtonLeft) {
if (press) {
m_buttons[kButtonLeft] = true;
}
else {
m_buttons[kButtonLeft] = false;
m_fakeDraggingStarted = false;
m_draggingStarted = false;
}
}
}
void
MSWindowsScreen::fakeMouseMove(SInt32 x, SInt32 y)
{
m_desks->fakeMouseMove(x, y);
if (m_buttons[kButtonLeft]) {
m_draggingStarted = true;
}
}
void
MSWindowsScreen::fakeMouseRelativeMove(SInt32 dx, SInt32 dy) const
{
m_desks->fakeMouseRelativeMove(dx, dy);
}
void
MSWindowsScreen::fakeMouseWheel(SInt32 xDelta, SInt32 yDelta) const
{
m_desks->fakeMouseWheel(xDelta, yDelta);
}
void
MSWindowsScreen::updateKeys()
{
m_desks->updateKeys();
}
void
MSWindowsScreen::fakeKeyDown(KeyID id, KeyModifierMask mask,
KeyButton button)
{
PlatformScreen::fakeKeyDown(id, mask, button);
updateForceShowCursor();
}
bool
MSWindowsScreen::fakeKeyRepeat(KeyID id, KeyModifierMask mask,
SInt32 count, KeyButton button)
{
bool result = PlatformScreen::fakeKeyRepeat(id, mask, count, button);
updateForceShowCursor();
return result;
}
bool
MSWindowsScreen::fakeKeyUp(KeyButton button)
{
bool result = PlatformScreen::fakeKeyUp(button);
updateForceShowCursor();
return result;
}
void
MSWindowsScreen::fakeAllKeysUp()
{
PlatformScreen::fakeAllKeysUp();
updateForceShowCursor();
}
HCURSOR
MSWindowsScreen::createBlankCursor() const
{
// create a transparent cursor
int cw = GetSystemMetrics(SM_CXCURSOR);
int ch = GetSystemMetrics(SM_CYCURSOR);
UInt8* cursorAND = new UInt8[ch * ((cw + 31) >> 2)];
UInt8* cursorXOR = new UInt8[ch * ((cw + 31) >> 2)];
memset(cursorAND, 0xff, ch * ((cw + 31) >> 2));
memset(cursorXOR, 0x00, ch * ((cw + 31) >> 2));
HCURSOR c = CreateCursor(s_windowInstance, 0, 0, cw, ch, cursorAND, cursorXOR);
delete[] cursorXOR;
delete[] cursorAND;
return c;
}
void
MSWindowsScreen::destroyCursor(HCURSOR cursor) const
{
if (cursor != NULL) {
DestroyCursor(cursor);
}
}
ATOM
MSWindowsScreen::createWindowClass() const
{
WNDCLASSEX classInfo;
classInfo.cbSize = sizeof(classInfo);
classInfo.style = CS_DBLCLKS | CS_NOCLOSE;
classInfo.lpfnWndProc = &MSWindowsScreen::wndProc;
classInfo.cbClsExtra = 0;
classInfo.cbWndExtra = 0;
classInfo.hInstance = s_windowInstance;
classInfo.hIcon = NULL;
classInfo.hCursor = NULL;
classInfo.hbrBackground = NULL;
classInfo.lpszMenuName = NULL;
classInfo.lpszClassName = "Barrier";
classInfo.hIconSm = NULL;
return RegisterClassEx(&classInfo);
}
void
MSWindowsScreen::destroyClass(ATOM windowClass) const
{
if (windowClass != 0) {
UnregisterClass(MAKEINTATOM(windowClass), s_windowInstance);
}
}
HWND
MSWindowsScreen::createWindow(ATOM windowClass, const char* name) const
{
HWND window = CreateWindowEx(WS_EX_TOPMOST |
WS_EX_TRANSPARENT |
WS_EX_TOOLWINDOW,
MAKEINTATOM(windowClass),
name,
WS_POPUP,
0, 0, 1, 1,
NULL, NULL,
s_windowInstance,
NULL);
if (window == NULL) {
LOG((CLOG_ERR "failed to create window: %d", GetLastError()));
throw XScreenOpenFailure();
}
return window;
}
HWND
MSWindowsScreen::createDropWindow(ATOM windowClass, const char* name) const
{
HWND window = CreateWindowEx(
WS_EX_TOPMOST |
WS_EX_TRANSPARENT |
WS_EX_ACCEPTFILES,
MAKEINTATOM(m_class),
name,
WS_POPUP,
0, 0, m_dropWindowSize, m_dropWindowSize,
NULL, NULL,
s_windowInstance,
NULL);
if (window == NULL) {
LOG((CLOG_ERR "failed to create drop window: %d", GetLastError()));
throw XScreenOpenFailure();
}
return window;
}
void
MSWindowsScreen::destroyWindow(HWND hwnd) const
{
if (hwnd != NULL) {
DestroyWindow(hwnd);
}
}
void
MSWindowsScreen::sendEvent(Event::Type type, void* data)
{
m_events->addEvent(Event(type, getEventTarget(), data));
}
void
MSWindowsScreen::sendClipboardEvent(Event::Type type, ClipboardID id)
{
ClipboardInfo* info = (ClipboardInfo*)malloc(sizeof(ClipboardInfo));
if (info == NULL) {
LOG((CLOG_ERR "malloc failed on %s:%s", __FILE__, __LINE__ ));
return;
}
info->m_id = id;
info->m_sequenceNumber = m_sequenceNumber;
sendEvent(type, info);
}
void
MSWindowsScreen::handleSystemEvent(const Event& event, void*)
{
MSG* msg = static_cast<MSG*>(event.getData());
assert(msg != NULL);
if (ArchMiscWindows::processDialog(msg)) {
return;
}
if (onPreDispatch(msg->hwnd, msg->message, msg->wParam, msg->lParam)) {
return;
}
TranslateMessage(msg);
DispatchMessage(msg);
}
void
MSWindowsScreen::updateButtons()
{
int numButtons = GetSystemMetrics(SM_CMOUSEBUTTONS);
m_buttons[kButtonNone] = false;
m_buttons[kButtonLeft] = (GetKeyState(VK_LBUTTON) < 0);
m_buttons[kButtonRight] = (GetKeyState(VK_RBUTTON) < 0);
m_buttons[kButtonMiddle] = (GetKeyState(VK_MBUTTON) < 0);
m_buttons[kButtonExtra0] = (numButtons >= 4) &&
(GetKeyState(VK_XBUTTON1) < 0);
m_buttons[kButtonExtra1] = (numButtons >= 5) &&
(GetKeyState(VK_XBUTTON2) < 0);
}
IKeyState*
MSWindowsScreen::getKeyState() const
{
return m_keyState;
}
bool
MSWindowsScreen::onPreDispatch(HWND hwnd,
UINT message, WPARAM wParam, LPARAM lParam)
{
// handle event
switch (message) {
case BARRIER_MSG_SCREEN_SAVER:
return onScreensaver(wParam != 0);
case BARRIER_MSG_DEBUG:
LOG((CLOG_DEBUG1 "hook: 0x%08x 0x%08x", wParam, lParam));
return true;
}
if (m_isPrimary) {
return onPreDispatchPrimary(hwnd, message, wParam, lParam);
}
return false;
}
bool
MSWindowsScreen::onPreDispatchPrimary(HWND,
UINT message, WPARAM wParam, LPARAM lParam)
{
LOG((CLOG_DEBUG5 "handling pre-dispatch primary"));
// handle event
switch (message) {
case BARRIER_MSG_MARK:
return onMark(static_cast<UInt32>(wParam));
case BARRIER_MSG_KEY:
return onKey(wParam, lParam);
case BARRIER_MSG_MOUSE_BUTTON:
return onMouseButton(wParam, lParam);
case BARRIER_MSG_MOUSE_MOVE:
return onMouseMove(static_cast<SInt32>(wParam),
static_cast<SInt32>(lParam));
case BARRIER_MSG_MOUSE_WHEEL:
return onMouseWheel(static_cast<SInt32>(lParam), static_cast<SInt32>(wParam));
case BARRIER_MSG_PRE_WARP:
{
// save position to compute delta of next motion
saveMousePosition(static_cast<SInt32>(wParam), static_cast<SInt32>(lParam));
// we warped the mouse. discard events until we find the
// matching post warp event. see warpCursorNoFlush() for