Optimize hotkey service

This commit is contained in:
2025-11-06 22:42:44 +08:00
parent e0179b5838
commit 551e7e2cfd
26 changed files with 2917 additions and 1116 deletions

View File

@@ -0,0 +1,191 @@
//go:build darwin
package darwin
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework Carbon
#include <stdint.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
extern void keydownCallback(uintptr_t handle);
extern void keyupCallback(uintptr_t handle);
int registerHotKey(int mod, int key, uintptr_t handle, EventHotKeyRef* ref);
int unregisterHotKey(EventHotKeyRef ref);
*/
import "C"
import (
"errors"
"fmt"
"runtime/cgo"
"sync"
)
// Standard errors
var (
ErrAlreadyRegistered = errors.New("hotkey: already registered")
ErrNotRegistered = errors.New("hotkey: not registered")
ErrFailedToRegister = errors.New("hotkey: failed to register")
ErrFailedToUnregister = errors.New("hotkey: failed to unregister")
)
// PlatformHotkey is a combination of modifiers and key to trigger an event
type PlatformHotkey struct {
mu sync.Mutex
registered bool
hkref C.EventHotKeyRef
handle cgo.Handle
}
func (ph *PlatformHotkey) Register(mods []Modifier, key Key, keydownIn, keyupIn chan<- interface{}) error {
ph.mu.Lock()
defer ph.mu.Unlock()
if ph.registered {
return ErrAlreadyRegistered
}
// Store callbacks info for C side
callbacks := &callbackData{
keydownIn: keydownIn,
keyupIn: keyupIn,
}
ph.handle = cgo.NewHandle(callbacks)
var mod Modifier
for _, m := range mods {
mod += m
}
ret := C.registerHotKey(C.int(mod), C.int(key), C.uintptr_t(ph.handle), &ph.hkref)
if ret == C.int(-1) {
ph.handle.Delete()
return fmt.Errorf("%w: Carbon API returned error", ErrFailedToRegister)
}
ph.registered = true
return nil
}
func (ph *PlatformHotkey) Unregister() error {
ph.mu.Lock()
defer ph.mu.Unlock()
if !ph.registered {
return ErrNotRegistered
}
ret := C.unregisterHotKey(ph.hkref)
if ret == C.int(-1) {
return fmt.Errorf("%w: Carbon API returned error", ErrFailedToUnregister)
}
// Clean up CGO handle
ph.handle.Delete()
ph.registered = false
return nil
}
type callbackData struct {
keydownIn chan<- interface{}
keyupIn chan<- interface{}
}
//export keydownCallback
func keydownCallback(h uintptr) {
cb := cgo.Handle(h).Value().(*callbackData)
cb.keydownIn <- struct{}{}
}
//export keyupCallback
func keyupCallback(h uintptr) {
cb := cgo.Handle(h).Value().(*callbackData)
cb.keyupIn <- struct{}{}
}
// Modifier represents a modifier.
// See: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
type Modifier uint32
// All kinds of Modifiers
const (
ModCtrl Modifier = 0x1000
ModShift Modifier = 0x200
ModOption Modifier = 0x800
ModCmd Modifier = 0x100
)
// Key represents a key.
// See: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
type Key uint8
// All kinds of keys
const (
KeySpace Key = 49
Key1 Key = 18
Key2 Key = 19
Key3 Key = 20
Key4 Key = 21
Key5 Key = 23
Key6 Key = 22
Key7 Key = 26
Key8 Key = 28
Key9 Key = 25
Key0 Key = 29
KeyA Key = 0
KeyB Key = 11
KeyC Key = 8
KeyD Key = 2
KeyE Key = 14
KeyF Key = 3
KeyG Key = 5
KeyH Key = 4
KeyI Key = 34
KeyJ Key = 38
KeyK Key = 40
KeyL Key = 37
KeyM Key = 46
KeyN Key = 45
KeyO Key = 31
KeyP Key = 35
KeyQ Key = 12
KeyR Key = 15
KeyS Key = 1
KeyT Key = 17
KeyU Key = 32
KeyV Key = 9
KeyW Key = 13
KeyX Key = 7
KeyY Key = 16
KeyZ Key = 6
KeyReturn Key = 0x24
KeyEscape Key = 0x35
KeyDelete Key = 0x33
KeyTab Key = 0x30
KeyLeft Key = 0x7B
KeyRight Key = 0x7C
KeyUp Key = 0x7E
KeyDown Key = 0x7D
KeyF1 Key = 0x7A
KeyF2 Key = 0x78
KeyF3 Key = 0x63
KeyF4 Key = 0x76
KeyF5 Key = 0x60
KeyF6 Key = 0x61
KeyF7 Key = 0x62
KeyF8 Key = 0x64
KeyF9 Key = 0x65
KeyF10 Key = 0x6D
KeyF11 Key = 0x67
KeyF12 Key = 0x6F
KeyF13 Key = 0x69
KeyF14 Key = 0x6B
KeyF15 Key = 0x71
KeyF16 Key = 0x6A
KeyF17 Key = 0x40
KeyF18 Key = 0x4F
KeyF19 Key = 0x50
KeyF20 Key = 0x5A
)

View File

@@ -0,0 +1,59 @@
//go:build darwin
#include <stdint.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
extern void keydownCallback(uintptr_t handle);
extern void keyupCallback(uintptr_t handle);
static OSStatus
keydownHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
EventHotKeyID k;
GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(k), NULL, &k);
keydownCallback((uintptr_t)k.id); // use id as handle
return noErr;
}
static OSStatus
keyupHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
EventHotKeyID k;
GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(k), NULL, &k);
keyupCallback((uintptr_t)k.id); // use id as handle
return noErr;
}
// registerHotkeyWithCallback registers a global system hotkey for callbacks.
int registerHotKey(int mod, int key, uintptr_t handle, EventHotKeyRef* ref) {
__block OSStatus s;
dispatch_sync(dispatch_get_main_queue(), ^{
EventTypeSpec keydownEvent;
keydownEvent.eventClass = kEventClassKeyboard;
keydownEvent.eventKind = kEventHotKeyPressed;
EventTypeSpec keyupEvent;
keyupEvent.eventClass = kEventClassKeyboard;
keyupEvent.eventKind = kEventHotKeyReleased;
InstallApplicationEventHandler(
&keydownHandler, 1, &keydownEvent, NULL, NULL
);
InstallApplicationEventHandler(
&keyupHandler, 1, &keyupEvent, NULL, NULL
);
EventHotKeyID hkid = {.id = handle};
s = RegisterEventHotKey(
key, mod, hkid, GetApplicationEventTarget(), 0, ref
);
});
if (s != noErr) {
return -1;
}
return 0;
}
int unregisterHotKey(EventHotKeyRef ref) {
OSStatus s = UnregisterEventHotKey(ref);
if (s != noErr) {
return -1;
}
return 0;
}

View File

@@ -0,0 +1,68 @@
//go:build darwin
package darwin
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#import <Dispatch/Dispatch.h>
extern void os_main(void);
extern void wakeupMainThread(void);
static bool isMainThread() {
return [NSThread isMainThread];
}
*/
import "C"
import (
"os"
"runtime"
)
func init() {
runtime.LockOSThread()
}
// Call calls f on the main thread and blocks until f finishes.
func Call(f func()) {
if C.isMainThread() {
f()
return
}
done := make(chan struct{})
go func() {
mainFuncs <- func() {
f()
close(done)
}
C.wakeupMainThread()
}()
<-done
}
// Init initializes the functionality of running arbitrary subsequent functions be called on the main system thread.
//
// Init must be called in the main.main function.
func Init(f func()) {
go func() {
f()
os.Exit(0)
}()
C.os_main()
}
var mainFuncs = make(chan func(), 1)
//export dispatchMainFuncs
func dispatchMainFuncs() {
for {
select {
case f := <-mainFuncs:
f()
default:
return
}
}
}

View File

@@ -0,0 +1,23 @@
//go:build darwin
#include <stdint.h>
#import <Cocoa/Cocoa.h>
extern void dispatchMainFuncs();
void wakeupMainThread(void) {
dispatch_async(dispatch_get_main_queue(), ^{
dispatchMainFuncs();
});
}
// The following three lines of code must run on the main thread.
// For GUI applications (Wails, Cocoa), the framework handles this automatically.
// For CLI applications, see README for manual event loop setup.
//
// Inspired from: https://github.com/cehoffman/dotfiles/blob/4be8e893517e970d40746a9bdc67fe5832dd1c33/os/mac/iTerm2HotKey.m
void os_main(void) {
[NSApplication sharedApplication];
[NSApp disableRelaunchOnLogin];
[NSApp run];
}