Last active
August 7, 2023 14:26
-
-
Save Jan200101/3424c20ea616f7b180e1724c602f1888 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
TEST_DATA = False | |
IGPU_PCI_PREFIX = r"pci-0000_00" | |
import sys | |
from traceback import print_exc | |
import re | |
from enum import Enum | |
import dbus | |
class GPU_TYPE(Enum): | |
INTEGRATED = 1 | |
DEDICATED = 2 | |
def atd(a): | |
return {k:a[a.index(k)+1] for k in a[::2]} | |
def get_gpu_type(e) -> GPU_TYPE: | |
name = e["Name"] | |
# Nvidia has no iGPU variant, so we don't need to check | |
if "nvidia" in name.lower(): | |
return GPU_TYPE.DEDICATED | |
env = atd(e["Environment"]) | |
dri_prime = env["DRI_PRIME"] | |
if dri_prime.startswith(IGPU_PCI_PREFIX): | |
return GPU_TYPE.INTEGRATED | |
return GPU_TYPE.DEDICATED | |
def get_gpu_name(e): | |
if e: | |
return e.get("Name") | |
return None | |
def get_switcheroo_reply(): | |
bus = dbus.SystemBus() | |
remote_object = bus.get_object( | |
"net.hadess.SwitcherooControl", | |
"/net/hadess/SwitcherooControl" | |
) | |
dbus_properties = dbus.Interface(remote_object, "org.freedesktop.DBus.Properties") | |
return dbus_properties.Get("net.hadess.SwitcherooControl", "GPUs") | |
def get_fake_reply(): | |
return [ | |
{ | |
"Name": "Intel Corporation TigerLake-LP GT2 [Iris Xe Graphics]", | |
"Environment": ["DRI_PRIME", "pci-0000_00_02_0"], | |
"Default": False | |
}, | |
{ | |
"Name": "NVIDIA Corporation GA107M [GeForce RTX 3050 Mobile]", | |
"Environment": ["__GLX_VENDOR_LIBRARY_NAME", "nvidia", "__NV_PRIME_RENDER_OFFLOAD", "1", "__VK_LAYER_NV_optimus", "NVIDIA_only"], | |
"Default": True | |
}, | |
] | |
def get_reply(): | |
return get_fake_reply() if TEST_DATA else get_switcheroo_reply() | |
def get_dedicated_gpu(): | |
reply = get_reply() | |
# Try to find default dGPU | |
for e in reply: | |
if not e["Default"]: | |
continue | |
if get_gpu_type(e) == GPU_TYPE.DEDICATED: | |
return e | |
# If no default was found use the first one found | |
for e in reply: | |
if get_gpu_type(e) == GPU_TYPE.DEDICATED: | |
return e | |
return None | |
def get_integrated_gpu(): | |
reply = get_reply() | |
# Try to find default iGPU | |
for e in reply: | |
if not e["Default"]: | |
continue | |
if get_gpu_type(e) == GPU_TYPE.INTEGRATED: | |
return e | |
# If no default was found use the first one found | |
for e in reply: | |
if get_gpu_type(e) == GPU_TYPE.INTEGRATED: | |
return e | |
return None | |
def get_default_gpu(): | |
reply = get_reply() | |
for e in reply: | |
if e["Default"]: | |
return e | |
return None | |
def get_first_non_default_gpu(): | |
reply = get_reply() | |
for e in reply: | |
name = e["Name"] | |
if not e["Default"]: | |
return e | |
return None | |
def main(): | |
try: | |
ded_gpu = get_gpu_name(get_dedicated_gpu()) | |
int_gpu = get_gpu_name(get_integrated_gpu()) | |
def_gpu = get_gpu_name(get_default_gpu()) | |
non_def_gpu = get_gpu_name(get_first_non_default_gpu()) | |
print("Dedicated GPU: {}\n".format(ded_gpu)) | |
print("Integrated GPU: {}\n".format(int_gpu)) | |
print("Default GPU: {}\n(This is what GNOME would use when you launch with integrated GPU)\n".format(def_gpu)) | |
print("First Non Default GPU: {}\n(This is what GNOME would use when you launch with dedicated GPU)\n".format(non_def_gpu)) | |
if ded_gpu and non_def_gpu and ded_gpu != non_def_gpu: | |
print("!!! Detected dedicated GPU is not the first non default !!!") | |
except dbus.exceptions.DBusException: | |
print("switcheroo-control is not installed or not running") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment