Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upower module #1484

Merged
merged 25 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4ee81c8
Added all callbacks
ErikReider Mar 15, 2022
5e9faeb
Now shows the percentage and the correct icon
ErikReider Mar 15, 2022
5396ffb
Added "icon-size" and "hide-if-empty" config options
ErikReider Mar 15, 2022
d47073e
Reload all devices after wake from suspend
ErikReider Mar 15, 2022
9b9e42f
use device variable instead of getting the device from the map
ErikReider Mar 15, 2022
6eb187a
Moved upower.*pp out of the upower directories
ErikReider Mar 15, 2022
136b47c
Fixed percentage not getting rounded
ErikReider Mar 15, 2022
2a563f5
Change name to upower
ErikReider Mar 16, 2022
c8d7850
Merge branch 'master' into upower-module
ErikReider Mar 19, 2022
175d828
Merge branch 'upower-module' of github.com:ErikReider/Waybar into upo…
ErikReider Mar 19, 2022
105f1ce
Fixed device unrefed
ErikReider Mar 19, 2022
7345918
Added mutex locking to resetDevices
ErikReider Mar 19, 2022
d7a030d
Lowered default icon size to 20
ErikReider Mar 19, 2022
2b2ac31
Fixed not visible when only battery is plugged in
ErikReider Mar 19, 2022
14a2a70
Moved upower into its own directory
ErikReider Mar 19, 2022
4196e0d
Unref all devices on destruct
ErikReider Mar 19, 2022
7fac2af
Added custom tooltip with device icon, device name and battery status
ErikReider Mar 19, 2022
5f19a54
Added man page
ErikReider Mar 19, 2022
05effad
Added CSS status classes
ErikReider Mar 19, 2022
c4282cf
Made tooltip getDeviceIcon return const string
ErikReider Mar 19, 2022
0764e9a
Added format and format-alt config properties
ErikReider Mar 19, 2022
2633ff3
Fixed time_left string not being set properly
ErikReider Mar 19, 2022
7b07156
Removed goto in update method
ErikReider Mar 19, 2022
e06316c
Only set label text if string doesn't only contain spaces
ErikReider Mar 19, 2022
84dc82e
Added tooltip-padding
ErikReider Mar 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#ifdef HAVE_LIBEVDEV
#include "modules/keyboard_state.hpp"
#endif
#ifdef HAVE_UPOWER
#include "modules/upower/upower.hpp"
#endif
#ifdef HAVE_LIBPULSE
#include "modules/pulseaudio.hpp"
#endif
Expand Down
72 changes: 72 additions & 0 deletions include/modules/upower/upower.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <libupower-glib/upower.h>

#include <iostream>
#include <map>
#include <string>

#include "ALabel.hpp"
#include "glibconfig.h"
#include "gtkmm/box.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
#include "modules/upower/upower_tooltip.hpp"

namespace waybar::modules::upower {

class UPower : public AModule {
public:
UPower(const std::string &, const Json::Value &);
~UPower();
auto update() -> void;

private:
typedef std::unordered_map<std::string, UpDevice *> Devices;

const std::string DEFAULT_FORMAT = "{percentage}";
const std::string DEFAULT_FORMAT_ALT = "{percentage} {time}";

static void deviceAdded_cb(UpClient *client, UpDevice *device, gpointer data);
static void deviceRemoved_cb(UpClient *client, const gchar *objectPath, gpointer data);
static void deviceNotify_cb(UpDevice *device, GParamSpec *pspec, gpointer user_data);
static void prepareForSleep_cb(GDBusConnection *system_bus, const gchar *sender_name,
const gchar *object_path, const gchar *interface_name,
const gchar *signal_name, GVariant *parameters,
gpointer user_data);
void removeDevice(const gchar *objectPath);
void addDevice(UpDevice *device);
void setDisplayDevice();
void resetDevices();
void removeDevices();
bool show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Tooltip> &tooltip);
bool handleToggle(GdkEventButton *const &);
std::string timeToString(gint64 time);

const std::string getDeviceStatus(UpDeviceState &state);

Gtk::Box box_;
Gtk::Image icon_;
Gtk::Label label_;

// Config
bool hideIfEmpty = true;
bool tooltip_enabled = true;
uint tooltip_spacing = 4;
uint tooltip_padding = 4;
uint iconSize = 20;
std::string format = DEFAULT_FORMAT;
std::string format_alt = DEFAULT_FORMAT_ALT;

Devices devices;
std::mutex m_Mutex;
UpClient *client;
UpDevice *displayDevice;
guint login1_id;
GDBusConnection *login1_connection;
UPowerTooltip *upower_tooltip;
std::string lastStatus;
bool showAltText;
};

} // namespace waybar::modules::upower
30 changes: 30 additions & 0 deletions include/modules/upower/upower_tooltip.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <libupower-glib/upower.h>

#include "gtkmm/box.h"
#include "gtkmm/label.h"
#include "gtkmm/window.h"

namespace waybar::modules::upower {

class UPowerTooltip : public Gtk::Window {
private:
typedef std::unordered_map<std::string, UpDevice*> Devices;

const std::string getDeviceIcon(UpDeviceKind& kind);

Gtk::Box* contentBox;

uint iconSize;
uint tooltipSpacing;
uint tooltipPadding;

public:
UPowerTooltip(uint iconSize, uint tooltipSpacing, uint tooltipPadding);
~UPowerTooltip();

uint updateTooltip(Devices& devices);
};

} // namespace waybar::modules::upower
74 changes: 74 additions & 0 deletions man/waybar-upower.5.scd
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
waybar-upower(5)

# NAME

waybar - upower module

# DESCRIPTION

The *upower* module displays the main battery capacity with all other upower
compatible devices in the tooltip.

# CONFIGURATION

*icon-size*: ++
typeof: integer ++
default: 20 ++
Defines the size of the icons.

*format*: ++
typeof: string ++
default: {percentage} ++
The text format.

*format-alt*: ++
typeof: string ++
default: {percentage} {time} ++
The text format when toggled.

*hide-if-empty*: ++
typeof: bool ++
default: true ++
Defines visibility of the module if no devices can be found.

*tooltip*: ++
typeof: bool ++
defualt: true ++
Option to disable tooltip on hover.

*tooltip-spacing*: ++
typeof: integer ++
default: 4 ++
Defines the spacing between the tooltip device name and device battery ++
status.

*tooltip-padding*: ++
typeof: integer ++
default: 4 ++
Defines the spacing between the tooltip window edge and the tooltip content.

# FORMAT REPLACEMENTS

*{percentage}*: The battery capacity in percentage

*{time}*: An estimated time either until empty or until fully charged ++
depending on the charging state.

# EXAMPLES

```
"upower": {
"icon-size": 20,
"hide-if-empty": true,
"tooltip": true,
"tooltip-spacing": 20
}

```

# STYLE

- *#upower*
- *#upower.charging*
- *#upower.discharging*
- *#upower.unknown-status*
12 changes: 10 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ wayland_cursor = dependency('wayland-cursor')
wayland_protos = dependency('wayland-protocols')
gtkmm = dependency('gtkmm-3.0', version : ['>=3.22.0'])
dbusmenu_gtk = dependency('dbusmenu-gtk3-0.4', required: get_option('dbusmenu-gtk'))
giounix = dependency('gio-unix-2.0', required: (get_option('dbusmenu-gtk').enabled() or get_option('logind').enabled()))
giounix = dependency('gio-unix-2.0', required: (get_option('dbusmenu-gtk').enabled() or get_option('logind').enabled() or get_option('upower_glib').enabled()))
jsoncpp = dependency('jsoncpp')
sigcpp = dependency('sigc++-2.0')
libepoll = dependency('epoll-shim', required: false)
libnl = dependency('libnl-3.0', required: get_option('libnl'))
libnlgen = dependency('libnl-genl-3.0', required: get_option('libnl'))
upower_glib = dependency('upower-glib', required: get_option('upower_glib'))
libpulse = dependency('libpulse', required: get_option('pulseaudio'))
libudev = dependency('libudev', required: get_option('libudev'))
libevdev = dependency('libevdev', required: get_option('libevdev'))
Expand Down Expand Up @@ -203,6 +204,12 @@ if libnl.found() and libnlgen.found()
src_files += 'src/modules/network.cpp'
endif

if (upower_glib.found() and giounix.found() and not get_option('logind').disabled())
add_project_arguments('-DHAVE_UPOWER', language: 'cpp')
src_files += 'src/modules/upower/upower.cpp'
src_files += 'src/modules/upower/upower_tooltip.cpp'
endif

if libpulse.found()
add_project_arguments('-DHAVE_LIBPULSE', language: 'cpp')
src_files += 'src/modules/pulseaudio.cpp'
Expand Down Expand Up @@ -288,6 +295,7 @@ executable(
giounix,
libnl,
libnlgen,
upower_glib,
libpulse,
libudev,
libepoll,
Expand All @@ -296,7 +304,7 @@ executable(
gtk_layer_shell,
libsndio,
tz_dep,
xkbregistry
xkbregistry
],
include_directories: [include_directories('include')],
install: true,
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ option('libnl', type: 'feature', value: 'auto', description: 'Enable libnl suppo
option('libudev', type: 'feature', value: 'auto', description: 'Enable libudev support for udev related features')
option('libevdev', type: 'feature', value: 'auto', description: 'Enable libevdev support for evdev related features')
option('pulseaudio', type: 'feature', value: 'auto', description: 'Enable support for pulseaudio')
option('upower_glib', type: 'feature', value: 'auto', description: 'Enable support for upower')
option('systemd', type: 'feature', value: 'auto', description: 'Install systemd user service unit')
option('dbusmenu-gtk', type: 'feature', value: 'auto', description: 'Enable support for tray')
option('man-pages', type: 'feature', value: 'auto', description: 'Generate and install man pages')
Expand Down
5 changes: 5 additions & 0 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
return new waybar::modules::Battery(id, config_[name]);
}
#endif
#ifdef HAVE_UPOWER
if (ref == "upower") {
return new waybar::modules::upower::UPower(id, config_[name]);
}
#endif
#ifdef HAVE_SWAY
if (ref == "sway/mode") {
return new waybar::modules::sway::Mode(id, config_[name]);
Expand Down
Loading