rename configuration file
[profile/ivi/pulseaudio-panda.git] / src / module-protocol-stub.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 General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   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 General Public License
17   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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <arpa/inet.h>
31 #include <unistd.h>
32
33 #include "module.h"
34 #include "socket-server.h"
35 #include "socket-util.h"
36 #include "util.h"
37 #include "modargs.h"
38
39 #ifdef USE_PROTOCOL_SIMPLE
40   #include "protocol-simple.h"
41   #define protocol_new pa_protocol_simple_new
42   #define protocol_free pa_protocol_simple_free
43   #define IPV4_PORT 4711
44   #define UNIX_SOCKET "/tmp/polypaudio/simple"
45   #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
46 #else
47   #ifdef USE_PROTOCOL_CLI
48     #include "protocol-cli.h" 
49     #define protocol_new pa_protocol_cli_new
50     #define protocol_free pa_protocol_cli_free
51     #define IPV4_PORT 4712
52     #define UNIX_SOCKET "/tmp/polypaudio/cli"
53     #define MODULE_ARGUMENTS 
54   #else
55     #ifdef USE_PROTOCOL_NATIVE
56       #include "protocol-native.h"
57       #define protocol_new pa_protocol_native_new
58       #define protocol_free pa_protocol_native_free
59       #define IPV4_PORT 4713
60       #define UNIX_SOCKET "/tmp/polypaudio/native"
61       #define MODULE_ARGUMENTS "public", "cookie",
62     #else
63       #ifdef USE_PROTOCOL_ESOUND
64         #include "protocol-esound.h"
65         #include "esound.h"
66         #define protocol_new pa_protocol_esound_new
67         #define protocol_free pa_protocol_esound_free
68         #define IPV4_PORT ESD_DEFAULT_PORT
69         #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
70         #define MODULE_ARGUMENTS "sink", "source", "public", "cookie",
71       #else
72         #error "Broken build system" 
73       #endif
74     #endif 
75   #endif
76 #endif
77
78 static const char* const valid_modargs[] = {
79     MODULE_ARGUMENTS
80 #ifdef USE_TCP_SOCKETS
81     "port",
82     "loopback",
83 #else
84     "socket",
85 #endif
86     NULL
87 };
88
89 static struct pa_socket_server *create_socket_server(struct pa_core *c, struct pa_modargs *ma) {
90     struct pa_socket_server *s;
91 #ifdef USE_TCP_SOCKETS
92     uint32_t loopback = 1, port = IPV4_PORT;
93
94     if (pa_modargs_get_value_u32(ma, "loopback", &loopback) < 0) {
95         fprintf(stderr, "loopback= expects a numerical argument.\n");
96         return NULL;
97     }
98
99     if (pa_modargs_get_value_u32(ma, "port", &port) < 0) {
100         fprintf(stderr, "port= expects a numerical argument.\n");
101         return NULL;
102     }
103     
104     if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port)))
105         return NULL;
106 #else
107     int r;
108     const char *p;
109
110     p = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
111     assert(p);
112
113     if (pa_unix_socket_make_secure_dir(p) < 0) {
114         fprintf(stderr, "Failed to create secure socket directory.\n");
115         return NULL;
116     }
117
118     if ((r = pa_unix_socket_remove_stale(p)) < 0) {
119         fprintf(stderr, "Failed to remove stale UNIX socket '%s': %s\n", p, strerror(errno));
120         return NULL;
121     }
122     
123     if (r)
124         fprintf(stderr, "Removed stale UNIX socket '%s'.", p);
125     
126     if (!(s = pa_socket_server_new_unix(c->mainloop, p)))
127         return NULL;
128     
129 #endif
130     return s;
131 }
132
133 int pa_module_init(struct pa_core *c, struct pa_module*m) {
134     struct pa_socket_server *s;
135     struct pa_modargs *ma = NULL;
136     int ret = -1;
137     assert(c && m);
138
139     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
140         fprintf(stderr, "Failed to parse module arguments\n");
141         goto finish;
142     }
143
144     if (!(s = create_socket_server(c, ma)))
145         goto finish;
146
147     if (!(m->userdata = protocol_new(c, s, m, ma))) {
148         pa_socket_server_free(s);
149         goto finish;
150     }
151
152     ret = 0;
153
154 finish:
155     if (ma)
156         pa_modargs_free(ma);
157
158     return ret;
159 }
160
161 void pa_module_done(struct pa_core *c, struct pa_module*m) {
162     assert(c && m);
163
164     protocol_free(m->userdata);
165 }