Skip to content

Commit

Permalink
FIX: Linux - avoid segfaults (#10)
Browse files Browse the repository at this point in the history
* FIX: Avoid segfaults

* FIX: try/except and run
  • Loading branch information
larsoner authored Apr 26, 2021
1 parent 3ab67b1 commit 09a292a
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions darkdetect/_linux_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
# Distributed under the terms of the 3-clause BSD License.
#-----------------------------------------------------------------------------

from ctypes import util, cdll, c_char_p, byref
import subprocess


def theme():
# Here we just triage to GTK settings for now
try:
gtklib = cdll.LoadLibrary(util.find_library('gtk-3'))
gtklib.gtk_init(None, None)
settings = gtklib.gtk_settings_get_default()
res = c_char_p()
gtklib.g_object_get(settings, b"gtk-theme-name", byref(res), 0)
theme = res.value.decode()
out = subprocess.run(
['gsettings', 'get', 'org.gnome.desktop.interface', 'gtk-theme'],
capture_output=True)
stdout = out.stdout.decode()
except Exception:
return 'Light'
# we have a string, now remove start and end quote
theme = stdout.lower().strip()[1:-1]
if theme.endswith('-dark'):
return 'Dark'
else:
if theme.endswith('-dark'):
return 'Dark'
else:
return 'Light'
return 'Light'

def isDark():
return theme() == 'Dark'
Expand Down

0 comments on commit 09a292a

Please sign in to comment.