75 lines
2.7 KiB
C
75 lines
2.7 KiB
C
|
|
// gKeyMapper daemon - D-Bus service for the GUI (control plane only).
|
||
|
|
#ifndef GKEYMAPPER_DAEMON_IPC_DBUS_H
|
||
|
|
#define GKEYMAPPER_DAEMON_IPC_DBUS_H
|
||
|
|
|
||
|
|
#include <gio/gio.h>
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
namespace gkeymapper::daemon {
|
||
|
|
|
||
|
|
enum class StartResult {
|
||
|
|
Ok, // the well-known name is owned, calls are served
|
||
|
|
NoBus, // no usable session bus; the daemon runs without IPC
|
||
|
|
AlreadyRunning, // another instance owns the name; this process must exit
|
||
|
|
};
|
||
|
|
|
||
|
|
// The daemon's control-plane interface on the session bus.
|
||
|
|
//
|
||
|
|
// The GUI uses it for status and reload; `TestMapping` also makes a mapping
|
||
|
|
// fireable from the command line (gdbus/busctl) without hardware. Nothing here
|
||
|
|
// is on the keypress path.
|
||
|
|
class DaemonService {
|
||
|
|
public:
|
||
|
|
// Re-reads the mappings file; returns whether it could be read.
|
||
|
|
using ReloadFn = std::function<bool()>;
|
||
|
|
// Description of the keyboard for GetStatus ("" when none is open).
|
||
|
|
using DeviceFn = std::function<std::string()>;
|
||
|
|
// Runs the mapping of G<index+1> (index is 0-based) and reports what ran.
|
||
|
|
using TestFn = std::function<bool(int index, std::string& action)>;
|
||
|
|
|
||
|
|
DaemonService() = default;
|
||
|
|
~DaemonService();
|
||
|
|
|
||
|
|
DaemonService(const DaemonService&) = delete;
|
||
|
|
DaemonService& operator=(const DaemonService&) = delete;
|
||
|
|
|
||
|
|
void set_reload(ReloadFn fn) { reload_ = std::move(fn); }
|
||
|
|
void set_device(DeviceFn fn) { device_ = std::move(fn); }
|
||
|
|
void set_test(TestFn fn) { test_ = std::move(fn); }
|
||
|
|
|
||
|
|
StartResult start(std::string configPath);
|
||
|
|
|
||
|
|
bool is_connected() const { return connection_ != nullptr; }
|
||
|
|
|
||
|
|
// Dispatches pending messages. Non-blocking and cheap; call it once per
|
||
|
|
// event-loop iteration. (Outgoing signals are flushed here too, so a
|
||
|
|
// notification is visible to the GUI within one loop pass.)
|
||
|
|
void pump();
|
||
|
|
|
||
|
|
void notify_config_reloaded(bool ok);
|
||
|
|
void notify_key_pressed(int index, const std::string& action);
|
||
|
|
void notify_device_changed(bool present, const std::string& device);
|
||
|
|
|
||
|
|
private:
|
||
|
|
static void handle_method_call(GDBusConnection* connection, const gchar* sender,
|
||
|
|
const gchar* objectPath, const gchar* interfaceName,
|
||
|
|
const gchar* methodName, GVariant* parameters,
|
||
|
|
GDBusMethodInvocation* invocation, gpointer userData);
|
||
|
|
|
||
|
|
GDBusConnection* connection_ = nullptr;
|
||
|
|
guint registration_ = 0;
|
||
|
|
guint owner_ = 0;
|
||
|
|
std::string configPath_;
|
||
|
|
bool name_owned_ = false;
|
||
|
|
bool name_lost_ = false;
|
||
|
|
ReloadFn reload_;
|
||
|
|
DeviceFn device_;
|
||
|
|
TestFn test_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace gkeymapper::daemon
|
||
|
|
|
||
|
|
#endif // GKEYMAPPER_DAEMON_IPC_DBUS_H
|