Repository: pwr-Solaar/Solaar

Issue:
Autodetect or option to disable XTEST extension
Some users like myself may not have the XTEST X11 extension enabled for one reason or another. A command line option or auto-detection of whether the extension is enabled or not would be nice. As it is now, I have to exit X11 then enable the extension in xorg.conf, then start X11, runt solaar to do whatever, then exit X11, disable XTEST again, then start X11 again. I realize that some functionality would be lost without the extension, but previous versions of solaar didn't have this requirement and worked fine for me.


Discussion:
That's a good idea.  

What happens if you run Solaar without the extension?
On Sun, Jan 16, 2022 at 04:36:50PM -0800, Peter F. Patel-Schneider wrote:
> That's a good idea.  
> 
> What happens if you run Solaar without the extension?

solaar: error: Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/Xlib/display.py", line 224, in __getattr__
    function = self.display_extension_methods[attr]
KeyError: 'record_create_context'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/share/solaar/lib/solaar/gtk.py", line 151, in main
    import solaar.ui as ui
  File "/usr/share/solaar/lib/solaar/ui/__init__.py", line 99, in <module>
    from . import notify, tray, window  # isort:skip  # noqa: E402
  File "/usr/share/solaar/lib/solaar/ui/tray.py", line 35, in <module>
    from .window import popup as _window_popup
  File "/usr/share/solaar/lib/solaar/ui/window.py", line 37, in <module>
    from .diversion_rules import show_window as _show_diversion_window
  File "/usr/share/solaar/lib/solaar/ui/diversion_rules.py", line 25, in <module>
    from logitech_receiver import diversion as _DIV
  File "/usr/share/solaar/lib/logitech_receiver/diversion.py", line 67, in <module>
    context = display.record_create_context(
  File "/usr/lib/python3/dist-packages/Xlib/display.py", line 227, in __getattr__
    raise AttributeError(attr)
AttributeError: record_create_context

Thanks.  I didn't realize that that code needed xtest enabled.

Resolving patch:
diff --git a/lib/logitech_receiver/diversion.py b/lib/logitech_receiver/diversion.py
--- a/lib/logitech_receiver/diversion.py
+++ b/lib/logitech_receiver/diversion.py
@@ -66,19 +66,23 @@
 
 if x11:
     display = Display()
-    context = display.record_create_context(
-        0, [record.AllClients], [{
-            'core_requests': (0, 0),
-            'core_replies': (0, 0),
-            'ext_requests': (0, 0, 0, 0),
-            'ext_replies': (0, 0, 0, 0),
-            'delivered_events': (0, 0),
-            'device_events': (X.KeyPress, X.KeyRelease),
-            'errors': (0, 0),
-            'client_started': False,
-            'client_died': False,
-        }]
-    )
+    try:
+        context = display.record_create_context(
+            0, [record.AllClients], [{
+                'core_requests': (0, 0),
+                'core_replies': (0, 0),
+                'ext_requests': (0, 0, 0, 0),
+                'ext_replies': (0, 0, 0, 0),
+                'delivered_events': (0, 0),
+                'device_events': (X.KeyPress, X.KeyRelease),
+                'errors': (0, 0),
+                'client_started': False,
+                'client_died': False,
+            }]
+        )
+    except Exception:
+        _log.warn('X11 xtest not available - Modifiers and KeyPress will not work correctly', exc_info=_sys.exc_info())
+        context = None
     modifier_keycodes = display.get_modifier_mapping()
     current_key_modifiers = 0
 
@@ -104,7 +108,7 @@ def key_press_handler(reply):
             current_key_modifiers = event.state & ~(1 << mod) if mod is not None else event.state
 
 
-if x11:
+if x11 and context is not None:
     _thread.start_new_thread(display.record_enable_context, (context, key_press_handler))
 # display.record_free_context(context)  when should this be done??
 


Test patch: