Skip to content

Commit 2ead1bb

Browse files
authored
Upower refactoring (#3220)
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
1 parent 29917fb commit 2ead1bb

File tree

8 files changed

+574
-699
lines changed

8 files changed

+574
-699
lines changed

include/modules/upower.hpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 &parameters);
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

include/modules/upower/upower.hpp

-82
This file was deleted.

include/modules/upower/upower_tooltip.hpp

-33
This file was deleted.

meson.build

+1-4
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,7 @@ endif
335335

336336
if (upower_glib.found() and not get_option('logind').disabled())
337337
add_project_arguments('-DHAVE_UPOWER', language: 'cpp')
338-
src_files += files(
339-
'src/modules/upower/upower.cpp',
340-
'src/modules/upower/upower_tooltip.cpp',
341-
)
338+
src_files += files('src/modules/upower.cpp')
342339
man_files += files('man/waybar-upower.5.scd')
343340
endif
344341

src/factory.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
#include "modules/gamemode.hpp"
7171
#endif
7272
#ifdef HAVE_UPOWER
73-
#include "modules/upower/upower.hpp"
73+
#include "modules/upower.hpp"
7474
#endif
7575
#ifdef HAVE_PIPEWIRE
7676
#include "modules/privacy/privacy.hpp"
@@ -130,7 +130,7 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
130130
#endif
131131
#ifdef HAVE_UPOWER
132132
if (ref == "upower") {
133-
return new waybar::modules::upower::UPower(id, config_[name]);
133+
return new waybar::modules::UPower(id, config_[name]);
134134
}
135135
#endif
136136
#ifdef HAVE_PIPEWIRE

0 commit comments

Comments
 (0)