summaryrefslogtreecommitdiff
path: root/dbus/dbus-timeout.c
blob: bea86eb9d06679dcb39590489e390ff2f1a4094e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-timeout.c DBusTimeout implementation
 *
 * Copyright (C) 2003  CodeFactory AB
 *
 * Licensed under the Academic Free License version 2.1
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "dbus-internals.h"
#include "dbus-timeout.h"
#include "dbus-list.h"

/**
 * @defgroup DBusTimeoutInternals DBusTimeout implementation details
 * @ingroup  DBusInternals
 * @brief implementation details for DBusTimeout
 * 
 * @{
 */

/**
 * Internals of DBusTimeout
 */
struct DBusTimeout
{
  int refcount;                                /**< Reference count */
  int interval;                                /**< Timeout interval in milliseconds. */

  DBusTimeoutHandler handler;                  /**< Timeout handler. */
  void *handler_data;                          /**< Timeout handler data. */
  DBusFreeFunction free_handler_data_function; /**< Free the timeout handler data. */
  
  void *data;		   	               /**< Application data. */
  DBusFreeFunction free_data_function;         /**< Free the application data. */
  unsigned int enabled : 1;                    /**< True if timeout is active. */
};

/**
 * Creates a new DBusTimeout, enabled by default.
 * @param interval the timeout interval in milliseconds.
 * @param handler function to call when the timeout occurs.
 * @param data data to pass to the handler
 * @param free_data_function function to be called to free the data.
 * @returns the new DBusTimeout object,
 */
DBusTimeout*
_dbus_timeout_new (int                 interval,
		   DBusTimeoutHandler  handler,
		   void               *data,
		   DBusFreeFunction    free_data_function)
{
  DBusTimeout *timeout;

  timeout = dbus_new0 (DBusTimeout, 1);
  if (timeout == NULL)
    return NULL;
  
  timeout->refcount = 1;
  timeout->interval = interval;

  timeout->handler = handler;
  timeout->handler_data = data;
  timeout->free_handler_data_function = free_data_function;

  timeout->enabled = TRUE;
  
  return timeout;
}

/**
 * Increments the reference count of a DBusTimeout object.
 *
 * @param timeout the timeout object.
 * @returns the timeout object.
 */
DBusTimeout *
_dbus_timeout_ref (DBusTimeout *timeout)
{
  timeout->refcount += 1;

  return timeout;
}

/**
 * Decrements the reference count of a DBusTimeout object
 * and finalizes the object if the count reaches zero.
 *
 * @param timeout the timeout object.
 */
void
_dbus_timeout_unref (DBusTimeout *timeout)
{
  _dbus_assert (timeout != NULL);
  _dbus_assert (timeout->refcount > 0);
  
  timeout->refcount -= 1;
  if (timeout->refcount == 0)
    {
      dbus_timeout_set_data (timeout, NULL, NULL); /* call free_data_function */

      if (timeout->free_handler_data_function)
	(* timeout->free_handler_data_function) (timeout->handler_data);
      
      dbus_free (timeout);
    }
}

/**
 * Changes the timeout interval. Note that you have to disable and
 * re-enable the timeout using the timeout toggle function
 * (_dbus_connection_toggle_timeout() etc.) to notify the application
 * of this change.
 *
 * @param timeout the timeout
 * @param interval the new interval
 */
void
_dbus_timeout_set_interval (DBusTimeout *timeout,
                            int          interval)
{
  _dbus_assert (interval >= 0);
  
  timeout->interval = interval;
}

/**
 * Changes the timeout's enabled-ness. Note that you should use
 * _dbus_connection_toggle_timeout() etc. instead, if
 * the timeout is passed out to an application main loop.
 * i.e. you can't use this function in the D-BUS library, it's
 * only used in the message bus daemon implementation.
 *
 * @param timeout the timeout
 * @param enabled #TRUE if timeout should be enabled.
 */
void
_dbus_timeout_set_enabled (DBusTimeout  *timeout,
                           dbus_bool_t   enabled)
{
  timeout->enabled = enabled != FALSE;
}


/**
 * @typedef DBusTimeoutList
 *
 * Opaque data type representing a list of timeouts
 * and a set of DBusAddTimeoutFunction/DBusRemoveTimeoutFunction.
 * Automatically handles removing/re-adding timeouts
 * when the DBusAddTimeoutFunction is updated or changed.
 * Holds a reference count to each timeout.
 *
 */

/**
 * DBusTimeoutList implementation details. All fields
 * are private.
 *
 */
struct DBusTimeoutList
{
  DBusList *timeouts; /**< Timeout objects. */

  DBusAddTimeoutFunction add_timeout_function;       /**< Callback for adding a timeout. */
  DBusRemoveTimeoutFunction remove_timeout_function; /**< Callback for removing a timeout. */
  DBusTimeoutToggledFunction timeout_toggled_function; /**< Callback when timeout is enabled/disabled or changes interval */
  void *timeout_data;                                /**< Data for timeout callbacks */
  DBusFreeFunction timeout_free_data_function;       /**< Free function for timeout callback data */
};

/**
 * Creates a new timeout list. Returns #NULL if insufficient
 * memory exists.
 *
 * @returns the new timeout list, or #NULL on failure.
 */
DBusTimeoutList*
_dbus_timeout_list_new (void)
{
  DBusTimeoutList *timeout_list;

  timeout_list = dbus_new0 (DBusTimeoutList, 1);
  if (timeout_list == NULL)
    return NULL;

  return timeout_list;
}

/**
 * Frees a DBusTimeoutList.
 *
 * @param timeout_list the timeout list.
 */
void
_dbus_timeout_list_free (DBusTimeoutList *timeout_list)
{
  /* free timeout_data and remove timeouts as a side effect */
  _dbus_timeout_list_set_functions (timeout_list,
				    NULL, NULL, NULL, NULL, NULL);

  _dbus_list_foreach (&timeout_list->timeouts,
		      (DBusForeachFunction) _dbus_timeout_unref,
		      NULL);
  _dbus_list_clear (&timeout_list->timeouts);

  dbus_free (timeout_list);
}

/**
 * Sets the timeout functions. This function is the "backend"
 * for dbus_connection_set_timeout_functions().
 *
 * @param timeout_list the timeout list
 * @param add_function the add timeout function.
 * @param remove_function the remove timeout function.
 * @param toggled_function toggle notify function, or #NULL
 * @param data the data for those functions.
 * @param free_data_function the function to free the data.
 * @returns #FALSE if no memory
 *
 */
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)
{
  /* Add timeouts with the new function, failing on OOM */
  if (add_function != NULL)
    {
      DBusList *link;
      
      link = _dbus_list_get_first_link (&timeout_list->timeouts);
      while (link != NULL)
        {
          DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
                                                     link);
      
          if (!(* add_function) (link->data, data))
            {
              /* remove it all again and return FALSE */
              DBusList *link2;
              
              link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
              while (link2 != link)
                {
                  DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
                                                             link2);

                  (* remove_function) (link2->data, data);
                  
                  link2 = next;
                }

              return FALSE;
            }
      
          link = next;
        }
    }
  
  /* Remove all current timeouts from previous timeout handlers */

  if (timeout_list->remove_timeout_function != NULL)
    {
      _dbus_list_foreach (&timeout_list->timeouts,
			  (DBusForeachFunction) timeout_list->remove_timeout_function,
			  timeout_list->timeout_data);
    }

  if (timeout_list->timeout_free_data_function != NULL)
    (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);

  timeout_list->add_timeout_function = add_function;
  timeout_list->remove_timeout_function = remove_function;
  timeout_list->timeout_toggled_function = toggled_function;
  timeout_list->timeout_data = data;
  timeout_list->timeout_free_data_function = free_data_function;

  return TRUE;
}

/**
 * Adds a new timeout to the timeout list, invoking the
 * application DBusAddTimeoutFunction if appropriate.
 *
 * @param timeout_list the timeout list.
 * @param timeout the timeout to add.
 * @returns #TRUE on success, #FALSE If no memory.
 */
dbus_bool_t
_dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
				DBusTimeout     *timeout)
{
  if (!_dbus_list_append (&timeout_list->timeouts, timeout))
    return FALSE;

  _dbus_timeout_ref (timeout);

  if (timeout_list->add_timeout_function != NULL)
    {
      if (!(* timeout_list->add_timeout_function) (timeout,
                                                   timeout_list->timeout_data))
        {
          _dbus_list_remove_last (&timeout_list->timeouts, timeout);
          _dbus_timeout_unref (timeout);
          return FALSE;
        }
    }

  return TRUE;
}

/**
 * Removes a timeout from the timeout list, invoking the
 * application's DBusRemoveTimeoutFunction if appropriate.
 *
 * @param timeout_list the timeout list.
 * @param timeout the timeout to remove.
 */
void
_dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
				   DBusTimeout     *timeout)
{
  if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
    _dbus_assert_not_reached ("Nonexistent timeout was removed");

  if (timeout_list->remove_timeout_function != NULL)
    (* timeout_list->remove_timeout_function) (timeout,
					       timeout_list->timeout_data);

  _dbus_timeout_unref (timeout);
}

/**
 * Sets a timeout to the given enabled state, invoking the
 * application's DBusTimeoutToggledFunction if appropriate.
 *
 * @param timeout_list the timeout list.
 * @param timeout the timeout to toggle.
 * @param enabled #TRUE to enable
 */
void
_dbus_timeout_list_toggle_timeout (DBusTimeoutList           *timeout_list,
                                   DBusTimeout               *timeout,
                                   dbus_bool_t                enabled)
{
  enabled = !!enabled;
  
  if (enabled == timeout->enabled)
    return;

  timeout->enabled = enabled;
  
  if (timeout_list->timeout_toggled_function != NULL)
    (* timeout_list->timeout_toggled_function) (timeout,
                                                timeout_list->timeout_data);
}

/** @} */

/**
 * @defgroup DBusTimeout DBusTimeout
 * @ingroup  DBus
 * @brief Object representing a timeout
 *
 * Types and functions related to DBusTimeout. A timeout
 * represents a timeout that the main loop needs to monitor,
 * as in Qt's QTimer or GLib's g_timeout_add().
 * 
 * @{
 */


/**
 * @typedef DBusTimeout
 *
 * Opaque object representing a timeout.
 */

/**
 * Gets the timeout interval. The dbus_timeout_handle()
 * should be called each time this interval elapses,
 * starting after it elapses once.
 *
 * The interval may change during the life of the
 * timeout; if so, the timeout will be disabled and
 * re-enabled (calling the "timeout toggled function")
 * to notify you of the change.
 *
 * @param timeout the DBusTimeout object.
 * @returns the interval in milliseconds.
 */
int
dbus_timeout_get_interval (DBusTimeout *timeout)
{
  return timeout->interval;
}

/**
 * Gets data previously set with dbus_timeout_set_data()
 * or #NULL if none.
 *
 * @param timeout the DBusTimeout object.
 * @returns previously-set data.
 */
void*
dbus_timeout_get_data (DBusTimeout *timeout)
{
  return timeout->data;
}

/**
 * Sets data which can be retrieved with dbus_timeout_get_data().
 * Intended for use by the DBusAddTimeoutFunction and
 * DBusRemoveTimeoutFunction to store their own data.  For example with
 * Qt you might store the QTimer for this timeout and with GLib
 * you might store a g_timeout_add result id.
 *
 * @param timeout the DBusTimeout object.
 * @param data the data.
 * @param free_data_function function to be called to free the data.
 */
void
dbus_timeout_set_data (DBusTimeout      *timeout,
		       void             *data,
		       DBusFreeFunction  free_data_function)
{
  if (timeout->free_data_function != NULL)
    (* timeout->free_data_function) (timeout->data);

  timeout->data = data;
  timeout->free_data_function = free_data_function;
}

/**
 * Calls the timeout handler for this timeout.
 * This function should be called when the timeout
 * occurs.
 *
 * If this function returns #FALSE, then there wasn't
 * enough memory to handle the timeout. Typically just
 * letting the timeout fire again next time it naturally
 * times out is an adequate response to that problem,
 * but you could try to do more if you wanted.
 *
 * @param timeout the DBusTimeout object.
 * @returns #FALSE if there wasn't enough memory 
 */
dbus_bool_t
dbus_timeout_handle (DBusTimeout *timeout)
{
  return (* timeout->handler) (timeout->handler_data);
}


/**
 * Returns whether a timeout is enabled or not. If not
 * enabled, it should not be polled by the main loop.
 *
 * @param timeout the DBusTimeout object
 * @returns #TRUE if the timeout is enabled
 */
dbus_bool_t
dbus_timeout_get_enabled (DBusTimeout *timeout)
{
  return timeout->enabled;
}

/** @} end public API docs */