00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "dbus-internals.h"
00025 #include "dbus-timeout.h"
00026 #include "dbus-list.h"
00027
00036 struct DBusTimeout
00037 {
00038 int refcount;
00039 int interval;
00041 DBusTimeoutHandler handler;
00042 void *handler_data;
00043 DBusFreeFunction free_handler_data_function;
00045 void *data;
00046 DBusFreeFunction free_data_function;
00047 unsigned int enabled : 1;
00048 };
00049
00058 DBusTimeout*
00059 _dbus_timeout_new (int interval,
00060 DBusTimeoutHandler handler,
00061 void *data,
00062 DBusFreeFunction free_data_function)
00063 {
00064 DBusTimeout *timeout;
00065
00066 timeout = dbus_new0 (DBusTimeout, 1);
00067 if (timeout == NULL)
00068 return NULL;
00069
00070 timeout->refcount = 1;
00071 timeout->interval = interval;
00072
00073 timeout->handler = handler;
00074 timeout->handler_data = data;
00075 timeout->free_handler_data_function = free_data_function;
00076
00077 timeout->enabled = TRUE;
00078
00079 return timeout;
00080 }
00081
00087 void
00088 _dbus_timeout_ref (DBusTimeout *timeout)
00089 {
00090 timeout->refcount += 1;
00091 }
00092
00099 void
00100 _dbus_timeout_unref (DBusTimeout *timeout)
00101 {
00102 _dbus_assert (timeout != NULL);
00103 _dbus_assert (timeout->refcount > 0);
00104
00105 timeout->refcount -= 1;
00106 if (timeout->refcount == 0)
00107 {
00108 dbus_timeout_set_data (timeout, NULL, NULL);
00109
00110 if (timeout->free_handler_data_function)
00111 (* timeout->free_handler_data_function) (timeout->handler_data);
00112
00113 dbus_free (timeout);
00114 }
00115 }
00116
00126 void
00127 _dbus_timeout_set_interval (DBusTimeout *timeout,
00128 int interval)
00129 {
00130 _dbus_assert (interval >= 0);
00131
00132 timeout->interval = interval;
00133 }
00134
00145 void
00146 _dbus_timeout_set_enabled (DBusTimeout *timeout,
00147 dbus_bool_t enabled)
00148 {
00149 timeout->enabled = enabled != FALSE;
00150 }
00151
00152
00169 struct DBusTimeoutList
00170 {
00171 DBusList *timeouts;
00173 DBusAddTimeoutFunction add_timeout_function;
00174 DBusRemoveTimeoutFunction remove_timeout_function;
00175 DBusTimeoutToggledFunction timeout_toggled_function;
00176 void *timeout_data;
00177 DBusFreeFunction timeout_free_data_function;
00178 };
00179
00186 DBusTimeoutList*
00187 _dbus_timeout_list_new (void)
00188 {
00189 DBusTimeoutList *timeout_list;
00190
00191 timeout_list = dbus_new0 (DBusTimeoutList, 1);
00192 if (timeout_list == NULL)
00193 return NULL;
00194
00195 return timeout_list;
00196 }
00197
00203 void
00204 _dbus_timeout_list_free (DBusTimeoutList *timeout_list)
00205 {
00206
00207 _dbus_timeout_list_set_functions (timeout_list,
00208 NULL, NULL, NULL, NULL, NULL);
00209
00210 _dbus_list_foreach (&timeout_list->timeouts,
00211 (DBusForeachFunction) _dbus_timeout_unref,
00212 NULL);
00213 _dbus_list_clear (&timeout_list->timeouts);
00214
00215 dbus_free (timeout_list);
00216 }
00217
00231 dbus_bool_t
00232 _dbus_timeout_list_set_functions (DBusTimeoutList *timeout_list,
00233 DBusAddTimeoutFunction add_function,
00234 DBusRemoveTimeoutFunction remove_function,
00235 DBusTimeoutToggledFunction toggled_function,
00236 void *data,
00237 DBusFreeFunction free_data_function)
00238 {
00239
00240 if (add_function != NULL)
00241 {
00242 DBusList *link;
00243
00244 link = _dbus_list_get_first_link (&timeout_list->timeouts);
00245 while (link != NULL)
00246 {
00247 DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
00248 link);
00249
00250 if (!(* add_function) (link->data, data))
00251 {
00252
00253 DBusList *link2;
00254
00255 link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
00256 while (link2 != link)
00257 {
00258 DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
00259 link2);
00260
00261 (* remove_function) (link2->data, data);
00262
00263 link2 = next;
00264 }
00265
00266 return FALSE;
00267 }
00268
00269 link = next;
00270 }
00271 }
00272
00273
00274
00275 if (timeout_list->remove_timeout_function != NULL)
00276 {
00277 _dbus_list_foreach (&timeout_list->timeouts,
00278 (DBusForeachFunction) timeout_list->remove_timeout_function,
00279 timeout_list->timeout_data);
00280 }
00281
00282 if (timeout_list->timeout_free_data_function != NULL)
00283 (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
00284
00285 timeout_list->add_timeout_function = add_function;
00286 timeout_list->remove_timeout_function = remove_function;
00287 timeout_list->timeout_toggled_function = toggled_function;
00288 timeout_list->timeout_data = data;
00289 timeout_list->timeout_free_data_function = free_data_function;
00290
00291 return TRUE;
00292 }
00293
00302 dbus_bool_t
00303 _dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
00304 DBusTimeout *timeout)
00305 {
00306 if (!_dbus_list_append (&timeout_list->timeouts, timeout))
00307 return FALSE;
00308
00309 _dbus_timeout_ref (timeout);
00310
00311 if (timeout_list->add_timeout_function != NULL)
00312 {
00313 if (!(* timeout_list->add_timeout_function) (timeout,
00314 timeout_list->timeout_data))
00315 {
00316 _dbus_list_remove_last (&timeout_list->timeouts, timeout);
00317 _dbus_timeout_unref (timeout);
00318 return FALSE;
00319 }
00320 }
00321
00322 return TRUE;
00323 }
00324
00332 void
00333 _dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
00334 DBusTimeout *timeout)
00335 {
00336 if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
00337 _dbus_assert_not_reached ("Nonexistent timeout was removed");
00338
00339 if (timeout_list->remove_timeout_function != NULL)
00340 (* timeout_list->remove_timeout_function) (timeout,
00341 timeout_list->timeout_data);
00342
00343 _dbus_timeout_unref (timeout);
00344 }
00345
00354 void
00355 _dbus_timeout_list_toggle_timeout (DBusTimeoutList *timeout_list,
00356 DBusTimeout *timeout,
00357 dbus_bool_t enabled)
00358 {
00359 enabled = !!enabled;
00360
00361 if (enabled == timeout->enabled)
00362 return;
00363
00364 timeout->enabled = enabled;
00365
00366 if (timeout_list->timeout_toggled_function != NULL)
00367 (* timeout_list->timeout_toggled_function) (timeout,
00368 timeout_list->timeout_data);
00369 }
00370
00405 int
00406 dbus_timeout_get_interval (DBusTimeout *timeout)
00407 {
00408 return timeout->interval;
00409 }
00410
00418 void*
00419 dbus_timeout_get_data (DBusTimeout *timeout)
00420 {
00421 return timeout->data;
00422 }
00423
00435 void
00436 dbus_timeout_set_data (DBusTimeout *timeout,
00437 void *data,
00438 DBusFreeFunction free_data_function)
00439 {
00440 if (timeout->free_data_function != NULL)
00441 (* timeout->free_data_function) (timeout->data);
00442
00443 timeout->data = data;
00444 timeout->free_data_function = free_data_function;
00445 }
00446
00461 dbus_bool_t
00462 dbus_timeout_handle (DBusTimeout *timeout)
00463 {
00464 return (* timeout->handler) (timeout->handler_data);
00465 }
00466
00467
00475 dbus_bool_t
00476 dbus_timeout_get_enabled (DBusTimeout *timeout)
00477 {
00478 return timeout->enabled;
00479 }
00480