daemon fix and development
[platform/upstream/dbus.git] / dbus / dbus-timeout.c
index 9aefbb4..a1d6ce5 100644 (file)
@@ -1,9 +1,9 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-timeout.c DBusTimeout implementation
  *
  * Copyright (C) 2003  CodeFactory AB
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
 #include "dbus-internals.h"
 #include "dbus-timeout.h"
 #include "dbus-list.h"
@@ -33,6 +34,9 @@
  * @{
  */
 
+/**
+ * Internals of DBusTimeout
+ */
 struct DBusTimeout
 {
   int refcount;                                /**< Reference count */
@@ -44,10 +48,11 @@ struct DBusTimeout
   
   void *data;                                 /**< Application data. */
   DBusFreeFunction free_data_function;         /**< Free the application data. */
+  unsigned int enabled : 1;                    /**< True if timeout is active. */
 };
 
 /**
- * Creates a new DBusTimeout.
+ * Creates a new DBusTimeout, enabled by default.
  * @param interval the timeout interval in milliseconds.
  * @param handler function to call when the timeout occurs.
  * @param data data to pass to the handler
@@ -63,11 +68,17 @@ _dbus_timeout_new (int                 interval,
   DBusTimeout *timeout;
 
   timeout = dbus_new0 (DBusTimeout, 1);
+  if (timeout == NULL)
+    return NULL;
+  
   timeout->refcount = 1;
   timeout->interval = interval;
 
+  timeout->handler = handler;
   timeout->handler_data = data;
   timeout->free_handler_data_function = free_data_function;
+
+  timeout->enabled = TRUE;
   
   return timeout;
 }
@@ -76,11 +87,14 @@ _dbus_timeout_new (int                 interval,
  * Increments the reference count of a DBusTimeout object.
  *
  * @param timeout the timeout object.
+ * @returns the timeout object.
  */
-void
+DBusTimeout *
 _dbus_timeout_ref (DBusTimeout *timeout)
 {
   timeout->refcount += 1;
+
+  return timeout;
 }
 
 /**
@@ -108,6 +122,42 @@ _dbus_timeout_unref (DBusTimeout *timeout)
 }
 
 /**
+ * Changes the timeout interval. Note that you have to disable and
+ * re-enable the timeout using the timeout toggle function
+ * (_dbus_connection_toggle_timeout_unlocked() etc.) to notify the
+ * application of this change.
+ *
+ * @param timeout the timeout
+ * @param interval the new interval
+ */
+void
+_dbus_timeout_set_interval (DBusTimeout *timeout,
+                            int          interval)
+{
+  _dbus_assert (interval >= 0);
+  
+  timeout->interval = interval;
+}
+
+/**
+ * Changes the timeout's enabled-ness. Note that you should use
+ * _dbus_connection_toggle_timeout_unlocked() etc. instead, if
+ * the timeout is passed out to an application main loop.
+ * i.e. you can't use this function in the D-Bus library, it's
+ * only used in the message bus daemon implementation.
+ *
+ * @param timeout the timeout
+ * @param enabled #TRUE if timeout should be enabled.
+ */
+void
+_dbus_timeout_set_enabled (DBusTimeout  *timeout,
+                           dbus_bool_t   enabled)
+{
+  timeout->enabled = enabled != FALSE;
+}
+
+
+/**
  * @typedef DBusTimeoutList
  *
  * Opaque data type representing a list of timeouts
@@ -129,6 +179,7 @@ struct DBusTimeoutList
 
   DBusAddTimeoutFunction add_timeout_function;       /**< Callback for adding a timeout. */
   DBusRemoveTimeoutFunction remove_timeout_function; /**< Callback for removing a timeout. */
+  DBusTimeoutToggledFunction timeout_toggled_function; /**< Callback when timeout is enabled/disabled or changes interval */
   void *timeout_data;                                /**< Data for timeout callbacks */
   DBusFreeFunction timeout_free_data_function;       /**< Free function for timeout callback data */
 };
@@ -161,7 +212,7 @@ _dbus_timeout_list_free (DBusTimeoutList *timeout_list)
 {
   /* free timeout_data and remove timeouts as a side effect */
   _dbus_timeout_list_set_functions (timeout_list,
-                                   NULL, NULL, NULL, NULL);
+                                   NULL, NULL, NULL, NULL, NULL);
 
   _dbus_list_foreach (&timeout_list->timeouts,
                      (DBusForeachFunction) _dbus_timeout_unref,
@@ -178,17 +229,54 @@ _dbus_timeout_list_free (DBusTimeoutList *timeout_list)
  * @param timeout_list the timeout list
  * @param add_function the add timeout function.
  * @param remove_function the remove timeout function.
+ * @param toggled_function toggle notify function, or #NULL
  * @param data the data for those functions.
  * @param free_data_function the function to free the data.
+ * @returns #FALSE if no memory
  *
  */
-void
+dbus_bool_t
 _dbus_timeout_list_set_functions (DBusTimeoutList           *timeout_list,
                                  DBusAddTimeoutFunction     add_function,
                                  DBusRemoveTimeoutFunction  remove_function,
+                                  DBusTimeoutToggledFunction toggled_function,
                                  void                      *data,
                                  DBusFreeFunction           free_data_function)
 {
+  /* Add timeouts with the new function, failing on OOM */
+  if (add_function != NULL)
+    {
+      DBusList *link;
+      
+      link = _dbus_list_get_first_link (&timeout_list->timeouts);
+      while (link != NULL)
+        {
+          DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
+                                                     link);
+      
+          if (!(* add_function) (link->data, data))
+            {
+              /* remove it all again and return FALSE */
+              DBusList *link2;
+              
+              link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
+              while (link2 != link)
+                {
+                  DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
+                                                             link2);
+
+                  (* remove_function) (link2->data, data);
+                  
+                  link2 = next;
+                }
+
+              return FALSE;
+            }
+      
+          link = next;
+        }
+    }
+  
   /* Remove all current timeouts from previous timeout handlers */
 
   if (timeout_list->remove_timeout_function != NULL)
@@ -203,16 +291,11 @@ _dbus_timeout_list_set_functions (DBusTimeoutList           *timeout_list,
 
   timeout_list->add_timeout_function = add_function;
   timeout_list->remove_timeout_function = remove_function;
+  timeout_list->timeout_toggled_function = toggled_function;
   timeout_list->timeout_data = data;
   timeout_list->timeout_free_data_function = free_data_function;
 
-  /* Re-add all pending timeouts */
-  if (timeout_list->add_timeout_function != NULL)
-    {
-      _dbus_list_foreach (&timeout_list->timeouts,
-                         (DBusForeachFunction) timeout_list->add_timeout_function,
-                         timeout_list->timeout_data);
-    }
+  return TRUE;
 }
 
 /**
@@ -233,14 +316,21 @@ _dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
   _dbus_timeout_ref (timeout);
 
   if (timeout_list->add_timeout_function != NULL)
-    (* timeout_list->add_timeout_function) (timeout,
-                                           timeout_list->timeout_data);
+    {
+      if (!(* timeout_list->add_timeout_function) (timeout,
+                                                   timeout_list->timeout_data))
+        {
+          _dbus_list_remove_last (&timeout_list->timeouts, timeout);
+          _dbus_timeout_unref (timeout);
+          return FALSE;
+        }
+    }
 
   return TRUE;
 }
 
 /**
- * Removes a timeout from the watch list, invoking the
+ * Removes a timeout from the timeout list, invoking the
  * application's DBusRemoveTimeoutFunction if appropriate.
  *
  * @param timeout_list the timeout list.
@@ -260,6 +350,31 @@ _dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
   _dbus_timeout_unref (timeout);
 }
 
+/**
+ * Sets a timeout to the given enabled state, invoking the
+ * application's DBusTimeoutToggledFunction if appropriate.
+ *
+ * @param timeout_list the timeout list.
+ * @param timeout the timeout to toggle.
+ * @param enabled #TRUE to enable
+ */
+void
+_dbus_timeout_list_toggle_timeout (DBusTimeoutList           *timeout_list,
+                                   DBusTimeout               *timeout,
+                                   dbus_bool_t                enabled)
+{
+  enabled = !!enabled;
+  
+  if (enabled == timeout->enabled)
+    return;
+
+  timeout->enabled = enabled;
+  
+  if (timeout_list->timeout_toggled_function != NULL)
+    (* timeout_list->timeout_toggled_function) (timeout,
+                                                timeout_list->timeout_data);
+}
+
 /** @} */
 
 /**
@@ -270,6 +385,9 @@ _dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
  * Types and functions related to DBusTimeout. A timeout
  * represents a timeout that the main loop needs to monitor,
  * as in Qt's QTimer or GLib's g_timeout_add().
+ *
+ * Use dbus_connection_set_timeout_functions() or dbus_server_set_timeout_functions()
+ * to be notified when libdbus needs to add or remove timeouts.
  * 
  * @{
  */
@@ -282,7 +400,15 @@ _dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
  */
 
 /**
- * Gets the timeout interval.
+ * Gets the timeout interval. The dbus_timeout_handle()
+ * should be called each time this interval elapses,
+ * starting after it elapses once.
+ *
+ * The interval may change during the life of the
+ * timeout; if so, the timeout will be disabled and
+ * re-enabled (calling the "timeout toggled function")
+ * to notify you of the change.
+ *
  * @param timeout the DBusTimeout object.
  * @returns the interval in milliseconds.
  */
@@ -333,10 +459,33 @@ dbus_timeout_set_data (DBusTimeout      *timeout,
  * This function should be called when the timeout
  * occurs.
  *
+ * If this function returns #FALSE, then there wasn't
+ * enough memory to handle the timeout. Typically just
+ * letting the timeout fire again next time it naturally
+ * times out is an adequate response to that problem,
+ * but you could try to do more if you wanted.
+ *
  * @param timeout the DBusTimeout object.
+ * @returns #FALSE if there wasn't enough memory 
  */
-void
+dbus_bool_t
 dbus_timeout_handle (DBusTimeout *timeout)
 {
-  (* timeout->handler) (timeout->handler_data);
+  return (* timeout->handler) (timeout->handler_data);
+}
+
+
+/**
+ * Returns whether a timeout is enabled or not. If not
+ * enabled, it should not be polled by the main loop.
+ *
+ * @param timeout the DBusTimeout object
+ * @returns #TRUE if the timeout is enabled
+ */
+dbus_bool_t
+dbus_timeout_get_enabled (DBusTimeout *timeout)
+{
+  return timeout->enabled;
 }
+
+/** @} end public API docs */