///| Power in the login session: the screen goes off and the computer suspends after the minutes /// without input the settings module's Power tab set (shell.screen_off_min / suspend_min), and the /// tab's Suspend / Restart / Power off buttons. Suspend, power off and restart go through logind /// (`systemctl`, else `loginctl`), so its inhibitors and polkit rules apply. /// /// The main loop calls `power_tick` every pass. An idle loop still wakes about once a second /// (IdleClock in compositor/app), so the timeouts fire within a second. CLOCK_MONOTONIC stops /// while the machine is suspended, so after a resume the idle time hasn't grown past the suspend /// timeout and the machine doesn't suspend again until there has been input and a new idle spell. ///| What `power_tick` wants the display / system to do now. pub(all) enum PowerChange { NoChange ScreenOff ScreenOn Suspend } derive(Eq, Debug) ///| Note input: it restarts the idle time (and wakes a screen that is off at the next tick). pub fn ChandraCompositor::note_activity(self : ChandraCompositor, now_ms : UInt64) -> Unit { self.last_input_ms = now_ms } ///| Check the idle timeouts at monotonic time `now_ms`. pub fn ChandraCompositor::power_tick(self : ChandraCompositor, now_ms : UInt64) -> PowerChange { let idle = if now_ms > self.last_input_ms { now_ms - self.last_input_ms } else { 0UL } let minutes = fn(m : Int) { m.to_uint64() * 60000UL } let off = self.shell.screen_off_min let susp = self.shell.suspend_min if susp > 0 && idle >= minutes(susp) { if !self.idle_suspended { self.idle_suspended = true return Suspend } } else { self.idle_suspended = false } if off > 0 && idle >= minutes(off) { if !self.screen_off { self.screen_off = true return ScreenOff } } else if self.screen_off { self.screen_off = false self.damage.mark_full() return ScreenOn } NoChange } ///| The command for a power action: 0 suspend, 1 power off, 2 restart. pub fn power_command(action : Int) -> String? { let verb = match action { 0 => "suspend" 1 => "poweroff" 2 => "reboot" _ => return None } Some("systemctl " + verb + " || loginctl " + verb) } ///| Suspend, power off or restart now. Only the login session does it: in a nested window (or a /// test) a notice says so instead, since it would act on the machine the outer desktop runs on. pub fn ChandraCompositor::system_power(self : ChandraCompositor, action : Int) -> Unit { match power_command(action) { None => () Some(cmd) => if self.login_session { if !@sys.spawn(cmd, self.shell.display) { self.shell.say("Could not run " + cmd) } } else { self.shell.say("Suspend, restart and power off work in the login session only") } } } ///| test "power commands go through logind" { assert_eq(power_command(0), Some("systemctl suspend || loginctl suspend")) assert_eq(power_command(1), Some("systemctl poweroff || loginctl poweroff")) assert_eq(power_command(2), Some("systemctl reboot || loginctl reboot")) assert_eq(power_command(3), None) }