@@ -426,21 +426,56 @@ let
426426 auto-display-scale
427427 pkgs . mako
428428 pkgs . wlr-randr
429+ pkgs . jq
429430 ] ;
430431 bashOptions = [ ] ;
431432 text = ''
432- # Run the following commands in order every time a display change event is detected
433+ stop_services() {
434+ echo "No displays connected. Stopping ghaf-launcher and ewwbar services."
435+ systemctl --user stop ewwbar ghaf-launcher
436+ }
437+
433438 auto-display-scale # Auto scaling
434439 makoctl set-mode default # Reset mako mode so notifications don't break
435440
436- if wlr-randr > /dev/null 2>&1; then
437- # If displays are connected (not headless mode), ensure ewwbar and ghaf-launcher are running
438- systemctl --user is-active --quiet ewwbar || systemctl --user reload-or-restart ewwbar
439- systemctl --user is-active --quiet ghaf-launcher || systemctl --user reload-or-restart ghaf-launcher
440- else
441- # If all displays were disconnected, we can stop ghaf-launcher and ewwbar services
442- echo "No displays connected. Stopping ghaf-launcher and ewwbar services"
443- systemctl --user stop ewwbar ghaf-launcher
441+ # Retrieve display information
442+ if ! wlr_output_json=$(wlr-randr --json); then
443+ echo "Error: Failed to get display info from wlr-randr"
444+ stop_services
445+ fi
446+
447+ # Check if any displays are connected
448+ if ! echo "$wlr_output_json" | jq -e 'length > 0' > /dev/null; then
449+ echo "Error: No connected displays found."
450+ stop_services
451+ fi
452+
453+ # If displays are connected (not headless mode), ensure ewwbar and ghaf-launcher are running
454+ systemctl --user is-active --quiet ewwbar || systemctl --user reload-or-restart ewwbar
455+ systemctl --user is-active --quiet ghaf-launcher || systemctl --user reload-or-restart ghaf-launcher
456+ '' ;
457+ } ;
458+
459+ display-connected = pkgs . writeShellApplication {
460+ name = "display-connected" ;
461+ runtimeInputs = [
462+ pkgs . jq
463+ pkgs . wlr-randr
464+ ] ;
465+ bashOptions = [ ] ;
466+ text = ''
467+ # Exits with error if no display is detected
468+
469+ # Retrieve display information
470+ if ! wlr_output_json=$(wlr-randr --json); then
471+ echo "Error: Failed to get display info from wlr-randr"
472+ exit 1
473+ fi
474+
475+ # Check if any displays are connected
476+ if ! echo "$wlr_output_json" | jq -e 'length > 0' > /dev/null; then
477+ echo "Error: No connected displays found."
478+ exit 1
444479 fi
445480 '' ;
446481 } ;
501536 serviceConfig = {
502537 Type = "simple" ;
503538 EnvironmentFile = "-/etc/locale.conf" ;
504- ExecCondition = ''
505- ${ pkgs . bash } /bin/bash -c "${ pkgs . wlr-randr } /bin/wlr-randr > /dev/null 2>&1"
506- '' ;
539+ ExecCondition = "${ display-connected } /bin/display-connected" ;
507540 ExecStart = "${ pkgs . nwg-drawer } /bin/nwg-drawer -r -nofs -nocats -s ${ drawerStyle } " ;
508541 Restart = "always" ;
509542 RestartSec = "1" ;
569602 Type = "simple" ;
570603 Restart = "always" ;
571604 RestartSec = "5" ;
605+ ExecCondition = "${ display-connected } /bin/display-connected" ;
572606 ExecStart = "${ pkgs . ghaf-audio-control } /bin/GhafAudioControlStandalone --pulseaudio_server=audio-vm:${ toString config . ghaf . services . audio . pulseaudioTcpControlPort } --deamon_mode=true --indicator_icon_name=audio-subwoofer" ;
573607 } ;
574608
0 commit comments