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