00001 /* 00002 * 00003 * D-Bus++ - C++ bindings for D-Bus 00004 * 00005 * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com> 00006 * 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 * 00022 */ 00023 00024 00025 #ifndef __DBUSXX_EVENTLOOP_H 00026 #define __DBUSXX_EVENTLOOP_H 00027 00028 #include <pthread.h> 00029 #include <list> 00030 00031 #include "api.h" 00032 #include "util.h" 00033 00034 namespace DBus { 00035 00036 /* 00037 * these Default *classes implement a very simple event loop which 00038 * is used here as the default main loop, if you want to hook 00039 * a different one use the Bus *classes in eventloop-integration.h 00040 * or the Glib::Bus *classes as a reference 00041 */ 00042 00043 class DefaultMainLoop; 00044 00045 class DXXAPI DefaultTimeout 00046 { 00047 public: 00048 00049 DefaultTimeout(int interval, bool repeat, DefaultMainLoop *); 00050 00051 virtual ~DefaultTimeout(); 00052 00053 bool enabled(){ return _enabled; } 00054 void enabled(bool e){ _enabled = e; } 00055 00056 int interval(){ return _interval; } 00057 void interval(int i){ _interval = i; } 00058 00059 bool repeat(){ return _repeat; } 00060 void repeat(bool r){ _repeat = r; } 00061 00062 void *data(){ return _data; } 00063 void data(void *d){ _data = d; } 00064 00065 Slot<void, DefaultTimeout &> expired; 00066 00067 private: 00068 00069 bool _enabled; 00070 00071 int _interval; 00072 bool _repeat; 00073 00074 double _expiration; 00075 00076 void *_data; 00077 00078 DefaultMainLoop *_disp; 00079 00080 friend class DefaultMainLoop; 00081 }; 00082 00083 typedef std::list< DefaultTimeout *> DefaultTimeouts; 00084 00085 class DXXAPI DefaultWatch 00086 { 00087 public: 00088 00089 DefaultWatch(int fd, int flags, DefaultMainLoop *); 00090 00091 virtual ~DefaultWatch(); 00092 00093 bool enabled(){ return _enabled; } 00094 void enabled(bool e){ _enabled = e; } 00095 00096 int descriptor(){ return _fd; } 00097 00098 int flags(){ return _flags; } 00099 void flags(int f){ _flags = f; } 00100 00101 int state(){ return _state; } 00102 00103 void *data(){ return _data; } 00104 void data(void *d){ _data = d; } 00105 00106 Slot<void, DefaultWatch &> ready; 00107 00108 private: 00109 00110 bool _enabled; 00111 00112 int _fd; 00113 int _flags; 00114 int _state; 00115 00116 void *_data; 00117 00118 DefaultMainLoop *_disp; 00119 00120 friend class DefaultMainLoop; 00121 }; 00122 00123 typedef std::list< DefaultWatch *> DefaultWatches; 00124 00125 class DXXAPI DefaultMutex 00126 { 00127 public: 00128 00129 DefaultMutex(); 00130 00131 ~DefaultMutex(); 00132 00133 void lock(); 00134 00135 void unlock(); 00136 00137 private: 00138 00139 class Attr; 00140 00141 pthread_mutex_t _mutex; 00142 }; 00143 00144 class DXXAPI DefaultMainLoop 00145 { 00146 public: 00147 00148 DefaultMainLoop(); 00149 00150 virtual ~DefaultMainLoop(); 00151 00152 virtual void dispatch(); 00153 00154 private: 00155 00156 DefaultMutex _mutex_t; 00157 DefaultTimeouts _timeouts; 00158 00159 DefaultMutex _mutex_w; 00160 DefaultWatches _watches; 00161 00162 friend class DefaultTimeout; 00163 friend class DefaultWatch; 00164 }; 00165 00166 } /* namespace DBus */ 00167 00168 #endif//__DBUSXX_EVENTLOOP_H