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 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 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
37 #include "sink-input.h"
38 #include "source-output.h"
39 #include "tokenizer.h"
43 #include "cli-command.h"
47 struct pa_ioline *line;
49 void (*eof_callback)(struct pa_cli *c, void *userdata);
52 struct pa_client *client;
54 int fail, verbose, kill_requested, defer_kill;
57 static void line_callback(struct pa_ioline *line, const char *s, void *userdata);
59 static const char prompt[] = ">>> ";
61 static void client_kill(struct pa_client *c);
63 struct pa_cli* pa_cli_new(struct pa_core *core, struct pa_iochannel *io, struct pa_module *m) {
68 c = malloc(sizeof(struct pa_cli));
71 c->line = pa_ioline_new(io);
75 c->eof_callback = NULL;
77 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
78 c->client = pa_client_new(core, "CLI", cname);
80 c->client->kill = client_kill;
81 c->client->userdata = c;
84 pa_ioline_set_callback(c->line, line_callback, c);
85 pa_ioline_puts(c->line, "Welcome to polypaudio! Use \"help\" for usage information.\n");
86 pa_ioline_puts(c->line, prompt);
88 c->fail = c->kill_requested = c->defer_kill = 0;
94 void pa_cli_free(struct pa_cli *c) {
96 pa_ioline_free(c->line);
97 pa_client_free(c->client);
101 static void client_kill(struct pa_client *client) {
103 assert(client && client->userdata);
104 c = client->userdata;
106 fprintf(stderr, "CLI client killed.\n");
108 c->kill_requested = 1;
111 c->eof_callback(c, c->userdata);
115 static void line_callback(struct pa_ioline *line, const char *s, void *userdata) {
116 struct pa_strbuf *buf;
117 struct pa_cli *c = userdata;
122 fprintf(stderr, "CLI got EOF from user.\n");
124 c->eof_callback(c, c->userdata);
129 buf = pa_strbuf_new();
132 pa_cli_command_execute_line(c->core, s, buf, &c->fail, &c->verbose);
134 pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
137 if (c->kill_requested) {
139 c->eof_callback(c, c->userdata);
141 pa_ioline_puts(line, prompt);
144 void pa_cli_set_eof_callback(struct pa_cli *c, void (*cb)(struct pa_cli*c, void *userdata), void *userdata) {
146 c->eof_callback = cb;
147 c->userdata = userdata;