Skip to content

Commit

Permalink
WIP of #125: screen locking detection
Browse files Browse the repository at this point in the history
wont be able to have separate backend tho,
will be able to hack the shell script tho to do it.

currently only supports xscreensaver
  • Loading branch information
dschep committed Feb 23, 2017
1 parent 83d153a commit 1946a5f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ntfy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
except ImportError:
def is_focused():
return True
from .screensaver import is_locked


def run_cmd(args):
Expand Down Expand Up @@ -61,6 +62,8 @@ def run_cmd(args):
retcode = process.returncode
if args.longer_than is not None and duration <= args.longer_than:
return None, None
if args.locked_only and not is_locked():
return None, None
if args.unfocused_only and is_focused():
return None, None
message = _result_message(args.command if not args.hide_command else None,
Expand Down Expand Up @@ -230,6 +233,12 @@ def default_sender(args):
type=int,
metavar='N',
help="Only notify if the command runs longer than N seconds")
done_parser.add_argument(
'--locked-only',
action='store_true',
default=False,
dest='locked_only',
help='Only notify if the screen is locked')
done_parser.add_argument(
'-b',
'--background-only',
Expand Down Expand Up @@ -283,6 +292,12 @@ def default_sender(args):
type=int,
metavar='N',
help="Only notify if the command runs longer than N seconds")
shell_integration_parser.add_argument(
'--locked-only',
action='store_true',
default=False,
dest='locked_only',
help='Only notify if the screen is locked')
shell_integration_parser.add_argument(
'-f',
'--foreground-too',
Expand Down
21 changes: 21 additions & 0 deletions ntfy/screensaver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from shlex import split
from subprocess import check_output, check_call, CalledProcessError, PIPE


def xscreensaver_detect():
try:
check_call(split('pgrep xscreensaver'), stdout=PIPE)
except CalledProcessError:
return False
else:
return True


def xscreensaver_is_locked():
return 'screen locked' in check_output(split('xscreensaver-command -time'))


def is_locked():
if xscreensaver_detect():
return xscreensaver_is_locked()
return True
11 changes: 11 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_default(self, mock_Popen):
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"true" succeeded in 0:00 minutes', 0), run_cmd(args))

@patch('ntfy.cli.Popen')
Expand All @@ -36,6 +37,7 @@ def test_emoji(self, mock_Popen):
args.no_emoji = False
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual((':white_check_mark: "true" succeeded in 0:00 minutes', 0),
run_cmd(args))

Expand All @@ -55,6 +57,7 @@ def test_longerthan(self, mock_Popen):
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual((None, None), run_cmd(args))

@patch('ntfy.cli.Popen')
Expand All @@ -66,6 +69,7 @@ def test_failure(self, mock_Popen):
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"false" failed (code 42) in 0:00 minutes', 42), run_cmd(args))

@patch('ntfy.cli.Popen')
Expand All @@ -77,6 +81,7 @@ def test_stdout(self, mock_Popen):
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = True
args.stderr = False
Expand All @@ -91,6 +96,7 @@ def test_stderr(self, mock_Popen):
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = False
args.stderr = True
Expand All @@ -105,6 +111,7 @@ def test_stdout_and_stderr(self, mock_Popen):
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = True
args.stderr = True
Expand All @@ -119,6 +126,7 @@ def test_failure_stdout_and_stderr(self, mock_Popen):
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = True
args.stderr = True
Expand All @@ -143,6 +151,7 @@ def test_formatter(self):
args.longer_than = -1
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"true" succeeded in 1:05 minutes', 0), run_cmd(args))

def test_formatter_failure(self):
Expand All @@ -153,6 +162,7 @@ def test_formatter_failure(self):
args.longer_than = -1
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"false" failed (code 1) in 0:10 minutes', 1), run_cmd(args))


Expand Down Expand Up @@ -185,6 +195,7 @@ def test_watch_pid(self, mock_process):
args = MagicMock()
args.pid = 1
args.unfocused_only = False
args.locked_only = False
self.assertEqual('PID[1]: "cmd" finished in 0:00 minutes',
run_cmd(args)[0])

Expand Down

0 comments on commit 1946a5f

Please sign in to comment.