1
0

move-window.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. # Klimbim Software collection, A bag full of things
  3. # Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # 2018-2019 https://www.bananas-playground.net
  18. # Move the current window to the next monitor.
  19. #
  20. # original code from:
  21. # https://unix.stackexchange.com/questions/48456/xfce-send-window-to-other-monitor-on-keystroke/322904#322904
  22. # run script with -r or -l as a keyboard shurtcut and the current active window will be moved.
  23. # works with horizontal monitors. Needs to be improved to work with vertical ones.
  24. # change MONITOR_COUNT below to your needs.
  25. # needs the following applications
  26. # xdpyinfo
  27. # xdotool
  28. # xprop
  29. # wmctrl
  30. # xwininfo
  31. # set this variable to your monitor count
  32. MONITOR_COUNT=3;
  33. screen_width=$(xdpyinfo | awk -F" |x" '/dimensions:/ { print $7 }')
  34. screen_height=$(xdpyinfo | awk -F" |x" '/dimensions:/ { print $8 }')
  35. window_id=$(xdotool getactivewindow)
  36. case $1 in
  37. -l )
  38. display_width=$((screen_width / MONITOR_COUNT * 2)) ;;
  39. -r )
  40. display_width=$((screen_width / MONITOR_COUNT)) ;;
  41. esac
  42. # Remember if it was maximized.
  43. window_state=$(xprop -id $window_id _NET_WM_STATE | awk '{ print $3 }')
  44. # Un-maximize current window so that we can move it
  45. wmctrl -ir $window_id -b remove,maximized_vert,maximized_horz
  46. # Read window position
  47. x=$(xwininfo -id $window_id | awk '/Absolute upper-left X:/ { print $4 }')
  48. y=$(xwininfo -id $window_id | awk '/Absolute upper-left Y:/ { print $4 }')
  49. # Subtract any offsets caused by window decorations and panels
  50. x_offset=$(xwininfo -id $window_id | awk '/Relative upper-left X:/ { print $4 }')
  51. y_offset=$(xwininfo -id $window_id | awk '/Relative upper-left Y:/ { print $4 }')
  52. x=$((x - x_offset))
  53. y=$((y - y_offset))
  54. # Fix Chromium app view issue of small un-maximized size
  55. width=$(xdotool getwindowgeometry $window_id | awk -F" |x" '/Geometry:/ { print $4 }')
  56. if [ "$width" -lt "150" ]; then
  57. display_width=$((display_width + 150))
  58. fi
  59. # Compute new X position
  60. new_x=$((x + display_width))
  61. # Compute new Y position
  62. new_y=$((y + screen_height))
  63. # If we would move off the right-most monitor, we set it to the left one.
  64. # We also respect the window's width here: moving a window off more than half its width won't happen.
  65. if [ $((new_x + width / 2)) -gt $screen_width ]; then
  66. new_x=$((new_x - screen_width))
  67. fi
  68. height=$(xdotool getwindowgeometry $window_id | awk -F" |x" '/Geometry:/ { print $5 }')
  69. if [ $((new_y + height / 2)) -gt $screen_height ]; then
  70. new_y=$((new_y - screen_height))
  71. fi
  72. # Don't move off the left side.
  73. if [ $new_x -lt 0 ]; then
  74. new_x=0
  75. fi
  76. # Don't move off the bottom
  77. if [ $new_y -lt 0 ]; then
  78. new_y=0
  79. fi
  80. # Move the window
  81. xdotool windowmove $window_id $new_x $new_y
  82. # Maintain if window was maximized or not
  83. if [ "${window_state}" = "_NET_WM_STATE_MAXIMIZED_HORZ," ]; then
  84. wmctrl -ir $window_id -b add,maximized_vert,maximized_horz
  85. fi