|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <windows.h> |
| 4 | +#include <functional> |
| 5 | +#include <mutex> |
| 6 | +#include <optional> |
| 7 | +#include <unordered_map> |
| 8 | + |
| 9 | +namespace nativeapi { |
| 10 | + |
| 11 | +/** |
| 12 | + * @brief Function type for handling Windows messages. |
| 13 | + * |
| 14 | + * @param hwnd Window handle receiving the message |
| 15 | + * @param msg Message identifier (WM_* constants) |
| 16 | + * @param wparam Message-specific parameter |
| 17 | + * @param lparam Message-specific parameter |
| 18 | + * @return std::optional<LRESULT> If a value is returned, the message is |
| 19 | + * considered handled. If std::nullopt is returned, the message continues to |
| 20 | + * other handlers. |
| 21 | + */ |
| 22 | +using WindowMessageHandler = std::function< |
| 23 | + std::optional<LRESULT>(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)>; |
| 24 | + |
| 25 | +/** |
| 26 | + * @brief Singleton dispatcher for Windows message handling across multiple |
| 27 | + * windows. |
| 28 | + * |
| 29 | + * This class provides a centralized way to register message handlers for |
| 30 | + * Windows messages. It supports both global handlers (applied to all windows) |
| 31 | + * and window-specific handlers. The dispatcher automatically hooks into window |
| 32 | + * procedures using SetWindowLongPtr and restores original procedures when |
| 33 | + * handlers are unregistered. |
| 34 | + * |
| 35 | + * Thread Safety: All public methods are thread-safe. |
| 36 | + * |
| 37 | + * Usage Example: |
| 38 | + * ```cpp |
| 39 | + * auto& dispatcher = WindowMessageDispatcher::GetInstance(); |
| 40 | + * |
| 41 | + * // Register global handler for all windows |
| 42 | + * int global_id = dispatcher.RegisterHandler([](HWND hwnd, UINT msg, WPARAM wp, |
| 43 | + * LPARAM lp) { if (msg == WM_SIZE) { |
| 44 | + * // Handle window resize |
| 45 | + * return std::make_optional<LRESULT>(0); |
| 46 | + * } |
| 47 | + * return std::nullopt; // Let other handlers process |
| 48 | + * }); |
| 49 | + * |
| 50 | + * // Register window-specific handler |
| 51 | + * int window_id = dispatcher.RegisterHandler(specific_hwnd, [](HWND hwnd, UINT |
| 52 | + * msg, WPARAM wp, LPARAM lp) { if (msg == WM_CLOSE) { |
| 53 | + * // Prevent window from closing |
| 54 | + * return std::make_optional<LRESULT>(0); |
| 55 | + * } |
| 56 | + * return std::nullopt; |
| 57 | + * }); |
| 58 | + * |
| 59 | + * // Unregister when done |
| 60 | + * dispatcher.UnregisterHandler(global_id); |
| 61 | + * dispatcher.UnregisterHandler(window_id); |
| 62 | + * ``` |
| 63 | + */ |
| 64 | +class WindowMessageDispatcher { |
| 65 | + public: |
| 66 | + /** |
| 67 | + * @brief Get the singleton instance of the dispatcher. |
| 68 | + * @return Reference to the singleton WindowMessageDispatcher instance. |
| 69 | + */ |
| 70 | + static WindowMessageDispatcher& GetInstance(); |
| 71 | + |
| 72 | + /** |
| 73 | + * @brief Register a global message handler that applies to all windows. |
| 74 | + * |
| 75 | + * @param handler Function to call for Windows messages |
| 76 | + * @return int Handler ID for unregistration |
| 77 | + */ |
| 78 | + int RegisterHandler(WindowMessageHandler handler); |
| 79 | + |
| 80 | + /** |
| 81 | + * @brief Register a message handler for a specific window. |
| 82 | + * |
| 83 | + * This method automatically installs a hook into the window's procedure |
| 84 | + * if not already installed. The hook is removed when the last handler |
| 85 | + * for the window is unregistered. |
| 86 | + * |
| 87 | + * @param hwnd Target window handle |
| 88 | + * @param handler Function to call for Windows messages |
| 89 | + * @return int Handler ID for unregistration |
| 90 | + */ |
| 91 | + int RegisterHandler(HWND hwnd, WindowMessageHandler handler); |
| 92 | + |
| 93 | + /** |
| 94 | + * @brief Unregister a message handler by ID. |
| 95 | + * |
| 96 | + * @param id Handler ID returned from RegisterHandler |
| 97 | + * @return bool true if handler was found and removed, false otherwise |
| 98 | + */ |
| 99 | + bool UnregisterHandler(int id); |
| 100 | + |
| 101 | + /** |
| 102 | + * @brief Internal window procedure function used for message dispatching. |
| 103 | + * |
| 104 | + * This function is installed as the window procedure for windows that have |
| 105 | + * registered handlers. It processes messages through registered handlers |
| 106 | + * and falls back to the original window procedure if no handler consumes |
| 107 | + * the message. |
| 108 | + * |
| 109 | + * @param hwnd Window handle |
| 110 | + * @param msg Message identifier |
| 111 | + * @param wparam Message parameter |
| 112 | + * @param lparam Message parameter |
| 113 | + * @return LRESULT Message processing result |
| 114 | + */ |
| 115 | + static LRESULT CALLBACK DispatchWindowProc(HWND hwnd, |
| 116 | + UINT msg, |
| 117 | + WPARAM wparam, |
| 118 | + LPARAM lparam); |
| 119 | + |
| 120 | + private: |
| 121 | + WindowMessageDispatcher() = default; |
| 122 | + ~WindowMessageDispatcher(); |
| 123 | + |
| 124 | + /** |
| 125 | + * @brief Entry for a registered message handler. |
| 126 | + */ |
| 127 | + struct HandlerEntry { |
| 128 | + WindowMessageHandler handler; ///< The handler function |
| 129 | + HWND target_hwnd; ///< Target window (HWND(0) for global handlers) |
| 130 | + }; |
| 131 | + |
| 132 | + /** |
| 133 | + * @brief Install message hook for a window. |
| 134 | + * |
| 135 | + * @param hwnd Window handle to hook |
| 136 | + * @return bool true if hook was installed successfully |
| 137 | + */ |
| 138 | + bool InstallHook(HWND hwnd); |
| 139 | + |
| 140 | + /** |
| 141 | + * @brief Uninstall message hook for a window. |
| 142 | + * |
| 143 | + * @param hwnd Window handle to unhook |
| 144 | + */ |
| 145 | + void UninstallHook(HWND hwnd); |
| 146 | + |
| 147 | + ///< Mutex for thread safety |
| 148 | + mutable std::mutex mutex_; |
| 149 | + ///< Registered handlers by ID |
| 150 | + std::unordered_map<int, HandlerEntry> handlers_; |
| 151 | + ///< Original window procedures |
| 152 | + std::unordered_map<HWND, WNDPROC> original_procs_; |
| 153 | + ///< Next available handler ID |
| 154 | + int next_id_ = 1; |
| 155 | +}; |
| 156 | + |
| 157 | +} // namespace nativeapi |
0 commit comments