Advertisement
Guest User

Untitled

a guest
Feb 19th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <dbus/dbus.h>
  3. #include <stdio.h>
  4. int main()
  5. {
  6.     DBusError err;
  7.     DBusConnection* conn;
  8.     // initialise the errors
  9.     dbus_error_init(&err);
  10.    
  11.    
  12.     // connect to the bus
  13.     conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
  14.     if (dbus_error_is_set(&err)) {
  15.         fprintf(stderr, "Connection Error (%s)\n", err.message);
  16.         dbus_error_free(&err);
  17.     }
  18.     if (NULL == conn) {
  19.         printf("conn == NULL");
  20.         exit(1);
  21.     }
  22.    
  23.     dbus_bus_add_match(conn,
  24.     "type='signal',interface='com.canonical.Unity.Panel.Service',member='EntryActivated'",
  25.     &err);
  26.     dbus_connection_flush(conn);
  27.     if (dbus_error_is_set(&err)) {
  28.         fprintf(stderr, "Match Error (%s)\n", err.message);
  29.         exit(1);
  30.     }
  31.    
  32.  
  33.     dbus_bus_add_match(conn,
  34.     "type='signal',interface='org.freedesktop.UPower'",
  35.     &err);
  36.    
  37.     dbus_connection_flush(conn);
  38.     if (dbus_error_is_set(&err)) {
  39.         fprintf(stderr, "Match Error (%s)\n", err.message);
  40.         exit(1);
  41.     }
  42.    
  43.    
  44.     DBusMessage* msg;
  45.     printf("Running Loop\n");
  46.     // loop listening for signals being emmitted
  47.     while (1){     
  48.         printf("Waiting for signal... ");
  49.         //first lets check if there is some message in the queue
  50.         msg = dbus_connection_pop_message(conn);
  51.         if (NULL == msg) {
  52.             //if not lets wait for one
  53.             dbus_connection_read_write(conn, -1);
  54.             msg = dbus_connection_pop_message(conn);
  55.         }
  56.         // check if the message is a signal from the correct interface and with the correct name
  57.         if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Sleeping")) {
  58.             printf("Got Sleeping\n");
  59.         }else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Resuming")) {
  60.             printf("Got Resuming\n");
  61.         }else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "DeviceChanged")) {
  62.             printf("Got DeviceChanged\n");
  63.         }else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Changed")) {
  64.             printf("Got Changed\n");
  65.         }else if (dbus_message_is_signal(msg, "com.canonical.Unity.Panel.Service", "EntryActivated")) {
  66.             printf("Got EntryActivated\n");
  67.         }else{
  68.             printf("Got unknown (%s)\n", dbus_message_get_member(msg));
  69.         }
  70.         // free the message
  71.         dbus_message_unref(msg);
  72.     }
  73.    
  74.     if (conn)
  75.       {
  76.         dbus_connection_close(conn);
  77.         dbus_connection_unref(conn);
  78.         conn = NULL;
  79.       }
  80.       dbus_error_free (&err);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement