-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_catcher_thread.cpp
255 lines (221 loc) · 5.8 KB
/
mouse_catcher_thread.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
/* Copyright (C) 2016 Alik Aslanyan <[email protected]>
* This file is part of DoubleClickFix.
*
* DoubleClickFix 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.
*
* DoubleClickFix 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 DoubleClickFix. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mouse_catcher_thread.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++" // TODO: Refactor
#include <QMessageBox>
#include <QTimer>
#if defined QT_DEBUG
#include <QDebug>
#endif
#pragma GCC diagnostic pop
MouseCatcherThread *MouseCatcherThread::m_This;
#define BLOCK_CALL 1
MouseCatcherThread::MouseCatcherThread() :
QThread(),
m_Hook(),
m_ClickTimer(),
m_isStarted(false),
m_isLoggingStarted(false),
m_Delay(1),
m_isSelectionMode(false),
m_isMouseHoldAutoSelectionMode(false),
m_MouseHoldingDelay(1),
m_iToggled(false)
{
MouseCatcherThread::m_This = this;
m_ClickTimer.start();
setObjectName("MouseCatcherThread");
iBlockMouseButtonUps[0] = iBlockMouseButtonUps[1] = 0;
iBlockMouseButtonDowns[0] = iBlockMouseButtonDowns[1] = 0;
}
MouseCatcherThread *MouseCatcherThread::getInstance()
{
if(!m_This) m_This = new MouseCatcherThread();
return m_This;
}
MouseCatcherThread::~MouseCatcherThread()
{
UnhookWindowsHookEx(m_Hook);
m_This = nullptr;
m_Hook = nullptr;
}
#if defined QT_DEBUG
#include <QTimer>
#endif
LRESULT inline MouseCatcherThread::MouseCallBackProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode != HC_ACTION) return CallNextHookEx(m_Hook, nCode, wParam, lParam);
switch(wParam)
{
case WM_MOUSEMOVE:
{
return CallNextHookEx(m_Hook, nCode, wParam, lParam);
break;
}
case WM_LBUTTONDOWN: case WM_RBUTTONDOWN:
{
if(isStarted())
{
bool isRight = wParam==WM_RBUTTONDOWN;
int iMSecs = m_ClickTimer.elapsed();
m_ClickTimer.restart();
bool needLog = isLoggingStarted();
if(getDelay()>=iMSecs)
{
iBlockMouseButtonUps[(int)isRight]++;
if(needLog) emit delay(iMSecs, true, isRight);
return BLOCK_CALL;
}
else if(needLog)
{
emit delay(iMSecs, false, isRight);
}
if(!isRight)
{
if(m_iToggled)
{
m_iToggled=false;
POINT p;
GetCursorPos(&p);
INPUT ip;
memset(&ip, 0, sizeof(INPUT));
ip.type = INPUT_MOUSE;
ip.mi.dx = p.x;
ip.mi.dy = p.y;
ip.mi.dwFlags = MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE;
SendInput(1, &ip, sizeof(INPUT));
setSelectionMode(false);
return BLOCK_CALL;
}
else if(isSelectionMode())
{
m_iToggled=true;
}
else if(isMouseHoldAutoSelectionMode())
{
QTimer *t = new QTimer();
t->setSingleShot(true);
t->setInterval(getMouseHoldingDelay());
t->moveToThread(this);
connect(this, SIGNAL(mouse_up()), t, SLOT(stop()));
connect(this, SIGNAL(mouse_up()), t, SLOT(deleteLater()));
connect(this, SIGNAL(finished()), t, SLOT(stop()));
connect(this, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, &QTimer::timeout, [this](){
setSelectionMode(true);
m_iToggled=true;
});
t->start();
}
}
}
break;
}
case WM_LBUTTONUP: case WM_RBUTTONUP:
{
if(isStarted())
{
short index = short(wParam==WM_RBUTTONUP);
if((m_iToggled || isSelectionMode()) && index==0)
{
if(iBlockMouseButtonUps[index]>0)
{
--iBlockMouseButtonUps[index];
}
return BLOCK_CALL;
}
else
{
if(iBlockMouseButtonUps[index]>0)
{
--iBlockMouseButtonUps[index];
return BLOCK_CALL;
}
else if(isMouseHoldAutoSelectionMode())
{
emit mouse_up();
}
}
break;
}
}
}
return CallNextHookEx(m_Hook, nCode, wParam, lParam);
}
double MouseCatcherThread::getMouseHoldingDelay() const
{
return m_MouseHoldingDelay;
}
void MouseCatcherThread::setMouseHoldingDelay(double MouseHoldingDelay)
{
m_MouseHoldingDelay = MouseHoldingDelay;
}
bool MouseCatcherThread::isMouseHoldAutoSelectionMode() const
{
return m_isMouseHoldAutoSelectionMode;
}
void MouseCatcherThread::setMouseHoldAutoSelectionMode(bool MouseHoldAutoSelectionMode)
{
m_isMouseHoldAutoSelectionMode = MouseHoldAutoSelectionMode;
}
bool inline MouseCatcherThread::isSelectionMode() const
{
return m_isSelectionMode;
}
void MouseCatcherThread::setSelectionMode(bool isSelectionMode)
{
m_isSelectionMode = isSelectionMode;
}
LRESULT inline MouseCatcherThread::MouseCallBackProcProxy(int nCode, WPARAM wParam, LPARAM lParam)
{
return MouseCatcherThread::m_This->MouseCallBackProc( nCode,wParam, lParam);
}
int inline MouseCatcherThread::getDelay() const
{
return m_Delay;
}
void MouseCatcherThread::setDelay(int Delay)
{
m_Delay = Delay;
}
bool inline MouseCatcherThread::isStarted() const
{
return m_isStarted;
}
void MouseCatcherThread::setStarted(bool Value)
{
m_isStarted = Value;
}
bool inline MouseCatcherThread::isLoggingStarted() const
{
return m_isLoggingStarted;
}
void MouseCatcherThread::setLoggingStarted(bool isLoggingStarted)
{
m_isLoggingStarted = isLoggingStarted;
}
void MouseCatcherThread::run()
{
m_Hook = SetWindowsHookEx(WH_MOUSE_LL,MouseCallBackProcProxy,nullptr,0);
if(m_Hook==NULL)
{
QMessageBox Mb;
Mb.critical(nullptr,"Critical Error!",(QString("Cannot set WH_MOUSE_LL hook! Error: ") + QString::number((double)GetLastError(),'f',1)));
}
else exec();
}