Clean up hidutil remaps on shutdown, signal, and panic - #10
Merged
Conversation
Wire up signal-hook (already a dependency, previously unused) to handle SIGTERM and SIGINT by stopping the CFRunLoop, allowing graceful cleanup. On any exit path — normal shutdown, signal, panic, or error — clear the hidutil UserKeyMapping so the keyboard returns to its default state. The LaunchAgent re-applies remaps on next boot, so there's no gap. - Add hidutil::clear_modifier_remaps() to reset UserKeyMapping to empty - Extract run_hidutil_set() helper to share between apply and clear - Install panic hook that clears hidutil before the default handler runs - Spawn signal listener thread that stops CFRunLoop on SIGINT/SIGTERM - Track hidutil state with AtomicBool to avoid unnecessary cleanup calls - Clean up on error path in main() as a final safety net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hidutilUserKeyMappingon every exit path so the keyboard returns to its default state when Switcheroo stopssignal-hook(was already a dep, unused) to catchSIGTERM/SIGINT, stop theCFRunLoop, and trigger cleanupAtomicBoolto avoid unnecessary cleanup callsWhy
The
hidutilmodifier remaps (e.g. Caps Lock → Ctrl) are kernel-level and persist after Switcheroo exits. This means stopping the daemon leaves stale remaps — confusing if you're debugging or if the daemon crashes. Since the LaunchAgent re-applies them on boot anyway, cleanup on exit is the right default.Exit paths covered
Ctrl+C/SIGINTCFRunLoop→ normal cleanuplaunchctl bootout/SIGTERMclear_modifier_remaps()run()main()error path callsclear_modifier_remaps()SIGKILL