diff --git a/libqtile/utils.py b/libqtile/utils.py index c9cb407041..fb970fbb5b 100644 --- a/libqtile/utils.py +++ b/libqtile/utils.py @@ -508,14 +508,14 @@ async def add_signal_receiver( return False match_args = { - "type": "signal", "sender": bus_name, "member": signal_name, "path": path, "interface": dbus_interface, } - rule = ",".join("{}='{}'".format(k, v) for k, v in match_args.items() if v) + rule = "type='signal'," + rule += ",".join("{}='{}'".format(k, v) for k, v in match_args.items() if v) logger.debug("Adding dbus match rule: %s", rule) @@ -532,7 +532,15 @@ async def add_signal_receiver( # Check if message sent successfully if bus and msg and msg.message_type == MessageType.METHOD_RETURN: - bus.add_message_handler(callback) + + def signal_callback_wrapper(msg: Message) -> None: + """Custom wrapper to only run callback if message matches our rule.""" + if msg.message_type == MessageType.SIGNAL and msg._matches( + **{k: v for k, v in match_args.items() if v} + ): + callback(msg) + + bus.add_message_handler(signal_callback_wrapper) return True else: diff --git a/libqtile/widget/keyboardkbdd.py b/libqtile/widget/keyboardkbdd.py index a573b27d50..a6135d70fa 100644 --- a/libqtile/widget/keyboardkbdd.py +++ b/libqtile/widget/keyboardkbdd.py @@ -22,8 +22,6 @@ import re -from dbus_next.constants import MessageType - from libqtile.log_utils import logger from libqtile.utils import add_signal_receiver from libqtile.widget import base @@ -90,9 +88,6 @@ async def _config_async(self): logger.warning("Could not subscribe to kbdd signal.") def _signal_received(self, message): - if message.message_type != MessageType.SIGNAL: - return - self._layout_changed(*message.body) def _layout_changed(self, layout_changed): diff --git a/libqtile/widget/mpris2widget.py b/libqtile/widget/mpris2widget.py index 7d34fd0b33..8d7bc770f9 100644 --- a/libqtile/widget/mpris2widget.py +++ b/libqtile/widget/mpris2widget.py @@ -252,9 +252,6 @@ def _name_owner_changed(self, message): self._set_background_poll(False) def message(self, message): - if message.message_type != MessageType.SIGNAL: - return - create_task(self.process_message(message)) async def process_message(self, message): diff --git a/test/widgets/docs_screenshots/ss_keyboardkbdd.py b/test/widgets/docs_screenshots/ss_keyboardkbdd.py index ded6196d90..40dc908a12 100644 --- a/test/widgets/docs_screenshots/ss_keyboardkbdd.py +++ b/test/widgets/docs_screenshots/ss_keyboardkbdd.py @@ -17,21 +17,18 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -import sys from importlib import reload import pytest -from test.widgets.test_keyboardkbdd import Mockconstants, MockSpawn, mock_signal_receiver +from test.widgets.test_keyboardkbdd import MockSpawn, mock_signal_receiver @pytest.fixture def widget(monkeypatch): - monkeypatch.setitem(sys.modules, "dbus_next.constants", Mockconstants("dbus_next.constants")) from libqtile.widget import keyboardkbdd reload(keyboardkbdd) - monkeypatch.setattr("libqtile.widget.keyboardkbdd.MessageType", Mockconstants.MessageType) monkeypatch.setattr( "libqtile.widget.keyboardkbdd.KeyboardKbdd.call_process", MockSpawn.call_process ) diff --git a/test/widgets/test_keyboardkbdd.py b/test/widgets/test_keyboardkbdd.py index 568ee7b84d..3c7ffa82b6 100644 --- a/test/widgets/test_keyboardkbdd.py +++ b/test/widgets/test_keyboardkbdd.py @@ -25,9 +25,7 @@ # This test file covers the remaining widget code -import sys from importlib import reload -from types import ModuleType import pytest @@ -38,11 +36,6 @@ async def mock_signal_receiver(*args, **kwargs): return True -class Mockconstants(ModuleType): - class MessageType: - SIGNAL = 1 - - class MockSpawn: call_count = 0 @@ -62,13 +55,11 @@ def __init__(self, is_signal=True, body=0): @pytest.fixture def patched_widget(monkeypatch): - monkeypatch.setitem(sys.modules, "dbus_next.constants", Mockconstants("dbus_next.constants")) from libqtile.widget import keyboardkbdd reload(keyboardkbdd) # The next line shouldn't be necessary but I got occasional failures without it when testing locally - monkeypatch.setattr("libqtile.widget.keyboardkbdd.MessageType", Mockconstants.MessageType) monkeypatch.setattr( "libqtile.widget.keyboardkbdd.KeyboardKbdd.call_process", MockSpawn.call_process ) @@ -89,11 +80,6 @@ def test_keyboardkbdd_process_running(fake_qtile, patched_widget, fake_window): kbd._signal_received(message) assert kbd.keyboard == "us" - # Test non-"signal" message - message = MockMessage(body=0, is_signal=False) - kbd._signal_received(message) - assert kbd.keyboard == "us" - def test_keyboardkbdd_process_not_running(fake_qtile, patched_widget, fake_window): MockSpawn.call_count = 0