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 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 2000-2015 PhiloSoft Design"
11 __license__ = "GNU GPL"
15 from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB
16 from datetime import timedelta
24 from xsetbg_conf import xsetbg_dir, xsetbg_conf
25 from xsetbg_db import xsetbg_db
29 sys.exit("%s: Error: %s\n" % (sys.argv[0], error_str))
33 error("No database found. Run rescan_fs.py. Abort.")
35 if xsetbg_db.select().count() == 0:
36 error("No images found. Run rescan_fs.py. Abort.")
39 # minimum time in seconds between background image changes
40 if xsetbg_conf.has_option("xsetbg", "min_pause"):
41 min_pause = xsetbg_conf.getint("xsetbg", "min_pause")
45 borders = xsetbg_conf.get("xsetbg", "borders").split(',')
46 if xsetbg_conf.has_option("xsetbg", "borders"):
47 borders = [border.strip() for border in
48 xsetbg_conf.get("xsetbg", "borders").split(',')]
50 borders = ["darkcyan", "steelblue", "midnightblue"]
52 # minimum time in seconds between occurences of the same image
53 if xsetbg_conf.has_option("xsetbg", "min_delay"):
54 min_delay = xsetbg_conf.get("xsetbg", "min_delay")
56 # Borrowed from http://stackoverflow.com/a/2765366
57 td_re = re.compile('(?:(?P<years>\d+)y)?'
58 '(?:(?P<months>\d+)m)?'
60 '(?:T(?:(?P<hours>\d+)h)?'
61 '(?:(?P<minutes>\d+)m)?(?:(?P<seconds>\d+)s)?)?')
62 td_dict = td_re.match(min_delay).groupdict(0)
63 delta = timedelta(days=int(td_dict['days']) +
64 (int(td_dict['months']) * 30) +
65 (int(td_dict['years']) * 365),
66 hours=int(td_dict['hours']),
67 minutes=int(td_dict['minutes']),
68 seconds=int(td_dict['seconds']))
71 min_delay = delta.days * 24*3600 + delta.seconds
73 min_delay = int(min_delay)
76 min_delay = 3600*24 # 24 hours
79 if xsetbg_db.select('last_shown IS NULL OR last_shown < %d' %
80 (time() - min_delay)).count() == 0:
81 error("No unshown images found. Run rescan_fs.py "
82 "or decrease min_delay. Abort.")
84 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
86 def change(force=False):
87 # Use the program's file as the lock file:
88 # lock it to prevent two processes run in parallel.
89 lock_file = open(os.path.join(xsetbg_dir, 'xsetbg.py'), 'r')
92 flock(lock_file, LOCK_EX | LOCK_NB)
93 except IOError: # already locked
98 timestamp = xsetbg_db.select('last_shown IS NOT NULL',
99 orderBy='-last_shown')[0].last_shown
100 current_time = time()
102 if not force and timestamp is not None and \
103 current_time - timestamp < min_pause:
104 # Too early to change background
107 # Select a random image that has never been shown
108 not_shown_select = xsetbg_db.select('last_shown IS NULL')
109 not_shown_count = not_shown_select.count()
111 row = not_shown_select[random.randint(0, not_shown_count - 1)]
113 old_shown_select = xsetbg_db.select(
114 'last_shown IS NOT NULL AND last_shown < %d' %
115 current_time - min_delay)
116 old_shown_count = old_shown_select.count()
118 row = old_shown_select[random.randint(0, old_shown_count - 1)]
120 error("No images to show found. Run rescan_fs.py "
121 "or decrease min_delay. Abort.")
123 program_options = ["xli", "-border", random.choice(borders),
124 "-center", "-onroot", "-quiet", "-zoom", "auto",
125 row.full_name.encode(fs_encoding)]
127 rc = subprocess.call(program_options)
129 error("cannot execute xli!")
131 row.last_shown = current_time
134 # Unlock and close the lock file
135 flock(lock_file, LOCK_UN)