1

I have a custom xrandr script which enables my external monitors and disables my laptop's monitor. When I disconnect my external monitor, I can't enable the monitor because my screen is blank.

I'd love to be able to access my console tty (ctrl-alt-f1), and trigger xrandr to go on (i.e. xrandr --output eDP1 --auto). Running that in my tty says "Can't open display". Any tips for how I can do that?

Jacob Vlijm
  • 83,767
  • Why not simply create a keyboard shortcut, calling the command? the command? (System Settings > Keyboard > Shortcuts > Custom Shortcuts > ) – Jacob Vlijm Mar 25 '16 at 21:17
  • I'll have to test it out to make sure my screensaver doesn't capture the key press, but thanks. I'll report back. – Justin Abrahms Mar 25 '16 at 21:20
  • I just tested to make sure, made xrandr shut off my screen, re enable by keyboard shortcut :) – Jacob Vlijm Mar 25 '16 at 21:21
  • If you know the X screen number you should be able to give it explicitly using xrandr's -d (--display) parameter e.g. xrandr -d :0 --output eDP1 --auto – steeldriver Mar 25 '16 at 21:40
  • 1
    Thing is the Unity Settings Daemon does automatically reconfigure screens for some graphics cards. For instance I've an Intel graphics card on one laptop which does reconfigure my monitor automatically once i unplug VGA cable, but on the other ( AMD ) it doesn't automatically reconfigure it. Try running dbus-monitor --profile "type='signal'" | grep '.*MonitorChanged.*' command see if it detects MonitorChanged signal when you disable that signal and let me know. ( You can use Ctrl + C to stop that command by the way ) – Sergiy Kolodyazhnyy Mar 25 '16 at 21:57
  • @Serg exactly, I have two systems, one does, the other doesn' switch on the remaining monitor. – Jacob Vlijm Mar 25 '16 at 22:01
  • @JacobVlijm what kind of cards do they have , if you don't mind me asking ? – Sergiy Kolodyazhnyy Mar 25 '16 at 22:02
  • @Serg I don't mind :), but I am unable to check atm :) – Jacob Vlijm Mar 25 '16 at 22:03
  • @steeldriver I get the error xrandr: Configure crtc 0 failed when running that. The -d :0 works in a display-enabled console though. – Justin Abrahms Mar 25 '16 at 22:05
  • @JustinAbrahms hi , did you see my comment ? Can you please try running the command I posted previously ? – Sergiy Kolodyazhnyy Mar 26 '16 at 00:17
  • I don't use unity, unfortunately. – Justin Abrahms Mar 26 '16 at 00:25
  • @JustinAbrahms what do you use then ? Gnome ? – Sergiy Kolodyazhnyy Mar 26 '16 at 00:52
  • I'm using Xmonad. – Justin Abrahms Mar 26 '16 at 08:44
  • This answer might be helpful for those looking at the above question: https://askubuntu.com/a/839371/129295. If I understand correctly, the idea is to use sleep in order to have time to switch to the graphical console before xrandr is called. – bli Mar 13 '18 at 17:30

1 Answers1

1

No need to go into console, you could achieve the same by adding a custom keyboard shortcut to re- enable your internal screen.

Choose System Settings > Keyboard > Shortcuts > Custom Shortcuts, click the + and add the command to a shortcut of your choice:

xrandr --output <screenname> --auto

Just tested it on my system (laptop, 15.10); switched off my screen and successfully re- enabled it with the keyboard shortcut, running the command :)

Alternatively

You could use an edited version of this script. The version below (small background script, is checking once per four seconds if an external screen is connected) makes sure your internal screen is switched on

#!/usr/bin/env python3
import subprocess
import time
# --- set your internal screen below (the example is my primary screen)
internal = "DVI-I-1"
#---

# don't change anything below
scr_info1 = 0

while True:
    time.sleep(4)
    # read the current screen setup from xrandr
    get_screens = subprocess.check_output("xrandr").decode("utf-8").splitlines()
    scr_data = [l for l in get_screens if " connected " in l]
    # count the number of connected screens
    scr_info2  = len(scr_data)
    # if the number of connected screens changes, 
    # switch off the internal one if there are two screens
    if scr_info2 != scr_info1:
        if scr_info2 == 2:
            ext = [s.split()[0] for s in scr_data if not internal in s][0]
            subprocess.Popen(["xrandr", "--output", internal, "--off", "--output", ext, "--auto"])
        else:
            subprocess.Popen(["xrandr", "--output", internal, "--auto"])

    scr_info1 = scr_info2

How to setup

  1. Copy the script above into an empty file, save it as switch_screens.py
  2. In the head section of your script, set the name of your internal screen. To find out, open a terminal window and run the command xrandr (no external screen connected) The line with "connected" in it shows the name of your screen in the first string, looking like: VGA-1 or something like that.
  3. Test- run it by opening a terminal window and typ the command:

    python3 /path/to/switch_screens.py
    

    While the script runs, connect your external screen, wait for you internal screen to switch of and disconnect again.

  4. If all works fine, add the command below to Startup Applications: open Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 15 && python3 /path/to/switch_screens.py"
    

Log out and back in. Now your internal screen is switched off automatically if an external screen is connected, re- enabling it if you disconnect.

The script adds no noticeable burden at all to your system.

Jacob Vlijm
  • 83,767