Skip to content

Commit

Permalink
add all arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
adryzz committed Sep 20, 2024
1 parent ef2d570 commit d50064b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 23 deletions.
7 changes: 4 additions & 3 deletions include/modules/gps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ namespace waybar::modules {
auto update() -> void override;

private:
const int getFixState() const;
const std::string getFixStateName() const;
const std::string getFixStateString() const;
const std::string getFixModeName() const;
const std::string getFixModeString() const;

const std::string getFixStatusString() const;

util::SleeperThread thread_;
gps_data_t gps_data_;
Expand Down
106 changes: 86 additions & 20 deletions src/modules/gps.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "modules/gps.hpp"
#include <spdlog/spdlog.h>

#include <cmath>
#include <cstdio>

// In the 80000 version of fmt library authors decided to optimize imports
// and moved declarations required for fmt::dynamic_format_arg_store in new
Expand All @@ -13,56 +15,84 @@

namespace {
using namespace waybar::util;
constexpr const char *DEFAULT_FORMAT = "{status}";
constexpr const char *DEFAULT_FORMAT = "{mode}";
} // namespace


waybar::modules::Gps::Gps(const std::string& id, const Json::Value& config)
: ALabel(config, "gps", id, "{}", 30) {
: ALabel(config, "gps", id, "{}", 1) {
thread_ = [this] {
dp.emit();
thread_.sleep_for(interval_);
};
// TODO: connect to gpsd socket

if (0 != gps_open("localhost", "2947", &gps_data_)) {
throw std::runtime_error("Can't open gpsd socket");
}

gps_stream(&gps_data_, WATCH_ENABLE, NULL);
}

const int waybar::modules::Gps::getFixState() const {
0
}

const std::string waybar::modules::Gps::getFixStateName() const {
switch (getFixState()) {
case 1:
const std::string waybar::modules::Gps::getFixModeName() const {
switch (gps_data_.fix.mode) {
case MODE_NO_FIX:
return "fix-none";
case MODE_2D:
return "fix-2d";
case 2:
case MODE_3D:
return "fix-3d";
default:
return "fix-none";
return "disconnected";
}
}

const std::string waybar::modules::Gps::getFixStateString() const {
switch (getFixState()) {
case 1:
const std::string waybar::modules::Gps::getFixModeString() const {
switch (gps_data_.fix.mode) {
case MODE_NO_FIX:
return "No fix";
case MODE_2D:
return "2D Fix";
case 2:
case MODE_3D:
return "3D Fix";
default:
return "No fix";
return "Disconnected";
}
}

const std::string waybar::modules::Gps::getFixStatusString() const {
switch (gps_data_.fix.status) {
case STATUS_UNK:
return "Unknown";
case STATUS_GPS:
return "GPS";
case STATUS_DGPS:
return "DGPS";
case STATUS_RTK_FIX:
return "RTK Fixed";
case STATUS_RTK_FLT:
return "RTK Float";
case STATUS_DR:
return "Dead Reckoning";
case STATUS_GNSSDR:
return "GNSS + Dead Reckoning";
case STATUS_TIME:
return "Time Only";
case STATUS_PPS_FIX:
return "PPS Fix";
default:
return "Unknown";
}
}

auto waybar::modules::Gps::update() -> void {
if (gps_read(&gps_data_, NULL, 0) == -1) {
throw std::runtime_error("Can't read data from gpsd.");
}

std::string tooltip_format;

if (!alt_) {
auto state = getFixStateName();
auto state = getFixModeName();
if (!state_.empty() && label_.get_style_context()->has_class(state_)) {
label_.get_style_context()->remove_class(state_);
}
Expand All @@ -87,14 +117,50 @@ auto waybar::modules::Gps::update() -> void {
auto format = format_;

fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(fmt::arg("status", getFixStateString()));
store.push_back(fmt::arg("mode", getFixModeString()));
store.push_back(fmt::arg("status", getFixStatusString()));

label_.set_markup(fmt::vformat(format, store));
store.push_back(fmt::arg("latitude", gps_data_.fix.latitude));
store.push_back(fmt::arg("latitude_error", gps_data_.fix.epy));

store.push_back(fmt::arg("longitude", gps_data_.fix.longitude));
store.push_back(fmt::arg("longitude_error", gps_data_.fix.epx));

store.push_back(fmt::arg("altitude_hae", gps_data_.fix.altHAE));
store.push_back(fmt::arg("altitude_msl", gps_data_.fix.altMSL));
store.push_back(fmt::arg("altitude_error", gps_data_.fix.epv));

store.push_back(fmt::arg("speed", gps_data_.fix.speed));
store.push_back(fmt::arg("speed_error", gps_data_.fix.eps));

store.push_back(fmt::arg("climb", gps_data_.fix.climb));
store.push_back(fmt::arg("climb_error", gps_data_.fix.epc));

store.push_back(fmt::arg("satellites_used", gps_data_.satellites_used));
store.push_back(fmt::arg("satellites_visible", gps_data_.satellites_visible));

auto text = fmt::vformat(format, store);

if (tooltipEnabled()) {
if (tooltip_format.empty() && config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
if (!tooltip_format.empty()) {
auto tooltip_text = fmt::vformat(tooltip_format, store);
if (label_.get_tooltip_text() != tooltip_text) {
label_.set_tooltip_markup(tooltip_text);
}
} else if (label_.get_tooltip_text() != text) {
label_.set_tooltip_markup(text);
}
}
label_.set_markup(text);
// Call parent update
ALabel::update();
}

waybar::modules::Gps::~Gps() {
gps_stream(&gps_data_, WATCH_DISABLE, NULL);
gps_close(&gps_data_);
}

Expand Down

0 comments on commit d50064b

Please sign in to comment.