1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-watch.c DBusWatch implementation
4 * Copyright (C) 2002, 2003 Red Hat Inc.
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-watch.h"
26 #include "dbus-list.h"
29 * @defgroup DBusWatchInternals DBusWatch implementation details
30 * @ingroup DBusInternals
31 * @brief implementation details for DBusWatch
38 int refcount; /**< Reference count */
39 int fd; /**< File descriptor. */
40 unsigned int flags; /**< Conditions to watch. */
42 DBusWatchHandler handler; /**< Watch handler. */
43 void *handler_data; /**< Watch handler data. */
44 DBusFreeFunction free_handler_data_function; /**< Free the watch handler data. */
46 void *data; /**< Application data. */
47 DBusFreeFunction free_data_function; /**< Free the application data. */
48 unsigned int enabled : 1; /**< Whether it's enabled. */
52 * Creates a new DBusWatch. Used to add a file descriptor to be polled
55 * @param fd the file descriptor to be watched.
56 * @param flags the conditions to watch for on the descriptor.
57 * @param enabled the initial enabled state
58 * @param handler the handler function
59 * @param data data for handler function
60 * @param free_data_function function to free the data
61 * @returns the new DBusWatch object.
64 _dbus_watch_new (int fd,
67 DBusWatchHandler handler,
69 DBusFreeFunction free_data_function)
73 #define VALID_WATCH_FLAGS (DBUS_WATCH_WRITABLE | DBUS_WATCH_READABLE)
75 _dbus_assert ((flags & VALID_WATCH_FLAGS) == flags);
77 watch = dbus_new0 (DBusWatch, 1);
84 watch->enabled = enabled;
86 watch->handler = handler;
87 watch->handler_data = data;
88 watch->free_handler_data_function = free_data_function;
94 * Increments the reference count of a DBusWatch object.
96 * @param watch the watch object.
99 _dbus_watch_ref (DBusWatch *watch)
101 watch->refcount += 1;
105 * Decrements the reference count of a DBusWatch object
106 * and finalizes the object if the count reaches zero.
108 * @param watch the watch object.
111 _dbus_watch_unref (DBusWatch *watch)
113 _dbus_assert (watch != NULL);
114 _dbus_assert (watch->refcount > 0);
116 watch->refcount -= 1;
117 if (watch->refcount == 0)
119 dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
121 if (watch->free_handler_data_function)
122 (* watch->free_handler_data_function) (watch->handler_data);
129 * Clears the file descriptor from a now-invalid watch object so that
130 * no one tries to use it. This is because a watch may stay alive due
131 * to reference counts after the file descriptor is closed.
132 * Invalidation makes it easier to catch bugs. It also
133 * keeps people from doing dorky things like assuming file descriptors
134 * are unique (never recycled).
136 * @param watch the watch object.
139 _dbus_watch_invalidate (DBusWatch *watch)
146 * Sanitizes the given condition so that it only contains
147 * flags that the DBusWatch requested. e.g. if the
148 * watch is a DBUS_WATCH_READABLE watch then
149 * DBUS_WATCH_WRITABLE will be stripped from the condition.
151 * @param watch the watch object.
152 * @param condition address of the condition to sanitize.
155 _dbus_watch_sanitize_condition (DBusWatch *watch,
156 unsigned int *condition)
158 if (!(watch->flags & DBUS_WATCH_READABLE))
159 *condition &= ~DBUS_WATCH_READABLE;
160 if (!(watch->flags & DBUS_WATCH_WRITABLE))
161 *condition &= ~DBUS_WATCH_WRITABLE;
166 * @typedef DBusWatchList
168 * Opaque data type representing a list of watches
169 * and a set of DBusAddWatchFunction/DBusRemoveWatchFunction.
170 * Automatically handles removing/re-adding watches
171 * when the DBusAddWatchFunction is updated or changed.
172 * Holds a reference count to each watch.
174 * Used in the implementation of both DBusServer and
180 * DBusWatchList implementation details. All fields
186 DBusList *watches; /**< Watch objects. */
188 DBusAddWatchFunction add_watch_function; /**< Callback for adding a watch. */
189 DBusRemoveWatchFunction remove_watch_function; /**< Callback for removing a watch. */
190 DBusWatchToggledFunction watch_toggled_function; /**< Callback on toggling enablement */
191 void *watch_data; /**< Data for watch callbacks */
192 DBusFreeFunction watch_free_data_function; /**< Free function for watch callback data */
196 * Creates a new watch list. Returns #NULL if insufficient
199 * @returns the new watch list, or #NULL on failure.
202 _dbus_watch_list_new (void)
204 DBusWatchList *watch_list;
206 watch_list = dbus_new0 (DBusWatchList, 1);
207 if (watch_list == NULL)
214 * Frees a DBusWatchList.
216 * @param watch_list the watch list.
219 _dbus_watch_list_free (DBusWatchList *watch_list)
221 /* free watch_data and removes watches as a side effect */
222 _dbus_watch_list_set_functions (watch_list,
223 NULL, NULL, NULL, NULL, NULL);
224 _dbus_list_foreach (&watch_list->watches,
225 (DBusForeachFunction) _dbus_watch_unref,
227 _dbus_list_clear (&watch_list->watches);
229 dbus_free (watch_list);
233 * Sets the watch functions. This function is the "backend"
234 * for dbus_connection_set_watch_functions() and
235 * dbus_server_set_watch_functions().
237 * @param watch_list the watch list.
238 * @param add_function the add watch function.
239 * @param remove_function the remove watch function.
240 * @param toggled_function function on toggling enabled flag, or #NULL
241 * @param data the data for those functions.
242 * @param free_data_function the function to free the data.
243 * @returns #FALSE if not enough memory
247 _dbus_watch_list_set_functions (DBusWatchList *watch_list,
248 DBusAddWatchFunction add_function,
249 DBusRemoveWatchFunction remove_function,
250 DBusWatchToggledFunction toggled_function,
252 DBusFreeFunction free_data_function)
254 /* Add watches with the new watch function, failing on OOM */
255 if (add_function != NULL)
259 link = _dbus_list_get_first_link (&watch_list->watches);
262 DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
265 _dbus_verbose ("Adding a watch on fd %d using newly-set add watch function\n",
266 dbus_watch_get_fd (link->data));
268 if (!(* add_function) (link->data, data))
270 /* remove it all again and return FALSE */
273 link2 = _dbus_list_get_first_link (&watch_list->watches);
274 while (link2 != link)
276 DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
279 _dbus_verbose ("Removing watch on fd %d using newly-set remove function because initial add failed\n",
280 dbus_watch_get_fd (link2->data));
282 (* remove_function) (link2->data, data);
294 /* Remove all current watches from previous watch handlers */
296 if (watch_list->remove_watch_function != NULL)
298 _dbus_verbose ("Removing all pre-existing watches\n");
300 _dbus_list_foreach (&watch_list->watches,
301 (DBusForeachFunction) watch_list->remove_watch_function,
302 watch_list->watch_data);
305 if (watch_list->watch_free_data_function != NULL)
306 (* watch_list->watch_free_data_function) (watch_list->watch_data);
308 watch_list->add_watch_function = add_function;
309 watch_list->remove_watch_function = remove_function;
310 watch_list->watch_toggled_function = toggled_function;
311 watch_list->watch_data = data;
312 watch_list->watch_free_data_function = free_data_function;
318 * Adds a new watch to the watch list, invoking the
319 * application DBusAddWatchFunction if appropriate.
321 * @param watch_list the watch list.
322 * @param watch the watch to add.
323 * @returns #TRUE on success, #FALSE if no memory.
326 _dbus_watch_list_add_watch (DBusWatchList *watch_list,
329 if (!_dbus_list_append (&watch_list->watches, watch))
332 _dbus_watch_ref (watch);
334 if (watch_list->add_watch_function != NULL)
336 _dbus_verbose ("Adding watch on fd %d\n",
337 dbus_watch_get_fd (watch));
339 if (!(* watch_list->add_watch_function) (watch,
340 watch_list->watch_data))
342 _dbus_list_remove_last (&watch_list->watches, watch);
343 _dbus_watch_unref (watch);
352 * Removes a watch from the watch list, invoking the
353 * application's DBusRemoveWatchFunction if appropriate.
355 * @param watch_list the watch list.
356 * @param watch the watch to remove.
359 _dbus_watch_list_remove_watch (DBusWatchList *watch_list,
362 if (!_dbus_list_remove (&watch_list->watches, watch))
363 _dbus_assert_not_reached ("Nonexistent watch was removed");
365 if (watch_list->remove_watch_function != NULL)
367 _dbus_verbose ("Removing watch on fd %d\n",
368 dbus_watch_get_fd (watch));
370 (* watch_list->remove_watch_function) (watch,
371 watch_list->watch_data);
374 _dbus_watch_unref (watch);
378 * Sets a watch to the given enabled state, invoking the
379 * application's DBusWatchToggledFunction if appropriate.
381 * @param watch_list the watch list.
382 * @param watch the watch to toggle.
383 * @param enabled #TRUE to enable
386 _dbus_watch_list_toggle_watch (DBusWatchList *watch_list,
392 if (enabled == watch->enabled)
395 watch->enabled = enabled;
397 if (watch_list->watch_toggled_function != NULL)
399 _dbus_verbose ("Toggling watch on fd %d to %d\n",
400 dbus_watch_get_fd (watch), watch->enabled);
402 (* watch_list->watch_toggled_function) (watch,
403 watch_list->watch_data);
408 * Sets the handler for the watch.
410 * @todo this function only exists because of the weird
411 * way connection watches are done, see the note
412 * in docs for _dbus_connection_handle_watch().
414 * @param watch the watch
415 * @param handler the new handler
416 * @param data the data
417 * @param free_data_function free data with this
420 _dbus_watch_set_handler (DBusWatch *watch,
421 DBusWatchHandler handler,
423 DBusFreeFunction free_data_function)
425 if (watch->free_handler_data_function)
426 (* watch->free_handler_data_function) (watch->handler_data);
428 watch->handler = handler;
429 watch->handler_data = data;
430 watch->free_handler_data_function = free_data_function;
436 * @defgroup DBusWatch DBusWatch
438 * @brief Object representing a file descriptor to be watched.
440 * Types and functions related to DBusWatch. A watch represents
441 * a file descriptor that the main loop needs to monitor,
442 * as in Qt's QSocketNotifier or GLib's g_io_add_watch().
450 * Opaque object representing a file descriptor
451 * to be watched for changes in readability,
452 * writability, or hangup.
456 * Gets the file descriptor that should be watched.
458 * @param watch the DBusWatch object.
459 * @returns the file descriptor to watch.
462 dbus_watch_get_fd (DBusWatch *watch)
468 * Gets flags from DBusWatchFlags indicating
469 * what conditions should be monitored on the
472 * The flags returned will only contain DBUS_WATCH_READABLE
473 * and DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or
474 * DBUS_WATCH_ERROR; all watches implicitly include a watch
475 * for hangups, errors, and other exceptional conditions.
477 * @param watch the DBusWatch object.
478 * @returns the conditions to watch.
481 dbus_watch_get_flags (DBusWatch *watch)
483 _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
489 * Gets data previously set with dbus_watch_set_data()
492 * @param watch the DBusWatch object.
493 * @returns previously-set data.
496 dbus_watch_get_data (DBusWatch *watch)
502 * Sets data which can be retrieved with dbus_watch_get_data().
503 * Intended for use by the DBusAddWatchFunction and
504 * DBusRemoveWatchFunction to store their own data. For example with
505 * Qt you might store the QSocketNotifier for this watch and with GLib
506 * you might store a GSource.
508 * @param watch the DBusWatch object.
509 * @param data the data.
510 * @param free_data_function function to be called to free the data.
513 dbus_watch_set_data (DBusWatch *watch,
515 DBusFreeFunction free_data_function)
517 _dbus_verbose ("Setting watch fd %d data to data = %p function = %p from data = %p function = %p\n",
518 dbus_watch_get_fd (watch),
519 data, free_data_function, watch->data, watch->free_data_function);
521 if (watch->free_data_function != NULL)
522 (* watch->free_data_function) (watch->data);
525 watch->free_data_function = free_data_function;
529 * Returns whether a watch is enabled or not. If not
530 * enabled, it should not be polled by the main loop.
532 * @param watch the DBusWatch object
533 * @returns #TRUE if the watch is enabled
536 dbus_watch_get_enabled (DBusWatch *watch)
538 return watch->enabled;
543 * Called to notify the D-BUS library when a previously-added watch is
544 * ready for reading or writing, or has an exception such as a hangup.
546 * If this function returns #FALSE, then the file descriptor may still
547 * be ready for reading or writing, but more memory is needed in order
548 * to do the reading or writing. If you ignore the #FALSE return, your
549 * application may spin in a busy loop on the file descriptor until
550 * memory becomes available, but nothing more catastrophic should
553 * dbus_watch_handle() cannot be called during the
554 * DBusAddWatchFunction, as the connection will not be ready to handle
557 * It is not allowed to reference a DBusWatch after it has been passed
558 * to remove_function.
560 * @param watch the DBusWatch object.
561 * @param flags the poll condition using #DBusWatchFlags values
562 * @returns #FALSE if there wasn't enough memory
565 dbus_watch_handle (DBusWatch *watch,
568 _dbus_watch_sanitize_condition (watch, &flags);
572 _dbus_verbose ("After sanitization, watch flags on fd %d were 0\n",
577 return (* watch->handler) (watch, flags,
578 watch->handler_data);