This commit is contained in:
sezanzeb 2020-11-29 01:53:12 +01:00
parent c87606cfb6
commit ba7f282dff
2 changed files with 55 additions and 31 deletions

View File

@ -22,6 +22,8 @@
"""Starts injecting keycodes based on the configuration.""" """Starts injecting keycodes based on the configuration."""
import subprocess
import dbus import dbus
from dbus import service from dbus import service
import dbus.mainloop.glib import dbus.mainloop.glib
@ -32,8 +34,24 @@ from keymapper.mapping import Mapping
from keymapper.config import config from keymapper.config import config
def is_service_running():
"""Check if the daemon is running."""
try:
subprocess.check_output(['pgrep', '-f', 'key-mapper-service'])
except subprocess.CalledProcessError:
return False
return True
def get_dbus_interface(): def get_dbus_interface():
"""Get an interface to start and stop injecting keystrokes.""" """Get an interface to start and stop injecting keystrokes."""
if not is_service_running():
logger.warning(
'The daemon "key-mapper-service" is not running, mapping keys '
'only works as long as the window is open.'
)
return Daemon(autoload=False)
try: try:
bus = dbus.SessionBus() bus = dbus.SessionBus()
remote_object = bus.get_object('keymapper.Control', '/') remote_object = bus.get_object('keymapper.Control', '/')
@ -45,7 +63,7 @@ def get_dbus_interface():
'keys only works as long as the window is open.' 'keys only works as long as the window is open.'
) )
logger.debug(error) logger.debug(error)
return Daemon() return Daemon(autoload=False)
return interface return interface

View File

@ -72,7 +72,7 @@ class _Macro:
async def run(self): async def run(self):
"""Run the macro.""" """Run the macro."""
for i, (_, task) in enumerate(self.tasks): for _, task in self.tasks:
coroutine = task() coroutine = task()
if asyncio.iscoroutine(coroutine): if asyncio.iscoroutine(coroutine):
await coroutine await coroutine
@ -168,6 +168,30 @@ def _extract_params(inner):
return params return params
def _count_brackets(macro):
"""Find where the first opening bracket closes."""
brackets = 0
position = 0
for char in macro:
position += 1
if char == '(':
brackets += 1
continue
if char == ')':
brackets -= 1
if brackets < 0:
raise Exception(f'There is one ")" too much at {position}')
if brackets == 0:
# the closing bracket of the call
break
if brackets != 0:
raise Exception(f'There are {brackets} closing brackets missing')
return brackets, position
def _parse_recurse(macro, handler, macro_instance=None, depth=0): def _parse_recurse(macro, handler, macro_instance=None, depth=0):
"""Handle a subset of the macro, e.g. one parameter or function call. """Handle a subset of the macro, e.g. one parameter or function call.
@ -215,25 +239,7 @@ def _parse_recurse(macro, handler, macro_instance=None, depth=0):
raise Exception(f'Unknown function {call}') raise Exception(f'Unknown function {call}')
# get all the stuff inbetween # get all the stuff inbetween
brackets = 0 brackets, position = _count_brackets(macro)
position = 0
for char in macro:
position += 1
if char == '(':
brackets += 1
continue
if char == ')':
brackets -= 1
if brackets < 0:
raise Exception(f'There is one ")" too much at {position}')
if brackets == 0:
# the closing bracket of the call
break
if brackets != 0:
raise Exception(f'There are {brackets} closing brackets missing')
inner = macro[2:position - 1] inner = macro[2:position - 1]
@ -256,7 +262,7 @@ def _parse_recurse(macro, handler, macro_instance=None, depth=0):
_parse_recurse(chain, handler, macro_instance, depth) _parse_recurse(chain, handler, macro_instance, depth)
return macro_instance return macro_instance
else:
# probably a parameter for an outer function # probably a parameter for an outer function
try: try:
macro = int(macro) macro = int(macro)
@ -286,6 +292,6 @@ def parse(macro, handler):
logger.spam('preparing macro %s for later execution', macro) logger.spam('preparing macro %s for later execution', macro)
try: try:
return _parse_recurse(macro, handler) return _parse_recurse(macro, handler)
except Exception as e: except Exception as error:
logger.error('Failed to parse macro "%s": %s', macro, e) logger.error('Failed to parse macro "%s": %s', macro, error)
return None return None