]> code.delx.au - pulseaudio/blob - src/pulsecore/cli.c
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[pulseaudio] / src / pulsecore / cli.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <pulse/xmalloc.h>
31
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>
46
47 #include "cli.h"
48
49 #define PROMPT ">>> "
50
51 struct pa_cli {
52 pa_core *core;
53 pa_ioline *line;
54
55 pa_cli_eof_cb_t eof_callback;
56 void *userdata;
57
58 pa_client *client;
59
60 pa_bool_t fail, kill_requested;
61 int defer_kill;
62
63 char *last_line;
64 };
65
66 static void line_callback(pa_ioline *line, const char *s, void *userdata);
67 static void client_kill(pa_client *c);
68
69 pa_cli* pa_cli_new(pa_core *core, pa_iochannel *io, pa_module *m) {
70 char cname[256];
71 pa_cli *c;
72 pa_client_new_data data;
73 pa_client *client;
74
75 pa_assert(io);
76
77 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
78
79 pa_client_new_data_init(&data);
80 data.driver = __FILE__;
81 data.module = m;
82 pa_proplist_sets(data.proplist, PA_PROP_APPLICATION_NAME, cname);
83 client = pa_client_new(core, &data);
84 pa_client_new_data_done(&data);
85
86 if (!client)
87 return NULL;
88
89 c = pa_xnew(pa_cli, 1);
90 c->core = core;
91 c->client = client;
92 pa_assert_se(c->line = pa_ioline_new(io));
93
94 c->userdata = NULL;
95 c->eof_callback = NULL;
96
97 c->client->kill = client_kill;
98 c->client->userdata = c;
99
100 pa_ioline_set_callback(c->line, line_callback, c);
101 pa_ioline_puts(c->line, "Welcome to PulseAudio! Use \"help\" for usage information.\n"PROMPT);
102
103 c->fail = c->kill_requested = FALSE;
104 c->defer_kill = 0;
105
106 c->last_line = NULL;
107
108 return c;
109 }
110
111 void pa_cli_free(pa_cli *c) {
112 pa_assert(c);
113
114 pa_ioline_close(c->line);
115 pa_ioline_unref(c->line);
116 pa_client_free(c->client);
117 pa_xfree(c->last_line);
118 pa_xfree(c);
119 }
120
121 static void client_kill(pa_client *client) {
122 pa_cli *c;
123
124 pa_assert(client);
125 pa_assert_se(c = client->userdata);
126
127 pa_log_debug("CLI client killed.");
128
129 if (c->defer_kill)
130 c->kill_requested = TRUE;
131 else if (c->eof_callback)
132 c->eof_callback(c, c->userdata);
133 }
134
135 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
136 pa_strbuf *buf;
137 pa_cli *c = userdata;
138 char *p;
139
140 pa_assert(line);
141 pa_assert(c);
142
143 if (!s) {
144 pa_log_debug("CLI got EOF from user.");
145
146 if (c->eof_callback)
147 c->eof_callback(c, c->userdata);
148
149 return;
150 }
151
152 /* Magic command, like they had in AT Hayes Modems! Those were the good days! */
153 if (pa_streq(s, "/"))
154 s = c->last_line;
155 else if (s[0]) {
156 pa_xfree(c->last_line);
157 c->last_line = pa_xstrdup(s);
158 }
159
160 pa_assert_se(buf = pa_strbuf_new());
161 c->defer_kill++;
162 pa_cli_command_execute_line(c->core, s, buf, &c->fail);
163 c->defer_kill--;
164 pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
165 pa_xfree(p);
166
167 if (c->kill_requested) {
168 if (c->eof_callback)
169 c->eof_callback(c, c->userdata);
170 } else
171 pa_ioline_puts(line, PROMPT);
172 }
173
174 void pa_cli_set_eof_callback(pa_cli *c, pa_cli_eof_cb_t cb, void *userdata) {
175 pa_assert(c);
176
177 c->eof_callback = cb;
178 c->userdata = userdata;
179 }
180
181 pa_module *pa_cli_get_module(pa_cli *c) {
182 pa_assert(c);
183
184 return c->client->module;
185 }