-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll.cpp
More file actions
185 lines (151 loc) · 4.27 KB
/
scroll.cpp
File metadata and controls
185 lines (151 loc) · 4.27 KB
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
#include "app.h"
#include "desktop.h"
#include "scroll.h"
// winapi
#include <algorithm>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
#include <Windows.h>
static LRESULT CALLBACK HookMouseCB(int code, WPARAM wParam, LPARAM lParam);
static void ScrollWorker();
static HHOOK hHook = nullptr;
static HANDLE qEvent = nullptr;
static std::thread threadWorker;
static bool workerRunning = false;
static std::queue<bool> qScroll;
static std::mutex qMutex;
bool ScrollInit()
{
if (workerRunning)
return false;
assert((qEvent = CreateEvent(nullptr, false, false, nullptr)) != nullptr, false);
workerRunning = true;
threadWorker = std::thread(ScrollWorker);
return (hHook = SetWindowsHookEx(WH_MOUSE_LL, HookMouseCB, nullptr, 0));
}
bool ScrollUninit()
{
if (!workerRunning)
return false;
{
std::lock_guard<std::mutex> lock(qMutex);
workerRunning = false;
}
SetEvent(qEvent);
if (threadWorker.joinable())
threadWorker.join();
CloseHandle(qEvent);
qEvent = nullptr;
return UnhookWindowsHookEx(hHook);
}
static void ScrollWorker()
{
bool hasTask, up;
do
{
WaitForSingleObject(qEvent, INFINITE);
while (true)
{
hasTask = false;
up = false;
{
std::lock_guard<std::mutex> lock(qMutex);
if (!qScroll.empty())
{
up = qScroll.front();
qScroll.pop();
hasTask = true;
}
}
if (hasTask)
HandleScroll(up);
else
break;
}
}
while (workerRunning || ([&]() {
std::lock_guard<std::mutex> lock(qMutex);
return !qScroll.empty();
}()));
}
static LRESULT CALLBACK HookMouseCB(int code, WPARAM wParam, LPARAM lParam)
{
static unsigned long long lastScrollTime = 0;
static std::vector<HWND> parents;
if (0 <= code && wParam == WM_MOUSEWHEEL)
{
unsigned long long currentTime = GetTickCount64();
if (currentTime - lastScrollTime < SCROLL_COOLDOWN)
return 1;
HWND taskbar = FindWindow("Shell_TrayWnd", nullptr);
if (!taskbar)
taskbar = FindWindow("Shell_SecondaryTrayWnd", nullptr);
if (taskbar)
{
RECT rect;
GetWindowRect(taskbar, &rect);
MSLLHOOKSTRUCT* data = (MSLLHOOKSTRUCT*)lParam;
if (PtInRect(&rect, data->pt))
{
HWND win = WindowFromPoint(data->pt);
if (win != taskbar)
{
parents.clear();
parents.reserve(16);
parents.push_back(win);
while (win = GetParent(win))
{
if (std::find(parents.begin(), parents.end(), win) != parents.end())
{
MsgDbg("HookMouseCB: Loop detected\n");
return 1;
}
if (win == taskbar)
break;
parents.push_back(win);
}
}
if (win == taskbar)
{
lastScrollTime = currentTime;
{
std::lock_guard<std::mutex> lock(qMutex);
qScroll.push(GET_WHEEL_DELTA_WPARAM(data->mouseData) > 0);
}
SetEvent(qEvent);
return 1;
}
}
}
}
return CallNextHookEx(hHook, code, wParam, lParam);
}
void HandleScroll(bool up)
{
MsgDbg("\nHandleScroll(): %s\n", up ? "up" : "down");
if (up)
{
if (NextVDesktop())
{
MsgDbg("[>] Next desktop exists, switch\n");
}
else if (WindowExist())
{
MsgDbg("[+] Create desktop\n");
CreateVDesktop();
}
return;
}
if (WindowExist())
{
MsgDbg("[<] Switch to the previous desktop\n");
SwitchVDesktop(false);
}
else
{
MsgDbg("[-] Delete desktop...\n");
DeleteVDesktop();
}
}