diff --git a/native/ppt/CMakeLists.txt b/native/ppt/CMakeLists.txt index 2c0e7c8..f2eab44 100644 --- a/native/ppt/CMakeLists.txt +++ b/native/ppt/CMakeLists.txt @@ -1,11 +1,12 @@ set(MODULE_NAME "teaclient_ppt") +set(SOURCE_FILES src/KeyboardHook.cpp) if (MSVC) set(SOURCE_FILES ${SOURCE_FILES} src/Win32KeyboardHook.cpp) add_definitions(-DUSING_UV_SHARED) else() add_definitions(-DHAVE_X11) - set(SOURCE_FILES ${SOURCE_FILES} src/X11KeyboardHook.cpp ../updater/main.cpp ../updater/config.cpp ../updater/config.h) + set(SOURCE_FILES ${SOURCE_FILES} src/X11KeyboardHook.cpp) endif() add_nodejs_module(${MODULE_NAME} binding.cc ${SOURCE_FILES}) diff --git a/native/ppt/src/KeyboardHook.cpp b/native/ppt/src/KeyboardHook.cpp new file mode 100644 index 0000000..738f480 --- /dev/null +++ b/native/ppt/src/KeyboardHook.cpp @@ -0,0 +1,17 @@ +#include "KeyboardHook.h" + +using namespace std; +void KeyboardHook::trigger_key_event(const enum KeyEvent::type& type, const std::string &key) { + if(!this->callback_event) return; + + auto event = make_shared(); + event->type = type; + event->code = key; + + event->key_alt = this->map_special[KeyType::KEY_ALT] > 0; + event->key_ctrl = this->map_special[KeyType::KEY_CTRL] > 0; + event->key_windows = this->map_special[KeyType::KEY_WIN] > 0; + event->key_shift = this->map_special[KeyType::KEY_SHIFT] > 0; + + this->callback_event(event); +} \ No newline at end of file diff --git a/native/ppt/src/Win32KeyboardHook.cpp b/native/ppt/src/Win32KeyboardHook.cpp index 82776d0..a71dd0b 100644 --- a/native/ppt/src/Win32KeyboardHook.cpp +++ b/native/ppt/src/Win32KeyboardHook.cpp @@ -15,21 +15,6 @@ KeyboardHook::~KeyboardHook() { this->detach(); } -void KeyboardHook::trigger_key_event(const enum KeyEvent::type& type, const std::string &key) { - if(!this->callback_event) return; - - auto event = make_shared(); - event->type = type; - event->code = key; - - event->key_alt = this->map_special[KeyType::KEY_ALT]; - event->key_ctrl = this->map_special[KeyType::KEY_CTRL]; - event->key_windows = this->map_special[KeyType::KEY_WIN]; - event->key_shift = this->map_special[KeyType::KEY_SHIFT]; - - this->callback_event(event); -} - bool KeyboardHook::attach() { assert(!this->_attached); this->active = true; diff --git a/native/ppt/src/X11KeyboardHook.cpp b/native/ppt/src/X11KeyboardHook.cpp index 24632ac..a6b1884 100644 --- a/native/ppt/src/X11KeyboardHook.cpp +++ b/native/ppt/src/X11KeyboardHook.cpp @@ -6,7 +6,7 @@ #include #include "KeyboardHook.h" -#define ClientMessageMask (1L<<24) +#define ClientMessageMask (1L << 24) #define SelectMask (KeyPressMask | KeyReleaseMask | FocusChangeMask | ClientMessageMask) using namespace std; @@ -63,15 +63,15 @@ bool KeyboardHook::attach() { //XGrabKeyboard(this->display, this->window_focused, false, GrabModeAsync, GrabModeAsync, CurrentTime); } - XSetErrorHandler([](Display* display, XErrorEvent* event) { + XSetErrorHandler([](Display* dp, XErrorEvent* event) { if(event->type == BadWindow) return 0; /* - * X Error of failed request: BadWindow (invalid Window parameter) - Major opcode of failed request: 2 (X_ChangeWindowAttributes) - Resource id in failed request: 0x0 - Serial number of failed request: 32 - Current serial number in output stream: 32 + X Error of failed request: BadWindow (invalid Window parameter) + Major opcode of failed request: 2 (X_ChangeWindowAttributes) + Resource id in failed request: 0x0 + Serial number of failed request: 32 + Current serial number in output stream: 32 */ cerr << "Caught X11 error: " << event->type << endl; return 0; @@ -146,40 +146,9 @@ void KeyboardHook::poll_events() { else this->map_special[type] = key_sym; - if (this->callback_event) { - if(!typed) { - auto e = make_shared(); - e->type = KeyboardHook::KeyEvent::PRESS; - if(type == KeyType::KEY_NORMAL) { - e->code = XKeysymToString(key_sym); - } else { - e->code = XKeysymToString(this->map_special[KeyType::KEY_NORMAL]); - } - - e->key_alt = this->map_special[KeyType::KEY_ALT] > 0; - e->key_ctrl = this->map_special[KeyType::KEY_CTRL] > 0; - e->key_windows = this->map_special[KeyType::KEY_WIN] > 0; - e->key_shift = this->map_special[KeyType::KEY_SHIFT] > 0; - - this->callback_event(e); - } - { - auto e = make_shared(); - e->type = KeyboardHook::KeyEvent::TYPE; - if(type == KeyType::KEY_NORMAL) { - e->code = XKeysymToString(key_sym); - } else { - e->code = XKeysymToString(this->map_special[KeyType::KEY_NORMAL]); - } - - e->key_alt = this->map_special[KeyType::KEY_ALT] > 0; - e->key_ctrl = this->map_special[KeyType::KEY_CTRL] > 0; - e->key_windows = this->map_special[KeyType::KEY_WIN] > 0; - e->key_shift = this->map_special[KeyType::KEY_SHIFT] > 0; - - this->callback_event(e); - } - } + if(!typed) + this->trigger_key_event(KeyEvent::PRESS, type == KeyType::KEY_NORMAL ? XKeysymToString(key_sym) : XKeysymToString(this->map_special[KeyType::KEY_NORMAL])); + this->trigger_key_event(KeyEvent::TYPE, type == KeyType::KEY_NORMAL ? XKeysymToString(key_sym) : XKeysymToString(this->map_special[KeyType::KEY_NORMAL])); } else if(event.type == KeyRelease) { auto key_sym = XLookupKeysym(&event.xkey, 0); if(XEventsQueued(this->display, 0) > 0) { @@ -203,25 +172,14 @@ void KeyboardHook::poll_events() { else if(this->map_special[KeyType::KEY_NORMAL] == key_sym) this->map_special[KeyType::KEY_NORMAL] = XK_VoidSymbol; - if (this->callback_event) { - auto e = make_shared(); - e->type = KeyboardHook::KeyEvent::RELEASE; - if(type == KeyType::KEY_NORMAL) { - e->code = XKeysymToString(key_sym); - } else { - e->code = XKeysymToString(this->map_special[KeyType::KEY_NORMAL]); - } - - e->key_alt = this->map_special[KeyType::KEY_ALT] > 0; - e->key_ctrl = this->map_special[KeyType::KEY_CTRL] > 0; - e->key_windows = this->map_special[KeyType::KEY_WIN] > 0; - e->key_shift = this->map_special[KeyType::KEY_SHIFT] > 0; - - this->callback_event(e); - } + 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) { cout << "Got client message. End ID: " << event.xclient.data.l[0] << " <=> " << this->end_id << endl; if(event.xclient.data.l[0] == this->end_id) break; + } else if(event.type == ButtonPress) { + cout << "Got button" << endl; + } else if(event.type == ButtonRelease) { + cout << "Got button" << endl; } else { cerr << "Got unknown event of type " << event.type << endl; } diff --git a/native/updater/main.cpp b/native/updater/main.cpp index 91f5b90..fc183b8 100644 --- a/native/updater/main.cpp +++ b/native/updater/main.cpp @@ -7,6 +7,7 @@ #include "config.h" #include "util.h" #include "file.h" + using namespace std; using namespace log_helper; @@ -81,7 +82,7 @@ static bool daemonize() { std::string log_file_path; int main(int argc, char** argv) { - srand((unsigned int) floor(chrono::system_clock::now().time_since_epoch()).count()); + srand((unsigned int) chrono::floor(chrono::system_clock::now().time_since_epoch()).count()); log_file_path = argc > 2 ? argv[1] : "update_installer.log"; //logger::info("Starting log at %s", log_file_path.c_str());