]>
code.delx.au - pulseaudio/blob - src/polypcore/cli.c
683d29ec9e02c792a1cc8722826c8e2d2d2288d1
4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <polyp/xmalloc.h>
33 #include <polypcore/ioline.h>
34 #include <polypcore/module.h>
35 #include <polypcore/sink.h>
36 #include <polypcore/source.h>
37 #include <polypcore/client.h>
38 #include <polypcore/sink-input.h>
39 #include <polypcore/source-output.h>
40 #include <polypcore/tokenizer.h>
41 #include <polypcore/strbuf.h>
42 #include <polypcore/namereg.h>
43 #include <polypcore/cli-text.h>
44 #include <polypcore/cli-command.h>
45 #include <polypcore/log.h>
55 void (*eof_callback
)(pa_cli
*c
, void *userdata
);
60 int fail
, kill_requested
, defer_kill
;
63 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
);
64 static void client_kill(pa_client
*c
);
66 pa_cli
* pa_cli_new(pa_core
*core
, pa_iochannel
*io
, pa_module
*m
) {
71 c
= pa_xmalloc(sizeof(pa_cli
));
73 c
->line
= pa_ioline_new(io
);
77 c
->eof_callback
= NULL
;
79 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
80 c
->client
= pa_client_new(core
, __FILE__
, cname
);
82 c
->client
->kill
= client_kill
;
83 c
->client
->userdata
= c
;
86 pa_ioline_set_callback(c
->line
, line_callback
, c
);
87 pa_ioline_puts(c
->line
, "Welcome to polypaudio! Use \"help\" for usage information.\n"PROMPT
);
89 c
->fail
= c
->kill_requested
= c
->defer_kill
= 0;
94 void pa_cli_free(pa_cli
*c
) {
96 pa_ioline_close(c
->line
);
97 pa_ioline_unref(c
->line
);
98 pa_client_free(c
->client
);
102 static void client_kill(pa_client
*client
) {
104 assert(client
&& client
->userdata
);
105 c
= client
->userdata
;
107 pa_log_debug(__FILE__
": CLI client killed.");
109 c
->kill_requested
= 1;
112 c
->eof_callback(c
, c
->userdata
);
116 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
) {
118 pa_cli
*c
= userdata
;
123 pa_log_debug(__FILE__
": CLI got EOF from user.");
125 c
->eof_callback(c
, c
->userdata
);
130 buf
= pa_strbuf_new();
133 pa_cli_command_execute_line(c
->core
, s
, buf
, &c
->fail
);
135 pa_ioline_puts(line
, p
= pa_strbuf_tostring_free(buf
));
138 if (c
->kill_requested
) {
140 c
->eof_callback(c
, c
->userdata
);
142 pa_ioline_puts(line
, PROMPT
);
145 void pa_cli_set_eof_callback(pa_cli
*c
, void (*cb
)(pa_cli
*c
, void *userdata
), void *userdata
) {
147 c
->eof_callback
= cb
;
148 c
->userdata
= userdata
;