Skip to content

Instantly share code, notes, and snippets.

@ykai55
Created January 7, 2019 07:50
Show Gist options
  • Select an option

  • Save ykai55/ec1a9b57aad4eecebbeaf8b16f36d94b to your computer and use it in GitHub Desktop.

Select an option

Save ykai55/ec1a9b57aad4eecebbeaf8b16f36d94b to your computer and use it in GitHub Desktop.
autofeh: auto wallpaper changer for i3wm
#! /usr/bin/env python3
# change wallpaper every argv[2] seconds
# photo folder is argv[1]
# use "kill -s 10 `/tmp/autofeh.pid`" to change by yourself
import os
import sys
import time
import signal
import fcntl
print(os.getpid())
if len(sys.argv) >= 2:
pic_folder = sys.argv[1]
else:
pic_folder = "Photo/Wallpapers/"
if len(sys.argv) >= 3:
interval = int(sys.argv[2])
else:
interval = 20
cmd = "feh --bg-fill " + pic_folder + " --randomize"
master_changing = False
next_change_time = time.time()
def handler(*args):
global next_change_time
global master_changing
if master_changing:
return
os.system(cmd)
next_change_time = time.time() + interval
def file_lock():
path = "/tmp/autofeh.pid"
# open without rewrite
f = open(path, 'a')
try:
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
return None
# lock succeed, then rewrite
f.truncate(0)
f.write(str(os.getpid()))
f.flush()
return f
if __name__ == "__main__":
lfile = file_lock()
if lfile is None:
raise Exception("another instance running")
signal.signal(signal.SIGUSR1, handler)
while True:
if next_change_time <= time.time():
# timeout
master_changing = True
os.system(cmd)
next_change_time = time.time() + interval
master_changing = False
time.sleep(next_change_time - time.time())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment