Skip to content

Instantly share code, notes, and snippets.

@Anduh
Forked from fatguytyson/ScreenMonitor.sh
Created April 3, 2021 20:29
Show Gist options
  • Save Anduh/12635a35d0031bf8819391c4b07e9b32 to your computer and use it in GitHub Desktop.
Save Anduh/12635a35d0031bf8819391c4b07e9b32 to your computer and use it in GitHub Desktop.
Screen watcher for Linux Mint Immersed (dis)connect.
[Unit]
Description=Brings up Virtual monitors when Immersed (dis)connects
[Service]
Type=simple
Restart=on-failure
ExecStart=~/Scripts/ScreenMonitor.sh
[Install]
WantedBy=default.target
#! /bin/bash
OUTPUT_NAME="DVI-I-2-2"
OUTPUT_MODE="1920x1080"
OUTPUT_LOCATION="--right-of DP-1-3"
LOG_DIR="~/.Immersed/Logs"
agent_started() {
pgrep Immersed &> /dev/null 2>&1
return $?
}
most_recent_log() {
ls -t1 $LOG_DIR | head -n 1
}
is_most_recent_log() {
if [[ $LOG_FILE == $(most_recent_log) ]] ; then
return 0
fi
return 1
}
session_has_started() {
grep -Fq "will init display" "$LOG_DIR/$LOG_FILE"
return $?
}
create_screen() {
xrandr --addmode $OUTPUT_NAME $OUTPUT_MODE
xrandr --output $OUTPUT_NAME --mode $OUTPUT_MODE $OUTPUT_LOCATION
}
remove_screen() {
xrandr --output $OUTPUT_NAME --off
}
watch_for_connect() {
while ! session_has_started ; do
sleep 1
if ! is_most_recent_log ; then
return
fi
done
}
watch_for_disconnect() {
while is_most_recent_log ; do
sleep 1;
done
}
while true; do
while ! agent_started ; do sleep 1 ; done
LOG_FILE=$(most_recent_log)
if session_has_started
then
create_screen
watch_for_disconnect
else
remove_screen
watch_for_connect
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment