# 13-04-2022 ⚙ External monitor with laptop

I installed an external monitor connected to my laptop. Since I'm on archlinux I needed to configure almost two things: screen and keyboards.
I wanted that screen stay poweron when I close the laptop and switch to the correct layout for the external keyboard (pure key pro).

## Configure ACPI `handle.sh` script

One easy way is to install acpi and setup script in `/etc/acpi/handler.sh`.

One major issue I faced was that randr need `.Xauthority` of the current session user to work and acpi script handle.sh run as root. So I added this adapted script I originally found on internet :

---
```bash
function GetXuser()
{
  #Guess the user logged on server $1
  w -huf|grep " $1 "|cut -d " " -f 1|head -n 1
}
export DISPLAY=:0
export USER=$(GetXuser $DISPLAY)
export XAUTHORITY=/home/$(GetXuser $DISPLAY)/.Xauthority
```
---

Here piece of code I used to set screen and keyboard on lid events:

---
```bash
button/lid)
    case "$3" in
        close)
            logger 'LID closed'
            if [ $(ls /sys/class/drm/*/status | xargs cat | grep -c "^connected") -gt 1 ]
            then
                logger 'More than one screen'
                logger "$DISPLAY - $XAUTHORITY"
        export WINIT_X11_SCALE_FACTOR=1.33
                xrandr --output eDP1  --off --output HDMI1 --auto --mode 1920x1080 --set "Broadcast RGB" "Full"
        sudo -H -u $USER bash -c "~/share/bin/switch-keyboard-layout.sh"
            else
                logger 'One screen'
                systemctl suspend
            fi
            ;;
        open)
            logger 'LID opened'
            if [ $(ls /sys/class/drm/*/status | xargs cat | grep -c "^connected") -gt 1 ]
            then
                logger 'More than one screen'
                xrandr --output HDMI1 --auto --mode 1920x1080 --set "Broadcast RGB" "Full" --primary --output eDP1 --mode 1440x900 --left-of HDMI1
        sudo -H -u $USER bash -c "~/share/bin/switch-keyboard-layout.sh"
            else
                logger 'One screen'
                xrandr --output eDP1 --mode 1440x900
        sudo -H -u $USER bash -c "~/share/bin/switch-keyboard-layout.sh"
                #systemctl suspend
            fi
            ;;
```
---

Keyboard part is not tested yet, still a WIP and I will update this entry as soon as I progress !

# TODO

* [ ] started to have some udev rules to detect screen plug but not much success to get the event launch the monitor script
* [ ] change layout keyboard on plugin via udev rules