-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
hooks.c
2439 lines (2239 loc) · 91.2 KB
/
hooks.c
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
/*
Copyright (C) 2015 Stefan Sundin
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 3 of the License, or
(at your option) any later version.
*/
#define UNICODE
#define _UNICODE
#define _WIN32_WINNT 0x0600
#define COBJMACROS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <shlwapi.h>
#include <commctrl.h>
#include <psapi.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
// Stuff missing in MinGW
CLSID my_CLSID_MMDeviceEnumerator = {0xBCDE0395,0xE52F,0x467C,{0x8E,0x3D,0xC4,0x57,0x92,0x91,0x69,0x2E}};
GUID my_IID_IMMDeviceEnumerator = {0xA95664D2,0x9614,0x4F35,{0xA7,0x46,0xDE,0x8D,0xB6,0x36,0x17,0xE6}};
GUID my_IID_IAudioEndpointVolume = {0x5CDF2C82,0x841E,0x4546,{0x97,0x22,0x0C,0xF7,0x40,0x78,0x22,0x9A}};
// App
#define APP_NAME L"AltDrag"
#define AERO_THRESHOLD 5
// Boring stuff
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define RESTORE_TIMER WM_APP+1
#define MOVE_TIMER WM_APP+2
#define REHOOK_TIMER WM_APP+3
#define INIT_TIMER WM_APP+4
#define FOCUS_TIMER WM_APP+5
HWND g_hwnd;
int UnhookMouse();
int HookMouse();
// Enumerators
enum action {ACTION_NONE=0, ACTION_MOVE, ACTION_RESIZE, ACTION_MINIMIZE, ACTION_CENTER, ACTION_ALWAYSONTOP, ACTION_CLOSE, ACTION_LOWER, ACTION_ALTTAB, ACTION_VOLUME, ACTION_TRANSPARENCY};
enum button {BUTTON_NONE=0, BUTTON_LMB, BUTTON_MMB, BUTTON_RMB, BUTTON_MB4, BUTTON_MB5};
enum resize {RESIZE_NONE=0, RESIZE_TOP, RESIZE_RIGHT, RESIZE_BOTTOM, RESIZE_LEFT, RESIZE_CENTER};
enum cursor {HAND, SIZENWSE, SIZENESW, SIZENS, SIZEWE, SIZEALL};
// Some variables must be shared so that CallWndProc hooks can access them
#define shareattr __attribute__((section ("shared"), shared))
// Window database
#define NUMWNDDB 30
struct wnddata {
HWND hwnd;
short restore;
int width;
int height;
struct {
int width;
int height;
} last;
};
struct {
struct wnddata items[NUMWNDDB];
struct wnddata *pos;
} wnddb;
// State
struct {
HWND hwnd;
HWND mdiclient;
short alt;
short activated; // Keep track on if an action has begun since the hotkey was depressed, in order to successfully block Alt from triggering a menu
short ctrl;
short interrupted;
short updaterate;
unsigned int clicktime;
POINT clickpt;
POINT prevpt;
POINT offset;
struct {
enum resize x, y;
} resize;
short blockaltup;
short blockmouseup;
short ignorectrl;
short locked;
struct wnddata *wndentry;
struct {
HMONITOR monitor;
short maximized;
int width;
int height;
int right;
int bottom;
} origin;
struct {
POINT ptMinTrackSize;
POINT ptMaxTrackSize;
} mmi;
} state;
struct {
short shift;
short snap;
enum action action;
} sharedstate shareattr = {0, 0, ACTION_NONE};
// Snap
RECT *monitors = NULL;
int nummonitors = 0;
RECT *wnds = NULL;
int numwnds = 0;
HWND *hwnds = NULL;
int numhwnds = 0;
HWND progman = NULL;
// Settings
#define MAXKEYS 10
struct {
int AutoFocus;
int AutoSnap;
int AutoRemaximize;
int Aero;
int MDI;
int InactiveScroll;
int LowerWithMMB;
int SnapThreshold;
int FocusOnTyping;
struct {
int Cursor;
int MoveRate;
int ResizeRate;
} Performance;
struct {
unsigned char keys[MAXKEYS];
int length;
} Hotkeys;
struct {
enum action LMB, MMB, RMB, MB4, MB5, Scroll;
} Mouse;
} sharedsettings shareattr;
short sharedsettings_loaded shareattr = 0;
wchar_t inipath[MAX_PATH] shareattr;
// Blacklist (not shared since dynamically allocated)
struct blacklistitem {
wchar_t *title;
wchar_t *classname;
};
struct blacklist {
struct blacklistitem *items;
int length;
wchar_t *data;
};
struct {
struct blacklist ProcessBlacklist;
struct blacklist Blacklist;
struct blacklist Snaplist;
} settings = {{NULL,0}, {NULL,0}, {NULL,0}};
// Cursor data
HWND cursorwnd shareattr = NULL;
HCURSOR cursors[6];
// Hook data
HINSTANCE hinstDLL = NULL;
HHOOK mousehook = NULL;
// Msghook data
BOOL subclassed = FALSE;
enum action msgaction shareattr = ACTION_NONE;
short unload shareattr = 0;
// Error()
#ifdef DEBUG
#define ERROR_WRITETOFILE
#include "include/error.c"
#else
#define Error(a,b,c)
#endif
// Blacklist
int blacklisted(HWND hwnd, struct blacklist *list) {
wchar_t title[256]=L"", classname[256]=L"";
int i;
// Do not check if list is empty
if (list->length == 0) {
return 0;
}
// ProcessBlacklist is case-insensitive
if (list == &settings.ProcessBlacklist) {
DWORD pid;
GetWindowThreadProcessId(hwnd, &pid);
HANDLE proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
GetProcessImageFileName(proc, title, ARRAY_SIZE(title));
CloseHandle(proc);
PathStripPath(title);
for (i=0; i < list->length; i++) {
if (!wcsicmp(title,list->items[i].title)) {
return 1;
}
}
return 0;
}
GetWindowText(hwnd, title, ARRAY_SIZE(title));
GetClassName(hwnd, classname, ARRAY_SIZE(classname));
for (i=0; i < list->length; i++) {
if ((list->items[i].title == NULL && !wcscmp(classname,list->items[i].classname))
|| (list->items[i].classname == NULL && !wcscmp(title,list->items[i].title))
|| (list->items[i].title != NULL && list->items[i].classname != NULL && !wcscmp(title,list->items[i].title) && !wcscmp(classname,list->items[i].classname))) {
return 1;
}
}
return 0;
}
// Enumerate
int monitors_alloc = 0;
BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
// Make sure we have enough space allocated
if (nummonitors == monitors_alloc) {
monitors_alloc++;
monitors = realloc(monitors, monitors_alloc*sizeof(RECT));
}
// Add monitor
monitors[nummonitors++] = *lprcMonitor;
return TRUE;
}
int wnds_alloc = 0;
BOOL CALLBACK EnumWindowsProc(HWND window, LPARAM lParam) {
// Make sure we have enough space allocated
if (numwnds == wnds_alloc) {
wnds_alloc += 20;
wnds = realloc(wnds, wnds_alloc*sizeof(RECT));
}
// Only store window if it's visible, not minimized to taskbar, not the window we are dragging and not blacklisted
RECT wnd;
LONG_PTR style;
if (window != state.hwnd && window != progman
&& IsWindowVisible(window) && !IsIconic(window)
&& (((style=GetWindowLongPtr(window,GWL_STYLE))&WS_CAPTION) == WS_CAPTION || blacklisted(window,&settings.Snaplist))
&& GetWindowRect(window,&wnd) != 0
) {
// Maximized?
if (IsZoomed(window)) {
// Get monitor size
HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST);
MONITORINFO mi = { sizeof(MONITORINFO) };
GetMonitorInfo(monitor, &mi);
// Crop this window so that it does not exceed the size of the monitor
// This is done because when maximized, windows have an extra, invisible, border (a border that stretches onto other monitors)
wnd.left = max(wnd.left, mi.rcMonitor.left);
wnd.top = max(wnd.top, mi.rcMonitor.top);
wnd.right = min(wnd.right, mi.rcMonitor.right);
wnd.bottom = min(wnd.bottom, mi.rcMonitor.bottom);
}
// Return if this window is overlapped by another window
int i;
for (i=0; i < numwnds; i++) {
if (wnd.left >= wnds[i].left && wnd.top >= wnds[i].top && wnd.right <= wnds[i].right && wnd.bottom <= wnds[i].bottom) {
return TRUE;
}
}
// Add window
wnds[numwnds++] = wnd;
// Use this to print the title and classname of the windows that are snapable
/*
FILE *f = OpenLog(L"ab");
wchar_t title[100], classname[100];
GetWindowText(window, title, ARRAY_SIZE(title));
GetClassName(window, classname, ARRAY_SIZE(classname));
fwprintf(f, L"window: %s|%s\n", title, classname);
CloseLog(f);
*/
}
return TRUE;
}
int hwnds_alloc = 0;
BOOL CALLBACK EnumAltTabWindows(HWND window, LPARAM lParam) {
// Make sure we have enough space allocated
if (numhwnds == hwnds_alloc) {
hwnds_alloc += 20;
hwnds = realloc(hwnds, hwnds_alloc*sizeof(HWND));
}
// Only store window if it's visible, not minimized to taskbar and on the same monitor as the cursor
if (IsWindowVisible(window) && !IsIconic(window)
&& (GetWindowLongPtr(window,GWL_STYLE)&WS_CAPTION) == WS_CAPTION
&& state.origin.monitor == MonitorFromWindow(window,MONITOR_DEFAULTTONULL)
) {
hwnds[numhwnds++] = window;
}
return TRUE;
}
void EnumMdi() {
// Add MDIClient as the monitor
RECT wnd;
if (GetClientRect(state.mdiclient,&wnd) != 0) {
monitors[nummonitors++] = wnd;
}
if (sharedstate.snap < 2) {
return;
}
// Add all the siblings to the window
POINT mdiclientpt = {0,0};
if (ClientToScreen(state.mdiclient,&mdiclientpt) == FALSE) {
return;
}
HWND window = GetWindow(state.mdiclient, GW_CHILD);
while (window != NULL) {
if (window == state.hwnd) {
window = GetWindow(window, GW_HWNDNEXT);
continue;
}
if (numwnds == wnds_alloc) {
wnds_alloc += 20;
wnds = realloc(wnds, wnds_alloc*sizeof(RECT));
}
if (GetWindowRect(window,&wnd) != 0) {
wnds[numwnds++] = (RECT) { wnd.left-mdiclientpt.x, wnd.top-mdiclientpt.y, wnd.right-mdiclientpt.x, wnd.bottom-mdiclientpt.y };
}
window = GetWindow(window, GW_HWNDNEXT);
}
}
void Enum() {
nummonitors = 0;
numwnds = 0;
// MDI
if (state.mdiclient) {
EnumMdi();
return;
}
// Update handle to progman
if (!IsWindow(progman)) {
progman = FindWindow(L"Progman", L"Program Manager");
}
// Enumerate monitors
EnumDisplayMonitors(NULL, NULL, EnumMonitorsProc, 0);
// Enumerate windows
HWND taskbar = FindWindow(L"Shell_TrayWnd", NULL);
RECT wnd;
if (taskbar != NULL && GetWindowRect(taskbar,&wnd) != 0) {
wnds[numwnds++] = wnd;
}
if (sharedstate.snap >= 2) {
EnumWindows(EnumWindowsProc, 0);
}
// Use this to print the monitors and windows
/*
FILE *f = OpenLog(L"ab");
fwprintf(f, L"nummonitors: %d\n", nummonitors);
int k;
for (k=0; k < nummonitors; k++) {
fwprintf(f, L"mon #%02d: left %d, top %d, right %d, bottom %d\n", k, monitors[k].left, monitors[k].top, monitors[k].right, monitors[k].bottom);
}
fwprintf(f, L"numwnds: %d\n", numwnds);
for (k=0; k < numwnds; k++) {
fwprintf(f, L"wnd #%02d: %dx%d @ %dx%d\n", k, wnds[k].right-wnds[k].left, wnds[k].bottom-wnds[k].top, wnds[k].left, wnds[k].top);
}
fwprintf(f, L"\n");
CloseLog(f);
*/
}
void MoveSnap(int *posx, int *posy, int wndwidth, int wndheight) {
// Enumerate monitors and windows
Enum();
// thresholdx and thresholdy will shrink to make sure the dragged window will snap to the closest windows
int i, j, thresholdx, thresholdy, stuckx=0, stucky=0, stickx=0, sticky=0;
thresholdx = thresholdy = sharedsettings.SnapThreshold;
// Loop monitors and windows
for (i=0, j=0; i < nummonitors || j < numwnds; ) {
RECT snapwnd;
int snapinside;
// Get snapwnd
if (i < nummonitors) {
snapwnd = monitors[i];
snapinside = 1;
i++;
}
else if (j < numwnds) {
snapwnd = wnds[j];
snapinside = (sharedstate.snap != 2);
j++;
}
// Check if posx snaps
if ((snapwnd.top-thresholdx < *posy && *posy < snapwnd.bottom+thresholdx)
|| (*posy-thresholdx < snapwnd.top && snapwnd.top < *posy+wndheight+thresholdx)) {
int snapinside_cond = (snapinside || *posy+wndheight-thresholdx < snapwnd.top || snapwnd.bottom < *posy+thresholdx);
if (*posx-thresholdx < snapwnd.right && snapwnd.right < *posx+thresholdx) {
// The left edge of the dragged window will snap to this window's right edge
stuckx = 1;
stickx = snapwnd.right;
thresholdx = snapwnd.right-*posx;
}
else if (snapinside_cond && *posx+wndwidth-thresholdx < snapwnd.right && snapwnd.right < *posx+wndwidth+thresholdx) {
// The right edge of the dragged window will snap to this window's right edge
stuckx = 1;
stickx = snapwnd.right-wndwidth;
thresholdx = snapwnd.right-(*posx+wndwidth);
}
else if (snapinside_cond && *posx-thresholdx < snapwnd.left && snapwnd.left < *posx+thresholdx) {
// The left edge of the dragged window will snap to this window's left edge
stuckx = 1;
stickx = snapwnd.left;
thresholdx = snapwnd.left-*posx;
}
else if (*posx+wndwidth-thresholdx < snapwnd.left && snapwnd.left < *posx+wndwidth+thresholdx) {
// The right edge of the dragged window will snap to this window's left edge
stuckx = 1;
stickx = snapwnd.left-wndwidth;
thresholdx = snapwnd.left-(*posx+wndwidth);
}
}
// Check if posy snaps
if ((snapwnd.left-thresholdy < *posx && *posx < snapwnd.right+thresholdy)
|| (*posx-thresholdy < snapwnd.left && snapwnd.left < *posx+wndwidth+thresholdy)) {
int snapinside_cond = (snapinside || *posx+wndwidth-thresholdy < snapwnd.left || snapwnd.right < *posx+thresholdy);
if (*posy-thresholdy < snapwnd.bottom && snapwnd.bottom < *posy+thresholdy) {
// The top edge of the dragged window will snap to this window's bottom edge
stucky = 1;
sticky = snapwnd.bottom;
thresholdy = snapwnd.bottom-*posy;
}
else if (snapinside_cond && *posy+wndheight-thresholdy < snapwnd.bottom && snapwnd.bottom < *posy+wndheight+thresholdy) {
// The bottom edge of the dragged window will snap to this window's bottom edge
stucky = 1;
sticky = snapwnd.bottom-wndheight;
thresholdy = snapwnd.bottom-(*posy+wndheight);
}
else if (snapinside_cond && *posy-thresholdy < snapwnd.top && snapwnd.top < *posy+thresholdy) {
// The top edge of the dragged window will snap to this window's top edge
stucky = 1;
sticky = snapwnd.top;
thresholdy = snapwnd.top-*posy;
}
else if (*posy+wndheight-thresholdy < snapwnd.top && snapwnd.top < *posy+wndheight+thresholdy) {
// The bottom edge of the dragged window will snap to this window's top edge
stucky = 1;
sticky = snapwnd.top-wndheight;
thresholdy = snapwnd.top-(*posy+wndheight);
}
}
}
// Update posx and posy
if (stuckx) {
*posx = stickx;
}
if (stucky) {
*posy = sticky;
}
}
void ResizeSnap(int *posx, int *posy, int *wndwidth, int *wndheight) {
Enum(); // Enumerate monitors and windows
// thresholdx and thresholdy will shrink to make sure the dragged window will snap to the closest windows
int i, j, thresholdx, thresholdy, stuckleft=0, stucktop=0, stuckright=0, stuckbottom=0, stickleft=0, sticktop=0, stickright=0, stickbottom=0;
thresholdx = thresholdy = sharedsettings.SnapThreshold;
// Loop monitors and windows
for (i=0, j=0; i < nummonitors || j < numwnds; ) {
RECT snapwnd;
int snapinside;
// Get snapwnd
if (i < nummonitors) {
snapwnd = monitors[i];
snapinside = 1;
i++;
}
else if (j < numwnds) {
snapwnd = wnds[j];
snapinside = (sharedstate.snap != 2);
j++;
}
// Check if posx snaps
if ((snapwnd.top-thresholdx < *posy && *posy < snapwnd.bottom+thresholdx)
|| (*posy-thresholdx < snapwnd.top && snapwnd.top < *posy+*wndheight+thresholdx)) {
int snapinside_cond = (snapinside || *posy+*wndheight-thresholdx < snapwnd.top || snapwnd.bottom < *posy+thresholdx);
if (state.resize.x == RESIZE_LEFT && *posx-thresholdx < snapwnd.right && snapwnd.right < *posx+thresholdx) {
// The left edge of the dragged window will snap to this window's right edge
stuckleft = 1;
stickleft = snapwnd.right;
thresholdx = snapwnd.right-*posx;
}
else if (snapinside_cond && state.resize.x == RESIZE_RIGHT && *posx+*wndwidth-thresholdx < snapwnd.right && snapwnd.right < *posx+*wndwidth+thresholdx) {
// The right edge of the dragged window will snap to this window's right edge
stuckright = 1;
stickright = snapwnd.right;
thresholdx = snapwnd.right-(*posx+*wndwidth);
}
else if (snapinside_cond && state.resize.x == RESIZE_LEFT && *posx-thresholdx < snapwnd.left && snapwnd.left < *posx+thresholdx) {
// The left edge of the dragged window will snap to this window's left edge
stuckleft = 1;
stickleft = snapwnd.left;
thresholdx = snapwnd.left-*posx;
}
else if (state.resize.x == RESIZE_RIGHT && *posx+*wndwidth-thresholdx < snapwnd.left && snapwnd.left < *posx+*wndwidth+thresholdx) {
// The right edge of the dragged window will snap to this window's left edge
stuckright = 1;
stickright = snapwnd.left;
thresholdx = snapwnd.left-(*posx+*wndwidth);
}
}
// Check if posy snaps
if ((snapwnd.left-thresholdy < *posx && *posx < snapwnd.right+thresholdy)
|| (*posx-thresholdy < snapwnd.left && snapwnd.left < *posx+*wndwidth+thresholdy)) {
int snapinside_cond = (snapinside || *posx+*wndwidth-thresholdy < snapwnd.left || snapwnd.right < *posx+thresholdy);
if (state.resize.y == RESIZE_TOP && *posy-thresholdy < snapwnd.bottom && snapwnd.bottom < *posy+thresholdy) {
// The top edge of the dragged window will snap to this window's bottom edge
stucktop = 1;
sticktop = snapwnd.bottom;
thresholdy = snapwnd.bottom-*posy;
}
else if (snapinside_cond && state.resize.y == RESIZE_BOTTOM && *posy+*wndheight-thresholdy < snapwnd.bottom && snapwnd.bottom < *posy+*wndheight+thresholdy) {
// The bottom edge of the dragged window will snap to this window's bottom edge
stuckbottom = 1;
stickbottom = snapwnd.bottom;
thresholdy = snapwnd.bottom-(*posy+*wndheight);
}
else if (snapinside_cond && state.resize.y == RESIZE_TOP && *posy-thresholdy < snapwnd.top && snapwnd.top < *posy+thresholdy) {
// The top edge of the dragged window will snap to this window's top edge
stucktop = 1;
sticktop = snapwnd.top;
thresholdy = snapwnd.top-*posy;
}
else if (state.resize.y == RESIZE_BOTTOM && *posy+*wndheight-thresholdy < snapwnd.top && snapwnd.top < *posy+*wndheight+thresholdy) {
// The bottom edge of the dragged window will snap to this window's top edge
stuckbottom = 1;
stickbottom = snapwnd.top;
thresholdy = snapwnd.top-(*posy+*wndheight);
}
}
}
// Update posx, posy, wndwidth and wndheight
if (stuckleft) {
*wndwidth = *wndwidth+*posx-stickleft;
*posx = stickleft;
}
if (stucktop) {
*wndheight = *wndheight+*posy-sticktop;
*posy = sticktop;
}
if (stuckright) {
*wndwidth = stickright-*posx;
}
if (stuckbottom) {
*wndheight = stickbottom-*posy;
}
}
#ifdef _WIN64
// x64 keyhook needs only to check when the shift key is depressed
__declspec(dllexport) LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
int vkey = ((PKBDLLHOOKSTRUCT)lParam)->vkCode;
if (vkey == VK_LSHIFT || vkey == VK_RSHIFT) {
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
sharedstate.shift = 1;
sharedstate.snap = 3;
}
else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
sharedstate.shift = 0;
sharedstate.snap = sharedsettings.AutoSnap;
}
}
else if (vkey == VK_SPACE
&& (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
&& (sharedstate.action || msgaction) && sharedstate.snap) {
sharedstate.snap = 0;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
#else
// Get action of button
int GetAction(int button) {
if (button == BUTTON_LMB) return sharedsettings.Mouse.LMB;
if (button == BUTTON_MMB) return sharedsettings.Mouse.MMB;
if (button == BUTTON_RMB) return sharedsettings.Mouse.RMB;
if (button == BUTTON_MB4) return sharedsettings.Mouse.MB4;
if (button == BUTTON_MB5) return sharedsettings.Mouse.MB5;
return ACTION_NONE;
}
// Check if key is assigned
int IsHotkey(int key) {
int i;
for (i=0; i < sharedsettings.Hotkeys.length; i++) {
if (key == sharedsettings.Hotkeys.keys[i]) {
return 1;
}
}
return 0;
}
void MouseMove() {
int posx, posy, wndwidth, wndheight;
// Make sure we got something to do
if (sharedstate.action != ACTION_MOVE && sharedstate.action != ACTION_RESIZE) {
return;
}
// Check if window still exists
if (!IsWindow(state.hwnd)) {
UnhookMouse();
return;
}
// Get cursor
POINT pt;
GetCursorPos(&pt);
// Restrict pt within origin monitor if Ctrl is being pressed
if ((GetAsyncKeyState(VK_CONTROL)&0x8000) && !state.ignorectrl) {
MONITORINFO mi = { sizeof(MONITORINFO) };
GetMonitorInfo(state.origin.monitor, &mi);
RECT fmon = mi.rcMonitor;
pt.x = (pt.x<fmon.left)?fmon.left: (pt.x>=fmon.right)?fmon.right-1: pt.x;
pt.y = (pt.y<fmon.top)?fmon.top: (pt.y>=fmon.bottom)?fmon.bottom-1: pt.y;
}
// Get window state
int maximized = IsZoomed(state.hwnd);
HMONITOR monitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
// AutoRemaximize has priority over locked flag
if (sharedstate.action == ACTION_MOVE && sharedsettings.AutoRemaximize && state.origin.maximized && monitor != state.origin.monitor && !state.mdiclient) {
// Get monitor rect
MONITORINFO mi = { sizeof(MONITORINFO) };
GetMonitorInfo(monitor, &mi);
RECT mon = mi.rcWork;
RECT fmon = mi.rcMonitor;
// Center window on monitor and maximize it
WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
GetWindowPlacement(state.hwnd, &wndpl);
wndpl.rcNormalPosition.left = fmon.left+(mon.right-mon.left)/2-state.origin.width/2;
wndpl.rcNormalPosition.top = fmon.top+(mon.bottom-mon.top)/2-state.origin.height/2;
wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left+state.origin.width;
wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top+state.origin.height;
if (maximized) {
wndpl.showCmd = SW_RESTORE;
SetWindowPlacement(state.hwnd, &wndpl);
}
wndpl.showCmd = SW_MAXIMIZE;
SetWindowPlacement(state.hwnd, &wndpl);
// Set this monitor as the origin (dirty hack maybe)
state.origin.monitor = monitor;
// Lock the current state
state.locked = 1;
// Restore window after a timeout if AutoRemaximize=2
if (sharedsettings.AutoRemaximize == 2) {
SetTimer(g_hwnd, RESTORE_TIMER, 1000, NULL);
}
return;
}
// Return if state is locked
if (state.locked) {
return;
}
// Get window size
RECT wnd, mon, fmon;
if (GetWindowRect(state.hwnd,&wnd) == 0) {
return;
}
// MDI
POINT mdiclientpt = {0,0};
if (state.mdiclient) {
RECT mdiclientwnd;
if (GetClientRect(state.mdiclient,&mdiclientwnd) == 0
|| ClientToScreen(state.mdiclient,&mdiclientpt) == FALSE) {
return;
}
mon = fmon = (RECT) {0, 0, mdiclientwnd.right-mdiclientwnd.left, mdiclientwnd.bottom-mdiclientwnd.top};
pt.x -= mdiclientpt.x;
pt.y -= mdiclientpt.y;
}
else {
// Get monitor info
MONITORINFO mi = { sizeof(MONITORINFO) };
GetMonitorInfo(monitor, &mi);
mon = mi.rcWork;
fmon = mi.rcMonitor;
}
// Get new position for window
if (sharedstate.action == ACTION_MOVE) {
posx = pt.x-state.offset.x;
posy = pt.y-state.offset.y;
wndwidth = wnd.right-wnd.left;
wndheight = wnd.bottom-wnd.top;
// Aero Snap
if (sharedsettings.Aero) {
// Restore window?
if (maximized
&& ((pt.y >= mon.top+AERO_THRESHOLD)
|| (fmon.left < pt.x && pt.x < mon.left+2*AERO_THRESHOLD)
|| (mon.right-2*AERO_THRESHOLD < pt.x && pt.x < fmon.right))) {
// Restore window
WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
GetWindowPlacement(state.hwnd, &wndpl);
wndpl.showCmd = SW_RESTORE;
SetWindowPlacement(state.hwnd, &wndpl);
// Update wndwidth and wndheight
wndwidth = wndpl.rcNormalPosition.right-wndpl.rcNormalPosition.left;
wndheight = wndpl.rcNormalPosition.bottom-wndpl.rcNormalPosition.top;
}
// Move window
if (pt.x < mon.left+2*AERO_THRESHOLD && pt.y < mon.top+2*AERO_THRESHOLD) {
// Top left
state.wndentry->restore = 1;
wndwidth = (mon.right-mon.left)/2;
wndheight = (mon.bottom-mon.top)/2;
posx = mon.left;
posy = mon.top;
}
else if (mon.right-2*AERO_THRESHOLD < pt.x && pt.y < mon.top+2*AERO_THRESHOLD) {
// Top right
state.wndentry->restore = 1;
wndwidth = max(min((mon.right-mon.left)/2, state.mmi.ptMaxTrackSize.x), state.mmi.ptMinTrackSize.x);
wndheight = (mon.bottom-mon.top)/2;
posx = mon.right-wndwidth;
posy = mon.top;
}
else if (pt.x < mon.left+2*AERO_THRESHOLD && mon.bottom-2*AERO_THRESHOLD < pt.y) {
// Bottom left
state.wndentry->restore = 1;
wndwidth = (mon.right-mon.left)/2;
wndheight = max(min((mon.bottom-mon.top)/2, state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
posx = mon.left;
posy = mon.bottom-wndheight;
}
else if (mon.right-2*AERO_THRESHOLD < pt.x && mon.bottom-2*AERO_THRESHOLD < pt.y) {
// Bottom right
state.wndentry->restore = 1;
wndwidth = max(min((mon.right-mon.left)/2, state.mmi.ptMaxTrackSize.x), state.mmi.ptMinTrackSize.x);
wndheight = max(min((mon.bottom-mon.top)/2, state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
posx = mon.right-wndwidth;
posy = mon.bottom-wndheight;
}
else if (pt.y < mon.top+AERO_THRESHOLD && !state.mdiclient) {
// Top
if (!maximized) {
state.wndentry->restore = 0;
// Center window on monitor and maximize it
WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
GetWindowPlacement(state.hwnd, &wndpl);
wndpl.rcNormalPosition.left = fmon.left+(mon.right-mon.left)/2-state.origin.width/2;
wndpl.rcNormalPosition.top = fmon.top+(mon.bottom-mon.top)/2-state.origin.height/2;
wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left+state.origin.width;
wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top+state.origin.height;
wndpl.showCmd = SW_MAXIMIZE;
SetWindowPlacement(state.hwnd, &wndpl);
}
return;
}
else if (pt.y < mon.top+2*AERO_THRESHOLD) {
// Top
state.wndentry->restore = 1;
wndwidth = max(min((mon.right-mon.left), state.mmi.ptMaxTrackSize.x), state.mmi.ptMinTrackSize.x);
wndheight = (mon.bottom-mon.top)/2;
posx = mon.left+(mon.right-mon.left)/2-wndwidth/2; // Center horizontally (if window has a max width)
posy = mon.top;
}
else if (mon.bottom-AERO_THRESHOLD < pt.y) {
// Bottom
state.wndentry->restore = 1;
wndwidth = max(min((mon.right-mon.left), state.mmi.ptMaxTrackSize.x), state.mmi.ptMinTrackSize.x);
wndheight = max(min((mon.bottom-mon.top)/2, state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
posx = mon.left+(mon.right-mon.left)/2-wndwidth/2; // Center horizontally (if window has a max width)
posy = mon.bottom-wndheight;
}
else if (pt.x < mon.left+AERO_THRESHOLD) {
// Left
state.wndentry->restore = 1;
wndwidth = (mon.right-mon.left)/2;
wndheight = max(min((mon.bottom-mon.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
posx = mon.left;
posy = mon.top+(mon.bottom-mon.top)/2-wndheight/2; // Center vertically (if window has a max height)
}
else if (mon.right-AERO_THRESHOLD < pt.x) {
// Right
state.wndentry->restore = 1;
wndwidth = max(min((mon.right-mon.left)/2, state.mmi.ptMaxTrackSize.x), state.mmi.ptMinTrackSize.x);
wndheight = max(min((mon.bottom-mon.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
posx = mon.right-wndwidth;
posy = mon.top+(mon.bottom-mon.top)/2-wndheight/2; // Center vertically (if window has a max height)
}
else if (state.wndentry->restore) {
// Restore original window size
state.wndentry->restore = 0;
wndwidth = state.origin.width;
wndheight = state.origin.height;
}
// Aero-move the window?
if (state.wndentry->restore) {
state.wndentry->width = state.origin.width;
state.wndentry->height = state.origin.height;
// Move
MoveWindow(state.hwnd, posx, posy, wndwidth, wndheight, TRUE);
// Get new size after move
// Doing this since wndwidth and wndheight might be wrong if the window is resized in chunks
GetWindowRect(state.hwnd, &wnd);
state.wndentry->last.width = wnd.right-wnd.left;
state.wndentry->last.height = wnd.bottom-wnd.top;
// We are done
return;
}
/*
FILE *f = OpenLog(L"wb");
int k;
for (k=0; k < NUMWNDDB; k++) {
struct wnddata *wnd = &wnddb.items[k];
fwprintf(f, L"wnd #%03d (0x%08x): restore:%d, origin:%4dx%4d, last:%4dx%4d", k, wnd->hwnd, wnd->restore, wnd->width, wnd->height, wnd->last.width, wnd->last.height);
if (wnd == wnddb.pos) {
fwprintf(f, L" <--");
}
fwprintf(f, L"\n");
}
CloseLog(f);
*/
}
// Check if the window will snap anywhere
if (sharedstate.snap) {
MoveSnap(&posx, &posy, wndwidth, wndheight);
}
}
else if (sharedstate.action == ACTION_RESIZE) {
// Clear restore flag
state.wndentry->restore = 0;
// Figure out new placement
if (state.resize.x == RESIZE_CENTER && state.resize.y == RESIZE_CENTER) {
posx = wnd.left-(pt.x-state.offset.x)-mdiclientpt.x;
posy = wnd.top-(pt.y-state.offset.y)-mdiclientpt.y;
wndwidth = wnd.right-wnd.left+2*(pt.x-state.offset.x);
wndheight = wnd.bottom-wnd.top+2*(pt.y-state.offset.y);
state.offset.x = pt.x;
state.offset.y = pt.y;
}
else {
RECT rootwnd = {0,0,0,0};
if (state.mdiclient) {
HWND root = GetAncestor(state.hwnd, GA_ROOT);
GetClientRect(root, &rootwnd);
}
if (state.resize.y == RESIZE_TOP) {
wndheight = max(min((wnd.bottom-pt.y+state.offset.y)-mdiclientpt.y, state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
posy = state.origin.bottom-wndheight;
}
else if (state.resize.y == RESIZE_CENTER) {
posy = wnd.top-rootwnd.top-mdiclientpt.y;
wndheight = wnd.bottom-wnd.top;
}
else if (state.resize.y == RESIZE_BOTTOM) {
posy = wnd.top-rootwnd.top-mdiclientpt.y;
wndheight = pt.y-posy+state.offset.y;
}
if (state.resize.x == RESIZE_LEFT) {
wndwidth = max(min((wnd.right-pt.x+state.offset.x)-mdiclientpt.x, state.mmi.ptMaxTrackSize.x), state.mmi.ptMinTrackSize.x);
posx = state.origin.right-wndwidth;
}
else if (state.resize.x == RESIZE_CENTER) {
posx = wnd.left-rootwnd.left-mdiclientpt.x;
wndwidth = wnd.right-wnd.left;
}
else if (state.resize.x == RESIZE_RIGHT) {
posx = wnd.left-rootwnd.left-mdiclientpt.x;
wndwidth = pt.x-posx+state.offset.x;
}
// Check if the window will snap anywhere
if (sharedstate.snap) {
ResizeSnap(&posx, &posy, &wndwidth, &wndheight);
}
}
}
MoveWindow(state.hwnd, posx, posy, wndwidth, wndheight, TRUE);
}
__declspec(dllexport) LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
int vkey = ((PKBDLLHOOKSTRUCT)lParam)->vkCode;
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
if (IsHotkey(vkey)) {
// Block this keypress if an we're already pressing a hotkey or an action is happening
if (state.activated && !state.alt && sharedstate.action) {
state.alt = 1;
}
if (state.activated && state.alt) {
return 1;
}
// Update state
state.alt = 1;
state.blockaltup = 0;
state.blockmouseup = 0;
state.interrupted = 0;
// Ctrl as hotkey should not trigger Ctrl-focusing when starting dragging, releasing and pressing it again will focus though
if (!sharedstate.action && (vkey == VK_LCONTROL || vkey == VK_RCONTROL)) {
state.ignorectrl = 1;
}
// Hook mouse
HookMouse();
}
else if (vkey == VK_LSHIFT || vkey == VK_RSHIFT) {
sharedstate.snap = 3;
if (!sharedstate.shift) {
sharedstate.shift = 1;
MouseMove();
}
// Block keydown to prevent Windows from changing keyboard layout
if (state.alt && sharedstate.action) {
return 1;
}
}
else if (vkey == VK_SPACE && (sharedstate.action || msgaction) && sharedstate.snap) {
sharedstate.snap = 0;
if (sharedstate.action) {
return 1;
}
}
else if (vkey == VK_ESCAPE) {
sharedstate.action = ACTION_NONE;
UnhookMouse();
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
else {
state.interrupted = 1;
}
if (sharedstate.action && (vkey == VK_LCONTROL || vkey == VK_RCONTROL) && !state.ignorectrl && !state.ctrl) {
POINT pt;
GetCursorPos(&pt);
state.locked = 0;
state.origin.maximized = 0;
state.origin.monitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
SetForegroundWindow(state.hwnd);
MouseMove();
state.ctrl = 1;
}
if (sharedsettings.FocusOnTyping && !sharedstate.action && !state.alt) {
POINT pt;
GetCursorPos(&pt);
HWND hoverwnd = WindowFromPoint(pt);
if (hoverwnd == NULL) {
return CallNextHookEx(NULL, nCode, wParam, lParam);