-
Notifications
You must be signed in to change notification settings - Fork 545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sokol_app.h: add custom win32 hook to handle windows events #283
Comments
Hi Rico :) Hmm yeah that's a good point. I need to think of a good way to expose this type of stuff though because I can't have any Win32-types in the "public API". So basically a callback which takes a void* which would need to be cast to a MSG* in the user-provided callback. The callback should be passed in via sapp_desc, e.g. void my_win32_msg_callback(const void* msg) {
const MSG* win32_msg = (const MSG*) msg;
...
}
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc) {
.win32_msg_intercept_cb = my_win32_msg_callback
....
};
} There should also be a second sapp_desc callback type with a user_data pointer (same as with the init/frame/event/cleanup callbacks). I probably won't get around implementing this for a little while because I'm still focused on the WebGPU backend, but you could provide a pull request in the meantime if you want :) |
Here is a workaround that works for my use case: It is possible to replace the SetWindowLongPtr((HWND)sapp_win32_get_hwnd(), GWLP_WNDPROC, (LONG_PTR)hook_sapp_win32_wndproc); I define the function alongside the sokol_app implementation to get access to the original callback _SOKOL_PRIVATE LRESULT CALLBACK hook_sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case ...:
...
default:
break;
}
return _sapp_win32_wndproc(hWnd, uMsg, wParam, lParam);
} Now about Gainput specifically. They are using MSG structures but they seem to only use the |
FYI I stumbled upon this issue while trying to integrate Dear IMGUI into a sokol-odin program the lazy way: instead of making a proper sokol imgui impl I just reused calls from the Win32/DX11, already included in imgui. The only thing left is this wndpropc hook, which is required for handling input and theres' no way to do this without patching sokol sources, since it's not exported (i'm also using SOKOL_DLL for hot-reloading reasons). |
There's a bit of a naughty trick to access the 'private guts' of a sokol header without modifying the header itself: you can write a C 'extension header' which you can include after the sokol_app.h implementation. E.g. in sokol-odin you have those C implementation files, like this #if defined(IMPL)
#define SOKOL_APP_IMPL
#endif
#include "sokol_defines.h"
#include "sokol_app.h" ...you could hook in your own extension header like this: #if defined(IMPL)
#define SOKOL_APP_IMPL
#endif
#include "sokol_defines.h"
#include "sokol_app.h"
#include "my_sokol_app_extensions.h" Now you have full access to all the private internals of the sokol_app.h implementation, e.g. you can probably inject your WinProc into the window which then chains to the original sokol_app.h WinProc. |
I am using gainput (https://github.com/jkuhlmann/gainput) for input management and on windows I have to pass in all windows message manually for gainput to correctly register mouse and keyboard events
unfortunately sokol_app "swallows" all win32 event messages and I have no way to stop sokol from stealing my events.
I found a workaround that makes me handle win32 events before sokol does. I have to paste the following code before the
#define SOKOL_IMPL
then in my main app I just have to implement
this works fine, but I am wondering if it is possible to add "native" event callbacks to sapp_desc to handle such cases.
Greetings!
~ Rico (Ex Bigpoint Member)
The text was updated successfully, but these errors were encountered: