]> git.meshlink.io Git - catta/blob - avahi-qt/qt-watch.cpp
* Make a few small build fixes to the QT stuff so make distcheck passes.
[catta] / avahi-qt / qt-watch.cpp
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <sys/time.h>
23 #ifdef QT4
24 #include <Qt/qsocketnotifier.h>
25 #include <Qt/qobject.h>
26 #include <Qt/qtimer.h>
27 #else
28 #include <qsocketnotifier.h>
29 #include <qobject.h>
30 #include <qtimer.h>
31 #endif
32 #include "qt-watch.h"
33
34 class AvahiWatch : public QObject 
35 {
36     Q_OBJECT
37 public:
38     AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata);
39     ~AvahiWatch() {}
40     AvahiWatchEvent getEvents() const { return m_incallback ? m_lastEvent : (AvahiWatchEvent)0; }
41     void setWatchedEvents(AvahiWatchEvent event);
42
43 private slots:
44     void gotIn();
45     void gotOut();
46
47 private:
48     QSocketNotifier* m_in;
49     QSocketNotifier* m_out;
50     //FIXME: ERR and HUP?
51     AvahiWatchCallback m_callback;
52     AvahiWatchEvent m_lastEvent;
53     int m_fd;
54     void* m_userdata;
55     bool m_incallback;
56 };
57
58 class AvahiTimeout : public QObject 
59 {
60     Q_OBJECT
61     
62 public:
63     AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata);
64     ~AvahiTimeout() {}
65     void update(const struct timeval* tv);
66     
67 private slots:
68     void timeout();
69     
70 private:
71     QTimer m_timer;
72     AvahiTimeoutCallback m_callback;
73     void* m_userdata;
74 };
75
76
77
78 AvahiWatch::AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata) : 
79     m_in(0), m_out(0),  m_callback(callback), m_fd(fd), m_userdata(userdata), m_incallback(false)
80 {
81     setWatchedEvents(event);
82 }
83
84 void AvahiWatch::gotIn()
85 {
86     m_lastEvent = AVAHI_WATCH_IN;
87     m_incallback=true;
88     m_callback(this,m_fd,m_lastEvent,m_userdata);
89     m_incallback=false;
90 }
91
92 void AvahiWatch::gotOut()
93 {
94     m_lastEvent = AVAHI_WATCH_IN;
95     m_incallback=true;
96     m_callback(this,m_fd,m_lastEvent,m_userdata);
97     m_incallback=false;
98 }
99
100 void AvahiWatch::setWatchedEvents(AvahiWatchEvent event) 
101 {
102     if (!(event & AVAHI_WATCH_IN)) { delete m_in; m_in=0; }
103     if (!(event & AVAHI_WATCH_OUT)) { delete m_out; m_out=0; }
104     if (event & AVAHI_WATCH_IN) { 
105         m_in = new QSocketNotifier(m_fd,QSocketNotifier::Read, this);
106         connect(m_in,SIGNAL(activated(int)),SLOT(gotIn()));
107     }
108     if (event & AVAHI_WATCH_OUT) { 
109         m_out = new QSocketNotifier(m_fd,QSocketNotifier::Write, this);
110         connect(m_out,SIGNAL(activated(int)),SLOT(gotOut()));
111     }
112 }    
113
114 AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void *userdata) : 
115     m_callback(callback), m_userdata(userdata)
116 {
117     connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
118     update(tv);
119 }
120
121 void AvahiTimeout::update(const struct timeval *tv)
122 {
123     m_timer.stop();
124     if (tv) {
125         struct timeval now;
126         gettimeofday(&now, 0);
127         m_timer.start((tv->tv_sec-now.tv_sec)*1000+(tv->tv_usec-now.tv_usec)/1000);
128     }
129 }
130
131 void AvahiTimeout::timeout()
132 {
133     m_callback(this,m_userdata);
134 }
135
136 static AvahiWatch* q_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, 
137     void *userdata) 
138 {
139     return new AvahiWatch(fd, event, callback, userdata);
140 }
141
142 static void q_watch_update(AvahiWatch *w, AvahiWatchEvent events) 
143 {
144     w->setWatchedEvents(events);
145 }
146
147 static AvahiWatchEvent q_watch_get_events(AvahiWatch *w) 
148 {
149     return w->getEvents();
150 }
151     
152 static void q_watch_free(AvahiWatch *w) 
153 {
154     delete w;
155 }
156     
157 static AvahiTimeout* q_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, 
158     void *userdata) 
159 {
160     return new AvahiTimeout(tv, callback, userdata);
161 }
162
163 static void q_timeout_update(AvahiTimeout *t, const struct timeval *tv) 
164 {
165     t->update(tv);
166 }
167
168 static void q_timeout_free(AvahiTimeout *t) 
169 {
170     delete t;
171 }
172
173 static AvahiPoll qt_poll;
174
175 const AvahiPoll* avahi_qt_poll_get(void) 
176 {
177     qt_poll.userdata=0;
178     qt_poll.watch_new = q_watch_new;
179     qt_poll.watch_free = q_watch_free;
180     qt_poll.watch_update = q_watch_update;
181     qt_poll.watch_get_events = q_watch_get_events;
182     
183     qt_poll.timeout_new = q_timeout_new;
184     qt_poll.timeout_free = q_timeout_free;
185     qt_poll.timeout_update = q_timeout_update;
186     return &qt_poll;
187 }
188
189 #ifdef QT4
190 #include "qt-watch.moc4"
191 #else
192 #include "qt-watch.moc3"
193 #endif