Looks like get_global_hooks only uses the thread to get its desktop. Maybe pass the desktop to get_first_global_hook and remove then unnecessary use of get_global_hooks?
Done in v2.
I don't think RIDEV_NOLEGACY handling is really correct anyway...
In v2 I added some tests to exercise this behavior between processes and a few focus scenarios. Based on these tests I updated the handling to use window thread/process which is going to be the target of the legacy message, instead of the current thread, to check RIDEV_NOLEGACY. This makes more sense to me from what I have understood of raw devices: it's not up to the sender, but rather the raw device registration in the target application to determine whether to emit legacy events for the hardware events.