Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smartpause: use X API instead of xprintidle subprocess #358

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Homepage: https://github.com/slgobinath/SafeEyes/

Package: safeeyes
Architecture: all
Depends: ${misc:Depends}, ${python3:Depends}, gir1.2-appindicator3-0.1, python3 (>= 3.4.0), python3-xlib, python3-dbus, gir1.2-notify-0.7, python3-babel, x11-utils, xprintidle, alsa-utils, python3-psutil
Depends: ${misc:Depends}, ${python3:Depends}, gir1.2-appindicator3-0.1, python3 (>= 3.4.0), python3-xlib, python3-dbus, gir1.2-notify-0.7, python3-babel, x11-utils, python3-xcffib, alsa-utils, python3-psutil
Description: Safe Eyes
Safe Eyes is a simple tool to remind you to take periodic breaks for your eyes. This is essential for anyone spending more time on the computer to avoid eye strain and other physical problems.
.
Expand Down
11 changes: 4 additions & 7 deletions safeeyes/plugins/smartpause/dependency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@


def validate(plugin_config):
command = None
if Utility.DESKTOP_ENVIRONMENT == "gnome" and Utility.IS_WAYLAND:
command = "dbus-send"
else:
command = "xprintidle"
if not Utility.command_exist(command):
return _("Please install the command-line tool '%s'") % command
else:
return None
if not Utility.command_exist(command):
return _("Please install the command-line tool '%s'") % command

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try to make the xcffib dependency optional, by putting an import check here?
Or keep it simple and not have maybe-dependencies for bundled components?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can Smart Pause plugin work without the xcffib library? If it cannot please add a check here. This check will only disable the plugin (not Safe Eyes) if the dependency is missing.

return None
35 changes: 30 additions & 5 deletions safeeyes/plugins/smartpause/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import threading
import re
import os
from typing import Optional

import xcffib
import xcffib.xproto
import xcffib.screensaver

from safeeyes import Utility
from safeeyes.model import State
Expand All @@ -45,6 +50,7 @@
waiting_time = 2
interpret_idle_as_break = False
is_wayland_and_gnome = False
xcb_connection: Optional[xcffib.Connection] = None


def __gnome_wayland_idle_time():
Expand All @@ -68,16 +74,29 @@ def __gnome_wayland_idle_time():
return 0


def __x11_idle_time():
assert xcb_connection is not None
try:
ext = xcb_connection(xcffib.screensaver.key)
root_window = xcb_connection.get_setup().roots[0].root
query = ext.QueryInfo(root_window)
info = query.reply()
# Convert to seconds
return info.ms_since_user_input / 1000
except Exception:
logging.warning("Failed to get system idle time from XScreenSaver API", exc_info=True)
return 0


def __system_idle_time():
"""
Get system idle time in minutes.
Return the idle time if xprintidle is available, otherwise return 0.
Get system idle time in seconds.
"""
try:
if is_wayland_and_gnome:
return __gnome_wayland_idle_time()
# Convert to seconds
return int(subprocess.check_output(['xprintidle']).decode('utf-8')) / 1000
else:
return __x11_idle_time()
except BaseException:
return 0

Expand Down Expand Up @@ -115,6 +134,7 @@ def init(ctx, safeeyes_config, plugin_config):
global interpret_idle_as_break
global postpone_if_active
global is_wayland_and_gnome
global xcb_connection
logging.debug('Initialize Smart Pause plugin')
context = ctx
enable_safe_eyes = context['api']['enable_safeeyes']
Expand Down Expand Up @@ -171,7 +191,8 @@ def on_start():
"""
Start a thread to continuously call xprintidle.
"""
global active
global xcb_connection
xcb_connection = xcffib.connect()
if not __is_active():
# If SmartPause is already started, do not start it again
logging.debug('Start Smart Pause plugin')
Expand All @@ -185,6 +206,10 @@ def on_stop():
"""
global active
global smart_pause_activated
global xcb_connection
if xcb_connection is not None:
xcb_connection.disconnect()
xcb_connection = None
if smart_pause_activated:
# Safe Eyes is stopped due to system idle
smart_pause_activated = False
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'babel',
'psutil',
'PyGObject',
'python-xlib'
'python-xlib',
'xcffib'
]

_ROOT = os.path.abspath(os.path.dirname(__file__))
Expand Down