]> code.delx.au - pulseaudio/blob - src/modules/module-cli.c
big s/polyp/pulse/g
[pulseaudio] / src / modules / module-cli.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 <stdio.h>
27 #include <assert.h>
28 #include <unistd.h>
29
30 #include <pulsecore/module.h>
31 #include <pulsecore/iochannel.h>
32 #include <pulsecore/cli.h>
33 #include <pulsecore/sioman.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/modargs.h>
36
37 #include "module-cli-symdef.h"
38
39 PA_MODULE_AUTHOR("Lennart Poettering")
40 PA_MODULE_DESCRIPTION("Command line interface")
41 PA_MODULE_VERSION(PACKAGE_VERSION)
42 PA_MODULE_USAGE("exit_on_eof=<exit daemon after EOF?>")
43
44 static const char* const valid_modargs[] = {
45 "exit_on_eof",
46 NULL
47 };
48
49 static void eof_and_unload_cb(pa_cli*c, void *userdata) {
50 pa_module *m = userdata;
51
52 assert(c);
53 assert(m);
54
55 pa_module_unload_request(m);
56 }
57
58 static void eof_and_exit_cb(pa_cli*c, void *userdata) {
59 pa_module *m = userdata;
60
61 assert(c);
62 assert(m);
63
64 m->core->mainloop->quit(m->core->mainloop, 0);
65 }
66
67 int pa__init(pa_core *c, pa_module*m) {
68 pa_iochannel *io;
69 pa_modargs *ma;
70 int exit_on_eof = 0;
71
72 assert(c);
73 assert(m);
74
75 if (c->running_as_daemon) {
76 pa_log_info(__FILE__": Running as daemon, refusing to load this module.");
77 return 0;
78 }
79
80 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
81 pa_log(__FILE__": failed to parse module arguments.");
82 goto fail;
83 }
84
85 if (pa_modargs_get_value_boolean(ma, "exit_on_eof", &exit_on_eof) < 0) {
86 pa_log(__FILE__": exit_on_eof= expects boolean argument.");
87 goto fail;
88 }
89
90 if (pa_stdio_acquire() < 0) {
91 pa_log(__FILE__": STDIN/STDUSE already in use.");
92 goto fail;
93 }
94
95 io = pa_iochannel_new(c->mainloop, STDIN_FILENO, STDOUT_FILENO);
96 assert(io);
97 pa_iochannel_set_noclose(io, 1);
98
99 m->userdata = pa_cli_new(c, io, m);
100 assert(m->userdata);
101
102 pa_cli_set_eof_callback(m->userdata, exit_on_eof ? eof_and_exit_cb : eof_and_unload_cb, m);
103
104 pa_modargs_free(ma);
105
106 return 0;
107
108 fail:
109
110 if (ma)
111 pa_modargs_free(ma);
112
113 return -1;
114 }
115
116 void pa__done(pa_core *c, pa_module*m) {
117 assert(c);
118 assert(m);
119
120 if (c->running_as_daemon == 0) {
121 pa_cli_free(m->userdata);
122 pa_stdio_release();
123 }
124 }