Skip to content

Commit

Permalink
use message dispatching instead of busy-waiting
Browse files Browse the repository at this point in the history
As cow-notify has no special requirements for a mainloop we can
avoid doing sleep cycles. This is not only saving a few cpu cycles,
but gives us also 'instant' notifications instead of ~1 sec delays.
.
The code also passes the used DBusConnection around instead of
storing it in a global variable - that's again more a taste than a
really needed change.
  • Loading branch information
DonKult authored and Cloudef committed Nov 29, 2011
1 parent 78f4ed1 commit 64a6705
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
12 changes: 4 additions & 8 deletions cow-notify.c
Expand Up @@ -26,18 +26,14 @@ int main(int argc, char **argv)
fprintf(stderr, "debugging enabled.\n");
}

while ( !notify_init(DEBUGGING) ) {
DBusConnection *dbus = NULL;
while ((dbus = notify_init(DEBUGGING)) == NULL) {
fprintf(stderr, "cannot bind notifications\n");
sleep(1);
}

while ( true )
{
if( !notify_check() )
{
sleep(1);
}
}
dbus_connection_add_filter(dbus, notify_handle, NULL, NULL);
while(dbus_connection_read_write_dispatch(dbus, -1));

return 0;
}
26 changes: 12 additions & 14 deletions notify.c
Expand Up @@ -16,13 +16,11 @@ bool DEBUGGING=0;
notification *messages=NULL;

dbus_uint32_t curNid = 1;
DBusConnection* dbus_conn;

bool notify_Notify(DBusMessage *msg);
bool notify_GetCapabilities(DBusMessage *msg);
bool notify_GetServerInformation(DBusMessage *msg);
bool notify_CloseNotification(DBusMessage *msg);
bool notify_NotificationClosed(unsigned int nid, unsigned int reason);
bool notify_Notify(DBusConnection *dbus, DBusMessage *msg);
bool notify_GetCapabilities(DBusConnection *dbus, DBusMessage *msg);
bool notify_GetServerInformation(DBusConnection *dbus, DBusMessage *msg);
bool notify_CloseNotification(DBusConnection *dbus, DBusMessage *msg);
bool notify_NotificationClosed(DBusConnection *dbus, unsigned int nid, unsigned int reason);

// Command from file
static char command[LINE_MAX];
Expand Down Expand Up @@ -63,7 +61,7 @@ DBusConnection* notify_init(bool const debug_enabled) {

dbus = dbus_bus_get(DBUS_BUS_SESSION, &dbus_err);
if (NULL == dbus)
return NULL;
return 0;

ret = dbus_bus_request_name(dbus, "org.freedesktop.Notifications", DBUS_NAME_FLAG_REPLACE_EXISTING , &dbus_err);
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
Expand Down Expand Up @@ -118,6 +116,7 @@ DBusHandlerResult notify_handle(DBusConnection *dbus, DBusMessage *msg, void *us
return DBUS_HANDLER_RESULT_HANDLED;
}


// check the dbus for notifications (1=something happened, 0=nothing)
bool notify_check(DBusConnection *dbus) {
DBusMessage* msg;
Expand All @@ -128,7 +127,6 @@ bool notify_check(DBusConnection *dbus) {

if (msg != NULL) {
notify_handle(dbus, msg, NULL);

dbus_message_unref(msg);
dbus_connection_flush(dbus);
return true;
Expand Down Expand Up @@ -172,7 +170,7 @@ bool notify_NotificationClosed(DBusConnection *dbus, unsigned int nid, unsigned
dbus_message_iter_init_append(notify_close_msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &nid) ||
!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &reason) ||
!dbus_connection_send(dbus_conn, notify_close_msg, NULL))
!dbus_connection_send(dbus, notify_close_msg, NULL))
{
dbus_message_unref(notify_close_msg);
return false;
Expand Down Expand Up @@ -356,7 +354,7 @@ bool notify_Notify(DBusConnection *dbus, DBusMessage *msg) {

dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &(note->nid)) ||
!dbus_connection_send(dbus_conn, reply, NULL))
!dbus_connection_send(dbus, reply, NULL))
{
return false;
}
Expand Down Expand Up @@ -394,7 +392,7 @@ bool notify_CloseNotification(DBusConnection *dbus, DBusMessage *msg) {
}

reply = dbus_message_new_method_return(msg);
if( !dbus_connection_send(dbus_conn, reply, NULL)) return false;
if( !dbus_connection_send(dbus, reply, NULL)) return false;
dbus_message_unref(reply);

DEBUG(" Close Notification Queued.\n");
Expand Down Expand Up @@ -426,7 +424,7 @@ bool notify_GetCapabilities(DBusConnection *dbus, DBusMessage *msg) {
return 1;

if (!dbus_message_iter_close_container(&args, &subargs) ||
!dbus_connection_send(dbus_conn, reply, NULL))
!dbus_connection_send(dbus, reply, NULL))
{
return false;
}
Expand All @@ -452,7 +450,7 @@ bool notify_GetServerInformation(DBusConnection *dbus, DBusMessage *msg) {
!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &info[1]) ||
!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &info[2]) ||
!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &info[3]) ||
!dbus_connection_send(dbus_conn, reply, NULL))
!dbus_connection_send(dbus, reply, NULL))
{
return false;
}
Expand Down
9 changes: 6 additions & 3 deletions notify.h
Expand Up @@ -31,10 +31,13 @@ typedef struct _notification {
} notification;

// initialize notifications
bool notify_init(bool const debug_enabled);
DBusConnection* notify_init(bool const debug_enabled);

// returns the first current notification into status (number of total messages in n)
notification *notify_get_message(int *n);
notification* notify_get_message(DBusConnection *dbus, int *n);

// calls the respective notify handler based on the message received
DBusHandlerResult notify_handle(DBusConnection *dbus, DBusMessage *msg, void *user_data);

// check the dbus for notifications (1=something happened, 0=nothing)
bool notify_check();
bool notify_check(DBusConnection *dbus);

0 comments on commit 64a6705

Please sign in to comment.