Media keys working again
This commit is contained in:
122
daemon/main.cpp
122
daemon/main.cpp
@@ -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), M3 (config watching + lifecycle) and M4 (D-Bus GUI channel) are
|
||||
// done.
|
||||
// execution), M3 (config watching + lifecycle), M4 (D-Bus GUI channel) and
|
||||
// M5 (hot-plug, repeat/chord decoding, error reporting to the GUI) are done.
|
||||
|
||||
#include <csignal>
|
||||
#include <cstdint>
|
||||
@@ -33,6 +33,12 @@ void handle_reload(int /*signum*/) {
|
||||
g_reload = 1;
|
||||
}
|
||||
|
||||
// How often the daemon looks for the keyboard while it has none. The loop keeps
|
||||
// pumping D-Bus and the config watch in between, so the control plane stays
|
||||
// responsive and GetStatus can report that there is no device.
|
||||
constexpr gint64 kReopenIntervalUs = 2 * 1000 * 1000;
|
||||
constexpr int kReopenSleepMs = 200;
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
@@ -126,14 +132,26 @@ int main(int argc, char** argv) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!device.open()) {
|
||||
return 1;
|
||||
}
|
||||
device.disable_binding();
|
||||
profile = device.profile();
|
||||
service.notify_device_changed(true, profile != nullptr ? profile->name : "");
|
||||
// Opens the keyboard and tells the GUI. Returns false when there is none,
|
||||
// which is not an error: the session can start before the keyboard is
|
||||
// plugged, and a lost or re-plugged keyboard must not cost the bus name —
|
||||
// the GUI would see the daemon as gone and D-Bus activation would start a
|
||||
// second one for nothing.
|
||||
bool missingReported = false;
|
||||
auto open_device = [&](bool quiet) -> bool {
|
||||
if (!device.open(quiet)) {
|
||||
return false;
|
||||
}
|
||||
device.disable_binding();
|
||||
profile = device.profile();
|
||||
service.notify_device_changed(true, profile != nullptr ? profile->name : "");
|
||||
missingReported = false;
|
||||
return true;
|
||||
};
|
||||
|
||||
uint8_t buffer[64];
|
||||
uint32_t pressed = 0; // G keys reported as pressed by the previous report
|
||||
gint64 next_open = 0; // earliest time to look for the keyboard again
|
||||
while (g_running) {
|
||||
// Control plane first (bounded by the read timeout below), then the
|
||||
// keyboard. The loop wakes up at least every read timeout, which also
|
||||
@@ -145,34 +163,88 @@ int main(int argc, char** argv) {
|
||||
reload();
|
||||
}
|
||||
|
||||
if (!device.is_open()) {
|
||||
const gint64 now = g_get_monotonic_time();
|
||||
if (now >= next_open) {
|
||||
next_open = now + kReopenIntervalUs;
|
||||
// The first attempt of an absence explains itself, the retries
|
||||
// stay quiet: one line per unplug instead of one per retry.
|
||||
if (open_device(missingReported)) {
|
||||
continue; // read from it right away
|
||||
}
|
||||
if (!missingReported) {
|
||||
missingReported = true;
|
||||
std::fprintf(stderr,
|
||||
"gkeymapper-daemon: no usable keyboard, will keep "
|
||||
"looking\n");
|
||||
}
|
||||
}
|
||||
// Sleep briefly instead of the 500 ms read timeout, so a keyboard
|
||||
// that appears is picked up sooner and the loop still services the
|
||||
// control plane.
|
||||
g_usleep(kReopenSleepMs * 1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
int n = device.read_report(buffer, sizeof(buffer), 500);
|
||||
if (n == 0) {
|
||||
continue; // no report within the timeout
|
||||
}
|
||||
if (n < 0) {
|
||||
std::fprintf(stderr, "gkeymapper-daemon: read error (%d), reopening\n", n);
|
||||
// Lost the keyboard (unplugged, or the kernel took the interface
|
||||
// back). Report it and keep looking; the daemon stays up.
|
||||
std::fprintf(stderr, "gkeymapper-daemon: read error (%d), device lost\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 : "");
|
||||
profile = nullptr;
|
||||
pressed = 0;
|
||||
next_open = 0;
|
||||
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];
|
||||
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.
|
||||
if (profile == nullptr) {
|
||||
continue;
|
||||
}
|
||||
uint32_t state = 0;
|
||||
if (gkeymapper::daemon::decode_gkey_state(*profile, buffer, n, state)) {
|
||||
// Only a key that was not pressed before fires: a held key repeats its
|
||||
// report, and a chord arrives as one report with several bits set, so
|
||||
// every key of it fires exactly once.
|
||||
for (size_t i = 0; i < mappings.keys.size() && i < gkeymapper::kNumKeys; ++i) {
|
||||
const uint32_t bit = 1u << i;
|
||||
if ((state & bit) == 0 || (pressed & bit) != 0) {
|
||||
continue;
|
||||
}
|
||||
const gkeymapper::Mapping& mapping = mappings.keys[i];
|
||||
const std::string action =
|
||||
std::string(gkeymapper::action_type_token(mapping.type)) + ":" +
|
||||
mapping.target;
|
||||
run_mapping(static_cast<int>(i), false);
|
||||
service.notify_key_pressed(static_cast<int>(i), action);
|
||||
}
|
||||
pressed = state;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Media keys (mute / volume / play) are forwarded unchanged: they are
|
||||
// reported only on the interface the daemon holds, so without this they
|
||||
// would do nothing at all while it runs (see README). Replaying every
|
||||
// report the keyboard sends keeps the behaviour identical to a keyboard
|
||||
// the kernel drives itself — including its release report, which carries
|
||||
// no bit and is therefore dropped below.
|
||||
uint32_t media = 0;
|
||||
if (!gkeymapper::daemon::decode_media_key(*profile, buffer, n, media) ||
|
||||
media == 0) {
|
||||
continue; // memory key, LED or command acknowledgement, or a release
|
||||
}
|
||||
for (const gkeymapper::daemon::MediaKey& key : profile->mediaKeys) {
|
||||
if ((media & key.bit) == 0) {
|
||||
continue;
|
||||
}
|
||||
std::printf("media -> %s\n", key.name);
|
||||
std::fflush(stdout);
|
||||
actions.tap_key(key.key);
|
||||
}
|
||||
// Media keys and LED acknowledgements are ignored.
|
||||
}
|
||||
|
||||
service.notify_device_changed(false, profile != nullptr ? profile->name : "");
|
||||
|
||||
Reference in New Issue
Block a user