Embedd assets icons
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user