summaryrefslogtreecommitdiffstats
path: root/src/modules/howl-wrap.c
blob: e56fca3e76ad7027d90b2977b477f327b7533394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* $Id$ */

/***
  This file is part of PulseAudio.
 
  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2 of the
  License, or (at your option) any later version.
 
  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#include <assert.h>

#include <pulse/xmalloc.h>

#include <pulsecore/log.h>
#include <pulsecore/props.h>

#include "howl-wrap.h"

#define HOWL_PROPERTY "howl"

struct pa_howl_wrapper {
    pa_core *core;
    int ref;

    pa_io_event *io_event;
    sw_discovery discovery;
};

static void howl_io_event(pa_mainloop_api*m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
    pa_howl_wrapper *w = userdata;
    assert(m && e && fd >= 0 && w && w->ref >= 1);

    if (f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
        goto fail;

    if (sw_discovery_read_socket(w->discovery) != SW_OKAY)
        goto fail;

    return;

fail:
    pa_log_error(__FILE__": howl connection died.");
    w->core->mainloop->io_free(w->io_event);
    w->io_event = NULL;
}

static pa_howl_wrapper* howl_wrapper_new(pa_core *c) {
    pa_howl_wrapper *h;
    sw_discovery session;
    assert(c);

    if (sw_discovery_init(&session) != SW_OKAY) {
        pa_log_error(__FILE__": sw_discovery_init() failed.");
        return NULL;
    }

    h = pa_xmalloc(sizeof(pa_howl_wrapper));
    h->core = c;
    h->ref = 1;
    h->discovery = session;

    h->io_event = c->mainloop->io_new(c->mainloop, sw_discovery_socket(session), PA_IO_EVENT_INPUT, howl_io_event, h);

    return h;
}

static void howl_wrapper_free(pa_howl_wrapper *h) {
    assert(h);

    sw_discovery_fina(h->discovery);

    if (h->io_event)
        h->core->mainloop->io_free(h->io_event);

    pa_xfree(h);
}

pa_howl_wrapper* pa_howl_wrapper_get(pa_core *c) {
    pa_howl_wrapper *h;
    assert(c);
    
    if ((h = pa_property_get(c, HOWL_PROPERTY)))
        return pa_howl_wrapper_ref(h);

    return howl_wrapper_new(c);
}

pa_howl_wrapper* pa_howl_wrapper_ref(pa_howl_wrapper *h) {
    assert(h && h->ref >= 1);
    h->ref++;
    return h;
}

void pa_howl_wrapper_unref(pa_howl_wrapper *h) {
    assert(h && h->ref >= 1);
    if (!(--h->ref))
        howl_wrapper_free(h);
}

sw_discovery pa_howl_wrapper_get_discovery(pa_howl_wrapper *h) {
    assert(h && h->ref >= 1);

    return h->discovery;
}