#! /usr/bin/python # pauseonlock # # Script watches xscreensaver status and executes specific commands if given # requirements are met. # # With the default configuration it pauses amarok if it is running and # continues playing when the screen is no longer locked. # # @author: Johannes Rueckert # @license: GPL3 or later # @version: 1.1 # # Changelog: # # Version 1.1: # - loop depends on subprocess - if xscreenshot --status is terminated, script ends (previously this caused an # infinite loop) ## # Configuration ## # Name of the process to check before performing operations processName = 'amarokapp' # Command that should be executed on lock (not when the screensaver ist activated!) commandOnLock = ['amarok', '--pause'] # Command that should be executed on unblank, i.e. when the screen is no longer # locked commandOnUnblank = ['amarok', '--play'] ## # Configuration end ## # import libraries import subprocess def isRunning(processName): """Test whether given process is running""" # try to get pid of process pid = subprocess.Popen(["pidof", "-s", processName], stdout=subprocess.PIPE).stdout # if pid was found if len(pid.readline())>1: return 1 else: return 0 # start xscreensaver watcher process = subprocess.Popen(["xscreensaver-command", "--watch"], stdout=subprocess.PIPE) pipe = process.stdout while (process.poll() == None): event = pipe.readline() if isRunning(processName): if event[0:4] == 'LOCK': subprocess.call(commandOnLock) elif event [0:7] == 'UNBLANK': subprocess.call(commandOnUnblank)