Fedora-Fr - Communauté francophone Fedora - Linux

Communauté francophone des utilisateurs de la distribution Linux Fedora.

  

Dernière news : Clap de fin pour Fedora 27

#1 07/12/2015 01:56:43

Heldwin
Gilets jaunes
Inscription : 07/10/2008
Messages : 4 091

[MATE] script pour lecture DVD

Hello,

J'ai fait un script qui fonctionne pas trop mal, qui évolue en fonction du matériel que j'ai eu ou possède, mais j'en n'ai pas beaucoup chez moi :)
C'est principalement pour regarder des films ou stream sur ma TV que je lance depuis mon  PC (reliée par un cable HDMI avec adapteur HDMI->DVI ou directement en HDMI, il détecte les 2)
(j'ai mis que 2 écrans, car je n'en ai pas plus pour regarder ce que ça donne avec ça, et j'ai pas le besoin, mais c'est modifiable)

Je le lance depuis 2 lanceurs que j'ai sur mon panel: 1 pour désactiver et l'autre pour l'activer (les utilisateurs l'ont en lecteur seule)

J'ai un doute pour la partie de la gestion Pulseaudio (qui est d'ailleurs incomplète), ainsi que sur la détection des écrans.
Je ne sais pas si juste gérer les 2 sinks suffit (est-ce que les speakers internes sont toujours sur le default-sink 0 ?)
et j'ai peur que l'affichage se mette sur un écran qui a disparu (ça deviendrait peut-être embêtant :) mais je pense que Mate sait rétablir)

Vous voyez des améliorations à faire ?
Peut-être la gestion des portables si sur batterie, mais je sais pas si c'est vraiment utile, car il devrait être sur ac dans ce cas ?

Fichier: dvd-time

#!/bin/bash
# Usage: dvd-time -s 0|1 [-m same|2|3] [-c soundcard_name -p profile] [-v] [-w] [-i] [-r] [-u]

shopt -s nullglob
dir_config="~/.config/dvd-time"
file_config="$dir_config/config"

if [[ ! -e "$file_config" ]]; then 

# ----------------------------------------------------------------------------------------
# CONFIG MANUELLE: si le fichier de configuration est trouvé, cela sera ignoré
# Liste des cartes voulues - remplacez par les votres ici
# pactl list cards, pour les noms des cartes sons et les profiles voulus
# 1er: speakers, 2ème: sortie hdmi, 3ème: carte son externe

	cards=("alsa_card.pci-0000_00_1b.0" "alsa_card.pci-0000_01_00.1" "alsa_card.usb-Denon_DJ_DS1-00")
	profiles=("output:analog-surround-51+input:analog-stereo" "output:hdmi-stereo" "output:analog-surround-40")

	# Indications des résolution/fréquences/etc. 
	# Il faut indiquer quel est l'écran principal dans la gestion d'affichage du système

	resolution_screen1="1920x1080;60"
	resolution_screen2="1920x1080;50"
	#resolution_screen2="1280x720;50"
	scale_screen2="1.0x1.0"
	resolution_screen3="1920x1080;60"
	scale_screen3="1.5x1.5"

	# Pour permettre d'exclure un écran (fantome ou non)
	excluded_screen="VGA-2-1"

# ----------------------------------------------------------------------------------------
fi

# Default variables
display="same_image"                       # valeur par défaut (voir -m)

# Variables
RED='\033[0;31m'
#DRED='\033[1;31m'
GREEN='\033[0;32m'
#DGREEN='\033[1;32m'
YELLOW='\033[0;33m'
#DYELLOW='\033[1;33m'
BLUE='\033[0;34m'
#DBLUE='\033[1;34m'
#MAGENTA='\033[0;35m'
#DMAGENTA='\033[1;35m'
#CYAN='\033[0;36m'
#DCYAN='\033[1;36m'
NC='\033[0m' 


# Functions
function getScreens {

	local cmd_screens
	cmd_screens=$(xrandr)
	ls_screens=($(printf '%s\n' "$cmd_screens" | gawk '/ connected/ && !/'"$excluded_screen"'/ { print $1 }'))
	nbr_screens="${#ls_screens[@]}"

	for screen_index in $(seq 1 "$nbr_screens"); do
		eval resolution"${screen_index}"=$(eval printf "%s" "\$resolution_screen${screen_index}" | gawk -F";" '{ print $1 }')
		eval frequency"${screen_index}"=$(eval printf  "%s" "\$resolution_screen${screen_index}" | gawk -F";" '{ print $2 }')
	done

	ls_primary="$(printf '%s\n' "$cmd_screens"| gawk '/ primary/ { print $1 }')"
	listModesScreen "$ls_primary"

	if [[ "$nbr_screens" -eq 2 ]]; then
		ls_secondary="$(printf '%s\n' "$cmd_screens" | gawk '/ connected/ && !/ primary/ && !/'"$excluded_screen"'/ { print $1 }')"
		listModesScreen "$ls_secondary"

	# TODO
	elif [[ "$nbr_screens" -eq 3 ]]; then
		ls_secondary=$(printf '%s\n' "$cmd_screens"| gawk '/ connected/ && !/ primary/ && !/'"$excluded_screen"'/ { print $1 }' | head -n 1)
		ls_tierce=$(printf '%s\n' "$cmd_screens"| gawk '/ connected/ && !/ primary/ && !/'"$excluded_screen"'/ { print $1 }' | tail -n 1)
		listModesScreen "$ls_tierce"
	fi

	check_primary=$(printf '%s\n' "${ls_screens[@]}" |  grep "$ls_primary ")
	check_secondary=$(printf '%s\n' "${ls_screens[@]}" |  grep "$ls_secondary ")
	check_tierce=$(printf '%s\n' "${ls_screens[@]}" |  grep "$ls_tierce ")
}

function calculatePos {

	local resolution1_temp
	local resolution2_temp
	local scale
	
	resolution1_temp=$(printf '%s' "$1" | gawk -F"x" '{ print $1 }')
	resolution2_temp=$(printf '%s' "$3" | gawk -F"x" '{ print $1 }')
	scale=$(printf '%s' "$2" | gawk -F"x" '{ print $1 }')

	position2="$resolution1_temp"

	if [[ "$nbr_screens" -eq 3 ]]; then
		position3=$(echo "$resolution1_temp + ($resolution2_temp * $scale)" | bc -l | gawk -F"." '{ print $1 }')
	fi
}

function listModesScreen {

	screen_name="$1"
	modes=("$(printf '%s' "$cmd_screens" | gawk '/'"$screen_name"'/{flag=1;next}/^[A-Z]/{flag=0}flag' | sed -e 's|^\ \ \ ||' -e 's|\*||' -e 's|\ +|+\ |')")
	#echo "${modes[@]}"
	nbr_modes="${#modes[@]}"

	if [[ "$ls_primary" == $1 ]]; then
		resolutions1_supported=($(printf '%s' "${modes[@]}" | grep x | gawk '{ print $1 }'))
		ls_resolutions1=$(printf '%s' "${modes[@]}" | grep "$resolution1 ")
		ls_freqs1=($(printf '%s' "$ls_resolutions1" | grep "$resolution1 " | sed "s|${resolution1} ||" | grep "${frequency1}."))

		if [[ -z $ls_resolutions1 ]]; then 	
			ERROR_RES1=1
			resolution1="$resolution1 (not supported)"
		fi
		if [[ -z $ls_freqs1 ]]; then 		
			ERROR_FREQ1=1
			frequency1="$frequency1 (not supported)"
			freqs1_supported="none"
		else			
			freqs1_supported="${ls_freqs1[@]}"
		fi

	elif [[ "$ls_secondary" == $1 ]]; then
		resolutions2_supported=($(printf '%s' "${modes[@]}" | grep x | gawk '{ print $1 }'))
		ls_resolutions2=$(printf '%s' "${modes[@]}" | grep "$resolution2 ")
		ls_freqs2=($(printf '%s' "$ls_resolutions2" | grep "$resolution2 " | sed "s|${resolution2} ||" | grep "${frequency2}."))
	
		if [[ -z $ls_resolutions2 ]]; then  
			ERROR_RES2=1
			resolution2="$resolution2 (your screen doesn't support this resolution)"
		fi
		if [[ -z $ls_freqs2 ]]; then 
			ERROR_FREQ2=1
			freqs2_supported="none"
			frequency2="$frequency2 (your screen doesn't support this frequency)"
		else					
			freqs2_supported="${ls_freqs2[@]}"
		fi

	elif [[ "$ls_tierce" == $1 ]]; then
		resolutions3_supported=($(printf '%s' "${modes[@]}" | grep x | gawk '{ print $1 }'))
		ls_resolutions3=$(printf '%s' "${modes[@]}" | grep "$resolution3 ")
		ls_freqs3=($(printf '%s' "$ls_resolutions3" | grep "$resolution3 " | sed "s|${resolution3} ||" | grep "${frequency3}."))
		if [[ -z $ls_resolutions3 ]]; then  
			ERROR_RES3=1
			resolution3="$resolution3 (not supported)"
		fi
		if [[ -z $ls_freqs3 ]]; then 
			ERROR_FREQ3=1
			freqs3_supported="none"
			frequency3="$frequency3 (not supported)"
		else					
			freqs3_supported="${ls_freqs3[@]}"
		fi
	fi
}

function getPulse {

	isPulse=$(pgrep -u "$(whoami)" pulseaudio)

	local cmd_sinks
	local cmd_sources
	cmd_sinks=$(pactl list short sinks)
	cmd_sources=$(pactl list short sources)
	ERROR=""	

	if [[ -n $isPulse ]]; then
		nbr_sinks=$(printf '%s\n' "$cmd_sinks" | grep -c alsa_output)
		nbr_sources=$(printf '%s\n' "$cmd_sources" | grep -c alsa_input)

		ls_sinks=$(printf '%s\n' "$cmd_sinks"| grep alsa_output | gawk '{ printf "%s:%s;",$1,$2 }' )
		ls_sources=$(printf '%s\n' "$cmd_sources"| grep alsa_input | gawk '{ printf "%s:%s;",$1,$2 }' )
	fi
}

function findSinks {

	pactl set-card-profile "$card_index" "output:$profile" &> /dev/null
	if [[ "$?" != 0 ]]; then ERROR+="findSinks;"; fi

	local src
	src=$(printf '%s' "$1" | sed 's|alsa_card|alsa_output|')
	sink_found=$(pactl list short sinks | grep "$src")

	if [[ ! -z "$sink_found" ]]; then 
		sink_index=$(printf '%s' "$sink_found" | gawk '{ print $1 }')
		sink_name=$(printf '%s' "$sink_found" | gawk '{ print $2 }')
	else
		sink_index="No output detected on card $card_index"
		sink_name="No output detected on card $card_index"
	fi
}

function findSources {

	pactl set-card-profile "$card_index" "output:${profile}" &> /dev/null
	if [[ "$?" != 0 ]]; then ERROR+="findSources;"; fi
	
	local src
	src=$(printf '%s' "$1" | sed 's|alsa_card|alsa_input|')
	source_found=$(pactl list short sources | grep "$src")

	if [[ ! -z $source_found ]]; then 
		source_index=$(printf '%s' "$source_found" | gawk '{ print $1 }')
		source_name=$(printf '%s' "$source_found" | gawk '{ print $2 }')
	else
		source_index="No input detected on card $card_index"
		source_name="No input detected on card $card_index"
	fi
}

function findCardNumber {

	card_found=$(pactl list short cards | grep "$1")
	card_index=$(printf '%s' "$card_found" | gawk '{ print $1 }')
}

function listCardProfiles {

	next_card_index=$((card_index + 1))
	ls_profiles=($(LANG=C pactl list cards | gawk '/Card #'"$card_index"'/{flag=1;next}/Card #'"$next_card_index"'/{flag=0}flag' \
		             | gawk '/Profiles/{flag=1;next}/Active Profile/{flag=0}flag' | gawk -F":" '/output:/{ print $2 }'))
	nbr_profiles="${#ls_profiles[@]}"
}

function moveAllSounds {

	local sinkindex
	local inputs
	local output
	sinkindex="$1"
	output=$(printf '%s' "$2" | sed 's|alsa_card|alsa_output|')
	inputs=($(pactl list short sink-inputs | gawk '{ print $1 }'))
	snd_count=0
	for sound in "${inputs[@]}"; do pactl move-sink-input "$sound" "$sinkindex"; ((snd_count++)); done

	if [[ "$ERROR" != "" ]]; then sink_status="ERROR"; sh -c 'exit 1'
	else sink_status=$(pactl list short sinks | grep "$output" | gawk '{ print $7}'); fi
}

function getDE {

	cmd_desktop=$(env)
	desktop=$(env | grep XDG_CURRENT_DESKTOP | sed 's|XDG_CURRENT_DESKTOP=||')
	desktop_type=$(env | grep XDG_SESSION_TYPE | sed 's|XDG_SESSION_TYPE=||')
	isMate=$(printf '%s' "$cmd_desktop" | grep MATE)
	isGnome=$(printf '%s' "$cmd_desktop" | grep GNOME)   # GNOME/GNOME-Classic/GNOME-Flashback
}

function switch_powerDisplay {

	if [[ "$status" -eq 1 ]]; then 
		text="Deactivation power display"
		display_ac="0"
	elif [[ "$status" -eq 0 ]]; then
		text="Activation power display (30 min)"
		display_ac="1800"
	fi
	if [[ "$isMate" != "" ]]; then
		gsettings set org.mate.power-manager sleep-display-ac "$display_ac"
	elif [[ "$isGnome" != "" ]]; then
		gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac "$display_ac"
		gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery "$display_ac"
	fi
}

function switch_screensaver {

	if [[ "$status" -eq 1 ]]; then 
		text="Deactivation screensaver"
		idle_enabled="false"
		lock_enabled="false"
	elif [[ "$status" -eq 0 ]]; then
		text="Activation screensaver"
		idle_enabled="true"
		lock_enabled="true"
	fi
	if [[ "$isMate" != "" ]]; then
		gsettings set org.mate.screensaver idle-activation-enabled "$idle_enabled"
		gsettings set org.mate.screensaver lock-enabled "$lock_enabled"
	elif [[ "$isGnome" != "" ]]; then
		gsettings set org.gnome.desktop.screensaver idle-activation-enabled "$idle_enabled"
		gsettings set org.gnome.desktop.screensaver lock-enabled "$lock_enabled"
	fi
}

function switch_inactivity {

	if [[ "$status" -eq 1 ]]; then 
		text="Setting session idle delay (2 hours)"
		idle_delay="120"
	elif [[ "$status" -eq 0 ]]; then
		text="Setting session idle delay (10 min)"
		idle_delay="10"
	fi
	if [[ "$isMate" != "" ]]; then
		gsettings set org.mate.session idle-delay "$idle_delay"
	elif [[ "$isGnome" != "" ]]; then
		gsettings set org.gnome.desktop.session idle-delay "$idle_delay"
	fi
}

function switch_screenOutput {

	getScreens
	sleep 2   # lancer au moins 2 fois xrandr à la suite semble poser un problème sans pause
	ERROR=""	


	if [[ "$nbr_screens" -eq 1 ]]; then 
		if [[ -z $check_secondary && -z $check_tierce ]]; then
			text="Forcing primary even if only 1 screen"
			screen="$ls_primary"

		elif [[ -z $check_primary && -z $check_tierce ]]; then
			text="Forcing secondary even if only 1 screen"
			screen="$ls_secondary"

		else
			text="Forcing tierce even if only 1 screen"
			screen="$ls_tierce"
		fi
		xrandr --output "$screen" --auto --rotate normal &> /dev/null

	elif [[ "$nbr_screens" -eq 2 ]]; then 

		if [[ "$status" -eq 1 ]]; then 

			if [[ "$display" == same_image ]]; then
				text="Activation same image on 2 screens"
				position2_txt="same as primary"
				xrandr --output "$ls_primary" --primary --mode "$resolution1" --rate "$frequency1"  --rotate normal \
				       --output "$ls_secondary" --mode "$resolution2" --rate "$frequency2" --rotate normal --same-as "$ls_primary" &> /dev/null

			elif [[ "$display" == 2_screens ]]; then
				text="Activation display on 2 screens"
				position2_txt="right of primary"
				calculatePos "$resolution1" "$scale_screen2"

			 	xrandr --output "$ls_primary" --primary --mode "$resolution1" --rate "$frequency1" --scale 1.0x1.0 --pos 0x0 --rotate normal \
				       --output "$ls_secondary" --mode "$resolution2" --rate "$frequency2" --scale "$scale_screen2" --pos "${position2}"x0  --rotate normal &> /dev/null

			else 
				text="Action switch display"
				return 1
			fi

		elif [[ "$status" -eq 0 ]]; then
			text="Activation only 1 screen (2 detected)"
			position2_txt="none"
			xrandr --output "$ls_primary" --primary --mode "$resolution1" --rate "$frequency1" --rotate normal --output "$ls_secondary" --off &> /dev/null
		fi

	elif [[ "$nbr_screens" -eq 3 ]]; then 

		if [[ "$status" -eq 1 ]]; then 

			# TODO and CHECK when 3 screens will be found
                        if [[ "$display" == 3_screens ]]; then
                                text="Activation display on 3 screens"
				position2_txt="right of primary"
				position3_txt="right of secondary"
                                calculatePos "$resolution1" "$scale_screen2" "$resolution2"

                                xrandr --output "$ls_primary" --primary --mode "$resolution1" --rate "$frequency1" --scale 1.0x1.0 --pos 0x0 --rotate normal \
                                       --output "$ls_secondary" --mode "$resolution2" --rate "$frequency2" --scale "$scale_screen2" --pos "${position2}x0" --rotate normal \
                                       --output "$ls_tierce" --mode "$resolution3" --rate "$frequency3" --scale "$scale_screen3" --pos "${position3}x0" --rotate normal &> /dev/null
				
			elif [[ "$display" == 2_screens ]]; then
				printf '%s' "2_screens is not supported for 3 screens   "
				return 1
			elif [[ "$display" == same_image ]]; then
				printf '%s' "same_image is not supported for 3 screens  "
				return 1
			fi

		elif [[ "$status" -eq 0 ]]; then
			text="Activation only 1 screen (3 detected)"
			position2_txt="none"
			position3_txt="none"
			xrandr --output "$ls_primary" --primary --mode "$resolution1" --rate "$frequency1" --rotate normal --output "$ls_secondary" --off &> /dev/null
		fi
		
	elif [[ "$nbr_screens" -eq 0 ]]; then 
		printf '%s' "Bug ? no screen found                      "
		return 1
		
	else 
		printf '%s' "Whatever you have is not supported         "
		return 1
	fi

	if [[ "$?" != 0 ]]; 	 then ERROR="switch_screenOutput;"; fi
	if [[ "$ERROR" != "" ]]; then sh -c 'exit 1'; fi
}

function switch_audioOutput {

	getPulse
	if [[ -n $isPulse ]]; then

		# cards[0]: "alsa_card.pci-0000_00_1b.0" 
		# cards[1]: "alsa_card.pci-0000_01_00.1" 
		# cards[2]: "alsa_card.usb-Denon_DJ_DS1-00" 
		if [[ "$status" -eq 1 ]]; then 

			if [[ "$nbr_screens" -eq 1  &&  -z $ls_secondary ]]; then
				text="Switching sound output to Internal"
				card="${cards[0]}"
				profile="${profiles[0]}"
			else 
				text="Switching sound output to External"
				if [[ "$nbr_sinks" -eq 1 ]]; then
					card="${cards[0]}"
				else 
					card="${cards[1]}"
				fi
				profile="${profiles[1]}"
			fi

		elif [[ "$status" -eq 0 ]]; then
			text="Switching sound output to Internal"
			card="${cards[0]}"
			profile="${profiles[0]}"
		fi

		if [[ -n $card_output && -n $profile_output ]]; then 
			card="$card_output"
			profile="$profile_output"
		fi

		profile=$(printf '%s' "$profile" | sed 's|output:||')
		
		findCardNumber "$card"
		listCardProfiles
		findSinks "$card"
		
		if [[ -n $sink_index ]]; then
			findSources "$card"
			pactl set-sink-mute "$sink_index" 0
			moveAllSounds "$sink_index" "$card"
		fi

	else
		printf '%s\n' "No pulseaudio sound detected           "
		return 1
	fi
}

# TODO
function init {

	cmd_cards=($(pactl list short cards | gawk -F"\t" '{ print $1":"$2 }'))
	nbr_cards="${#cmd_cards[*]}"
	printf '\n%s: %s\n' "Cartes trouvées" "$nbr_cards"	

	for i in $(seq 0 "$((nbr_cards - 1))"); do
		printf '[%s] %s\n' "$i" "$(printf '%s' "${cmd_cards[$i]}" | gawk -F":" '{ print $2 }')"
	done
	
	printf '\n%s:\n' "Please, enter the cards numbers in the order you want them"

	for i in $(seq 0 "$((nbr_cards - 1))"); do
		printf '%s: ' "card"
		read card
		
		order_cards+="$card"
	done

	printf '\n%s: %s\n' "Cards order" "$order_cards"
	printf '\n%s: %s' "Is it correct ? [Y/n] " 
	read answer

	if [[ "$answer" == "" || "$answer" == "Y" || "$answer" == "y" ]]; then confirm=1
	elif [[ "$answer" == "n" || "$answer" == "N" ]]; then confirm=0
	fi

	echo "$confirm"

	if [[ ! -d "~/.config/dvd-time" ]]; then mkdir "~/.config/dvd-time"; fi
	#printf '%s' "$config" > "$file_config"	
}

function reset {
	
	# Cela va le remettre en config manuelle, ou faut relancer l'initialisation de la config avec -i
	if [[ -e $file_config ]]; then
		rm -f "$file_config"
	fi
}

function getIndex_functions {

       	index="not_last"
	for i in "${!ls_functions[@]}"; do
		last_index=$(("${#ls_functions[@]}" - 1))
		if [[ "${ls_functions[$i]}" == $1 && "$i" -eq $last_index ]]; then index="last"; fi
	done
}

function writeTree {

	getIndex_functions "$2"

	if [[ "$index" == not_last ]];	then
		if [[ "$1" == tree ]]; 		then 	printf '%s' " ├─> "
		else 				  	printf '%s' " │ "; fi
	
	elif [[ "$index" == last ]];	then
		if [[ "$1" == tree ]]; 		then 	printf '%s' " └─> "
		else 				  	printf '%s' "   "; fi
	fi
		
	if   [[ "$1" == error ]];	then 	
		if [[ "$debug" == "" ]]; 	then	printf '%s' "    "
		else 					printf '%s' "  │ "; fi
	elif [[ "$1" == subtree ]]; 		then 	printf '%s' "  ├─ "
	elif [[ "$1" == subtree_end ]]; 	then 	printf '%s' "  └─ "
	elif [[ "$1" == subsubintree ]]; 	then 	printf '%s' "  │  ├─ "
	elif [[ "$1" == subsubintree_end ]]; 	then 	printf '%s' "  │  └─ "
	elif [[ "$1" == subsubouttree ]]; 	then	printf '%s' "     ├─ "
	elif [[ "$1" == subsubouttree_end ]];	then	printf '%s' "     └─ "
	elif [[ "$1" == subsubsubintree ]]; 	then 	printf '%s' "  │     ├─ "
	elif [[ "$1" == subsubsubintree_end ]]; then 	printf '%s' "  │     └─ "
	elif [[ "$1" == subsubsubouttree ]]; 	then 	printf '%s' "        ├─ "
	elif [[ "$1" == subsubsubouttree_end ]];then 	printf '%s' "        └─ "
	fi
}

function printLine {

	#echo "1: $ERROR_FREQ2"
	if [[ "$1" == tree ]]; then
		printf "${!3}%s${NC}%s" "$text" "$(addSpaces "${spaces}" "${text}")" 
		
	elif [[ "$1" == error ]]; then
		printf "%s" "$(addSpaces "${spaces}")" 

	else	
		if [[ "$2" == color ]]; then
			printf "${BLUE}%s${NC}" "$text" 
			if [[ "$function" == switch_audioOutput && "$text" == "sink status" && "$sink_status" == "RUNNING" ]]; then
				COLOR="GREEN"
			elif [[ "$function" == switch_audioOutput && "$text" == "sink status" && "$sink_status" == "ERROR" ]]; then
				COLOR="RED"
			else 
				COLOR="BLUE"
			fi
			printf "%s${!COLOR}%s${NC}\\n" "$(addSpaces "$spaces" "$text")" "$var" 

		elif [[ "$2" == nocolor ]]; then
			#echo "1: $ERROR_RES2"
			#echo "2: $function"
			#echo "3: $text"
			COLOR="NC"
			if [[ "$ERROR_RES1" -eq 1 && "$function" == "switch_screenOutput" && "$text" == resolution1 ]]; then
				COLOR="RED"
			fi
			if [[ "$ERROR_FREQ1" -eq 1 && "$function" == "switch_screenOutput" &&  "$text" == frequency1 ]]; then
				COLOR="RED"
			fi
			if [[ "$ERROR_RES2" -eq 1 && "$function" == "switch_screenOutput" && "$text" == resolution2 ]]; then
				COLOR="RED"
			fi
			if [[ "$ERROR_FREQ2" -eq 1 && "$function" == "switch_screenOutput" && "$text" == frequency2 ]]; then
				COLOR="RED"
			fi
			if [[ "$ERROR_RES3" -eq 1 && "$function" == "switch_screenOutput" && "$text" == resolution3 ]]; then
				COLOR="RED"
			fi
			if [[ "$ERROR_FREQ3" -eq 1 && "$function" == "switch_screenOutput" && "$text" == frequency3 ]]; then
				COLOR="RED"
			fi
			printf "${!COLOR}%s%s%s %s${NC}\n" "$text" "$(addSpaces "${spaces}" "${text}")" "$var"

		else 
			printf "color or nocolor is missing as 2nd argument"; fi
	fi
}

function printTree {

	if   [[ "$1" == tree ]]; then 												spaces="39"
	elif [[ "$1" == "error" ]]; then 											spaces="39"
	elif [[ "$1" == subtree  || "$1" == subtree_end ]]; then 								spaces="38"
	elif [[ "$1" == subsubintree || "$1" == subsubintree_end || "$1" == subsubouttree || "$1" == subsubouttree_end ]]; then spaces="35"
	elif [[ "$1" == subsubsubtree || "$1" == subsubsubtree_end ]]; then 							spaces="30"
	else printf "The number of spaces could not be found"
	fi
	writeTree "$1" "$function"
	printLine "$1" "$2" "$3"
}

function addSpaces {

	nbr_spaces=$(( $1 - ${#2} ))
	for i in $(seq 1 "$nbr_spaces"); do printf " "; done
}

function checkSuccess {

	if [[ "$?" == 0 ]]; then
		printTree tree color GREEN
       		printf ": ${GREEN}%s${NC}\n" "OK" 
	else
		printTree tree color RED
		printf ": ${RED}%s${NC}\n" "Fail"

		text=""
		var=""
		if [[ "$function" == switch_audioOutput ]]; then 
			printTree "error" "color"
			printf "${RED}%s${NC} %s" "ERROR in functions:" "$ERROR" 
			printf " %s\n" "(please check the cards/profiles names)"
		elif [[ "$function" == switch_screenOutput ]]; then 
			printTree "error" "color"
			printf "${RED}%s${NC} %s" "ERROR in functions:" "$ERROR" 
			printf " %s\n"  "(please check the resolutions/frequencies)"
		fi
	fi
}

function show_usage {

	printf '\n%s\n'   "To use this script, you need to give as argument:"
	printf '%s\n'     "	-s: 1 (activate) or 0 (deactivate), for the DVD mode needed	(obligatory)"
	printf '%s\n'     "	-m: same, 2 or 3, for the screens mode needed (default: same)	(optional)"
	printf '%s\n'     "	-c: sound card name, without the profile. -p is needed          (optional)"
	printf '%s\n'     "	-p: sound profile name. In this case, -c is needed              (optional)"
	printf '%s\n'     "	-i: used to init the configuration. Cannot be used with -r	(optional)"
	printf '%s\n'     "	-r: used to reset the configuration. Cannot be used with -i     (optional)"
	printf '%s\n'     "	-v: for verbose mode						(optional)"
	printf '%s\n'     "	-w: for more verbose mode					(optional)"
	printf '%s\n\n'   "	-u: to show this usage						(optional)"
}

function run {

	ls_functions=( )
	#ls_functions=("switch_powerDisplay" "switch_screensaver" "switch_inactivity" "switch_audioOutput" "switch_screenOutput")
	ls_functions=("switch_powerDisplay" "switch_screensaver" "switch_inactivity" "switch_screenOutput" "switch_audioOutput")

	for function in "${ls_functions[@]}"; do
		"$function"
		checkSuccess

		if [[ "$debug" -eq 1 || "$debug" -eq 2 ]]; then

			if [[ "$function" == switch_powerDisplay ]]; then
				text="sleep-display-ac"
				var="$display_ac seconds"
				printTree subtree_end nocolor
			fi

			if [[ "$function" == switch_screensaver ]]; then
				text="idle-activation-enabled"
				var="$idle_enabled"
				printTree subtree nocolor
				text="lock-enabled"
				var="$lock_enabled"
				printTree subtree_end nocolor
			fi

			if [[ "$function" == switch_inactivity ]]; then
				text="idle-delay"
				var="$idle_delay minutes"
				printTree subtree_end nocolor
			fi

			if [[ "$function" == switch_screenOutput ]]; then
				if [[ "$debug" -eq 2 ]]; then
					text="number of screens"
					var="$nbr_screens"
					printTree subtree nocolor
				fi
				text="primary"
				var="$ls_primary"
				printTree subtree color
				if [[ "$debug" -eq 2 ]]; then
					text="resolution1"
					var="$resolution1"
					printTree subsubintree nocolor
					text="resolutions supported"
					var="${resolutions1_supported[@]}"
					printTree subsubintree nocolor
					text="scaling"
					var="1.0x1.0"
					printTree subsubintree nocolor
					text="frequency1"
					var="$frequency1"
					printTree subsubintree nocolor
					text="frequencies on this resolution"
					var="${freqs1_supported[@]}"
					printTree subsubintree_end nocolor
				fi
				if [[ "$nbr_screens" -ge 2 ]]; then
					text="secondary"
                                        var="$ls_secondary"
				
					if [[ "$nbr_screens" -eq 2 ]]; then
						
						if [[ "$status" -eq 1 ]];   then printTree subtree_end color 
						elif [[ "$status" -eq 0 ]]; then printTree subtree_end nocolor; fi
					
						if [[ "$debug" -eq 2 ]]; then
							text="resolution2"
							var="$resolution2"
							printTree subsubouttree nocolor
							text="resolutions supported"
							var="${resolutions2_supported[@]}"
							printTree subsubouttree nocolor
							text="scaling"
							var="$scale_screen2"
							printTree subsubouttree nocolor
							text="frequency2"
							var="$frequency2"
							printTree subsubouttree nocolor
							text="frequencies on this resolution"
							var="${freqs2_supported[@]}"
							printTree subsubouttree nocolor
							text="position"
							var="$position2_txt"
							printTree subsubouttree_end nocolor
						fi
					fi
					if [[ "$nbr_screens" -eq 3 ]]; then
						
						if [[ "$status" -eq 1 ]];   then printTree subtree color 
						elif [[ "$status" -eq 0 ]]; then printTree subtree nocolor; fi
						
						text="tierce"
                                        	var="$ls_tierce"
						
						if [[ "$status" -eq 1 ]];   then printTree subtree_end color 
						elif [[ "$status" -eq 0 ]]; then printTree subtree_end nocolor; fi

						if [[ "$debug" -eq 2 ]]; then
							text="resolution"
							var="$resolution3"
							printTree subsubouttree nocolor
							text="resolutions supported"
							var="${resolutions3_supported[@]}"
							printTree subsubouttree nocolor
							text="scaling"
							var="$scale_screen3"
							printTree subsubouttree nocolor
							text="frequency"
							var="$frequency3"
							printTree subsubouttree nocolor
							text="frequencies on this resolution"
							var="${freqs3_supported[@]}"
							printTree subsubouttree nocolor
							text="position"
							var="$position3_txt"
							printTree subsubouttree_end nocolor
						fi
					fi
				fi
			fi

			if [[ "$function" == switch_audioOutput ]]; then
				text="card name"
				var="$card"
				printTree subtree color
				if [[ "$debug" -eq 2 ]]; then
					text="card index"
					var="$card_index"
					printTree subsubintree_end nocolor
				fi
				text="profile name"
				var="output:$profile"
				printTree subtree color
				if [[ "$debug" -eq 2 ]]; then
					text="number of profiles"
					var="$nbr_profiles"
					printTree subsubintree_end nocolor
				fi
				text="sink name"
				var="$sink_name"
				printTree subtree color
				if [[ "$debug" -eq 2 ]]; then
					text="sink index"
					var="$sink_index"
					printTree subsubintree_end nocolor
				fi
				text="source name"
				var="$source_name"
				printTree subtree color
				if [[ "$debug" -eq 2 ]]; then
					text="source index"
					var="$source_index"
					printTree subsubintree_end nocolor
					text="number of sinks on all cards"
					var="$nbr_sinks"
					printTree subtree nocolor
					text="sink names found"
					var="$ls_sinks"
					printTree subsubintree_end nocolor
					text="number of sources on all cards"
					var="$nbr_sources"
					printTree subtree nocolor
					text="source names found"
					var="$ls_sources"
					printTree subsubintree_end nocolor
				fi
				text="sounds moved"
				var="$snd_count"
				printTree subtree color
				text="sink status"
				var="$sink_status"
				printTree subtree_end color
			fi
		fi
	done
}


# MAIN

if [[ "$UID" -eq 0 ]]; then
	printf '\n%s\n\n' "!! This script shouldn't be run as root -- Stopping"
	exit 1
fi

getDE
if [[ "$desktop_type" == x11 ]]; then
	if [[ -z $isMate  && -z $isGnome ]]; then
		printf '\n%s\n\n' "!! This script doesn't support your desktop ($desktop) -- sorry"
		exit 1
	fi
else 
	printf '\n%s\n\n' "!! This script only works for a desktop running Xorg -- sorry"
	exit 1
fi	

while getopts "s:m:c:p:irvwhu" option; do
	case "$option" in
		s)   status="${OPTARG}" ;;
		m)   mode="${OPTARG}"  ;;
		c)   card_output="${OPTARG}"  ;;
		p)   profile_output="${OPTARG}"  ;;
		i)   init_config=1 ;;
		r)   reset_config=1 ;;
		v)   debug=1 ;; 
		w)   debug=2 ;; 
 		h|u) show_usage; exit 0 ;;
 		*) show_usage; exit 1 ;;
 	esac
done

case "$mode" in 
	same) display="same_image" ;;
	2)    display="2_screens"  ;;
	3)    display="3_screens"  ;;
esac

if [[ -n $card_output && -z $profile_output ]]  || [[ -z $card_output && -n $profile_output ]]; then
	printf '\n%s\n\n' "!! If you give a soundcard with '-c' as argument, you need to also give a profile with '-p'"
	show_usage
	exit 1
fi

if [[ "$init_config" == 1 && "$reset_config" == 1 ]]; then
	printf '\n%s\n\n' "!! You cannot init and reset the config at the same time"
	show_usage
	exit 1
fi

case "$init_config" in
	1) init ;;
esac

case "$reset_config" in
	1) reset ;;
esac


if [[ "$init_config" -ne 1 && "$reset_config" -ne 1 ]]; then
	if [[ -e $file_config ]]; then type_config="config file"
	else type_config="config manual"; fi

	case "$status" in
		1) printf "\\n${YELLOW}%s${NC}: %s (%s - %s)\\n" "[+] Activation mode DVD" "$type_config" "$desktop" "$desktop_type"; run ;;
		0) printf "\\n${YELLOW}%s${NC}: %s (%s - %s)\\n" "[-] Deactivation mode DVD" "$type_config" "$desktop" "$desktop_type"; run ;;
		*) show_usage; exit 1 ;;
	esac
fi

echo " "
exit 0

exemple d'utilisation (mode 1):
dvd-time 1

EDIT: code actualisé car il est souvent modifié, et que je me sers de cette page comme clé usb quand un portable linux me tombe dans les mains ^^

Dernière modification par Heldwin (01/12/2018 23:26:17)


"The only way to achieve the impossible is to believe it’s possible"

Hors ligne

#2 25/11/2018 18:45:49

Heldwin
Gilets jaunes
Inscription : 07/10/2008
Messages : 4 091

Re : [MATE] script pour lecture DVD

Quelqu'un sait pourquoi lancer ça pose problème ?

xrandr; xrandr --output VGA-0 --primary --mode 1920x1080 --rate 60 --scale 1.0x1.0 --pos 0x0 --rotate normal --output HDMI-0 --mode 1920x1080 --rate 60 --scale 1.0x1.0 --pos 1920x0 --rotate normal

mais pas avec une pause de 1 seconde ? (et même 2 secondes sont nécessaires des fois)

xrandr; sleep 1; xrandr --output VGA-0 --primary --mode 1920x1080 --rate 60 --scale 1.0x1.0 --pos 0x0 --rotate normal --output HDMI-0 --mode 1920x1080 --rate 60 --scale 1.0x1.0 --pos 1920x0 --rotate normal

Et y a-t-il une utilité à mettre un output comme source sur une carte son ?

Dernière modification par Heldwin (26/11/2018 19:28:13)


"The only way to achieve the impossible is to believe it’s possible"

Hors ligne

Pied de page des forums