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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/ioline.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/sink.h>
35 #include <pulsecore/source.h>
36 #include <pulsecore/client.h>
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/source-output.h>
39 #include <pulsecore/tokenizer.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/namereg.h>
42 #include <pulsecore/cli-text.h>
43 #include <pulsecore/cli-command.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/macro.h>
55 pa_cli_eof_cb_t eof_callback
;
60 pa_bool_t fail
, kill_requested
;
64 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
);
65 static void client_kill(pa_client
*c
);
67 pa_cli
* pa_cli_new(pa_core
*core
, pa_iochannel
*io
, pa_module
*m
) {
70 pa_client_new_data data
;
75 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
77 pa_client_new_data_init(&data
);
78 data
.driver
= __FILE__
;
80 pa_proplist_sets(data
.proplist
, PA_PROP_APPLICATION_NAME
, cname
);
81 client
= pa_client_new(core
, &data
);
82 pa_client_new_data_done(&data
);
87 c
= pa_xnew(pa_cli
, 1);
90 pa_assert_se(c
->line
= pa_ioline_new(io
));
93 c
->eof_callback
= NULL
;
95 c
->client
->kill
= client_kill
;
96 c
->client
->userdata
= c
;
98 pa_ioline_set_callback(c
->line
, line_callback
, c
);
99 pa_ioline_puts(c
->line
, "Welcome to PulseAudio! Use \"help\" for usage information.\n"PROMPT
);
101 c
->fail
= c
->kill_requested
= FALSE
;
107 void pa_cli_free(pa_cli
*c
) {
110 pa_ioline_close(c
->line
);
111 pa_ioline_unref(c
->line
);
112 pa_client_free(c
->client
);
116 static void client_kill(pa_client
*client
) {
120 pa_assert_se(c
= client
->userdata
);
122 pa_log_debug("CLI client killed.");
125 c
->kill_requested
= TRUE
;
126 else if (c
->eof_callback
)
127 c
->eof_callback(c
, c
->userdata
);
130 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
) {
132 pa_cli
*c
= userdata
;
139 pa_log_debug("CLI got EOF from user.");
142 c
->eof_callback(c
, c
->userdata
);
147 pa_assert_se(buf
= pa_strbuf_new());
149 pa_cli_command_execute_line(c
->core
, s
, buf
, &c
->fail
);
151 pa_ioline_puts(line
, p
= pa_strbuf_tostring_free(buf
));
154 if (c
->kill_requested
) {
156 c
->eof_callback(c
, c
->userdata
);
158 pa_ioline_puts(line
, PROMPT
);
161 void pa_cli_set_eof_callback(pa_cli
*c
, pa_cli_eof_cb_t cb
, void *userdata
) {
164 c
->eof_callback
= cb
;
165 c
->userdata
= userdata
;
168 pa_module
*pa_cli_get_module(pa_cli
*c
) {
171 return c
->client
->module
;