Rabbit Remote Control 0.1.0-bate16
Loading...
Searching...
No Matches
Hook.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QDesktopServices>
4#include <QLoggingCategory>
5#include <QApplication>
6#include <QKeyEvent>
7#include "Hook.h"
8#if defined(Q_OS_WIN)
9 #include "Windows/HookWindows.h"
10#elif defined(Q_OS_ANDROID)
11#elif defined(Q_OS_MACOS)
12#elif defined(Q_OS_LINUX)
13 #include "Unix/NativeEventFilterUnix.h"
14#endif
15#include "RabbitCommonTools.h"
16
17QAbstractNativeEventFilter* g_pNativeEventFilter = nullptr;
18static Q_LOGGING_CATEGORY(log, "Plugin.Hook")
19
20CHook::CHook(CParameterPlugin *pPara, QObject *parent)
21 : QObject(parent)
22 , m_pParameterPlugin(pPara)
23 , m_bScript(false)
24{
25 qDebug(log) << "CHook::~CHook()";
26}
27
28CHook::~CHook()
29{
30 qDebug(log) << "CHook::~CHook()";
31}
32
33CHook* CHook::GetHook(CParameterPlugin *pPara, QObject *parent)
34{
35 CHook* p = nullptr;
36#if defined(Q_OS_WIN)
37 p = new CHookWindows(pPara, parent);
38#elif defined(Q_OS_LINUX)
39 p = new CHook(pPara, parent);
40#endif
41 return p;
42}
43
44int CHook::RegisterKeyboard()
45{
46 OnRegisterKeyboard();
47
48 if(m_pParameterPlugin->GetDesktopShortcutsScript()
49 && !m_pParameterPlugin->GetRestoreDesktopShortcutsScript().isEmpty()
50 && !m_pParameterPlugin->GetDisableDesktopShortcutsScript().isEmpty()) {
51#if defined(Q_OS_WIN32)
52 QDesktopServices::openUrl(m_pParameterPlugin->GetDisableDesktopShortcutsScript());
53#else
54 RunCommand(m_pParameterPlugin->GetDisableDesktopShortcutsScript());
55#endif
56 m_bScript = true;
57 } else {
58 OnDisableDesktopShortcuts();
59 m_bScript = false;
60 }
61 return 0;
62}
63
64int CHook::UnRegisterKeyboard()
65{
66 if(m_bScript) {
67#if defined(Q_OS_WIN32)
68 QDesktopServices::openUrl(m_pParameterPlugin->GetRestoreDesktopShortcutsScript());
69#else
70 RunCommand(m_pParameterPlugin->GetRestoreDesktopShortcutsScript());
71#endif
72 } else {
73 OnRestoreDesktopShortcuts();
74 }
75
76 OnUnRegisterKeyboard();
77 return 0;
78}
79
80int CHook::OnRegisterKeyboard()
81{
82#if defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_ANDROID)
83 if(-1 == QGuiApplication::platformName().indexOf(QRegularExpression("wayland.*"))) {
84 if(!g_pNativeEventFilter)
85 g_pNativeEventFilter = new CNativeEventFilterUnix(m_pParameterPlugin);
86 if(g_pNativeEventFilter)
87 qApp->installNativeEventFilter(g_pNativeEventFilter);
88 } else
89 qApp->installEventFilter(this);
90#else
91 qApp->installEventFilter(this);
92#endif
93 return 0;
94}
95
96int CHook::OnUnRegisterKeyboard()
97{
98 if(g_pNativeEventFilter) {
99 qApp->removeNativeEventFilter(g_pNativeEventFilter);
100 delete g_pNativeEventFilter;
101 g_pNativeEventFilter = nullptr;
102 }
103
104 if(m_pParameterPlugin)
105 qApp->removeEventFilter(this);
106
107 return 0;
108}
109
110int CHook::OnDisableDesktopShortcuts()
111{
112#if defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_ANDROID)
113 m_DesktopShortcutsManager.disableAllShortcuts();
114#endif
115 return 0;
116}
117
118int CHook::OnRestoreDesktopShortcuts()
119{
120#if defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_ANDROID)
121 m_DesktopShortcutsManager.restoreAllShortcuts();
122#endif
123 return 0;
124}
125
126bool CHook::eventFilter(QObject *watched, QEvent *event)
127{
128 bool bRet = false;
129 if(QEvent::KeyPress == event->type() || QEvent::KeyRelease == event->type())
130 {
131 if(m_pParameterPlugin && m_pParameterPlugin->GetCaptureAllKeyboard()) {
132 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
133
134 CFrmViewer* focus = qobject_cast<CFrmViewer*>(QApplication::focusWidget());
135 qDebug(log) << "eventFilter:" << keyEvent
136 << watched << focus;
137 if(focus) {
138 if(focus == watched) {
139 return false;
140 }
141
142 /*
143 QKeyEvent* ke = new QKeyEvent(
144 keyEvent->type(), keyEvent->key(),
145 keyEvent->modifiers(), keyEvent->text());
146 QApplication::postEvent(focus, ke);
147 return true;//*/
148 switch(keyEvent->type())
149 {
150 case QKeyEvent::KeyPress:
151 emit focus->sigKeyPressEvent(keyEvent);
152 bRet = true;
153 break;
154 case QKeyEvent::KeyRelease:
155 emit focus->sigKeyReleaseEvent(keyEvent);
156 bRet = true;
157 break;
158 default:
159 break;
160 }
161 }
162 }
163 }
164 return bRet;
165}
166
167bool CHook::RunCommand(const QString &program, const QStringList &args, int timeout)
168{
169 QProcess process;
170 process.setProgram(program);
171 if(!args.isEmpty())
172 process.setArguments(args);
173
174 //qDebug(log) << "Command:" << program << args;
175
176 process.start();
177
178 if (!process.waitForFinished(timeout)) {
179 qWarning(log) << "Command timeout:" << program << args;
180 return false;
181 }
182
183 if (process.exitCode() != 0) {
184 QString errorOutput = process.readAllStandardError();
185 qWarning(log) << "Command failed:" << program << args
186 << "exit code:" << process.exitCode()
187 << "failed:" << errorOutput;
188 return false;
189 }
190
191 QString standardOutput = process.readAllStandardOutput();
192 if (!standardOutput.isEmpty()) {
193 qDebug(log) << "Command output:" << standardOutput.trimmed();
194 }
195
196 return true;
197}
The class is the HOOK abstract class.
Definition Hook.h:15
Global parameters of plugins.