Files
openchamber-android-native/third_party/mygo/internal/linux/power.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

117 lines
4.0 KiB
Go

//go:build linux && !android && (amd64 || arm64)
package linux
import (
"os"
"path/filepath"
"strings"
"time"
)
type power struct{ b *Backend }
// Watch listens to logind for sleep, on the system bus, and to the screen
// savers of GNOME and of the freedesktop specification (KDE and others),
// which lock the screen, on the session bus.
func (p power) Watch() {
var gerr ptr
if system := gBusGetSync(1, 0, &gerr); system != 0 { // G_BUS_TYPE_SYSTEM
gDBusConnectionSignalSubscribe(system, cs("org.freedesktop.login1"), cs("org.freedesktop.login1.Manager"),
cs("PrepareForSleep"), cs("/org/freedesktop/login1"), nil, 0, cbPowerSignal, 0, 0)
} else {
_ = gErr(gerr)
}
if session, err := bus(); err == nil {
for _, iface := range []string{"org.gnome.ScreenSaver", "org.freedesktop.ScreenSaver"} {
gDBusConnectionSignalSubscribe(session, nil, cs(iface), cs("ActiveChanged"), nil, nil, 0, cbPowerSignal, 0, 0)
}
}
}
// KeepRunning does nothing: Linux desktops do not slow down apps whose
// windows are out of sight, as macOS's App Nap does.
func (p power) KeepRunning(string) func() { return func() {} }
// KeepAwake asks the XDG desktop portal to inhibit suspending (and, with
// display, the session going idle), else the freedesktop screen saver to
// inhibit idleness.
func (p power) KeepAwake(display bool, reason string) func() {
const inhibitSuspend, inhibitIdle = 4, 8
flags := uint32(inhibitSuspend)
if display {
flags |= inhibitIdle
}
params := tuple(gVariantNewString(cs("")), gVariantNewUint32(flags), vardict(vardictEntry("reason", gVariantNewString(cs(reason)))))
if res, err := portalCall(portalPath, "org.freedesktop.portal.Inhibit", "Inhibit", params, "(o)"); err == nil {
child := gVariantGetChildValue(res, 0)
handle := goStr(gVariantGetString(child, nil))
gVariantUnref(child)
gVariantUnref(res)
return func() {
if res, err := portalCall(handle, "org.freedesktop.portal.Request", "Close", 0, ""); err == nil {
gVariantUnref(res)
}
}
}
res, err := dbusCall("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver", "Inhibit",
tuple(gVariantNewString(cs(appName())), gVariantNewString(cs(reason))), "(u)")
if err != nil {
return nil
}
child := gVariantGetChildValue(res, 0)
cookie := gVariantGetUint32(child)
gVariantUnref(child)
gVariantUnref(res)
return func() {
if res, err := dbusCall("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver", "UnInhibit",
tuple(gVariantNewUint32(cookie)), ""); err == nil {
gVariantUnref(res)
}
}
}
// OnBattery reports whether a battery discharges while no power supply is
// online, from /sys/class/power_supply.
func (p power) OnBattery() bool {
read := func(dir, name string) string {
b, _ := os.ReadFile(filepath.Join(dir, name))
return strings.TrimSpace(string(b))
}
supplies, _ := filepath.Glob("/sys/class/power_supply/*")
discharging := false
for _, s := range supplies {
switch read(s, "type") {
case "Mains", "USB":
if read(s, "online") == "1" {
return false
}
case "Battery":
if read(s, "scope") != "Device" && read(s, "status") == "Discharging" {
discharging = true
}
}
}
return discharging
}
// IdleTime asks GNOME's idle monitor, else the freedesktop screen saver
// (KDE).
func (p power) IdleTime() time.Duration {
if res, err := dbusCall("org.gnome.Mutter.IdleMonitor", "/org/gnome/Mutter/IdleMonitor/Core", "org.gnome.Mutter.IdleMonitor", "GetIdletime", 0, "(t)"); err == nil {
child := gVariantGetChildValue(res, 0)
ms := gVariantGetUint64(child)
gVariantUnref(child)
gVariantUnref(res)
return time.Duration(ms) * time.Millisecond
}
if res, err := dbusCall("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver", "GetSessionIdleTime", 0, "(u)"); err == nil {
child := gVariantGetChildValue(res, 0)
s := gVariantGetUint32(child)
gVariantUnref(child)
gVariantUnref(res)
return time.Duration(s) * time.Second
}
return 0
}