4 #define DBUS_COMPILATION /* cheat and use DBusList */
5 #include <dbus/dbus-list.h>
6 #undef DBUS_COMPILATION
8 /* Cheesy main loop used in test programs. Any real app would use the
9 * GLib or Qt or other non-sucky main loops.
13 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
15 static DBusList *watches = NULL;
16 static dbus_bool_t exited = FALSE;
17 static DBusList *connections = NULL;
32 add_connection_watch (DBusWatch *watch,
33 DBusConnection *connection)
37 wd = dbus_new0 (WatchData, 1);
38 wd->type = WATCH_CONNECTION;
39 wd->data = connection;
41 _dbus_list_append (&watches, watch);
42 dbus_watch_set_data (watch, wd, dbus_free);
46 remove_connection_watch (DBusWatch *watch,
47 DBusConnection *connection)
49 _dbus_list_remove (&watches, watch);
50 dbus_watch_set_data (watch, NULL, NULL);
54 add_server_watch (DBusWatch *watch,
59 wd = dbus_new0 (WatchData, 1);
60 wd->type = WATCH_SERVER;
63 _dbus_list_append (&watches, watch);
65 dbus_watch_set_data (watch, wd, dbus_free);
69 remove_server_watch (DBusWatch *watch,
72 _dbus_list_remove (&watches, watch);
73 dbus_watch_set_data (watch, NULL, NULL);
83 link = _dbus_list_get_first_link (&connections);
86 DBusList *next = _dbus_list_get_next_link (&connections, link);
87 DBusConnection *connection = link->data;
90 while ((message = dbus_connection_pop_message (connection)))
94 printf ("Received message %d, sending reply\n", count);
96 reply = dbus_message_new ();
97 dbus_connection_send_message (connection,
100 dbus_message_unref (reply);
102 dbus_message_unref (message);
107 printf ("Saw %d messages, exiting\n", count);
119 /* Of course with any real app you'd use GMainLoop or
120 * QSocketNotifier and not have to see all this crap.
123 while (!exited && watches != NULL)
134 FD_ZERO (&write_set);
139 link = _dbus_list_get_first_link (&watches);
142 DBusList *next = _dbus_list_get_next_link (&watches, link);
149 fd = dbus_watch_get_fd (watch);
150 flags = dbus_watch_get_flags (watch);
152 max_fd = MAX (max_fd, fd);
154 if (flags & DBUS_WATCH_READABLE)
155 FD_SET (fd, &read_set);
157 if (flags & DBUS_WATCH_WRITABLE)
158 FD_SET (fd, &write_set);
160 FD_SET (fd, &err_set);
165 select (max_fd + 1, &read_set, &write_set, &err_set, NULL);
167 link = _dbus_list_get_first_link (&watches);
170 DBusList *next = _dbus_list_get_next_link (&watches, link);
174 unsigned int condition;
178 fd = dbus_watch_get_fd (watch);
179 flags = dbus_watch_get_flags (watch);
183 if ((flags & DBUS_WATCH_READABLE) &&
184 FD_ISSET (fd, &read_set))
185 condition |= DBUS_WATCH_READABLE;
187 if ((flags & DBUS_WATCH_WRITABLE) &&
188 FD_ISSET (fd, &write_set))
189 condition |= DBUS_WATCH_WRITABLE;
191 if (FD_ISSET (fd, &err_set))
192 condition |= DBUS_WATCH_ERROR;
198 wd = dbus_watch_get_data (watch);
200 if (wd->type == WATCH_CONNECTION)
202 DBusConnection *connection = wd->data;
204 dbus_connection_handle_watch (connection,
208 else if (wd->type == WATCH_SERVER)
210 DBusServer *server = wd->data;
212 dbus_server_handle_watch (server,
230 error_handler (DBusConnection *connection,
231 DBusResultCode error_code,
235 "Error on connection: %s\n",
236 dbus_result_to_string (error_code));
238 _dbus_list_remove (&connections, connection);
239 dbus_connection_unref (connection);
244 setup_connection (DBusConnection *connection)
246 dbus_connection_set_watch_functions (connection,
247 (DBusAddWatchFunction) add_connection_watch,
248 (DBusRemoveWatchFunction) remove_connection_watch,
252 dbus_connection_set_error_function (connection,
256 dbus_connection_ref (connection);
257 _dbus_list_append (&connections, connection);
261 setup_server (DBusServer *server)
263 dbus_server_set_watch_functions (server,
264 (DBusAddWatchFunction) add_server_watch,
265 (DBusRemoveWatchFunction) remove_server_watch,