SWITCHING BETWEEN TV AND LAPTOP DISPLAY
       
       
       
       
       Overview
       ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       I often switch between my TV screen and my laptop. If I'm home, I'm
       mostly connected to the TV screen, which I roll around the house on
       its cart. My favorite place to put it is right by my bed. I'll sink
       into a pile of pillows and furry friends, clacking away at my little
       computer tasks for hours. If I'm out-of-doors, then I'm certainly
       using the laptops internal display.
       
       I have two screen layouts setup to accomodate these two displays,
       `tv.sh' and `lappy.sh'. The first layout only uses the external
       display. The second layout only uses the internal display. These
       layouts were configured using `arandr', saved inside
       `~/.screenlayouts' and made executable. Previously, I would switch
       between one and the other by invoking the desired script
       manually. This would put me into inconvenient situations: I'd be
       connected to the TV, I'd unplugged my laptop while it was sleeping,
       and I'd open the lid. The computer'd still have the TV as its primary
       display, despite it being unplugged.
       
       I built a shell script, `display.sh', to automatically switch between
       the two displays. The script is stored in `/etc/zzz.d/resume', where
       it will be executed each time the computer wakes up from its
       slumber. Arisen, it will then set the primary display according to
       what's available.
       
       
       The Script
       ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       ,----
       | #!/bin/sh
       | # Tiny script to switch between TV and laptop display.
       | # Author: ROYGBYTE
       | # Date: September 2023
       | # Version: 0.1
       | LOG_PREFIX="display.sh: "
       | XRANDR_Q=$(xrandr -q)
       | # HDMI could be connect but not the primary display. If that's the case,
       | # xrandr will report the display number, state, and resolution, as matched
       | # for below.
       | HDMI_CONNECTED=$(printf "${XRANDR_Q}" | grep "HDMI1 connected" -c)
       | PRIMARY_DISPLAY=$(printf "${XRANDR_Q}" | grep "^[[:alnum:]]* connected primary" -o \
       |                     | grep "^[[:alnum:]]*[[:space:]]" -o)
       | logger "HDMI Connected: ${HDMI_CONNECTED}"
       | logger "${LOG_PREFIX}Primary display: ${PRIMARY_DISPLAY}"
       | case "$HDMI_CONNECTED" in
       |     0) # No
       |     logger "${LOG_PREFIX}HDMI not connected."
       |     case "$PRIMARY_DISPLAY" in
       |         eDP*) # Using asterisk, because shell pattern doesn't match number, i.e.: eDP1.
       |             logger "${LOG_PREFIX}eDP is primary display. Doing nothing."
       |             ;;
       |         *)
       |             logger "${LOG_PREFIX}Switching internal display to primary."
       |             ~/.screenlayout/lappy.sh
       |             ;;
       |     esac
       |     ;;
       |     1) # Yes
       |     logger "${LOG_PREFIX}HDMI connected."
       |     case "${PRIMARY_DISPLAY}" in
       |         eDP*)
       |             logger "Switching HDMI to primary."
       |             ~/.screenlayout/tv.sh
       |             ;;
       |         HDMI*)
       |             logger "${LOG_PREFIX}HDMI is primary display. Doing nothing."
       |             ;;
       |     esac
       |     ;;
       | esac
       `----
       
       
       Improvements
       ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       Eventually, I'd like to build a `udev' trigger to run this script when
       the HDMI cable is unplugged. I'd also like to understand why the
       strings used to pattern match my case statements (`eDP*', `HDMI*')
       don't work if they contain numbers (indeed, `HDMI1' won't match...).