|
| 1 | +#include "window_message_dispatcher.h" |
| 2 | + |
| 3 | +#include <windows.h> |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +namespace nativeapi { |
| 8 | + |
| 9 | +WindowMessageDispatcher& WindowMessageDispatcher::GetInstance() { |
| 10 | + static WindowMessageDispatcher instance; |
| 11 | + return instance; |
| 12 | +} |
| 13 | + |
| 14 | +WindowMessageDispatcher::~WindowMessageDispatcher() { |
| 15 | + RestoreOriginalWindowProc(); |
| 16 | +} |
| 17 | + |
| 18 | +LRESULT CALLBACK WindowMessageDispatcher::WindowProc(HWND hwnd, |
| 19 | + UINT message, |
| 20 | + WPARAM wparam, |
| 21 | + LPARAM lparam) { |
| 22 | + WindowMessageDispatcher& dispatcher = GetInstance(); |
| 23 | + |
| 24 | + // Copy handlers while holding the lock to avoid holding lock during handler |
| 25 | + // execution |
| 26 | + std::vector<WindowMessageHandler> handlers_copy; |
| 27 | + WNDPROC original_proc = nullptr; |
| 28 | + |
| 29 | + { |
| 30 | + std::lock_guard<std::mutex> lock(dispatcher.mutex_); |
| 31 | + handlers_copy.reserve(dispatcher.handlers_.size()); |
| 32 | + for (const auto& pair : dispatcher.handlers_) { |
| 33 | + if (pair.second) { |
| 34 | + handlers_copy.push_back(pair.second); |
| 35 | + } |
| 36 | + } |
| 37 | + original_proc = dispatcher.original_window_proc_; |
| 38 | + } |
| 39 | + |
| 40 | + // Dispatch message to all registered handlers (without holding the lock) |
| 41 | + for (const auto& handler : handlers_copy) { |
| 42 | + if (handler) { |
| 43 | + auto result = handler(hwnd, message, wparam, lparam); |
| 44 | + if (result.has_value()) { |
| 45 | + return result.value(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // If no handler handled the message, call the original WindowProc |
| 51 | + if (original_proc) { |
| 52 | + return CallWindowProc(original_proc, hwnd, message, wparam, lparam); |
| 53 | + } |
| 54 | + |
| 55 | + // Fallback to DefWindowProc if no original proc is available |
| 56 | + return DefWindowProc(hwnd, message, wparam, lparam); |
| 57 | +} |
| 58 | + |
| 59 | +HWND WindowMessageDispatcher::GetTopLevelWindow() { |
| 60 | + // Try to get the foreground window first |
| 61 | + HWND hwnd = GetForegroundWindow(); |
| 62 | + if (hwnd && IsWindow(hwnd)) { |
| 63 | + return hwnd; |
| 64 | + } |
| 65 | + |
| 66 | + // If no foreground window, try to get the active window |
| 67 | + hwnd = GetActiveWindow(); |
| 68 | + if (hwnd && IsWindow(hwnd)) { |
| 69 | + return hwnd; |
| 70 | + } |
| 71 | + |
| 72 | + // If still no window, try to find any top-level window |
| 73 | + hwnd = FindWindow(nullptr, nullptr); |
| 74 | + if (hwnd && IsWindow(hwnd)) { |
| 75 | + return hwnd; |
| 76 | + } |
| 77 | + |
| 78 | + return nullptr; |
| 79 | +} |
| 80 | + |
| 81 | +void WindowMessageDispatcher::SetupInternalWindowProc() { |
| 82 | + if (window_proc_hooked_) { |
| 83 | + return; // Already hooked |
| 84 | + } |
| 85 | + |
| 86 | + top_level_window_ = GetTopLevelWindow(); |
| 87 | + if (!top_level_window_) { |
| 88 | + std::cerr << "Failed to get top level window for WindowProc hooking" |
| 89 | + << std::endl; |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // Store the original WindowProc |
| 94 | + original_window_proc_ = reinterpret_cast<WNDPROC>( |
| 95 | + GetWindowLongPtr(top_level_window_, GWLP_WNDPROC)); |
| 96 | + if (!original_window_proc_) { |
| 97 | + std::cerr << "Failed to get original WindowProc" << std::endl; |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // Set our internal WindowProc |
| 102 | + if (SetWindowLongPtr(top_level_window_, GWLP_WNDPROC, |
| 103 | + reinterpret_cast<LONG_PTR>(WindowProc)) == 0) { |
| 104 | + DWORD error = GetLastError(); |
| 105 | + std::cerr << "Failed to set internal WindowProc. Error: " << error |
| 106 | + << std::endl; |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + window_proc_hooked_ = true; |
| 111 | +} |
| 112 | + |
| 113 | +void WindowMessageDispatcher::RestoreOriginalWindowProc() { |
| 114 | + if (!window_proc_hooked_ || !top_level_window_ || !original_window_proc_) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + // Restore the original WindowProc |
| 119 | + if (SetWindowLongPtr(top_level_window_, GWLP_WNDPROC, |
| 120 | + reinterpret_cast<LONG_PTR>(original_window_proc_)) == |
| 121 | + 0) { |
| 122 | + DWORD error = GetLastError(); |
| 123 | + std::cerr << "Failed to restore original WindowProc. Error: " << error |
| 124 | + << std::endl; |
| 125 | + } |
| 126 | + |
| 127 | + // Reset state |
| 128 | + window_proc_hooked_ = false; |
| 129 | + top_level_window_ = nullptr; |
| 130 | + original_window_proc_ = nullptr; |
| 131 | +} |
| 132 | + |
| 133 | +int WindowMessageDispatcher::RegisterHandler(WindowMessageHandler handler) { |
| 134 | + if (!handler) { |
| 135 | + return -1; |
| 136 | + } |
| 137 | + |
| 138 | + std::lock_guard<std::mutex> lock(mutex_); |
| 139 | + |
| 140 | + int id = next_id_++; |
| 141 | + handlers_[id] = std::move(handler); |
| 142 | + |
| 143 | + // If this is the first handler, setup the internal WindowProc |
| 144 | + if (handlers_.size() == 1) { |
| 145 | + SetupInternalWindowProc(); |
| 146 | + } |
| 147 | + |
| 148 | + return id; |
| 149 | +} |
| 150 | + |
| 151 | +bool WindowMessageDispatcher::UnregisterHandler(int id) { |
| 152 | + if (id < 0) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + std::lock_guard<std::mutex> lock(mutex_); |
| 157 | + |
| 158 | + auto it = handlers_.find(id); |
| 159 | + if (it != handlers_.end()) { |
| 160 | + handlers_.erase(it); |
| 161 | + |
| 162 | + // If no handlers remain, restore the original WindowProc |
| 163 | + if (handlers_.empty()) { |
| 164 | + RestoreOriginalWindowProc(); |
| 165 | + } |
| 166 | + |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + return false; |
| 171 | +} |
| 172 | + |
| 173 | +} // namespace nativeapi |
0 commit comments