diff --git a/bin/key-mapper-control b/bin/key-mapper-control index c3d8d351..521637c3 100755 --- a/bin/key-mapper-control +++ b/bin/key-mapper-control @@ -152,19 +152,18 @@ def internals(options): key-mapper-control should be started with sudo or pkexec for this. """ + debug = ' -d' if options.debug else '' + if options.command == HELPER: - cmd = ['key-mapper-helper'] + cmd = f'key-mapper-helper{debug}' elif options.command == START_DAEMON: - cmd = ['key-mapper-service', '--hide-info'] + cmd = f'key-mapper-service --hide-info{debug}' else: return - if options.debug: - cmd.append('-d') - - # Popen makes os.system of the main process continue with stuff while - # cmd runs in the background. - subprocess.Popen(cmd) + # daemonize + cmd = f'{cmd} &' + os.system(cmd) if __name__ == '__main__': diff --git a/keymapper/daemon.py b/keymapper/daemon.py index 2653172b..385820c5 100644 --- a/keymapper/daemon.py +++ b/keymapper/daemon.py @@ -198,9 +198,8 @@ class Daemon: # Blocks until pkexec is done asking for the password. # Runs via key-mapper-control so that auth_admin_keep works # for all pkexec calls of the gui - cmd = 'pkexec key-mapper-control --command start-daemon' - if is_debug(): - cmd += ' -d' + debug = ' -d' if is_debug() else '' + cmd = f'pkexec key-mapper-control --command start-daemon {debug}' # using pkexec will also cause the service to continue running in # the background after the gui has been closed, which will keep diff --git a/keymapper/gui/window.py b/keymapper/gui/window.py index 794c433f..9bbaa9bb 100755 --- a/keymapper/gui/window.py +++ b/keymapper/gui/window.py @@ -111,6 +111,8 @@ class Window: def __init__(self): self.dbus = None + self.start_processes() + self.selected_device = None self.selected_preset = None @@ -130,8 +132,6 @@ class Window: builder.connect_signals(self) self.builder = builder - self.start_processes() - self.confirm_delete = builder.get_object('confirm-delete') self.about = builder.get_object('about-dialog') self.about.connect('delete-event', on_close_about) @@ -169,21 +169,20 @@ class Window: self.ctrl = False self.unreleased_warn = 0 + if not is_helper_running(): + self.show_status(CTX_ERROR, 'The helper did not start') + def start_processes(self): """Start helper and daemon via pkexec to run in the background.""" # this function is overwritten in tests self.dbus = Daemon.connect() - cmd = 'pkexec key-mapper-control --command helper' - if is_debug: - cmd += ' -d' + debug = ' -d' if is_debug() else '' + cmd = f'pkexec key-mapper-control --command helper {debug}' logger.debug('Running `%s`', cmd) os.system(cmd) - if not is_helper_running(): - self.show_status(CTX_ERROR, 'The helper did not start') - def show_confirm_delete(self): """Blocks until the user decided about an action.""" text = f'Are you sure to delete preset "{self.selected_preset}"?' diff --git a/tests/testcases/test_control.py b/tests/testcases/test_control.py index c990dd82..6bf613cf 100644 --- a/tests/testcases/test_control.py +++ b/tests/testcases/test_control.py @@ -249,17 +249,17 @@ class TestControl(unittest.TestCase): self.assertEqual(config.get('foo'), 'bar') def test_internals(self): - with mock.patch('subprocess.Popen') as popen_patch: + with mock.patch('os.system') as os_system_patch: internals(options('helper', None, None, None, False, False, False)) - popen_patch.assert_called_once() - self.assertIn('key-mapper-helper', popen_patch.call_args.args[0]) - self.assertNotIn('-d', popen_patch.call_args.args[0]) + os_system_patch.assert_called_once() + self.assertIn('key-mapper-helper', os_system_patch.call_args.args[0]) + self.assertNotIn('-d', os_system_patch.call_args.args[0]) - with mock.patch('subprocess.Popen') as popen_patch: + with mock.patch('os.system') as os_system_patch: internals(options('start-daemon', None, None, None, False, False, True)) - popen_patch.assert_called_once() - self.assertIn('key-mapper-service', popen_patch.call_args.args[0]) - self.assertIn('-d', popen_patch.call_args.args[0]) + os_system_patch.assert_called_once() + self.assertIn('key-mapper-service', os_system_patch.call_args.args[0]) + self.assertIn('-d', os_system_patch.call_args.args[0]) if __name__ == "__main__":