Skip to content

Commit d4a0748

Browse files
authored
Merge pull request #1484 from ErikReider/upower-module
Upower module
2 parents 8c6063d + 84dc82e commit d4a0748

File tree

9 files changed

+719
-2
lines changed

9 files changed

+719
-2
lines changed

include/factory.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#ifdef HAVE_LIBEVDEV
4343
#include "modules/keyboard_state.hpp"
4444
#endif
45+
#ifdef HAVE_UPOWER
46+
#include "modules/upower/upower.hpp"
47+
#endif
4548
#ifdef HAVE_LIBPULSE
4649
#include "modules/pulseaudio.hpp"
4750
#endif

include/modules/upower/upower.hpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma once
2+
3+
#include <libupower-glib/upower.h>
4+
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
9+
#include "ALabel.hpp"
10+
#include "glibconfig.h"
11+
#include "gtkmm/box.h"
12+
#include "gtkmm/image.h"
13+
#include "gtkmm/label.h"
14+
#include "modules/upower/upower_tooltip.hpp"
15+
16+
namespace waybar::modules::upower {
17+
18+
class UPower : public AModule {
19+
public:
20+
UPower(const std::string &, const Json::Value &);
21+
~UPower();
22+
auto update() -> void;
23+
24+
private:
25+
typedef std::unordered_map<std::string, UpDevice *> Devices;
26+
27+
const std::string DEFAULT_FORMAT = "{percentage}";
28+
const std::string DEFAULT_FORMAT_ALT = "{percentage} {time}";
29+
30+
static void deviceAdded_cb(UpClient *client, UpDevice *device, gpointer data);
31+
static void deviceRemoved_cb(UpClient *client, const gchar *objectPath, gpointer data);
32+
static void deviceNotify_cb(UpDevice *device, GParamSpec *pspec, gpointer user_data);
33+
static void prepareForSleep_cb(GDBusConnection *system_bus, const gchar *sender_name,
34+
const gchar *object_path, const gchar *interface_name,
35+
const gchar *signal_name, GVariant *parameters,
36+
gpointer user_data);
37+
void removeDevice(const gchar *objectPath);
38+
void addDevice(UpDevice *device);
39+
void setDisplayDevice();
40+
void resetDevices();
41+
void removeDevices();
42+
bool show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Tooltip> &tooltip);
43+
bool handleToggle(GdkEventButton *const &);
44+
std::string timeToString(gint64 time);
45+
46+
const std::string getDeviceStatus(UpDeviceState &state);
47+
48+
Gtk::Box box_;
49+
Gtk::Image icon_;
50+
Gtk::Label label_;
51+
52+
// Config
53+
bool hideIfEmpty = true;
54+
bool tooltip_enabled = true;
55+
uint tooltip_spacing = 4;
56+
uint tooltip_padding = 4;
57+
uint iconSize = 20;
58+
std::string format = DEFAULT_FORMAT;
59+
std::string format_alt = DEFAULT_FORMAT_ALT;
60+
61+
Devices devices;
62+
std::mutex m_Mutex;
63+
UpClient *client;
64+
UpDevice *displayDevice;
65+
guint login1_id;
66+
GDBusConnection *login1_connection;
67+
UPowerTooltip *upower_tooltip;
68+
std::string lastStatus;
69+
bool showAltText;
70+
};
71+
72+
} // namespace waybar::modules::upower
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <libupower-glib/upower.h>
4+
5+
#include "gtkmm/box.h"
6+
#include "gtkmm/label.h"
7+
#include "gtkmm/window.h"
8+
9+
namespace waybar::modules::upower {
10+
11+
class UPowerTooltip : public Gtk::Window {
12+
private:
13+
typedef std::unordered_map<std::string, UpDevice*> Devices;
14+
15+
const std::string getDeviceIcon(UpDeviceKind& kind);
16+
17+
Gtk::Box* contentBox;
18+
19+
uint iconSize;
20+
uint tooltipSpacing;
21+
uint tooltipPadding;
22+
23+
public:
24+
UPowerTooltip(uint iconSize, uint tooltipSpacing, uint tooltipPadding);
25+
~UPowerTooltip();
26+
27+
uint updateTooltip(Devices& devices);
28+
};
29+
30+
} // namespace waybar::modules::upower

man/waybar-upower.5.scd

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
waybar-upower(5)
2+
3+
# NAME
4+
5+
waybar - upower module
6+
7+
# DESCRIPTION
8+
9+
The *upower* module displays the main battery capacity with all other upower
10+
compatible devices in the tooltip.
11+
12+
# CONFIGURATION
13+
14+
*icon-size*: ++
15+
typeof: integer ++
16+
default: 20 ++
17+
Defines the size of the icons.
18+
19+
*format*: ++
20+
typeof: string ++
21+
default: {percentage} ++
22+
The text format.
23+
24+
*format-alt*: ++
25+
typeof: string ++
26+
default: {percentage} {time} ++
27+
The text format when toggled.
28+
29+
*hide-if-empty*: ++
30+
typeof: bool ++
31+
default: true ++
32+
Defines visibility of the module if no devices can be found.
33+
34+
*tooltip*: ++
35+
typeof: bool ++
36+
defualt: true ++
37+
Option to disable tooltip on hover.
38+
39+
*tooltip-spacing*: ++
40+
typeof: integer ++
41+
default: 4 ++
42+
Defines the spacing between the tooltip device name and device battery ++
43+
status.
44+
45+
*tooltip-padding*: ++
46+
typeof: integer ++
47+
default: 4 ++
48+
Defines the spacing between the tooltip window edge and the tooltip content.
49+
50+
# FORMAT REPLACEMENTS
51+
52+
*{percentage}*: The battery capacity in percentage
53+
54+
*{time}*: An estimated time either until empty or until fully charged ++
55+
depending on the charging state.
56+
57+
# EXAMPLES
58+
59+
```
60+
"upower": {
61+
"icon-size": 20,
62+
"hide-if-empty": true,
63+
"tooltip": true,
64+
"tooltip-spacing": 20
65+
}
66+
67+
```
68+
69+
# STYLE
70+
71+
- *#upower*
72+
- *#upower.charging*
73+
- *#upower.discharging*
74+
- *#upower.unknown-status*

meson.build

+10-2
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ wayland_cursor = dependency('wayland-cursor')
8686
wayland_protos = dependency('wayland-protocols')
8787
gtkmm = dependency('gtkmm-3.0', version : ['>=3.22.0'])
8888
dbusmenu_gtk = dependency('dbusmenu-gtk3-0.4', required: get_option('dbusmenu-gtk'))
89-
giounix = dependency('gio-unix-2.0', required: (get_option('dbusmenu-gtk').enabled() or get_option('logind').enabled()))
89+
giounix = dependency('gio-unix-2.0', required: (get_option('dbusmenu-gtk').enabled() or get_option('logind').enabled() or get_option('upower_glib').enabled()))
9090
jsoncpp = dependency('jsoncpp')
9191
sigcpp = dependency('sigc++-2.0')
9292
libepoll = dependency('epoll-shim', required: false)
9393
libnl = dependency('libnl-3.0', required: get_option('libnl'))
9494
libnlgen = dependency('libnl-genl-3.0', required: get_option('libnl'))
95+
upower_glib = dependency('upower-glib', required: get_option('upower_glib'))
9596
libpulse = dependency('libpulse', required: get_option('pulseaudio'))
9697
libudev = dependency('libudev', required: get_option('libudev'))
9798
libevdev = dependency('libevdev', required: get_option('libevdev'))
@@ -203,6 +204,12 @@ if libnl.found() and libnlgen.found()
203204
src_files += 'src/modules/network.cpp'
204205
endif
205206

207+
if (upower_glib.found() and giounix.found() and not get_option('logind').disabled())
208+
add_project_arguments('-DHAVE_UPOWER', language: 'cpp')
209+
src_files += 'src/modules/upower/upower.cpp'
210+
src_files += 'src/modules/upower/upower_tooltip.cpp'
211+
endif
212+
206213
if libpulse.found()
207214
add_project_arguments('-DHAVE_LIBPULSE', language: 'cpp')
208215
src_files += 'src/modules/pulseaudio.cpp'
@@ -288,6 +295,7 @@ executable(
288295
giounix,
289296
libnl,
290297
libnlgen,
298+
upower_glib,
291299
libpulse,
292300
libudev,
293301
libepoll,
@@ -296,7 +304,7 @@ executable(
296304
gtk_layer_shell,
297305
libsndio,
298306
tz_dep,
299-
xkbregistry
307+
xkbregistry
300308
],
301309
include_directories: [include_directories('include')],
302310
install: true,

meson_options.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ option('libnl', type: 'feature', value: 'auto', description: 'Enable libnl suppo
33
option('libudev', type: 'feature', value: 'auto', description: 'Enable libudev support for udev related features')
44
option('libevdev', type: 'feature', value: 'auto', description: 'Enable libevdev support for evdev related features')
55
option('pulseaudio', type: 'feature', value: 'auto', description: 'Enable support for pulseaudio')
6+
option('upower_glib', type: 'feature', value: 'auto', description: 'Enable support for upower')
67
option('systemd', type: 'feature', value: 'auto', description: 'Install systemd user service unit')
78
option('dbusmenu-gtk', type: 'feature', value: 'auto', description: 'Enable support for tray')
89
option('man-pages', type: 'feature', value: 'auto', description: 'Generate and install man pages')

src/factory.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
1212
return new waybar::modules::Battery(id, config_[name]);
1313
}
1414
#endif
15+
#ifdef HAVE_UPOWER
16+
if (ref == "upower") {
17+
return new waybar::modules::upower::UPower(id, config_[name]);
18+
}
19+
#endif
1520
#ifdef HAVE_SWAY
1621
if (ref == "sway/mode") {
1722
return new waybar::modules::sway::Mode(id, config_[name]);

0 commit comments

Comments
 (0)