X-Git-Url: https://git.phdru.name/?p=xsetbg.git;a=blobdiff_plain;f=xsetbg.py;h=68828cf567b5db4c43875df9e5c36b737c02362d;hp=93791a4ef38830d5e0e4dacc1be9b0c49cc057b6;hb=HEAD;hpb=eeb09e886cad6befca5aeab03772517e74541525 diff --git a/xsetbg.py b/xsetbg.py old mode 100755 new mode 100644 index 93791a4..825b20e --- a/xsetbg.py +++ b/xsetbg.py @@ -1,248 +1,147 @@ -#! /usr/local/bin/python -O """Set a random background image (XWin) -Select a random image from $HOME/lib/xsetbg/images subdirectories -and set it as the desktop wallpaper (display it in the root window). +Select a random image from a (list of) directory(s) +and set it as the desktop wallpaper (display it in the root window) +using xli program. """ -__version__ = "$Revision$"[11:-2] -__revision__ = "$Id$"[5:-2] -__date__ = "$Date$"[7:-2] - -__author__ = "Oleg BroytMann " -__copyright__ = "Copyright (C) 2000-2006 PhiloSoft Design" -__license__ = "GNU GPL" - - -import sys, os - - -def usage(): - sys.stderr.write("%s version %s\n" % (sys.argv[0], __version__)) - sys.stderr.write("Usage: %s [force]\n" % sys.argv[0]) - sys.exit(0) - -def error(error_str, error_code=1): - sys.stderr.write("%s: Error: %s\n" % (sys.argv[0], error_str)) - sys.exit(error_code) - - -xwininfo = os.popen("xwininfo -root", 'r') -xwininfo_lines = xwininfo.read() -rc = xwininfo.close() -if rc: - error("calling xwininfo", rc) - -screen_width = None -screen_height = None -for line in xwininfo_lines.split('\n'): - line = line.strip() - if line.startswith("Width: "): - screen_width = int(line[len("Width: "):]) - elif line.startswith("Height: "): - screen_height = int(line[len("Height: "):]) - -if not screen_width or not screen_height: - error("parsing xwininfo output") - - -from ConfigParser import SafeConfigParser - -xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg") -os.chdir(xsetbg_dir) - -config = SafeConfigParser() -config.read("xsetbg.conf") - -if config.has_option("images", "directory") or \ - config.has_option("images", "directory0") or \ - config.has_option("images", "directory1"): - image_dirs = [] - if config.has_option("images", "directory"): - image_dirs.append(config.get("images", "directory")) - if config.has_option("images", "directory0"): - image_dirs.append(config.get("images", "directory0")) - if config.has_option("images", "directory1"): - image_dirs.append(config.get("images", "directory1")) - i = 2 - while True: - option = "directory%d" % i - if config.has_option("images", option): - image_dirs.append(config.get("images", option)) - i += 1 - else: - break -else: - image_dirs = ["images"] - -image_dirs = [os.path.join(xsetbg_dir, - os.path.expandvars(os.path.expanduser(dirname))) - for dirname in image_dirs -] - -# minimum time in seconds between background image changes -if config.has_option("xsetbg", "min_pause"): - min_pause = config.getint("xsetbg", "min_pause") -else: - min_pause = 60 - -borders = config.get("xsetbg", "borders").split(',') -if config.has_option("xsetbg", "borders"): - borders = [border.strip() for border in config.get("xsetbg", "borders").split(',')] -else: - borders = ["darkcyan", "steelblue", "midnightblue"] - -# minimum time in seconds between occurences of the same image -if config.has_option("xsetbg", "min_delay"): - min_delay = config.getint("xsetbg", "min_delay") -else: - min_delay = 3600*24 # 24 hours - -del config - - -os.umask(0066) # octal; -rw-------; make the global persistent dictionary - # readable only by the user -global_db_name = "xsetbg.db" - - +from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB +from datetime import timedelta +import os import random -import anydbm, shelve +import re +import subprocess +import sys from time import time -from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB -# Import pickle and all anydbm/shelve internal machinery, so that -# when ReadyExec forks they will be ready. -# Also create the database if it is not exists yet. +from xsetbg_conf import xsetbg_dir, xsetbg_conf +from xsetbg_db import xsetbg_db -try: - global_db = shelve.open(global_db_name, flag='c') -except anydbm.error, msg: - if str(msg) == "db type could not be determined": - os.remove(global_db_name) - global_db = shelve.open(global_db_name, flag='c') +__all__ = ['change'] -global_db.close() # Close DB in parent process +def error(error_str): + sys.exit("%s: Error: %s\n" % (sys.argv[0], error_str)) -# DB keys -timestamp_key = "timestamp" -filename_key = "filename" -old_filename_key = "old_filename" +if not xsetbg_db: + error("No database found. Run rescan_fs.py. Abort.") -images = [] +if xsetbg_db.select().count() == 0: + error("No images found. Run rescan_fs.py. Abort.") -for image_dir in image_dirs: - # List images in all subdirectories - for dirpath, dirs, files in os.walk(image_dir): - images.extend([os.path.join(dirpath, file) for file in files]) -if not images: - error("No images found. Abort.") +# minimum time in seconds between background image changes +if xsetbg_conf.has_option("xsetbg", "min_pause"): + min_pause = xsetbg_conf.getint("xsetbg", "min_pause") +else: + min_pause = 60 +borders = xsetbg_conf.get("xsetbg", "borders").split(',') +if xsetbg_conf.has_option("xsetbg", "borders"): + borders = [border.strip() for border in + xsetbg_conf.get("xsetbg", "borders").split(',')] +else: + borders = ["darkcyan", "steelblue", "midnightblue"] -try: - from PIL import Image - use_PIL = True -except ImportError: - use_PIL = False +# minimum time in seconds between occurences of the same image +if xsetbg_conf.has_option("xsetbg", "min_delay"): + min_delay = xsetbg_conf.get("xsetbg", "min_delay") + + # Borrowed from http://stackoverflow.com/a/2765366 + td_re = re.compile('(?:(?P\\d+)y)?' + '(?:(?P\\d+)m)?' + '(?:(?P\\d+)d)?' + '(?:T(?:(?P\\d+)h)?' + '(?:(?P\\d+)m)?(?:(?P\\d+)s)?)?') + td_dict = td_re.match(min_delay).groupdict(0) + delta = timedelta(days=int(td_dict['days']) + + (int(td_dict['months']) * 30) + + (int(td_dict['years']) * 365), + hours=int(td_dict['hours']), + minutes=int(td_dict['minutes']), + seconds=int(td_dict['seconds'])) + + if delta: + min_delay = delta.days * 24*3600 + delta.seconds + else: + min_delay = int(min_delay) +else: + min_delay = 3600*24 # 24 hours -def run(): - if len(sys.argv) not in (1, 2): - usage() - force = False - if len(sys.argv) == 2: - if sys.argv[1] == "force": - force = True - else: - usage() +if xsetbg_db.select( + '(is_image = 1) AND (last_shown IS NULL OR last_shown < %d)' % + (time() - min_delay)).count() == 0: + error("No unshown images found. Run rescan_fs.py " + "or decrease min_delay. Abort.") - # Use the program's file as the lock file: - # lock it to prevent two processes run in parallel. - lock_file = open("xsetbg.py", 'r') - try: - flock(lock_file , LOCK_EX|LOCK_NB) - except IOError: # already locked - lock_file.close() - return +fs_encoding = xsetbg_conf.get("images", "fs_encoding") - global_db = None - try: - # Re-seed the RNG; this is neccessary because ReadyExecd forks - # and RNG in a child process got the same seed from the parent - # after every fork. - random.seed() +def change(force=False): + # Use the program's file as the lock file: + # lock it to prevent two processes run in parallel. + lock_file = open(os.path.join(xsetbg_dir, 'xsetbg.py'), 'r') - # Reopen the global persistent dictionary - global_db = shelve.open(global_db_name, 'w') + try: + flock(lock_file, LOCK_EX | LOCK_NB) + except IOError: # already locked + lock_file.close() + return - timestamp = global_db.get(timestamp_key) - current_time = time() + try: + timestamp = xsetbg_db.select( + '(is_image = 1) AND (last_shown IS NOT NULL)', + orderBy='-last_shown')[0].last_shown + current_time = time() - if not force and timestamp is not None and \ - current_time - timestamp < min_pause: # Too early to change background + if not force and timestamp is not None and \ + current_time - timestamp < min_pause: + # Too early to change background return - # Save current time - global_db[timestamp_key] = current_time - - # Select a random image and check if we've seen it recently; - # loop until we can find a new image (never seen before) or old enough. - for i in xrange(len(images)): # ensure the loop is not infinite - image_name = random.choice(images) - if global_db.has_key(image_name): - image_time = global_db[image_name] - if current_time - image_time > min_delay: - break - else: - break - global_db[image_name] = current_time - - border = random.choice(borders) - root, ext = os.path.splitext(image_name) - - # Save filename - if global_db.has_key(filename_key): - global_db[old_filename_key] = global_db[filename_key] - global_db[filename_key] = image_name - - placement_options = [] - if use_PIL: - image = Image.open(image_name, 'r') - im_w, im_h = image.size - del image - if (im_w > screen_width) or (im_h > screen_height): - zoom = min(screen_width*100//im_w, screen_height*100//im_h) - if zoom > 0: placement_options = ["-zoom", str(zoom)] - - finally: - # Unlock and close the lock file - flock(lock_file , LOCK_UN) - lock_file.close() - # Flush and close the global persistent dictionary - if global_db: global_db.close() - - - if ext.lower() in (".bmp", ".png"): - # xsetbg does not recognize BMP files. - # PNG files have gamma settings, and xli can adapt it to the display gamma; - # xloadimage/xview/xsetbg display them with wrong gamma. - program_options = ["xli", "xli", "-onroot", "-quiet"] + placement_options + \ - ["-center", "-border", border, image_name] - os.execlp(*program_options) - error("cannot execute xli!") - else: - # ...but xli failed to load many image types, use xsetbg for them - program_options = ["xsetbg", "xsetbg"] + placement_options + \ - ["-center", "-border", border, image_name] - os.execlp(*program_options) - error("cannot execute xsetbg!") - - -if __name__ == "__main__": - run() + # Select a random image that has never been shown + not_shown_select = xsetbg_db.select( + '(is_image = 1) AND (last_shown IS NULL)') + not_shown_count = not_shown_select.count() + if not_shown_count: + row = not_shown_select[random.randint(0, not_shown_count - 1)] + else: + old_shown_select = xsetbg_db.select( + '(is_image = 1) AND ' + '(last_shown IS NOT NULL AND last_shown < %d)' % + current_time - min_delay) + old_shown_count = old_shown_select.count() + if old_shown_count: + row = old_shown_select[random.randint(0, old_shown_count - 1)] + else: + error("No images to show found. Run rescan_fs.py " + "or decrease min_delay. Abort.") + + program_options = ["xli", "-border", random.choice(borders), + "-center", "-onroot", "-quiet", "-zoom", "auto", + row.full_name.encode(fs_encoding)] + + ext = os.path.splitext(row.full_name)[1].lower() + if ext in ('tif', 'tiff'): + program_options[0] = 'xsetbg' + + if ext == 'webp': + program_options = ['display', '-backdrop', + '-background', random.choice(borders), + '-window', 'root', + row.full_name.encode(fs_encoding)] + + rc = subprocess.call(program_options) + if rc: + error("cannot execute xli!") + else: + row.last_shown = current_time + + finally: + # Unlock and close the lock file + flock(lock_file, LOCK_UN) + lock_file.close()