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_lastEvent; }
39 void setWatchedEvents(AvahiWatchEvent event);
46 QSocketNotifier* m_in;
47 QSocketNotifier* m_out;
49 AvahiWatchCallback m_callback;
50 AvahiWatchEvent m_lastEvent;
55 class AvahiTimeout : public QObject
60 AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata);
62 void update(const struct timeval* tv);
69 AvahiTimeoutCallback m_callback;
75 AvahiWatch::AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata) :
76 m_in(0), m_out(0), m_callback(callback), m_fd(fd), m_userdata(userdata)
78 setWatchedEvents(event);
81 void AvahiWatch::gotIn()
83 m_lastEvent = AVAHI_WATCH_IN;
84 m_callback(this,m_fd,m_lastEvent,m_userdata);
87 void AvahiWatch::gotOut()
89 m_lastEvent = AVAHI_WATCH_IN;
90 m_callback(this,m_fd,m_lastEvent,m_userdata);
93 void AvahiWatch::setWatchedEvents(AvahiWatchEvent event)
95 if (!(event & AVAHI_WATCH_IN)) { delete m_in; m_in=0; }
96 if (!(event & AVAHI_WATCH_OUT)) { delete m_out; m_out=0; }
97 if (event & AVAHI_WATCH_IN) {
98 m_in = new QSocketNotifier(m_fd,QSocketNotifier::Read, this);
99 connect(m_in,SIGNAL(activated(int)),SLOT(gotIn()));
101 if (event & AVAHI_WATCH_OUT) {
102 m_out = new QSocketNotifier(m_fd,QSocketNotifier::Write, this);
103 connect(m_out,SIGNAL(activated(int)),SLOT(gotOut()));
107 AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void *userdata) :
108 m_callback(callback), m_userdata(userdata)
110 connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
114 void AvahiTimeout::update(const struct timeval *tv)
119 gettimeofday(&now, 0);
120 m_timer.start((tv->tv_sec-now.tv_sec)*1000+(tv->tv_usec-now.tv_usec)/1000);
124 void AvahiTimeout::timeout()
126 m_callback(this,m_userdata);
129 static AvahiWatch* q_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback,
132 return new AvahiWatch(fd, event, callback, userdata);
135 static void q_watch_update(AvahiWatch *w, AvahiWatchEvent events)
137 w->setWatchedEvents(events);
140 static AvahiWatchEvent q_watch_get_events(AvahiWatch *w)
142 return w->getEvents();
145 static void q_watch_free(AvahiWatch *w)
150 static AvahiTimeout* q_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback,
153 return new AvahiTimeout(tv, callback, userdata);
156 static void q_timeout_update(AvahiTimeout *t, const struct timeval *tv)
161 static void q_timeout_free(AvahiTimeout *t)
167 void create_qt_poll(AvahiPoll *api)
170 api->watch_new = q_watch_new;
171 api->watch_free = q_watch_free;
172 api->watch_update = q_watch_update;
173 api->watch_get_events = q_watch_get_events;
175 api->timeout_new = q_timeout_new;
176 api->timeout_free = q_timeout_free;
177 api->timeout_update = q_timeout_update;
180 #include "qt-watch.moc"