Beispiel #1
0
def _check_daemon_active():

    if not checks.is_daemon_active():
        print("The optimus-manager service is not running. Please enable and start it with :\n\n"
              "sudo systemctl enable optimus-manager\n"
              "sudo systemctl start optimus-manager\n")
        sys.exit(1)
Beispiel #2
0
def main():

    # Arguments parsing
    parser = argparse.ArgumentParser(description="Client program for the Optimus Manager tool.\n"
                                                 "https://github.com/Askannz/optimus-manager")
    parser.add_argument('-v', '--version', action='store_true', help='Print version and exit.')
    parser.add_argument('--print-mode', action='store_true',
                        help="Print the current mode.")
    parser.add_argument('--switch', metavar='MODE', action='store',
                        help="Set the GPU mode to MODE and restart the display manager. "
                             "Possible modes : intel, nvidia, auto (checks the current mode and switch to the other). "
                             "WARNING : All your applications will close ! Be sure to save your work.")
    parser.add_argument('--set-startup', metavar='STARTUP_MODE', action='store',
                        help="Set the startup mode to STARTUP_MODE. Possible modes : "
                             "intel, nvidia, nvidia_once (starts with Nvidia and reverts to Intel for the next boot)")
    parser.add_argument('--print-startup', action='store_true',
                        help="Print the current startup mode.")
    parser.add_argument('--no-confirm', action='store_true',
                        help="Do not ask for confirmation before switching GPUs.")
    parser.add_argument('--cleanup', action='store_true',
                        help="Remove auto-generated configuration files left over by the daemon.")
    args = parser.parse_args()

    # Config loading
    # Even if the client does not need any option from it (yet), we parse the config file.
    # That way the user can see parsing errors without opening a systemd log.
    if not args.version:
        try:
            load_config()
        except ConfigError as e:
            print("Error loading config file : %s" % str(e))
            sys.exit(1)

    #
    # Arguments switch

    if args.version:
        print("Optimus Manager (Client) version %s" % envs.VERSION)
        sys.exit(0)

    elif args.print_mode:

        try:
            mode = checks.read_gpu_mode()
        except checks.CheckError as e:
            print("Error reading mode : %s" % str(e))
            sys.exit(1)

        print("Current mode : %s" % mode)

    elif args.print_startup:

        try:
            startup_mode = var.read_startup_mode()
        except var.VarError as e:
            print("Error reading startup mode : %s" % str(e))
            sys.exit(1)

        print("Current startup mode : %s" % startup_mode)

    elif args.switch:

        if args.switch not in ["auto", "intel", "nvidia"]:
            print("Invalid mode : %s" % args.switch)
            sys.exit(1)

        if not checks.is_daemon_active():
            print("The optimus-manager service is not running. Please enable and start it with :\n\n"
                  "sudo systemctl enable optimus-manager\n"
                  "sudo systemctl start optimus-manager\n")
            sys.exit(1)

        if args.switch == "auto":
            try:
                gpu_mode = checks.read_gpu_mode()
            except checks.CheckError as e:
                print("Error reading mode: %s" % str(e))
                sys.exit(1)

            if gpu_mode == "nvidia":
                switch_mode = "intel"
            else:
                switch_mode = "nvidia"
            print("Switching to : %s" % switch_mode)

        else:
            switch_mode = args.switch

        if args.no_confirm:
            send_command(switch_mode)
        else:
            print("WARNING : You are about to switch GPUs. This will restart the display manager and all your applications WILL CLOSE.\n"
                  "(you can pass the --no-confirm option to disable this warning)\n"
                  "Continue ? (y/N)")
            ans = input("> ").lower()

            if ans == "y":
                send_command(switch_mode)
            elif ans == "n" or ans == "N":
                print("Aborting.")
            else:
                print("Invalid choice. Aborting")

    elif args.set_startup:

        if args.set_startup not in ["intel", "nvidia", "nvidia_once"]:
            print("Invalid startup mode : %s" % args.set_startup)
            sys.exit(1)

        send_command("startup_" + args.set_startup)

    elif args.cleanup:

        if os.geteuid() != 0:
            print("You need to execute the command as root for this action.")
            sys.exit(1)

        clean_all()

    else:

        print("Invalid arguments.")
def _abort_if_service_inactive():
    if not is_daemon_active():
        print("ERROR : the optimus-manager service is not running. Aborting.")
        sys.exit(0)
def main():

    # Arguments parsing
    parser = argparse.ArgumentParser(
        description="Client program for the Optimus Manager tool.\n"
        "https://github.com/Askannz/optimus-manager")
    parser.add_argument('-v',
                        '--version',
                        action='store_true',
                        help='Print version and exit.')
    parser.add_argument('--print-mode',
                        action='store_true',
                        help="Print the current mode.")
    parser.add_argument(
        '--switch',
        metavar='MODE',
        action='store',
        help="Set the GPU mode to MODE and restart the display manager. "
        "Possible modes : intel, nvidia, auto (checks the current mode and switch to the other). "
        "WARNING : All your applications will close ! Be sure to save your work."
    )
    parser.add_argument(
        '--set-startup',
        metavar='STARTUP_MODE',
        action='store',
        help="Set the startup mode to STARTUP_MODE. Possible modes : "
        "intel, nvidia, nvidia_once (starts with Nvidia and reverts to Intel for the next boot)"
    )
    parser.add_argument('--print-startup',
                        action='store_true',
                        help="Print the current startup mode.")
    parser.add_argument(
        '--no-confirm',
        action='store_true',
        help="Do not ask for confirmation before switching GPUs.")
    parser.add_argument(
        '--cleanup',
        action='store_true',
        help=
        "Remove auto-generated configuration files left over by the daemon.")
    args = parser.parse_args()

    # Config loading
    if not args.version:
        try:
            config = load_config()
        except ConfigError as e:
            print("Error loading config file : %s" % str(e))
            sys.exit(1)

    #
    # Arguments switch

    if args.version:
        print("Optimus Manager (Client) version %s" % envs.VERSION)
        sys.exit(0)

    elif args.print_mode:

        try:
            mode = checks.read_gpu_mode()
        except checks.CheckError as e:
            print("Error reading mode : %s" % str(e))
            sys.exit(1)

        print("Current mode : %s" % mode)

    elif args.print_startup:

        try:
            startup_mode = var.read_startup_mode()
        except var.VarError as e:
            print("Error reading startup mode : %s" % str(e))
            sys.exit(1)

        print("Current startup mode : %s" % startup_mode)

    elif args.switch:

        if args.switch not in ["auto", "intel", "nvidia"]:
            print("Invalid mode : %s" % args.switch)
            sys.exit(1)

        if not checks.is_daemon_active():
            print(
                "The optimus-manager service is not running. Please enable and start it with :\n\n"
                "sudo systemctl enable optimus-manager\n"
                "sudo systemctl start optimus-manager\n")
            sys.exit(1)

        if args.switch == "auto":
            try:
                gpu_mode = checks.read_gpu_mode()
            except checks.CheckError as e:
                print("Error reading mode: %s" % str(e))
                sys.exit(1)

            if gpu_mode == "nvidia":
                switch_mode = "intel"
            else:
                switch_mode = "nvidia"
            print("Switching to : %s" % switch_mode)

        else:
            switch_mode = args.switch

        # Printing warnings if something is wrong
        if config["optimus"][
                "switching"] == "bbswitch" and not checks.is_module_available(
                    "bbswitch"):
            print(
                "WARNING : bbswitch is enabled in the configuration file but the bbswitch module does"
                " not seem to be available for the current kernel. Power switching will not work.\n"
                "You can install bbswitch for the default kernel with \"sudo pacman -S bbswitch\" or"
                " for all kernels with \"sudo pacman -S bbswitch-dkms\".\n")

        if switch_mode == "nvidia" and not checks.is_module_available(
                "nvidia"):
            print(
                "WARNING : the nvidia module does not seem to be available for the current kernel."
                " It is likely the Nvidia driver was not properly installed. GPU switching will probably fail,"
                " continue anyway ? (y/N)")
            ans = input("> ").lower()

            if ans == "y":
                pass
            elif ans == "n" or ans == "N":
                print("Aborting.")
                sys.exit(0)
            else:
                print("Invalid choice. Aborting")
                sys.exit(0)

        if args.no_confirm:
            send_command(switch_mode)
        else:
            print(
                "You are about to switch GPUs. This will restart the display manager and all your applications WILL CLOSE.\n"
                "(you can pass the --no-confirm option to disable this warning)\n"
                "Continue ? (y/N)")
            ans = input("> ").lower()

            if ans == "y":
                send_command(switch_mode)
            elif ans == "n" or ans == "N":
                print("Aborting.")
                sys.exit(0)
            else:
                print("Invalid choice. Aborting")
                sys.exit(0)

    elif args.set_startup:

        if args.set_startup not in ["intel", "nvidia", "nvidia_once"]:
            print("Invalid startup mode : %s" % args.set_startup)
            sys.exit(1)

        send_command("startup_" + args.set_startup)

    elif args.cleanup:

        if os.geteuid() != 0:
            print("You need to execute the command as root for this action.")
            sys.exit(1)

        clean_autogenerated()
        var.remove_startup_mode_var()
        var.remove_requested_mode_var()

    else:

        print("Invalid arguments.")