1 """Set a random background image (XWin)
3 Select a random image from a (list of) directory(s)
4 and set it as the desktop wallpaper (display it in the root window)
9 from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB
10 from datetime import timedelta
18 from xsetbg_conf import xsetbg_dir, xsetbg_conf
19 from xsetbg_db import xsetbg_db
25 sys.exit("%s: Error: %s\n" % (sys.argv[0], error_str))
29 error("No database found. Run rescan_fs.py. Abort.")
31 if xsetbg_db.select().count() == 0:
32 error("No images found. Run rescan_fs.py. Abort.")
35 # minimum time in seconds between background image changes
36 if xsetbg_conf.has_option("xsetbg", "min_pause"):
37 min_pause = xsetbg_conf.getint("xsetbg", "min_pause")
41 borders = xsetbg_conf.get("xsetbg", "borders").split(',')
42 if xsetbg_conf.has_option("xsetbg", "borders"):
43 borders = [border.strip() for border in
44 xsetbg_conf.get("xsetbg", "borders").split(',')]
46 borders = ["darkcyan", "steelblue", "midnightblue"]
48 # minimum time in seconds between occurences of the same image
49 if xsetbg_conf.has_option("xsetbg", "min_delay"):
50 min_delay = xsetbg_conf.get("xsetbg", "min_delay")
52 # Borrowed from http://stackoverflow.com/a/2765366
53 td_re = re.compile('(?:(?P<years>\d+)y)?'
54 '(?:(?P<months>\d+)m)?'
56 '(?:T(?:(?P<hours>\d+)h)?'
57 '(?:(?P<minutes>\d+)m)?(?:(?P<seconds>\d+)s)?)?')
58 td_dict = td_re.match(min_delay).groupdict(0)
59 delta = timedelta(days=int(td_dict['days']) +
60 (int(td_dict['months']) * 30) +
61 (int(td_dict['years']) * 365),
62 hours=int(td_dict['hours']),
63 minutes=int(td_dict['minutes']),
64 seconds=int(td_dict['seconds']))
67 min_delay = delta.days * 24*3600 + delta.seconds
69 min_delay = int(min_delay)
72 min_delay = 3600*24 # 24 hours
75 if xsetbg_db.select('last_shown IS NULL OR last_shown < %d' %
76 (time() - min_delay)).count() == 0:
77 error("No unshown images found. Run rescan_fs.py "
78 "or decrease min_delay. Abort.")
80 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
82 def change(force=False):
83 # Use the program's file as the lock file:
84 # lock it to prevent two processes run in parallel.
85 lock_file = open(os.path.join(xsetbg_dir, 'xsetbg.py'), 'r')
88 flock(lock_file, LOCK_EX | LOCK_NB)
89 except IOError: # already locked
94 timestamp = xsetbg_db.select('last_shown IS NOT NULL',
95 orderBy='-last_shown')[0].last_shown
98 if not force and timestamp is not None and \
99 current_time - timestamp < min_pause:
100 # Too early to change background
103 # Select a random image that has never been shown
104 not_shown_select = xsetbg_db.select('last_shown IS NULL')
105 not_shown_count = not_shown_select.count()
107 row = not_shown_select[random.randint(0, not_shown_count - 1)]
109 old_shown_select = xsetbg_db.select(
110 'last_shown IS NOT NULL AND last_shown < %d' %
111 current_time - min_delay)
112 old_shown_count = old_shown_select.count()
114 row = old_shown_select[random.randint(0, old_shown_count - 1)]
116 error("No images to show found. Run rescan_fs.py "
117 "or decrease min_delay. Abort.")
119 program_options = ["xli", "-border", random.choice(borders),
120 "-center", "-onroot", "-quiet", "-zoom", "auto",
121 row.full_name.encode(fs_encoding)]
123 rc = subprocess.call(program_options)
125 error("cannot execute xli!")
127 row.last_shown = current_time
130 # Unlock and close the lock file
131 flock(lock_file, LOCK_UN)