Files
openchamber-android-native/third_party/mygo/internal/linux/hotkey.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

186 lines
5.6 KiB
Go

//go:build linux && !android && (amd64 || arm64)
package linux
import (
"errors"
"fmt"
"os"
"unsafe"
"github.com/ebitengine/purego"
"github.com/egoist/mygo/internal/accelerator"
)
// On X11, global shortcuts grab their keys on the root window, and the key
// presses arrive through a GDK event filter. Elsewhere the desktop binds
// them through the XDG desktop portal (hotkey_portal.go), and so it does
// under XWayland, where a grab gets the keys only while a window of the app
// has the focus, when the desktop offers the portal.
var (
x11 struct {
loaded, ok bool
stringToKeysym func(name *byte) uint64
keysymToKeycode func(dpy ptr, keysym uint64) uint8
grabKey func(dpy ptr, keycode int32, modifiers uint32, window uint64, ownerEvents bool, pointerMode, keyboardMode int32) int32
ungrabKey func(dpy ptr, keycode int32, modifiers uint32, window uint64) int32
defaultRoot func(dpy ptr) uint64
flush func(dpy ptr) int32
displayType func() uintptr
xdisplay func(display ptr) ptr
errorTrapPush func(display ptr)
errorTrapPop func(display ptr) int32
addFilter func(window ptr, fn ptr, data ptr)
}
cbHotkeyFilter ptr
)
// X modifier masks.
const (
shiftMask = 1 << 0
lockMask = 1 << 1 // Caps Lock
controlMask = 1 << 2
mod1Mask = 1 << 3 // Alt
mod2Mask = 1 << 4 // Num Lock
mod4Mask = 1 << 6 // Super
)
type grab struct {
keycode uint8
mods uint32
}
// loadX11 binds the X11 functions once; it reports whether global
// shortcuts can work: GDK runs on an X server.
func loadX11() bool {
if x11.loaded {
return x11.ok
}
x11.loaded = true
lib, err := open("libX11.so.6")
if err != nil {
return false
}
ok := bind(lib, &x11.stringToKeysym, "XStringToKeysym") &&
bind(lib, &x11.keysymToKeycode, "XKeysymToKeycode") &&
bind(lib, &x11.grabKey, "XGrabKey") &&
bind(lib, &x11.ungrabKey, "XUngrabKey") &&
bind(lib, &x11.defaultRoot, "XDefaultRootWindow") &&
bind(lib, &x11.flush, "XFlush") &&
bind(libGDK, &x11.displayType, "gdk_x11_display_get_type") &&
bind(libGDK, &x11.xdisplay, "gdk_x11_display_get_xdisplay") &&
bind(libGDK, &x11.errorTrapPush, "gdk_x11_display_error_trap_push") &&
bind(libGDK, &x11.errorTrapPop, "gdk_x11_display_error_trap_pop") &&
bind(libGDK, &x11.addFilter, "gdk_window_add_filter")
if !ok {
return false
}
x11.ok = gTypeCheckInstanceIsA(gdkDisplayGetDefault(), x11.displayType())
if x11.ok {
cbHotkeyFilter = purego.NewCallback(hotkeyFilter)
x11.addFilter(0, cbHotkeyFilter, 0)
}
return x11.ok
}
// hotkeyFilter reports the key presses of grabbed shortcuts.
func hotkeyFilter(xevent, event, data ptr) int32 {
const keyPress, filterContinue, filterRemove = 2, 0, 2
ev := *(*unsafe.Pointer)(unsafe.Pointer(&xevent))
if *(*int32)(ev) != keyPress {
return filterContinue
}
// XKeyEvent: state and keycode follow the time.
state := *(*uint32)(unsafe.Add(ev, 80)) & (shiftMask | controlMask | mod1Mask | mod4Mask)
keycode := uint8(*(*uint32)(unsafe.Add(ev, 84)))
for id, g := range theBackend.grabs {
if g.keycode == keycode && g.mods == state {
theBackend.h.HotkeyPressed(id)
return filterRemove
}
}
return filterContinue
}
func (b *Backend) RegisterHotkey(id int, acc, description string) error {
a, err := accelerator.Parse(acc, "linux")
if err != nil {
return err
}
if b.shortcutsThroughPortal() {
return b.bindShortcut(id, a, description)
}
display := gdkDisplayGetDefault()
dpy := x11.xdisplay(display)
keycode := x11.keysymToKeycode(dpy, x11.stringToKeysym(cs(a.Keysym())))
if keycode == 0 {
return fmt.Errorf("mygo: the keyboard has no key for %q", acc)
}
var mods uint32
for m, x := range map[accelerator.Modifiers]uint32{accelerator.Shift: shiftMask, accelerator.Ctrl: controlMask, accelerator.Alt: mod1Mask, accelerator.Super: mod4Mask} {
if a.Has(m) {
mods |= x
}
}
g := grab{keycode, mods}
root := x11.defaultRoot(dpy)
x11.errorTrapPush(display)
for _, lock := range []uint32{0, lockMask, mod2Mask, lockMask | mod2Mask} {
x11.grabKey(dpy, int32(keycode), mods|lock, root, false, 1, 1) // GrabModeAsync
}
if x11.errorTrapPop(display) != 0 {
b.ungrab(g)
return errors.New("mygo: " + acc + " is already a global shortcut of another app")
}
if b.grabs == nil {
b.grabs = map[int]grab{}
}
b.grabs[id] = g
return nil
}
// shortcutsThroughPortal tells whether global shortcuts go through the
// portal rather than X11 grabs, asking the desktop for the portal once.
func (b *Backend) shortcutsThroughPortal() bool {
if portalShortcutsOnly || !loadX11() {
return true
}
if !b.portalAsked {
b.portalAsked = true
b.portalOnX11 = portalOnX11(os.Getenv, func() bool { return portalVersion(shortcutsPortal) != 0 })
}
return b.portalOnX11
}
// portalOnX11 tells whether an app whose GDK runs on an X server binds its
// global shortcuts through the portal: when the X server is XWayland, in a
// Wayland session, and the desktop offers the portal. A grab there gets the
// keys only while a window of the app has the focus on GNOME, which sends
// XWayland no key presses meant for Wayland windows.
func portalOnX11(getenv func(string) string, offered func() bool) bool {
return getenv("WAYLAND_DISPLAY") != "" && offered()
}
func (b *Backend) UnregisterHotkey(id int) {
if g, ok := b.grabs[id]; ok {
delete(b.grabs, id)
b.ungrab(g)
}
b.unbindShortcut(id)
}
func (b *Backend) ungrab(g grab) {
display := gdkDisplayGetDefault()
dpy := x11.xdisplay(display)
root := x11.defaultRoot(dpy)
x11.errorTrapPush(display)
for _, lock := range []uint32{0, lockMask, mod2Mask, lockMask | mod2Mask} {
x11.ungrabKey(dpy, int32(g.keycode), g.mods|lock, root)
}
x11.flush(dpy)
x11.errorTrapPop(display)
}