aboutsummaryrefslogtreecommitdiff
path: root/macropad.pyw
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2025-05-06 19:33:30 +0200
committerSébastien Dailly <sebastien@dailly.me>2025-05-06 19:33:30 +0200
commit4bf1c1ffe795a1d28a543a20cc3771a30a090b89 (patch)
tree09e1bb81744be02fd3a8422f9002ec3139f54100 /macropad.pyw
parent0d037fd1170ecc30cea677f563dfb50dff7360c6 (diff)
Added a dedicated thread for handling events sent to the endpoint
Diffstat (limited to 'macropad.pyw')
-rwxr-xr-xmacropad.pyw64
1 files changed, 11 insertions, 53 deletions
diff --git a/macropad.pyw b/macropad.pyw
index f73cd1c..c7e8770 100755
--- a/macropad.pyw
+++ b/macropad.pyw
@@ -14,7 +14,7 @@ import sys
from zope import component
import interfaces
-from interfaces import endpoint
+from interfaces import endpoint, configuration
from interfaces.message import IMessage, Debug
import configparser
@@ -36,6 +36,7 @@ from queue import Queue
q = Queue()
component.provideAdapter(interfaces.endpoint.EndPoint)
+component.provideUtility(mapping, interfaces.configuration.IConfiguration)
#
# Guess the platform and the load the corresponding event listener
@@ -46,28 +47,26 @@ component.provideAdapter(interfaces.endpoint.EndPoint)
#
if config.has_section("connection.serial"):
-
from serial_conn import SerialConnection
endpoint = component.queryAdapter(SerialConnection(config["connection.serial"]), endpoint.IEndpoint)
- endpoint.queue = q
- endpoint.connect()
- component.provideUtility(endpoint, interfaces.endpoint.IEndpoint)
elif config.has_section("connection.socket"):
-
from socket_conn import SocketConnection
endpoint = component.queryAdapter(SocketConnection(config["connection.socket"]), endpoint.IEndpoint)
- endpoint.queue = q
- component.provideUtility(endpoint, interfaces.endpoint.IEndpoint)
- endpoint.connect()
+
+component.provideUtility(endpoint, interfaces.endpoint.IEndpoint)
+endpoint.connect()
if config.has_section("socket.serve"):
import socketserver
- server = socketserver.Handler(config["socket.serve"], q)
+ server = socketserver.Handler(config["socket.serve"])
else:
server = None
+import consumer
+handler = consumer.SocketMessageConsumer()
+handler.start()
class Icon(object):
@@ -147,10 +146,10 @@ class Application(object):
component.handle(Debug(platform))
if platform == "win32":
import win32
- listener = win32.Listener(mapping, q)
+ listener = win32.Listener(mapping)
elif platform == 'linux':
import xlib
- listener = xlib.Listener(mapping, q)
+ listener = xlib.Listener(mapping)
component.handle(Debug("Starting xlib"))
component.provideUtility(listener, interfaces.desktopEvent.IDesktop)
listener.start()
@@ -202,46 +201,10 @@ class Application(object):
except Exception as e:
print(e)
-
- def send(self, data: str):
- """ Send the configuration to the device.
- The configuration can be either
- - a dictionnary, and will be send as is
- - a string, and will be load in a file
- If the content is the same, ignore the message and return.
- """
- if data == self.last_layout:
- return
- # Merge the new layout with the previous one, ignoring all the null.
- self.last_layout = data
-
- conn = component.queryUtility(interfaces.endpoint.IEndpoint)
- if isinstance(data, dict):
- conn.send(data)
- elif isinstance(data, str):
- layer = mapping.get(data, None)
- if layer is not None:
- conn.send(layer)
-
- def associate(self, layout: Dict, name: str):
- mapping[name] = layout
- for key in layout.keys():
- component.handle(Debug("Associating %s with %s" % (name, key)))
-
def exec(self):
try:
self.update()
if server is not None: server.update()
- conn = component.queryUtility(interfaces.endpoint.IEndpoint)
- if not conn.isConnected():
- component.handle(Debug("Reconnecting…"))
-
- conn.state = conn.STATE_CONNECTING
- self.window.after(1000, conn.connect)
- else:
- # Check if we have something to read from the server, and by
- # the by if the server is still active.
- conn.fetch()
except BaseException as e:
component.handle(Debug( str(e) ))
print(e)
@@ -251,11 +214,6 @@ class Application(object):
if app.running:
self.window.after(200, self.exec)
- while not q.empty():
- last_layout, app_ = q.get(False)
- self.send(last_layout)
- if app_ is not None: self.associate(last_layout, app_)
-
if __name__ == '__main__':
# Start the main application, Initializing the message listener before
# listening desktop events