I am not familiar with all the interactions and assumptions in this part of the code. Is it safe to use `foreground` in these cases?
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`?
Can it happen in practice that `foreground->process != current->process` (for the first case)
It can happen when injecting input, but this won't trigger a repeat, or when mouse is moved over a window that's in the background, but here as well there's no repeat mechanism (then maybe changing the mouse message handling isn't appropriate yet).
I don't think `RIDEV_NOLEGACY` handling is really correct anyway, and ideally, "legacy" input messages should probably be generated from the rawinput messages, instead of in parallel, and `RIDEV_NOLEGACY` would be then handled per-process. This is a larger change though.
As other options, you can probably also check whether `current` is set, and ignore `RIDEV_NOLEGACY` handling if it's not, or maybe pass `current` as a `thread` repeat parameter to restore it when the timer fires.