optirun-prime-switcher icon indicating copy to clipboard operation
optirun-prime-switcher copied to clipboard

New version of the script which should work on Ubuntu 15.04

Open deveee opened this issue 9 years ago • 0 comments

If you are interested in it, I'm sending new version of this script, which should work on Ubuntu 15.04.

It needed some changes:

  • new xorg.conf file
  • switch to systemd
  • disabled bumblebeed when using nvidia-prime, because bumblebee was turning off nvidia graphics card during restart computer.

Now it is standalone script, which should work in typical case without additional work.

Dependencies:

  • gxmessage/zenity
  • Gnome
  • GDM
  • Systemd

You can rewrite it for Lightdm/Unity if you want ;)

I checked it with nvidia drivers 346.59.

#!/bin/bash

if [ $# = 0 ]; then
    if [ ! -z "$DISPLAY" ] && [ -x /usr/bin/gxmessage ]; then
        gxmessage -title "nvidia-switch" -center -buttons \
                    "Nvidia":2,"Intel":3 \
                    "Set nvidia or intel graphics card.";

        case $? in
          "2") gksudo nohup $0 nvidia ;;
          "3") gksudo nohup $0 intel ;;
        esac
    elif [ ! -z "$DISPLAY" ] && [ -x /usr/bin/zenity ]; then
        ANS=$(zenity --title "nvidia-switch" \
                --text "Set nvidia or intel graphics card." \
                --list --radiolist --hide-header \
                --column="" --column="Card" --column="" \
                --hide-column=3 --print-column=3 \
                TRUE nvidia 2 FALSE intel 3)

        case "$ANS" in
          "2") gksudo nohup $0 nvidia ;;
          "3") gksudo nohup $0 intel ;;
        esac
    else
        echo "Use the one of following commands:"
        echo
        echo "1) sudo nohup $0 nvidia"
        echo "2) sudo nohup $0 intel"
        echo

        read command

        if [ "$command" = "1" ]; then
            sudo nohup "$0" nvidia
        fi

        if [ "$command" = "2" ]; then
            sudo nohup "$0" intel
        fi  
    fi

    exit
fi


if [ `whoami` != "root" ]; then 
    echo "You must run it as root";
    exit
fi


function write_xorg_conf {
    FILE="/etc/X11/xorg.conf"

    echo 'Section "ServerLayout"'                            > "$FILE"
    echo '    Identifier "layout"'                          >> "$FILE"
    echo '    Screen 0 "nvidia"'                            >> "$FILE"
    echo '    Inactive "intel"'                             >> "$FILE"
    echo 'EndSection'                                       >> "$FILE"
    echo ''                                                 >> "$FILE"
    echo 'Section "Device"'                                 >> "$FILE"
    echo '    Identifier "intel"'                           >> "$FILE"
    echo '    Driver "intel"'                               >> "$FILE"
    #~ echo '    BusID "PCI:0:2:0"'                            >> "$FILE"
    #~ echo '    Option "AccelMethod" "SNA"'                   >> "$FILE"
    echo 'EndSection'                                       >> "$FILE"
    echo ''                                                 >> "$FILE"
    echo 'Section "Screen"'                                 >> "$FILE"
    echo '    Identifier "intel"'                           >> "$FILE"
    echo '    Device "intel"'                               >> "$FILE"
    echo 'EndSection'                                       >> "$FILE"
    echo ''                                                 >> "$FILE"
    echo 'Section "Device"'                                 >> "$FILE"
    echo '    Identifier "nvidia"'                          >> "$FILE"
    echo '    Driver "nvidia"'                              >> "$FILE"
    echo '    BusID "PCI:1:0:0"'                            >> "$FILE"
    echo '    Option "ConstrainCursor" "off"'               >> "$FILE"
    echo 'EndSection'                                       >> "$FILE"
    echo ''                                                 >> "$FILE"
    echo 'Section "Screen"'                                 >> "$FILE"
    echo '    Identifier "nvidia"'                          >> "$FILE"
    echo '    Device "nvidia"'                              >> "$FILE"
    echo '    Option "AllowEmptyInitialConfiguration" "on"' >> "$FILE"
    echo '    Option "IgnoreDisplayDevices" "CRT"'          >> "$FILE"
    echo 'EndSection'                                       >> "$FILE"
}


function write_systemd_script {
    FILE="/etc/systemd/system/nvidia-switch.service"

    echo '[Unit]'                                                      > "$FILE"
    echo 'Description=Nvidia-switch start script'                     >> "$FILE"
    #echo 'Before=gdm.service display-manager.service'                 >> "$FILE"
    echo ''                                                           >> "$FILE"
    echo '[Service]'                                                  >> "$FILE"
    echo 'Type=simple'                                                >> "$FILE"
    echo 'User=root'                                                  >> "$FILE"
    echo 'ExecStart=/bin/sh -c "echo \"ON\" > /proc/acpi/bbswitch;"'  >> "$FILE"
    echo ''                                                           >> "$FILE"
    echo '[Install]'                                                  >> "$FILE"
    echo 'WantedBy=graphical.target'                                  >> "$FILE"
}


function get_nvidia_module_name {
    MODULE="`ls /usr/lib | grep -o nvidia\-[0-9][0-9][0-9]\-updates | head -n1`"
    if [ -z "$MODULE" ]; then
        MODULE="`ls /usr/lib | grep -o nvidia\-[0-9][0-9][0-9] | head -n1`"
    fi
    if [ -z "$MODULE" ]; then
        echo "Error! Cannot find nvidia module name. You should modify this script to make it working."
        exit
    fi

    echo "$MODULE"
}


if [ "$1" = "nvidia" ]; then
    NVIDIA_MODULE=$(get_nvidia_module_name)

    gnome-session-quit
    service gdm stop

    echo "ON" > /proc/acpi/bbswitch

    write_xorg_conf
    write_systemd_script
    echo 'xrandr --setprovideroutputsource Intel NVIDIA-0 && xrandr --auto' > /etc/gdm/Prime/Default

    systemctl enable /etc/systemd/system/nvidia-switch.service
    systemctl disable /lib/systemd/system/bumblebeed.service
    systemctl stop bumblebeed

    if [ "`cat /usr/bin/optirun`" != '$@' ]; then 
        cp /usr/bin/optirun /usr/bin/optirun-org 
        echo '$@' > /usr/bin/optirun 
    fi

    update-alternatives --set x86_64-linux-gnu_gl_conf /usr/lib/$NVIDIA_MODULE/ld.so.conf
    update-alternatives --set i386-linux-gnu_gl_conf /usr/lib/$NVIDIA_MODULE/alt_ld.so.conf

    ldconfig

    service gdm start
fi


if [ "$1" = "intel" ]; then
    gnome-session-quit
    service gdm stop

    systemctl disable /etc/systemd/system/nvidia-switch.service
    systemctl enable /lib/systemd/system/bumblebeed.service
    systemctl start bumblebeed

    rm -f /etc/X11/xorg.conf
    rm -f /etc/systemd/system/nvidia-switch.service
    echo > /etc/gdm/Prime/Default

    if [ "`cat /usr/bin/optirun`" = '$@' ]; then 
        mv /usr/bin/optirun-org /usr/bin/optirun
    fi

    update-alternatives --set x86_64-linux-gnu_gl_conf /usr/lib/x86_64-linux-gnu/mesa/ld.so.conf
    update-alternatives --set i386-linux-gnu_gl_conf /usr/lib/i386-linux-gnu/mesa/ld.so.conf

    echo "OFF" > /proc/acpi/bbswitch    
    sleep 2
    rmmod nvidia

    ldconfig

    service gdm start
fi

deveee avatar May 18 '15 22:05 deveee