From 7272b213f7e2d01faf15a912e137f586c5781cff Mon Sep 17 00:00:00 2001 From: GaspardCulis Date: Fri, 10 Nov 2023 14:05:42 +0100 Subject: [PATCH] Added new x11-toggle-primary-gpu script & config --- bin/x11-toggle-primary-gpu | 81 +++++++++++++++++++ home/xinitrc | 17 ++-- .../10-nvidia-drm-outputclass.conf | 15 ++++ misc/x11-toggle-gpu/20-amdgpu.conf | 5 ++ sync | 2 + 5 files changed, 115 insertions(+), 5 deletions(-) create mode 100755 bin/x11-toggle-primary-gpu create mode 100644 misc/x11-toggle-gpu/10-nvidia-drm-outputclass.conf create mode 100644 misc/x11-toggle-gpu/20-amdgpu.conf diff --git a/bin/x11-toggle-primary-gpu b/bin/x11-toggle-primary-gpu new file mode 100755 index 0000000..999602b --- /dev/null +++ b/bin/x11-toggle-primary-gpu @@ -0,0 +1,81 @@ +#!/bin/bash + +SHARED_DIR='/home/gaspard/.local/share/x11-toggle-gpu' +XORG_DIR='/etc/X11/xorg.conf.d' + +AMD_CONF_FILE='20-amdgpu.conf' +NVIDIA_CONF_FILE='10-nvidia-drm-outputclass.conf' + +get-mode () { + if [ -f "$XORG_DIR/$NVIDIA_CONF_FILE" ]; then + echo 'NVIDIA' + else + echo 'AMD' + fi +} + +check-config () { + if [ ! -d "$SHARED_DIR" ]; then + echo "[ERROR] Shared dir could not be found." + exit 1 + fi + + if [ ! -d "$XORG_DIR" ]; then + echo "[ERROR] X11 config dir could not be found." + exit 2 + fi + + if [ -f "$XORG_DIR/$AMD_CONF_FILE" ] && [ -f "$XORG_DIR/$NVIDIA_CONF_FILE" ]; then + echo "[ERROR] Corrupted configuration folder." + exit 3 + fi + + CONF_FILE="$AMD_CONF_FILE" + if [ "$(get-mode)" == "NVIDIA" ]; then + CONF_FILE="$NVIDIA_CONF_FILE" + fi + + if ! cmp --silent $SHARED_DIR/$CONF_FILE $XORG_DIR/$CONF_FILE ; then + echo "[ERROR] Corrupted configuration file." + exit 4 + fi + +} + +gpu-toggle () { + MODE="$(get-mode)" + + if [ "$MODE" == "AMD" ]; then + rm "$XORG_DIR/$AMD_CONF_FILE" + cp "$SHARED_DIR/$NVIDIA_CONF_FILE" "$XORG_DIR/$NVIDIA_CONF_FILE" + elif [ "$MODE" == "NVIDIA" ]; then + rm "$XORG_DIR/$NVIDIA_CONF_FILE" + cp "$SHARED_DIR/$AMD_CONF_FILE" "$XORG_DIR/$AMD_CONF_FILE" + fi + + echo "[INFO] Primary GPU configuration toggled to $(get-mode)." +} + +check-config + +if [ "$1" == "get" ]; then + echo "$(get-mode)" + exit 0 +fi + +if [ "$1" == "toggle" ]; then + # ROOT NEEDED + if [ "$EUID" -ne 0 ] + then echo "[ERROR] Please run as root" + exit + fi + + gpu-toggle + + exit 0 +fi + +echo "Usage: $0 {get, toggle}" +exit 1 + + diff --git a/home/xinitrc b/home/xinitrc index 56cad80..b982397 100755 --- a/home/xinitrc +++ b/home/xinitrc @@ -29,11 +29,18 @@ if [ -f "$usermodmap" ]; then xmodmap "$usermodmap" fi -if [ -d /etc/X11/xinit/xinitrc.d ] ; then - for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do - [ -x "$f" ] && . "$f" - done - unset f +# if [ -d /etc/X11/xinit/xinitrc.d ] ; then +# for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do +# [ -x "$f" ] && . "$f" +# done +# unset f +# fi + +if type "x11-toggle-primary-gpu" > /dev/null; then + if [ "$(x11-toggle-primary-gpu get)" == "NVIDIA" ]; then + xrandr --setprovideroutputsource modesetting NVIDIA-0 + xrandr --auto + fi fi setxkbmap fr diff --git a/misc/x11-toggle-gpu/10-nvidia-drm-outputclass.conf b/misc/x11-toggle-gpu/10-nvidia-drm-outputclass.conf new file mode 100644 index 0000000..a33043f --- /dev/null +++ b/misc/x11-toggle-gpu/10-nvidia-drm-outputclass.conf @@ -0,0 +1,15 @@ +Section "OutputClass" + Identifier "AMD Graphics" + MatchDriver "amdgpu" + Driver "modesetting" +EndSection + +Section "OutputClass" + Identifier "nvidia" + MatchDriver "nvidia-drm" + Driver "nvidia" + Option "AllowEmptyInitialConfiguration" + Option "PrimaryGPU" "yes" + ModulePath "/usr/lib/nvidia/xorg" + ModulePath "/usr/lib/xorg/modules" +EndSection diff --git a/misc/x11-toggle-gpu/20-amdgpu.conf b/misc/x11-toggle-gpu/20-amdgpu.conf new file mode 100644 index 0000000..153e2dd --- /dev/null +++ b/misc/x11-toggle-gpu/20-amdgpu.conf @@ -0,0 +1,5 @@ +Section "Device" + Identifier "AMD Graphics" + Driver "amdgpu" + Option "TearFree" "true" +EndSection diff --git a/sync b/sync index 02b4298..63b5730 100755 --- a/sync +++ b/sync @@ -26,6 +26,7 @@ synced_files = [ ('home/xinitrc', '~/.xinitrc'), ('misc/picom/', '~/.config/picom/'), ('misc/runst/', '~/.config/runst/'), + ('misc/x11-toggle-gpu/', '~/.local/share/x11-toggle-gpu/'), ('bin/swaylock-hyprland', '~/.local/bin/swaylock-hyprland'), ('bin/Hyprland', '~/.local/bin/Hyprland'), @@ -34,6 +35,7 @@ synced_files = [ ('bin/wtoggle-touchpad', '~/.local/bin/wtoggle-touchpad'), ('bin/togglescreen', '~/.local/bin/togglescreen'), ('bin/mc-key-fix', '~/.local/bin/mc-key-fix'), + ('bin/x11-toggle-primary-gpu', '~/.local/bin/x11-toggle-primary-gpu'), ] def save():