Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display monitor hotplugging v2 #199

Merged
merged 17 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions application.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ rec {
(import ./application/overlays version)
];

module = { config, lib, pkgs, ... }: {
module = { config, lib, pkgs, ... }:
let
selectDisplay = pkgs.writeShellApplication {
name = "select-display";
runtimeInputs = with pkgs; [
gnugrep
gawk
xorg.xrandr
bash
];
text = (builtins.readFile ./application/select-display.sh);
};
in {

imports = [
./application/playos-status.nix
Expand Down Expand Up @@ -67,6 +79,9 @@ rec {
xset s noblank
xset -dpms

# Select best display to output to
${selectDisplay}/bin/select-display || true

# Localization for xsession
if [ -f /var/lib/gui-localization/lang ]; then
export LANG=$(cat /var/lib/gui-localization/lang)
Expand All @@ -75,19 +90,6 @@ rec {
setxkbmap $(cat /var/lib/gui-localization/keymap) || true
fi

# Set preferred screen resolution
scaling_pref=$(cat /var/lib/gui-localization/screen-scaling 2>/dev/null || echo "default")
case "$scaling_pref" in
"default" | "full-hd")
xrandr --size 1920x1080;;
"native")
# Nothing to do, let system decide.
;;
*)
echo "Unknown scaling preference '$scaling_pref'. Ignoring."
;;
esac

# Enable Qt WebEngine Developer Tools (https://doc.qt.io/qt-6/qtwebengine-debugging.html)
export QTWEBENGINE_REMOTE_DEBUGGING="127.0.0.1:3355"

Expand Down Expand Up @@ -145,6 +147,24 @@ rec {
wantedBy = [ "multi-user.target" ];
};

# Monitor hotplugging
services.udev.extraRules = ''
ACTION=="change", SUBSYSTEM=="drm", RUN+="${pkgs.systemd}/bin/systemctl start select-display.service"
'';
systemd.services."select-display" = {
description = "Select best display to output to";
serviceConfig = {
Type = "oneshot";
ExecStart = "${selectDisplay}/bin/select-display";
User = "play";
};
environment = {
XAUTHORITY = "${config.users.users.play.home}/.Xauthority";
DISPLAY = ":0";
};
after = [ "graphical.target" ];
};

# Audio
sound.enable = true;
hardware.pulseaudio = {
Expand Down
50 changes: 50 additions & 0 deletions application/select-display.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
set -euo pipefail

SCALING_PREF=$(cat /var/lib/gui-localization/screen-scaling 2>/dev/null || echo "default")
readonly SCALING_PREF

CONNECTED_OUTPUTS=$(xrandr | grep ' connected' | awk '{ print $1 }')
readonly CONNECTED_OUTPUTS

echo -e "Connected outputs:\n$CONNECTED_OUTPUTS\n"

scaling_pref_params=""

echo "Using scaling preference '$SCALING_PREF'"

case "$SCALING_PREF" in
"default" | "full-hd")
scaling_pref_params=(--mode 1920x1080)
;;
"native")
scaling_pref_params=(--auto)
;;
*)
scaling_pref_params=(--auto)
;;
esac

if [ -z "$CONNECTED_OUTPUTS" ]; then

echo "No connected outputs found. Attempting to apply xrandr globally."
xrandr --auto # this is kind of useless?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From xrandr man pages:

For disconnected but enabled outputs, this will disable them.

So if all are disconnected, this will simply disable them. I suggest to replace this with a warning, unless there's some reason to do this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this in b1d2db7


else


first_functional_output=""
for output in $CONNECTED_OUTPUTS; do
if [ -z "$first_functional_output" ]; then
if xrandr --output "$output" --primary "${scaling_pref_params[@]}"; then
first_functional_output=$output
echo "Configured display $output as primary"
else
echo "Failed to configure display $output"
fi
else
xrandr --output "$output" \
--same-as "$first_functional_output" \
"${scaling_pref_params[@]}" || echo "Failed to configure display $output"
fi
done
fi