From 14a2a7027ffc5ea6e8bf129d84ffce3f076f338a Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Sat, 19 Mar 2022 11:34:34 +0100 Subject: [PATCH] Moved upower into its own directory --- include/factory.hpp | 2 +- include/modules/{ => upower}/upower.hpp | 5 +++-- meson.build | 2 +- src/modules/{ => upower}/upower.cpp | 29 +++++++++++++++++++------ 4 files changed, 27 insertions(+), 11 deletions(-) rename include/modules/{ => upower}/upower.hpp (95%) rename src/modules/{ => upower}/upower.cpp (91%) diff --git a/include/factory.hpp b/include/factory.hpp index e4e8fa7b..3954dac4 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -43,7 +43,7 @@ #include "modules/keyboard_state.hpp" #endif #ifdef HAVE_UPOWER -#include "modules/upower.hpp" +#include "modules/upower/upower.hpp" #endif #ifdef HAVE_LIBPULSE #include "modules/pulseaudio.hpp" diff --git a/include/modules/upower.hpp b/include/modules/upower/upower.hpp similarity index 95% rename from include/modules/upower.hpp rename to include/modules/upower/upower.hpp index cf72a479..6145a15b 100644 --- a/include/modules/upower.hpp +++ b/include/modules/upower/upower.hpp @@ -19,9 +19,9 @@ class UPower : public AModule { ~UPower(); auto update() -> void; - private: typedef std::unordered_map Devices; + private: 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); @@ -30,9 +30,10 @@ class UPower : public AModule { const gchar *signal_name, GVariant *parameters, gpointer user_data); void removeDevice(const gchar *objectPath); - void addDevice(UpDevice *device, bool lockMutex = true); + void addDevice(UpDevice *device); void setDisplayDevice(); void resetDevices(); + void removeDevices(); Gtk::Box box_; Gtk::Image icon_; diff --git a/meson.build b/meson.build index 3155137b..5e76181f 100644 --- a/meson.build +++ b/meson.build @@ -206,7 +206,7 @@ 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.cpp' + src_files += 'src/modules/upower/upower.cpp' endif if libpulse.found() diff --git a/src/modules/upower.cpp b/src/modules/upower/upower.cpp similarity index 91% rename from src/modules/upower.cpp rename to src/modules/upower/upower.cpp index 5becb91f..a4f62005 100644 --- a/src/modules/upower.cpp +++ b/src/modules/upower/upower.cpp @@ -1,4 +1,4 @@ -#include "modules/upower.hpp" +#include "modules/upower/upower.hpp" #include "gtkmm/icontheme.h" @@ -109,7 +109,7 @@ void UPower::removeDevice(const gchar* objectPath) { } } -void UPower::addDevice(UpDevice* device, bool lockMutex) { +void UPower::addDevice(UpDevice* device) { if (G_IS_OBJECT(device)) { const gchar* objectPath = up_device_get_object_path(device); @@ -123,7 +123,7 @@ void UPower::addDevice(UpDevice* device, bool lockMutex) { return; } - if (lockMutex) std::lock_guard guard(m_Mutex); + std::lock_guard guard(m_Mutex); if (devices.find(objectPath) != devices.end()) { UpDevice* device = devices[objectPath]; @@ -144,10 +144,8 @@ void UPower::setDisplayDevice() { g_signal_connect(displayDevice, "notify", G_CALLBACK(deviceNotify_cb), this); } -/** Removes all devices and adds the current devices */ -void UPower::resetDevices() { +void UPower::removeDevices() { std::lock_guard guard(m_Mutex); - // Removes all devices if (!devices.empty()) { auto it = devices.cbegin(); while (it != devices.cend()) { @@ -157,12 +155,18 @@ void UPower::resetDevices() { devices.erase(it++); } } +} + +/** Removes all devices and adds the current devices */ +void UPower::resetDevices() { + // Removes all devices + removeDevices(); // Adds all devices GPtrArray* newDevices = up_client_get_devices2(client); for (guint i = 0; i < newDevices->len; i++) { UpDevice* device = (UpDevice*)g_ptr_array_index(newDevices, i); - if (device) addDevice(device, false); + if (device && G_IS_OBJECT(device)) addDevice(device); } // Update the widget @@ -209,6 +213,17 @@ auto UPower::update() -> void { event_box_.set_visible(true); // TODO: Tooltip + if (!devices.empty()) { + for (auto& e : devices) { + const gchar* objectPath = up_device_get_object_path(e.second); + double percentage; + g_object_get(e.second, "percentage", &percentage, NULL); + printf("Device: %s, VALID: %f\n", objectPath, percentage); + } + } else { + printf("No devices\n"); + } + // box_.set_tooltip // Set percentage if (displayDeviceValid) {