popup-terminal.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env bash
  2. # Toggle terminal to show up as a popup terminal
  3. # by Adnan Shameem (adnan360); License: MIT (Expat)
  4. #
  5. # Requirements:
  6. # - any terminal emulator (xterm, lxterminal etc.)
  7. # - xdotool
  8. # - wmctrl
  9. #
  10. # Usage:
  11. # - Change configs below as you wish
  12. # - Run the script: ./popup-terminal.sh
  13. # or bind it on a key
  14. # Config
  15. config_term_program='xterm'
  16. config_term_width=800
  17. config_term_height=300
  18. config_top_decoration_height=30
  19. # Temporary files
  20. widfile='/tmp/popup_term_wid'
  21. visiblefile='/tmp/popup_term_visible'
  22. # Position and prepare the terminal window
  23. # Params:
  24. # - $1: Window ID of the terminal
  25. prepare_term() {
  26. screen_width=$(xdpyinfo | awk '/dimensions:/{print $2 }' | cut -d 'x' -f1)
  27. # Set window title
  28. xdotool set_window --name 'My Popup Terminal' "$1"
  29. # Size and position the terminal window to top center
  30. xdotool windowmove "$1" $(( ($screen_width - $config_term_width) / 2 )) -$config_top_decoration_height
  31. xdotool windowsize "$1" $config_term_width $config_term_height
  32. # Needs to be focused to, in order to fix a weird bug which doesn't make
  33. # always on top work when last window is maximized.
  34. xdotool windowfocus --sync "$1"
  35. # Always on top
  36. wmctrl -r :ACTIVE: -b add,above
  37. }
  38. # Terminal not running, we need to start terminal
  39. if [ -z "$(xdotool getwindowpid $(cat "$widfile") 2>/dev/null)" ]; then
  40. prevfocus=$(xdotool getactivewindow)
  41. $config_term_program &>/dev/null & disown
  42. # Sleep until terminal opens and then we continue...
  43. until [ "$(xdotool getactivewindow)" != "$prevfocus" ]; do
  44. sleep 1
  45. done
  46. # When terminal opens, we store the Window ID
  47. wid=$(xdotool getactivewindow)
  48. echo "$wid"> "$widfile"
  49. prepare_term "$wid"
  50. touch "$visiblefile"
  51. # Terminal is already there, we just need to show/hide it
  52. else
  53. if [ ! -f "$visiblefile" ]; then
  54. wid=$(cat "$widfile")
  55. xdotool windowmap --sync "$wid"
  56. prepare_term "$wid"
  57. touch "$visiblefile"
  58. else
  59. wid=$(cat "$widfile")
  60. xdotool windowunmap --sync "$wid"
  61. rm "$visiblefile"
  62. fi
  63. fi