Skip to content

Instantly share code, notes, and snippets.

@crispyricepc
Last active October 21, 2024 13:14
Show Gist options
  • Save crispyricepc/f313386043395ff06570e02af2d9a8e0 to your computer and use it in GitHub Desktop.
Save crispyricepc/f313386043395ff06570e02af2d9a8e0 to your computer and use it in GitHub Desktop.
wlprop - An xprop clone for wlroots based compositors

Dependencies

Make sure you have installed the following commands:

  • swaymsg
  • jq
  • slurp
  • awk

Installation

curl -L https://gist.github.com/crispyricepc/f313386043395ff06570e02af2d9a8e0/raw/8b06f025a0f34685f5ffc2000fd19a64754c9b29/wlprop.sh > ~/.local/bin/wlprop
chmod +x ~/.local/bin/wlprop

Usage

Type wlprop in a terminal, then click the window you'd like to get the properties of.

Since all wlprop outputs is JSON, you can filter the values you need using jq:

# Get just the name of the window
wlprop | jq -r '.name'
# Get the app_id value (useful for `for_window` configs in sway)
wlprop | jq -r '.app_id'
#!/usr/bin/env sh
# wlprop
#
# Licensed under the MIT license
# Copyright © 2022 [email protected]
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Dependencies:
# - swaymsg
# - jq
# - slurp
# - awk
# Get the sway tree and store the output
SWAY_TREE=$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?)')
# Invoke slurp to let the user select a window
SELECTION=$(echo $SWAY_TREE | jq -r '.rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp)
# Extract the X, Y, Width, and Height from the selection
X=$(echo $SELECTION | awk -F'[, x]' '{print $1}')
Y=$(echo $SELECTION | awk -F'[, x]' '{print $2}')
W=$(echo $SELECTION | awk -F'[, x]' '{print $3}')
H=$(echo $SELECTION | awk -F'[, x]' '{print $4}')
# Find the window matching the selection
echo $SWAY_TREE | jq -r --argjson x $X --argjson y $Y --argjson w $W --argjson h $H \
'. | select(.rect.x == $x and .rect.y == $y and .rect.width == $w and .rect.height == $h)'
@ItsTerm1n4l
Copy link

No, sorry, I should have been more specific, I was wondering if I could use it with wayfire, but neither of the scripts mentioned above could do that as the use swaymsg and hyperctl. Thank you.
I love your profile description btw.

@iguanajuice
Copy link

Consider changing slurp to slurp -r. That way, it'll still work in case you accidentally drag the cursor while clicking a window.

@majamin
Copy link

majamin commented Oct 6, 2024

Thanks for this. @magicmonty I used your comment here, also added -r switch as suggested by @iguanajuice . Here's a version for Hyprland, with dep checks:

#!/usr/bin/env sh

# wlprop - Hyprland
#
# Licensed under the MIT license
# Copyright © 2022 [email protected]
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# 
# Dependencies:
# - hyprctl
# - jq
# - slurp
# - awk

# Dependencies
cmd_failures=''
for cmd in hyprctl jq slurp awk; do
  command -v "${cmd}" >/dev/null 2>&1 || cmd_failures="${cmd_failures},${cmd}"
done

if (( "${#cmd_failures}" > 0 )); then
  printf -- '%s\n' "The following dependencies are missing: ${cmd_failures/,/}" >&2
  exit 1
fi

# Get all clients and selection (point) region via slurp
TREE=$(hyprctl clients -j | jq -r '.[] | select(.hidden==false and .mapped==true)')
SELECTION=$(echo $TREE | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | slurp -r)

# Get individual coordinates
X=$(echo $SELECTION | awk -F'[, x]' '{print $1}')
Y=$(echo $SELECTION | awk -F'[, x]' '{print $2}')
W=$(echo $SELECTION | awk -F'[, x]' '{print $3}')
H=$(echo $SELECTION | awk -F'[, x]' '{print $4}')

echo $TREE | jq -r --argjson x $X --argjson y $Y --argjson w $W --argjson h $H '. | select(.at[0]==$x and .at[1]==$y and .size[0]==$w and.size[1]==$h)

, and then something like:

hyprctl setprop address:$(wlprop | jq -r '.["address"]') opaque 1

to set properties, or:

hyprctl dispatch closewindow address:$(wlprop | jq -r '.["address"]')

to close a window.

@GOKOP
Copy link

GOKOP commented Oct 10, 2024

The script as posted in the main gist and as packaged in several distros by now, isn't "for wl-roots based compositors" but "for Sway" and should be called swayprop instead. Calling is wlprop is misleading when it depends on a tool that only works with one specific compositor

@majamin
Copy link

majamin commented Oct 10, 2024

@GOKOP, you're right, I see it on the AUR (where it's maintained by maintained by NicolasVontobel at protonmail dot com, I'm assuming that's @NiciTheNici) and NixOS Hydra (where it has no maintainer). Unfortunately, it's the responsibility of the package maintainers to use the correct script, not @crispyricepc. The naming of the script is unfortunate, but there's no reason why it couldn't handle different compositors. I wonder if @crispyricepc would be up for a pull request? There should be a more sophisticated logic to handle different compositors.

@tacerus
Copy link

tacerus commented Oct 21, 2024

Hi, we also have it in openSUSE as wlprop, currently with a dependency on Sway. My idea was to keep the generic name to allow for expansion to other Wayland compositors if someone contributes such in the future (for example the Hyprland one by majamin could be integrated). That would make for a more similar experience to xprop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment