Skip to content

Instantly share code, notes, and snippets.

@Gordin
Created December 5, 2020 01:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gordin/a749a004f8e4314d691fd74f8aa9f681 to your computer and use it in GitHub Desktop.
Save Gordin/a749a004f8e4314d691fd74f8aa9f681 to your computer and use it in GitHub Desktop.
script, that moves windows to another monitor, when the lid of the notebook is closed. Run with `watch -n 0.5 ./lid.sh`
#!/usr/bin/env bash
LID_TMP='/tmp/lidscript.tmp'
# Read hoved windows from file
readarray moved_windows < $LID_TMP
# remove newlines after every element...
newMovedWindows=()
for i in "${!moved_windows[@]}"; do
without_newline=${moved_windows[$i]%$'\n'}
if [[ $without_newline != "" ]]; then
newMovedWindows+=("$without_newline")
echo "Moved Window: $without_newline"
fi
done
moved_windows=("${newMovedWindows[@]}")
function monitors() {
# Get Names of monitors
PRIMARY_MONITOR=$(xrandr | grep ' connected primary' | cut -d " " -f1)
OTHER_MONITORS=$(xrandr | grep ' connected [0-9]' | cut -d " " -f1)
for monitor in $OTHER_MONITORS ; do
OTHER_MONITOR="$monitor"
done
}
monitors
echo "Primary monitor: $PRIMARY_MONITOR"
I3WSS=$(i3-msg -t get_workspaces)
# Get Names of currently active workspace on both monitors
NOTEBOOK_WS=$(echo "${I3WSS}" |
jq ".[]
| select(.visible==true)
| select(.output==\"$PRIMARY_MONITOR\")
| .name")
OTHER_WS=$(echo "${I3WSS}" |
jq ".[]
| select(.visible==true)
| select(.output==\"$OTHER_MONITOR\")
| .name")
# Get windows on active notebook monitor
NB_WINDOWS=$(i3-msg -t get_tree |
jq ".. | objects | select(.name== $NOTEBOOK_WS)
| .[\"nodes\"]
| map(.id)
| .[]")
function moveToExternalMonitor() {
for window in $NB_WINDOWS; do
i3-msg "[con_id=\"$window\"] focus"
i3-msg move container to workspace "$OTHER_WS"
moved_windows+=("$window")
done
}
function moveBack() {
for i in "${!moved_windows[@]}"; do
i3-msg "[con_id=\"${moved_windows[$i]}\"] focus"
i3-msg move container to workspace "$NOTEBOOK_WS"
moved_windows=( "${moved_windows[@]/${moved_windows[$i]}}" )
done
}
function lidCheck() {
LID_OPEN=$(grep open /proc/acpi/button/lid/LID/state)
echo "X${LID_OPEN}X"
if [ -n "$LID_OPEN" ]; then
echo 'lid open'
moveBack
else
echo 'lid closed'
moveToExternalMonitor
fi
}
lidCheck
echo before
cat $LID_TMP
# save moved windows to tmp file
printf "%s\n" "${moved_windows[@]}" > $LID_TMP
echo after
cat $LID_TMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment