0547f7e60e3fa4200acf7e4ebd4036afaeadcc4b
[profile/ivi/pulseaudio.git] / src / module-protocol-stub.c
1 #include <string.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <assert.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7
8 #include "module.h"
9 #include "socket-server.h"
10 #include "socket-util.h"
11 #include "util.h"
12
13 #ifdef USE_PROTOCOL_SIMPLE
14   #include "protocol-simple.h"
15   #define protocol_free pa_protocol_simple_free
16   #define IPV4_PORT 4711
17   #define UNIX_SOCKET_DIR "/tmp/polypaudio"
18   #define UNIX_SOCKET "/tmp/polypaudio/simple"
19 #else
20   #ifdef USE_PROTOCOL_CLI
21     #include "protocol-cli.h" 
22     #define protocol_new pa_protocol_cli_new
23     #define protocol_free pa_protocol_cli_free
24     #define IPV4_PORT 4712
25     #define UNIX_SOCKET_DIR "/tmp/polypaudio"
26     #define UNIX_SOCKET "/tmp/polypaudio/cli"
27   #else
28     #ifdef USE_PROTOCOL_NATIVE
29       #include "protocol-native.h"
30       #define protocol_new pa_protocol_native_new
31       #define protocol_free pa_protocol_native_free
32       #define IPV4_PORT 4713
33       #define UNIX_SOCKET_DIR "/tmp/polypaudio"
34       #define UNIX_SOCKET "/tmp/polypaudio/native"
35     #else
36       #ifdef USE_PROTOCOL_ESOUND
37         #include "protocol-esound.h"
38         #include "esound-spec.h"
39         #define protocol_new pa_protocol_esound_new
40         #define protocol_free pa_protocol_esound_free
41         #define IPV4_PORT ESD_DEFAULT_PORT
42         #define UNIX_SOCKET_DIR ESD_UNIX_SOCKET_DIR
43         #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
44       #else
45         #error "Broken build system" 
46       #endif
47     #endif 
48   #endif
49 #endif
50
51 int pa_module_init(struct pa_core *c, struct pa_module*m) {
52     struct pa_socket_server *s;
53     assert(c && m);
54
55 #ifdef USE_TCP_SOCKETS
56     if (!(s = pa_socket_server_new_ipv4(c->mainloop, INADDR_ANY, IPV4_PORT)))
57         return -1;
58 #else
59     if (pa_make_secure_dir(UNIX_SOCKET_DIR) < 0) {
60         fprintf(stderr, "Failed to create secure socket directory.\n");
61         return -1;
62     }
63
64     {
65         int r;
66         if ((r = pa_unix_socket_remove_stale(UNIX_SOCKET)) < 0) {
67             fprintf(stderr, "Failed to remove stale UNIX socket '%s': %s\n", UNIX_SOCKET, strerror(errno));
68             return -1;
69         }
70
71         if (r)
72             fprintf(stderr, "Removed stale UNIX socket '%s'.", UNIX_SOCKET);
73     }
74     
75     if (!(s = pa_socket_server_new_unix(c->mainloop, UNIX_SOCKET))) {
76         rmdir(UNIX_SOCKET_DIR);
77         return -1;
78     }
79 #endif
80
81 #ifdef USE_PROTOCOL_SIMPLE
82     m->userdata = pa_protocol_simple_new(c, s, m, PA_PROTOCOL_SIMPLE_PLAYBACK);
83 #else
84     m->userdata = protocol_new(c, s, m);
85 #endif
86
87     if (!m->userdata) {
88         pa_socket_server_free(s);
89         return -1;
90     }
91
92     return 0;
93 }
94
95 void pa_module_done(struct pa_core *c, struct pa_module*m) {
96     assert(c && m);
97
98     protocol_free(m->userdata);
99
100 #ifndef USE_TCP_SOCKETS
101     rmdir(UNIX_SOCKET_DIR);
102 #endif
103 }