dwmswallow 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/dash
  2. # Separator and command prefix, as defined in dwm.c:fakesignal()
  3. SEP='###'
  4. PREFIX='#!'
  5. # Asserts that all arguments are valid X11 window IDs, i.e. positive integers.
  6. # For the purpose of this script 0 is declared invalid aswe
  7. is_winid() {
  8. while :; do
  9. # Given input incompatible to %d, some implementations of printf return
  10. # an error while others silently evaluate the expression to 0.
  11. if ! wid=$(printf '%d' "$1" 2>/dev/null) || [ "$wid" -le 0 ]; then
  12. return 1
  13. fi
  14. [ -n "$2" ] && shift || break
  15. done
  16. }
  17. # Prints usage help. If "$1" is provided, function exits script after
  18. # execution.
  19. usage() {
  20. [ -t 1 ] && myprintf=printf || myprintf=true
  21. msg="$(cat <<-EOF
  22. dwm window swallowing command-line interface. Usage:
  23. $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWER [-c CLASS] [-i INSTANCE] [-t TITLE]$($myprintf "\033[0m")
  24. Register window $($myprintf "\033[3m")SWALLOWER$($myprintf "\033[0m") to swallow the next future window whose attributes
  25. match the $($myprintf "\033[3m")CLASS$($myprintf "\033[0m") name, $($myprintf "\033[3m")INSTANCE$($myprintf "\033[0m") name and window $($myprintf "\033[3m")TITLE$($myprintf "\033[0m") filters using basic
  26. string-matching. An omitted filter will match anything.
  27. $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWER -d$($myprintf "\033[0m")
  28. Deregister queued swallow for window $($myprintf "\033[3m")SWALLOWER$($myprintf "\033[0m"). Inverse of above signature.
  29. $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWER SWALLOWEE$($myprintf "\033[0m")
  30. Perform immediate swallow of window $($myprintf "\033[3m")SWALLOWEE$($myprintf "\033[0m") by window $($myprintf "\033[3m")SWALLOWER$($myprintf "\033[0m").
  31. $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWEE -s$($myprintf "\033[0m")
  32. Stop swallow of window $($myprintf "\033[3m")SWALLOWEE$($myprintf "\033[0m"). Inverse of the above signature. Visible
  33. windows only.
  34. $($myprintf "\033[1m")dwmswallow -h$($myprintf "\033[0m")
  35. Show this usage information.
  36. EOF
  37. )"
  38. if [ -n "$1" ]; then
  39. echo "$msg" >&2
  40. exit "$1"
  41. else
  42. echo "$msg"
  43. fi
  44. }
  45. # Determine number of leading positional arguments
  46. arg1="$1" # save for later
  47. arg2="$2" # save for later
  48. num_pargs=0
  49. while :; do
  50. case "$1" in
  51. -*|"") break ;;
  52. *) num_pargs=$((num_pargs + 1)); shift ;;
  53. esac
  54. done
  55. case "$num_pargs" in
  56. 1)
  57. ! is_winid "$arg1" && usage 1
  58. widswer="$arg1"
  59. if [ "$1" = "-d" ] && [ "$#" -eq 1 ]; then
  60. if name="$(printf "${PREFIX}swalunreg${SEP}%u" "$widswer" 2>/dev/null)"; then
  61. xsetroot -name "$name"
  62. else
  63. usage 1
  64. fi
  65. elif [ "$1" = "-s" ] && [ "$#" -eq 1 ]; then
  66. widswee="$arg1"
  67. if name="$(printf "${PREFIX}swalstop${SEP}%u" "$widswee" 2>/dev/null)"; then
  68. xsetroot -name "$name"
  69. else
  70. usage 1
  71. fi
  72. else
  73. while :; do
  74. case "$1" in
  75. -c) [ -n "$2" ] && { class="$2"; shift 2; } || usage 1 ;;
  76. -i) [ -n "$2" ] && { instance="$2"; shift 2; } || usage 1 ;;
  77. -t) [ -n "$2" ] && { title="$2"; shift 2; } || usage 1 ;;
  78. "") break ;;
  79. *) usage 1 ;;
  80. esac
  81. done
  82. widswer="$arg1"
  83. if name="$(printf "${PREFIX}swalreg${SEP}%u${SEP}%s${SEP}%s${SEP}%s" "$widswer" "$class" "$instance" "$title" 2>/dev/null)"; then
  84. xsetroot -name "$name"
  85. else
  86. usage 1
  87. fi
  88. fi
  89. ;;
  90. 2)
  91. ! is_winid "$arg1" "$arg2" || [ -n "$1" ] && usage 1
  92. widswer="$arg1"
  93. widswee="$arg2"
  94. if name="$(printf "${PREFIX}swal${SEP}%u${SEP}%u" "$widswer" "$widswee" 2>/dev/null)"; then
  95. xsetroot -name "$name"
  96. else
  97. usage 1
  98. fi
  99. ;;
  100. *)
  101. if [ "$arg1" = "-h" ] && [ $# -eq 1 ]; then
  102. usage
  103. else
  104. usage 1
  105. fi
  106. esac