]> code.delx.au - pulseaudio/blob - src/modules/module-cli.c
alsa-mixer: Add surround 2.1 profile
[pulseaudio] / src / modules / module-cli.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
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 <stdio.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 #include <pulsecore/module.h>
32 #include <pulsecore/iochannel.h>
33 #include <pulsecore/cli.h>
34 #include <pulsecore/sioman.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/modargs.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/core-util.h>
39
40 #include "module-cli-symdef.h"
41
42 PA_MODULE_AUTHOR("Lennart Poettering");
43 PA_MODULE_DESCRIPTION("Command line interface");
44 PA_MODULE_VERSION(PACKAGE_VERSION);
45 PA_MODULE_LOAD_ONCE(true);
46 PA_MODULE_USAGE("exit_on_eof=<exit daemon after EOF?>");
47
48 static const char* const valid_modargs[] = {
49 "exit_on_eof",
50 NULL
51 };
52
53 static void eof_and_unload_cb(pa_cli*c, void *userdata) {
54 pa_module *m = userdata;
55
56 pa_assert(c);
57 pa_assert(m);
58
59 pa_module_unload_request(m, true);
60 }
61
62 static void eof_and_exit_cb(pa_cli*c, void *userdata) {
63 pa_module *m = userdata;
64
65 pa_assert(c);
66 pa_assert(m);
67
68 pa_core_exit(m->core, false, 0);
69 }
70
71 int pa__init(pa_module*m) {
72 pa_iochannel *io;
73 pa_modargs *ma;
74 bool exit_on_eof = false;
75 #ifndef OS_IS_WIN32
76 int fd;
77 #endif
78
79 pa_assert(m);
80
81 if (m->core->running_as_daemon) {
82 pa_log_info("Running as daemon, refusing to load this module.");
83 return 0;
84 }
85
86 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
87 pa_log("failed to parse module arguments.");
88 goto fail;
89 }
90
91 if (pa_modargs_get_value_boolean(ma, "exit_on_eof", &exit_on_eof) < 0) {
92 pa_log("exit_on_eof= expects boolean argument.");
93 goto fail;
94 }
95
96 if (pa_stdio_acquire() < 0) {
97 pa_log("STDIN/STDOUT already in use.");
98 goto fail;
99 }
100
101 /* We try to open the controlling tty anew here. This has the
102 * benefit of giving us a new fd that doesn't share the O_NDELAY
103 * flag with fds 0, 1, or 2. Since pa_iochannel_xxx needs O_NDELAY
104 * on its fd using those fds directly could set O_NDELAY which
105 * fprintf() doesn't really like, resulting in truncated output
106 * of log messages, particularly because if stdout and stderr are
107 * dup'ed they share the same O_NDELAY, too. */
108
109 #ifndef OS_IS_WIN32
110 if ((fd = pa_open_cloexec("/dev/tty", O_RDWR|O_NONBLOCK, 0)) >= 0) {
111 io = pa_iochannel_new(m->core->mainloop, fd, fd);
112 pa_log_debug("Managed to open /dev/tty.");
113 }
114 else
115 #endif
116 {
117 io = pa_iochannel_new(m->core->mainloop, STDIN_FILENO, STDOUT_FILENO);
118 pa_iochannel_set_noclose(io, true);
119 pa_log_debug("Failed to open /dev/tty, using stdin/stdout fds instead.");
120 }
121
122 m->userdata = pa_cli_new(m->core, io, m);
123 pa_cli_set_eof_callback(m->userdata, exit_on_eof ? eof_and_exit_cb : eof_and_unload_cb, m);
124
125 pa_modargs_free(ma);
126
127 return 0;
128
129 fail:
130
131 if (ma)
132 pa_modargs_free(ma);
133
134 return -1;
135 }
136
137 void pa__done(pa_module*m) {
138 pa_assert(m);
139
140 if (m->userdata) {
141 pa_cli_free(m->userdata);
142 pa_stdio_release();
143 }
144 }