From a8b1299c4e3cc49df3f6e61fecd7c51414257c66 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sun, 23 Aug 2020 21:52:57 +0200 Subject: [PATCH] Fixed PPT exiting --- native/ppt/binding.cc | 8 ++++++++ native/ppt/src/KeyboardHook.cpp | 11 +++++------ native/ppt/src/KeyboardHook.h | 4 ++-- native/ppt/src/Win32KeyboardHook.h | 3 +++ native/ppt/src/Win32KeyboardRawInput.cpp | 8 +++++++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/native/ppt/binding.cc b/native/ppt/binding.cc index 76afce8..c8815c7 100644 --- a/native/ppt/binding.cc +++ b/native/ppt/binding.cc @@ -113,6 +113,14 @@ NAN_MODULE_INIT(init) { NAN_EXPORT(target, RegisterCallback); NAN_EXPORT(target, UnregisterCallback); + + node::AtExit([](auto){ + hook->detach(); + + std::unique_lock lock{callback_lock}; + callbacks.clear(); + queued_events.clear(); + }); } NODE_MODULE(MODULE_NAME, init) \ No newline at end of file diff --git a/native/ppt/src/KeyboardHook.cpp b/native/ppt/src/KeyboardHook.cpp index 30c4f9d..65f0197 100644 --- a/native/ppt/src/KeyboardHook.cpp +++ b/native/ppt/src/KeyboardHook.cpp @@ -6,19 +6,18 @@ using namespace std; KeyboardHook::KeyboardHook(KeyboardHookType type) : type_{type} {}; KeyboardHook::~KeyboardHook() { - if(this->_attached) - this->detach(); + assert(!this->attached_); } bool KeyboardHook::attach() { - assert(!this->_attached); - this->_attached = true; + assert(!this->attached_); + this->attached_ = true; return true; } void KeyboardHook::detach() { - assert(this->_attached); - this->_attached = false; + assert(this->attached_); + this->attached_ = false; } void KeyboardHook::trigger_key_event(const enum KeyEvent::type& type, const std::string &key) { diff --git a/native/ppt/src/KeyboardHook.h b/native/ppt/src/KeyboardHook.h index c02d043..39691b6 100644 --- a/native/ppt/src/KeyboardHook.h +++ b/native/ppt/src/KeyboardHook.h @@ -48,7 +48,7 @@ class KeyboardHook { [[nodiscard]] virtual bool keytype_supported() const = 0; [[nodiscard]] virtual bool attach(); - [[nodiscard]] inline bool attached() const { return this->_attached; } + [[nodiscard]] inline bool attached() const { return this->attached_; } virtual void detach(); void trigger_key_event(const enum KeyEvent::type&, const std::string& /* key */); @@ -59,5 +59,5 @@ class KeyboardHook { std::map map_key; std::map map_special; - bool _attached = false; + bool attached_ = false; }; \ No newline at end of file diff --git a/native/ppt/src/Win32KeyboardHook.h b/native/ppt/src/Win32KeyboardHook.h index 40cf66a..441777b 100644 --- a/native/ppt/src/Win32KeyboardHook.h +++ b/native/ppt/src/Win32KeyboardHook.h @@ -12,6 +12,7 @@ namespace hooks { class Win32RawHook : public KeyboardHook { public: Win32RawHook(); + ~Win32RawHook() override; bool attach() override; void detach() override; @@ -20,6 +21,8 @@ namespace hooks { private: static LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); + void do_detach(); + std::thread window_thread; void window_loop(); diff --git a/native/ppt/src/Win32KeyboardRawInput.cpp b/native/ppt/src/Win32KeyboardRawInput.cpp index 6713f00..19378cf 100644 --- a/native/ppt/src/Win32KeyboardRawInput.cpp +++ b/native/ppt/src/Win32KeyboardRawInput.cpp @@ -8,6 +8,9 @@ namespace hooks { Win32RawHook::Win32RawHook() : KeyboardHook{KeyboardHookType::RAW_INPUT} {} + Win32RawHook::~Win32RawHook() noexcept { + this->do_detach(); + } bool Win32RawHook::attach() { if(!KeyboardHook::attach()) @@ -26,6 +29,10 @@ namespace hooks { } void Win32RawHook::detach() { + this->do_detach(); + } + + void Win32RawHook::do_detach() { this->wactive = false; if(this->hwnd) { @@ -36,7 +43,6 @@ namespace hooks { this->window_thread.join(); this->set_wstatus(WorkerStatus::STOPPED); - KeyboardHook::detach(); }