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