Optimize smart pause in wayland feature

This commit is contained in:
Gobinath 2019-07-13 22:08:15 -04:00
parent d4e732a7f0
commit 098f320aa1
6 changed files with 69 additions and 25 deletions

View File

@ -254,7 +254,8 @@ class PluginManager(object):
logging.error('plugin.py not found for the plugin: %s', plugin['id'])
return
# Look for config.json
plugin_config_path = os.path.join(plugin_dir, plugin['id'], 'config.json')
plugin_path = os.path.join(plugin_dir, plugin['id'])
plugin_config_path = os.path.join(plugin_path, 'config.json')
if not os.path.isfile(plugin_config_path):
logging.error('config.json not found for the plugin: %s', plugin['id'])
return
@ -291,7 +292,7 @@ class PluginManager(object):
else:
# This is the first time to load the plugin
# Check for dependencies
if Utility.check_plugin_dependencies(plugin_config):
if Utility.check_plugin_dependencies(plugin['id'], plugin_config, plugin_path):
return
# Load the plugin module

View File

@ -62,6 +62,7 @@ class SafeEyes(object):
# Initialize the Safe Eyes Context
self.context['version'] = SAFE_EYES_VERSION
self.context['desktop'] = Utility.desktop_environment()
self.context['is_wayland'] = Utility.is_wayland()
self.context['locale'] = system_locale
self.context['api'] = {}
self.context['api']['show_settings'] = lambda: Utility.execute_main_thread(

View File

@ -22,10 +22,12 @@ This module contains utility functions for Safe Eyes and its plugins.
import errno
import imp
import importlib
import json
import locale
import logging
import os
import re
import shutil
import subprocess
import threading
@ -57,6 +59,7 @@ SYSTEM_PLUGINS_DIR = os.path.join(BIN_DIRECTORY, 'plugins')
USER_PLUGINS_DIR = os.path.join(CONFIG_DIRECTORY, 'plugins')
LOCALE_PATH = os.path.join(BIN_DIRECTORY, 'config/locale')
DESKTOP_ENVIRONMENT = None
IS_WAYLAND = False
def get_resource_path(resource_name):
@ -167,7 +170,7 @@ def delete(file_path):
pass
def check_plugin_dependencies(plugin_config):
def check_plugin_dependencies(plugin_id, plugin_config, plugin_path):
"""
Check the plugin dependencies.
"""
@ -192,6 +195,12 @@ def check_plugin_dependencies(plugin_config):
if get_resource_path(resource) is None:
return _('Please add the resource %(resource)s to %(config_resource)s directory') % {'resource': resource, 'config_resource': CONFIG_RESOURCE}
plugin_dependency_checker = os.path.join(plugin_path, 'dependency_checker.py')
if os.path.isfile(plugin_dependency_checker):
dependency_checker = importlib.import_module((plugin_id + '.dependency_checker'))
if dependency_checker and hasattr(dependency_checker, "validate"):
return dependency_checker.validate(plugin_config)
return None
@ -218,7 +227,7 @@ def load_plugins_config(safeeyes_config):
config = load_json(plugin_config_path)
if config is None:
continue
dependency_description = check_plugin_dependencies(config)
dependency_description = check_plugin_dependencies(plugin['id'], config, plugin_path)
if dependency_description:
plugin['enabled'] = False
config['error'] = True
@ -260,6 +269,24 @@ def desktop_environment():
DESKTOP_ENVIRONMENT = env
return env
def is_wayland():
"""
Determine if Wayland is running
https://unix.stackexchange.com/a/325972/222290
"""
global IS_WAYLAND
try:
session_id = subprocess.check_output(['loginctl']).split(b'\n')[1].split()[0]
output = subprocess.check_output(
['loginctl', 'show-session', session_id, '-p', 'Type']
)
except BaseException:
logging.warning('Unable to determine if wayland is running. Assuming no.')
IS_WAYLAND = False
else:
IS_WAYLAND = bool(re.search(b'wayland', output, re.IGNORECASE))
return IS_WAYLAND
def execute_command(command, args=[]):
"""

View File

@ -6,7 +6,7 @@
},
"dependencies": {
"python_modules": [],
"shell_commands": ["xprintidle"],
"shell_commands": [],
"operating_systems": [],
"desktop_environments": [],
"resources": []

View File

@ -0,0 +1,31 @@
# Safe Eyes is a utility to remind you to take break frequently
# to protect your eyes from eye strain.
# Copyright (C) 2017 Gobinath
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from safeeyes import Utility
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

View File

@ -44,25 +44,7 @@ next_break_duration = 0
break_interval = 0
waiting_time = 2
interpret_idle_as_break = False
def is_wayland():
"""
Determine if Wayland is running
https://unix.stackexchange.com/a/325972/222290
"""
try:
output = subprocess.check_output(['loginctl'])
second_line = output.split(b'\n')[1]
session_id = re.search(rb'^ *?(\d+)', second_line).group(1)
output = subprocess.check_output(
['loginctl', 'show-session', session_id, '-p', 'Type']
)
except BaseException:
logging.info('Unable to determine if wayland is running. Assuming no.')
return False
else:
return bool(re.search(b'wayland', output, re.IGNORECASE))
is_wayland_and_gnome = False
def __gnome_wayland_idle_time():
@ -92,7 +74,7 @@ def __system_idle_time():
Return the idle time if xprintidle is available, otherwise return 0.
"""
try:
if is_wayland() and Utility.DESKTOP_ENVIRONMENT == 'gnome':
if is_wayland_and_gnome:
return __gnome_wayland_idle_time()
# Convert to seconds
return int(subprocess.check_output(['xprintidle']).decode('utf-8')) / 1000
@ -132,6 +114,7 @@ def init(ctx, safeeyes_config, plugin_config):
global waiting_time
global interpret_idle_as_break
global postpone_if_active
global is_wayland_and_gnome
logging.debug('Initialize Smart Pause plugin')
context = ctx
enable_safe_eyes = context['api']['enable_safeeyes']
@ -143,6 +126,7 @@ def init(ctx, safeeyes_config, plugin_config):
break_interval = safeeyes_config.get(
'short_break_interval') * 60 # Convert to seconds
waiting_time = min(2, idle_time) # If idle time is 1 sec, wait only 1 sec
is_wayland_and_gnome = context['desktop'] == 'gnome' and context['is_wayland']
def __start_idle_monitor():