From a3088f5c7bd464ed6c7a6773bd4508c8c29ef9cf Mon Sep 17 00:00:00 2001 From: Radek SPRTA Date: Thu, 23 Feb 2017 23:41:02 +0100 Subject: [PATCH] Do not start SafeEyes if another instance is already running --- README.md | 6 ++-- safeeyes/debian/control | 2 +- safeeyes/safeeyes/safeeyes | 68 +++++++++++++++++++++++++------------- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index b71062a..d9c8660 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,11 @@ yaourt -S safeeyes ### Other Linux: 1: Install the dependencies: - * Arch: `hicolor-icon-theme`, `libappindicator-gtk3`, `xorg-xprop`, `python2-xlib`, `python2-gobject`, `python2-dbus`, `python2-babel`, `xprintidle` and `python-pyaudio` + * Arch: `hicolor-icon-theme`, `libappindicator-gtk3`, `xorg-xprop`, `python2-xlib`, `python2-gobject`, `python2-dbus`, `python2-babel`, `xprintidle`, `python-psutil` and `python-pyaudio` - * Debian: `gir1.2-appindicator3-0.1`, `python-xlib`, `python-gobject`, `python-gi`, `python-dbus`, `gir1.2-notify-0.7`, `python-gtk2`, `python-babel`, `xprintidle` and `python-pyaudio` + * Debian: `gir1.2-appindicator3-0.1`, `python-xlib`, `python-gobject`, `python-gi`, `python-dbus`, `gir1.2-notify-0.7`, `python-gtk2`, `python-babel`, `xprintidle`, `python-psutil` and `python-pyaudio` - * Fedora 24: `libappindicator-gtk3`, `python-xlib`, `python-gobject`, `xorg-x11-utils`, `python-dbus`, `python-babel`, `xprintidle` and `python-pyaudio` + * Fedora 24: `libappindicator-gtk3`, `python-xlib`, `python-gobject`, `xorg-x11-utils`, `python-dbus`, `python-babel`, `xprintidle`, `python-psutil` and `python-pyaudio` 2: Download and extract [safeeyes.tar.gz](https://github.com/slgobinath/SafeEyes/releases/download/v1.1.8/safeeyes.tar.gz) into `/`: `sudo tar -xzvf safeeyes.tar.gz -C /` diff --git a/safeeyes/debian/control b/safeeyes/debian/control index 8b1c8a1..3b4056f 100644 --- a/safeeyes/debian/control +++ b/safeeyes/debian/control @@ -8,7 +8,7 @@ Homepage: https://github.com/slgobinath/SafeEyes/ Package: safeeyes Architecture: any -Depends: gir1.2-appindicator3-0.1, python (>= 2.7.0), python-xlib, python-gi, python-dbus, gir1.2-notify-0.7, python-gtk2, python-babel, xprintidle, python-pyaudio +Depends: gir1.2-appindicator3-0.1, python (>= 2.7.0), python-xlib, python-gi, python-dbus, gir1.2-notify-0.7, python-gtk2, python-babel, xprintidle, python-pyaudio, python-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. . diff --git a/safeeyes/safeeyes/safeeyes b/safeeyes/safeeyes/safeeyes index ce57a9a..df6b1ad 100755 --- a/safeeyes/safeeyes/safeeyes +++ b/safeeyes/safeeyes/safeeyes @@ -19,6 +19,7 @@ # along with this program. If not, see . import os, gi, json, shutil, dbus, logging, operator, Utility +import psutil from threading import Timer from dbus.mainloop.glib import DBusGMainLoop from BreakScreen import BreakScreen @@ -229,7 +230,7 @@ def validate_config(): shutil.rmtree(os.path.join(Utility.home_directory, '.config/safeeyes'), ignore_errors=False) except: pass - + # Remove startup script try: os.remove(os.path.join(Utility.home_directory, '.config/autostart/safeeyes.desktop')) @@ -254,6 +255,22 @@ def read_lang_files(): return languages +def running(): + ''' + Check if SafeEyes is already running. + ''' + for proc in psutil.process_iter(): + try: + # Check if safeeyes is in process arguments + if 'safeeyes' in proc.cmdline()[1]: + return True + + # Ignore if process does not exist or does not have command line args + except (IndexError, psutil.NoSuchProcess): + pass + return False + + def main(): initialize_config() @@ -261,28 +278,33 @@ def main(): logging.basicConfig(format='%(asctime)s [%(levelname)s]:[%(threadName)s] %(message)s', filename=log_file_path, filemode='w', level=logging.INFO) logging.info("Starting Safe Eyes") - validate_config() + if not running(): + validate_config() - global break_screen - global core - global notification - global tray_icon - global language - - language_file_path = os.path.join(system_language_directory, str(config['language']) + '.json') - with open(language_file_path) as language_file: - language = json.load(language_file) - - tray_icon = TrayIcon(config, language, show_settings, show_about, enable_safeeyes, disable_safeeyes, on_quit) - break_screen = BreakScreen(on_skipped, break_screen_glade, style_sheet_path) - break_screen.initialize(config, language) - core = SafeEyesCore(show_notification, show_alert, close_alert, on_countdown, tray_icon.next_break_time) - core.initialize(config, language) - core.start() - notification = Notification(language) - - handle_system_suspend() + global break_screen + global core + global notification + global tray_icon + global language - Gtk.main() + language_file_path = os.path.join(system_language_directory, str(config['language']) + '.json') + with open(language_file_path) as language_file: + language = json.load(language_file) -main() \ No newline at end of file + tray_icon = TrayIcon(config, language, show_settings, show_about, enable_safeeyes, disable_safeeyes, on_quit) + break_screen = BreakScreen(on_skipped, break_screen_glade, style_sheet_path) + break_screen.initialize(config, language) + core = SafeEyesCore(show_notification, show_alert, close_alert, on_countdown, tray_icon.next_break_time) + core.initialize(config, language) + core.start() + notification = Notification(language) + + handle_system_suspend() + + Gtk.main() + else: + logging.info('SafeEyes is already running') + sys.exit(0) + +if __name__ == '__main__': + main()