scripts/ecran

206 lines
7.4 KiB
Plaintext
Raw Permalink Normal View History

2014-09-25 23:37:13 +02:00
#! /bin/sh
# Description: Manage video output (size, orientation, ...)
# And reload wm configuration
2020-08-21 19:58:25 +02:00
2020-08-21 20:02:03 +02:00
# Vars {{{
[ -z "${DEBUG}" ] && readonly DEBUG=0
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
## First and primary output (laptop screen, hdmi-1,…)
2020-10-23 09:03:36 +02:00
## It can be forced here or the script will try to discover it's name
#readonly PRIMARY_OUTPUT_NAME="LVDS-1"
## Second output (hdmi-2, dp-1,…)
## It can be forced here or the script will try to discover it's name
#readonly SECOND_OUTPUT_NAME
2024-03-27 08:01:45 +01:00
readonly PRIMARY_OUTPUT_MODE="--auto"
2024-03-26 15:01:07 +01:00
readonly SECOND_OUTPUT_MODE="--auto"
readonly SECOND_OUTPUT_ACTION="NONE"
2020-08-21 20:02:03 +02:00
# }}}
debug_message() { # {{{
local_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_message}"
return 0
}
# }}}
2020-08-21 21:09:00 +02:00
define_vars() { # {{{
2020-08-21 20:02:03 +02:00
2024-03-26 15:01:07 +01:00
[ -f "${HOME}/.config/xrandr.conf" ] && \
. "${HOME}/.config/xrandr.conf" && \
printf "%b\n" 'Use local config file.'
2020-10-23 09:03:36 +02:00
## Primary output {{{
### If the primary output wasn't manually defined,
### try to get it's name
if [ -z "${PRIMARY_OUTPUT_NAME}" ]; then
2020-10-23 09:03:36 +02:00
get_primary_output_name
fi
debug_message "define_vars \
The primary output is: ${PRIMARY_OUTPUT_NAME}."
2020-10-23 09:03:36 +02:00
## }}}
## Second output {{{
### If the second output wasn't manually defined,
### try to get it's name
if [ -z "${SECOND_OUTPUT_NAME}" ]; then
SECOND_OUTPUT_NAME=$(xrandr | grep " connected" | grep -v "${PRIMARY_OUTPUT_NAME}" | awk 'NR==1{ print $1 }')
debug_message "define_vars \
The second output is: ${SECOND_OUTPUT_NAME}."
fi
## }}}
2024-03-27 08:01:45 +01:00
## Primary output mode {{{
### If primary_output_mode wasn't defined (~/.config/xrandr.conf file, var env…)
### Use default value
### Then use ${primary_output_mode} variable without "" to be able to use it as xrandr's option
if [ -z "${primary_output_mode}" ]; then
primary_output_mode=${PRIMARY_OUTPUT_MODE}
fi
## }}}
2024-03-26 15:01:07 +01:00
## Second output mode {{{
### If second_output_mode wasn't defined (~/.config/xrandr.conf file, var env…)
### Use default value
### Then use ${second_output_mode} variable without "" to be able to use it as xrandr's option
if [ -z "${second_output_mode}" ]; then
second_output_mode=${SECOND_OUTPUT_MODE}
fi
## }}}
2020-12-21 21:07:14 +01:00
## Second output action {{{
### If second_output_action wasn't defined (by argument, ~/.config/xrandr.conf file, var env…)
### Use default value
### Then use ${second_output_action} variable without "" to be able to use it as xrandr's option
if [ -z "${second_output_action}" ]; then
second_output_action=${SECOND_OUTPUT_ACTION}
fi
## }}}
2020-08-21 21:09:00 +02:00
}
2020-08-21 20:02:03 +02:00
# }}}
2020-10-23 09:03:36 +02:00
get_primary_output_name() { # {{{
## First, check if an output is already defined as primary
2020-10-23 10:16:11 +02:00
local_primary_output_name=$(xrandr | grep " primary" | grep -v " disconnected" | awk 'NR==1{ print $1 }')
2020-10-23 09:03:36 +02:00
## If the var contains something
if [ -n "${local_primary_output_name}" ]; then
debug_message "get_primary_output_name \
An output is defined as primary: ${local_primary_output_name}".
else
## Then check if an output starting with "LVD" (typically a laptop) is connected
local_primary_output_name=$(xrandr | grep "^LVD" | grep " connected" | awk 'NR==1{ print $1 }')
if [ -n "${local_primary_output_name}" ]; then
debug_message "get_primary_output_name \
The primary output seems to be a laptop screen: ${local_primary_output_name}".
else
## Then check if any output is connected and take the first one
local_primary_output_name=$(xrandr | grep " connected" | awk 'NR==1{ print $1 }')
if [ -n "${local_primary_output_name}" ]; then
debug_message "get_primary_output_name \
Take the first connected output as primary: ${local_primary_output_name}".
else
debug_message "get_primary_output_name \
Unable to get primary output name!
Ensure to have a primary, LVDS or any connected output in xrandr."
exit 2
fi
fi
2020-10-23 09:03:36 +02:00
fi
readonly PRIMARY_OUTPUT_NAME="${local_primary_output_name}"
2020-10-23 09:03:36 +02:00
}
# }}}
is_proc_running() { # {{{
local_proc_pattern="${1}"
2014-09-25 23:37:13 +02:00
local_count_proc_pattern="$(pgrep -f -- "${local_proc_pattern}" | wc -l)"
case "${local_count_proc_pattern}" in
0 ) ## No procs related to this pattern are running
return_proc_running="1"
;;
* ) ## At least one proc seems running
return_proc_running="0"
;;
esac
## Simple debug message to valid current variables
debug_message "is_proc_running \
procs running (with the pattern: ${RED}${local_proc_pattern}${COLOR_DEBUG}) on the current host: ${RED}${local_count_proc_pattern}${COLOR_DEBUG}."
return "${return_proc_running}"
2020-08-21 21:09:00 +02:00
}
# }}}
main() { # {{{
2020-08-21 21:09:00 +02:00
## Update herbstluftwm Window Manager configuration if it's running
is_proc_running "herbstluftwm" \
&& herbstclient detect_monitors \
&& herbstclient reload
}
# }}}
# Define all vars
define_vars
2020-08-21 21:09:00 +02:00
# Manage arguments # {{{
# This code can't be in a function due to arguments
2014-09-25 23:37:13 +02:00
case "${1}" in
primary | off )
## TODO: One command to enable primary screen and a loop to disable all other connected output
2024-03-27 08:27:56 +01:00
xrandr --output "${PRIMARY_OUTPUT_NAME:-/dev/null}" ${primary_output_mode}
if [ -n "${SECOND_OUTPUT_NAME}" ]; then xrandr --output "${SECOND_OUTPUT_NAME:-/dev/null}" --off ; fi
printf "%b\n" 'Enable only primary output'
;;
leftof | left-of | ds | dock | dockstation | work )
second_output_action="--left-of"
2024-03-27 08:27:56 +01:00
#xrandr --output VGA-1 --mode 1024x768 --left-of "${PRIMARY_OUTPUT_NAME:-/dev/null}" --output "${PRIMARY_OUTPUT_NAME:-/dev/null}" --mode 1366x768
xrandr --output "${SECOND_OUTPUT_NAME:-/dev/null}" ${second_output_mode} ${second_output_action} "${PRIMARY_OUTPUT_NAME:-/dev/null}" --output "${PRIMARY_OUTPUT_NAME:-/dev/null}" ${primary_output_mode}
printf "%b\n" "Xrandr - Manage second output position from primary output (option : \"${second_output_action}\")."
#printf "%b\n" "Xrandr for second output left-of (option : \"${second_output_action}\") primary screen"
;;
rightof | right-of | home )
second_output_action="--right-of"
xrandr --output "${SECOND_OUTPUT_NAME:-/dev/null}" ${second_output_mode} ${second_output_action} "${PRIMARY_OUTPUT_NAME:-/dev/null}" --output "${PRIMARY_OUTPUT_NAME:-/dev/null}" ${primary_output_mode}
printf "%b\n" "Xrandr - Manage second output position from primary output (option : \"${second_output_action}\")."
;;
above )
second_output_action="--above"
xrandr --output "${SECOND_OUTPUT_NAME:-/dev/null}" ${second_output_mode} ${second_output_action} "${PRIMARY_OUTPUT_NAME:-/dev/null}" --output "${PRIMARY_OUTPUT_NAME:-/dev/null}" ${primary_output_mode}
printf "%b\n" "Xrandr - Manage second output position from primary output (option : \"${second_output_action}\")."
;;
out | game | second )
2024-03-27 08:27:56 +01:00
xrandr --output "${SECOND_OUTPUT_NAME:-/dev/null}" ${second_output_mode} --output "${PRIMARY_OUTPUT_NAME:-/dev/null}" --off
printf "%b\n" 'Xrandr for second output only'
2020-08-21 19:58:25 +02:00
;;
primary_off | first_off | main_off | laptop_off )
2024-03-27 08:27:56 +01:00
xrandr --output "${PRIMARY_OUTPUT_NAME:-/dev/null}" --off
printf "%b\n" 'Xrandr to disable primary output'
;;
* )
printf "%b\n" 'Bad argument, please use [primary|left-of|right-of|above|second|primary_off|...]'
2020-08-21 21:09:00 +02:00
exit 1
;;
2014-09-25 23:37:13 +02:00
esac
# }}}
2014-09-25 23:37:13 +02:00
2024-03-27 08:27:56 +01:00
main
2014-09-25 23:37:13 +02:00
exit 0