aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar √Čtienne Deparis 2021-02-09 11:32:17 +0100
committerGravatar √Čtienne Deparis 2021-02-09 11:32:17 +0100
commit646f293f0ec75eb913c862ef31e1533b12f44ce7 (patch)
tree4a47805eb62505071147220d568f3a2968453ac8
parent1fbe56c5b7284d5982ea151ce70bf84e4101077d (diff)
downloadshell_utils-master.tar.gz
shell_utils-master.tar.xz
shell_utils-master.zip
feat(switch_screen.rb): Add a tools to allow mouse or window to go to other screenHEADmaster
-rw-r--r--switch_screen.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/switch_screen.rb b/switch_screen.rb
new file mode 100644
index 0000000..a021671
--- /dev/null
+++ b/switch_screen.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+require 'optparse'
+
+def screen_geometry
+ screen_data = []
+ `xrandr -q -d :0`.split("\n").each do |l|
+ m = / connected (?:\w+ )?(\d+)x(\d+)\+(\d+)\+(\d+) /.match(l)
+ next unless m
+ data = m.to_a[1..].map(&:to_i)
+ data << data[0] / 2 + data[2]
+ data << data[1] / 2 + data[3]
+ screen_data << data
+ end
+ screen_data.sort { |a, b| b[3] <=> a[3] }
+end
+
+def next_screen(screen_data)
+ m = /x:(\d+) y:(\d+) /.match(`xdotool getmouselocation`)
+ return nil unless m
+ current_screen = 0
+ cur_x = m[1].to_i
+ screen_data.each_with_index do |data, index|
+ if cur_x > data[2]
+ current_screen = index
+ end
+ end
+ next_screen = current_screen + 1
+ return next_screen if next_screen < screen_data.length
+ 0
+end
+
+params = {}
+OptionParser.new do |opts|
+ opts.on('-m', '--mouse', 'Move only mouse to other screen')
+ opts.on('-v', '--verbose', 'Display command before they are run')
+end.parse!(into: params)
+
+screen_data = screen_geometry
+ns = next_screen(screen_data)
+ns_mid_x = screen_data[ns][4]
+ns_mid_y = screen_data[ns][5]
+
+if params[:mouse]
+ commands = ["xdotool mousemove #{ns_mid_x} #{ns_mid_y}"]
+else
+ current_window = `xdotool getactivewindow`.strip
+ commands = [
+ "wmctrl -ir #{current_window} -b remove,maximized_vert,maximized_horz",
+ "xdotool windowmove #{current_window} #{ns_mid_x} #{ns_mid_y} mousemove #{ns_mid_x} #{ns_mid_y}",
+ "wmctrl -ir #{current_window} -b add,maximized_vert,maximized_horz",
+ "wmctrl -ia #{current_window}"
+ ]
+end
+
+commands.each do |cmd|
+ puts cmd if params[:verbose]
+ success = system cmd
+ exit 1 unless success
+end