-
Notifications
You must be signed in to change notification settings - Fork 74
/
move-to-next-monitor
executable file
·57 lines (47 loc) · 2.06 KB
/
move-to-next-monitor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh
#
# Move the current window to the next monitor.
#
# Only works on a horizontal monitor setup.
# Also works only on one X screen (which is the most common case).
#
# Props to
# http://icyrock.com/blog/2012/05/xubuntu-moving-windows-between-monitors/
#
# Unfortunately, both "xdotool getwindowgeometry --shell $window_id" and
# checking "-geometry" of "xwininfo -id $window_id" are not sufficient, as
# the first command does not respect panel/decoration offsets and the second
# will sometimes give a "-0-0" geometry. This is why we resort to "xwininfo".
screen_width=`xdpyinfo | awk '/dimensions:/ { print $2; exit }' | cut -d"x" -f1`
display_width=`xdotool getdisplaygeometry | cut -d" " -f1`
window_id=`xdotool getactivewindow`
# Remember if it was maximized.
window_state=`xprop -id $window_id _NET_WM_STATE | awk '{ print $3 }'`
# Un-maximize current window so that we can move it
wmctrl -ir $window_id -b remove,maximized_vert,maximized_horz
# Read window position
x=`xwininfo -id $window_id | awk '/Absolute upper-left X:/ { print $4 }'`
y=`xwininfo -id $window_id | awk '/Absolute upper-left Y:/ { print $4 }'`
# Subtract any offsets caused by panels or window decorations
x_offset=`xwininfo -id $window_id | awk '/Relative upper-left X:/ { print $4 }'`
y_offset=`xwininfo -id $window_id | awk '/Relative upper-left Y:/ { print $4 }'`
x=`expr $x - $x_offset`
y=`expr $y - $y_offset`
# Compute new X position
new_x=`expr $x + $display_width`
# If we would move off the right-most monitor, we set it to the left one.
# We also respect the window's width here: moving a window off more than half its width won't happen.
width=`xdotool getwindowgeometry $window_id | awk '/Geometry:/ { print $2 }'|cut -d"x" -f1`
if [ `expr $new_x + $width / 2` -gt $screen_width ]; then
new_x=`expr $new_x - $screen_width`
fi
# Don't move off the left side.
if [ $new_x -lt 0 ]; then
new_x=0
fi
# Move the window
xdotool windowmove $window_id $new_x $y
# Maximize window again, if it was before
if [ -n "${window_state}" ]; then
wmctrl -ir $window_id -b add,maximized_vert,maximized_horz
fi