halfscreen 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. # calculate the size of a window that takes exactly half the screen
  3. # excluding decorations (frame extents).
  4. # net_frame_extents is a little C prog I hacked to get those
  5. # (it needs to create a window to measure its dimensions)
  6. # dependencies
  7. deps=(pidof sed net_frame_extents xprop)
  8. for dep in "${deps[@]}"; do
  9. type -f $dep >/dev/null || __fail__ "Dependency $dep not found."
  10. done
  11. unset dep deps
  12. # Wait for this panel to show up
  13. panel="tint2"
  14. timeout=100 # tenths of s
  15. [ -z "$DISPLAY" ] && { echo "DISPLAY not set. Is Xorg running?"; exit 1; }
  16. if ! pidof "$panel" 1>&2; then
  17. >&2 echo -n "Waiting for $panel"
  18. until ((timeout <= 0)); do sleep 0.1; pidof "$panel" 1>&2 && break || { >&2 echo -n '.'; ((timeout--)); }; done
  19. >&2 echo
  20. fi
  21. (( timeout > 0 )) && {
  22. sleep 1
  23. read left right top bottom <<<"$(net_frame_extents | sed 's/^.*= //; s/,//g')"
  24. read discard discard width height discard <<<"$(xprop -notype -root _NET_WORKAREA| sed 's/^.*= //; s/,//g')"
  25. re='^[0-9]+$' # make sure they're all unsigned integers
  26. if [[ "$left" =~ $re && "$right" =~ $re && "$width" =~ $re && "$top" =~ $re && "$bottom" =~ $re && "$height" =~ $re ]]; then
  27. printf 'export FRAME_EXTENTS=%s,%s,%s,%s\n' "$left" "$right" "$top" "$bottom"
  28. halfscreen_w="$(( width/2 - left - right ))"
  29. printf 'export HALFSCREEN_WIDTH=%s\n' "$halfscreen_w"
  30. halfscreen_h="$(( height - top - bottom ))"
  31. printf 'export HALFSCREEN_HEIGHT=%s\n' "$halfscreen_h"
  32. type -f xmlstarlet openbox 1>&2 && (( halfscreen_w > 0 )) && {
  33. obrc="$HOME/.config/openbox/rc.xml"
  34. >&2 echo "Patching $obrc with new width & height"
  35. # priceless piece of information: https://superuser.com/a/508143
  36. xmlstarlet ed -L -N o="http://openbox.org/3.4/rc" \
  37. -u "/o:openbox_config/o:applications/o:application[@class=\"Halfscreen\" or @name=\"Halfscreen\"]/o:size/o:width" \
  38. -v "$halfscreen_w" \
  39. -u "/o:openbox_config/o:applications/o:application[@class=\"Halfscreen\" or @name=\"Halfscreen\"]/o:size/o:height" \
  40. -v "$halfscreen_h" \
  41. "$obrc" 1>&2 || { >&2 echo "Something went wrong."; exit 1; }
  42. >&2 echo "Reconfiguring openbox"
  43. pidof openbox 1>&2 && openbox --reconfigure
  44. }
  45. else
  46. >&2 echo "Got invalid numbers querying extents."; exit 1
  47. fi
  48. } || { >&2 echo "Panel $panel does not appear to be running"; exit 1; }