Walid-Shouman
3/19/2013 - 3:46 PM

Krawall is a "Wallpaper Changer" for all common desktop environments (xfce4, i3/xmonad, enlightenment (e17), gnome2, gnome3, kde3 and kde4)

Krawall is a "Wallpaper Changer" for all common desktop environments (xfce4, i3/xmonad, enlightenment (e17), gnome2, gnome3, kde3 and kde4)

# The value 16 is an array index and can be any number
[Containments][16]
ActionPluginsSource=Global
activity=Desktop
activityId=e363f362-e727-4589-be60-1d49b812503f
desktop=-1 
formfactor=0
geometry=1286,0,1280,800
immutability=1
lastDesktop=-1
lastScreen=0
location=0 
plugin=folderview
screen=0
wallpaperplugin=image
wallpaperpluginmode=SingleImage
zvalue=0

[Containments][16][Wallpaper][image]
slideTimer=10
slidepaths=/usr/share/wallpapers/
userswallpapers=/usr/share/backgrounds/sabayonlinux.png
wallpaper=/home/ferhat/Bilder/Sample Pictures/Part-0013/75131-redhead.jpg
wallpapercolor=56,111,150
wallpaperposition=0
#!/bin/bash
# -------------------------------------------------------
# @author X4
# @version 1.0
#
# HowTo:
# You can add a new GlobalHotkey to the media forward an back buttons for example.
# There are other ways to run this script: Button or Keypresses, Plasmoids, Events..
#
# Use these parameters when on XFCE for example:
#   calling "./krawall --xfce --prev" should return the previous wallpaper in the stack
#   calling "./krawall --xfce --next" should return the next wallpaper in the stack
#
# TODO: 
#   [x] Switch to different Wallpaper on each Virtual-Desktop (Only for e17 atm).
#   [ ] Get the call_enlightenment function to work for the Enlightenment Desktop.
#   [ ] Get the call_kde4 function for KDE4 to work without a restart of the plasma desktop. run some ./qt_cpp maybe?
#   [x] I think I should cache the result of $WALLPAPER for faster switching of wallpapers.
# -------------------------------------------------------
set -e

# Set the path to your own wallpaper directory
WALLPAPER_PATH="$HOME/Bilder/Sample Pictures/"


#########################################################
 # display usage
usage() {
    printf "%s\n\n%s" \
    "$0 is an experimental wallpaper changer that works with most Linux Desktops."\
    "Usage: $0 [arguments]"\
    
    printf "\n  %s\t\t%s" \
    "-p (--prev)"	"Show previous Wallpaper"\
    "-n (--next)"	"Show next Wallpaper"\
    "-r (--resort)"	"Randomize Wallpaper list"\
    "-s (--rescan)"	"Rescan Wallpaper directories"
    printf "\n"

    printf "\n  %s\t\tSetup for the %s" \
    "--xfce"		"XFCE4-Desktop"\
    "--hacker"		"i3 or xmonad Desktop"\
    "--gnome2"		"GNOME2-Desktop"\
    "--gnome3"		"GNOME3-Desktop"\
    "--kde3"		"KDE3-Desktop"\
    "--kde4"		"KDE4-Desktop (experimental)"\
    "--edesk"		"Enlightenment Desktop"
    printf "\n"
}
 
create_queue() {
    find "$WALLPAPER_PATH" -type f -iregex ".*\.\(jpg\|gif\|png\|jpeg\)$" > /tmp/krawall_queue
}
 
shuffle_wallpapers() {
    if [[ ! -f "/tmp/krawall_last" ]] ; then
	create_queue
    fi
    cp -f /tmp/krawall_head /tmp/krawall_last
    cat /tmp/krawall_queue | shuf -n 1 > /tmp/krawall_head
}
 
# if less than two arguments supplied, display usage
if [[  $1 == "" ]] ; then
    usage
    exit 1
fi

krawall ()
{
    local needle=$1
    shift
    while [[ $1 == -* ]]
    do
        # By convention, "--" means end of options.
        case "$1" in
            --)      return 1 ;;
            $needle) return 0 ;;
        esac

        shift
    done
    return 1
}

# Show the user some help when -h or --help is supplied
krawall -h "$@" || krawall --help "$@" &&
    usage

# shuffle wallpaper queue, or create it
if [[ ! -f "/tmp/krawall_queue" ]] ; then
    create_queue
else
    krawall -p "$@" || krawall --prev "$@" &&
	NEXT_WALLPAPER="$(cat /tmp/krawall_last 2>/dev/null)" &&
	printf "%s\n" "Switching to previous Wallpapers."

    krawall -n "$@" || krawall --next "$@" &&
	shuffle_wallpapers &&
	NEXT_WALLPAPER="$(cat /tmp/krawall_head 2>/dev/null)" &&
	printf "%s\n" "Switching to next Wallpapers."

    krawall -r "$@" || krawall --resort "$@" &&
	shuffle_wallpapers &&
	printf "%s\n" "Successfully randomized the wallpaper list."

    krawall -s "$@" || krawall --rescan "$@" &&
	printf "%s\n" "rescan is set" &&
	rm -f /tmp/krawall_queue > /dev/null &&
	create_queue &&
	printf "%s\n" "Successfully created the wallpaper list."
fi

FILENAME=$(echo "$NEXT_WALLPAPER" | sed 's:.*/::')

call_xfce() {
    xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$NEXT_WALLPAPER"
    printf "%s\n" "xfce is set"
}

call_xmonad_i3() {
    feh --bg-scale "$NEXT_WALLPAPER"
    printf "%s\n" "i3/xmonad is set"
}

call_gnome2() {
    gconftool-2 --set /desktop/gnome/background/picture_filename --type string "$NEXT_WALLPAPER"
    printf "%s\n" "gnome2 is set"
}

call_gnome3() {
    gsettings set org.gnome.desktop.background picture-uri "file://$NEXT_WALLPAPER"
    printf "%s\n" "gnome3 is set"
}

call_kde3() {
    dcop kdesktop KBackgroundIface setWallpaper "$NEXT_WALLPAPER" 1
    printf "%s\n" "kde3 is set"
}

call_kde4() {
    kde4_script=$(mktemp) && cat > $kde4_script \
 
<<_EOF
  var wallpaper = "$NEXT_WALLPAPER";
  var activity = activities()[0];
  activity.currentConfigGroup = ["Wallpaper","image"];
  activity.writeConfig("wallpaper", wallpaper);
  activity.writeConfig("userswallpaper", wallpaper);
  activity.reloadConfig();
_EOF
    
    # Run the above javascript, then remove the temp file
    qdbus org.kde.plasma-desktop /MainApplication loadScriptInInteractiveConsole $kde4_script > /dev/null && sleep 10
    rm -f "$kde4_script"
    
    # Let's reparse the configuration
    dbus-send --dest=org.kde.plasma-desktop /MainApplication org.kde.plasma-desktop.reparseConfiguration
    dbus-send --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ReloadConfig
    dbus-send --dest=org.kde.kwin /KWin org.kde.KWin.reloadConfig
    
    # Run again to be sure, because I don't know what I do
    #qdbus org.kde.plasma-desktop /MainApplication reparseConfiguration
    #qdbus org.freedesktop.DBus /org/freedesktop/DBus ReloadConfig
    #qdbus org.kde.kwin /KWin reconfigure
    
    
    # This is stupid! I don't want to restart plasma, but have to? Isn't there a better way????????
    kbuildsycoca4 2>/dev/null && kquitapp plasma-desktop 2>/dev/null ; kstart plasma-desktop > /dev/null 2>&1
    
    # I'll keep the maybe useless crap below...
    #kwriteconfig --file plasma-desktop-appletsrc --group Containments --group $GROUP_ID --group Wallpaper --group image --key "$NEXT_WALLPAPER"
    #WALLPAPER="$(kreadconfig --file plasma-desktop-appletsrc --group Containments --group $GROUP_ID --group Wallpaper --group image --key wallpaper)"
    #CurrentDesktopID="$(qdbus org.kde.KWin /KWin currentDesktop)"
    
    printf "%s\n" "kde4 is set"
}

# forked from: https://gist.github.com/rumia/4565562#file-imgtoedj-sh-L8
call_enlightenment() {

    OUTPUT_DIR="$HOME/.e/e/backgrounds"
    TEMPLATE='
    images { image: "@IMAGE@" USER; }
    collections {
      group {
	  name: "e/desktop/background";
	  data { item: "style" "4"; item: "noanimation" "1"; }
	  max: @WIDTH@ @HEIGHT@;
	  parts {
	    part {
		name: "bg";
		mouse_events: 0;
		description {
		  state: "default" 0.0;
		  aspect: @ASPECT@ @ASPECT@;
		  aspect_preference: NONE;
		  image { normal: "@IMAGE@"; scale_hint: STATIC; }
		}
	    }
	  }
      }
    }
    '
   
    #-desktop-bg-add OPT1 OPT2 OPT3 OPT4 OPT5 Add a desktop bg definition.
    # OPT1 = ContainerNo OPT2 = ZoneNo OPT3 = Desk_x. OPT4 = Desk_y. OPT5 = bg file path
    desk_x_count=$(enlightenment_remote -desktops-get | awk '{print $1}')
    desk_y_count=$(enlightenment_remote -desktops-get | awk '{print $2}')

    for ((x=0; x<$desk_x_count; x++)); do
	for ((y=0; y<$desk_y_count; y++)); do
    
	    shuffle_wallpapers
	    NEXT_WALLPAPER="$(cat /tmp/krawall_head 2>/dev/null)"
	    FILENAME=$(echo "$NEXT_WALLPAPER" | sed 's:.*/::')
	    #echo "$x" "$y $OUTPUT_DIR/${FILENAME%%.*}.edj"

	    DIMENSION="$(identify -format "%w/%h" "$NEXT_WALLPAPER")"
	    if [ ! -z "$DIMENSION" ]; then
		WIDTH=$(echo $DIMENSION | cut -d/ -f1)
		HEIGHT=$(echo $DIMENSION | cut -d/ -f2)
		IMAGE="$(echo "$NEXT_WALLPAPER" | sed 's/[^[:alnum:]_-]/\\&/g')"
		if [ -z "$HEIGHT" -o "$HEIGHT" = "0" ]; then
		    ASPECT="0.0"
		else
		    ASPECT=$(echo "scale=9; $DIMENSION" | bc)
		fi

		printf "%s" "$TEMPLATE" | \
		sed "s/@ASPECT@/$ASPECT/g; s/@WIDTH@/$WIDTH/g; s/@HEIGHT@/$HEIGHT/g; s|@IMAGE@|$IMAGE|g" > /tmp/krawall.edc
		edje_cc -nothreads /tmp/krawall.edc -o "$OUTPUT_DIR/${FILENAME%%.*}.edj" 2>/dev/null
		rm /tmp/krawall.edc
		#echo "Compiled $OUTPUT_DIR/${FILENAME%%.*}.edj"
	    fi	
	    
	    enlightenment_remote -desktop-bg-del 0 0 "$x" "$y"
	    enlightenment_remote -desktop-bg-add 0 0 "$x" "$y" $OUTPUT_DIR/${FILENAME%%.*}.edj;
	done;
    done    

    printf "%s\n" "enlightenment is set"
}


# Change wallpaper when xfce4-desktop is set.
krawall --xfce "$@" &&
    call_xfce

# Change wallpaper when xfce4-desktop i3 or xmonad is set.
krawall --hacker "$@" || krawall --xmonad "$@" || krawall --i3 "$@" &&
    call_xmonad_i3

# Change wallpaper when gnome2 is set.
krawall --gnome2 "$@" &&
    call_gnome2

# Change wallpaper when gnome3 is set.
krawall --gnome3 "$@" &&
    call_gnome3

# Change wallpaper when kde3 is set.
krawall --kde3 "$@" &&
    call_kde3
    
# Change wallpaper when kde4 is set.  
krawall --kde4 "$@" &&
    call_kde4
    
# Change wallpaper when enlightenment is set.  
krawall --e17 "$@" || krawall --edesk "$@" &&
    call_enlightenment