2

Is there any way to configure Ubuntu to set default "first-try" monitor resolution whenever a (or two) monitor plugged in. Regardless of what graphic cards or graphic driver that being used.

For example, when I plug HDMI monitor which support 1920x1080, ubuntu plays well by automatically set the screen resolution to FullHD. That's good.

But what I need is simple 1024x768 resolution, over HDMI. To solve this, I can easily go to system settings - display and set it, ubuntu also remember my setup. Or using xrandr command.

But everytime I change monitor, I need to go to system settings (or run another xrandr command again).

So, is there anyway to tell ubuntu, something like

"for every monitor that plugged in, no matter it is via D-SUB or HDMI or whatever, try to check if it can support 1024x768, and if it does, use that resolution"

I'm thinking of

  1. Generate something like generic monitors.xml for all monitor vendor and models
  2. Run custom xrandr script everytime a monitor is plugged/unplugged, but may be this will cause screen flicker.

But I still have no idea how to implement those.

As information, the system is configured to use lightdm, if that matters. I've reading about xrandr, monitors.xml, but still can't find the solutions. Thanks in advance.

  • Can be done, but are you allergic to a small background script? – Jacob Vlijm May 24 '16 at 6:13
  • No, bash script is fine. Please advice. Thanks. – Lee May 24 '16 at 6:30
  • Great. I have to run now, but will post in the afternoon if no one posted a brilliant solution in between. – Jacob Vlijm May 24 '16 at 6:34
2

Background script to check for new screens

Once per 5 seconds, the background script below looks for new screens to be connected.
If that happens, it tries to set the resolution to what you set in the head section of the script (see: How to use).

A notification shows if the creen was successfully set:

enter image description here

If the command to set the resolution fails, it shows a notification:

enter image description here

The script

#!/usr/bin/env python3
import subprocess
import time

# --- set your main screen below, since you probably want to set it differently
main_screen = "DVI-I-1"
# --- set the tried resolution below
res = "200x300"
#---

screens1 = [main_screen]

def screeens():
    [l.split()[0] for l in subprocess.check_output("xrandr")\
     .decode("utf-8").splitlines() if " connected" in l]

while True:
    time.sleep(5)
    screens2 = [l.split()[0] for l in subprocess.check_output("xrandr").decode("utf-8")\
               .splitlines() if " connected" in l]
    new = [scr for scr in screens2 if not scr in screens1]
    for scr in new:
        try:
            subprocess.check_call(["xrandr", "--auto", "--output", scr, "--mode", res])
            # small break to give the screens time to adapt
            time.sleep(5)
            subprocess.Popen(["notify-send", "successfully set "+scr+" to "+res])
        except subprocess.CalledProcessError:
            subprocess.Popen(["notify-send", "failed to set resolution of "+scr+" to "+res])
    screens1 = screens2

How to use

  1. Copy the script into an empty file, save it as set_screens.py
  2. Set in the head section of the script:

    • the name of your default screen, since you probably want to exclude it from the automatic resolution- set.
    • the resolution you want to be tried on new screens.

      # --- set your main screen below, since you probably want to set it differently
      main_screen = "DVI-I-1"
      # --- set the tried resolution below
      res = "200X300"
      #---
      
  3. Test- run the script by the command (from a terminal):

    python /path/to/set_screens.py
    

If all works fine, add it to Startup Applications:

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

Notes

  • The sleep 15 is essential in the startup command, especially if secundary screens are possibly attached. The script will break if the desktop is not fully loaded yet if it starts to run.

    If the script runs fine from terminal, but breaks as a sartup command, play a bit with the sleep 15.

  • If you don't want to exclude your main screen from trying to set the resolution, change the line:

    screens1 = [main_screen]
    

    into:

    screens1 = []
    

Running a background script

If you don't need to run a background script, then don't. If it adds important functionality and/or saves you time, I don't see any reason not to however, if:

  • There is no other, 100% clean and easy solution available, with the same functionality.

and

  • The script is well written and adds no noticeable cpu load

In this case, checking xrandr once per five seconds means nothing to your system.

  • I'll try it when i get back to site. But, just a little thought here, does the loop will affect cpu and system performance? – Lee May 24 '16 at 11:24
  • @Lee Theoretically, yes, a tiny little bit. Measurable, noticeable, definitely no. I run this kind of scrips constantly, 5-10 at a time, on a computer from the stoneage. I always check scripts on cpu load. Once per 5 seconds check xrandr is nothing. While this script means nothing, It can be made even less if the command dconf read /org/compiz/profiles/unity/plugins/core/outputs has an output on your system. Then the command checks the dconf database in binary format, which is even less. – Jacob Vlijm May 24 '16 at 11:50
1

I constructed a couple of desktop shortcuts to achieve this sort of thing on my laptop. They are

$ cat LowRes.desktop

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_GB]=gnome-panel-launcher
Name[en_GB]=LowRes
Exec=xrandr -s 8
Comment[en_GB]=1368x768
Name=LowResDisplay
Comment=1368x768
Icon=gnome-panel-launcher

and

cat HighRes.desktop

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_GB]=gnome-panel-launcher
Name[en_GB]=HighRes
Exec=xrandr -s 4
Comment[en_GB]=1600x900
Name=HighResDisplay
Comment=1600x900
Icon=gnome-panel-launcher

Maybe something similar might be useful for you.

  • Thank you, but i think still require user to "click" the desktop icon. I need it to be automatic. – Lee May 24 '16 at 8:18
1

I created a script for one of my monitors and run it as I connect the monitor. So you can make similar different scripts for your different monitor types and run each as per requirement.

#!/bin/bash
sudo xrandr --addmode VGA-0 1440x900_60.00
sudo xrandr --newmode  Modeline "1440x900_60.00"  106.47  1440 1520   1672 1904  900 901 904 932  -HSync +Vsync

Create your Modeline for your monitor with the following

cvt 1440x900 60

horizontal pixels x vertical pixesl refreshrate

  • Is it "automatically" run as the monitor connected? I need it to be automatic. Thank you. – Lee May 24 '16 at 8:17
  • Nope. You will have to run the scripts manually after you switch your monitors. – Sushrut Kanetkar May 24 '16 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.