You are not logged in.

#1 2017-09-22 11:31:30

fajung
Member
From: Argentina, Buenos aires
Registered: 2016-05-28
Posts: 16

turn On/Off the notebook's screen when HDMI is connected

hello, as the tittle say, I'm trying to do, is: automaticlly turn Off the notebook screen when I plug in a HDMI cable to a TV. To do so, I followed this Arch Wiki's Automatically switch audio to HDMI to switch the audio from speakers to HDMI, it works perfect, now I tried to add this Script Toggle external monitor does not turn Off the Notebook's screen.

this is the script that the Service runs when the HDMI is plugg/unplugged: "/usr/local/bin/hdmi_sound_toggle.sh"

#!/bin/bash
intern=LVDS1
extern=HDMI1

USER_NAME=fajung
USER_ID=$(id -u "$USER_NAME")
HDMI_STATUS=$(cat /sys/class/drm/card0/*HDMI*/status)

export PULSE_SERVER="unix:/run/user/"$USER_ID"/pulse/native"

if [[ $HDMI_STATUS == connected ]]
then
   sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 output:hdmi-stereo+input:analog-stereo
   sudo -u "$USER_NAME" DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USER_ID/bus notify-send 'Cable HDMI' "Conectado" -i "/usr/share/icons/h$
else
   sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 output:analog-stereo+input:analog-stereo
   sudo -u "$USER_NAME" DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USER_ID/bus notify-send 'Cable HDMI' "Desconectado" -i "/usr/share/icon$
fi

sleep 2

if xrandr | grep "$extern disconnected"; then
    xrandr --output "$extern" --off --output "$intern" --auto
else
    xrandr --output "$intern" --off --output "$extern" --auto
fi

It should Turn Off the notebook screen Off when I connect the HDMI cable, and turn it back On when the HDMI is disconnected, the Audio part of the script works fine.

Offline

#2 2017-09-22 11:56:24

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,742

Re: turn On/Off the notebook's screen when HDMI is connected

And from where do you know that HDMI1 and LVDS1 are correct for your setup? And you likely will want to sudo -u "$USER_NAME" DISPLAY=:0 the xrandr commands as well. You will have to replace these with the correct identifiers for your setup, use

xrandr -q

Last edited by V1del (2017-09-22 11:56:48)

Offline

#3 2017-09-22 12:54:12

fajung
Member
From: Argentina, Buenos aires
Registered: 2016-05-28
Posts: 16

Re: turn On/Off the notebook's screen when HDMI is connected

thanks. this is how it works(I think)-

#!/bin/bash
intern=LVDS1
extern=HDMI1

USER_NAME=fajung
USER_ID=$(id -u "$USER_NAME")

HDMI_STATUS=$(cat /sys/class/drm/card0/*HDMI*/status)
export PULSE_SERVER="unix:/run/user/"$USER_ID"/pulse/native"

if [[ $HDMI_STATUS == connected ]]
then
   sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 output:hdmi-stereo+input:analog-stereo
   sudo -u "$USER_NAME" DISPLAY=:0 xrandr --output "$extern" --auto
   sudo -u "$USER_NAME" DISPLAY=:0 xrandr --output "$intern" --off
   sudo -u "$USER_NAME" DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Cable HDMI' "Conectado" -i "/usr/share/icons/hicolor/256x256/devices/HDMI-256.png" -t 5000
else
   sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 output:analog-stereo+input:analog-stereo
   sudo -u "$USER_NAME" DISPLAY=:0 xrandr --output "$intern" --auto
   sudo -u "$USER_NAME" DISPLAY=:0 xrandr --output "$extern" --off
   sudo -u "$USER_NAME" DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Cable HDMI' "Desconectado" -i "/usr/share/icons/hicolor/256x256/devices/HDMI-256.Disconnected.png" -t 5000
fi

Offline

#4 2017-09-22 13:19:37

seth
Member
Registered: 2012-09-03
Posts: 51,270

Re: turn On/Off the notebook's screen when HDMI is connected

Don't hardcode user/display values but rather adapt https://gist.githubusercontent.com/Alad … tfile1.txt (you're likely not using xinit?)

Offline

Board footer

Powered by FluxBB