master ohmu/wmutils / src / contrib / underneath.sh
ohmu  ·  2026-07-25
 1#!/bin/sh
 2#
 3# wildefyr - 2016 (c) MIT
 4# print window id directly underneath mouse
 5
 6# fuck sh
 7intCheck() {
 8    test $1 -ne 0 2> /dev/null
 9    test $? -ne 2 || return 1
10}
11
12# find current mouse location or check input for XY coordinates
13test $# -eq 0 && {
14    pointerX="$(wmp | cut -d\  -f 1)"
15    pointerY="$(wmp | cut -d\  -f 2)"
16} || {
17    intCheck $1 && pointerX=$1 || return 1
18    intCheck $2 && pointerY=$2 || return 1
19}
20
21# start from the currently highest stacked window
22for wid in $(lsw | tac); do
23    windowX="$(wattr x $wid)"
24    windowY="$(wattr y $wid)"
25
26    # we won't get a match if the left and top edges are greater than X or Y
27    test "$windowX" -gt "$pointerX" && continue
28    test "$windowY" -gt "$pointerY" && continue
29
30    windowW="$(wattr w $wid)"
31    windowH="$(wattr h $wid)"
32
33    # we won't get a match if the right and bottom edges are less than X or Y
34    test "$((windowX + windowW))" -lt "$pointerX" && continue
35    test "$((windowY + windowH))" -lt "$pointerY" && continue
36
37    # print match!
38    printf '%s\n' "$wid" 
39    unset -v wid
40    exit
41done