From 4aebf2bf86e1389ed8f39f8fe90210ebb9784801 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Thu, 12 Aug 2010 11:10:48 +0200 Subject: random_wallpaper: rewrite in Python --- functions.config | 2 +- scripts/random_wallpaper.py | 55 +++++++++++++++++++++++++++++++++++++++++++++ scripts/random_wallpaper.sh | 21 ----------------- 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100755 scripts/random_wallpaper.py delete mode 100755 scripts/random_wallpaper.sh diff --git a/functions.config b/functions.config index 7d759b7..ec6949e 100644 --- a/functions.config +++ b/functions.config @@ -78,7 +78,7 @@ AddToFunc SlideshowOn DestroyFunc RandomWall AddToFunc RandomWall -+ I Exec '$[FVWM_USERDIR]/scripts/random_wallpaper.sh' ++ I Exec '$[FVWM_USERDIR]/scripts/random_wallpaper.py' DestroyFunc MPDControl AddToFunc MPDControl diff --git a/scripts/random_wallpaper.py b/scripts/random_wallpaper.py new file mode 100755 index 0000000..55bb618 --- /dev/null +++ b/scripts/random_wallpaper.py @@ -0,0 +1,55 @@ +#!/usr/bin/python +# Reads a given directory and set a random +# wallpaper using the images on that dir. + +import subprocess +import os +import os.path +import sys +import random + +if not 'WALLPAPERDIR' in os.environ: + sys.stderr.write('WALLPAPERDIR variable not set, using default value.\n') + d = os.path.expandvars('${HOME}/${FVWM_USERDIR}/images') +else: + d = os.path.expandvars('${WALLPAPERDIR}') + +sys.stderr.write('Trying directory %s.\n'%d) +if not os.path.isdir(d): + sys.stderr.write('Path %s is not a directory, aborting.\n'%d) + sys.exit(-1) + +files = os.listdir(d) +random.seed() +try: + f = random.choice(files) +except IndexError: + sys.stderr.write('No files in dir %s, aborting.\n'%d) + sys.exit(-1) + +sys.stderr.write('Selected file %s.\n'%f) + +try: + p = subprocess.Popen('xdpyinfo', stdout = subprocess.PIPE) + for line in p.stdout: + if 'dimensions' in line: + res = line.split()[1] + break + sys.stderr.write('X screen resolution is %s.\n'%res) +except (OSError, IndexError, NameError): + sys.stderr.write('Error getting X screen resolution, aborting.\n') + sys.exit(-1) + +try: + subprocess.check_call(['convert', os.path.join(d, f), '-resize', res, os.path.expandvars('${FVWM_USERDIR}/tmp/current_wall.png')]) +except subprocess.CalledProcessError: + sys.stderr.write('Error resizing the wallpaper. Is Imagemagick installed?\n') + sys.exit(-1) + +try: + subprocess.check_call(['feh', '--bg-center', os.path.expandvars('${FVWM_USERDIR}/tmp/current_wall.png')]) +except subprocess.CalledProcessError: + sys.stderr.write('Error setting the wallpaper. Is feh installed?\n') + sys.exit(-1) + +sys.exit(0) diff --git a/scripts/random_wallpaper.sh b/scripts/random_wallpaper.sh deleted file mode 100755 index 3d2f803..0000000 --- a/scripts/random_wallpaper.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash -# Reads a given directory and set a random -# wallpaper using the images on that dir. - -[ -x /usr/bin/convert ] || exit 1 -[ -x /usr/bin/feh ] || exit 1 - -if [ -d "${WALLPAPERDIR}" ]; then - files=$(ls "${WALLPAPERDIR}") - file_matrix=($files) - num_files=${#file_matrix[*]} - file="${WALLPAPERDIR}/${file_matrix[$((RANDOM%num_files))]}" -else - file = ${FVWM_USERDIR}/images/default_wall.jpg -fi - -convert "${file}" -resize `xdpyinfo |grep dimensions|awk '{print $2}'` "${FVWM_USERDIR}/tmp/current_wall.png" -feh --bg-center "${FVWM_USERDIR}/tmp/current_wall.png" -FvwmCommand Refresh - -exit 0 -- cgit v1.2.3