Skip to content

Commit 681b3fb

Browse files
committed
UPower refactoring
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
1 parent 231d697 commit 681b3fb

File tree

8 files changed

+602
-699
lines changed

8 files changed

+602
-699
lines changed

Diff for: include/modules/upower.hpp

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

Diff for: include/modules/upower/upower.hpp

-82
This file was deleted.

Diff for: include/modules/upower/upower_tooltip.hpp

-33
This file was deleted.

Diff for: 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

Diff for: 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)