-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP of #125: screen locking detection
separate backends implemented via a 'multi' meta-backend. Uses config like this: ``` backends: [multi] multi: locked: pushover: user_key: user-api-key unfocused: default: {} focused: {} ``` This config would cause no notifications if the shell is focused, desktop notifications when unfocused, and pushover notifications when the screen is locked. Freaking magic. 🎉
- Loading branch information
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from importlib import import_module | ||
try: | ||
from ..terminal import is_focused | ||
except ImportError: | ||
def is_focused(): | ||
return True | ||
from ..screensaver import is_locked | ||
|
||
|
||
def notify(title, | ||
message, | ||
locked=None, | ||
focused=None, | ||
unfocused=None, | ||
retcode=None): | ||
for condition, options in ((is_locked, locked), | ||
(is_focused, focused), | ||
(lambda: not is_focused(), unfocused)): | ||
for backend_name, backend_options in options.items(): | ||
if not condition(): | ||
continue | ||
backend = import_module('ntfy.backends.{}'.format( | ||
backend_options.get('backend', backend_name))) | ||
backend_options.pop('backend', None) | ||
backend.notify(title, message, retcode=retcode, **backend_options) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from shlex import split | ||
from subprocess import check_output, check_call, CalledProcessError, PIPE | ||
|
||
# some adapted from | ||
# https://github.com/mtorromeo/xdg-utils/blob/master/scripts/xdg-screensaver.in#L540 | ||
|
||
|
||
def xscreensaver_detect(): | ||
try: | ||
check_call(split('pgrep xscreensaver'), stdout=PIPE) | ||
except (CalledProcessError, OSError): | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
def xscreensaver_is_locked(): | ||
return 'screen locked' in check_output(split('xscreensaver-command -time')) | ||
|
||
|
||
def lightlocker_detect(): | ||
try: | ||
check_call(split('pgrep light-locker'), stdout=PIPE) | ||
except (CalledProcessError, OSError): | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
def lightlocker_is_active(): | ||
return 'The screensaver is active' in check_output(split( | ||
'light-locker-command -q')) | ||
|
||
|
||
def gnomescreensaver_detect(): | ||
try: | ||
import dbus | ||
except ImportError: | ||
return False | ||
bus = dbus.SessionBus() | ||
dbus_obj = bus.get_object('org.freedesktop.DBus', | ||
'/org/freedesktop/DBus') | ||
dbus_iface = dbus.Interface(dbus_obj, | ||
dbus_interface='org.freedesktop.DBus') | ||
try: | ||
dbus_iface.GetNameOwner('org.gnome.ScreenSaver') | ||
except dbus.DBusException as e: | ||
if e.get_dbus_name() == 'org.freedesktop.DBus.Error.NameHasNoOwner': | ||
return False | ||
else: | ||
raise e | ||
else: | ||
return True | ||
|
||
|
||
def gnomescreensaver_is_locked(): | ||
import dbus | ||
bus = dbus.SessionBus() | ||
dbus_obj = bus.get_object('org.gnome.ScreenSaver', | ||
'/org/gnome/ScreenSaver') | ||
dbus_iface = dbus.Interface(dbus_obj, | ||
dbus_interface='org.gnome.ScreenSaver') | ||
return bool(dbus_iface.GetActive()) | ||
|
||
|
||
def matescreensaver_detect(): | ||
try: | ||
import dbus | ||
except ImportError: | ||
return False | ||
bus = dbus.SessionBus() | ||
dbus_obj = bus.get_object('org.freedesktop.DBus', | ||
'/org/freedesktop/DBus') | ||
dbus_iface = dbus.Interface(dbus_obj, | ||
dbus_interface='org.freedesktop.DBus') | ||
try: | ||
dbus_iface.GetNameOwner('org.mate.ScreenSaver') | ||
except dbus.DBusException as e: | ||
if e.get_dbus_name() == 'org.freedesktop.DBus.Error.NameHasNoOwner': | ||
return False | ||
else: | ||
raise e | ||
else: | ||
return True | ||
|
||
|
||
def matescreensaver_is_locked(): | ||
import dbus | ||
bus = dbus.SessionBus() | ||
dbus_obj = bus.get_object('org.mate.ScreenSaver', | ||
'/org/mate/ScreenSaver') | ||
dbus_iface = dbus.Interface(dbus_obj, | ||
dbus_interface='org.mate.ScreenSaver') | ||
return bool(dbus_iface.GetActive()) | ||
|
||
|
||
def is_locked(): | ||
if xscreensaver_detect(): | ||
return xscreensaver_is_locked() | ||
if lightlocker_detect(): | ||
return lightlocker_is_active() | ||
if gnomescreensaver_detect(): | ||
return gnomescreensaver_is_locked() | ||
if matescreensaver_detect(): | ||
return matescreensaver_is_locked() | ||
return True | ||
This comment has been minimized.
Sorry, something went wrong. |
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
This seems suspicious. I'd expect that this should default to False, or perhaps None, but I haven't read through all the rest of the logic to see if it matters