00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00035 #include <string.h>
00036 #include <stdlib.h>
00037
00038 #include "debug.h"
00039 #include "easydbus-core.h"
00040 #include "monitor_internal.h"
00041
00042
00049 EasyDbus_watcher *
00050 easydbus_watcher_create_skeleton (void)
00051 {
00052 struct EasyDbus_watcher *watcher = NULL;
00053
00054 watcher = (struct EasyDbus_watcher *)
00055 malloc (sizeof (struct EasyDbus_watcher));
00056
00057 if (!watcher)
00058 return NULL;
00059
00060 memset (watcher, 0, sizeof (struct EasyDbus_watcher));
00061
00062 watcher->watch = NULL;
00063 watcher->next = NULL;
00064
00065 return watcher;
00066 }
00067
00077 int
00078 easydbus_watcher_add (struct EasyDbus_core *core,
00079 struct EasyDbus_watcher *watcher)
00080 {
00081 struct EasyDbus_watcher *watch = NULL;
00082
00083 if (!core || !watcher)
00084 return -1;
00085
00086 if (!core->watcher) {
00087
00088 core->watcher = watcher;
00089 }
00090 else {
00091 watch = core->watcher;
00092 while (watch->next)
00093 watch = watch->next;
00094 watch->next = watcher;
00095 }
00096 watcher->core = core;
00097 if (dbus_watch_get_enabled (watcher->watch))
00098 core->user_data.watcher_add_cb (watcher,
00099 core->user_data.watcher_closure);
00100 return 0;
00101 }
00102
00112 int
00113 easydbus_watcher_remove_dbus_watch (struct EasyDbus_core *core,
00114 DBusWatch * watch)
00115 {
00116 struct EasyDbus_watcher *current, *prev;
00117
00118 if (!watch || !core)
00119 return -1;
00120
00122 prev = current = core->watcher;
00123
00124 while (current) {
00125 if (watch == current->watch) {
00126 if (core->user_data.watcher_remove_cb &&
00127 dbus_watch_get_enabled (watch))
00128 core->user_data.watcher_remove_cb (current,
00129 core->user_data.watcher_closure);
00130 if (prev == current)
00131 core->watcher = current->next;
00132 else
00133 prev->next = current->next;
00134 free (current);
00135 return 0;
00136 }
00137 prev = current;
00138 current = current->next;
00139 }
00140
00141 return -1;
00142 }
00143
00155 struct EasyDbus_watcher *
00156 easydbus_watcher_get_from_fd (struct EasyDbus_watcher *list,
00157 int fd,
00158 enum easydbus_watcher_flags flags)
00159 {
00160 if (!list || fd < 0)
00161 return NULL;
00162
00163 while (list) {
00164 if (list->watch) {
00165 if (dbus_watch_get_enabled (list->watch))
00166 #if defined(__DBUS_VERSION__) && (__DBUS_VERSION__ <= 102)
00167 if (dbus_watch_get_fd (list->watch) == fd) {
00168 #else
00169 if (dbus_watch_get_unix_fd (list->watch) == fd) {
00170 #endif
00171 if (dbus_watch_get_flags (list->watch) &
00172 (flags & EASYDBUS_WATCHER_READABLE ? DBUS_WATCH_READABLE :
00173 DBUS_WATCH_WRITABLE))
00174 return list;
00175 }
00176 }
00177 list = list->next;
00178 }
00179
00180 return NULL;
00181 }
00182
00191 EasyDbus_watcher *
00192 easydbus_watcher_get_from_watch (EasyDbus_watcher *list,
00193 DBusWatch *watch)
00194 {
00195 if (list && watch)
00196 while (list) {
00197 if (list->watch == watch)
00198 return list;
00199 list = list->next;
00200 }
00201
00202 return NULL;
00203 }
00204
00214 unsigned int
00215 easydbus_watcher_get_flags (EasyDbus_watcher *watch)
00216 {
00217 int flags = EASYDBUS_WATCHER_INVALID;
00218 int easydbus_flags = EASYDBUS_WATCHER_INVALID;
00219
00220 if (!watch)
00221 return EASYDBUS_WATCHER_INVALID;
00222
00223 flags = dbus_watch_get_flags (watch->watch);
00224 if (flags & DBUS_WATCH_READABLE)
00225 easydbus_flags |= EASYDBUS_WATCHER_READABLE;
00226 if (flags & DBUS_WATCH_WRITABLE)
00227 easydbus_flags |= EASYDBUS_WATCHER_WRITABLE;
00228 if (flags & DBUS_WATCH_HANGUP)
00229 easydbus_flags |= EASYDBUS_WATCHER_HANGUP;
00230 if (flags & DBUS_WATCH_ERROR)
00231 easydbus_flags |= EASYDBUS_WATCHER_ERROR;
00232
00233 EasyDbusDebug ("EasyDbus_watcher (%p) flags are: %s%s%s%s",
00234 watch,
00235 flags & DBUS_WATCH_READABLE ? "readable " : "",
00236 flags & DBUS_WATCH_WRITABLE ? "writable " : "",
00237 flags & DBUS_WATCH_HANGUP ? "hangup " : "",
00238 flags & DBUS_WATCH_ERROR ? "error" : "");
00239
00240 return easydbus_flags;
00241 }
00242
00249 inline EasyDbus_conn *
00250 easydbus_watcher_get_conn (EasyDbus_watcher *watch)
00251 {
00252 return (watch != NULL ? (EasyDbus_conn *) watch->core : NULL);
00253 }
00254
00255