]>
code.delx.au - pulseaudio/blob - src/pulsecore/cli.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/ioline.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/source.h>
38 #include <pulsecore/client.h>
39 #include <pulsecore/sink-input.h>
40 #include <pulsecore/source-output.h>
41 #include <pulsecore/tokenizer.h>
42 #include <pulsecore/strbuf.h>
43 #include <pulsecore/namereg.h>
44 #include <pulsecore/cli-text.h>
45 #include <pulsecore/cli-command.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/macro.h>
57 void (*eof_callback
)(pa_cli
*c
, void *userdata
);
62 pa_bool_t fail
, kill_requested
;
66 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
);
67 static void client_kill(pa_client
*c
);
69 pa_cli
* pa_cli_new(pa_core
*core
, pa_iochannel
*io
, pa_module
*m
) {
74 c
= pa_xnew(pa_cli
, 1);
76 pa_assert_se(c
->line
= pa_ioline_new(io
));
79 c
->eof_callback
= NULL
;
81 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
82 pa_assert_se(c
->client
= pa_client_new(core
, __FILE__
, cname
));
83 c
->client
->kill
= client_kill
;
84 c
->client
->userdata
= c
;
87 pa_ioline_set_callback(c
->line
, line_callback
, c
);
88 pa_ioline_puts(c
->line
, "Welcome to PulseAudio! Use \"help\" for usage information.\n"PROMPT
);
90 c
->fail
= c
->kill_requested
= FALSE
;
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
);
105 static void client_kill(pa_client
*client
) {
109 pa_assert_se(c
= client
->userdata
);
111 pa_log_debug("CLI client killed.");
113 c
->kill_requested
= TRUE
;
116 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.");
131 c
->eof_callback(c
, c
->userdata
);
136 pa_assert_se(buf
= pa_strbuf_new());
138 pa_cli_command_execute_line(c
->core
, s
, buf
, &c
->fail
);
140 pa_ioline_puts(line
, p
= pa_strbuf_tostring_free(buf
));
143 if (c
->kill_requested
) {
145 c
->eof_callback(c
, c
->userdata
);
147 pa_ioline_puts(line
, PROMPT
);
150 void pa_cli_set_eof_callback(pa_cli
*c
, void (*cb
)(pa_cli
*c
, void *userdata
), void *userdata
) {
153 c
->eof_callback
= cb
;
154 c
->userdata
= userdata
;