[
  {
    "path": "README.md",
    "content": "# blurme\n\n<p>blurme v1.0 Codename MEH (My Eyes Hurt) is a small script which will blur your feh set background if any window, or rofi is open. Furthermore there are some options, which allow you to change the default behaviour a bit. Read more about the options down below.</p>\n<p>Please feel free to fork it, make pull requests and use it in your own projects.</p>\n<p>This is my first project i publish. Please be gentle on me :)</p>\n<h2>How to start</h2>\n<h3>Requirements</h3>\n<ol>\n<li><b>wmctrl</b>: This script makes use of wmctrl to get the current workspace number and to clarify, whether any window is open. It's essential!</li>\n<li><b>feh</b>: Furthermore you'll need feh to set your background image(s).</li>\n<li><b>imagemagick</b>: And last but not least imagemagick is mandatory to blur the images</li>\n</ol>\n<h3>Download and ...</h3>\n<ol>\n    <li>Place it anywhere you like (e.g. ~/.scripts)</li>\n    <li>Make it executable (chmod +x ~/.scripts/blurme)</li>\n</ol>\n<h2>How to run it</h2>\n<h3>Manual start</h3>\n<p>Run it once in a terminal of your choice with ~/.scripts/blurme</p>\n<h3>Autostart</h3>\n<h4>i3</h4>\n<p>Add the line \"exec --no-startup-id ~/.scripts/blurme &\" to your i3 config.</p>\n<h4>bspwm</h4>\n<p>Add the line \"sh ~/.scripts/blurme &\" to ~/.xinitrc</p>\n<h4>openbox</h4>\n<p>Add the line \"sh ~/.scripts/blurme &\" to ~/.config/openbox/autostart</p>\n<h3>Options</h3>\n<p>There are several optional arguments provided that may be used. These include:</p>\n<ul>\n    <li><b>-a</b>: Add another program (process name), which will start the blur effect</li>\n    <li><b>-c</b>: set custom directory for transition images (default: ~/.cache/blurme)</li>\n    <li><b>-d</b>: Set custom directory (default: ~/.local/share/blurme)</li>\n    <li><b>-t</b>: Set custom transition time in sec (default: 0.01)</li>\n    <li><b>-v</b>: Show additional output (verbose)</li>\n    <li><b>-h</b>: Show all these parameters</li>\n</ul>\n"
  },
  {
    "path": "blurme",
    "content": "#!/bin/bash\n\n# Error Prevention: Multiple Instances ----------------------------------\n\nif [[ $(pgrep -cl blurme) > 1 ]]; then\n    echo 'BLURME: Another instance of blurme me is already running.'\n    echo '        Please kill it with fire (pkill blurme) first, then try running me again.'\n    exit\nfi\n\n\n\n# Options ---------------------------------------------------------------\n\nwhile getopts :a:c:d:t:vh option; do\n    case \"${option}\" in\n        a) ADDBLUR=${OPTARG};;\n        c) CACHEDIR=${OPTARG};;\n        d) WORKINGDIR=${OPTARG};;\n        t) TIME=${OPTARG};;\n        v) VERBOSE=true;;\n        h)\n            echo '---------------------------------------'\n            echo 'BLURME v1.0 Codename MEH (My Eyes Hurt)'\n            echo '---------------------------------------'\n            echo \"Options:  '-a' adds one additional program, which will start the blur effect (must be process name!)\" 1>&2\n            echo \"          '-c' set custom directory for transition images (default: ~/.cache/blurme)\" 1>&2\n            echo \"          '-d' set custom directory (default: ~/.cache/blurme)\" 1>&2\n            echo \"          '-t' set custom transition time in sec (default: 0.01)\" 1>&2\n            echo \"          '-v' set verbose true\" 1>&2\n            exit\n            ;;\n        \\?)\n            echo 'BLURME: Invalid option! Please use as shown below:' 1>&2\n            echo \"        '-a' adds one additional program, which will start the blur effect (must be process name!)\" 1>&2\n            echo \"        '-c' set custom directory for transition images (default: ~/.cache/blurme)\" 1>&2\n            echo \"        '-d' set custom directory (default: ~/.cache/blurme)\" 1>&2\n            echo \"        '-t' set custom transition time in sec (default: 0.01)\" 1>&2\n            echo \"        '-v' set verbose true\" 1>&2\n            exit\n            ;;\n        :) echo \"BLURME: '-$OPTARG' requires an argument\" 1>&2; exit;;\n    esac\ndone\nshift $((OPTIND-1))\n\nif [ \"$VERBOSE\" = true ]; then\n    echo '---------------------------------------'\n    echo 'BLURME v1.0 Codename MEH (My Eyes Hurt)'\n    echo '---------------------------------------'\nfi\n\n\n\n# Variables -------------------------------------------------------------\n\n# Working directory contains a copy of the original unblured image,\n# and its path.\nif [ -z \"$WORKINGDIR\" ]; then WORKINGDIR=/home/$USER/.local/share/blurme; fi\nif ! [ -d $WORKINGDIR ]; then mkdir $WORKINGDIR; fi\nif [ \"$VERBOSE\" = true ]; then echo 'BLURME: Workingdir is set to '$WORKINGDIR; fi\n\n# Cache directory contains all blurry rendered transition images.\nif [ -z \"$CACHEDIR\" ]; then CACHEDIR=/home/$USER/.cache/blurme; fi\nif ! [ -d $CACHEDIR ]; then mkdir $CACHEDIR; fi\nif [ \"$VERBOSE\" = true ]; then echo 'BLURME: Cache (contains transition images) is set to '$CACHEDIR; fi\n\n\n# Error Prevention: Blurry startup\n#\n# Check if background is set to not blurred on start.\n# If for some reason the script wasn't terminated correctly,\n# the background might be still blurry, and the logic will go crazy.\n# This only fix only works when the workingdir and its containing\n# copy of the original not blurred background still exist\n# after reboot (no temp directory!).\nif [[ $(tail -n1 ~/.fehbg | cut -d \"'\" -f2) = \"$CACHEDIR/trans-9\" ]]; then\n    # Reset wallpaper to original one\n    feh --bg-fill $(cat \"$WORKINGDIR/original-bg\")\nfi\n\n\nif [ -z \"$TIME\" ]; then TIME=0.01; fi\n#echo \"Transition time is set to: \"$TIME\n\nCURRWORKSPACE=$(wmctrl -d | grep '*' | cut -d ' ' -f1)\n#echo \"Current workspace: $CURRWORKSPACE\"\n\nOPENWINDOWS=$(wmctrl -l | cut -d ' ' -f3 | grep $CURRWORKSPACE | wc -l)\n#echo \"Open windows on workspace$CURRWORKSPACE: $WINDOWS\"\n\nCURRWALLPAPER=$(tail -n1 ~/.fehbg | cut -d \"'\" -f2)\n#echo \"Wallpaper path: $CURRWALLPAPER\"\n\nORIGINAL=$WORKINGDIR/original-bg\necho $CURRWALLPAPER > $ORIGINAL\n\nCOPYBG=$WORKINGDIR/copied-bg\ncp $(cat $ORIGINAL) $COPYBG\n\n\n\n\n\n\n# Functions -------------------------------------------------------------\n\nfunction create_images {\n\n    if [ \"$VERBOSE\" = true ]; then echo 'BLURME: Create transition images ...'; fi\n\n    local i=\"0\"\n\n    while [ $i -lt \"10\" ]; do\n        A=$(echo \"($i + 1) * 2.4\" | bc -l)\n        B=$(echo \"($i + 1) * 1.2\" | bc -l)\n        convert $COPYBG -blur $A,$B $CACHEDIR/trans-$i\n        local PERC=$(echo \"($i+1) * 10\" | bc -l)\n        if [ \"$VERBOSE\" = true ]; then echo 'BLURME: '$PERC'%'; fi\n        i=$[$i+1]\n    done\n\n    if [ \"$VERBOSE\" = true ]; then echo 'BLURME: Done!'; fi\n}\n\nfunction transinit {\n\n    local REF=$CACHEDIR/trans-0\n    local CMP=/tmp/blurme-trans-0.tmp\n\n    if [ \"$VERBOSE\" = true ]; then\n        echo \"BLURME: Huh?! Background is not blurred, let's fix that!\"\n        echo 'BLURME: Unblurred original wallpaper is '$(cat $ORIGINAL)\n    fi\n\n    # is it really necessary to render again\n    if [ -f $REF ]; then\n        convert $(cat $ORIGINAL) -blur 2.4,1.2 $CMP\n        # create new transition images if newly created trans-0 and old trans-0 are not the same\n        if ! cmp -s $CMP $REF; then\n            if [ \"$VERBOSE\" = true ]; then echo 'BLURME: Seems like we have a new wallpaper which needs to be rendered.'; fi\n            rm $CMP\n            create_images\n        else\n            if [ \"$VERBOSE\" = true ]; then echo 'BLURME: This background seems familiar. No need to render it again! :)'; fi\n            rm $CMP\n        fi\n    else\n        # reference file not found inside existing working dir ... better render them, to get things working\n        if [ \"$VERBOSE\" = true ]; then echo \"BLURME: Could'nt find transition images in cachedir. Will create them on the fly.\"; fi\n        create_images\n    fi\n}\n\nfunction transition {\n\n    if [ \"$VERBOSE\" = true ]; then echo 'BLURME: Blur wallpaper ...'; fi\n\n    local j=\"0\"\n\n    while [ $j -lt \"10\" ]; do\n        feh --bg-fill $CACHEDIR/trans-$j\n        local PERC=$(echo \"($j+1) * 10\" | bc -l)\n        if [ \"$VERBOSE\" = true ]; then echo 'BLURME: '$PERC'%'; fi\n        j=$[$j+1]\n        sleep $TIME\n    done\n    if [ \"$VERBOSE\" = true ]; then echo 'BLURME: Done!'; fi\n}\n\nfunction transition_rev {\n\n    if [ \"$VERBOSE\" = true ]; then echo 'BLURME: Unblur wallpaper ...'; fi\n\n    local j=\"9\"\n\n    while [ $j -ge \"0\" ]; do\n        feh --bg-fill $CACHEDIR/trans-$j\n        local PERC=$(echo \"($j+1) * 10\" | bc -l)\n        if [ \"$VERBOSE\" = true ]; then echo 'BLURME: '$PERC'%'; fi\n        j=$[$j-1]\n        sleep $TIME\n    done\n\n    feh --bg-fill $(cat $ORIGINAL)\n    if [ \"$VERBOSE\" = true ]; then echo 'BLURME: Done!'; fi\n}\n\nfunction finish {\n    feh --bg-fill $(cat $ORIGINAL)\n    exit\n}\n\n\n# Logic ------------------------------------------------------------------\n\ntrap finish EXIT\ntrap finish SIGHUP\ntrap finish SIGINT\ntrap finish SIGKILL\ntrap finish SIGTERM\n\ntransinit\n\nwhile true; do\n\n    CURRWORKSPACE=$(wmctrl -d | grep '*' | cut -d ' ' -f1)\n    OPENWINDOWS=$(wmctrl -l | cut -d ' ' -f3 | grep $CURRWORKSPACE | wc -l)\n    CURRWALLPAPER=$(tail -n1 ~/.fehbg | cut -d \"'\" -f2)\n\n    # is any window or rofi open on current workspace\n    if [[ $OPENWINDOWS > 0 ]] || [[ $(pgrep -cl rofi) > 0 ]]  || [[ $(pidof $ADDBLUR) > 0 ]]; then\n        # is wallpaper not blurred\n        if [ \"$CURRWALLPAPER\" != \"$CACHEDIR/trans-9\" ]; then\n            # has wallpaper been changed\n            if [ \"$CURRWALLPAPER\" != \"$(cat $ORIGINAL)\" ]; then\n                # save current wallpapers path\n                echo $CURRWALLPAPER > $ORIGINAL\n                # copy current wallpaper as reference\n                cp $(cat $ORIGINAL) $COPYBG\n                # create transition images\n                transinit\n            fi\n            # set wallpapers to blur\n            transition\n        fi\n    else\n        # no windows or rofi open and wallpaper is not the unblurred original\n        if [ \"$CURRWALLPAPER\" != \"$(cat $ORIGINAL)\" ]; then\n            # unblur and set to original wallpaper\n            transition_rev\n        fi\n    fi\n\n    sleep 0.5\ndone\n"
  }
]