a77363be1556b550b9ce093306f129515e03274d
[platform/upstream/dbus.git] / dbus / dbus-timeout.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-timeout.c DBusTimeout implementation
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include "dbus-internals.h"
25 #include "dbus-timeout.h"
26 #include "dbus-list.h"
27
28 /**
29  * @defgroup DBusTimeoutInternals DBusTimeout implementation details
30  * @ingroup  DBusInternals
31  * @brief implementation details for DBusTimeout
32  * 
33  * @{
34  */
35
36 struct DBusTimeout
37 {
38   int refcount;                                /**< Reference count */
39   int interval;                                /**< Timeout interval in milliseconds. */
40
41   DBusTimeoutHandler handler;                  /**< Timeout handler. */
42   void *handler_data;                          /**< Timeout handler data. */
43   DBusFreeFunction free_handler_data_function; /**< Free the timeout handler data. */
44   
45   void *data;                                  /**< Application data. */
46   DBusFreeFunction free_data_function;         /**< Free the application data. */
47   unsigned int enabled : 1;                    /**< True if timeout is active. */
48 };
49
50 /**
51  * Creates a new DBusTimeout.
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,
57  */
58 DBusTimeout*
59 _dbus_timeout_new (int                 interval,
60                    DBusTimeoutHandler  handler,
61                    void               *data,
62                    DBusFreeFunction    free_data_function)
63 {
64   DBusTimeout *timeout;
65
66   timeout = dbus_new0 (DBusTimeout, 1);
67   timeout->refcount = 1;
68   timeout->interval = interval;
69
70   timeout->handler = handler;
71   timeout->handler_data = data;
72   timeout->free_handler_data_function = free_data_function;
73
74   timeout->enabled = TRUE;
75   
76   return timeout;
77 }
78
79 /**
80  * Increments the reference count of a DBusTimeout object.
81  *
82  * @param timeout the timeout object.
83  */
84 void
85 _dbus_timeout_ref (DBusTimeout *timeout)
86 {
87   timeout->refcount += 1;
88 }
89
90 /**
91  * Decrements the reference count of a DBusTimeout object
92  * and finalizes the object if the count reaches zero.
93  *
94  * @param timeout the timeout object.
95  */
96 void
97 _dbus_timeout_unref (DBusTimeout *timeout)
98 {
99   _dbus_assert (timeout != NULL);
100   _dbus_assert (timeout->refcount > 0);
101   
102   timeout->refcount -= 1;
103   if (timeout->refcount == 0)
104     {
105       dbus_timeout_set_data (timeout, NULL, NULL); /* call free_data_function */
106
107       if (timeout->free_handler_data_function)
108         (* timeout->free_handler_data_function) (timeout->handler_data);
109       
110       dbus_free (timeout);
111     }
112 }
113
114 /**
115  * @typedef DBusTimeoutList
116  *
117  * Opaque data type representing a list of timeouts
118  * and a set of DBusAddTimeoutFunction/DBusRemoveTimeoutFunction.
119  * Automatically handles removing/re-adding timeouts
120  * when the DBusAddTimeoutFunction is updated or changed.
121  * Holds a reference count to each timeout.
122  *
123  */
124
125 /**
126  * DBusTimeoutList implementation details. All fields
127  * are private.
128  *
129  */
130 struct DBusTimeoutList
131 {
132   DBusList *timeouts; /**< Timeout objects. */
133
134   DBusAddTimeoutFunction add_timeout_function;       /**< Callback for adding a timeout. */
135   DBusRemoveTimeoutFunction remove_timeout_function; /**< Callback for removing a timeout. */
136   DBusTimeoutToggledFunction timeout_toggled_function; /**< Callback when timeout is enabled/disabled */
137   void *timeout_data;                                /**< Data for timeout callbacks */
138   DBusFreeFunction timeout_free_data_function;       /**< Free function for timeout callback data */
139 };
140
141 /**
142  * Creates a new timeout list. Returns #NULL if insufficient
143  * memory exists.
144  *
145  * @returns the new timeout list, or #NULL on failure.
146  */
147 DBusTimeoutList*
148 _dbus_timeout_list_new (void)
149 {
150   DBusTimeoutList *timeout_list;
151
152   timeout_list = dbus_new0 (DBusTimeoutList, 1);
153   if (timeout_list == NULL)
154     return NULL;
155
156   return timeout_list;
157 }
158
159 /**
160  * Frees a DBusTimeoutList.
161  *
162  * @param timeout_list the timeout list.
163  */
164 void
165 _dbus_timeout_list_free (DBusTimeoutList *timeout_list)
166 {
167   /* free timeout_data and remove timeouts as a side effect */
168   _dbus_timeout_list_set_functions (timeout_list,
169                                     NULL, NULL, NULL, NULL, NULL);
170
171   _dbus_list_foreach (&timeout_list->timeouts,
172                       (DBusForeachFunction) _dbus_timeout_unref,
173                       NULL);
174   _dbus_list_clear (&timeout_list->timeouts);
175
176   dbus_free (timeout_list);
177 }
178
179 /**
180  * Sets the timeout functions. This function is the "backend"
181  * for dbus_connection_set_timeout_functions().
182  *
183  * @param timeout_list the timeout list
184  * @param add_function the add timeout function.
185  * @param remove_function the remove timeout function.
186  * @param toggled_function toggle notify function, or #NULL
187  * @param data the data for those functions.
188  * @param free_data_function the function to free the data.
189  * @returns #FALSE if no memory
190  *
191  */
192 dbus_bool_t
193 _dbus_timeout_list_set_functions (DBusTimeoutList           *timeout_list,
194                                   DBusAddTimeoutFunction     add_function,
195                                   DBusRemoveTimeoutFunction  remove_function,
196                                   DBusTimeoutToggledFunction toggled_function,
197                                   void                      *data,
198                                   DBusFreeFunction           free_data_function)
199 {
200   /* Add timeouts with the new function, failing on OOM */
201   if (add_function != NULL)
202     {
203       DBusList *link;
204       
205       link = _dbus_list_get_first_link (&timeout_list->timeouts);
206       while (link != NULL)
207         {
208           DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
209                                                      link);
210       
211           if (!(* add_function) (link->data, data))
212             {
213               /* remove it all again and return FALSE */
214               DBusList *link2;
215               
216               link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
217               while (link2 != link)
218                 {
219                   DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
220                                                              link2);
221
222                   (* remove_function) (link2->data, data);
223                   
224                   link2 = next;
225                 }
226
227               return FALSE;
228             }
229       
230           link = next;
231         }
232     }
233   
234   /* Remove all current timeouts from previous timeout handlers */
235
236   if (timeout_list->remove_timeout_function != NULL)
237     {
238       _dbus_list_foreach (&timeout_list->timeouts,
239                           (DBusForeachFunction) timeout_list->remove_timeout_function,
240                           timeout_list->timeout_data);
241     }
242
243   if (timeout_list->timeout_free_data_function != NULL)
244     (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
245
246   timeout_list->add_timeout_function = add_function;
247   timeout_list->remove_timeout_function = remove_function;
248   timeout_list->timeout_toggled_function = toggled_function;
249   timeout_list->timeout_data = data;
250   timeout_list->timeout_free_data_function = free_data_function;
251
252   return TRUE;
253 }
254
255 /**
256  * Adds a new timeout to the timeout list, invoking the
257  * application DBusAddTimeoutFunction if appropriate.
258  *
259  * @param timeout_list the timeout list.
260  * @param timeout the timeout to add.
261  * @returns #TRUE on success, #FALSE If no memory.
262  */
263 dbus_bool_t
264 _dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
265                                 DBusTimeout     *timeout)
266 {
267   if (!_dbus_list_append (&timeout_list->timeouts, timeout))
268     return FALSE;
269
270   _dbus_timeout_ref (timeout);
271
272   if (timeout_list->add_timeout_function != NULL)
273     {
274       if (!(* timeout_list->add_timeout_function) (timeout,
275                                                    timeout_list->timeout_data))
276         {
277           _dbus_list_remove_last (&timeout_list->timeouts, timeout);
278           _dbus_timeout_unref (timeout);
279           return FALSE;
280         }
281     }
282
283   return TRUE;
284 }
285
286 /**
287  * Removes a timeout from the timeout list, invoking the
288  * application's DBusRemoveTimeoutFunction if appropriate.
289  *
290  * @param timeout_list the timeout list.
291  * @param timeout the timeout to remove.
292  */
293 void
294 _dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
295                                    DBusTimeout     *timeout)
296 {
297   if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
298     _dbus_assert_not_reached ("Nonexistent timeout was removed");
299
300   if (timeout_list->remove_timeout_function != NULL)
301     (* timeout_list->remove_timeout_function) (timeout,
302                                                timeout_list->timeout_data);
303
304   _dbus_timeout_unref (timeout);
305 }
306
307 /**
308  * Sets a timeout to the given enabled state, invoking the
309  * application's DBusTimeoutToggledFunction if appropriate.
310  *
311  * @param timeout_list the timeout list.
312  * @param timeout the timeout to toggle.
313  * @param enabled #TRUE to enable
314  */
315 void
316 _dbus_timeout_list_toggle_timeout (DBusTimeoutList           *timeout_list,
317                                    DBusTimeout               *timeout,
318                                    dbus_bool_t                enabled)
319 {
320   enabled = !!enabled;
321   
322   if (enabled == timeout->enabled)
323     return;
324
325   timeout->enabled = enabled;
326   
327   if (timeout_list->timeout_toggled_function != NULL)
328     (* timeout_list->timeout_toggled_function) (timeout,
329                                                 timeout_list->timeout_data);
330 }
331
332 /** @} */
333
334 /**
335  * @defgroup DBusTimeout DBusTimeout
336  * @ingroup  DBus
337  * @brief Object representing a timeout
338  *
339  * Types and functions related to DBusTimeout. A timeout
340  * represents a timeout that the main loop needs to monitor,
341  * as in Qt's QTimer or GLib's g_timeout_add().
342  * 
343  * @{
344  */
345
346
347 /**
348  * @typedef DBusTimeout
349  *
350  * Opaque object representing a timeout.
351  */
352
353 /**
354  * Gets the timeout interval. The dbus_timeout_handle()
355  * should be called each time this interval elapses,
356  * starting after it elapses once.
357  *
358  * @param timeout the DBusTimeout object.
359  * @returns the interval in milliseconds.
360  */
361 int
362 dbus_timeout_get_interval (DBusTimeout *timeout)
363 {
364   return timeout->interval;
365 }
366
367 /**
368  * Gets data previously set with dbus_timeout_set_data()
369  * or #NULL if none.
370  *
371  * @param timeout the DBusTimeout object.
372  * @returns previously-set data.
373  */
374 void*
375 dbus_timeout_get_data (DBusTimeout *timeout)
376 {
377   return timeout->data;
378 }
379
380 /**
381  * Sets data which can be retrieved with dbus_timeout_get_data().
382  * Intended for use by the DBusAddTimeoutFunction and
383  * DBusRemoveTimeoutFunction to store their own data.  For example with
384  * Qt you might store the QTimer for this timeout and with GLib
385  * you might store a g_timeout_add result id.
386  *
387  * @param timeout the DBusTimeout object.
388  * @param data the data.
389  * @param free_data_function function to be called to free the data.
390  */
391 void
392 dbus_timeout_set_data (DBusTimeout      *timeout,
393                        void             *data,
394                        DBusFreeFunction  free_data_function)
395 {
396   if (timeout->free_data_function != NULL)
397     (* timeout->free_data_function) (timeout->data);
398
399   timeout->data = data;
400   timeout->free_data_function = free_data_function;
401 }
402
403 /**
404  * Calls the timeout handler for this timeout.
405  * This function should be called when the timeout
406  * occurs.
407  *
408  * If this function returns #FALSE, then there wasn't
409  * enough memory to handle the timeout. Typically just
410  * letting the timeout fire again next time it naturally
411  * times out is an adequate response to that problem,
412  * but you could try to do more if you wanted.
413  *
414  * @param timeout the DBusTimeout object.
415  * @returns #FALSE if there wasn't enough memory 
416  */
417 dbus_bool_t
418 dbus_timeout_handle (DBusTimeout *timeout)
419 {
420   return (* timeout->handler) (timeout->handler_data);
421 }
422
423
424 /**
425  * Returns whether a timeout is enabled or not. If not
426  * enabled, it should not be polled by the main loop.
427  *
428  * @param timeout the DBusTimeout object
429  * @returns #TRUE if the timeout is enabled
430  */
431 dbus_bool_t
432 dbus_timeout_get_enabled (DBusTimeout *timeout)
433 {
434   return timeout->enabled;
435 }
436
437 /** @} end public API docs */