mirror of
				https://github.com/rad4day/Waybar.git
				synced 2025-11-04 01:32:42 +01:00 
			
		
		
		
	Reload all devices after wake from suspend
This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
			
		||||
#include "modules/upower/upower.hpp"
 | 
			
		||||
 | 
			
		||||
#include <gio/gio.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <string>
 | 
			
		||||
@@ -35,7 +37,22 @@ UPower::UPower(const std::string& id, const Json::Value& config)
 | 
			
		||||
    throw std::runtime_error("Unable to create UPower client!");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // TODO: Connect to login1 prepare_for_sleep signal
 | 
			
		||||
  // Connect to Login1 PrepareForSleep signal
 | 
			
		||||
  login1_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
 | 
			
		||||
  if (!login1_connection) {
 | 
			
		||||
    throw std::runtime_error("Unable to connect to the SYSTEM Bus!...");
 | 
			
		||||
  } else {
 | 
			
		||||
    login1_id = g_dbus_connection_signal_subscribe(login1_connection,
 | 
			
		||||
                                                   "org.freedesktop.login1",
 | 
			
		||||
                                                   "org.freedesktop.login1.Manager",
 | 
			
		||||
                                                   "PrepareForSleep",
 | 
			
		||||
                                                   "/org/freedesktop/login1",
 | 
			
		||||
                                                   NULL,
 | 
			
		||||
                                                   G_DBUS_SIGNAL_FLAGS_NONE,
 | 
			
		||||
                                                   prepareForSleep_cb,
 | 
			
		||||
                                                   this,
 | 
			
		||||
                                                   NULL);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  g_signal_connect(client, "device-added", G_CALLBACK(deviceAdded_cb), this);
 | 
			
		||||
  g_signal_connect(client, "device-removed", G_CALLBACK(deviceRemoved_cb), this);
 | 
			
		||||
@@ -46,7 +63,13 @@ UPower::UPower(const std::string& id, const Json::Value& config)
 | 
			
		||||
  dp.emit();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
UPower::~UPower() {}
 | 
			
		||||
UPower::~UPower() {
 | 
			
		||||
  if (client != NULL) g_object_unref(client);
 | 
			
		||||
  if (login1_id > 0) {
 | 
			
		||||
    g_dbus_connection_signal_unsubscribe(login1_connection, login1_id);
 | 
			
		||||
    login1_id = 0;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UPower::deviceAdded_cb(UpClient* client, UpDevice* device, gpointer data) {
 | 
			
		||||
  UPower* up = static_cast<UPower*>(data);
 | 
			
		||||
@@ -67,6 +90,19 @@ void UPower::deviceNotify_cb(gpointer data) {
 | 
			
		||||
  // Update the widget
 | 
			
		||||
  up->dp.emit();
 | 
			
		||||
}
 | 
			
		||||
void UPower::prepareForSleep_cb(GDBusConnection* system_bus, const gchar* sender_name,
 | 
			
		||||
                                const gchar* object_path, const gchar* interface_name,
 | 
			
		||||
                                const gchar* signal_name, GVariant* parameters, gpointer data) {
 | 
			
		||||
  if (g_variant_is_of_type(parameters, G_VARIANT_TYPE("(b)"))) {
 | 
			
		||||
    gboolean sleeping;
 | 
			
		||||
    g_variant_get(parameters, "(b)", &sleeping);
 | 
			
		||||
 | 
			
		||||
    if (!sleeping) {
 | 
			
		||||
      UPower* up = static_cast<UPower*>(data);
 | 
			
		||||
      up->resetDevices();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UPower::removeDevice(const std::string devicePath) { devices.erase(devicePath); }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user