]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - contrib/autorandr_nitrogen_wallpaper/autorandr_nitrogen_wallpaper
Contrib: script for changing wallpapers on change
[deb_pkgs/autorandr.git] / contrib / autorandr_nitrogen_wallpaper / autorandr_nitrogen_wallpaper
1 #!/bin/bash
2
3 # autorandr_nitrogen_wallpaper
4 # Copyright (c) 2015, Ondra 'Kepi' KudlĂ­k, http://kepi.cz
5 #
6 # Simple script to switch wallpapers when autorandr change profile
7 #
8 # Usage
9 # =====
10 #
11 # 1. place this script (or better symlink it from autorandr location)
12 #    to ~/.config/autorandr/postswitch
13 # 2. run "nitrogen" and set wallpapers for current profile as you wish
14 # 3. run ~/.config/autorandr/postswitch savebg
15 # 4. repeat steps 2-3 for any profile you wish
16 #
17 # License
18 # =======
19 #
20 # Permission is hereby granted, free of charge, to any person obtaining
21 # a copy of this software and associated documentation files (the
22 # "Software"), to deal in the Software without restriction, including
23 # without limitation the rights to use, copy, modify, merge, publish,
24 # distribute, sublicense, and/or sell copies of the Software, and to
25 # permit persons to whom the Software is furnished to do so, subject to
26 # the following conditions:
27
28 # The above copyright notice and this permission notice shall be
29 # included in all copies or substantial portions of the Software.
30
31 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38
39 NITROGEN_HOME="$HOME/.config/nitrogen"
40 NITROGEN_BG="$NITROGEN_HOME/bg-saved.cfg"
41
42 # try to detect profile
43 PROFILE=$(autorandr 2>&1| grep detected |awk '{ print $1 }')
44 [[ -n "$PROFILE" ]] || exit 1
45
46 PROFILE_BG_FILE="bg-saved.$PROFILE.cfg"
47 PROFILE_BG="$NITROGEN_HOME/$PROFILE_BG_FILE"
48
49 # save background for detected profile
50 if [[ $1 = 'savebg' ]]; then
51     # nitrogen config doesnt exist, instruct to run it first
52     if [[ ! -f "$NITROGEN_BG" ]]; then
53         echo "wallpaper: you need to first run 'nitrogen' and set your wallpapers"
54         exit 2
55     fi
56     # nitrogen config exists but is broken symlink, just remove it and instruct to run it first
57     if [ ! -e "$NITROGEN_BG" ] ; then
58         /usr/bin/unlink "$NITROGEN_BG"
59         echo "wallpaper: you need to first run 'nitrogen' and set your wallpapers (config was broken)"
60         exit 3
61     fi
62
63     /bin/cp -L "$NITROGEN_BG" "$PROFILE_BG"
64
65 # load background for detected profile
66 else
67     # we have some profile background config for this setup
68     if [[ -f "$PROFILE_BG" ]]; then
69         # nitrogen original file exists and not symlink
70         if [[ -f "$NITROGEN_BG" ]] && [[ ! -L "$NITROGEN_BG" ]]; then
71             echo "wallpaper: Backing up nitrogen saved bg to $NITROGEN_BG.backup" 
72             /bin/mv "$NITROGEN_BG" "$NITROGEN_BG".backup
73         fi
74         
75         # set symlink
76         echo "wallpaper: Found saved wallpaper for profile $PROFILE - changing"
77         ln -f -s "$PROFILE_BG_FILE" "$NITROGEN_BG"
78         # call nitrogen
79         nitrogen --restore
80     else
81         echo "wallpaper: No saved wallpaper found for profile $PROFILE"
82     fi
83 fi