-
Notifications
You must be signed in to change notification settings - Fork 1
/
move_to_other_screen.sh
executable file
·83 lines (65 loc) · 2.59 KB
/
move_to_other_screen.sh
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
declare -A resolutions
tmpfile=$(mktemp)
xrandr --listactivemonitors | \
sed -En 's!^.* ([0-9]+)/[0-9]+x([0-9]+)/[0-9]+\+([0-9]+)\+([0-9]+).*$!\1 \2 \3 \4!;Te;p;:e' > $tmpfile
count=0
while read width height xoffset yoffset; do
resolutions["width_$count"]=$width
resolutions["height_$count"]=$height
resolutions["xoffset_$count"]=$xoffset
resolutions["yoffset_$count"]=$yoffset
resolutions["xmax_$count"]=$[ $xoffset + $width ]
resolutions["ymax_$count"]=$[ $yoffset + $height ]
count=$[ $count + 1 ]
done < $tmpfile
rm $tmpfile
echo "Found $count screens"
window_id=$(xdotool getactivewindow)
function echo_location {
xdotool getwindowgeometry $window_id | \
grep Position | \
sed -En 's/.*Position.* ([0-9]+),([0-9]+).*$/\1 \2/;Te;p;:e'
}
echo_location > $tmpfile ; read window_x window_y < $tmpfile ; rm $tmpfile
for i in $(seq -s" " 0 $[ $count - 1 ] ) ; do
if [ $window_x -ge ${resolutions["xoffset_$i"]} ] && \
[ $window_x -lt ${resolutions["xmax_$i"]} ] && \
[ $window_y -ge ${resolutions["yoffset_$i"]} ] && \
[ $window_y -lt ${resolutions["ymax_$i"]} ] ; then
if [ "$1" == "prev" ] ; then
nexti=$[ $i - 1 ]
else
nexti=$[ $i + 1 ]
fi
if [ $nexti -lt 0 ] ; then
nexti=$[ $count - 1 ]
fi
if [ $nexti -ge $count ] ; then
nexti=0
fi
offset_x=$[ $window_x - ${resolutions["xoffset_$i"]} ]
offset_y=$[ $window_y - ${resolutions["yoffset_$i"]} ]
newx=$[ $offset_x + ${resolutions["xoffset_$nexti"]} ]
newy=$[ $offset_y + ${resolutions["yoffset_$nexti"]} ]
xdotool windowmove -sync \
$window_id $newx $newy
echo_location > $tmpfile ; read window_x window_y < $tmpfile ; rm $tmpfile
echo "On screen $i"
echo "Moving to screen $nexti"
echo " moving to x=$newx y=$newy"
echo " arrived at x=$window_x y=$window_y"
fixed_x_offset=$[ $window_x - $newx ]
fixed_y_offset=$[ $window_y - $newy ]
if [ ! $fixed_x_offset -eq 0 ] || [ ! $fixed_x_offset -eq 0 ] ; then
echo " ...arrived at different location... fixing"
xdotool windowmove -sync \
$window_id \
$[ $newx - $fixed_x_offset ] \
$[ $newy - $fixed_y_offset ]
echo_location > $tmpfile ; read window_x window_y < $tmpfile ; rm $tmpfile
echo " fixed location: x=$window_x y=$window_y"
fi
break
fi
done