]> code.delx.au - pulseaudio/blob - src/pulsecore/avahi-wrap.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / avahi-wrap.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/log.h>
31 #include <pulsecore/macro.h>
32
33 #include "avahi-wrap.h"
34
35 typedef struct {
36 AvahiPoll api;
37 pa_mainloop_api *mainloop;
38 } pa_avahi_poll;
39
40 struct AvahiWatch {
41 pa_io_event *io_event;
42 pa_avahi_poll *avahi_poll;
43 AvahiWatchEvent current_event;
44 AvahiWatchCallback callback;
45 void *userdata;
46 };
47
48 static AvahiWatchEvent translate_io_flags_back(pa_io_event_flags_t e) {
49 return
50 (e & PA_IO_EVENT_INPUT ? AVAHI_WATCH_IN : 0) |
51 (e & PA_IO_EVENT_OUTPUT ? AVAHI_WATCH_OUT : 0) |
52 (e & PA_IO_EVENT_ERROR ? AVAHI_WATCH_ERR : 0) |
53 (e & PA_IO_EVENT_HANGUP ? AVAHI_WATCH_HUP : 0);
54 }
55
56 static pa_io_event_flags_t translate_io_flags(AvahiWatchEvent e) {
57 return
58 (e & AVAHI_WATCH_IN ? PA_IO_EVENT_INPUT : 0) |
59 (e & AVAHI_WATCH_OUT ? PA_IO_EVENT_OUTPUT : 0) |
60 (e & AVAHI_WATCH_ERR ? PA_IO_EVENT_ERROR : 0) |
61 (e & AVAHI_WATCH_HUP ? PA_IO_EVENT_HANGUP : 0);
62 }
63
64 static void watch_callback(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
65 AvahiWatch *w = userdata;
66
67 pa_assert(a);
68 pa_assert(e);
69 pa_assert(w);
70
71 w->current_event = translate_io_flags_back(events);
72 w->callback(w, fd, w->current_event, w->userdata);
73 w->current_event = 0;
74 }
75
76 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
77 pa_avahi_poll *p;
78 AvahiWatch *w;
79
80 pa_assert(api);
81 pa_assert(fd >= 0);
82 pa_assert(callback);
83 pa_assert_se(p = api->userdata);
84
85 w = pa_xnew(AvahiWatch, 1);
86 w->avahi_poll = p;
87 w->current_event = 0;
88 w->callback = callback;
89 w->userdata = userdata;
90 w->io_event = p->mainloop->io_new(p->mainloop, fd, translate_io_flags(event), watch_callback, w);
91
92 return w;
93 }
94
95 static void watch_update(AvahiWatch *w, AvahiWatchEvent event) {
96 pa_assert(w);
97
98 w->avahi_poll->mainloop->io_enable(w->io_event, translate_io_flags(event));
99 }
100
101 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
102 pa_assert(w);
103
104 return w->current_event;
105 }
106
107 static void watch_free(AvahiWatch *w) {
108 pa_assert(w);
109
110 w->avahi_poll->mainloop->io_free(w->io_event);
111 pa_xfree(w);
112 }
113
114 struct AvahiTimeout {
115 pa_time_event *time_event;
116 pa_avahi_poll *avahi_poll;
117 AvahiTimeoutCallback callback;
118 void *userdata;
119 };
120
121 static void timeout_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
122 AvahiTimeout *t = userdata;
123
124 pa_assert(a);
125 pa_assert(e);
126 pa_assert(t);
127
128 t->callback(t, t->userdata);
129 }
130
131 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
132 pa_avahi_poll *p;
133 AvahiTimeout *t;
134
135 pa_assert(api);
136 pa_assert(callback);
137 pa_assert_se(p = api->userdata);
138
139 t = pa_xnew(AvahiTimeout, 1);
140 t->avahi_poll = p;
141 t->callback = callback;
142 t->userdata = userdata;
143
144 t->time_event = tv ? p->mainloop->time_new(p->mainloop, tv, timeout_callback, t) : NULL;
145
146 return t;
147 }
148
149 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
150 pa_assert(t);
151
152 if (t->time_event && tv)
153 t->avahi_poll->mainloop->time_restart(t->time_event, tv);
154 else if (!t->time_event && tv)
155 t->time_event = t->avahi_poll->mainloop->time_new(t->avahi_poll->mainloop, tv, timeout_callback, t);
156 else if (t->time_event && !tv) {
157 t->avahi_poll->mainloop->time_free(t->time_event);
158 t->time_event = NULL;
159 }
160 }
161
162 static void timeout_free(AvahiTimeout *t) {
163 pa_assert(t);
164
165 if (t->time_event)
166 t->avahi_poll->mainloop->time_free(t->time_event);
167 pa_xfree(t);
168 }
169
170 AvahiPoll* pa_avahi_poll_new(pa_mainloop_api *m) {
171 pa_avahi_poll *p;
172
173 pa_assert(m);
174
175 p = pa_xnew(pa_avahi_poll, 1);
176
177 p->api.userdata = p;
178 p->api.watch_new = watch_new;
179 p->api.watch_update = watch_update;
180 p->api.watch_get_events = watch_get_events;
181 p->api.watch_free = watch_free;
182 p->api.timeout_new = timeout_new;
183 p->api.timeout_update = timeout_update;
184 p->api.timeout_free = timeout_free;
185 p->mainloop = m;
186
187 return &p->api;
188 }
189
190 void pa_avahi_poll_free(AvahiPoll *api) {
191 pa_avahi_poll *p;
192 pa_assert(api);
193 pa_assert_se(p = api->userdata);
194
195 pa_xfree(p);
196 }
197