2 """Set a random background image (XWin)
4 Select a random image from a (list of) directory(s)
5 and set it as the desktop wallpaper (display it in the root window)
10 __author__ = "Oleg Broytman <phd@phdru.name>"
11 __copyright__ = "Copyright (C) 2000-2014 PhiloSoft Design"
12 __license__ = "GNU GPL"
18 from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB
19 from datetime import timedelta
28 from xsetbg_conf import xsetbg_dir, xsetbg_conf
29 from xsetbg_db import xsetbg_db_path
32 def error(error_str, error_code=1):
33 sys.stderr.write("%s: Error: %s\n" % (sys.argv[0], error_str))
37 if xsetbg_conf.has_option("images", "directory") or \
38 xsetbg_conf.has_option("images", "directory0") or \
39 xsetbg_conf.has_option("images", "directory1"):
41 if xsetbg_conf.has_option("images", "directory"):
42 image_dirs.append(xsetbg_conf.get("images", "directory"))
43 if xsetbg_conf.has_option("images", "directory0"):
44 image_dirs.append(xsetbg_conf.get("images", "directory0"))
45 if xsetbg_conf.has_option("images", "directory1"):
46 image_dirs.append(xsetbg_conf.get("images", "directory1"))
49 option = "directory%d" % i
50 if xsetbg_conf.has_option("images", option):
51 image_dirs.append(xsetbg_conf.get("images", option))
56 image_dirs = ["images"]
58 image_dirs = [os.path.join(xsetbg_dir,
59 os.path.expandvars(os.path.expanduser(dirname)))
60 for dirname in image_dirs
63 # minimum time in seconds between background image changes
64 if xsetbg_conf.has_option("xsetbg", "min_pause"):
65 min_pause = xsetbg_conf.getint("xsetbg", "min_pause")
69 borders = xsetbg_conf.get("xsetbg", "borders").split(',')
70 if xsetbg_conf.has_option("xsetbg", "borders"):
71 borders = [border.strip() for border in xsetbg_conf.get("xsetbg", "borders").split(',')]
73 borders = ["darkcyan", "steelblue", "midnightblue"]
75 # minimum time in seconds between occurences of the same image
76 if xsetbg_conf.has_option("xsetbg", "min_delay"):
77 min_delay = xsetbg_conf.get("xsetbg", "min_delay")
79 # Borrowed from http://stackoverflow.com/a/2765366
80 td_re = re.compile('(?:(?P<years>\d+)y)?(?:(?P<months>\d+)m)?(?:(?P<days>\d+)d)?(?:T(?:(?P<hours>\d+)h)?(?:(?P<minutes>\d+)m)?(?:(?P<seconds>\d+)s)?)?')
81 td_dict = td_re.match(min_delay).groupdict(0)
82 delta = timedelta(days=int(td_dict['days']) + (int(td_dict['months']) * 30) + (int(td_dict['years']) * 365),
83 hours=int(td_dict['hours']),
84 minutes=int(td_dict['minutes']),
85 seconds=int(td_dict['seconds']))
88 min_delay = delta.days * 24*3600 + delta.seconds
90 min_delay = int(min_delay)
93 min_delay = 3600*24 # 24 hours
97 timestamp_key = "timestamp"
98 filename_key = "filename"
99 old_filename_key = "old_filename"
102 # Create the database if it is not exists yet.
104 os.umask(0066) # octal; -rw-------; make the global persistent dictionary
105 # readable only by the user
108 xsetbg_db = shelve.open(xsetbg_db_path, flag='c')
109 except anydbm.error, msg:
110 if str(msg) == "db type could not be determined":
111 os.remove(xsetbg_db_path)
112 xsetbg_db = shelve.open(xsetbg_db_path, flag='c')
114 # Remove old filenames
115 old_time = time() - min_delay
117 to_delete = [timestamp_key]
118 for key in xsetbg_db.keys():
119 if key.startswith('/') and xsetbg_db[key] < old_time:
120 to_delete.append(key)
122 for key in to_delete:
128 xsetbg_db.close() # Close DB in the parent process
133 for image_dir in image_dirs:
134 # List images in all subdirectories
135 for dirpath, dirs, files in os.walk(image_dir):
136 images.extend([os.path.join(dirpath, file) for file in files])
139 error("No images found. Abort.")
142 def change(force=False):
143 # Use the program's file as the lock file:
144 # lock it to prevent two processes run in parallel.
145 lock_file = open(os.path.join(xsetbg_dir, 'xsetbg.py'), 'r')
148 flock(lock_file, LOCK_EX|LOCK_NB)
149 except IOError: # already locked
156 # Reopen the global persistent dictionary
157 xsetbg_db = shelve.open(xsetbg_db_path, 'w')
159 timestamp = xsetbg_db.get(timestamp_key)
160 current_time = time()
162 if not force and timestamp is not None and \
163 current_time - timestamp < min_pause: # Too early to change background
167 xsetbg_db[timestamp_key] = current_time
169 # Select a random image and check if we've seen it recently;
170 # loop until we can find a new image (never seen before) or old enough.
171 for i in xrange(len(images)): # ensure the loop is not infinite
172 image_name = random.choice(images)
173 if xsetbg_db.has_key(image_name):
174 image_time = xsetbg_db[image_name]
175 if current_time - image_time > min_delay:
179 xsetbg_db[image_name] = current_time
182 if xsetbg_db.has_key(filename_key):
183 xsetbg_db[old_filename_key] = xsetbg_db[filename_key]
184 xsetbg_db[filename_key] = image_name
186 program_options = ["xli", "-onroot", "-quiet"] + \
187 ["-center", "-border", random.choice(borders), "-zoom", "auto",
190 rc = subprocess.call(program_options)
192 error("cannot execute xli!")
195 # Unlock and close the lock file
196 flock(lock_file, LOCK_UN)
198 # Flush and close the global persistent dictionary
199 if xsetbg_db: xsetbg_db.close()