|
#!/bin/sh |
|
|
|
alias jq='cat monitors.json | jq -r' |
|
|
|
# Resolves the name of a monitor from an alias or name |
|
function resolve_monitor { |
|
name="$1" |
|
|
|
# check monitors object |
|
exists="$(jq '.monitors | has("'$name'")')" |
|
if [[ "$exists" == "true" ]]; then |
|
echo "$name" |
|
return 0 |
|
fi |
|
|
|
# check alias object |
|
exists=`jq '.alias | has("'$1'")'` |
|
if [[ "$exists" == "true" ]]; then |
|
jq '.alias["'$1'"]' |
|
return 0 |
|
fi |
|
|
|
return 1 |
|
} |
|
|
|
# Prints true or false depending on whether a monitor exists |
|
function monitor_exists { |
|
name="$(resolve_monitor "$1")" |
|
if [[ "$name" == "" ]]; then |
|
echo "false" |
|
return 1 |
|
fi |
|
|
|
jq '.monitors | has("'$name'")' |
|
} |
|
|
|
function list_monitors { |
|
monitors="$(jq -r '.monitors')" |
|
|
|
echo "Available monitors:" |
|
while IFS= read -r monitor; do |
|
size=$(jq -r '.monitors.["'$monitor'"].size') |
|
aliases=$(jq -r --arg monitor "$monitor" '.alias | to_entries | map(select(.value == $monitor) | .key) | join(", ")') |
|
|
|
echo " $monitor:" |
|
echo " Alias: $aliases" |
|
echo " Size: $size" |
|
done <<< "$(jq -r '.monitors | keys_unsorted[]')" |
|
} |
|
|
|
function kill_bars { |
|
bars="$1" |
|
[[ "$bars" == "" ]] && return |
|
|
|
for bar in $bars; do |
|
pids="$(ps aux | grep 'polybar '"$bar"'' | awk '{ print $2 }')" |
|
if [[ "$pids" != "" ]]; then |
|
kill $pids &> /dev/null |
|
fi |
|
done |
|
sleep 1 |
|
} |
|
|
|
function start_bars { |
|
bars="$1" |
|
for bar in $bars; do |
|
echo "enabling bar: $bar" |
|
polybar "$bar" &> /dev/null & |
|
sleep 1 |
|
done |
|
} |
|
|
|
function enable_monitor { |
|
name="$(resolve_monitor "$1")" |
|
if [[ "$(monitor_exists "$name")" != "true" ]]; then |
|
echo "invalid monitor: $1" |
|
exit 1 |
|
fi |
|
|
|
size="$(jq '.monitors["'$name'"].size')" |
|
position="$(jq '.monitors["'$name'"].position')" |
|
bars="$(jq '.monitors["'$name'"].bars')" |
|
|
|
echo "enabling monitor: $name" |
|
xrandr --output "$name" \ |
|
--mode "$size" \ |
|
--pos "$position" \ |
|
--rotate "normal" |
|
|
|
if [[ "$bars" != "" ]]; then |
|
kill_bars "$bars" |
|
enable_bars "$bars" |
|
fi |
|
} |
|
|
|
function disable_monitor { |
|
name="$(resolve_monitor "$1")" |
|
if [[ "$(monitor_exists "$name")" != "true" ]]; then |
|
echo "invalid monitor: $1" |
|
exit 1 |
|
fi |
|
|
|
bars="$(jq '.monitors["'$name'"].bars')" |
|
kill_bars "$bars" |
|
|
|
echo "disabling monitor: $name" |
|
xrandr --output "$name" --off |
|
} |
|
|
|
function usage { |
|
script_name="$1" |
|
echo "Usage: $script_name <command> <monitor>" |
|
echo "Available Commands:" |
|
echo " list List the available monitors." |
|
echo " enable <monitor> Enable the specified monitor." |
|
echo " disable <monitor> Disable the specified monitor." |
|
echo |
|
echo "Example:" |
|
echo " $script_name enable HDMI-1" |
|
echo " $script_name disable DVI-D-1" |
|
exit 1 |
|
} |
|
|
|
command="$1" |
|
case "$command" in |
|
"list") list_monitors;; |
|
"enable") enable_monitor "$2";; |
|
"disable") disable_monitor "$2";; |
|
*) usage "$0";; |
|
esac |