Browse Source

ExtendedControls.__init__ params: engine pid instead of Popen instance

Fabian Peter Hammerle 6 years ago
parent
commit
11565a6b6e
3 changed files with 23 additions and 7 deletions
  1. 1 1
      setup.py
  2. 1 1
      tooncher/__init__.py
  3. 21 5
      tooncher/controls.py

+ 1 - 1
setup.py

@@ -18,7 +18,7 @@ setup(
     scripts=glob.glob('scripts/*'),
     install_requires=['pyyaml'],
     extras_require={
-        'extended-controls': ['xlib'],
+        'extended-controls': ['xlib', 'psutil'],
     },
     tests_require=['pytest'],
 )

+ 1 - 1
tooncher/__init__.py

@@ -152,7 +152,7 @@ def launch(engine_path, username, password, validate_ssl_certs=True,
         if enable_extended_keyboard_controls:
             try:
                 tooncher.controls.ExtendedControls(
-                    engine_process=p,
+                    engine_pid=p.pid,
                     toggle_keysym_name=extended_keyboard_control_toggle_keysym_name,
                 ).run()
             except Exception as e:

+ 21 - 5
tooncher/controls.py

@@ -1,5 +1,9 @@
 import copy
 import time
+try:
+    import psutil
+except ImportError:
+    psutil = False
 try:
     import Xlib.display
     from Xlib import X, XK
@@ -35,7 +39,15 @@ def x_find_window_by_pid(display, pid):
 
 class ExtendedControls:
 
-    def __init__(self, engine_process, toggle_keysym_name):
+    def __init__(self, engine_pid, toggle_keysym_name):
+        if not psutil:
+            raise Exception('\n'.join([
+                'Extended keyboard controls require the python lib psutil to be installed.',
+                'Depending on your system run',
+                '\t$ sudo apt-get install python3-psutil',
+                'or',
+                '\t$ pip3 install --user psutil',
+            ]))
         if not Xlib:
             raise Exception('\n'.join([
                 'Extended keyboard controls require xlib for python to be installed.',
@@ -44,7 +56,7 @@ class ExtendedControls:
                 'or',
                 '\t$ pip3 install --user xlib',
             ]))
-        self._engine_process = engine_process
+        self._engine_pid = engine_pid
         self._xdisplay = Xlib.display.Display()
         self._toggle_keysym = XK.string_to_keysym(toggle_keysym_name)
         if self._toggle_keysym == X.NoSymbol:
@@ -61,12 +73,16 @@ class ExtendedControls:
         self._engine_window = None
         self._enabled = False
 
+    @property
+    def engine_running(self):
+        return psutil.pid_exists(self._engine_pid)
+
     def _wait_for_engine_window(self, timeout_seconds=20, search_interval_seconds=2):
         start_epoch = time.time()
-        while self._engine_process.poll() is None and (time.time() - start_epoch) <= timeout_seconds:
+        while self.engine_running and (time.time() - start_epoch) <= timeout_seconds:
             windows = x_find_window_by_pid(
                 self._xdisplay,
-                self._engine_process.pid,
+                self._engine_pid,
             )
             assert len(windows) <= 1
             if len(windows) == 1:
@@ -85,7 +101,7 @@ class ExtendedControls:
             keysym_name = XK.keysym_to_string(self._toggle_keysym)
             print("INFO Extended Controls are currently disabled."
                   + " Press key '{}' to enable.".format(keysym_name))
-        while self._engine_process.poll() is None:
+        while self.engine_running:
             # TODO don't block here, engine might have already been stopped
             self._handle_xevent(self._xdisplay.next_event())