Setup: Ubuntu, Openbox, notebook, occasionally an external screen.

I’ve set things up so that if an external monitor is attached when I start my notebook, a script (run by Openbox’s autostart), xrandr makes that the primary monitor.

What I’d like to do is run that script when I turn off or otherwise disconnect that external monitor, so that I can make the notebook’s built-in screen the primary again. Does anyone here have any ideas how to do that automatically?

  • Helix 🧬@feddit.deB
    link
    fedilink
    arrow-up
    1
    ·
    3 years ago

    there’s a file called status in cat /sys/class/drm/card0-CONNECTION where CONNECTION is the name xrandr --listmonitors shows on the right. So e.g. for me it looks like this:

    $ xrandr --listmonitors
    Monitors: 2
     0: +*DP-3 2560/553x1440/311+0+430  DP-3
     1: +DP-5 1440/553x2560/311+2560+0  DP-5
    
    $ cat /sys/class/drm/card0-DP-3/status 
    connected
    

    You can watch this file with inotifywatch -e modify. You can put this in a systemd.path unit which calls your script as a one-shot or with an environment parameter.

    You can also use srandrd but IMHO that’s a bit overkill when you can do it with pid0 tools 😅

    • N0b3d@lemmy.mlOP
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      Hmm. Maybe I’m doing something wrong but doing

      inotifywatch -e modify /sys/class/drm/card0-HDMI-A-2/status
      

      only gives me “No events occurred.” (after ctrl-c stops it). Oddly, I can see the file contents change (connected to disconnected and back again) if I cat it in a separate term window so I don’t know what’s going on.

      • Helix 🧬@feddit.deB
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        3 years ago

        hm, maybe you need another event type. What GPU is it?

        Events:
                access          file or directory contents were read
                modify          file or directory contents were written
                attrib          file or directory attributes changed
                close_write     file or directory closed, after being opened in
                                writeable mode
                close_nowrite   file or directory closed, after being opened in
                                read-only mode
                close           file or directory closed, regardless of read/write mode
                open            file or directory opened
                moved_to        file or directory moved to watched directory
                moved_from      file or directory moved from watched directory
                move            file or directory moved to or from watched directory
                move_self               A watched file or directory was moved.
                create          file or directory created within watched directory
                delete          file or directory deleted within watched directory
                delete_self     file or directory was deleted
                unmount         file system containing file or directory unmounted
        

        May be create or delete