#!/usr/bin/env bash

CURSORPOS=$(hyprctl cursorpos -j)

X_POS=$(jq '.x' <<< $CURSORPOS)
Y_POS=$(jq '.y' <<< $CURSORPOS)

ACTIVE_WORKSPACE=$(hyprctl activeworkspace -j | jq '.id')

hyprctl clients -j | jq "$(cat << EOF
  .[] |
  select(.workspace.id == $ACTIVE_WORKSPACE) |
  select(
    .at[0] <= $X_POS
    and .at[1] <= $Y_POS
    and (.at[0] + .size[0] >= $X_POS)
    and (.at[1] + .size[1] >= $Y_POS)
  ) | .pid
EOF
)" | tail -n 1

