#!/usr/bin/env python3

from argparse import ArgumentParser
from subprocess import call
import i3ipc

i3 = i3ipc.Connection()

parser = ArgumentParser(prog='disable-standby-fs',
        description='''
        Disable standby (dpms) and screensaver when a window becomes fullscreen
        or exits fullscreen-mode. Requires `xorg-xset`.
        ''')

args = parser.parse_args()

def on_fullscreen_mode(i3, e):
    if e.container.props.fullscreen_mode:
        call(['xset', 's', 'off'])
        call(['xset', '-dpms'])
    else:
        call(['xset', 's', 'on'])
        call(['xset', '+dpms'])

def on_window_close(i3, e):
    if e.container.props.fullscreen_mode:
        call(['xset', 's', 'on'])
        call(['xset', '+dpms'])

i3.on('window::fullscreen_mode', on_fullscreen_mode)
i3.on('window::close', on_window_close)

i3.main()

@@ -377,18 +377,20 @@
 ()%0A%0Adef 
-on
+find
 _fullscr
@@ -396,63 +396,252 @@
 reen
-_mode(i3, e):%0A    if e.container.props.fullscreen_mode:
+(con):%0A    # XXX remove me when this method is available on the con in a release%0A    return %5Bc for c in con.descendents()%0A            if c.type == 'con' and c.fullscreen_mode%5D%0A%0Adef set_dpms(state):%0A    if state:%0A        print('setting dpms on')
 %0A   
@@ -670,10 +670,9 @@
 , 'o
-ff
+n
 '%5D)%0A
@@ -694,17 +694,17 @@
 xset', '
--
++
 dpms'%5D)%0A
@@ -713,16 +713,50 @@
   else:%0A
+        print('setting dpms off')%0A
         
@@ -768,33 +768,34 @@
 %5B'xset', 's', 'o
-n
+ff
 '%5D)%0A        call
@@ -797,33 +797,33 @@
  call(%5B'xset', '
-+
+-
 dpms'%5D)%0A%0Adef on_
@@ -826,135 +826,184 @@
  on_
-window_close(i3, e):%0A    if e.container.props.fullscreen_mode:%0A        call(%5B'xset', 's', 'on'%5D)%0A        call(%5B'xset', '+dpms'%5D
+fullscreen_mode(i3, e):%0A    set_dpms(not len(find_fullscreen(i3.get_tree())))%0A%0Adef on_window_close(i3, e):%0A    if not len(find_fullscreen(i3.get_tree())):%0A        set_dpms(True
 )%0A%0Ai

handle multiple fs windows