Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dbus/dbus.h>
- #include <stdio.h>
- int main()
- {
- DBusError err;
- DBusConnection* conn;
- // initialise the errors
- dbus_error_init(&err);
- // connect to the bus
- conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
- if (dbus_error_is_set(&err)) {
- fprintf(stderr, "Connection Error (%s)\n", err.message);
- dbus_error_free(&err);
- }
- if (NULL == conn) {
- printf("conn == NULL");
- exit(1);
- }
- dbus_bus_add_match(conn,
- "type='signal',interface='com.canonical.Unity.Panel.Service',member='EntryActivated'",
- &err);
- dbus_connection_flush(conn);
- if (dbus_error_is_set(&err)) {
- fprintf(stderr, "Match Error (%s)\n", err.message);
- exit(1);
- }
- dbus_bus_add_match(conn,
- "type='signal',interface='org.freedesktop.UPower'",
- &err);
- dbus_connection_flush(conn);
- if (dbus_error_is_set(&err)) {
- fprintf(stderr, "Match Error (%s)\n", err.message);
- exit(1);
- }
- DBusMessage* msg;
- printf("Running Loop\n");
- // loop listening for signals being emmitted
- while (1){
- printf("Waiting for signal... ");
- //first lets check if there is some message in the queue
- msg = dbus_connection_pop_message(conn);
- if (NULL == msg) {
- //if not lets wait for one
- dbus_connection_read_write(conn, -1);
- msg = dbus_connection_pop_message(conn);
- }
- // check if the message is a signal from the correct interface and with the correct name
- if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Sleeping")) {
- printf("Got Sleeping\n");
- }else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Resuming")) {
- printf("Got Resuming\n");
- }else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "DeviceChanged")) {
- printf("Got DeviceChanged\n");
- }else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Changed")) {
- printf("Got Changed\n");
- }else if (dbus_message_is_signal(msg, "com.canonical.Unity.Panel.Service", "EntryActivated")) {
- printf("Got EntryActivated\n");
- }else{
- printf("Got unknown (%s)\n", dbus_message_get_member(msg));
- }
- // free the message
- dbus_message_unref(msg);
- }
- if (conn)
- {
- dbus_connection_close(conn);
- dbus_connection_unref(conn);
- conn = NULL;
- }
- dbus_error_free (&err);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement