don't send SCM_CREDENTIALS on every sendmsg(), instead do it only on handshake
[pulseaudio.git] / src / pulsecore / avahi-wrap.c
blob80256a124b76a2b551f42e6c11b6a76b4b765424
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
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 of the
9 License, or (at your option) any later version.
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.
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 ***/
22 #include <assert.h>
24 #include <pulse/xmalloc.h>
26 #include <pulsecore/log.h>
28 #include "avahi-wrap.h"
30 typedef struct {
31 AvahiPoll api;
32 pa_mainloop_api *mainloop;
33 } pa_avahi_poll;
35 struct AvahiWatch {
36 pa_io_event *io_event;
37 pa_avahi_poll *avahi_poll;
38 AvahiWatchEvent current_event;
39 AvahiWatchCallback callback;
40 void *userdata;
43 static AvahiWatchEvent translate_io_flags_back(pa_io_event_flags_t e) {
44 return
45 (e & PA_IO_EVENT_INPUT ? AVAHI_WATCH_IN : 0) |
46 (e & PA_IO_EVENT_OUTPUT ? AVAHI_WATCH_OUT : 0) |
47 (e & PA_IO_EVENT_ERROR ? AVAHI_WATCH_ERR : 0) |
48 (e & PA_IO_EVENT_HANGUP ? AVAHI_WATCH_HUP : 0);
51 static pa_io_event_flags_t translate_io_flags(AvahiWatchEvent e) {
52 return
53 (e & AVAHI_WATCH_IN ? PA_IO_EVENT_INPUT : 0) |
54 (e & AVAHI_WATCH_OUT ? PA_IO_EVENT_OUTPUT : 0) |
55 (e & AVAHI_WATCH_ERR ? PA_IO_EVENT_ERROR : 0) |
56 (e & AVAHI_WATCH_HUP ? PA_IO_EVENT_HANGUP : 0);
59 static void watch_callback(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
60 AvahiWatch *w = userdata;
62 assert(a);
63 assert(e);
64 assert(w);
66 w->current_event = translate_io_flags_back(events);
67 w->callback(w, fd, w->current_event, w->userdata);
68 w->current_event = 0;
71 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
72 pa_avahi_poll *p;
73 AvahiWatch *w;
75 assert(api);
76 assert(fd >= 0);
77 assert(callback);
79 p = api->userdata;
80 assert(p);
82 w = pa_xnew(AvahiWatch, 1);
83 w->avahi_poll = p;
84 w->current_event = 0;
85 w->callback = callback;
86 w->userdata = userdata;
87 w->io_event = p->mainloop->io_new(p->mainloop, fd, translate_io_flags(event), watch_callback, w);
89 return w;
92 static void watch_update(AvahiWatch *w, AvahiWatchEvent event) {
93 assert(w);
95 w->avahi_poll->mainloop->io_enable(w->io_event, translate_io_flags(event));
98 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
99 assert(w);
101 return w->current_event;
104 static void watch_free(AvahiWatch *w) {
105 assert(w);
107 w->avahi_poll->mainloop->io_free(w->io_event);
108 pa_xfree(w);
111 struct AvahiTimeout {
112 pa_time_event *time_event;
113 pa_avahi_poll *avahi_poll;
114 AvahiTimeoutCallback callback;
115 void *userdata;
118 static void timeout_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
119 AvahiTimeout *t = userdata;
121 assert(a);
122 assert(e);
123 assert(t);
125 t->callback(t, t->userdata);
128 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
129 pa_avahi_poll *p;
130 AvahiTimeout *t;
132 assert(api);
133 assert(callback);
135 p = api->userdata;
136 assert(p);
138 t = pa_xnew(AvahiTimeout, 1);
139 t->avahi_poll = p;
140 t->callback = callback;
141 t->userdata = userdata;
143 t->time_event = tv ? p->mainloop->time_new(p->mainloop, tv, timeout_callback, t) : NULL;
145 return t;
148 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
149 assert(t);
151 if (t->time_event && tv)
152 t->avahi_poll->mainloop->time_restart(t->time_event, tv);
153 else if (!t->time_event && tv)
154 t->time_event = t->avahi_poll->mainloop->time_new(t->avahi_poll->mainloop, tv, timeout_callback, t);
155 else if (t->time_event && !tv) {
156 t->avahi_poll->mainloop->time_free(t->time_event);
157 t->time_event = NULL;
161 static void timeout_free(AvahiTimeout *t) {
162 assert(t);
164 if (t->time_event)
165 t->avahi_poll->mainloop->time_free(t->time_event);
166 pa_xfree(t);
169 AvahiPoll* pa_avahi_poll_new(pa_mainloop_api *m) {
170 pa_avahi_poll *p;
172 assert(m);
174 p = pa_xnew(pa_avahi_poll, 1);
176 p->api.userdata = p;
177 p->api.watch_new = watch_new;
178 p->api.watch_update = watch_update;
179 p->api.watch_get_events = watch_get_events;
180 p->api.watch_free = watch_free;
181 p->api.timeout_new = timeout_new;
182 p->api.timeout_update = timeout_update;
183 p->api.timeout_free = timeout_free;
184 p->mainloop = m;
186 return &p->api;
189 void pa_avahi_poll_free(AvahiPoll *api) {
190 pa_avahi_poll *p;
191 assert(api);
192 p = api->userdata;
193 assert(p);
195 pa_xfree(p);