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