Skip to content

Commit 0d5ecb1

Browse files
committed
-New Xhook is fully integrated
1 parent 7a83515 commit 0d5ecb1

File tree

6 files changed

+15
-98
lines changed

6 files changed

+15
-98
lines changed

Tasker/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ set(SOURCES
5252
${CMAKE_SOURCE_DIR}/udata/UdataUtils.cpp
5353
${CMAKE_SOURCE_DIR}/udata/User.cpp
5454
${CMAKE_SOURCE_DIR}/Engine/Hooks/XHook/XHook.cpp
55-
${CMAKE_SOURCE_DIR}/Engine/Hooks/XHook/xhook_engine.cpp
5655
${CMAKE_SOURCE_DIR}/TaskerPerf/perftimer.cpp
5756
${CMAKE_SOURCE_DIR}/Ui/CommStatsQWidget.ui
5857
${CMAKE_SOURCE_DIR}/Ui/CreateCommitmentQWidget.ui
@@ -80,7 +79,6 @@ set(HEADERS
8079
${CMAKE_SOURCE_DIR}/udata/UdataUtils.h
8180
${CMAKE_SOURCE_DIR}/udata/User.h
8281
${CMAKE_SOURCE_DIR}/Engine/Hooks/XHook/XHook.h
83-
${CMAKE_SOURCE_DIR}/Engine/Hooks/XHook/xhook_engine.h
8482
TaskerPerf/perftimer.h)
8583

8684
add_executable(Tasker build_libuiohook ${SOURCES} ${HEADERS})

Tasker/Engine/Hooks/XHook/XHook.cpp

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "XHook.h"
2-
#include "xhook_engine.h"
32
#include <errno.h>
43
#include <fcntl.h>
54
#include <stdio.h>
@@ -14,8 +13,6 @@
1413
#include <QtCore>
1514
using namespace Engine;
1615

17-
#include "xhook_engine.h"
18-
1916
/* libUIOHook: Cross-platfrom userland keyboard and mouse hooking.
2017
* Copyright (C) 2006-2017 Alexander Barker. All Rights Received.
2118
* https://github.com/kwhat/libuiohook/
@@ -45,7 +42,7 @@ using namespace Engine;
4542
#include <string.h>
4643
#include <uiohook.h>
4744
#include <wchar.h>
48-
static int current_mode;
45+
static XHookMode current_mode;
4946

5047
static Engine::Hook::HookState currentState;
5148
static std::unique_ptr<QMutex> lock = std::make_unique<QMutex>();
@@ -84,15 +81,12 @@ bool logger_proc(unsigned int level, const char *format, ...) {
8481
// thread.
8582
void dispatch_proc(uiohook_event *const event) {
8683
QMutexLocker locker{lock.get()};
87-
printf("\ndispatch_proc1\n");
88-
printf("current mode:%d\n", current_mode);
8984
switch (event->type) {
9085
case EVENT_KEY_PRESSED:
9186
case EVENT_KEY_RELEASED:
9287
case EVENT_KEY_TYPED:
93-
printf("event->type:%d", event->type);
94-
if (current_mode == IOHOOK_KEYBOARD_MODE ||
95-
current_mode == IOHOOK_KEYBOARD_AND_MOUSE_MODE) {
88+
if (current_mode == XHookMode::KEYBOARD ||
89+
current_mode == XHookMode::MOUSE_AND_KEYBOARD) {
9690
// printf("%d\n", IOHOOK_KEYBOARD_MODE);
9791
currentState = Engine::Hook::HookState::productive;
9892
}
@@ -103,18 +97,17 @@ void dispatch_proc(uiohook_event *const event) {
10397
case EVENT_MOUSE_MOVED:
10498
case EVENT_MOUSE_DRAGGED:
10599
case EVENT_MOUSE_WHEEL:
106-
if (current_mode == IOHOOK_MOUSE_MODE ||
107-
current_mode == IOHOOK_KEYBOARD_AND_MOUSE_MODE) {
100+
if (current_mode == XHookMode::MOUSE ||
101+
current_mode == XHookMode::MOUSE_AND_KEYBOARD) {
108102
currentState = Engine::Hook::HookState::productive;
109103
}
110104
break;
111105
default:
112106
break;
113107
}
114-
printf("dispatch_proc2\n");
115108
}
116109

117-
int Engine::run_xhook_engine(int mode) {
110+
int Engine::run_xhook_engine(XHookMode mode) {
118111
// We need to disable buffering in order for this process to send the
119112
// "readyReadStandardOutput" signal to Tasker
120113
setbuf(stdout, NULL);
@@ -216,26 +209,6 @@ int Engine::run_xhook_engine(int mode) {
216209
*/
217210
XHook::XHook(Engine::XHookMode newXMode) {
218211
XMode = newXMode;
219-
xChildHook.setParent(this);
220-
// switch (XMode) {
221-
// case XHookMode::MOUSE_AND_KEYBOARD:
222-
// xChildHookArguments << IOHOOK_KEYBOARD_AND_MOUSE_MODE;
223-
// break;
224-
// case XHookMode::MOUSE:
225-
// xChildHookArguments << IOHOOK_MOUSE_MODE;
226-
// break;
227-
// case XHookMode::KEYBOARD:
228-
// xChildHookArguments << IOHOOK_KEYBOARD_MODE;
229-
// break;
230-
// }
231-
connect(&xChildHook, &QProcess::readyReadStandardOutput, this,
232-
&XHook::update);
233-
connect(&xChildHook, &QProcess::errorOccurred, this,
234-
[=](QProcess::ProcessError e) { qDebug() << "error code=" << e; });
235-
connect(&xChildHook, &QProcess::readyReadStandardError, this, [=]() {
236-
QByteArray errorData = this->xChildHook.readAllStandardError();
237-
qDebug() << "Error on Xhook process:" << QString{errorData};
238-
});
239212
currentState = state;
240213
}
241214
/**
@@ -244,28 +217,16 @@ XHook::XHook(Engine::XHookMode newXMode) {
244217
* process.
245218
* @param newXMode The mode(mouse, keyboard or both) this instance will be on.
246219
*/
247-
XHook::XHook() {
248-
XMode = XHookMode::MOUSE_AND_KEYBOARD;
249-
xChildHook.setParent(this);
250-
// xChildHookArguments << IOHOOK_KEYBOARD_AND_MOUSE_MODE;
251-
connect(&xChildHook, &QProcess::readyReadStandardOutput, this,
252-
&XHook::update);
253-
connect(&xChildHook, &QProcess::errorOccurred, this,
254-
[=](QProcess::ProcessError e) { qDebug() << "error code=" << e; });
255-
connect(&xChildHook, &QProcess::readyReadStandardError, this, [=]() {
256-
QByteArray errorData = this->xChildHook.readAllStandardError();
257-
qDebug() << "Error on Xhook process:" << QString{errorData};
258-
});
259-
}
220+
XHook::XHook() { XMode = XHookMode::MOUSE_AND_KEYBOARD; }
260221
/**
261222
* @brief XHook::start
262223
*/
263224
void XHook::start() { startXHook(); }
264225

265226
/**
266-
* @brief XHook::end kills the XListenerHook process.
227+
* @brief XHook::end kills the keyboard/mouse hook.
267228
*/
268-
void XHook::end() { xChildHook.kill(); }
229+
void XHook::end() { hook_stop(); }
269230

270231
/**
271232
* @brief XHook::pause Does nothing.
@@ -278,24 +239,7 @@ void XHook::pause() {}
278239
* It's essentially IPC.
279240
*
280241
*/
281-
void XHook::update() {
282-
// current_mode = 12;
283-
return;
284-
QByteArray Xdata = xChildHook.readAllStandardOutput();
285-
qDebug() << "update for Xhook...";
286-
Xdata.chop(Xdata.length() - 1);
287-
QString iohookState{Xdata};
288-
if (XMode == XHookMode::MOUSE_AND_KEYBOARD &&
289-
iohookState == IOHOOK_KEYBOARD_AND_MOUSE_MODE) {
290-
setState(HookState::productive);
291-
} else if (XMode == XHookMode::MOUSE && iohookState == IOHOOK_MOUSE_MODE) {
292-
setState(HookState::productive);
293-
} else if (XMode == XHookMode::KEYBOARD &&
294-
iohookState == IOHOOK_KEYBOARD_MODE) {
295-
setState(HookState::productive);
296-
} else {
297-
}
298-
}
242+
void XHook::update() { return; }
299243

300244
/**
301245
* @brief XHook::startHook
@@ -311,20 +255,17 @@ void XHook::resetState() { setState(HookState::unproductive); }
311255
*/
312256
int XHook::startXHook() {
313257
setState(HookState::unproductive);
314-
printf("current val:%d\n", current_mode);
315258

316-
run_xhook_engine(IOHOOK_KEYBOARD_MODE);
259+
run_xhook_engine(XMode);
317260

318261
return 0;
319262
}
320263
void XHook::setState(HookState newState) {
321264
QMutexLocker locker{lock.get()};
322-
printf("\nsetState1\n");
323265
currentState = newState;
324266
state = currentState;
325267

326268
Hook::setState(state);
327-
printf("setState2\n");
328269
}
329270
/**
330271
* @brief Engine::Engine::Hook::getState

Tasker/Engine/Hooks/XHook/XHook.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
#endif
2929
#endif
3030
namespace Engine {
31+
enum class XHookMode { MOUSE, KEYBOARD, MOUSE_AND_KEYBOARD };
32+
int run_xhook_engine(XHookMode mode);
3133
class XHook;
3234
enum class XHookMode;
3335
} // namespace Engine
3436

35-
enum class Engine::XHookMode { MOUSE, KEYBOARD, MOUSE_AND_KEYBOARD };
3637
/**
3738
* @brief The Engine::XListener class
3839
* This class has the capiblity of detecting mouse and keyboard
@@ -60,8 +61,6 @@ public slots:
6061
virtual void resetState() override;
6162

6263
private:
63-
QProcess xChildHook;
64-
QStringList xChildHookArguments;
6564
XHookMode XMode;
6665
signals:
6766
void signalThread();

Tasker/Engine/Hooks/XHook/xhook_engine.cpp

Whitespace-only changes.

Tasker/Engine/Hooks/XHook/xhook_engine.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

Tasker/Engine/Timer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void Timer::startTimer() {
5050
hook = std::make_unique<XHook>();
5151

5252
} else if (hookType == Hook::HookType::X_MOUSE) {
53+
std::cout << "mouse type..";
5354
hook = std::make_unique<XHook>(XHookMode::MOUSE);
5455
} else if (hookType == Hook::HookType::X_KEYBOARD) {
5556
hook = std::make_unique<XHook>(XHookMode::KEYBOARD);
@@ -165,6 +166,7 @@ void Timer::unProductiveSlot() { unProductiveSignalCount++; }
165166
*/
166167
void Timer::stopTimerSlot() {
167168
emit congrats();
169+
hook->end();
168170
reset();
169171
timer->stop();
170172
}

0 commit comments

Comments
 (0)