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. */
41 void *data; /**< Application data. */
42 DBusFreeFunction free_data_function; /**< Free the application data. */
43 unsigned int enabled : 1; /**< Whether it's enabled. */
47 * Creates a new DBusWatch. Normally used by a DBusTransport
49 * @param fd the file descriptor to be watched.
50 * @param flags the conditions to watch for on the descriptor.
51 * @param enabled the initial enabled state
52 * @returns the new DBusWatch object.
55 _dbus_watch_new (int fd,
61 #define VALID_WATCH_FLAGS (DBUS_WATCH_WRITABLE | DBUS_WATCH_READABLE)
63 _dbus_assert ((flags & VALID_WATCH_FLAGS) == flags);
65 watch = dbus_new0 (DBusWatch, 1);
69 watch->enabled = enabled;
75 * Increments the reference count of a DBusWatch object.
77 * @param watch the watch object.
80 _dbus_watch_ref (DBusWatch *watch)
86 * Decrements the reference count of a DBusWatch object
87 * and finalizes the object if the count reaches zero.
89 * @param watch the watch object.
92 _dbus_watch_unref (DBusWatch *watch)
94 _dbus_assert (watch != NULL);
95 _dbus_assert (watch->refcount > 0);
98 if (watch->refcount == 0)
100 dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
106 * Clears the file descriptor from a now-invalid watch object so that
107 * no one tries to use it. This is because a watch may stay alive due
108 * to reference counts after the file descriptor is closed.
109 * Invalidation makes it easier to catch bugs. It also
110 * keeps people from doing dorky things like assuming file descriptors
111 * are unique (never recycled).
113 * @param watch the watch object.
116 _dbus_watch_invalidate (DBusWatch *watch)
123 * Sanitizes the given condition so that it only contains
124 * flags that the DBusWatch requested. e.g. if the
125 * watch is a DBUS_WATCH_READABLE watch then
126 * DBUS_WATCH_WRITABLE will be stripped from the condition.
128 * @param watch the watch object.
129 * @param condition address of the condition to sanitize.
132 _dbus_watch_sanitize_condition (DBusWatch *watch,
133 unsigned int *condition)
135 if (!(watch->flags & DBUS_WATCH_READABLE))
136 *condition &= ~DBUS_WATCH_READABLE;
137 if (!(watch->flags & DBUS_WATCH_WRITABLE))
138 *condition &= ~DBUS_WATCH_WRITABLE;
143 * @typedef DBusWatchList
145 * Opaque data type representing a list of watches
146 * and a set of DBusAddWatchFunction/DBusRemoveWatchFunction.
147 * Automatically handles removing/re-adding watches
148 * when the DBusAddWatchFunction is updated or changed.
149 * Holds a reference count to each watch.
151 * Used in the implementation of both DBusServer and
157 * DBusWatchList implementation details. All fields
163 DBusList *watches; /**< Watch objects. */
165 DBusAddWatchFunction add_watch_function; /**< Callback for adding a watch. */
166 DBusRemoveWatchFunction remove_watch_function; /**< Callback for removing a watch. */
167 DBusWatchToggledFunction watch_toggled_function; /**< Callback on toggling enablement */
168 void *watch_data; /**< Data for watch callbacks */
169 DBusFreeFunction watch_free_data_function; /**< Free function for watch callback data */
173 * Creates a new watch list. Returns #NULL if insufficient
176 * @returns the new watch list, or #NULL on failure.
179 _dbus_watch_list_new (void)
181 DBusWatchList *watch_list;
183 watch_list = dbus_new0 (DBusWatchList, 1);
184 if (watch_list == NULL)
191 * Frees a DBusWatchList.
193 * @param watch_list the watch list.
196 _dbus_watch_list_free (DBusWatchList *watch_list)
198 /* free watch_data and removes watches as a side effect */
199 _dbus_watch_list_set_functions (watch_list,
200 NULL, NULL, NULL, NULL, NULL);
202 _dbus_list_foreach (&watch_list->watches,
203 (DBusForeachFunction) _dbus_watch_unref,
205 _dbus_list_clear (&watch_list->watches);
207 dbus_free (watch_list);
211 * Sets the watch functions. This function is the "backend"
212 * for dbus_connection_set_watch_functions() and
213 * dbus_server_set_watch_functions().
215 * @param watch_list the watch list.
216 * @param add_function the add watch function.
217 * @param remove_function the remove watch function.
218 * @param toggled_function function on toggling enabled flag, or #NULL
219 * @param data the data for those functions.
220 * @param free_data_function the function to free the data.
221 * @returns #FALSE if not enough memory
225 _dbus_watch_list_set_functions (DBusWatchList *watch_list,
226 DBusAddWatchFunction add_function,
227 DBusRemoveWatchFunction remove_function,
228 DBusWatchToggledFunction toggled_function,
230 DBusFreeFunction free_data_function)
232 /* Add watches with the new watch function, failing on OOM */
233 if (add_function != NULL)
237 link = _dbus_list_get_first_link (&watch_list->watches);
240 DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
243 if (!(* add_function) (link->data, data))
245 /* remove it all again and return FALSE */
248 link2 = _dbus_list_get_first_link (&watch_list->watches);
249 while (link2 != link)
251 DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
254 (* remove_function) (link2->data, data);
266 /* Remove all current watches from previous watch handlers */
268 if (watch_list->remove_watch_function != NULL)
270 _dbus_list_foreach (&watch_list->watches,
271 (DBusForeachFunction) watch_list->remove_watch_function,
272 watch_list->watch_data);
275 if (watch_list->watch_free_data_function != NULL)
276 (* watch_list->watch_free_data_function) (watch_list->watch_data);
278 watch_list->add_watch_function = add_function;
279 watch_list->remove_watch_function = remove_function;
280 watch_list->watch_toggled_function = toggled_function;
281 watch_list->watch_data = data;
282 watch_list->watch_free_data_function = free_data_function;
288 * Adds a new watch to the watch list, invoking the
289 * application DBusAddWatchFunction if appropriate.
291 * @param watch_list the watch list.
292 * @param watch the watch to add.
293 * @returns #TRUE on success, #FALSE if no memory.
296 _dbus_watch_list_add_watch (DBusWatchList *watch_list,
299 if (!_dbus_list_append (&watch_list->watches, watch))
302 _dbus_watch_ref (watch);
304 if (watch_list->add_watch_function != NULL)
306 if (!(* watch_list->add_watch_function) (watch,
307 watch_list->watch_data))
309 _dbus_list_remove_last (&watch_list->watches, watch);
310 _dbus_watch_unref (watch);
319 * Removes a watch from the watch list, invoking the
320 * application's DBusRemoveWatchFunction if appropriate.
322 * @param watch_list the watch list.
323 * @param watch the watch to remove.
326 _dbus_watch_list_remove_watch (DBusWatchList *watch_list,
329 if (!_dbus_list_remove (&watch_list->watches, watch))
330 _dbus_assert_not_reached ("Nonexistent watch was removed");
332 if (watch_list->remove_watch_function != NULL)
333 (* watch_list->remove_watch_function) (watch,
334 watch_list->watch_data);
336 _dbus_watch_unref (watch);
340 * Sets a watch to the given enabled state, invoking the
341 * application's DBusWatchToggledFunction if appropriate.
343 * @param watch_list the watch list.
344 * @param watch the watch to toggle.
345 * @param enabled #TRUE to enable
348 _dbus_watch_list_toggle_watch (DBusWatchList *watch_list,
354 if (enabled == watch->enabled)
357 watch->enabled = enabled;
359 if (watch_list->watch_toggled_function != NULL)
360 (* watch_list->watch_toggled_function) (watch,
361 watch_list->watch_data);
367 * @defgroup DBusWatch DBusWatch
369 * @brief Object representing a file descriptor to be watched.
371 * Types and functions related to DBusWatch. A watch represents
372 * a file descriptor that the main loop needs to monitor,
373 * as in Qt's QSocketNotifier or GLib's g_io_add_watch().
381 * Opaque object representing a file descriptor
382 * to be watched for changes in readability,
383 * writability, or hangup.
387 * Gets the file descriptor that should be watched.
389 * @param watch the DBusWatch object.
390 * @returns the file descriptor to watch.
393 dbus_watch_get_fd (DBusWatch *watch)
399 * Gets flags from DBusWatchFlags indicating
400 * what conditions should be monitored on the
403 * The flags returned will only contain DBUS_WATCH_READABLE
404 * and DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or
405 * DBUS_WATCH_ERROR; all watches implicitly include a watch
406 * for hangups, errors, and other exceptional conditions.
408 * @param watch the DBusWatch object.
409 * @returns the conditions to watch.
412 dbus_watch_get_flags (DBusWatch *watch)
414 _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
420 * Gets data previously set with dbus_watch_set_data()
423 * @param watch the DBusWatch object.
424 * @returns previously-set data.
427 dbus_watch_get_data (DBusWatch *watch)
433 * Sets data which can be retrieved with dbus_watch_get_data().
434 * Intended for use by the DBusAddWatchFunction and
435 * DBusRemoveWatchFunction to store their own data. For example with
436 * Qt you might store the QSocketNotifier for this watch and with GLib
437 * you might store a GSource.
439 * @param watch the DBusWatch object.
440 * @param data the data.
441 * @param free_data_function function to be called to free the data.
444 dbus_watch_set_data (DBusWatch *watch,
446 DBusFreeFunction free_data_function)
448 if (watch->free_data_function != NULL)
449 (* watch->free_data_function) (watch->data);
452 watch->free_data_function = free_data_function;
456 * Returns whether a watch is enabled or not. If not
457 * enabled, it should not be polled by the main loop.
459 * @param watch the DBusWatch object
460 * @returns #TRUE if the watch is enabled
463 dbus_watch_get_enabled (DBusWatch *watch)
465 return watch->enabled;