summaryrefslogtreecommitdiff
path: root/herbstluftwm/panel.sh
blob: ad7efbe2ac81976ff596f2d7124e86b4e8a3ca93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash

source ${HOME}/.config/herbstluftwm/fn.sh

hc() { "${herbstclient_command[@]:-herbstclient}" "$@" ;}
monitor=${1:-0}
geometry=( $(herbstclient monitor_rect "$monitor") )
if [ -z "$geometry" ] ;then
    echo "Invalid monitor $monitor"
    exit 1
fi
# geometry has the format W H X Y
x=${geometry[0]}
y=${geometry[1]}
panel_width=${geometry[2]}
panel_height=16
#font="-*-envy code r-*-*-*-*-11-*-*-*-*-*-*-*"
xfont=$(get_xresources font)
echo "${xfont}" > log
escaped="${xfont//[a-z 0-9\*-]/}" 
if [[ ${#escaped} -gt 0 ]]
then
    font="-Misc-Termsyn.Icons-Medium-R-Normal--11-79-100-100-C-60-ISO8859-1"
else
    font="${xfont}"
    
    if [[ "${xfont}" =~ fixed ]]
    then
        font="${xfont/13/12}"
    fi
fi
bgcolor=$(get_xresources background)
fgcolor=$(get_xresources foreground)
selbg=$(get_xresources color12)
selfg=$(get_xresources background)
popups="/home/matias/.config/herbstluftwm/popups.sh"

####
# Load the font for the panel, should implement something more optimized
# But for now it's fine
xset +fp /usr/share/fonts/local/
xset fp rehash

####
# Try to find textwidth binary.
# In e.g. Ubuntu, this is named dzen2-textwidth.
if which textwidth &> /dev/null ; then
    textwidth="textwidth";
elif which dzen2-textwidth &> /dev/null ; then
    textwidth="dzen2-textwidth";
else
    echo "This script requires the textwidth tool of the dzen2 project."
    exit 1
fi
####
# true if we are using the svn version of dzen2
# depending on version/distribution, this seems to have version strings like
# "dzen-" or "dzen-x.x.x-svn"
if dzen2 -v 2>&1 | head -n 1 | grep -q '^dzen-\([^,]*-svn\|\),'; then
    dzen2_svn="true"
else
    dzen2_svn=""
fi

if awk -Wv 2>/dev/null | head -1 | grep -q '^mawk'; then
    # mawk needs "-W interactive" to line-buffer stdout correctly
    # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=593504
    uniq_linebuffered() {
      awk -W interactive '$0 != l { print ; l=$0 ; fflush(); }' "$@"
    }
else
    # other awk versions (e.g. gawk) issue a warning with "-W interactive", so
    # we don't want to use it there.
    uniq_linebuffered() {
      awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
    }
fi

hc pad $monitor $panel_height

{
    ### Event generator ###
    # based on different input data (mpc, date, hlwm hooks, ...) this generates events, formed like this:
    #   <eventname>\t<data> [...]
    # e.g.
    #   date    ^fg(#efefef)18:33^fg(#909090), 2013-10-^fg(#efefef)29

    #mpc idleloop player &
    while true ; do
        # "date" output is checked once a second, but an event is only
        # generated if the output changed compared to the previous run.
        date +$'date\t^fg('"$fgcolor"')%H:%M^fg(#707070), %Y-%m-^fg('"$fgcolor"')%d'
        sleep 3 || break
    done > >(uniq_linebuffered) &
    childpid=$!

    while true; do
        mpc_output="$(mpc -f "player\t%title% - %artist%" || echo -n "player\t")"
        echo "$mpc_output" | head -n 1
        sleep 11 || break
    done > >(uniq_linebuffered) &
    mpcpid=$!

    hc --idle
    kill $childpid
    kill $mpcpid
} 2> /dev/null | {
    IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
    visible=true
    date=""
    windowtitle=""
    while true ; do

        ### Output ###
        # This part prints dzen data based on the _previous_ data handling run,
        # and then waits for the next event to happen.

        bordercolor="#26221C"
        separator="^bg()^fg($selbg)|"
        # draw tags
        for i in "${tags[@]}" ; do
            case ${i:0:1} in
                '#')
                    # Current tag
                    echo -n "^bg($selbg)^fg($selfg)"
                    ;;
                '+')
                    # Dunno
                    echo -n "^bg(#9CA668)^fg(#141414)"
                    ;;
                ':')
                    # with something
                    echo -n "^bg()^fg($fgcolor)"
                    ;;
                '!')
                    # urgent
                    echo -n "^bg(#FF0675)^fg(#141414)"
                    ;;
                *)
                    # Empty
                    echo -n "^bg()^fg(#797979)"
                    ;;
            esac
            if [ ! -z "$dzen2_svn" ] ; then
                # clickable tags if using SVN dzen
                echo -n "^ca(1,\"${herbstclient_command[@]:-herbstclient}\" "
                echo -n "focus_monitor \"$monitor\" && "
                echo -n "\"${herbstclient_command[@]:-herbstclient}\" "
                echo -n "use \"${i:1}\") ${i:1} ^ca()"
            else
                # non-clickable tags if using older dzen
                echo -n " ${i:1} "
            fi
        done
        echo -n "$separator"
        echo -n "^bg()^fg() ${windowtitle//^/^^}"
        # small adjustments
        battery=$(expr $(expr $(cat /sys/class/power_supply/BAT*/charge_now) \* 100) / $(cat /sys/class/power_supply/BAT*/charge_full))
        if [ "$battery" = "/" ] ;then
            right="$separator^bg($hintcolor) $date $separator"
        else
            right="^bg($hintcolor) ^fg($fgcolor)$song $separator "
            if [[ $battery -lt 20 ]]
            then
                right="$right^fg($(get_xresources color1))$battery%% $separator $date"
            elif [[ $battery -lt 50 ]]
            then
                right="$right^fg($(get_xresources color3))$battery%% $separator $date"
            else
                right="$right^fg($fgcolor)$battery%% $separator $date"
            fi
        fi
        right_text_only=$(echo -n "$right" | sed 's.\^[^(]*([^)]*)..g')

        # get width of right aligned text.. and add some space..
        width=$($textwidth "$font" "$right_text_only ")
        echo "panel width: $panel_width and widht $width for '$font' and '$right_text_only'" >> log
        echo -n "^pa($(($panel_width - $width + 175)))$(printf "$right")"
        echo

        ### Data handling ###
        # This part handles the events generated in the event loop, and sets
        # internal variables based on them. The event and its arguments are
        # read into the array cmd, then action is taken depending on the event
        # name.
        # "Special" events (quit_panel/togglehidepanel/reload) are also handled
        # here.

        # wait for next event
        IFS=$'\t' read -ra cmd || break
        # find out event origin
        case "${cmd[0]}" in
            tag*)
                #echo "resetting tags" >&2
                IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
                ;;
            date)
                #echo "resetting date" >&2
                date="^ca(1, ${popups} cal)${cmd[@]:1}^ca()"
                ;;
            quit_panel)
                exit
                ;;
            togglehidepanel)
                currentmonidx=$(hc list_monitors | sed -n '/\[FOCUS\]$/s/:.*//p')
                if [ "${cmd[1]}" -ne "$monitor" ] ; then
                    continue
                fi
                if [ "${cmd[1]}" = "current" ] && [ "$currentmonidx" -ne "$monitor" ] ; then
                    continue
                fi
                echo "^togglehide()"
                if $visible ; then
                    visible=false
                    hc pad $monitor 0
                else
                    visible=true
                    hc pad $monitor $panel_height
                fi
                ;;
            reload)
                exit
                ;;
            focus_changed|window_title_changed)
                windowtitle="${cmd[@]:2}"
                ;;
            player)
                if [[ x"${cmd[@]:1}" != "x" ]]
                then
                    song="^ca(1, ${popups} music) music^ca()"
                else
                    song="wep"
                fi
                ;;
        esac
    done

    ### dzen2 ###
    # After the data is gathered and processed, the output of the previous block
    # gets piped to dzen2. font -> -fn "$font" 

} 2> /dev/null | dzen2 -w $panel_width -x $x -y $y -fn "$font" -h $panel_height \
    -e 'button3=;button4=exec:herbstclient use_index -1;button5=exec:herbstclient use_index +1' \
    -ta l -bg "$bgcolor" -fg "$fgcolor"