2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
27 #include <pulse/xmalloc.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/ioline.h>
31 #include <pulsecore/module.h>
32 #include <pulsecore/client.h>
33 #include <pulsecore/tokenizer.h>
34 #include <pulsecore/strbuf.h>
35 #include <pulsecore/cli-text.h>
36 #include <pulsecore/cli-command.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
48 pa_cli_eof_cb_t eof_callback;
53 bool fail, kill_requested;
60 static void line_callback(pa_ioline *line, const char *s, void *userdata);
61 static void client_kill(pa_client *c);
63 pa_cli* pa_cli_new(pa_core *core, pa_iochannel *io, pa_module *m) {
66 pa_client_new_data data;
71 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
73 pa_client_new_data_init(&data);
74 data.driver = __FILE__;
76 pa_proplist_sets(data.proplist, PA_PROP_APPLICATION_NAME, cname);
77 client = pa_client_new(core, &data);
78 pa_client_new_data_done(&data);
83 c = pa_xnew0(pa_cli, 1);
86 pa_assert_se(c->line = pa_ioline_new(io));
88 c->client->kill = client_kill;
89 c->client->userdata = c;
91 pa_ioline_set_callback(c->line, line_callback, c);
96 void pa_cli_free(pa_cli *c) {
99 pa_ioline_close(c->line);
100 pa_ioline_unref(c->line);
101 pa_client_free(c->client);
102 pa_xfree(c->last_line);
106 static void client_kill(pa_client *client) {
110 pa_assert_se(c = client->userdata);
112 pa_log_debug("CLI client killed.");
115 c->kill_requested = true;
116 else if (c->eof_callback)
117 c->eof_callback(c, c->userdata);
120 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
122 pa_cli *c = userdata;
129 pa_log_debug("CLI got EOF from user.");
132 c->eof_callback(c, c->userdata);
137 /* Magic command, like they had in AT Hayes Modems! Those were the good days! */
138 if (pa_streq(s, "/"))
141 pa_xfree(c->last_line);
142 c->last_line = pa_xstrdup(s);
145 pa_assert_se(buf = pa_strbuf_new());
147 if (pa_streq(s, "hello")) {
148 pa_strbuf_printf(buf, "Welcome to PulseAudio %s! "
149 "Use \"help\" for usage information.\n", PACKAGE_VERSION);
150 c->interactive = true;
153 pa_cli_command_execute_line(c->core, s, buf, &c->fail);
155 pa_ioline_puts(line, p = pa_strbuf_to_string_free(buf));
158 if (c->kill_requested) {
160 c->eof_callback(c, c->userdata);
161 } else if (c->interactive)
162 pa_ioline_puts(line, PROMPT);
165 void pa_cli_set_eof_callback(pa_cli *c, pa_cli_eof_cb_t cb, void *userdata) {
168 c->eof_callback = cb;
169 c->userdata = userdata;
172 pa_module *pa_cli_get_module(pa_cli *c) {
175 return c->client->module;