Skip to content

Instantly share code, notes, and snippets.

@JoeBlakeB
Last active June 8, 2023 17:58
Show Gist options
  • Save JoeBlakeB/bb1c1501a85a7029981baccaade2041c to your computer and use it in GitHub Desktop.
Save JoeBlakeB/bb1c1501a85a7029981baccaade2041c to your computer and use it in GitHub Desktop.
Notification to switch between bumblebee and optimus-manager.
#!/usr/bin/env python
#
# Copyright (C) 2022 Joe Baker (JoeBlakeB)
# This program is free software under the GPLv3 license.
#
# Notification to switch between bumblebee and optimus-manager.
#
# Requires python3, bumblebee, optimus-manger, and notify-send
#
# This file only works if you have something in the /etc/sudoers file like:
# user host = NOPASSWD: /usr/bin/systemctl enable bumblebeed.service
# user host = NOPASSWD: /usr/bin/systemctl disable bumblebeed.service
# user host = NOPASSWD: /usr/bin/systemctl enable optimus-manager.service
# user host = NOPASSWD: /usr/bin/systemctl disable optimus-manager.service
import subprocess
import sys
def notify(message, option1, option2):
try:
return int(subprocess.check_output(["notify-send", "-i", "nvidia",
"-A", option1, "-A", option2, "-t", "0",
"-a", "Bumblebee Optimus Switcher", message]))
except ValueError: # Notification was closed
sys.exit(0)
# enable and disable services
def enableService(option):
for a, b in zip(("enable", "disable"),
("optimus-manager.service", "bumblebeed.service")[::(-1+2*option)]):
subprocess.call(["/usr/bin/sudo", "/usr/bin/systemctl", a, b])
# Get the current state of the services
bumblebeeEnabled = 0 == subprocess.call(
["/usr/bin/systemctl", "is-enabled", "bumblebeed.service"])
bumblebeeActive = 0 == subprocess.call(
["/usr/bin/systemctl", "is-active", "bumblebeed.service"])
optimusEnabled = 0 == subprocess.call(
["/usr/bin/systemctl", "is-enabled", "optimus-manager.service"])
optimusActive = 0 == subprocess.call(
["/usr/bin/systemctl", "is-active", "optimus-manager.service"])
if bumblebeeEnabled + bumblebeeActive + optimusEnabled + optimusActive != 2:
# There is a problem with the services
enableService(notify("What service do you want to enable?",
"Bumblebee", "Optimus-Manager"))
elif bumblebeeEnabled + bumblebeeActive == 2 or optimusEnabled + optimusActive == 2:
# A service is enabled and running
enabled = ("Bumblebee"*bumblebeeEnabled) + ("Optimus-Manager"*optimusEnabled)
switchTo = ("Optimus-Manager"*bumblebeeEnabled) + ("Bumblebee"*optimusEnabled)
reboot = bool(notify(enabled + " is currently enabled and running.",
"Enable " + switchTo, "Switch and reboot"))
# Will have exited if user didnt chose an option
enableService(int(bumblebeeEnabled))
if reboot:
subprocess.call(["reboot"])
else:
# A service is enabled but the other is running
enabled = ("Bumblebee"*bumblebeeEnabled) + ("Optimus-Manager"*optimusEnabled)
active = ("Bumblebee"*bumblebeeActive) + ("Optimus-Manager"*optimusActive)
reboot = bool(notify(enabled + " is currently enabled but " + active +
" is running.",
"Enable " + active, "Reboot, run " + enabled))
if reboot:
subprocess.call(["reboot"])
else:
pass # enable the one thats active
enableService(int(bumblebeeEnabled))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment