bumblebee-status/bumblebee_status/modules/contrib/prime.py

70 lines
2 KiB
Python
Raw Normal View History

2020-04-21 20:30:29 +02:00
# pylint: disable=C0111,R0903
"""Displays and changes the current selected prime video card
Left click will call 'sudo prime-select nvidia'
Right click will call 'sudo prime-select nvidia'
Running these commands without a password requires editing your sudoers file
(always use visudo, it's very easy to make a mistake and get locked out of your computer!)
sudo visudo -f /etc/sudoers.d/prime
Then put a line like this in there:
user ALL=(ALL) NOPASSWD: /usr/bin/prime-select
If you can't figure out the sudoers thing, then don't worry, it's still really useful.
Parameters:
2020-04-21 20:30:44 +02:00
* prime.nvidiastring: String to use when nvidia is selected (defaults to 'intel')
* prime.intelstring: String to use when intel is selected (defaults to 'intel')
2020-04-21 20:30:29 +02:00
2020-07-18 08:23:28 +02:00
Requires the following executables:
* sudo
2020-04-21 20:30:29 +02:00
* prime-select
contributed by `jeffeb3 <https://github.com/jeffeb3>`_ - many thanks!
2020-04-21 20:30:29 +02:00
"""
2020-04-21 20:33:34 +02:00
import core.module
import core.widget
import core.input
import util.cli
2020-04-21 20:33:34 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.query))
2020-04-21 20:33:34 +02:00
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__chooseNvidia)
core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self.__chooseIntel)
2020-04-21 20:30:29 +02:00
self.nvidiastring = self.parameter("nvidiastring", "nv")
self.intelstring = self.parameter("intelstring", "it")
2020-04-21 20:30:29 +02:00
2020-04-21 20:33:34 +02:00
def __chooseNvidia(self, event):
util.cli.execute("sudo prime-select nvidia")
2020-04-21 20:30:29 +02:00
2020-04-21 20:33:34 +02:00
def __chooseIntel(self, event):
util.cli.execute("sudo prime-select intel")
2020-04-21 20:30:29 +02:00
def query(self, widget):
try:
res = util.cli.execute("prime-select query")
2020-04-21 20:30:29 +02:00
except RuntimeError:
return "n/a"
2020-04-21 20:30:29 +02:00
for line in res.split("\n"):
if not line:
continue
if "nvidia" in line:
2020-04-21 20:30:29 +02:00
return self.nvidiastring
if "intel" in line:
2020-04-21 20:30:29 +02:00
return self.intelstring
return "n/a"
2020-04-21 20:30:29 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4