2 This file is part of avahi.
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.
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.
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
22 #include <Qt/qsocketnotifier.h>
23 #include <Qt/qobject.h>
24 #include <Qt/qtimer.h>
26 #include <qsocketnotifier.h>
32 class AvahiWatch : public QObject
36 AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata);
38 AvahiWatchEvent getEvents() const { return m_incallback ? m_lastEvent : (AvahiWatchEvent)0; }
39 void setWatchedEvents(AvahiWatchEvent event);
46 QSocketNotifier* m_in;
47 QSocketNotifier* m_out;
49 AvahiWatchCallback m_callback;
50 AvahiWatchEvent m_lastEvent;
56 class AvahiTimeout : public QObject
61 AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata);
63 void update(const struct timeval* tv);
70 AvahiTimeoutCallback m_callback;
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)
79 setWatchedEvents(event);
82 void AvahiWatch::gotIn()
84 m_lastEvent = AVAHI_WATCH_IN;
86 m_callback(this,m_fd,m_lastEvent,m_userdata);
90 void AvahiWatch::gotOut()
92 m_lastEvent = AVAHI_WATCH_IN;
94 m_callback(this,m_fd,m_lastEvent,m_userdata);
98 void AvahiWatch::setWatchedEvents(AvahiWatchEvent event)
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()));
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()));
112 AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void *userdata) :
113 m_callback(callback), m_userdata(userdata)
115 connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
119 void AvahiTimeout::update(const struct timeval *tv)
124 gettimeofday(&now, 0);
125 m_timer.start((tv->tv_sec-now.tv_sec)*1000+(tv->tv_usec-now.tv_usec)/1000);
129 void AvahiTimeout::timeout()
131 m_callback(this,m_userdata);
134 static AvahiWatch* q_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback,
137 return new AvahiWatch(fd, event, callback, userdata);
140 static void q_watch_update(AvahiWatch *w, AvahiWatchEvent events)
142 w->setWatchedEvents(events);
145 static AvahiWatchEvent q_watch_get_events(AvahiWatch *w)
147 return w->getEvents();
150 static void q_watch_free(AvahiWatch *w)
155 static AvahiTimeout* q_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback,
158 return new AvahiTimeout(tv, callback, userdata);
161 static void q_timeout_update(AvahiTimeout *t, const struct timeval *tv)
166 static void q_timeout_free(AvahiTimeout *t)
171 static AvahiPoll qt_poll;
173 const AvahiPoll* avahi_qt_poll_get(void)
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;
181 qt_poll.timeout_new = q_timeout_new;
182 qt_poll.timeout_free = q_timeout_free;
183 qt_poll.timeout_update = q_timeout_update;
188 #include "qt-watch.moc4"
190 #include "qt-watch.moc3"