You are not logged in.

#1 2015-10-22 00:00:02

HaloSlayer255
Member
Registered: 2015-02-11
Posts: 29

[SOLVED] Bash Script, xrandr HDMI Monitor switching

Hello all,

This is my first attempt at creating a bash script. Some friends and I watch DVDs or Blu-Rays sometimes over the weekend, I was wondering if I could have programs open directly on the HDMI monitor, close, then open on the primary monitor so the program doesn't stay on the external HDMI monitor.

For example:

Connect HDMI monitor (TV) --> Configure video output for 1440x900 on primary monitor, and 1920x1080 on HDMI monitor with HDMI on the right --> Open VLC in Fullscreen on HDMI Monitor, and use /dev/sr0 as source directory --> DVD/Blu-Ray/media_file.mp4/etc ends (quit vlc after finished playback) --> turn off HDMI monitor and set back to primary display only.

Go to last post for the script

Last edited by HaloSlayer255 (2015-12-17 16:50:13)

Offline

#2 2015-10-22 09:26:14

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

It's not very clear what you really want to do. In the normal xinerama mode, I do not know if it is possible to open a program directly on the other monitor but you can drag the windows on them and make it and make it full screen (if the windows manager support it it will be full screen only on the monitor it is on). You can enable and disable monitor with xrandr and the windows are then displayed on the monitor that is on.

Your script does not make sense at all, probably you want to say:

if xrandr | grep -q 'HDMI-0 connected' ; then
    xrandr --output HDMI-0 --auto --right-of LVDS
fi

but it tests only if HDMI is connected, not if it is enabled ot not.

Last edited by olive (2015-10-22 09:27:17)

Offline

#3 2015-10-22 17:51:30

HaloSlayer255
Member
Registered: 2015-02-11
Posts: 29

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

Thank you for the reply olive,

This is my first attempt at bash scripting. IIRC xinerama mode is for the open source nvidia drivers, correct? I use the proprietary nvidia drivers, and the linux-ck kernel. I tested the code that you have and that works, I was able to tweak it a bit to this:

#/bin/bash

# ~/Desktop/movie_night
# Created by HaloSlayer255 with assistance of olive from https://bbs.archlinux.org/ on October 21st 2015 at 6:55 PM (18:55)
# Bash Script to start media playback on external TV (second display)
# Using /dev/sr0 as the source for Blu-Ray and Digital-Versatile-Disk
# Uses VLC Media Player as the default media playback program
# Uses xrandr to switch between displays

# xrandr second display connected
if xrandr | grep -q 'HDMI-0 connected' ; then
	xrandr --output HDMI-0 --mode 1920x1080 --right-of DVI-I-1;
fi
# Kill xflux to prevent picture color distortion
pkill xflux
# Blu-Ray Media Detected
# Mount Blu-Ray Media to /media/blurays
	sudo mount /dev/sr0 /media/blurays ;
# Set Sound to HDMI 2 Connection 
	pacmd set-default-sink 1
# Start VLC Blu-Ray Playback
	vlc --play-and-exit bluray:///media/blurays/ --bluray-menu --fullscreen --dts-dynrng vlc://quit ;
# Turn Off HDMI-0 TV Connection
	xrandr --output HDMI-0 --off ;
# Reset Display Settings to Primary Monitor Only 
	xrandr --output DVI-I-1 --auto ;
# Reset Sound to Default Device
	pacmd set-default-sink 0 ;
# Restore xflux to help reduce eye strain
	xflux -z <#####> -k 2000
# --x11-display <string> X11 display; video rendered with this X11 display (to test if vlc can load on second display)
exit
#
# DVD Media Detected
#	sudo mount /dev/sr0 /media/dvds ;
#	vlc dvd:///media/dvds --dvdnav-menu --fullscreen vlc://quit ;
#	xrandr --output HDMI-0 --off ; xrandr --output DVI-I-1 --auto ;
#
# Media File Selected

Also didn't know that programs can't be started on another monitor, was hoping this was possible, I keep having to squint my eyes to see the context menus on the other display.
Is it possible to have differenct scenarios for DVDs and blurays? Perhaps something to check if the media is blu-ray or dvd and then mount to either /media/blurays or /media/dvds?


Thank you for the help,
HaloSlayer255

---Edit---
Wondering if its possible to start vlc at the HDMI monitor after all, http://www.videolan.org/doc/vlc-user-guide/en/ch02.html perhaps the --x11-display <string> or xvideo-display <string>
--- End Edit ---

Last edited by HaloSlayer255 (2015-12-18 23:03:09)

Offline

#4 2015-10-23 07:01:00

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

Checking if the media is blue ray or DVD should not be difficult, but I have no blue ray to test. Just check the file, there must be some way to differntiate the two. What "file -s /dev/sr0" says?

Last edited by olive (2015-10-23 07:01:08)

Offline

#5 2015-10-23 12:48:59

HaloSlayer255
Member
Registered: 2015-02-11
Posts: 29

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

Hello olive,

If the media is blu-ray: file -s /dev/sr0 reports the following: /dev/sr0: data

If the media is dvd: file -s /dev/sr0 reports the following: /dev/sr0: UDF filesystem data (version 1.5)

Offline

#6 2015-10-23 13:06:27

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

data means it is not recognized (hence the generic term data). UDF file system is the filesystem of DVD. You can already use that to differntiate the two, although it would be better to explicitly know that the media is blue ray. Anyway you can say something like:

if file -s /dev/sr0 | grep -q 'UDF filesystem' ; then
    echo There is a DVD in the drive
else
   echo The is no DVD in the drive
fi

Offline

#7 2015-10-23 13:18:23

HaloSlayer255
Member
Registered: 2015-02-11
Posts: 29

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

Thanks for the reply olive,

Just change the script to this:

Looks like that is a good start and from my test it executes the right conditions, I was also able to remove some duplicate entries. I was able to have all xflux processes killed with pkill -f xflux and that prevents multiple entries. As for the vlc window I just drag it to the second monitor for now. This script is working when I tested it last as of October 30th 2015.

Thanks for the help,
HaloSlayer255

--- Outdated Script Removed ---

Last edited by HaloSlayer255 (2015-11-22 04:00:43)

Offline

#8 2015-10-30 19:26:00

HaloSlayer255
Member
Registered: 2015-02-11
Posts: 29

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

This is the last post in this thread, promise.

I was able to do this with the script:

#!/bin/bash

# ~/Desktop/Bash_Scripts/movie_night
# Created by HaloSlayer255 with assistance of olive from 
# https://bbs.archlinux.org/ on October 21st 2015 at 6:55 PM (18:55)
# Bash Script to start media playback on external TV (second display)
# Using /dev/sr0 as the source for Blu-Ray and Digital-Versatile-Disk
# Uses VLC Media Player as the default media playback program
# Uses xrandr to switch between displays
#
# xrandr second display connected
if xrandr | grep -q 'HDMI-0 connected' ; then
	xrandr --output HDMI-0 --mode 1920x1080 --right-of DVI-I-1;
# Kill xflux to prevent picture color distortion
	pkill -f xflux ;
# Set Sound to HDMI 2 Connection
	pacmd set-default-sink 1 ;
fi
# Detect Media Inserted in Optical Drive
if file -s /dev/sr0 | grep -q 'UDF filesystem data' ; then
	echo DVD Media Detected ;
# DVD Media Detected
# Checking if Directory Exists
	if [ -d /media/dvds ] ; then
		echo "/media/dvds exists"
	else
	# Creating Mount Point If Needed
		sudo mkdir -p /media/dvds
		echo "/media/dvds created"
	fi
# Mount DVD Media to /media/dvds
	sudo mount /dev/sr0 /media/dvds ;
# Start VLC DVD Playback
	vlc dvd:///media/dvds --dvdnav-menu --fullscreen --dts-dynrng vlc://quit ;
fi
if file -s /dev/sr0 | grep -qv 'UDF filesystem data' ; then 
	echo Blu-Ray Media Detected ;
# Blu-Ray Media Detected
# Checking if Directory Exists
	if [ -d /media/blurays ] ; then
		echo "/media/blurays exists"
	else
	# Creating Mount Point If Needed
		sudo mkdir -p /media/blurays
		echo "/media/blurays created"
	fi
# Mount Blu-Ray Media to /media/blurays
	sudo mount /dev/sr0 /media/blurays ;
# Start VLC Blu-Ray Playback
	vlc bluray:///media/blurays/ --bluray-menu --fullscreen --dts-dynrng vlc://quit ;
fi
# Media File Selected, Optical Drive Not Connected, External HDD connected
if file -s /dev/sr0 | grep -q 'No such file or directory' ; then
vlc --fullscreen --dts-dynrng /path/to/external/harddrive/media/directory/*/*.iso vlc://quit
fi
# Unmount Media from Mount Point
	sudo umount /dev/sr0
# Eject Optical Drive Tray
	eject /dev/sr0
# Turn Off HDMI-0 TV Connection
	xrandr --output HDMI-0 --off ;
# Reset Display Settings to Primary Monitor Only 
	xrandr --output DVI-I-1 --auto ;
# Reset Sound to Default Device
	pacmd set-default-sink 0 ;
# Restore xflux to help reduce eye strain
	xflux -z ##### -k 2000
# --x11-display <string> X11 display; video rendered with this 
# X11 display (to test if vlc can load on second display)
exit

Thanks for the help olive!,
HaloSlayer255

--- Removed duplicate line that mounts dvd to directory /media/dvds ---

Last edited by HaloSlayer255 (2015-12-18 23:03:48)

Offline

#9 2016-05-22 01:40:12

HaloSlayer255
Member
Registered: 2015-02-11
Posts: 29

Re: [SOLVED] Bash Script, xrandr HDMI Monitor switching

As of May 21st 2016 the software xflux seems to have an issue with the latest nvidia drivers update, all new users of the script should comment out the lines regarding xflux since the xflux program doesn't work with nvidia 364.19-3.

Offline

Board footer

Powered by FluxBB