@@ -595,12 +595,14 @@ wayland_debug_snapshot() {
595595 log_info " ----- End snapshot: $tag -----"
596596}
597597
598+ # Discover an available Wayland socket from common runtime locations.
599+ # Prefers user-session sockets first, then system-wide sockets such as /run/wayland-*.
598600discover_wayland_socket_anywhere () {
599601 candidates=" "
600602 if [ -n " ${XDG_RUNTIME_DIR:- } " ]; then
601603 candidates=" $candidates $XDG_RUNTIME_DIR "
602604 fi
603- candidates=" $candidates /dev/socket/weston /run/user/0 /run/user/1000 "
605+ candidates=" $candidates /run/user/1000 /run/user/0 /run /dev/socket/weston "
604606
605607 for dir in $candidates ; do
606608 [ -d " $dir " ] || continue
@@ -665,6 +667,113 @@ wayland_connection_ok() {
665667 return 0
666668}
667669
670+ # Return success if any real Weston/Wayland socket currently exists.
671+ # Checks discovered sockets first, then common fallback socket paths.
672+ weston_runtime_socket_exists () {
673+ weston_sock=" "
674+
675+ if command -v discover_wayland_socket_anywhere > /dev/null 2>&1 ; then
676+ weston_sock=$( discover_wayland_socket_anywhere 2> /dev/null | head -n 1 || true)
677+ if [ -n " $weston_sock " ]; then
678+ return 0
679+ fi
680+ fi
681+
682+ for weston_sock in /run/wayland-* /run/user/0/wayland-* /run/user/1000/wayland-* ; do
683+ [ -S " $weston_sock " ] || continue
684+ return 0
685+ done
686+
687+ return 1
688+ }
689+
690+ # Wait until Weston is usable by requiring both process presence and socket availability.
691+ # Returns 0 when ready, 1 after timeout seconds if Weston does not become ready.
692+ weston_wait_ready () {
693+ timeout=" ${1:- 15} "
694+ i=0
695+
696+ while [ " $i " -lt " $timeout " ]; do
697+ weston_proc_ready=1
698+ weston_sock_ready=1
699+
700+ if command -v weston_is_running > /dev/null 2>&1 ; then
701+ if weston_is_running > /dev/null 2>&1 ; then
702+ weston_proc_ready=0
703+ fi
704+ else
705+ if command -v pgrep > /dev/null 2>&1 ; then
706+ if pgrep -x weston > /dev/null 2>&1 ; then
707+ weston_proc_ready=0
708+ fi
709+ fi
710+ fi
711+
712+ if weston_runtime_socket_exists; then
713+ weston_sock_ready=0
714+ fi
715+
716+ if [ " $weston_proc_ready " -eq 0 ] && [ " $weston_sock_ready " -eq 0 ]; then
717+ return 0
718+ fi
719+
720+ i=$(( i + 1 ))
721+ sleep 1
722+ done
723+
724+ log_warn " weston_wait_ready: Weston did not become ready within ${timeout} s"
725+ return 1
726+ }
727+
728+ # Restore Weston runtime after a DRM-exclusive test stopped it.
729+ # Starts socket/service first, then falls back to weston_start, and waits until ready.
730+ weston_restore_runtime () {
731+ timeout=" ${1:- 15} "
732+
733+ if command -v weston_is_running > /dev/null 2>&1 ; then
734+ if weston_is_running > /dev/null 2>&1 && weston_runtime_socket_exists; then
735+ return 0
736+ fi
737+ fi
738+
739+ if command -v systemctl > /dev/null 2>&1 ; then
740+ systemctl start weston.socket > /dev/null 2>&1 || true
741+ systemctl start weston.service > /dev/null 2>&1 || \
742+ systemctl start weston@root.service > /dev/null 2>&1 || true
743+ fi
744+
745+ if weston_wait_ready 3; then
746+ return 0
747+ fi
748+
749+ if command -v weston_start > /dev/null 2>&1 ; then
750+ weston_start > /dev/null 2>&1 || true
751+ fi
752+
753+ weston_wait_ready " $timeout "
754+ }
755+
756+ # Remove stale Wayland socket files only when Weston is not running.
757+ # Best-effort cleanup for common Weston runtime paths; ignores missing files
758+ # and permission errors. Intentionally does not touch /run/wayland-* because
759+ # that may be owned by systemd weston.socket on base builds.
760+ weston_cleanup_stale_sockets () {
761+ if weston_is_running; then
762+ return 0
763+ fi
764+
765+ for s in \
766+ /dev/socket/weston/wayland-* \
767+ /run/user/0/wayland-* \
768+ /run/user/1000/wayland-* \
769+ /tmp/wayland-* \
770+ " ${XDG_RUNTIME_DIR:-/ nonexistent} " /wayland-* ; do
771+ [ -S " $s " ] || continue
772+ rm -f " $s " 2> /dev/null || true
773+ done
774+
775+ return 0
776+ }
668777# ##############################################################################
669778# Hz helpers
670779# ##############################################################################
0 commit comments