forked from RamonUnch/AltSnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.c
6556 lines (6095 loc) · 245 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 or later. *
* Modified By Raymond Gillibert in 2022 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "hooks.h"
static void MoveWindowAsync(HWND hwnd, int x, int y, int w, int h);
static BOOL CALLBACK EnumMonitorsProc(HMONITOR, HDC, LPRECT , LPARAM );
static LRESULT CALLBACK MenuWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
// Timer messages
#define REHOOK_TIMER WM_APP+1
#define SPEED_TIMER WM_APP+2
#define GRAB_TIMER WM_APP+3
//#define ALTUP_TIMER WM_APP+4
#define POOL_TIMER WM_APP+5
// #define NO_HOOK_LL
#define CURSOR_ONLY 66
#define NOT_MOVED 33
#define RESET_OFFSET 22
#define DRAG_WAIT 77
// Number of actions per button!
// 2 for Alt+Clikc
// +2 for Titlebar action
// +2 for Action while moving
// +2 for action while resizing
#define NACPB 8
#define STACK 0x1000
static HWND g_transhwnd[4]; // 4 windows to make a hollow window
static HWND g_timerhwnd; // For various timers
static HWND g_mchwnd; // For the Action menu messages
static HWND g_hkhwnd; // For the hotkeys message window.
static void UnhookMouse();
static void HookMouse();
static void UnhookMouseOnly();
static HWND KreateMsgWin(WNDPROC proc, const TCHAR *name, LONG_PTR userdata);
// Enumerators
enum button { BT_NONE=0, BT_LMB=0x02, BT_RMB=0x03, BT_MMB=0x04, BT_MB4=0x05
, BT_MB5=0x06, BT_MB6=0x07, BT_MB7=0x08, BT_MB8=0x09
, BT_MB9=0x0A, BT_MB10=0x0B, BT_MB11=0x0C, BT_MB12=0x0D
, BT_MB13=0x0E, BT_MB14=0x0F, BT_MB15=0x10, BT_MB16=0x11
, BT_MB17=0x12, BT_MB18=0x13, BT_MB19=0x14, BT_MB20=0x15
, BT_WHEEL=0x16, BT_HWHEEL=0x17 };
enum resizeX { RZ_XNONE=0, RZ_LEFT=1, RZ_RIGHT= 2, RZ_XCENTER=3 };
enum resizeY { RZ_YNONE=0, RZ_TOP= 1, RZ_BOTTOM=2, RZ_YCENTER=3 };
enum buttonstate {STATE_NONE, STATE_DOWN, STATE_UP};
#define BT_PROBE (1<<16)
static int init_movement_and_actions(POINT pt, HWND hwnd, enum action action, int button);
static void FinishMovement();
static void MoveTransWin(int x, int y, int w, int h);
static struct windowRR {
HWND hwnd;
int x;
int y;
int width;
int height;
UINT odpi;
UCHAR end;
UCHAR moveonly;
UCHAR maximize;
UCHAR snap;
} LastWin;
struct resizeXY {
enum resizeX x;
enum resizeY y;
};
static const struct resizeXY AUTORESIZE = {RZ_XNONE, RZ_YNONE};
// State
static struct {
struct {
POINT Min;
POINT Max;
} mmi;
POINT clickpt;
POINT prevpt;
POINT ctrlpt;
POINT shiftpt;
POINT offset;
POINT mdipt;
HWND hwnd;
HWND sclickhwnd;
HWND mdiclient;
DWORD clicktime;
short hittest;
short delta;
unsigned Speed;
HMENU unikeymenu;
volatile LONG ignorekey;
volatile LONG ignoreclick;
UCHAR alt;
UCHAR alt1;
UCHAR blockaltup;
UCHAR ctrl;
UCHAR shift;
UCHAR snap;
UCHAR altsnaponoff;
UCHAR moving;
UCHAR blockmouseup;
UCHAR fwmouseup;
UCHAR enumed;
UCHAR usezones;
UCHAR clickbutton;
UCHAR resizable;
struct {
UCHAR maximized;
UCHAR fullscreen;
RECT mon;
HMONITOR monitor;
int width;
int height;
int right;
int bottom;
UINT dpi;
} origin;
UCHAR sactiondone;
UCHAR xxbutton;
UCHAR ignorept;
enum action action;
struct resizeXY resize;
} state;
// Snap
RECT *monitors = NULL;
unsigned nummonitors = 0;
RECT *wnds = NULL;
unsigned numwnds = 0;
HWND *hwnds = NULL;
unsigned numhwnds = 0;
// Settings
#define MAXKEYS 15
static struct config {
// System settings
short dragXth;
short dragYth;
short dbclickX;
short dbclickY;
// [General]
UCHAR AutoFocus;
UCHAR AutoSnap;
UCHAR Aero;
UCHAR SmartAero;
UCHAR StickyResize;
UCHAR InactiveScroll;
UCHAR MDI;
UCHAR ResizeCenter;
UCHAR CenterFraction;
UCHAR SidesFraction;
UCHAR AVoff;
UCHAR AHoff;
UCHAR MoveTrans;
UCHAR MMMaximize;
// [Advanced]
UCHAR ResizeAll;
UCHAR FullScreen;
UCHAR BLMaximized;
UCHAR AutoRemaximize;
UCHAR SnapThreshold;
UCHAR AeroThreshold;
UCHAR KBMoveStep;
UCHAR KBMoveSStep;
UCHAR AeroTopMaximizes;
UCHAR UseCursor;
UCHAR MinAlpha;
char AlphaDelta;
char AlphaDeltaShift;
UCHAR ZoomFrac;
UCHAR ZoomFracShift;
UCHAR NumberMenuItems;
UCHAR MaxMenuWidth;
UCHAR AeroSpeedTau;
char SnapGap;
UCHAR ShiftSnaps;
UCHAR PiercingClick;
UCHAR DragSendsAltCtrl;
UCHAR TopmostIndicator;
UCHAR RCCloseMItem;
UCHAR MaxKeysNum;
UCHAR DragThreshold;
UCHAR AblockHotclick;
UCHAR MenuShowOffscreenWin;
UCHAR MenuShowEmptyLabelWin;
UCHAR IgnoreMinMaxInfo;
// [Performance]
UCHAR FullWin;
UCHAR TransWinOpacity;
UCHAR RefreshRate;
UCHAR RezTimer;
UCHAR PinRate;
UCHAR MoveRate;
UCHAR ResizeRate;
// [Input]
UCHAR TTBActions;
UCHAR KeyCombo;
UCHAR ScrollLockState;
UCHAR LongClickMove;
UCHAR UniKeyHoldMenu;
// [Zones]
UCHAR UseZones;
UCHAR ShowZonesPrevw;
UCHAR ZonesPrevwOpacity;
UCHAR ZSnapMode;
UCHAR LayoutNumber;
char InterZone;
# ifdef WIN64
UCHAR FancyZone;
#endif
// [KBShortcuts]
UCHAR UsePtWindow;
// -- -- -- -- -- -- --
UCHAR keepMousehook;
UCHAR EndSendKey; // Used to be VK_CONTROL
WORD AeroMaxSpeed;
WORD LongClickMoveDelay;
DWORD BLCapButtons;
DWORD BLUpperBorder;
int PinColor;
UCHAR Hotkeys[MAXKEYS+1];
UCHAR Shiftkeys[MAXKEYS+1];
UCHAR Hotclick[MAXKEYS+1];
UCHAR Killkey[MAXKEYS+1];
UCHAR XXButtons[MAXKEYS+1];
UCHAR ModKey[MAXKEYS+1];
UCHAR HScrollKey[MAXKEYS+1];
UCHAR ESCkeys[MAXKEYS+1];
struct {
enum action // Up to 20 BUTTONS!!!
LMB[NACPB], RMB[NACPB], MMB[NACPB], MB4[NACPB], MB5[NACPB]
, MB6[NACPB], MB7[NACPB], MB8[NACPB], MB9[NACPB], MB10[NACPB]
, MB11[NACPB], MB12[NACPB], MB13[NACPB], MB14[NACPB], MB15[NACPB]
, MB16[NACPB], MB17[NACPB], MB18[NACPB], MB19[NACPB], MB20[NACPB]
, Scroll[NACPB], HScroll[NACPB]; // Plus vertical and horizontal wheels
} Mouse;
enum action GrabWithAlt[NACPB]; // Actions without click
enum action MoveUp[NACPB]; // Actions on (long) Move Up w/o drag
enum action ResizeUp[NACPB]; // Actions on (long) Resize Up w/o drag
UCHAR *inputSequences[AC_SHRTF-AC_SHRT0]; // 36
} conf;
struct OptionListItem {
const char *name; int def;
};
// [General]
static const struct OptionListItem General_uchars[] = {
{ "AutoFocus", 0 },
{ "AutoSnap", 0 },
{ "Aero", 1 },
{ "SmartAero", 1 },
{ "StickyResize", 0 },
{ "InactiveScroll", 0 },
{ "MDI", 0 },
{ "ResizeCenter", 1 },
{ "CenterFraction", 24 },
{ "SidesFraction", 255 },
{ "AeroHoffset", 50 },
{ "AeroVoffset", 50 },
{ "MoveTrans", 255 },
{ "MMMaximize", 1 },
};
// [Advanced]
static const struct OptionListItem Advanced_uchars[] = {
{ "ResizeAll", 1 },
{ "FullScreen", 1 },
{ "BLMaximized", 0 },
{ "AutoRemaximize", 0 },
{ "SnapThreshold", 20 },
{ "AeroThreshold", 5 },
{ "KBMoveStep", 100 },
{ "KBMoveSStep", 10 },
{ "AeroTopMaximizes", 1 },
{ "UseCursor", 1 },
{ "MinAlpha", 32 },
{ "AlphaDelta", 64 },
{ "AlphaDeltaShift", 8 },
{ "ZoomFrac", 16 },
{ "ZoomFracShift", 64 },
{ "NumberMenuItems", 0},
{ "MaxMenuWidth", 80},
{ "AeroSpeedTau", 64 },
{ "SnapGap", 0 },
{ "ShiftSnaps", 1 },
{ "PiercingClick", 0 },
{ "DragSendsAltCtrl", 0 },
{ "TopmostIndicator", 0 },
{ "RCCloseMItem", 1 },
{ "MaxKeysNum", 0 },
{ "DragThreshold", 1 },
{ "AblockHotclick", 0 },
{ "MenuShowOffscreenWin", 0 },
{ "MenuShowEmptyLabelWin", 0 },
{ "IgnoreMinMaxInfo", 0 },
};
// [Performance]
static const struct OptionListItem Performance_uchars[] = {
{ "FullWin", 2 },
{ "TransWinOpacity", 0 },
{ "RefreshRate", 0 },
{ "RezTimer", 0 },
{ "PinRate", 32 },
{ "MoveRate", 2 },
{ "ResizeRate", 4 },
};
// [Input]
static const struct OptionListItem Input_uchars[] = {
{ "TTBActions", 0 },
{ "KeyCombo", 0 },
{ "ScrollLockState", 0 },
{ "LongClickMove", 0 },
{ "UniKeyHoldMenu", 0 },
};
// [Zones]
static const struct OptionListItem Zones_uchars[] = {
{ "UseZones", 0 },
{ "ShowZonesPrevw", 1 },
{ "ZonesPrevwOpacity", 161 },
{ "ZSnapMode", 0 },
{ "LayoutNumber", 0 },
{ "InterZone", 32 },
# ifdef WIN64
{ "FancyZone", 0 },
# endif
};
// Blacklist (dynamically allocated)
struct blacklistitem {
const TCHAR *title;
const TCHAR *classname;
const TCHAR *exename;
};
struct blacklist {
struct blacklistitem *items;
unsigned length;
TCHAR *data;
};
static struct {
struct blacklist Processes;
struct blacklist Windows;
struct blacklist Snaplist;
struct blacklist MDIs;
struct blacklist Pause;
struct blacklist MMBLower;
struct blacklist Scroll;
struct blacklist IScroll;
struct blacklist AResize;
struct blacklist SSizeMove;
struct blacklist NCHittest;
struct blacklist Bottommost;
} BlkLst;
// MUST MATCH THE ABOVE!!!
static const char *BlackListStrings[] = {
"Processes", // Max length is 15 char + NULL
"Windows",
"Snaplist",
"MDIs",
"Pause",
"MMBLower",
"Scroll",
"IScroll",
"AResize",
"SSizeMove",
"NCHittest",
"Bottommost"
};
// Cursor data
HWND g_mainhwnd = NULL;
// Hook data
HINSTANCE hinstDLL = NULL;
HHOOK mousehook = NULL;
#define FixDWMRect(hwnd, rect) FixDWMRectLL(hwnd, rect, conf.SnapGap)
#undef GetWindowRectL
#define GetWindowRectL(hwnd, rect) GetWindowRectLL(hwnd, rect, conf.SnapGap)
// To clamp width and height of windows
static pure int CLAMPW(int width) { return CLAMP(state.mmi.Min.x, width, state.mmi.Max.x); }
static pure int CLAMPH(int height) { return CLAMP(state.mmi.Min.y, height, state.mmi.Max.y); }
static pure int ISCLAMPEDW(int x) { return state.mmi.Min.x <= x && x <= state.mmi.Max.x; }
static pure int ISCLAMPEDH(int y) { return state.mmi.Min.y <= y && y <= state.mmi.Max.y; }
/* If pt and ptt are it is the same points with 4px tolerence */
static xpure int IsSamePTT(const POINT *pt, const POINT *ptt)
{
const short Tx = conf.dbclickX;
const short Ty = conf.dbclickY;
return !( pt->x > ptt->x+Tx || pt->y > ptt->y+Ty || pt->x < ptt->x-Tx || pt->y < ptt->y-Ty );
}
static xpure int IsPtDragOut(const POINT *pt, const POINT *ptt)
{
const short Tx = conf.dragXth;
const short Ty = conf.dragYth;
return !( pt->x > ptt->x+Tx || pt->y > ptt->y+Ty || pt->x < ptt->x-Tx || pt->y < ptt->y-Ty );
}
// Specific includes
#include "snap.c"
#include "zones.c"
/////////////////////////////////////////////////////////////////////////////
// Wether a window is present or not in a blacklist
static pure int blacklisted(HWND hwnd, const struct blacklist *list)
{
// Null hwnd or empty list
if (!hwnd || !list->length || !list->items)
return 0;
// If the first element is *:*|* (NULL:NULL|NULL)then we are in whitelist mode
// mode = 1 => blacklist, mode = 0 => whitelist;
UCHAR mode = list->items[0].classname
|| list->items[0].title
|| list->items[0].exename;
unsigned i = !mode; // Skip the first item...
TCHAR title[256], classname[256];
TCHAR exename[MAX_PATH];
title[0] = classname[0] = exename[0] = '\0';
GetWindowText(hwnd, title, ARR_SZ(title));
GetClassName(hwnd, classname, ARR_SZ(classname));
GetWindowProgName(hwnd, exename, ARR_SZ(exename));
for ( ; i < list->length; i++) {
if (!lstrcmp_star(classname, list->items[i].classname)
&& !lstrcmp_star(title, list->items[i].title)
&& (!list->items[i].exename || !lstrcmpi(exename, list->items[i].exename))
) {
return mode;
}
}
return !mode;
}
static int isClassName(HWND hwnd, const TCHAR *str)
{
TCHAR classname[256];
return GetClassName(hwnd, classname, ARR_SZ(classname))
&& !lstrcmp(classname, str);
}
/////////////////////////////////////////////////////////////////////////////
// The second bit (&2) will always correspond to the WS_THICKFRAME flag
static int pure IsResizable(HWND hwnd)
{
int thickf = !!(GetWindowLongPtr(hwnd, GWL_STYLE)&WS_THICKFRAME);
int ret = conf.ResizeAll // bit two is the real thickframe state.
| thickf | (thickf<<1);
if (!ret) ret = !!GetBorderlessFlag(hwnd);
if (!ret) ret = !!blacklisted(hwnd, &BlkLst.AResize); // Always resize list
return ret;
}
static HMONITOR GetMonitorInfoFromWin(HWND hwnd, MONITORINFO *mi)
{
mi->cbSize = sizeof(MONITORINFO);
HMONITOR hmon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
GetMonitorInfo(hmon, mi);
return hmon;
}
/////////////////////////////////////////////////////////////////////////////
static BOOL IsFullscreen(HWND hwnd)
{
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
if ((style&WS_CAPTION) != WS_CAPTION) {
RECT rc;
if (GetWindowRect(hwnd, &rc)) {
MONITORINFO mi;
GetMonitorInfoFromWin(hwnd, &mi);
return EqualRect(&rc, &mi.rcMonitor);
}
}
return FALSE;
}
static int IsFullscreenF(HWND hwnd, const RECT *wnd, const RECT *fmon)
{
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
// No caption and fullscreen window
return ((style&WS_CAPTION) != WS_CAPTION)
&& EqualRect(wnd, fmon);
}
static int IsFullScreenBL(HWND hwnd)
{
return conf.FullScreen && IsFullscreen(hwnd);
}
/////////////////////////////////////////////////////////////////////////////
// WM_ENTERSIZEMOVE or WM_EXITSIZEMOVE...
static void SendSizeMove(DWORD msg)
{
// LockWindowUpdate(WM_ENTERSIZEMOVE?state.hwnd:NULL);
// Don't send WM_ENTER/EXIT SIZEMOVE if the window is in SSizeMove BL
if(!blacklisted(state.hwnd, &BlkLst.SSizeMove)) {
PostMessage(state.hwnd, msg, 0, 0);
}
}
/////////////////////////////////////////////////////////////////////////////
// Overloading of the Hittest function to include a whitelist
// x and y are in screen coordinate.
static int HitTestTimeoutbl(HWND hwnd, POINT pt)
{
DorQWORD area=0;
// Try first with the ancestor window for some buggy AppX?
HWND ancestor = GetAncestor(hwnd, GA_ROOT);
if (blacklisted(ancestor, &BlkLst.MMBLower)) return 0;
if (hwnd != ancestor
&& blacklisted(ancestor, &BlkLst.NCHittest)) {
SendMessageTimeout(ancestor, WM_NCHITTEST, 0, MAKELPARAM(pt.x, pt.y), SMTO_NORMAL, 200, &area);
if(area == HTCAPTION) goto DOUBLECHECK_CAPTION;
}
area = HitTestTimeoutL(hwnd, MAKELPARAM(pt.x, pt.y));
DOUBLECHECK_CAPTION:
if (area == HTCAPTION) {
// Double check that we are not inside one of the
// caption buttons buttons because of buggy Win10..
RECT buttonRc;
if (GetCaptionButtonsRect(ancestor, &buttonRc) && PtInRect(&buttonRc, pt)) {
// let us assume it is the minimize button, it makes no sence
// But Windows is too buggy
area = HTMINBUTTON;
}
}
return area;
}
/////////////////////////////////////////////////////////////////////////////
//
static void GetMinMaxInfo(HWND hwnd, POINT *Min, POINT *Max)
{
GetMinMaxInfoF(hwnd, Min, Max, conf.IgnoreMinMaxInfo);
}
/////////////////////////////////////////////////////////////////////////////
// Use NULL to restore old transparency.
// Set to -1 to clear old state
static void SetWindowTrans(HWND hwnd)
{
static BYTE oldtrans;
static HWND oldhwnd;
if (conf.MoveTrans == 0 || conf.MoveTrans == 255) return;
if (oldhwnd == hwnd) return; // Nothing to do
if ((DorQWORD)hwnd == (DorQWORD)(-1)) {
oldhwnd = NULL;
oldtrans = 0;
return;
}
if (hwnd && !oldtrans) {
oldhwnd = hwnd;
LONG_PTR exstyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
if (exstyle&WS_EX_LAYERED) {
BYTE ctrans=0;
if(GetLayeredWindowAttributes(hwnd, NULL, &ctrans, NULL))
if(ctrans) oldtrans = ctrans;
} else {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, exstyle|WS_EX_LAYERED);
oldtrans = 255;
}
SetLayeredWindowAttributes(hwnd, 0, conf.MoveTrans, LWA_ALPHA);
} else if (!hwnd && oldhwnd) { // restore old trans;
LONG_PTR exstyle = GetWindowLongPtr(oldhwnd, GWL_EXSTYLE);
if (!oldtrans || oldtrans == 255) {
SetWindowLongPtr(oldhwnd, GWL_EXSTYLE, exstyle & ~WS_EX_LAYERED);
} else {
SetLayeredWindowAttributes(oldhwnd, 0, oldtrans, LWA_ALPHA);
}
oldhwnd = NULL;
oldtrans = 0;
}
}
static void *GetEnoughSpace(void *ptr, unsigned num, unsigned *alloc, size_t size)
{
if (num >= *alloc) {
void *nptr = realloc(ptr, (*alloc+4)*size);
if (!nptr) { free(ptr); *alloc=0; return NULL; }
ptr = nptr;
if(ptr) *alloc = (*alloc+4); // Realloc succeeded, increase count.
else *alloc = 0;
}
return ptr;
}
/////////////////////////////////////////////////////////////////////////////
// Enumerate callback proc
unsigned monitors_alloc = 0;
BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
// Make sure we have enough space allocated
monitors = (RECT *)GetEnoughSpace(monitors, nummonitors, &monitors_alloc, sizeof(RECT));
if (!monitors) return FALSE; // Stop enum, we failed
// Add monitor
MONITORINFO mi; mi.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(hMonitor, &mi);
CopyRect(&monitors[nummonitors++], &mi.rcWork); //*lprcMonitor;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
static void OffsetRectMDI(RECT *wnd)
{
OffsetRect(wnd, -state.mdipt.x, -state.mdipt.y);
}
static int ShouldSnapTo(HWND hwnd)
{
LONG_PTR style;
return hwnd != state.hwnd
&& IsVisible(hwnd)
&& !IsIconic(hwnd)
&& !(GetWindowLongPtr(hwnd, GWL_EXSTYLE)&WS_EX_NOACTIVATE) // != WS_EX_NOACTIVATE
&&( ((style=GetWindowLongPtr(hwnd, GWL_STYLE))&WS_CAPTION) == WS_CAPTION
|| (style&WS_THICKFRAME)
|| GetBorderlessFlag(hwnd)//&(WS_THICKFRAME|WS_CAPTION)
|| blacklisted(hwnd, &BlkLst.Snaplist)
);
}
/////////////////////////////////////////////////////////////////////////////
unsigned wnds_alloc = 0;
BOOL CALLBACK EnumWindowsProc(HWND window, LPARAM lParam)
{
// Make sure we have enough space allocated
wnds = (RECT *)GetEnoughSpace(wnds, numwnds, &wnds_alloc, sizeof(RECT));
if (!wnds) return FALSE; // Stop enum, we failed
// Only store window if it's visible, not minimized to taskbar,
// not the window we are dragging and not blacklisted
RECT wnd;
if (ShouldSnapTo(window) && GetWindowRectL(window, &wnd)) {
// Maximized?
if (IsZoomed(window)) {
// Skip maximized windows in MDI clients
if (state.mdiclient) return TRUE;
// Get monitor size
MONITORINFO mi;
GetMonitorInfoFromWin(window, &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)
CropRect(&wnd, &mi.rcWork);
}
OffsetRectMDI(&wnd);
// Return if this window is overlapped by another window
unsigned i;
for (i=0; i < numwnds; i++) {
if (RectInRect(&wnds[i], &wnd)) {
return TRUE;
}
}
// Add window to wnds db
CopyRect(&wnds[numwnds++], &wnd);
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// snapped windows database.
struct snwdata {
RECT wnd;
HWND hwnd;
unsigned flag;
};
struct snwdata *snwnds;
unsigned numsnwnds = 0;
unsigned snwnds_alloc = 0;
BOOL CALLBACK EnumSnappedWindows(HWND hwnd, LPARAM lParam)
{
// Make sure we have enough space allocated
snwnds = (struct snwdata *)GetEnoughSpace(snwnds, numsnwnds, &snwnds_alloc, sizeof(struct snwdata));
if (!snwnds) return FALSE; // Stop enum, we failed
RECT wnd;
if (ShouldSnapTo(hwnd)
&& !IsZoomed(hwnd)
&& GetWindowRectL(hwnd, &wnd)) {
unsigned restore;
if (conf.SmartAero&2 || IsWindowSnapped(hwnd)) {
// In SMARTER snapping mode or if the WINDOW IS SNAPPED
// We only consider the position of the window
// to determine its snapping state
MONITORINFO mi;
GetMonitorInfoFromWin(hwnd, &mi);
snwnds[numsnwnds].flag = WhichSideRectInRect(&mi.rcWork, &wnd);
} else if ((restore = GetRestoreFlag(hwnd)) && restore&SNAPPED && restore&SNAPPEDSIDE) {
// The window was AltSnapped...
snwnds[numsnwnds].flag = restore;
} else {
// thiw window is not snapped.
return TRUE; // next hwnd
}
// Add the window to the list
OffsetRectMDI(&wnd);
CopyRect(&snwnds[numsnwnds].wnd, &wnd);
snwnds[numsnwnds].hwnd = hwnd;
numsnwnds++;
}
return TRUE;
}
// If lParam is set to 1 then only windows that are
// touching the current window will be considered.
static void EnumSnapped()
{
numsnwnds = 0;
if (conf.SmartAero&1) {
if(state.mdiclient)
EnumChildWindows(state.mdiclient, EnumSnappedWindows, 0);
else
EnumDesktopWindows(NULL, EnumSnappedWindows, 0);
}
}
/////////////////////////////////////////////////////////////////////////////
// Uses the same DB than snapped windows db because they will never
// be used together Enum() vs EnumSnapped()
BOOL CALLBACK EnumTouchingWindows(HWND hwnd, LPARAM lParam)
{
// Make sure we have enough space allocated
snwnds = (struct snwdata *)GetEnoughSpace(snwnds, numsnwnds, &snwnds_alloc, sizeof(*snwnds));
if (!snwnds) return FALSE; // Stop enum, we failed
RECT wnd;
if (ShouldSnapTo(hwnd)
&& !IsZoomed(hwnd)
&& IsResizable(hwnd)
&& !blacklisted(hwnd, &BlkLst.Windows)
&& GetWindowRectL(hwnd, &wnd)) {
// Only considers windows that are
// touching the currently resized window
RECT statewnd;
GetWindowRectL(state.hwnd, &statewnd);
unsigned flag = AreRectsTouchingT(&statewnd, &wnd, conf.SnapThreshold/2);
if (flag) {
OffsetRectMDI(&wnd);
// Return if this window is overlapped by another window
unsigned i;
for (i=0; i < numsnwnds; i++) {
if (RectInRect(&snwnds[i].wnd, &wnd)) {
return TRUE;
}
}
CopyRect(&snwnds[numsnwnds].wnd, &wnd);
snwnds[numsnwnds].flag = flag;
snwnds[numsnwnds].hwnd = hwnd;
numsnwnds++;
}
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
//
static DWORD WINAPI EndDeferWindowPosThread(LPVOID hwndSS)
{
EndDeferWindowPos(hwndSS);
if (conf.RefreshRate) Sleep(conf.RefreshRate);
LastWin.hwnd = NULL;
return TRUE;
}
static void EndDeferWindowPosAsync(HDWP hwndSS)
{
DWORD lpThreadId;
CloseHandle(CreateThread(NULL, STACK, EndDeferWindowPosThread, hwndSS, STACK_SIZE_PARAM_IS_A_RESERVATION, &lpThreadId));
}
static int ShouldResizeTouching()
{
return state.action == AC_RESIZE
&& ( (conf.StickyResize&1 && state.shift)
|| ((conf.StickyResize&3)==2 && !state.shift)
);
}
static void EnumOnce(RECT **bd);
static int ResizeTouchingWindows(LPVOID lwptr)
{
if (!ShouldResizeTouching()) return 0;
RECT *bd;
EnumOnce(&bd);
if (!snwnds || !numsnwnds) return 0;
struct windowRR *lw = (struct windowRR *)lwptr;
// posx, posy, correspond to the VISIBLE rect
// of the current window...
int posx = lw->x + bd->left;
int posy = lw->y + bd->top;
int width = lw->width - (bd->left+bd->right);
int height = lw->height - (bd->top+bd->bottom);
HDWP hwndSS = NULL; // For DeferwindowPos.
if (conf.FullWin) {
hwndSS = BeginDeferWindowPos(numsnwnds+1);
}
unsigned i;
for (i=0; i < numsnwnds; i++) {
RECT *nwnd = &snwnds[i].wnd;
unsigned flag = snwnds[i].flag;
HWND hwnd = snwnds[i].hwnd;
POINT tpt;
tpt.x = (nwnd->left+nwnd->right)/2;
tpt.y = (nwnd->top+nwnd->bottom)/2 ;
if(!PtInRect(&state.origin.mon, tpt))
continue;
if (PureLeft(flag)) {
nwnd->right = posx;
} else if (PureRight(flag)) {
POINT Min, Max;
GetMinMaxInfo(hwnd, &Min, &Max);
nwnd->left = CLAMP(nwnd->right-Max.x, posx + width, nwnd->right-Min.x);
} else if (PureTop(flag)) {
nwnd->bottom = posy;
} else if (PureBottom(flag)) {
POINT Min, Max;
GetMinMaxInfo(hwnd, &Min, &Max);
nwnd->top = CLAMP(nwnd->bottom-Max.x, posy + height, nwnd->bottom-Min.x);
} else {
continue;
}
if (hwndSS) {
RECT nbd;
FixDWMRect(hwnd, &nbd);
hwndSS = DeferWindowPos(hwndSS, hwnd, NULL
, nwnd->left - nbd.left
, nwnd->top - nbd.top
, nwnd->right - nwnd->left + nbd.left + nbd.right
, nwnd->bottom - nwnd->top + nbd.top + nbd.bottom
, SWP_NOACTIVATE|SWP_NOZORDER|SWP_NOOWNERZORDER);
}
snwnds[i].flag = flag|TORESIZE;
}
if (hwndSS) {
// Draw changes ONLY if full win is ON,
hwndSS = DeferWindowPos(hwndSS, state.hwnd, NULL
, lw->x, lw->y, lw->width, lw->height
, SWP_NOACTIVATE|SWP_NOZORDER|SWP_NOOWNERZORDER);
if(hwndSS) EndDeferWindowPosAsync(hwndSS);
}
return 1;
}
/////////////////////////////////////////////////////////////////////////////
static void ResizeAllSnappedWindowsAsync()
{
if (!conf.StickyResize || !numsnwnds) return;
HDWP hwndSS = BeginDeferWindowPos(numsnwnds+1);
unsigned i;
for (i=0; i < numsnwnds; i++) {
if(hwndSS && snwnds[i].flag&TORESIZE) {
RECT bd;
FixDWMRect(snwnds[i].hwnd, &bd);
InflateRectBorder(&snwnds[i].wnd, &bd);
hwndSS = DeferWindowPos(hwndSS, snwnds[i].hwnd, NULL
, snwnds[i].wnd.left
, snwnds[i].wnd.top
, snwnds[i].wnd.right - snwnds[i].wnd.left
, snwnds[i].wnd.bottom - snwnds[i].wnd.top
, SWP_NOACTIVATE|SWP_NOZORDER|SWP_NOOWNERZORDER);
}
}
if (hwndSS)
hwndSS = DeferWindowPos(hwndSS, LastWin.hwnd, NULL
, LastWin.x, LastWin.y, LastWin.width, LastWin.height
, SWP_NOACTIVATE|SWP_NOZORDER|SWP_NOOWNERZORDER);
if(hwndSS) EndDeferWindowPosAsync(hwndSS);
LastWin.hwnd = NULL;
}
///////////////////////////////////////////////////////////////////////////
// Just used in Enum
static void EnumMdi()
{
// Make sure we have enough space allocated
monitors = (RECT *)GetEnoughSpace(monitors, nummonitors, &monitors_alloc, sizeof(RECT));
if (!monitors) return; // Fail
// Add MDIClient as the monitor
nummonitors = !!GetClientRect(state.mdiclient, &monitors[0]);
if (state.snap > 1) {
EnumChildWindows(state.mdiclient, EnumWindowsProc, 0);
}
if (conf.StickyResize) {
EnumChildWindows(state.mdiclient, EnumTouchingWindows, 0);
}
}
///////////////////////////////////////////////////////////////////////////
// Enumerate all monitors/windows/MDI depending on state.
static void Enum()
{
nummonitors = 0;
numwnds = 0;
numsnwnds = 0;
// MDI
if (state.mdiclient && IsWindow(state.mdiclient)) {
EnumMdi();
return;
}
// Enumerate monitors
EnumDisplayMonitors(NULL, NULL, EnumMonitorsProc, 0);
// Enumerate windows
if (state.snap >= 2) {
EnumDesktopWindows(NULL, EnumWindowsProc, 0);
}
if (conf.StickyResize) {
EnumDesktopWindows(NULL, EnumTouchingWindows, 0);
}
}
///////////////////////////////////////////////////////////////////////////
//
#define RECALC_INVISIBLE_BORDERS ((RECT **)1)
static void EnumOnce(RECT **bd)
{
static RECT borders;
if (bd == RECALC_INVISIBLE_BORDERS) {
FixDWMRect(state.hwnd, &borders);
return;
}
if (bd && !(state.enumed&1)) {
// LOGA("Enum");
Enum(); // Enumerate monitors and windows
FixDWMRect(state.hwnd, &borders);
state.enumed |= 1;
*bd = &borders;
} else if (bd && state.enumed) {
*bd = &borders;
}
}
static void EnumSnappedOnce()
{
if (!(state.enumed&2)) {
// LOGA("EnumSnapped");
EnumSnapped();
state.enumed |= 2;
}
}
///////////////////////////////////////////////////////////////////////////
void MoveSnap(int *_posx, int *_posy, int wndwidth, int wndheight, UCHAR pth)
{
RECT *bd;
if (!state.snap || state.Speed > conf.AeroMaxSpeed) return;
EnumOnce(&bd);
int posx = *_posx + bd->left;
int posy = *_posy + bd->top;
wndwidth -= bd->left + bd->right;
wndheight -= bd->top + bd->bottom;
// thresholdx and thresholdy will shrink to make sure
// the dragged window will snap to the closest windows
int stickx=0, sticky=0;
short thresholdx, thresholdy;
UCHAR stuckx=0, stucky=0;
thresholdx = thresholdy = pth; // conf.SnapThreshold;
// Loop monitors and windows
unsigned i, j;
for (i=0, j=0; i < nummonitors || j < numwnds; ) {
RECT snapwnd;
UCHAR snapinside=0;
// Get snapwnd
if (monitors && i < nummonitors) {
snapwnd = monitors[i];
snapinside = 1;
i++;
} else if (wnds && j < numwnds) {
snapwnd = wnds[j];
snapinside = (state.snap != 2);
j++;
} else {
// No monitors and no windows to snap to.
return;
}
// Check if posx snaps
if (IsInRangeT(posy, snapwnd.top, snapwnd.bottom, thresholdx)
|| IsInRangeT(snapwnd.top, posy, posy+wndheight, thresholdx)) {