Hopefully fix some wayland keyboard stuff.

This commit is contained in:
Storm Dragon
2026-04-08 03:37:50 -04:00
parent 2d9790de88
commit eeb7bd046f
6 changed files with 124 additions and 12 deletions
+2 -2
View File
@@ -172,7 +172,8 @@ class LearnModePresenter:
and event.getHandler() is None: and event.getHandler() is None:
cthulhu_state.activeScript.phoneticSpellCurrentItem(event.event_string) cthulhu_state.activeScript.phoneticSpellCurrentItem(event.event_string)
if event.event_string == "Escape": key_name = event.keyval_name or event.event_string
if key_name == "Escape":
self.quit(script=None, event=event) self.quit(script=None, event=event)
return True return True
@@ -404,4 +405,3 @@ def getPresenter():
_presenter = LearnModePresenter(cthulhu.cthulhuApp) _presenter = LearnModePresenter(cthulhu.cthulhuApp)
return _presenter return _presenter
+12 -10
View File
@@ -23,6 +23,7 @@ class ByeCthulhu(Plugin):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
logger.info("ByeCthulhu plugin initialized") logger.info("ByeCthulhu plugin initialized")
self._signal_handler_id = None self._signal_handler_id = None
self._is_connected = False
@cthulhu_hookimpl @cthulhu_hookimpl
def activate(self, plugin=None): def activate(self, plugin=None):
@@ -33,13 +34,14 @@ class ByeCthulhu(Plugin):
logger.info("Activating ByeCthulhu plugin") logger.info("Activating ByeCthulhu plugin")
try: try:
# Connect to the stop-application-completed signal if not self._is_connected:
signal_manager = self.app.getSignalManager() signal_manager = self.app.getSignalManager()
self._signal_handler_id = signal_manager.connectSignal( self._signal_handler_id = signal_manager.connectSignal(
"stop-application-completed", "stop-application-completed",
self.process, self.process,
"default" # Add profile parameter "default" # Add profile parameter
) )
self._is_connected = True
except Exception as e: except Exception as e:
logger.error(f"Error activating ByeCthulhu plugin: {e}") logger.error(f"Error activating ByeCthulhu plugin: {e}")
@@ -52,14 +54,14 @@ class ByeCthulhu(Plugin):
logger.info("Deactivating ByeCthulhu plugin") logger.info("Deactivating ByeCthulhu plugin")
try: try:
# Disconnect signal if we have an ID if self._is_connected and self._signal_handler_id is not None:
if self._signal_handler_id is not None:
signal_manager = self.app.getSignalManager() signal_manager = self.app.getSignalManager()
# Use disconnectSignalByFunction instead since disconnectSignal doesn't exist # Use disconnectSignalByFunction instead since disconnectSignal doesn't exist
signal_manager.disconnectSignalByFunction( signal_manager.disconnectSignalByFunction(
self.process self.process
) )
self._signal_handler_id = None self._signal_handler_id = None
self._is_connected = False
except Exception as e: except Exception as e:
logger.error(f"Error deactivating ByeCthulhu plugin: {e}") logger.error(f"Error deactivating ByeCthulhu plugin: {e}")
@@ -72,4 +74,4 @@ class ByeCthulhu(Plugin):
state.activeScript.presentationInterrupt() state.activeScript.presentationInterrupt()
state.activeScript.presentMessage(messages.STOP_CTHULHU, resetStyles=False) state.activeScript.presentMessage(messages.STOP_CTHULHU, resetStyles=False)
except Exception as e: except Exception as e:
logger.error(f"Error in ByeCthulhu process: {e}") logger.error(f"Error in ByeCthulhu process: {e}")
+4
View File
@@ -597,6 +597,10 @@ class Script(script.Script):
""" Removes this script's AT-SPI key grabs. """ """ Removes this script's AT-SPI key grabs. """
msg = "DEFAULT: removing key grabs" msg = "DEFAULT: removing key grabs"
debug.printMessage(debug.LEVEL_INFO, msg, True) debug.printMessage(debug.LEVEL_INFO, msg, True)
if cthulhu_state.device is None:
self.grab_ids = []
self._modifierGrabIds = []
return
for id in self.grab_ids: for id in self.grab_ids:
cthulhu.removeKeyGrab(id) cthulhu.removeKeyGrab(id)
self.grab_ids = [] self.grab_ids = []
+30
View File
@@ -0,0 +1,30 @@
import sys
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cthulhu.plugins.ByeCthulhu.plugin import ByeCthulhu
class ByeCthulhuRegressionTests(unittest.TestCase):
def test_activate_only_connects_stop_signal_once(self):
plugin = ByeCthulhu()
signalManager = mock.Mock()
signalManager.connectSignal.return_value = 42
plugin.app = mock.Mock()
plugin.app.getSignalManager.return_value = signalManager
plugin.activate(plugin)
plugin.activate(plugin)
signalManager.connectSignal.assert_called_once_with(
"stop-application-completed",
plugin.process,
"default",
)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,32 @@
import sys
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cthulhu import cthulhu_state
from cthulhu.scripts import default
class DefaultScriptShutdownRegressionTests(unittest.TestCase):
def test_remove_key_grabs_clears_tracking_without_touching_dead_device(self):
script = object.__new__(default.Script)
script.grab_ids = [474, 475]
script._modifierGrabIds = [("Insert", 99)]
with (
mock.patch.object(cthulhu_state, "device", None),
mock.patch("cthulhu.scripts.default.cthulhu.removeKeyGrab") as removeKeyGrab,
mock.patch("cthulhu.scripts.default.input_event_manager.get_manager") as getManager,
):
script.removeKeyGrabs()
removeKeyGrab.assert_not_called()
getManager.assert_not_called()
self.assertEqual(script.grab_ids, [])
self.assertEqual(script._modifierGrabIds, [])
if __name__ == "__main__":
unittest.main()
+44
View File
@@ -0,0 +1,44 @@
import sys
import unittest
from pathlib import Path
from unittest import mock
import gi
gi.require_version("Gdk", "3.0")
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from gi.repository import Gdk
from cthulhu import cthulhu_state
from cthulhu import input_event
from cthulhu import learn_mode_presenter
class LearnModePresenterRegressionTests(unittest.TestCase):
def test_escape_keyval_exits_learn_mode_when_event_string_is_control_character(self):
presenter = learn_mode_presenter.LearnModePresenter(mock.Mock())
presenter._is_active = True
keyboardEvent = input_event.KeyboardEvent(
True,
9,
Gdk.KEY_Escape,
0,
"\x1b",
)
activeScript = mock.Mock()
with mock.patch.object(cthulhu_state, "activeScript", activeScript):
result = presenter.handle_event(keyboardEvent)
self.assertTrue(result)
self.assertFalse(presenter.is_active())
activeScript.speakKeyEvent.assert_called_once_with(keyboardEvent)
activeScript.presentMessage.assert_called_once()
if __name__ == "__main__":
unittest.main()