1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-timeout.c DBusTimeout implementation
4 * Copyright (C) 2003 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-timeout.h"
26 #include "dbus-list.h"
29 * @defgroup DBusTimeoutInternals DBusTimeout implementation details
30 * @ingroup DBusInternals
31 * @brief implementation details for DBusTimeout
38 int refcount; /**< Reference count */
39 int interval; /**< Timeout interval in milliseconds. */
41 DBusTimeoutHandler handler; /**< Timeout handler. */
42 void *handler_data; /**< Timeout handler data. */
43 DBusFreeFunction free_handler_data_function; /**< Free the timeout handler data. */
45 void *data; /**< Application data. */
46 DBusFreeFunction free_data_function; /**< Free the application data. */
50 * Creates a new DBusTimeout.
51 * @param interval the timeout interval in milliseconds.
52 * @param handler function to call when the timeout occurs.
53 * @param data data to pass to the handler
54 * @param free_data_function function to be called to free the data.
55 * @returns the new DBusTimeout object,
58 _dbus_timeout_new (int interval,
59 DBusTimeoutHandler handler,
61 DBusFreeFunction free_data_function)
65 timeout = dbus_new0 (DBusTimeout, 1);
66 timeout->refcount = 1;
67 timeout->interval = interval;
69 timeout->handler = handler;
70 timeout->handler_data = data;
71 timeout->free_handler_data_function = free_data_function;
77 * Increments the reference count of a DBusTimeout object.
79 * @param timeout the timeout object.
82 _dbus_timeout_ref (DBusTimeout *timeout)
84 timeout->refcount += 1;
88 * Decrements the reference count of a DBusTimeout object
89 * and finalizes the object if the count reaches zero.
91 * @param timeout the timeout object.
94 _dbus_timeout_unref (DBusTimeout *timeout)
96 _dbus_assert (timeout != NULL);
97 _dbus_assert (timeout->refcount > 0);
99 timeout->refcount -= 1;
100 if (timeout->refcount == 0)
102 dbus_timeout_set_data (timeout, NULL, NULL); /* call free_data_function */
104 if (timeout->free_handler_data_function)
105 (* timeout->free_handler_data_function) (timeout->handler_data);
112 * @typedef DBusTimeoutList
114 * Opaque data type representing a list of timeouts
115 * and a set of DBusAddTimeoutFunction/DBusRemoveTimeoutFunction.
116 * Automatically handles removing/re-adding timeouts
117 * when the DBusAddTimeoutFunction is updated or changed.
118 * Holds a reference count to each timeout.
123 * DBusTimeoutList implementation details. All fields
127 struct DBusTimeoutList
129 DBusList *timeouts; /**< Timeout objects. */
131 DBusAddTimeoutFunction add_timeout_function; /**< Callback for adding a timeout. */
132 DBusRemoveTimeoutFunction remove_timeout_function; /**< Callback for removing a timeout. */
133 void *timeout_data; /**< Data for timeout callbacks */
134 DBusFreeFunction timeout_free_data_function; /**< Free function for timeout callback data */
138 * Creates a new timeout list. Returns #NULL if insufficient
141 * @returns the new timeout list, or #NULL on failure.
144 _dbus_timeout_list_new (void)
146 DBusTimeoutList *timeout_list;
148 timeout_list = dbus_new0 (DBusTimeoutList, 1);
149 if (timeout_list == NULL)
156 * Frees a DBusTimeoutList.
158 * @param timeout_list the timeout list.
161 _dbus_timeout_list_free (DBusTimeoutList *timeout_list)
163 /* free timeout_data and remove timeouts as a side effect */
164 _dbus_timeout_list_set_functions (timeout_list,
165 NULL, NULL, NULL, NULL);
167 _dbus_list_foreach (&timeout_list->timeouts,
168 (DBusForeachFunction) _dbus_timeout_unref,
170 _dbus_list_clear (&timeout_list->timeouts);
172 dbus_free (timeout_list);
176 * Sets the timeout functions. This function is the "backend"
177 * for dbus_connection_set_timeout_functions().
179 * @param timeout_list the timeout list
180 * @param add_function the add timeout function.
181 * @param remove_function the remove timeout function.
182 * @param data the data for those functions.
183 * @param free_data_function the function to free the data.
187 _dbus_timeout_list_set_functions (DBusTimeoutList *timeout_list,
188 DBusAddTimeoutFunction add_function,
189 DBusRemoveTimeoutFunction remove_function,
191 DBusFreeFunction free_data_function)
193 /* Remove all current timeouts from previous timeout handlers */
195 if (timeout_list->remove_timeout_function != NULL)
197 _dbus_list_foreach (&timeout_list->timeouts,
198 (DBusForeachFunction) timeout_list->remove_timeout_function,
199 timeout_list->timeout_data);
202 if (timeout_list->timeout_free_data_function != NULL)
203 (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
205 timeout_list->add_timeout_function = add_function;
206 timeout_list->remove_timeout_function = remove_function;
207 timeout_list->timeout_data = data;
208 timeout_list->timeout_free_data_function = free_data_function;
210 /* Re-add all pending timeouts */
211 if (timeout_list->add_timeout_function != NULL)
213 _dbus_list_foreach (&timeout_list->timeouts,
214 (DBusForeachFunction) timeout_list->add_timeout_function,
215 timeout_list->timeout_data);
220 * Adds a new timeout to the timeout list, invoking the
221 * application DBusAddTimeoutFunction if appropriate.
223 * @param timeout_list the timeout list.
224 * @param timeout the timeout to add.
225 * @returns #TRUE on success, #FALSE If no memory.
228 _dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
229 DBusTimeout *timeout)
231 if (!_dbus_list_append (&timeout_list->timeouts, timeout))
234 _dbus_timeout_ref (timeout);
236 if (timeout_list->add_timeout_function != NULL)
237 (* timeout_list->add_timeout_function) (timeout,
238 timeout_list->timeout_data);
244 * Removes a timeout from the watch list, invoking the
245 * application's DBusRemoveTimeoutFunction if appropriate.
247 * @param timeout_list the timeout list.
248 * @param timeout the timeout to remove.
251 _dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
252 DBusTimeout *timeout)
254 if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
255 _dbus_assert_not_reached ("Nonexistent timeout was removed");
257 if (timeout_list->remove_timeout_function != NULL)
258 (* timeout_list->remove_timeout_function) (timeout,
259 timeout_list->timeout_data);
261 _dbus_timeout_unref (timeout);
267 * @defgroup DBusTimeout DBusTimeout
269 * @brief Object representing a timeout
271 * Types and functions related to DBusTimeout. A timeout
272 * represents a timeout that the main loop needs to monitor,
273 * as in Qt's QTimer or GLib's g_timeout_add().
280 * @typedef DBusTimeout
282 * Opaque object representing a timeout.
286 * Gets the timeout interval.
287 * @param timeout the DBusTimeout object.
288 * @returns the interval in milliseconds.
291 dbus_timeout_get_interval (DBusTimeout *timeout)
293 return timeout->interval;
297 * Gets data previously set with dbus_timeout_set_data()
300 * @param timeout the DBusTimeout object.
301 * @returns previously-set data.
304 dbus_timeout_get_data (DBusTimeout *timeout)
306 return timeout->data;
310 * Sets data which can be retrieved with dbus_timeout_get_data().
311 * Intended for use by the DBusAddTimeoutFunction and
312 * DBusRemoveTimeoutFunction to store their own data. For example with
313 * Qt you might store the QTimer for this timeout and with GLib
314 * you might store a g_timeout_add result id.
316 * @param timeout the DBusTimeout object.
317 * @param data the data.
318 * @param free_data_function function to be called to free the data.
321 dbus_timeout_set_data (DBusTimeout *timeout,
323 DBusFreeFunction free_data_function)
325 if (timeout->free_data_function != NULL)
326 (* timeout->free_data_function) (timeout->data);
328 timeout->data = data;
329 timeout->free_data_function = free_data_function;
333 * Calls the timeout handler for this timeout.
334 * This function should be called when the timeout
337 * @param timeout the DBusTimeout object.
340 dbus_timeout_handle (DBusTimeout *timeout)
342 (* timeout->handler) (timeout->handler_data);