Embedd assets icons

This commit is contained in:
2026-10-18 02:57:29 +02:00
parent b77b4a3fc6
commit 726a7ae92d
19 changed files with 1202 additions and 126 deletions

219
daemon/ipc_dbus.cpp Normal file
View File

@@ -0,0 +1,219 @@
// gKeyMapper daemon - D-Bus service for the GUI (control plane only).
#include "ipc_dbus.h"
#include <cstdio>
#include "ipc_ids.h"
#include "mapping.h"
namespace gkeymapper::daemon {
namespace {
// Index convention: 0 = G1 ... 8 = G9, the same as the internal mapping vector
// and the KeyPressed signal, so GUI and CLI agree on one numbering.
const char kIntrospectionXml[] =
"<node>"
" <interface name='dev.rager.gkeymapper.Daemon'>"
" <method name='Reload'/>"
" <method name='GetStatus'>"
" <arg type='s' name='version' direction='out'/>"
" <arg type='b' name='running' direction='out'/>"
" <arg type='s' name='device' direction='out'/>"
" <arg type='s' name='configPath' direction='out'/>"
" </method>"
" <method name='TestMapping'>"
" <arg type='u' name='index' direction='in'/>"
" </method>"
" <signal name='KeyPressed'>"
" <arg type='u' name='index'/>"
" <arg type='s' name='action'/>"
" </signal>"
" <signal name='ConfigReloaded'>"
" <arg type='b' name='ok'/>"
" </signal>"
" <signal name='DeviceChanged'>"
" <arg type='b' name='present'/>"
" <arg type='s' name='device'/>"
" </signal>"
" </interface>"
"</node>";
} // namespace
DaemonService::~DaemonService() {
if (owner_ != 0) {
g_bus_unown_name(owner_);
}
if (registration_ != 0 && connection_ != nullptr) {
g_dbus_connection_unregister_object(connection_, registration_);
}
if (connection_ != nullptr) {
g_object_unref(connection_);
}
}
StartResult DaemonService::start(std::string configPath) {
configPath_ = std::move(configPath);
GError* error = nullptr;
connection_ = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error);
if (connection_ == nullptr) {
std::fprintf(stderr,
"gkeymapper-daemon: no session bus (%s); running without "
"the GUI channel\n",
error != nullptr ? error->message : "unknown");
g_clear_error(&error);
return StartResult::NoBus;
}
GDBusNodeInfo* info = g_dbus_node_info_new_for_xml(kIntrospectionXml, &error);
if (info == nullptr) {
std::fprintf(stderr, "gkeymapper-daemon: bad D-Bus introspection: %s\n",
error->message);
g_clear_error(&error);
return StartResult::NoBus;
}
GDBusInterfaceVTable vtable{};
vtable.method_call = &DaemonService::handle_method_call;
registration_ = g_dbus_connection_register_object(
connection_, gkeymapper::ipc::kObjectPath, info->interfaces[0], &vtable, this,
nullptr, &error);
g_dbus_node_info_unref(info);
if (registration_ == 0) {
std::fprintf(stderr, "gkeymapper-daemon: cannot register object: %s\n",
error->message);
g_clear_error(&error);
return StartResult::NoBus;
}
// DO_NOT_QUEUE: if the name is taken, fail instead of waiting. Two daemons
// would fight over the keyboard and create two virtual keyboards.
owner_ = g_bus_own_name_on_connection(
connection_, gkeymapper::ipc::kBusName,
G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE,
[](GDBusConnection*, const gchar*, gpointer userData) {
static_cast<DaemonService*>(userData)->name_owned_ = true;
},
[](GDBusConnection*, const gchar* name, gpointer userData) {
auto* self = static_cast<DaemonService*>(userData);
self->name_lost_ = true;
std::fprintf(stderr, "gkeymapper-daemon: bus name %s is taken\n", name);
},
this, nullptr);
// Let the name request complete before deciding.
for (int i = 0; i < 100 && !name_owned_ && !name_lost_; ++i) {
g_main_context_iteration(nullptr, FALSE);
g_usleep(10 * 1000);
}
if (!name_owned_ && name_lost_) {
return StartResult::AlreadyRunning;
}
std::fprintf(stderr, "gkeymapper-daemon: listening on %s (%s)\n",
gkeymapper::ipc::kBusName, gkeymapper::ipc::kObjectPath);
return StartResult::Ok;
}
void DaemonService::pump() {
if (connection_ == nullptr) {
return;
}
while (g_main_context_iteration(nullptr, FALSE)) {
// Drain everything that is ready; nothing else uses this context.
}
}
void DaemonService::handle_method_call(GDBusConnection* /*connection*/, const gchar* /*sender*/,
const gchar* /*objectPath*/, const gchar* /*interfaceName*/,
const gchar* methodName, GVariant* parameters,
GDBusMethodInvocation* invocation, gpointer userData) {
auto* self = static_cast<DaemonService*>(userData);
if (g_strcmp0(methodName, "Reload") == 0) {
if (self->reload_) {
self->reload_();
}
g_dbus_method_invocation_return_value(invocation, nullptr);
return;
}
if (g_strcmp0(methodName, "GetStatus") == 0) {
const std::string device = self->device_ ? self->device_() : std::string();
g_dbus_method_invocation_return_value(
invocation, g_variant_new("(sbss)", gkeymapper::ipc::kVersion, TRUE,
device.c_str(), self->configPath_.c_str()));
return;
}
if (g_strcmp0(methodName, "TestMapping") == 0) {
guint32 index = 0;
g_variant_get(parameters, "(u)", &index);
if (index >= static_cast<guint32>(gkeymapper::kNumKeys)) {
g_dbus_method_invocation_return_error(
invocation, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
"no G%u (index 0..%d)", index + 1, gkeymapper::kNumKeys - 1);
return;
}
std::string action;
if (!self->test_ || !self->test_(static_cast<int>(index), action)) {
g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
"G%u has no usable mapping", index + 1);
return;
}
g_dbus_method_invocation_return_value(invocation, nullptr);
self->notify_key_pressed(static_cast<int>(index), action);
return;
}
g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"unknown method %s", methodName);
}
void DaemonService::notify_key_pressed(int index, const std::string& action) {
if (connection_ == nullptr) {
return;
}
GError* error = nullptr;
g_dbus_connection_emit_signal(
connection_, nullptr, gkeymapper::ipc::kObjectPath, gkeymapper::ipc::kInterface,
"KeyPressed",
g_variant_new("(us)", static_cast<guint32>(index), action.c_str()), &error);
if (error != nullptr) {
g_clear_error(&error);
}
}
void DaemonService::notify_config_reloaded(bool ok) {
if (connection_ == nullptr) {
return;
}
GError* error = nullptr;
g_dbus_connection_emit_signal(connection_, nullptr, gkeymapper::ipc::kObjectPath,
gkeymapper::ipc::kInterface, "ConfigReloaded",
g_variant_new("(b)", ok ? TRUE : FALSE), &error);
if (error != nullptr) {
g_clear_error(&error);
}
}
void DaemonService::notify_device_changed(bool present, const std::string& device) {
if (connection_ == nullptr) {
return;
}
GError* error = nullptr;
g_dbus_connection_emit_signal(
connection_, nullptr, gkeymapper::ipc::kObjectPath, gkeymapper::ipc::kInterface,
"DeviceChanged", g_variant_new("(bs)", present ? TRUE : FALSE, device.c_str()),
&error);
if (error != nullptr) {
g_clear_error(&error);
}
}
} // namespace gkeymapper::daemon

74
daemon/ipc_dbus.h Normal file
View File

@@ -0,0 +1,74 @@
// 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

View File

@@ -4,8 +4,8 @@
// configured action and replays it ("key" via uinput, "command" via exec).
//
// Milestones: M0 (shared core), M1 (device bring-up/decoding), M2 (action
// execution) and M3 (config watching + lifecycle) are done; the GUI channel
// (D-Bus) is still open (M4).
// execution), M3 (config watching + lifecycle) and M4 (D-Bus GUI channel) are
// done.
#include <csignal>
#include <cstdint>
@@ -17,6 +17,7 @@
#include "config_watch.h"
#include "device_tables.h"
#include "g910_device.h"
#include "ipc_dbus.h"
#include "mapping.h"
namespace {
@@ -50,14 +51,13 @@ int main(int argc, char** argv) {
// Reads the config; on failure the previous mappings stay in place, so a
// transient read error never silently disables every key.
auto reload = [&]() {
if (gkeymapper::load_mappings_file(configPath, mappings)) {
std::fprintf(stderr, "gkeymapper-daemon: loaded %s\n", configPath.c_str());
} else {
std::fprintf(stderr, "gkeymapper-daemon: cannot read %s\n", configPath.c_str());
}
auto load = [&]() -> bool {
const bool ok = gkeymapper::load_mappings_file(configPath, mappings);
std::fprintf(stderr, "gkeymapper-daemon: %s %s\n",
ok ? "loaded" : "cannot read", configPath.c_str());
return ok;
};
reload();
load();
// The GUI is the writer; a save is picked up through inotify, SIGHUP/SIGUSR1
// stay as the fallback for scripts and for systems without inotify.
@@ -67,17 +67,78 @@ int main(int argc, char** argv) {
gkeymapper::daemon::ActionRunner actions;
actions.init();
// Declared before the IPC service so its status callback can report the
// device. It is opened only after the name check below: a second instance
// must not detach the kernel driver from the keyboard the running one uses.
gkeymapper::daemon::GKeyDevice device;
const gkeymapper::daemon::DeviceProfile* profile = nullptr;
// Runs the mapping of G<index+1> (0-based). A real keypress and the D-Bus
// TestMapping call both go through here, so a test fires exactly the same
// path. Returns whether there was an action to run.
auto run_mapping = [&](int index, bool test) -> bool {
if (index < 0 || index >= static_cast<int>(mappings.keys.size())) {
return false;
}
const gkeymapper::Mapping& mapping = mappings.keys[index];
const bool usable = mapping.type != gkeymapper::ActionType::Disabled &&
!mapping.target.empty();
std::printf("G%d -> %s:%s%s\n", index + 1,
gkeymapper::action_type_token(mapping.type), mapping.target.c_str(),
test ? " (test)" : "");
std::fflush(stdout);
if (usable) {
actions.run(mapping);
}
return usable;
};
gkeymapper::daemon::DaemonService service;
auto reload = [&]() -> bool {
const bool ok = load();
service.notify_config_reloaded(ok);
return ok;
};
service.set_reload(reload);
service.set_device([&]() {
return profile != nullptr ? std::string(profile->name) : std::string();
});
service.set_test([&](int index, std::string& action) {
if (!run_mapping(index, true)) {
return false;
}
const gkeymapper::Mapping& mapping = mappings.keys[index];
action = std::string(gkeymapper::action_type_token(mapping.type)) + ":" +
mapping.target;
return true;
});
switch (service.start(configPath)) {
case gkeymapper::daemon::StartResult::AlreadyRunning:
// Two instances would fight over the device and create a second
// virtual keyboard, so the newcomer gives way.
std::fprintf(stderr,
"gkeymapper-daemon: another instance is already running; "
"exiting\n");
return 0;
case gkeymapper::daemon::StartResult::NoBus:
case gkeymapper::daemon::StartResult::Ok:
break;
}
if (!device.open()) {
return 1;
}
device.disable_binding();
const gkeymapper::daemon::DeviceProfile* profile = device.profile();
profile = device.profile();
service.notify_device_changed(true, profile != nullptr ? profile->name : "");
uint8_t buffer[64];
while (g_running) {
// The loop wakes up at least every read timeout, which also bounds how
// long a config change takes to be applied (well under a second).
// Control plane first (bounded by the read timeout below), then the
// keyboard. The loop wakes up at least every read timeout, which also
// bounds how long a config change takes to be applied.
service.pump();
watcher.poll_events();
if (g_reload || watcher.take_change()) {
g_reload = 0;
@@ -90,29 +151,32 @@ int main(int argc, char** argv) {
}
if (n < 0) {
std::fprintf(stderr, "gkeymapper-daemon: read error (%d), reopening\n", n);
service.notify_device_changed(false, profile != nullptr ? profile->name : "");
device.close();
if (!device.open()) {
return 1;
}
device.disable_binding();
profile = device.profile();
service.notify_device_changed(true, profile != nullptr ? profile->name : "");
continue;
}
int index = gkeymapper::daemon::match_gkey(*profile, buffer, n);
if (index >= 0 && index < static_cast<int>(mappings.keys.size())) {
run_mapping(index, false);
const gkeymapper::Mapping& mapping = mappings.keys[index];
std::printf("G%d -> %s:%s\n", index + 1,
gkeymapper::action_type_token(mapping.type),
mapping.target.c_str());
std::fflush(stdout);
actions.run(mapping);
service.notify_key_pressed(
index, std::string(gkeymapper::action_type_token(mapping.type)) + ":" +
mapping.target);
} else if (gkeymapper::daemon::is_release_report(*profile, buffer, n)) {
// Key released; nothing to do.
}
// Media keys and LED acknowledgements are ignored.
}
service.notify_device_changed(false, profile != nullptr ? profile->name : "");
service.pump();
device.close();
return 0;
}