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>
30 #include <avahi-common/timeval.h>
33 class AvahiWatch : public QObject
37 AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata);
39 AvahiWatchEvent getEvents() const { return m_incallback ? m_lastEvent : (AvahiWatchEvent)0; }
40 void setWatchedEvents(AvahiWatchEvent event);
47 QSocketNotifier* m_in;
48 QSocketNotifier* m_out;
50 AvahiWatchCallback m_callback;
51 AvahiWatchEvent m_lastEvent;
57 class AvahiTimeout : public QObject
62 AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata);
64 void update(const struct timeval* tv);
71 AvahiTimeoutCallback m_callback;
77 AvahiWatch::AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata) :
78 m_in(0), m_out(0), m_callback(callback), m_fd(fd), m_userdata(userdata), m_incallback(false)
80 setWatchedEvents(event);
83 void AvahiWatch::gotIn()
85 m_lastEvent = AVAHI_WATCH_IN;
87 m_callback(this,m_fd,m_lastEvent,m_userdata);
91 void AvahiWatch::gotOut()
93 m_lastEvent = AVAHI_WATCH_IN;
95 m_callback(this,m_fd,m_lastEvent,m_userdata);
99 void AvahiWatch::setWatchedEvents(AvahiWatchEvent event)
101 if (!(event & AVAHI_WATCH_IN)) { delete m_in; m_in=0; }
102 if (!(event & AVAHI_WATCH_OUT)) { delete m_out; m_out=0; }
103 if (event & AVAHI_WATCH_IN) {
104 m_in = new QSocketNotifier(m_fd,QSocketNotifier::Read, this);
105 connect(m_in,SIGNAL(activated(int)),SLOT(gotIn()));
107 if (event & AVAHI_WATCH_OUT) {
108 m_out = new QSocketNotifier(m_fd,QSocketNotifier::Write, this);
109 connect(m_out,SIGNAL(activated(int)),SLOT(gotOut()));
113 AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void *userdata) :
114 m_callback(callback), m_userdata(userdata)
116 connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
118 m_timer.setSingleShot(true);
123 void AvahiTimeout::update(const struct timeval *tv)
127 AvahiUsec u = avahi_age(tv)/1000;
129 m_timer.start( (u>0) ? 0 : -u);
131 m_timer.start( (u>0) ? 0 : -u,true);
136 void AvahiTimeout::timeout()
138 m_callback(this,m_userdata);
141 static AvahiWatch* q_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback,
144 return new AvahiWatch(fd, event, callback, userdata);
147 static void q_watch_update(AvahiWatch *w, AvahiWatchEvent events)
149 w->setWatchedEvents(events);
152 static AvahiWatchEvent q_watch_get_events(AvahiWatch *w)
154 return w->getEvents();
157 static void q_watch_free(AvahiWatch *w)
162 static AvahiTimeout* q_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback,
165 return new AvahiTimeout(tv, callback, userdata);
168 static void q_timeout_update(AvahiTimeout *t, const struct timeval *tv)
173 static void q_timeout_free(AvahiTimeout *t)
178 const AvahiPoll* avahi_qt_poll_get(void)
180 static const AvahiPoll qt_poll = {
195 #include "qt-watch.moc4"
197 #include "qt-watch.moc3"