1a64f43cbb081fe48951f101f6bde7b7f2b5ac36
[profile/ivi/pulseaudio.git] / src / modules / dbus / iface-device.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2009 Tanu Kaskinen
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 published
8   by the Free Software Foundation; either version 2.1 of the License,
9   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 License
17   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 <pulsecore/core-util.h>
27 #include <pulsecore/protocol-dbus.h>
28
29 #include "iface-device.h"
30
31 #define SINK_OBJECT_NAME "sink"
32 #define SOURCE_OBJECT_NAME "source"
33
34 enum device_type {
35     DEVICE_TYPE_SINK,
36     DEVICE_TYPE_SOURCE
37 };
38
39 struct pa_dbusiface_device {
40     union {
41         pa_sink *sink;
42         pa_source *source;
43     };
44     enum device_type type;
45     char *path;
46 };
47
48 pa_dbusiface_device *pa_dbusiface_device_new_sink(pa_dbusiface_core *core, pa_sink *sink) {
49     pa_dbusiface_device *d;
50
51     pa_assert(core);
52     pa_assert(sink);
53
54     d = pa_xnew(pa_dbusiface_device, 1);
55     d->sink = pa_sink_ref(sink);
56     d->type = DEVICE_TYPE_SINK;
57     d->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, SINK_OBJECT_NAME, sink->index);
58
59     return d;
60 }
61
62 pa_dbusiface_device *pa_dbusiface_device_new_source(pa_dbusiface_core *core, pa_source *source) {
63     pa_dbusiface_device *d;
64
65     pa_assert(core);
66     pa_assert(source);
67
68     d = pa_xnew(pa_dbusiface_device, 1);
69     d->source = pa_source_ref(source);
70     d->type = DEVICE_TYPE_SOURCE;
71     d->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, SOURCE_OBJECT_NAME, source->index);
72
73     return d;
74 }
75
76 void pa_dbusiface_device_free(pa_dbusiface_device *d) {
77     pa_assert(d);
78
79     if (d->type == DEVICE_TYPE_SINK)
80         pa_sink_unref(d->sink);
81     else
82         pa_source_unref(d->source);
83
84     pa_xfree(d->path);
85     pa_xfree(d);
86 }
87
88 const char *pa_dbusiface_device_get_path(pa_dbusiface_device *d) {
89     pa_assert(d);
90
91     return d->path;
92 }
93
94 pa_sink *pa_dbusiface_device_get_sink(pa_dbusiface_device *d) {
95     pa_assert(d);
96     pa_assert(d->type == DEVICE_TYPE_SINK);
97
98     return d->sink;
99 }
100
101 pa_source *pa_dbusiface_device_get_source(pa_dbusiface_device *d) {
102     pa_assert(d);
103     pa_assert(d->type == DEVICE_TYPE_SOURCE);
104
105     return d->source;
106 }