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. */
47 unsigned int enabled : 1; /**< True if timeout is active. */
51 * Creates a new DBusTimeout, enabled by default.
52 * @param interval the timeout interval in milliseconds.
53 * @param handler function to call when the timeout occurs.
54 * @param data data to pass to the handler
55 * @param free_data_function function to be called to free the data.
56 * @returns the new DBusTimeout object,
59 _dbus_timeout_new (int interval,
60 DBusTimeoutHandler handler,
62 DBusFreeFunction free_data_function)
66 timeout = dbus_new0 (DBusTimeout, 1);
67 timeout->refcount = 1;
68 timeout->interval = interval;
70 timeout->handler = handler;
71 timeout->handler_data = data;
72 timeout->free_handler_data_function = free_data_function;
74 timeout->enabled = TRUE;
80 * Increments the reference count of a DBusTimeout object.
82 * @param timeout the timeout object.
85 _dbus_timeout_ref (DBusTimeout *timeout)
87 timeout->refcount += 1;
91 * Decrements the reference count of a DBusTimeout object
92 * and finalizes the object if the count reaches zero.
94 * @param timeout the timeout object.
97 _dbus_timeout_unref (DBusTimeout *timeout)
99 _dbus_assert (timeout != NULL);
100 _dbus_assert (timeout->refcount > 0);
102 timeout->refcount -= 1;
103 if (timeout->refcount == 0)
105 dbus_timeout_set_data (timeout, NULL, NULL); /* call free_data_function */
107 if (timeout->free_handler_data_function)
108 (* timeout->free_handler_data_function) (timeout->handler_data);
115 * Changes the timeout interval. Note that you have to disable and
116 * re-enable the timeout using the timeout toggle function
117 * (_dbus_connection_toggle_timeout() etc.) to notify the application
120 * @param timeout the timeout
121 * @param interval the new interval
124 _dbus_timeout_set_interval (DBusTimeout *timeout,
127 timeout->interval = interval;
131 * Changes the timeout's enabled-ness. Note that you should use
132 * _dbus_connection_toggle_timeout() etc. instead, if
133 * the timeout is passed out to an application main loop.
134 * i.e. you can't use this function in the D-BUS library, it's
135 * only used in the message bus daemon implementation.
137 * @param timeout the timeout
138 * @param interval the new interval
141 _dbus_timeout_set_enabled (DBusTimeout *timeout,
144 timeout->enabled = enabled != FALSE;
149 * @typedef DBusTimeoutList
151 * Opaque data type representing a list of timeouts
152 * and a set of DBusAddTimeoutFunction/DBusRemoveTimeoutFunction.
153 * Automatically handles removing/re-adding timeouts
154 * when the DBusAddTimeoutFunction is updated or changed.
155 * Holds a reference count to each timeout.
160 * DBusTimeoutList implementation details. All fields
164 struct DBusTimeoutList
166 DBusList *timeouts; /**< Timeout objects. */
168 DBusAddTimeoutFunction add_timeout_function; /**< Callback for adding a timeout. */
169 DBusRemoveTimeoutFunction remove_timeout_function; /**< Callback for removing a timeout. */
170 DBusTimeoutToggledFunction timeout_toggled_function; /**< Callback when timeout is enabled/disabled or changes interval */
171 void *timeout_data; /**< Data for timeout callbacks */
172 DBusFreeFunction timeout_free_data_function; /**< Free function for timeout callback data */
176 * Creates a new timeout list. Returns #NULL if insufficient
179 * @returns the new timeout list, or #NULL on failure.
182 _dbus_timeout_list_new (void)
184 DBusTimeoutList *timeout_list;
186 timeout_list = dbus_new0 (DBusTimeoutList, 1);
187 if (timeout_list == NULL)
194 * Frees a DBusTimeoutList.
196 * @param timeout_list the timeout list.
199 _dbus_timeout_list_free (DBusTimeoutList *timeout_list)
201 /* free timeout_data and remove timeouts as a side effect */
202 _dbus_timeout_list_set_functions (timeout_list,
203 NULL, NULL, NULL, NULL, NULL);
205 _dbus_list_foreach (&timeout_list->timeouts,
206 (DBusForeachFunction) _dbus_timeout_unref,
208 _dbus_list_clear (&timeout_list->timeouts);
210 dbus_free (timeout_list);
214 * Sets the timeout functions. This function is the "backend"
215 * for dbus_connection_set_timeout_functions().
217 * @param timeout_list the timeout list
218 * @param add_function the add timeout function.
219 * @param remove_function the remove timeout function.
220 * @param toggled_function toggle notify function, or #NULL
221 * @param data the data for those functions.
222 * @param free_data_function the function to free the data.
223 * @returns #FALSE if no memory
227 _dbus_timeout_list_set_functions (DBusTimeoutList *timeout_list,
228 DBusAddTimeoutFunction add_function,
229 DBusRemoveTimeoutFunction remove_function,
230 DBusTimeoutToggledFunction toggled_function,
232 DBusFreeFunction free_data_function)
234 /* Add timeouts with the new function, failing on OOM */
235 if (add_function != NULL)
239 link = _dbus_list_get_first_link (&timeout_list->timeouts);
242 DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
245 if (!(* add_function) (link->data, data))
247 /* remove it all again and return FALSE */
250 link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
251 while (link2 != link)
253 DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
256 (* remove_function) (link2->data, data);
268 /* Remove all current timeouts from previous timeout handlers */
270 if (timeout_list->remove_timeout_function != NULL)
272 _dbus_list_foreach (&timeout_list->timeouts,
273 (DBusForeachFunction) timeout_list->remove_timeout_function,
274 timeout_list->timeout_data);
277 if (timeout_list->timeout_free_data_function != NULL)
278 (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
280 timeout_list->add_timeout_function = add_function;
281 timeout_list->remove_timeout_function = remove_function;
282 timeout_list->timeout_toggled_function = toggled_function;
283 timeout_list->timeout_data = data;
284 timeout_list->timeout_free_data_function = free_data_function;
290 * Adds a new timeout to the timeout list, invoking the
291 * application DBusAddTimeoutFunction if appropriate.
293 * @param timeout_list the timeout list.
294 * @param timeout the timeout to add.
295 * @returns #TRUE on success, #FALSE If no memory.
298 _dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
299 DBusTimeout *timeout)
301 if (!_dbus_list_append (&timeout_list->timeouts, timeout))
304 _dbus_timeout_ref (timeout);
306 if (timeout_list->add_timeout_function != NULL)
308 if (!(* timeout_list->add_timeout_function) (timeout,
309 timeout_list->timeout_data))
311 _dbus_list_remove_last (&timeout_list->timeouts, timeout);
312 _dbus_timeout_unref (timeout);
321 * Removes a timeout from the timeout list, invoking the
322 * application's DBusRemoveTimeoutFunction if appropriate.
324 * @param timeout_list the timeout list.
325 * @param timeout the timeout to remove.
328 _dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
329 DBusTimeout *timeout)
331 if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
332 _dbus_assert_not_reached ("Nonexistent timeout was removed");
334 if (timeout_list->remove_timeout_function != NULL)
335 (* timeout_list->remove_timeout_function) (timeout,
336 timeout_list->timeout_data);
338 _dbus_timeout_unref (timeout);
342 * Sets a timeout to the given enabled state, invoking the
343 * application's DBusTimeoutToggledFunction if appropriate.
345 * @param timeout_list the timeout list.
346 * @param timeout the timeout to toggle.
347 * @param enabled #TRUE to enable
350 _dbus_timeout_list_toggle_timeout (DBusTimeoutList *timeout_list,
351 DBusTimeout *timeout,
356 if (enabled == timeout->enabled)
359 timeout->enabled = enabled;
361 if (timeout_list->timeout_toggled_function != NULL)
362 (* timeout_list->timeout_toggled_function) (timeout,
363 timeout_list->timeout_data);
369 * @defgroup DBusTimeout DBusTimeout
371 * @brief Object representing a timeout
373 * Types and functions related to DBusTimeout. A timeout
374 * represents a timeout that the main loop needs to monitor,
375 * as in Qt's QTimer or GLib's g_timeout_add().
382 * @typedef DBusTimeout
384 * Opaque object representing a timeout.
388 * Gets the timeout interval. The dbus_timeout_handle()
389 * should be called each time this interval elapses,
390 * starting after it elapses once.
392 * The interval may change during the life of the
393 * timeout; if so, the timeout will be disabled and
394 * re-enabled (calling the "timeout toggled function")
395 * to notify you of the change.
397 * @param timeout the DBusTimeout object.
398 * @returns the interval in milliseconds.
401 dbus_timeout_get_interval (DBusTimeout *timeout)
403 return timeout->interval;
407 * Gets data previously set with dbus_timeout_set_data()
410 * @param timeout the DBusTimeout object.
411 * @returns previously-set data.
414 dbus_timeout_get_data (DBusTimeout *timeout)
416 return timeout->data;
420 * Sets data which can be retrieved with dbus_timeout_get_data().
421 * Intended for use by the DBusAddTimeoutFunction and
422 * DBusRemoveTimeoutFunction to store their own data. For example with
423 * Qt you might store the QTimer for this timeout and with GLib
424 * you might store a g_timeout_add result id.
426 * @param timeout the DBusTimeout object.
427 * @param data the data.
428 * @param free_data_function function to be called to free the data.
431 dbus_timeout_set_data (DBusTimeout *timeout,
433 DBusFreeFunction free_data_function)
435 if (timeout->free_data_function != NULL)
436 (* timeout->free_data_function) (timeout->data);
438 timeout->data = data;
439 timeout->free_data_function = free_data_function;
443 * Calls the timeout handler for this timeout.
444 * This function should be called when the timeout
447 * If this function returns #FALSE, then there wasn't
448 * enough memory to handle the timeout. Typically just
449 * letting the timeout fire again next time it naturally
450 * times out is an adequate response to that problem,
451 * but you could try to do more if you wanted.
453 * @param timeout the DBusTimeout object.
454 * @returns #FALSE if there wasn't enough memory
457 dbus_timeout_handle (DBusTimeout *timeout)
459 return (* timeout->handler) (timeout->handler_data);
464 * Returns whether a timeout is enabled or not. If not
465 * enabled, it should not be polled by the main loop.
467 * @param timeout the DBusTimeout object
468 * @returns #TRUE if the timeout is enabled
471 dbus_timeout_get_enabled (DBusTimeout *timeout)
473 return timeout->enabled;
476 /** @} end public API docs */