]> code.delx.au - pulseaudio/blob - polyp/module-protocol-stub.c
add version routines to polyplib
[pulseaudio] / polyp / 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 #if defined(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 #elif defined(USE_PROTOCOL_CLI)
47 #include "protocol-cli.h"
48 #define protocol_new pa_protocol_cli_new
49 #define protocol_free pa_protocol_cli_free
50 #define IPV4_PORT 4712
51 #define UNIX_SOCKET "/tmp/polypaudio/cli"
52 #define MODULE_ARGUMENTS
53 #elif defined(USE_PROTOCOL_NATIVE)
54 #include "protocol-native.h"
55 #define protocol_new pa_protocol_native_new
56 #define protocol_free pa_protocol_native_free
57 #define IPV4_PORT 4713
58 #define UNIX_SOCKET "/tmp/polypaudio/native"
59 #define MODULE_ARGUMENTS "public", "cookie",
60 #elif defined(USE_PROTOCOL_ESOUND)
61 #include "protocol-esound.h"
62 #include "esound.h"
63 #define protocol_new pa_protocol_esound_new
64 #define protocol_free pa_protocol_esound_free
65 #define IPV4_PORT ESD_DEFAULT_PORT
66 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
67 #define MODULE_ARGUMENTS "sink", "source", "public", "cookie",
68 #else
69 #error "Broken build system"
70 #endif
71
72 static const char* const valid_modargs[] = {
73 MODULE_ARGUMENTS
74 #ifdef USE_TCP_SOCKETS
75 "port",
76 "loopback",
77 #else
78 "socket",
79 #endif
80 NULL
81 };
82
83 static struct pa_socket_server *create_socket_server(struct pa_core *c, struct pa_modargs *ma) {
84 struct pa_socket_server *s;
85 #ifdef USE_TCP_SOCKETS
86 uint32_t loopback = 1, port = IPV4_PORT;
87
88 if (pa_modargs_get_value_u32(ma, "loopback", &loopback) < 0) {
89 fprintf(stderr, "loopback= expects a numerical argument.\n");
90 return NULL;
91 }
92
93 if (pa_modargs_get_value_u32(ma, "port", &port) < 0) {
94 fprintf(stderr, "port= expects a numerical argument.\n");
95 return NULL;
96 }
97
98 if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port)))
99 return NULL;
100 #else
101 int r;
102 const char *p;
103
104 p = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
105 assert(p);
106
107 if (pa_unix_socket_make_secure_dir(p) < 0) {
108 fprintf(stderr, "Failed to create secure socket directory.\n");
109 return NULL;
110 }
111
112 if ((r = pa_unix_socket_remove_stale(p)) < 0) {
113 fprintf(stderr, "Failed to remove stale UNIX socket '%s': %s\n", p, strerror(errno));
114 return NULL;
115 }
116
117 if (r)
118 fprintf(stderr, "Removed stale UNIX socket '%s'.", p);
119
120 if (!(s = pa_socket_server_new_unix(c->mainloop, p)))
121 return NULL;
122
123 #endif
124 return s;
125 }
126
127 int pa_module_init(struct pa_core *c, struct pa_module*m) {
128 struct pa_socket_server *s;
129 struct pa_modargs *ma = NULL;
130 int ret = -1;
131 assert(c && m);
132
133 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
134 fprintf(stderr, "Failed to parse module arguments\n");
135 goto finish;
136 }
137
138 if (!(s = create_socket_server(c, ma)))
139 goto finish;
140
141 if (!(m->userdata = protocol_new(c, s, m, ma))) {
142 pa_socket_server_unref(s);
143 goto finish;
144 }
145
146 ret = 0;
147
148 finish:
149 if (ma)
150 pa_modargs_free(ma);
151
152 return ret;
153 }
154
155 void pa_module_done(struct pa_core *c, struct pa_module*m) {
156 assert(c && m);
157
158 protocol_free(m->userdata);
159 }