#!/usr/bin/env python3 """Show the verified Power off screen, then request an orderly init shutdown.""" import argparse import fcntl import os from pathlib import Path import runpy import signal import subprocess import sys import time def is_charger_connected(): for name in ('pm660-charger', 'battery', 'qpnp-smbcharger', 'usb'): supply = Path(f'/sys/class/power_supply/{name}') if not supply.exists(): continue try: online = supply / 'online' if online.exists() and online.read_text().strip() in ('1', 'true'): return True status = supply / 'status' if status.exists() and status.read_text().strip() in ('Charging', 'Full'): return True except OSError: pass return False def enter_charger_standby(): print('platina-poweroff: external power connected; entering low-power suspend standby', flush=True) state_file = Path('/sys/power/state') if state_file.exists(): for mode in ('mem', 'freeze'): try: state_file.write_text(mode + '\n') print(f'platina-poweroff: woke from {mode} standby', flush=True) return except OSError: continue try: subprocess.run(['pm-suspend'], check=True, timeout=10) except (OSError, subprocess.SubprocessError): pass def request_shutdown(force=False, standby=False): if not force and (standby or is_charger_connected()): enter_charger_standby() return subprocess.run(['/usr/sbin/poweroff'], check=True, timeout=10) print('platina-poweroff: init accepted shutdown; keeping screen until shutdown', flush=True) # Shutdown should terminate this process. If it does not, restore the display # instead of leaving an indefinite claim that the phone is already off. time.sleep(30) raise RuntimeError('Shutdown did not finish within 30 seconds') def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--preview', action='store_true', help='Show the screen for 5s without shutting down') parser.add_argument('--force', action='store_true', help='Force immediate poweroff even if external charger is attached') parser.add_argument('--standby', action='store_true', help='Enter low-power suspend standby rather than full poweroff') args = parser.parse_args() if os.geteuid() != 0: raise ValueError('Run on the phone as root') signal.signal(signal.SIGTERM, lambda *_: sys.exit(0)) control = runpy.run_path('/usr/local/sbin/platina-display', run_name='poweroff_display') directory = Path('/usr/local/share/platina/current/poweroff') with Path('/run/platina-poweroff.lock').open('a') as lock: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) with control['device']() as fb: control['show'](fb, directory, 5 if args.preview else 3, 25, after_display=None if args.preview else lambda: request_shutdown(force=args.force, standby=args.standby)) if __name__ == '__main__': try: main() except (OSError, ValueError, RuntimeError, subprocess.SubprocessError) as error: print('platina-poweroff:', error, file=sys.stderr) raise SystemExit(1)