Dbusを使用してLinuxでシステムウェイクイベントを検出する

Dbusを使用してLinuxでシステムウェイクイベントを検出する

アプリケーションがスリープモードから起動するとすぐに、アプリケーションで特定のタスクを実行したいと思います。そのために、私のアプリケーションで起きているイベントを検出したいと思います。私のcppプログラムでスリープイベントを検出するには、次のコードを使用しています。オンラインで何も見つからないので、使用するDBusシグナルなどのcppで起きているイベントを検出する方法を誰かが助けることができますか?

#include <dbus/dbus.h>

static DBusHandlerResult handleSleepSignal (DBusConnection * connection, DBusMessage * message, void * user_data) 
{
  std::cout << "System is going to sleep." << std::endl;
  return DBUS_HANDLER_RESULT_HANDLED;
}

int main () 
{
  DBusError err;
  dbus_error_init( & err);

  // Connect to the session bus
  DBusConnection * connection = dbus_bus_get(DBUS_BUS_SESSION, & err);

  // Add a match rule to listen for signals indicating events
  const char * match_rule = "type='signal',interface='org.freedesktop.login1.Manager',member='PrepareForSleep'";

  dbus_bus_add_match(connection, match_rule, & err);

  // Specify the callback function to handle sleep signal

  dbus_connection_add_filter(connection, handleSleepSignal, nullptr, nullptr);

  // Enter the main event loop
  while (true) {
    dbus_connection_read_write_dispatch(connection, -1);
  }

  // Cleanup
  dbus_connection_unref(connection);

  return 0;
}

ベストアンサー1

おすすめ記事