#!/bin/bash
# Anbernic RG DS: DS-style dual-screen layout for X11.
# Stacks DSI-2 (top, primary) over DSI-1 (bottom) and maps each gt911
# touchscreen (i2c controllers fe5c0000 / fe5e0000) to its own panel.
# X11 only: bail out under Wayland / no DISPLAY so xrandr/xinput never error.
[ "${XDG_SESSION_TYPE:-}" = "x11" ] && [ -n "${DISPLAY:-}" ] || exit 0
sleep 2
xrandr --output DSI-1 --pos 0x480
xrandr --output DSI-2 --primary --pos 0x0
DISP_A=DSI-2; DISP_B=DSI-1
ev_for(){ readlink -f /dev/input/by-path/platform-$1.i2c*-event 2>/dev/null; }
id_for(){ local w="$1" id n; for id in $(xinput list --id-only); do
n=$(xinput list-props "$id" 2>/dev/null | grep -oP 'Device Node[^"]*"\K[^"]+')
[ "$n" = "$w" ] && { echo "$id"; return; }; done; }
A=$(id_for "$(ev_for fe5c0000)"); B=$(id_for "$(ev_for fe5e0000)")
[ -n "$A" ] && xinput map-to-output "$A" "$DISP_A"
[ -n "$B" ] && xinput map-to-output "$B" "$DISP_B"
