|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <giomm/dbusconnection.h> |
| 4 | +#include <gtkmm/icontheme.h> |
| 5 | +#include <libupower-glib/upower.h> |
| 6 | + |
| 7 | +#include <unordered_map> |
| 8 | + |
| 9 | +#include "AIconLabel.hpp" |
| 10 | + |
| 11 | +namespace waybar::modules { |
| 12 | + |
| 13 | +class UPower final : public AIconLabel { |
| 14 | + public: |
| 15 | + UPower(const std::string &, const Json::Value &); |
| 16 | + virtual ~UPower(); |
| 17 | + auto update() -> void override; |
| 18 | + |
| 19 | + private: |
| 20 | + const std::string NO_BATTERY{"battery-missing-symbolic"}; |
| 21 | + |
| 22 | + // Config |
| 23 | + bool showIcon_{true}; |
| 24 | + bool hideIfEmpty_{true}; |
| 25 | + int iconSize_{20}; |
| 26 | + int tooltip_spacing_{4}; |
| 27 | + int tooltip_padding_{4}; |
| 28 | + Gtk::Box contentBox_; // tooltip box |
| 29 | + std::string tooltipFormat_; |
| 30 | + |
| 31 | + // UPower device info |
| 32 | + struct upDevice_output { |
| 33 | + UpDevice *upDevice{NULL}; |
| 34 | + double percentage{0.0}; |
| 35 | + double temperature{0.0}; |
| 36 | + guint64 time_full{0u}; |
| 37 | + guint64 time_empty{0u}; |
| 38 | + gchar *icon_name{(char *)'\0'}; |
| 39 | + bool upDeviceValid{false}; |
| 40 | + UpDeviceState state; |
| 41 | + UpDeviceKind kind; |
| 42 | + char *nativePath{(char *)'\0'}; |
| 43 | + char *model{(char *)'\0'}; |
| 44 | + }; |
| 45 | + |
| 46 | + // Technical variables |
| 47 | + std::string nativePath_; |
| 48 | + std::string lastStatus_; |
| 49 | + Glib::ustring label_markup_; |
| 50 | + std::mutex mutex_; |
| 51 | + Glib::RefPtr<Gtk::IconTheme> gtkTheme_; |
| 52 | + |
| 53 | + // Technical functions |
| 54 | + void addDevice(UpDevice *); |
| 55 | + void removeDevice(const gchar *); |
| 56 | + void removeDevices(); |
| 57 | + void resetDevices(); |
| 58 | + void setDisplayDevice(); |
| 59 | + const Glib::ustring getText(const upDevice_output &upDevice_, const std::string &format); |
| 60 | + bool queryTooltipCb(int, int, bool, const Glib::RefPtr<Gtk::Tooltip> &); |
| 61 | + |
| 62 | + // DBUS variables |
| 63 | + guint watcherID_; |
| 64 | + Glib::RefPtr<Gio::DBus::Connection> conn_; |
| 65 | + guint subscrID_{0u}; |
| 66 | + |
| 67 | + // UPower variables |
| 68 | + UpClient *upClient_; |
| 69 | + upDevice_output upDevice_; // Device to display |
| 70 | + typedef std::unordered_map<std::string, upDevice_output> Devices; |
| 71 | + Devices devices_; |
| 72 | + bool upRunning_{true}; |
| 73 | + |
| 74 | + // DBus callbacks |
| 75 | + void getConn_cb(Glib::RefPtr<Gio::AsyncResult> &result); |
| 76 | + void onAppear(const Glib::RefPtr<Gio::DBus::Connection> &, const Glib::ustring &, |
| 77 | + const Glib::ustring &); |
| 78 | + void onVanished(const Glib::RefPtr<Gio::DBus::Connection> &, const Glib::ustring &); |
| 79 | + void prepareForSleep_cb(const Glib::RefPtr<Gio::DBus::Connection> &connection, |
| 80 | + const Glib::ustring &sender_name, const Glib::ustring &object_path, |
| 81 | + const Glib::ustring &interface_name, const Glib::ustring &signal_name, |
| 82 | + const Glib::VariantContainerBase ¶meters); |
| 83 | + |
| 84 | + // UPower callbacks |
| 85 | + static void deviceAdded_cb(UpClient *client, UpDevice *device, gpointer data); |
| 86 | + static void deviceRemoved_cb(UpClient *client, const gchar *objectPath, gpointer data); |
| 87 | + static void deviceNotify_cb(UpDevice *device, GParamSpec *pspec, gpointer user_data); |
| 88 | + // UPower secondary functions |
| 89 | + void getUpDeviceInfo(upDevice_output &upDevice_); |
| 90 | +}; |
| 91 | + |
| 92 | +} // namespace waybar::modules |
0 commit comments