Skip to content

Instantly share code, notes, and snippets.

@nfnty
Created November 18, 2015 12:22
Show Gist options
  • Save nfnty/21a2dca0c17d11c08c8f to your computer and use it in GitHub Desktop.
Save nfnty/21a2dca0c17d11c08c8f to your computer and use it in GitHub Desktop.
bspwm events screensaver toggling
#!/usr/bin/python3 -u
''' Screensaver fullscreen toggling '''
import subprocess
import json
def class_find(windows, name):
''' Find className '''
for window in windows.values():
if window['client']['className'] == name:
return True
return False
def main():
''' Main '''
windows = {}
process = subprocess.Popen(['/usr/bin/bspc', 'control', '--subscribe', 'window_state'],
stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)
for line in process.stdout:
line = json.loads(line)
if line['client']['state'] == 'fullscreen':
windows[line['client']['window']] = line
elif line['client']['window'] in windows:
del windows[line['client']['window']]
else:
continue
try:
subprocess.check_call(['/usr/bin/pgrep', 'i3lock'])
except subprocess.CalledProcessError:
pass
else:
continue
xset_output = subprocess.check_output(['/usr/bin/xset', 'q']).decode('UTF-8')
if 'DPMS is Enabled' in xset_output and windows:
if class_find(windows, 'Kodi'):
pass
else:
subprocess.check_call(['/usr/bin/xset', '-dpms'])
elif 'DPMS is Disabled' in xset_output:
if (windows and class_find(windows, 'Kodi')) or not windows:
subprocess.check_call(['/usr/bin/xset', '+dpms'])
if __name__ == '__main__':
main()
@baskerville
Copy link

The proper way to do this would be to build an initial world view based on the output of query -T and then update that world view in response to window_{state,manage,unmanage} events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment