]> code.delx.au - pulseaudio/blob - src/pulsecore/avahi-wrap.c
855ed5671c854497617102c581eb087157b4e59d
[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 #include <assert.h>
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/log.h>
29
30 #include "avahi-wrap.h"
31
32 typedef struct {
33 AvahiPoll api;
34 pa_mainloop_api *mainloop;
35 } pa_avahi_poll;
36
37 struct AvahiWatch {
38 pa_io_event *io_event;
39 pa_avahi_poll *avahi_poll;
40 AvahiWatchEvent current_event;
41 AvahiWatchCallback callback;
42 void *userdata;
43 };
44
45 static AvahiWatchEvent translate_io_flags_back(pa_io_event_flags_t e) {
46 return
47 (e & PA_IO_EVENT_INPUT ? AVAHI_WATCH_IN : 0) |
48 (e & PA_IO_EVENT_OUTPUT ? AVAHI_WATCH_OUT : 0) |
49 (e & PA_IO_EVENT_ERROR ? AVAHI_WATCH_ERR : 0) |
50 (e & PA_IO_EVENT_HANGUP ? AVAHI_WATCH_HUP : 0);
51 }
52
53 static pa_io_event_flags_t translate_io_flags(AvahiWatchEvent e) {
54 return
55 (e & AVAHI_WATCH_IN ? PA_IO_EVENT_INPUT : 0) |
56 (e & AVAHI_WATCH_OUT ? PA_IO_EVENT_OUTPUT : 0) |
57 (e & AVAHI_WATCH_ERR ? PA_IO_EVENT_ERROR : 0) |
58 (e & AVAHI_WATCH_HUP ? PA_IO_EVENT_HANGUP : 0);
59 }
60
61 static void watch_callback(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
62 AvahiWatch *w = userdata;
63
64 assert(a);
65 assert(e);
66 assert(w);
67
68 w->current_event = translate_io_flags_back(events);
69 w->callback(w, fd, w->current_event, w->userdata);
70 w->current_event = 0;
71 }
72
73 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
74 pa_avahi_poll *p;
75 AvahiWatch *w;
76
77 assert(api);
78 assert(fd >= 0);
79 assert(callback);
80
81 p = api->userdata;
82 assert(p);
83
84 w = pa_xnew(AvahiWatch, 1);
85 w->avahi_poll = p;
86 w->current_event = 0;
87 w->callback = callback;
88 w->userdata = userdata;
89 w->io_event = p->mainloop->io_new(p->mainloop, fd, translate_io_flags(event), watch_callback, w);
90
91 return w;
92 }
93
94 static void watch_update(AvahiWatch *w, AvahiWatchEvent event) {
95 assert(w);
96
97 w->avahi_poll->mainloop->io_enable(w->io_event, translate_io_flags(event));
98 }
99
100 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
101 assert(w);
102
103 return w->current_event;
104 }
105
106 static void watch_free(AvahiWatch *w) {
107 assert(w);
108
109 w->avahi_poll->mainloop->io_free(w->io_event);
110 pa_xfree(w);
111 }
112
113 struct AvahiTimeout {
114 pa_time_event *time_event;
115 pa_avahi_poll *avahi_poll;
116 AvahiTimeoutCallback callback;
117 void *userdata;
118 };
119
120 static void timeout_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
121 AvahiTimeout *t = userdata;
122
123 assert(a);
124 assert(e);
125 assert(t);
126
127 t->callback(t, t->userdata);
128 }
129
130 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
131 pa_avahi_poll *p;
132 AvahiTimeout *t;
133
134 assert(api);
135 assert(callback);
136
137 p = api->userdata;
138 assert(p);
139
140 t = pa_xnew(AvahiTimeout, 1);
141 t->avahi_poll = p;
142 t->callback = callback;
143 t->userdata = userdata;
144
145 t->time_event = tv ? p->mainloop->time_new(p->mainloop, tv, timeout_callback, t) : NULL;
146
147 return t;
148 }
149
150 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
151 assert(t);
152
153 if (t->time_event && tv)
154 t->avahi_poll->mainloop->time_restart(t->time_event, tv);
155 else if (!t->time_event && tv)
156 t->time_event = t->avahi_poll->mainloop->time_new(t->avahi_poll->mainloop, tv, timeout_callback, t);
157 else if (t->time_event && !tv) {
158 t->avahi_poll->mainloop->time_free(t->time_event);
159 t->time_event = NULL;
160 }
161 }
162
163 static void timeout_free(AvahiTimeout *t) {
164 assert(t);
165
166 if (t->time_event)
167 t->avahi_poll->mainloop->time_free(t->time_event);
168 pa_xfree(t);
169 }
170
171 AvahiPoll* pa_avahi_poll_new(pa_mainloop_api *m) {
172 pa_avahi_poll *p;
173
174 assert(m);
175
176 p = pa_xnew(pa_avahi_poll, 1);
177
178 p->api.userdata = p;
179 p->api.watch_new = watch_new;
180 p->api.watch_update = watch_update;
181 p->api.watch_get_events = watch_get_events;
182 p->api.watch_free = watch_free;
183 p->api.timeout_new = timeout_new;
184 p->api.timeout_update = timeout_update;
185 p->api.timeout_free = timeout_free;
186 p->mainloop = m;
187
188 return &p->api;
189 }
190
191 void pa_avahi_poll_free(AvahiPoll *api) {
192 pa_avahi_poll *p;
193 assert(api);
194 p = api->userdata;
195 assert(p);
196
197 pa_xfree(p);
198 }
199