From 956e39d3d3eedd287b14aa82fbc6313246a64b9e Mon Sep 17 00:00:00 2001 From: Lena Date: Fri, 8 Nov 2024 23:40:12 +0100 Subject: [PATCH] make gpsd receiver not poll --- include/modules/gps.hpp | 2 +- man/waybar-gps.5.scd | 3 ++- src/modules/gps.cpp | 28 ++++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/include/modules/gps.hpp b/include/modules/gps.hpp index 6c2e980e..500614a4 100644 --- a/include/modules/gps.hpp +++ b/include/modules/gps.hpp @@ -24,7 +24,7 @@ namespace waybar::modules { const std::string getFixStatusString() const; - util::SleeperThread thread_; + util::SleeperThread thread_, gps_thread_; gps_data_t gps_data_; std::string state_; diff --git a/man/waybar-gps.5.scd b/man/waybar-gps.5.scd index cf5faf42..3b2c2cac 100644 --- a/man/waybar-gps.5.scd +++ b/man/waybar-gps.5.scd @@ -42,7 +42,8 @@ libgps lives in: *interval*: ++ typeof: integer ++ default: 5 ++ - The interval in which the GPS information gets polled (e.g. fix status). + The interval in which the GPS information gets polled (e.g. current speed). + Significant updates (e.g. the current fix mode) are updated immediately. *hide-disconnected*: ++ typeof: bool ++ diff --git a/src/modules/gps.cpp b/src/modules/gps.cpp index 12e01ac4..85726876 100644 --- a/src/modules/gps.cpp +++ b/src/modules/gps.cpp @@ -1,4 +1,5 @@ #include "modules/gps.hpp" +#include #include #include @@ -38,7 +39,28 @@ waybar::modules::Gps::Gps(const std::string& id, const Json::Value& config) hideNoFix = config_["hide-no-fix"].asBool(); } - gps_stream(&gps_data_, WATCH_ENABLE, NULL); + gps_thread_ = [this] { + dp.emit(); + gps_stream(&gps_data_, WATCH_ENABLE, NULL); + int last_gps_mode = 0; + + while (gps_waiting(&gps_data_, 5000000)) { + if (gps_read(&gps_data_, NULL, 0) == -1) { + throw std::runtime_error("Can't read data from gpsd."); + } + + if (MODE_SET != (MODE_SET & gps_data_.set)) { + // did not even get mode, nothing to see here + continue; + } + + if (gps_data_.fix.mode != last_gps_mode) { + // significant update + dp.emit(); + } + last_gps_mode = gps_data_.fix.mode; + } + }; } const std::string waybar::modules::Gps::getFixModeName() const { @@ -93,9 +115,7 @@ const std::string waybar::modules::Gps::getFixStatusString() const { } auto waybar::modules::Gps::update() -> void { - if (gps_read(&gps_data_, NULL, 0) == -1) { - throw std::runtime_error("Can't read data from gpsd."); - } + sleep(0); // Wait for gps status change if ((gps_data_.fix.mode == MODE_NOT_SEEN && hideDisconnected) || (gps_data_.fix.mode == MODE_NO_FIX && hideNoFix)) { event_box_.set_visible(false);