- # 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('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(
+ '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]
+
+ 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()