watcher.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus: DBUS Binding Library.
00003  Copyright (C) 2007  Daniele Rondina aka ge@@ru, geaaru@gmail.com 
00004 
00005  This program is free software; you can redistribute it and/or
00006  modify it under the terms of the GNU General Public License
00007  as published by the Free Software Foundation; either version 2
00008  of the License, or (at your option) any later version.
00009 
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  GNU General Public License for more details.
00014 
00015  You should have received a copy of the GNU General Public License
00016  along with this program; if not, write to the Free Software
00017  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 
00019  Filename:  watcher.c
00020  
00021  Description:  
00022  
00023  Version:  1.0
00024  Created:  09/20/07 19:02:41 CEST
00025  Revision:  none
00026  
00027  Author:   Daniele Rondina aka Ge@@ru (geaaru@gmail.com) 
00028  License:  GPL 2.0
00029 */
00030 
00035 #include <string.h>
00036 #include <stdlib.h>
00037 // easydbus includes
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       // no watcher
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 // vim: ts=3 shiftwidth=3 expandtab

Generated on Thu Apr 10 10:00:18 2008 for EasyDbus-0.2 by  doxygen 1.5.4