Fixed ppt hook for linux

This commit is contained in:
WolverinDEV 2020-06-13 17:21:29 +02:00
parent fe102294ad
commit 5b54b5d130
4 changed files with 59 additions and 49 deletions

View File

@ -13,6 +13,8 @@ using namespace std;
#include "src/Win32KeyboardHook.h" #include "src/Win32KeyboardHook.h"
#else #else
#include "src/KeyboardHook.h" #include "src/KeyboardHook.h"
#include "src/X11KeyboardHook.h"
#endif #endif
@ -67,7 +69,12 @@ NAN_METHOD(UnregisterCallback) {
} }
NAN_MODULE_INIT(init) { NAN_MODULE_INIT(init) {
#ifdef WIN32
hook = make_unique<hooks::Win32RawHook>(); hook = make_unique<hooks::Win32RawHook>();
#else
hook = make_unique<hooks::X11KeyboardHook>();
#endif
if(!hook->attach()) { if(!hook->attach()) {
NAN_THROW_EXCEPTION(Error, "Failed to attach hook!"); NAN_THROW_EXCEPTION(Error, "Failed to attach hook!");
return; return;

View File

@ -4,12 +4,6 @@
#include <thread> #include <thread>
#include <map> #include <map>
//#define HOOK_X11
#if defined(HOOK_X11)
#include <X11/Xlib.h>
#endif
enum struct KeyboardHookType { enum struct KeyboardHookType {
X11, X11,
@ -53,7 +47,7 @@ class KeyboardHook {
}; };
typedef std::function<void(const std::shared_ptr<KeyEvent>& /* event */)> callback_event_t; typedef std::function<void(const std::shared_ptr<KeyEvent>& /* event */)> callback_event_t;
KeyboardHook(KeyboardHookType); explicit KeyboardHook(KeyboardHookType);
virtual ~KeyboardHook(); virtual ~KeyboardHook();
[[nodiscard]] inline KeyboardHookType type() const { return this->type_; } [[nodiscard]] inline KeyboardHookType type() const { return this->type_; }
@ -68,27 +62,6 @@ class KeyboardHook {
protected: protected:
const KeyboardHookType type_; const KeyboardHookType type_;
#if 0
#if defined(HOOK_X11)
typedef int KeyID;
Display* display = nullptr;
Window window_root = 0;
Window window_focused = 0;
int focus_revert;
long end_id = 0;
#elseif defined(HOOK_WIN32_LL)
typedef UINT KeyID;
HHOOK keyboad_hook_id{nullptr};
bool keyboard_hook_callback(int, WPARAM, LPARAM);
#ifdef USE_MOUSE_HOOK
HHOOK mouse_hook_id{nullptr};
bool mouse_hook_callback(int, WPARAM, LPARAM);
#endif
#endif
#endif
std::map<KeyID, bool> map_key; std::map<KeyID, bool> map_key;
std::map<KeyType, KeyID> map_special; std::map<KeyType, KeyID> map_special;

View File

@ -4,13 +4,13 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include "KeyboardHook.h" #include "X11KeyboardHook.h"
#define ClientMessageMask (1L << 24) #define ClientMessageMask (1L << 24)
#define SelectMask (KeyPressMask | KeyReleaseMask | FocusChangeMask | ClientMessageMask) #define SelectMask (KeyPressMask | KeyReleaseMask | FocusChangeMask | ClientMessageMask)
using namespace std; using namespace hooks;
using KeyType = KeyboardHook::KeyType::value; using KeyType = KeyboardHook::KeyType;
inline KeyType key_type(XID key_id) { inline KeyType key_type(XID key_id) {
switch (key_id) { switch (key_id) {
@ -38,15 +38,17 @@ inline KeyType key_type(XID key_id) {
return KeyType::KEY_NORMAL; return KeyType::KEY_NORMAL;
} }
KeyboardHook::KeyboardHook() { X11KeyboardHook::X11KeyboardHook() : KeyboardHook{KeyboardHookType::X11} {
this->map_special[KeyType::KEY_NORMAL] = XK_VoidSymbol; this->map_special[KeyType::KEY_NORMAL] = XK_VoidSymbol;
}
KeyboardHook::~KeyboardHook() {
if(this->active)
this->detach();
} }
bool KeyboardHook::attach() { X11KeyboardHook::~X11KeyboardHook() {
if(this->active) {
(void) this->detach();
}
}
bool X11KeyboardHook::attach() {
assert(!this->active); assert(!this->active);
this->active = true; this->active = true;
@ -73,15 +75,15 @@ bool KeyboardHook::attach() {
Serial number of failed request: 32 Serial number of failed request: 32
Current serial number in output stream: 32 Current serial number in output stream: 32
*/ */
cerr << "Caught X11 error: " << event->type << endl; std::cerr << "Caught X11 error: " << event->type << std::endl;
return 0; return 0;
}); });
this->poll_thread = thread(bind(&KeyboardHook::poll_events, this)); this->poll_thread = std::thread(std::bind(&X11KeyboardHook::poll_events, this));
return true; return true;
} }
void KeyboardHook::detach() { void X11KeyboardHook::detach() {
assert(this->active); assert(this->active);
this->active = false; this->active = false;
@ -105,7 +107,7 @@ void KeyboardHook::detach() {
this->display = nullptr; this->display = nullptr;
} }
void KeyboardHook::poll_events() { void X11KeyboardHook::poll_events() {
XEvent event; XEvent event;
bool has_event = false; bool has_event = false;
@ -114,14 +116,14 @@ void KeyboardHook::poll_events() {
auto result = XNextEvent(this->display, &event); auto result = XNextEvent(this->display, &event);
if(result != 0) { if(result != 0) {
//TODO throw error //TODO throw error
cout << "XNextEvent returned invalid code: " << result << endl; std::cout << "XNextEvent returned invalid code: " << result << std::endl;
break; break;
} }
} }
has_event = false; has_event = false;
if(event.type == FocusOut) { if(event.type == FocusOut) {
cout << "Old windows lost focus. Attaching to new one" << endl; std::cout << "Old windows lost focus. Attaching to new one" << std::endl;
int result = 0; int result = 0;
if(this->window_root != this->window_focused) { if(this->window_root != this->window_focused) {
result = XSelectInput(display, this->window_focused, 0); //Release events result = XSelectInput(display, this->window_focused, 0); //Release events
@ -154,7 +156,7 @@ void KeyboardHook::poll_events() {
if(XEventsQueued(this->display, 0) > 0) { if(XEventsQueued(this->display, 0) > 0) {
auto result = XNextEvent(this->display, &event); auto result = XNextEvent(this->display, &event);
if(result != 0) { if(result != 0) {
cerr << "XNextEvent(...) => " << result << endl; std::cerr << "XNextEvent(...) => " << result << std::endl;
break; break;
} }
has_event = true; has_event = true;
@ -174,14 +176,14 @@ void KeyboardHook::poll_events() {
this->trigger_key_event(KeyEvent::RELEASE, type == KeyType::KEY_NORMAL ? XKeysymToString(key_sym) : XKeysymToString(this->map_special[KeyType::KEY_NORMAL])); this->trigger_key_event(KeyEvent::RELEASE, type == KeyType::KEY_NORMAL ? XKeysymToString(key_sym) : XKeysymToString(this->map_special[KeyType::KEY_NORMAL]));
} else if(event.type == ClientMessage) { } else if(event.type == ClientMessage) {
cout << "Got client message. End ID: " << event.xclient.data.l[0] << " <=> " << this->end_id << endl; std::cout << "Got client message. End ID: " << event.xclient.data.l[0] << " <=> " << this->end_id << std::endl;
if(event.xclient.data.l[0] == this->end_id) break; if(event.xclient.data.l[0] == this->end_id) break;
} else if(event.type == ButtonPress) { } else if(event.type == ButtonPress) {
cout << "Got button" << endl; std::cout << "Got button" << std::endl;
} else if(event.type == ButtonRelease) { } else if(event.type == ButtonRelease) {
cout << "Got button" << endl; std::cout << "Got button" << std::endl;
} else { } else {
cerr << "Got unknown event of type " << event.type << endl; std::cerr << "Got unknown event of type " << event.type << std::endl;
} }
} }
} }

View File

@ -0,0 +1,28 @@
#pragma once
#include <X11/Xlib.h>
#include "./KeyboardHook.h"
namespace hooks {
class X11KeyboardHook : public KeyboardHook {
public:
X11KeyboardHook();
~X11KeyboardHook() override;
bool attach() override;
void detach() override;
bool keytype_supported() const override { return true; }
private:
Display* display{nullptr};
Window window_root{0};
Window window_focused{0};
int focus_revert{};
long end_id{0};
bool active{false};
std::thread poll_thread;
void poll_events();
};
}