master ohmu/wmutils / src / contrib / focus.sh
ohmu  ·  2026-07-25
 1#!/bin/sh
 2#
 3# z3bra - 2014 (c) wtfpl
 4# window focus wrapper that sets borders and can focus next/previous window
 5
 6BW=${BW:-2}                    # border width
 7ACTIVE=${ACTIVE:-0xffffff}     # active border color
 8INACTIVE=${INACTIVE:-0x333333} # inactive border color
 9
10# get current window id
11CUR=$(pfw)
12
13usage() {
14    echo "usage: $(basename $0) <next|prev|wid>"
15    exit 1
16}
17
18setborder() {
19    ROOT=$(lsw -r)
20
21    # check if window exists
22    wattr $2 || return
23
24    # do not modify border of fullscreen windows
25    test "$(wattr xywh $2)" = "$(wattr xywh $ROOT)" && return
26
27    case $1 in
28        active)   chwb -s $BW -c $ACTIVE $2 ;;
29        inactive) chwb -s $BW -c $INACTIVE $2 ;;
30    esac
31}
32
33case $1 in
34    next) wid=$(lsw|grep -v $CUR|sed '1 p;d') ;;
35    prev) wid=$(lsw|grep -v $CUR|sed '$ p;d') ;;
36    0x*) wattr $1 && wid=$1 ;;
37    *) usage ;;
38esac
39
40# exit if we can't find another window to focus
41test -z "$wid" && echo "$(basename $0): can't find a window to focus" >&2 && exit 1
42
43setborder inactive $CUR # set inactive border on current window
44setborder active $wid   # activate the new window
45chwso -r $wid           # put it on top of the stack
46wtf $wid                # set focus on it
47
48# you might want to remove this for sloppy focus
49wmp -a $(wattr xy $wid) # move the mouse cursor to
50wmp -r $(wattr wh $wid) # .. its bottom right corner