From 4716b05f62470261a14dc188490d2ba12c5189be Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 2 Mar 2015 19:19:02 +0100 Subject: [PATCH] scripts --- i3_mouse_click.sh | 3 ++ i3_move_mouse.sh | 22 +++++++++++ i3_switch_focus_container.php | 63 ++++++++++++++++++++++++++++++ i3_switch_monitor.sh | 22 +++++++++++ i3_switch_monitor_wrapper.sh | 3 ++ i3_switch_workspace.php | 73 +++++++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100755 i3_mouse_click.sh create mode 100755 i3_move_mouse.sh create mode 100755 i3_switch_focus_container.php create mode 100755 i3_switch_monitor.sh create mode 100755 i3_switch_monitor_wrapper.sh create mode 100755 i3_switch_workspace.php diff --git a/i3_mouse_click.sh b/i3_mouse_click.sh new file mode 100755 index 0000000..384941c --- /dev/null +++ b/i3_mouse_click.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +xdotool click 1 diff --git a/i3_move_mouse.sh b/i3_move_mouse.sh new file mode 100755 index 0000000..d950211 --- /dev/null +++ b/i3_move_mouse.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +DIRECTION=$1 + +getMouseX() { + xdotool getmouselocation 2>/dev/null | awk '{ print $1 }' | cut -d: -f2 +} + +getMouseY() { + xdotool getmouselocation 2>/dev/null | awk '{ print $2 }' | cut -d: -f2 +} + +moveTo() { + xdotool mousemove $1 $2 +} + +case $DIRECTION in + left) moveTo $(echo "$(getMouseX)-20" | bc) $(getMouseY);; + right) moveTo $(echo "$(getMouseX)+20" | bc) $(getMouseY);; + up) moveTo $(getMouseX) $(echo "$(getMouseY)-20" | bc) ;; + down) moveTo $(getMouseX) $(echo "$(getMouseY)+20" | bc);; +esac diff --git a/i3_switch_focus_container.php b/i3_switch_focus_container.php new file mode 100755 index 0000000..14b4914 --- /dev/null +++ b/i3_switch_focus_container.php @@ -0,0 +1,63 @@ +#!/usr/bin/php5 +nodes)) { + return null; + } + + foreach ($tree->nodes as $k => $container) { + if (true === $container->focused) { + return $tree->nodes; + } + + if (null !== $container = getFocusedWindowContainer($container)) { + return $container; + } + } + + return null; +} + +function getFocusWindowNumber($default, $max) +{ + global $argv; + + if (!isset($argv[1])) { + $argv[1] = $default; + } + + return min($max, max(1, $argv[1])) - 1; +} + +function getDefaultFocusWindowNumber($container) +{ + foreach ($container as $k => $v) { + if ($v->focused) { + return $k + 1; + } + } +} + +function setFocusToWindow($window) +{ + shell_exec(sprintf("i3-msg '[con_id=\"%s\"] focus'", $window->id)); +} + +$container = getFocusedWindowContainer(getTree()); + +if ($container !== null) { + $number = getFocusWindowNumber(getDefaultFocusWindowNumber($container), count($container)); + + foreach ($container as $k => $window) { + if ($k == $number) { + setFocusToWindow($window); + } + } +} diff --git a/i3_switch_monitor.sh b/i3_switch_monitor.sh new file mode 100755 index 0000000..34715b0 --- /dev/null +++ b/i3_switch_monitor.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +MONITOR1_WIDTH=$1 +MONITOR2_WIDTH=$2 + +getMouseX() { + xdotool getmouselocation 2>/dev/null | awk '{ print $1 }' | cut -d: -f2 +} + +getMouveNextX() { + if [ $(getMouseX) -lt $MONITOR1_WIDTH ]; then + echo "$MONITOR1_WIDTH+$MONITOR2_WIDTH/2-10" | bc + else + echo "$MONITOR1_WIDTH/2-10" | bc + fi +} + +moveTo() { + xdotool mousemove $1 540 +} + +moveTo $(getMouveNextX) diff --git a/i3_switch_monitor_wrapper.sh b/i3_switch_monitor_wrapper.sh new file mode 100755 index 0000000..d40644c --- /dev/null +++ b/i3_switch_monitor_wrapper.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +exec ~/bin/i3_switch_monitor.sh 1920 1920 diff --git a/i3_switch_workspace.php b/i3_switch_workspace.php new file mode 100755 index 0000000..5822512 --- /dev/null +++ b/i3_switch_workspace.php @@ -0,0 +1,73 @@ +#!/usr/bin/php5 +name; + } + + sort($ordered); + + foreach ($ordered as $k => $v) { + foreach ($workspaces as $x => $w) { + if ($w->name == $v) { + $ordered[$k] = $w; + unset($workspaces[$x]); + } + } + } + + return $ordered; +} + +function getMoveOption() +{ + global $argv; + + if (!isset($argv[1])) { + $argv[1] = 'next'; + } + + return in_array($argv[1], array('next', 'previous')) ? $argv[1] : 'next'; +} + +function getNextWorkspace($workspaces) +{ + $key = 0; + + foreach ($workspaces as $k => $v) { + if ($v->focused) { + $key = $k; + } + } + + return $workspaces[($key + 1) % count($workspaces)]; +} + +function getPreviousWorkspace($workspaces) +{ + $count = count($workspaces); + $key = 0; + + foreach ($workspaces as $k => $v) { + if ($v->focused) { + $key = $k; + } + } + + return $workspaces[ --$key >= 0 ? $key : $count - 1 ]; +} + +function moveToWorkspace($workspace) +{ + shell_exec(sprintf("i3-msg 'workspace \"%s\"'", $workspace->name)); +} + +$workspaces = getOrderedWorkspaces(); +moveToWorkspace(getMoveOption() === 'next' ? getNextWorkspace($workspaces) : getPreviousWorkspace($workspaces));