D-Bus  1.8.16
dbus-timeout.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-timeout.c DBusTimeout implementation
3  *
4  * Copyright (C) 2003 CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-timeout.h"
27 #include "dbus-list.h"
28 
41 {
42  int refcount;
43  int interval;
46  void *handler_data;
49  void *data;
51  unsigned int enabled : 1;
52 };
53 
63 _dbus_timeout_new (int interval,
64  DBusTimeoutHandler handler,
65  void *data,
66  DBusFreeFunction free_data_function)
67 {
68  DBusTimeout *timeout;
69 
70  timeout = dbus_new0 (DBusTimeout, 1);
71  if (timeout == NULL)
72  return NULL;
73 
74  timeout->refcount = 1;
75  timeout->interval = interval;
76 
77  timeout->handler = handler;
78  timeout->handler_data = data;
79  timeout->free_handler_data_function = free_data_function;
80 
81  timeout->enabled = TRUE;
82 
83  return timeout;
84 }
85 
94 {
95  timeout->refcount += 1;
96 
97  return timeout;
98 }
99 
106 void
108 {
109  _dbus_assert (timeout != NULL);
110  _dbus_assert (timeout->refcount > 0);
111 
112  timeout->refcount -= 1;
113  if (timeout->refcount == 0)
114  {
115  dbus_timeout_set_data (timeout, NULL, NULL); /* call free_data_function */
116 
117  if (timeout->free_handler_data_function)
118  (* timeout->free_handler_data_function) (timeout->handler_data);
119 
120  dbus_free (timeout);
121  }
122 }
123 
133 void
135  int interval)
136 {
137  _dbus_assert (interval >= 0);
138 
139  timeout->interval = interval;
140 }
141 
152 void
154  dbus_bool_t enabled)
155 {
156  timeout->enabled = enabled != FALSE;
157 }
158 
159 
177 {
183  void *timeout_data;
185 };
186 
195 {
196  DBusTimeoutList *timeout_list;
197 
198  timeout_list = dbus_new0 (DBusTimeoutList, 1);
199  if (timeout_list == NULL)
200  return NULL;
201 
202  return timeout_list;
203 }
204 
210 void
212 {
213  /* free timeout_data and remove timeouts as a side effect */
214  _dbus_timeout_list_set_functions (timeout_list,
215  NULL, NULL, NULL, NULL, NULL);
216 
217  _dbus_list_foreach (&timeout_list->timeouts,
219  NULL);
220  _dbus_list_clear (&timeout_list->timeouts);
221 
222  dbus_free (timeout_list);
223 }
224 
240  DBusAddTimeoutFunction add_function,
241  DBusRemoveTimeoutFunction remove_function,
242  DBusTimeoutToggledFunction toggled_function,
243  void *data,
244  DBusFreeFunction free_data_function)
245 {
246  /* Add timeouts with the new function, failing on OOM */
247  if (add_function != NULL)
248  {
249  DBusList *link;
250 
251  link = _dbus_list_get_first_link (&timeout_list->timeouts);
252  while (link != NULL)
253  {
254  DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
255  link);
256 
257  if (!(* add_function) (link->data, data))
258  {
259  /* remove it all again and return FALSE */
260  DBusList *link2;
261 
262  link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
263  while (link2 != link)
264  {
265  DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
266  link2);
267 
268  (* remove_function) (link2->data, data);
269 
270  link2 = next;
271  }
272 
273  return FALSE;
274  }
275 
276  link = next;
277  }
278  }
279 
280  /* Remove all current timeouts from previous timeout handlers */
281 
282  if (timeout_list->remove_timeout_function != NULL)
283  {
284  _dbus_list_foreach (&timeout_list->timeouts,
286  timeout_list->timeout_data);
287  }
288 
289  if (timeout_list->timeout_free_data_function != NULL)
290  (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
291 
292  timeout_list->add_timeout_function = add_function;
293  timeout_list->remove_timeout_function = remove_function;
294  timeout_list->timeout_toggled_function = toggled_function;
295  timeout_list->timeout_data = data;
296  timeout_list->timeout_free_data_function = free_data_function;
297 
298  return TRUE;
299 }
300 
311  DBusTimeout *timeout)
312 {
313  if (!_dbus_list_append (&timeout_list->timeouts, timeout))
314  return FALSE;
315 
316  _dbus_timeout_ref (timeout);
317 
318  if (timeout_list->add_timeout_function != NULL)
319  {
320  if (!(* timeout_list->add_timeout_function) (timeout,
321  timeout_list->timeout_data))
322  {
323  _dbus_list_remove_last (&timeout_list->timeouts, timeout);
324  _dbus_timeout_unref (timeout);
325  return FALSE;
326  }
327  }
328 
329  return TRUE;
330 }
331 
339 void
341  DBusTimeout *timeout)
342 {
343  if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
344  _dbus_assert_not_reached ("Nonexistent timeout was removed");
345 
346  if (timeout_list->remove_timeout_function != NULL)
347  (* timeout_list->remove_timeout_function) (timeout,
348  timeout_list->timeout_data);
349 
350  _dbus_timeout_unref (timeout);
351 }
352 
361 void
363  DBusTimeout *timeout,
364  dbus_bool_t enabled)
365 {
366  enabled = !!enabled;
367 
368  if (enabled == timeout->enabled)
369  return;
370 
371  timeout->enabled = enabled;
372 
373  if (timeout_list->timeout_toggled_function != NULL)
374  (* timeout_list->timeout_toggled_function) (timeout,
375  timeout_list->timeout_data);
376 }
377 
415 int
417 {
418  return timeout->interval;
419 }
420 
428 void*
430 {
431  return timeout->data;
432 }
433 
445 void
447  void *data,
448  DBusFreeFunction free_data_function)
449 {
450  if (timeout->free_data_function != NULL)
451  (* timeout->free_data_function) (timeout->data);
452 
453  timeout->data = data;
454  timeout->free_data_function = free_data_function;
455 }
456 
473 {
474  return (* timeout->handler) (timeout->handler_data);
475 }
476 
477 
487 {
488  return timeout->enabled;
489 }
490 
Internals of DBusTimeout.
Definition: dbus-timeout.c:40
unsigned int enabled
True if timeout is active.
Definition: dbus-timeout.c:51
DBusTimeout * _dbus_timeout_new(int interval, DBusTimeoutHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusTimeout, enabled by default.
Definition: dbus-timeout.c:63
DBusAddTimeoutFunction add_timeout_function
Callback for adding a timeout.
Definition: dbus-timeout.c:180
#define NULL
A null pointer, defined appropriately for C or C++.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
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)
Sets the timeout functions.
Definition: dbus-timeout.c:239
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
DBusTimeout * _dbus_timeout_ref(DBusTimeout *timeout)
Increments the reference count of a DBusTimeout object.
Definition: dbus-timeout.c:93
void _dbus_timeout_set_interval(DBusTimeout *timeout, int interval)
Changes the timeout interval.
Definition: dbus-timeout.c:134
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
Definition: dbus-timeout.c:211
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
Definition: dbus-timeout.c:194
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data stored at this element.
Definition: dbus-list.h:38
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:415
void _dbus_timeout_unref(DBusTimeout *timeout)
Decrements the reference count of a DBusTimeout object and finalizes the object if the count reaches ...
Definition: dbus-timeout.c:107
void * data
Application data.
Definition: dbus-timeout.c:49
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition: dbus-list.h:90
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
DBusTimeoutToggledFunction timeout_toggled_function
Callback when timeout is enabled/disabled or changes interval.
Definition: dbus-timeout.c:182
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
Definition: dbus-timeout.c:486
dbus_bool_t _dbus_list_remove_last(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:446
void * timeout_data
Data for timeout callbacks.
Definition: dbus-timeout.c:183
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
dbus_bool_t _dbus_timeout_list_add_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Adds a new timeout to the timeout list, invoking the application DBusAddTimeoutFunction if appropriat...
Definition: dbus-timeout.c:310
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
DBUS_EXPORT dbus_bool_t dbus_timeout_handle(DBusTimeout *timeout)
Calls the timeout handler for this timeout.
Definition: dbus-timeout.c:472
int refcount
Reference count.
Definition: dbus-timeout.c:42
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
DBusList * timeouts
Timeout objects.
Definition: dbus-timeout.c:178
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:270
void _dbus_list_foreach(DBusList **list, DBusForeachFunction function, void *data)
Calls the given function for each element in the list.
Definition: dbus-list.c:759
DBusFreeFunction free_handler_data_function
Free the timeout handler data.
Definition: dbus-timeout.c:47
void _dbus_timeout_list_toggle_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout, dbus_bool_t enabled)
Sets a timeout to the given enabled state, invoking the application's DBusTimeoutToggledFunction if a...
Definition: dbus-timeout.c:362
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
DBusTimeoutHandler handler
Timeout handler.
Definition: dbus-timeout.c:45
void _dbus_timeout_set_enabled(DBusTimeout *timeout, dbus_bool_t enabled)
Changes the timeout's enabled-ness.
Definition: dbus-timeout.c:153
void _dbus_timeout_list_remove_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Removes a timeout from the timeout list, invoking the application's DBusRemoveTimeoutFunction if appr...
Definition: dbus-timeout.c:340
A node in a linked list.
Definition: dbus-list.h:34
void * handler_data
Timeout handler data.
Definition: dbus-timeout.c:46
DBusRemoveTimeoutFunction remove_timeout_function
Callback for removing a timeout.
Definition: dbus-timeout.c:181
DBUS_EXPORT void dbus_timeout_set_data(DBusTimeout *timeout, void *data, DBusFreeFunction free_data_function)
Sets data which can be retrieved with dbus_timeout_get_data().
Definition: dbus-timeout.c:446
DBusFreeFunction free_data_function
Free the application data.
Definition: dbus-timeout.c:50
void(* DBusForeachFunction)(void *element, void *data)
Used to iterate over each item in a collection, such as a DBusList.
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition: dbus-list.c:567
#define FALSE
Expands to "0".
DBUS_EXPORT void * dbus_timeout_get_data(DBusTimeout *timeout)
Gets data previously set with dbus_timeout_set_data() or NULL if none.
Definition: dbus-timeout.c:429
int interval
Timeout interval in milliseconds.
Definition: dbus-timeout.c:43
DBusTimeoutList implementation details.
Definition: dbus-timeout.c:176
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:542
dbus_bool_t(* DBusTimeoutHandler)(void *data)
function to run when the timeout is handled
Definition: dbus-timeout.h:41
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.
Definition: dbus-timeout.c:416
DBusFreeFunction timeout_free_data_function
Free function for timeout callback data.
Definition: dbus-timeout.c:184