def sleep_on_lid_close(self): log('Checking if should go to sleep') if not self.__is_lid_closed(): log('Not going to sleep, lid open') return p = Power() if p.is_on_ac(): log('Not going to sleep, on AC') return d = Display() if d.is_using_external_display(): log('Not going to sleep, external monitor on') return dirs = os.listdir('/tmp') for d in dirs: if not re.match('^pulse-\w+$', d): continue pidfile = '/tmp/{}/pid'.format(d) if not os.path.isfile(pidfile): continue with open(pidfile, 'r') as f: pid = int(f.read().strip()) try: p = psutil.Process(pid) except psutil.NoSuchProcess: continue if p.name() == 'pulseaudio': pa = PulseAudio(user=p.username()) if pa.is_in_use(): log('Not going to sleep, audio playing') return self.suspend()
def powersave(self): log('Setting power state to powersave') if self.__uses_pstate(): self.__cpufreq('performance') self.__pstate(0, 0) else: self.__cpufreq('powersave') S.run('sudo pm-powersave true')
def __on_switch(self): try: cmd = self.__conf.get('OnSwitch', 'command') except: # No `OnSwitch` section. cmd = None if cmd is not None: log(cmd) S.run(cmd, shell=True)
def __on_switch(self): try: cmd = self.__conf.get('OnSwitch', 'command') except: # No `OnSwitch` section. cmd = None if cmd is not None: log(cmd) S.run(cmd, shell = True)
def __sleep(self, sleep_type, can_sleep, go_sleep): if can_sleep(): log('Going to {}', sleep_type) self.__iface.AboutToSleep('') self.lock() try: go_sleep() pass except: # ignore reply timeout errors and stuff. pass self.__on_resume() log('Got out of {}, normal operation resumed', sleep_type) else: stderr('{} is not supported on your hardware.', sleep_type)
def switch(self, mode): log('Switching to {} display mode', mode) if mode not in self.__modes: log('{} display mode is not defined', mode) return # Commands that will be run. cmds = [] # Command to turn requested displays on. on = ['xrandr'] # Command to turn all other displays off. off = ['xrandr'] # On and off commands are separate because most laptop video cards are # limited to only two displays at a time. Turning displays off and on # with one xrandr command will fail. d = self.__displays.copy() for display in self.__conf.options(mode): d.remove(display) pos = self.__conf.get(mode, display) on.append('--output') on.append(display) on.append('--auto') if pos == 'main': on.append('--primary') else: on.append('--' + pos) if display in self.__properties: prop = self.__conf.get('Properties', display) cmds.append(' '.join([ 'xrandr --output', display, '--set', '"' + prop.split(':')[0] + '"', '"' + prop.split(':')[1] + '"' ])) for display in d: off.append('--output') off.append(display) off.append('--off') cmds.append(' '.join(off)) cmds.append(' '.join(on)) cmds.append('xrandr --dpi 96') for cmd in cmds: log(cmd) S.run(cmd) self.__on_switch()
def lock(self): log('Locking screen') cmd = 'sudo -u {} xscreensaver-command -lock' S.run(cmd.format(S.get_xuser())) # Wait for screensaver to appear. time.sleep(5)
def performance(self): log('Setting power state to performance') self.__cpufreq('performance') if self.__uses_pstate(): self.__pstate(0, 100) S.run('sudo pm-powersave false')