From 263d1dfdd40b87f62648c4c5c50e8a6472fd5322 Mon Sep 17 00:00:00 2001 From: "John (J5) Palmieri" Date: Wed, 30 Nov 2005 20:30:02 +0000 Subject: * dbus/dbus-connection.c (dbus_connection_read_write): Add new method for getting messages off the bus in the absence of a mainloop. This method is much like dbus_connection_read_write_dispatch except it does not dispatch the messages to a registered filter function. Instead it allows a developer to process messages by directly popping them off the bus. --- ChangeLog | 10 +++++++ dbus/dbus-connection.c | 80 +++++++++++++++++++++++++++++++++++++++++++++----- dbus/dbus-connection.h | 2 ++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8b5b184b..a9c39e92 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2005-11-30 John (J5) Palmieri + + * dbus/dbus-connection.c (dbus_connection_read_write): Add new + method for getting messages off the bus in the absence of a + mainloop. This method is much like + dbus_connection_read_write_dispatch except it does not dispatch + the messages to a registered filter function. Instead it + allows a developer to process messages by directly popping + them off the bus. + 2005-11-30 John (J5) Palmieri * bus/desktop-file.c (parse_key_value): Ignore locales allowing diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 3c8c765d..0c9fe28d 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -2847,11 +2847,11 @@ dbus_connection_flush (DBusConnection *connection) * In this usage you would normally have set up a filter function to look * at each message as it is dispatched. The loop terminates when the last * message from the connection (the disconnected signal) is processed. - * - * If there are messages to dispatch, this function will - * dbus_connection_dispatch() once, and return. If there are no - * messages to dispatch, this function will block until it can read or - * write, then read or write, then return. + * + * If there are messages to dispatch and the dispatch flag is set, this + * function will dbus_connection_dispatch() once, and return. If there are no + * messages to dispatch, this function will block until it can read or write, + * then read or write, then return. * * The way to think of this function is that it either makes some sort * of progress, or it blocks. @@ -2863,11 +2863,13 @@ dbus_connection_flush (DBusConnection *connection) * * @param connection the connection * @param timeout_milliseconds max time to block or -1 for infinite + * @param dispatch dispatch new messages or leave them on the incoming queue * @returns #TRUE if the disconnect message has not been processed */ dbus_bool_t -dbus_connection_read_write_dispatch (DBusConnection *connection, - int timeout_milliseconds) +_dbus_connection_read_write_dispatch (DBusConnection *connection, + int timeout_milliseconds, + dbus_bool_t dispatch) { DBusDispatchStatus dstatus; dbus_bool_t dispatched_disconnected; @@ -2876,7 +2878,7 @@ dbus_connection_read_write_dispatch (DBusConnection *connection, _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); dstatus = dbus_connection_get_dispatch_status (connection); - if (dstatus == DBUS_DISPATCH_DATA_REMAINS) + if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS) { _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME); dbus_connection_dispatch (connection); @@ -2909,6 +2911,68 @@ dbus_connection_read_write_dispatch (DBusConnection *connection, return !dispatched_disconnected; /* TRUE if we have not processed disconnected */ } + +/** + * This function is intended for use with applications that don't want + * to write a main loop and deal with #DBusWatch and #DBusTimeout. An + * example usage would be: + * + * @code + * while (dbus_connection_read_write_dispatch (connection, -1)) + * ; // empty loop body + * @endcode + * + * In this usage you would normally have set up a filter function to look + * at each message as it is dispatched. The loop terminates when the last + * message from the connection (the disconnected signal) is processed. + * + * If there are messages to dispatch, this function will + * dbus_connection_dispatch() once, and return. If there are no + * messages to dispatch, this function will block until it can read or + * write, then read or write, then return. + * + * The way to think of this function is that it either makes some sort + * of progress, or it blocks. + * + * The return value indicates whether the disconnect message has been + * processed, NOT whether the connection is connected. This is + * important because even after disconnecting, you want to process any + * messages you received prior to the disconnect. + * + * @param connection the connection + * @param timeout_milliseconds max time to block or -1 for infinite + * @returns #TRUE if the disconnect message has not been processed + */ +dbus_bool_t +dbus_connection_read_write_dispatch (DBusConnection *connection, + int timeout_milliseconds) +{ + return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE); +} + +/** + * This function is intended for use with applications that don't want to + * write a main loop and deal with #DBusWatch and #DBusTimeout. + * + * If there are no messages to dispatch, this function will block until it can + * read or write, then read or write, then return. + * + * The return value indicates whether the disconnect message has been + * processed, NOT whether the connection is connected. This is important + * because even after disconnecting, you want to process any messages you + * received prior to the disconnect. + * + * @param connection the connection + * @param timeout_milliseconds max time to block or -1 for infinite + * @returns #TRUE if the disconnect message has not been processed + */ +dbus_bool_t +dbus_connection_read_write (DBusConnection *connection, + int timeout_milliseconds) +{ + return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE); +} + /** * Returns the first-received message from the incoming message queue, * leaving it in the queue. If the queue is empty, returns #NULL. diff --git a/dbus/dbus-connection.h b/dbus/dbus-connection.h index 70369d3f..9784f260 100644 --- a/dbus/dbus-connection.h +++ b/dbus/dbus-connection.h @@ -102,6 +102,8 @@ void dbus_connection_set_exit_on_disconnect (DBusConnection void dbus_connection_flush (DBusConnection *connection); dbus_bool_t dbus_connection_read_write_dispatch (DBusConnection *connection, int timeout_milliseconds); +dbus_bool_t dbus_connection_read_write (DBusConnection *connection, + int timeout_milliseconds); DBusMessage* dbus_connection_borrow_message (DBusConnection *connection); void dbus_connection_return_message (DBusConnection *connection, DBusMessage *message); -- cgit