summaryrefslogtreecommitdiffstats
path: root/avahi-qt/qt-watch.cpp
blob: dac9dcc7ea116d0328f219ca3be1b1bd619224b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/***
  This file is part of avahi.
 
  avahi is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.
 
  avahi is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
  Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with avahi; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#include <sys/time.h>
#ifdef QT4
#include <Qt/qsocketnotifier.h>
#include <Qt/qobject.h>
#include <Qt/qtimer.h>
#else
#include <qsocketnotifier.h>
#include <qobject.h>
#include <qtimer.h>
#endif
#include <avahi-common/timeval.h>
#include "qt-watch.h"

class AvahiWatch : public QObject 
{
    Q_OBJECT
public:
    AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata);
    ~AvahiWatch() {}
    AvahiWatchEvent getEvents() const { return m_incallback ? m_lastEvent : (AvahiWatchEvent)0; }
    void setWatchedEvents(AvahiWatchEvent event);

private slots:
    void gotIn();
    void gotOut();

private:
    QSocketNotifier* m_in;
    QSocketNotifier* m_out;
    //FIXME: ERR and HUP?
    AvahiWatchCallback m_callback;
    AvahiWatchEvent m_lastEvent;
    int m_fd;
    void* m_userdata;
    bool m_incallback;
};

class AvahiTimeout : public QObject 
{
    Q_OBJECT
    
public:
    AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata);
    ~AvahiTimeout() {}
    void update(const struct timeval* tv);
    
private slots:
    void timeout();
    
private:
    QTimer m_timer;
    AvahiTimeoutCallback m_callback;
    void* m_userdata;
};



AvahiWatch::AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata) : 
    m_in(0), m_out(0),  m_callback(callback), m_fd(fd), m_userdata(userdata), m_incallback(false)
{
    setWatchedEvents(event);
}

void AvahiWatch::gotIn()
{
    m_lastEvent = AVAHI_WATCH_IN;
    m_incallback=true;
    m_callback(this,m_fd,m_lastEvent,m_userdata);
    m_incallback=false;
}

void AvahiWatch::gotOut()
{
    m_lastEvent = AVAHI_WATCH_IN;
    m_incallback=true;
    m_callback(this,m_fd,m_lastEvent,m_userdata);
    m_incallback=false;
}

void AvahiWatch::setWatchedEvents(AvahiWatchEvent event) 
{
    if (!(event & AVAHI_WATCH_IN)) { delete m_in; m_in=0; }
    if (!(event & AVAHI_WATCH_OUT)) { delete m_out; m_out=0; }
    if (event & AVAHI_WATCH_IN) { 
	m_in = new QSocketNotifier(m_fd,QSocketNotifier::Read, this);
	connect(m_in,SIGNAL(activated(int)),SLOT(gotIn()));
    }
    if (event & AVAHI_WATCH_OUT) { 
	m_out = new QSocketNotifier(m_fd,QSocketNotifier::Write, this);
	connect(m_out,SIGNAL(activated(int)),SLOT(gotOut()));
    }
}    

AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void *userdata) : 
    m_callback(callback), m_userdata(userdata)
{
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
#ifdef QT4
    m_timer.setSingleShot(true);
#endif
    update(tv);
}

void AvahiTimeout::update(const struct timeval *tv)
{
    m_timer.stop();
    if (tv) {
    AvahiUsec u = avahi_age(tv)/1000;
#ifdef QT4
    m_timer.start( (u>0) ? 0 : -u);
#else
    m_timer.start( (u>0) ? 0 : -u,true);
#endif
    }
}

void AvahiTimeout::timeout()
{
    m_callback(this,m_userdata);
}

static AvahiWatch* q_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, 
    void *userdata) 
{
    return new AvahiWatch(fd, event, callback, userdata);
}

static void q_watch_update(AvahiWatch *w, AvahiWatchEvent events) 
{
    w->setWatchedEvents(events);
}

static AvahiWatchEvent q_watch_get_events(AvahiWatch *w) 
{
    return w->getEvents();
}
    
static void q_watch_free(AvahiWatch *w) 
{
    delete w;
}
    
static AvahiTimeout* q_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, 
    void *userdata) 
{
    return new AvahiTimeout(tv, callback, userdata);
}

static void q_timeout_update(AvahiTimeout *t, const struct timeval *tv) 
{
    t->update(tv);
}

static void q_timeout_free(AvahiTimeout *t) 
{
    delete t;
}

const AvahiPoll* avahi_qt_poll_get(void) 
{
    static const AvahiPoll qt_poll = {
        NULL,
        q_watch_new,
        q_watch_update,
        q_watch_get_events,
        q_watch_free,
        q_timeout_new,
        q_timeout_update,
        q_timeout_free
    };

    return &qt_poll;
}

#ifdef QT4
#include "qt-watch.moc4"
#else
#include "qt-watch.moc3"
#endif