Last active
October 5, 2022 12:51
-
-
Save firelightning13/c10dfa6e44526a95272e86957a61485d to your computer and use it in GitHub Desktop.
A poorly made script that I've made to facilitate users to use wayland w/o nvidia-drm (disable external monitor) or wayland w/ nvidia-drm (enables external monitor) for Optimus MUXed laptops. This will affect rebootless mode via supergfxctl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# vars for getopt | |
LONGOPTS=mode,status,help | |
OPTIONS=m:sh | |
# use getopt to parse argument idk | |
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") | |
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then | |
exit 2 | |
fi | |
eval set -- "$PARSED" | |
mode=- s=n h=n | |
while true; do | |
case "$1" in | |
-m|--mode) | |
mode="$2" | |
shift 2 | |
break | |
;; | |
-s|--status) | |
s=y | |
shift | |
break | |
;; | |
-h|--help) | |
h=y | |
shift | |
break | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
echo "Programming error" | |
exit 3 | |
;; | |
esac | |
done | |
# handle non-option arguments | |
if [[ $# -eq 0 ]]; then | |
echo echo "$0: Invalid argument. See -h or --help" | |
exit 4 | |
fi | |
# check if the user are in hybrid mode | |
function check_hybrid { | |
gfxstatus=$(supergfxctl -g) | |
if [ $gfxstatus != "hybrid" ] ; then | |
echo "You need to switch to hybrid first before running this tool" | |
exit | |
fi | |
} | |
# check if the file exists, therefor capable of the use of external monitor | |
function check_status { | |
file="/usr/share/glvnd/egl_vendor.d/10_nvidia.json" | |
if [ -e $file ] ; then | |
echo "Your machine IS external monitor capable." | |
return 1 | |
else | |
echo "Your machine IS NOT external monitor capable." | |
return 0 | |
fi | |
} | |
# check if user has sudo this script | |
function check_admin { | |
if [ "$EUID" -ne 0 ] | |
then echo "$0: Run this tool as root first." | |
exit 4 | |
fi | |
check_hybrid | |
} | |
# create NVIDIA JSON file that points to libEGL | |
function create_json { | |
cat > /usr/share/glvnd/egl_vendor.d/10_nvidia.json << EOF | |
{ | |
"file_format_version" : "1.0.0", | |
"ICD" : { | |
"library_path" : "libEGL_nvidia.so.0" | |
} | |
} | |
EOF | |
} | |
# create config for RTD3 | |
function create_config { | |
cat > /etc/modprobe.d/nvidia.conf << EOF | |
options nvidia "NVreg_DynamicPowerManagement=0x02" | |
EOF | |
} | |
# don't want to ask again | |
# this file will be located under /root | |
function dont_ask { | |
touch ~/.dont_ask | |
} | |
# enable/disable wayland even if nvidia-drm.modeset=0 | |
function wayland_rules { | |
if [ $1 = "enable" ] ; then | |
# remove a comment line if any to ATTR{parameters/modeset}, so user will forced to use Xorg | |
sed -i "/parameters/s/^#//g" /usr/lib/udev/rules.d/61-gdm.rules | |
elif [ $1 = "disable" ] ; then | |
# add comment line to ATTR{parameters/modeset} to enable choose wayland | |
sed -i "/parameters/s/^/#/" /usr/lib/udev/rules.d/61-gdm.rules | |
else | |
echo "Programming error" | |
exit 3 | |
fi | |
} | |
# update NVIDIA modeset via grub2 | |
function config_grub { | |
if [ $1 = "enable" ] ; then | |
sed -i 's/nvidia-drm.modeset=0/nvidia-drm.modeset=1/g' /etc/default/grub | |
elif [ $1 = "disable" ] ; then | |
sed -i 's/nvidia-drm.modeset=1/nvidia-drm.modeset=0/g' /etc/default/grub | |
else | |
echo "Programming error" | |
exit 3 | |
fi | |
$(grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg) | |
} | |
# Enable RTD3 if haven't | |
function enable_rtd { | |
rtdfile="/etc/modprobe.d/nvidia.conf" | |
whoask="~/.dont_ask" | |
if [ ! -f $rtdfile ] && [ ! -f $whoask ] ; then | |
echo "This option applies only for Geforce 16+ Series for power save when using hybrid mode." | |
echo "Do you want to enable RTD3 Power Management? [y/N]" | |
read input | |
if [ $input = "y" ] || [ $input = "Y" ] ; then | |
create_config | |
fi | |
dont_ask | |
fi | |
} | |
# help manual | |
function help_tool { | |
echo "This tool enables or disable use of external monitor. Disable external monitor will enable rootless mode for supergfxctl." | |
echo | |
echo "Valid arguments:" | |
echo -e "-h, --help\tSee help message" | |
echo -e "-m, --mode\tSet emswitch mode. Valid modes are: enable, disable" | |
echo -e "-s, --status\tSee emswitch status" | |
echo | |
echo "Make sure to switch to hybrid mode before using this tool." | |
} | |
if [ $h = y ] ; then | |
help_tool | |
exit | |
fi | |
if [ $s = y ] ; then | |
check_status | |
exit | |
fi | |
if [ $mode = "enable" ] ; then | |
check_status | |
if [ $? = 1 ] ; then | |
echo "The mode is already enabled. Exiting..." | |
exit 2 | |
fi | |
check_admin | |
create_json | |
wayland_rules $mode | |
config_grub $mode | |
elif [ $mode = "disable" ] ; then | |
check_status | |
if [ $? = 0 ] ; then | |
echo "The mode is already disabled. Exiting..." | |
exit 2 | |
fi | |
check_admin | |
enable_rtd | |
# remove NVIDIA json to disable libEGL library | |
rm /usr/share/glvnd/egl_vendor.d/10_nvidia.json | |
wayland_rules $mode | |
config_grub $mode | |
else | |
echo "Invalid mode. Valid modes are: enable , disable" | |
exit 2 | |
fi | |
echo "Use of external monitor: " $mode | |
echo "System reboot required. Do you want to reboot now? [Y/n]" | |
read input | |
if [ $input = "n" ] || [ $input = "N" ] ; then | |
exit | |
fi | |
# reboot the system | |
$(reboot now) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment