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

91 lines
3.2 KiB
Go

//go:build darwin && !ios
package darwin
import "time"
type power struct{ b *Backend }
// powerEvents maps the notifications the app delegate observes to power
// events.
var powerEvents = map[string]string{
"NSWorkspaceWillSleepNotification": "suspend",
"NSWorkspaceDidWakeNotification": "resume",
"com.apple.screenIsLocked": "lock-screen",
"com.apple.screenIsUnlocked": "unlock-screen",
}
func (p power) Watch() {
withPool(func() {
observe := func(center id, name string) {
send(center, "addObserver:selector:name:object:", uintptr(p.b.delegate), uintptr(sel("mygoPowerNotification:")), uintptr(nsString(name)), 0)
}
workspace := send(send(class("NSWorkspace"), "sharedWorkspace"), "notificationCenter")
observe(workspace, "NSWorkspaceWillSleepNotification")
observe(workspace, "NSWorkspaceDidWakeNotification")
// Screen locking is only announced to other processes.
distributed := send(class("NSDistributedNotificationCenter"), "defaultCenter")
observe(distributed, "com.apple.screenIsLocked")
observe(distributed, "com.apple.screenIsUnlocked")
})
}
// Options of NSProcessInfo's activities.
const (
activityIdleSystemSleepDisabled = 1 << 20
activityIdleDisplaySleepDisabled = 1 << 40
// activityUserInitiated is NSActivityUserInitiated, which keeps App
// Nap off and the Mac from sleeping when idle: the second it leaves
// out (NSActivityUserInitiatedAllowingIdleSystemSleep).
activityUserInitiated = 0x00FFFFFF | activityIdleSystemSleepDisabled
)
func (p power) KeepAwake(display bool, reason string) func() {
opts := uint64(activityIdleSystemSleepDisabled)
if display {
opts |= activityIdleDisplaySleepDisabled
}
return beginActivity(opts, reason)
}
// KeepRunning holds NSActivityUserInitiatedAllowingIdleSystemSleep, an
// activity of the user's that keeps App Nap off while the Mac still sleeps
// when idle: the system does not suppress the app (proc_pidinfo's
// PROC_FLAG_SUPPRESSED) once none of its windows shows. KeepAwake's
// activity keeps App Nap off too, but the Mac awake as well, and shows in
// pmset -g assertions, which this one does not.
func (p power) KeepRunning(reason string) func() {
return beginActivity(activityUserInitiated&^activityIdleSystemSleepDisabled, reason)
}
// beginActivity begins an activity of NSProcessInfo with the options, which
// the function it returns ends.
func beginActivity(opts uint64, reason string) func() {
var token id
withPool(func() {
info := send(class("NSProcessInfo"), "processInfo")
token = retain(send(info, "beginActivityWithOptions:reason:", uintptr(opts), uintptr(nsString(reason))))
})
return func() {
send(send(class("NSProcessInfo"), "processInfo"), "endActivity:", uintptr(token))
release(token)
}
}
func (p power) OnBattery() bool {
if iopsCopyPowerSourcesInfo == nil {
return false
}
info := iopsCopyPowerSourcesInfo()
if info == 0 {
return false
}
defer cfRelease(info)
return goString(iopsGetProvidingPowerSourceType(info)) == "Battery Power"
}
func (p power) IdleTime() time.Duration {
const combinedSessionState, anyInputEvent = 0, ^uint32(0)
return time.Duration(cgEventSourceSecondsSinceLastType(combinedSessionState, anyInputEvent) * float64(time.Second))
}