Skip to content

Commit

Permalink
Use systemd service to trigger select-display from udev
Browse files Browse the repository at this point in the history
This change should help avoid freezing due to udev evoking the script
too early.
  • Loading branch information
knuton committed May 2, 2024
1 parent 20c2e34 commit 1721391
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
60 changes: 27 additions & 33 deletions application.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,6 @@ rec {
];

module = { config, lib, pkgs, ... }:
let
selectDisplay = pkgs.writeShellScriptBin "select-display" ''
# Discover connected displays
scaling_pref=$(cat /var/lib/gui-localization/screen-scaling 2>/dev/null || echo "default")
connected_outputs=$(${pkgs.xorg.xrandr}/bin/xrandr | ${pkgs.gnugrep}/bin/grep ' connected' | ${pkgs.gawk}/bin/awk '{ print $1 }')
if [ -z "$connected_outputs" ]; then
echo "No connected outputs found. Trying to apply xrandr globally."
case "$scaling_pref" in
"default" | "full-hd")
${pkgs.xorg.xrandr}/bin/xrandr --size 1920x1080;;
"native")
${pkgs.xorg.xrandr}/bin/xrandr --auto;;
*)
echo "Unknown scaling preference '$scaling_pref'. Ignoring.";;
esac
else
for output in $connected_outputs; do
case "$scaling_pref" in
"default" | "full-hd")
${pkgs.xorg.xrandr}/bin/xrandr --output $output --mode 1920x1080;;
"native")
${pkgs.xorg.xrandr}/bin/xrandr --output $output --auto;;
*)
echo "Unknown scaling preference '$scaling_pref'. Ignoring.";;
esac
done
fi
'';
in
{

imports = [ ./application/playos-status.nix ];
Expand All @@ -75,7 +46,13 @@ rec {
};

# System-wide packages
environment.systemPackages = with pkgs; [ breeze-contrast-cursor-theme ];
environment.systemPackages = with pkgs; [
breeze-contrast-cursor-theme

gnugrep
gawk
xorg.xrandr
];

# Kiosk session
services.xserver = let sessionName = "kiosk-browser";
Expand All @@ -101,7 +78,7 @@ rec {
fi
# Set preferred screen resolution
${selectDisplay}/bin/select-display
${./application/select-display.sh} > /home/play/select-display-output
# Enable Qt WebEngine Developer Tools (https://doc.qt.io/qt-5/qtwebengine-debugging.html)
export QTWEBENGINE_REMOTE_DEBUGGING="127.0.0.1:3355"
Expand Down Expand Up @@ -146,10 +123,27 @@ rec {
wantedBy = [ "multi-user.target" ];
};

# Video
# Monitor hotplugging
services.udev.extraRules = ''
ACTION=="change", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/play/.Xauthority", RUN+="${selectDisplay}/bin/select-display"
ACTION=="change", SUBSYSTEM=="drm", RUN+="${pkgs.systemd}/bin/systemctl restart select-display.service"
'';
systemd.services."select-display" = {
description = "Select best display to output to";
serviceConfig.Type = "oneshot";
serviceConfig.ExecStart = "${pkgs.bash}/bin/bash ${./application/select-display.sh}";
serviceConfig.User = "play";
serviceConfig.After = "multi-user.target";
environment = {
XAUTHORITY = "${config.users.users.play.home}/.Xauthority";
DISPLAY = ":0";
};
path = with pkgs; [
gnugrep
gawk
xorg.xrandr
];
wantedBy = [ "multi-user.target" ];
};

# Audio
sound.enable = true;
Expand Down
32 changes: 32 additions & 0 deletions application/select-display.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /usr/bin/env bash

# Discover connected displays
scaling_pref=$(cat /var/lib/gui-localization/screen-scaling 2>/dev/null || echo "default")
echo "Using scaling preference '$scaling_pref'"
connected_outputs=$(xrandr | grep ' connected' | awk '{ print $1 }')
echo "Connected outputs: $connected_outputs"
if [ -z "$connected_outputs" ]; then
echo "No connected outputs found. Trying to apply xrandr globally."
case "$scaling_pref" in
"default" | "full-hd")
xrandr --size 1920x1080;;
"native")
xrandr --auto;;
*)
echo "Unknown scaling preference '$scaling_pref'. Applying auto.";
xrandr --auto;;
esac
else
for output in $connected_outputs; do
echo "Enabling connected output $output"
case "$scaling_pref" in
"default" | "full-hd")
xrandr --output $output --mode 1920x1080;;
"native")
xrandr --output $output --auto;;
*)
echo "Unknown scaling preference '$scaling_pref'. Applying auto.";
xrandr --output $output --auto;;
esac
done
fi

0 comments on commit 1721391

Please sign in to comment.