]> code.delx.au - pulseaudio/blob - src/modules/howl-wrap.c
e56fca3e76ad7027d90b2977b477f327b7533394
[pulseaudio] / src / modules / howl-wrap.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 #include <assert.h>
23
24 #include <pulse/xmalloc.h>
25
26 #include <pulsecore/log.h>
27 #include <pulsecore/props.h>
28
29 #include "howl-wrap.h"
30
31 #define HOWL_PROPERTY "howl"
32
33 struct pa_howl_wrapper {
34 pa_core *core;
35 int ref;
36
37 pa_io_event *io_event;
38 sw_discovery discovery;
39 };
40
41 static void howl_io_event(pa_mainloop_api*m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
42 pa_howl_wrapper *w = userdata;
43 assert(m && e && fd >= 0 && w && w->ref >= 1);
44
45 if (f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
46 goto fail;
47
48 if (sw_discovery_read_socket(w->discovery) != SW_OKAY)
49 goto fail;
50
51 return;
52
53 fail:
54 pa_log_error(__FILE__": howl connection died.");
55 w->core->mainloop->io_free(w->io_event);
56 w->io_event = NULL;
57 }
58
59 static pa_howl_wrapper* howl_wrapper_new(pa_core *c) {
60 pa_howl_wrapper *h;
61 sw_discovery session;
62 assert(c);
63
64 if (sw_discovery_init(&session) != SW_OKAY) {
65 pa_log_error(__FILE__": sw_discovery_init() failed.");
66 return NULL;
67 }
68
69 h = pa_xmalloc(sizeof(pa_howl_wrapper));
70 h->core = c;
71 h->ref = 1;
72 h->discovery = session;
73
74 h->io_event = c->mainloop->io_new(c->mainloop, sw_discovery_socket(session), PA_IO_EVENT_INPUT, howl_io_event, h);
75
76 return h;
77 }
78
79 static void howl_wrapper_free(pa_howl_wrapper *h) {
80 assert(h);
81
82 sw_discovery_fina(h->discovery);
83
84 if (h->io_event)
85 h->core->mainloop->io_free(h->io_event);
86
87 pa_xfree(h);
88 }
89
90 pa_howl_wrapper* pa_howl_wrapper_get(pa_core *c) {
91 pa_howl_wrapper *h;
92 assert(c);
93
94 if ((h = pa_property_get(c, HOWL_PROPERTY)))
95 return pa_howl_wrapper_ref(h);
96
97 return howl_wrapper_new(c);
98 }
99
100 pa_howl_wrapper* pa_howl_wrapper_ref(pa_howl_wrapper *h) {
101 assert(h && h->ref >= 1);
102 h->ref++;
103 return h;
104 }
105
106 void pa_howl_wrapper_unref(pa_howl_wrapper *h) {
107 assert(h && h->ref >= 1);
108 if (!(--h->ref))
109 howl_wrapper_free(h);
110 }
111
112 sw_discovery pa_howl_wrapper_get_discovery(pa_howl_wrapper *h) {
113 assert(h && h->ref >= 1);
114
115 return h->discovery;
116 }
117