]> code.delx.au - pulseaudio/blob - src/pulsecore/cli.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[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 <stdlib.h>
28
29 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/ioline.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/client.h>
35 #include <pulsecore/tokenizer.h>
36 #include <pulsecore/strbuf.h>
37 #include <pulsecore/cli-text.h>
38 #include <pulsecore/cli-command.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/macro.h>
41
42 #include "cli.h"
43
44 #define PROMPT ">>> "
45
46 struct pa_cli {
47 pa_core *core;
48 pa_ioline *line;
49
50 pa_cli_eof_cb_t eof_callback;
51 void *userdata;
52
53 pa_client *client;
54
55 bool fail, kill_requested;
56 int defer_kill;
57
58 bool interactive;
59 char *last_line;
60 };
61
62 static void line_callback(pa_ioline *line, const char *s, void *userdata);
63 static void client_kill(pa_client *c);
64
65 pa_cli* pa_cli_new(pa_core *core, pa_iochannel *io, pa_module *m) {
66 char cname[256];
67 pa_cli *c;
68 pa_client_new_data data;
69 pa_client *client;
70
71 pa_assert(io);
72
73 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
74
75 pa_client_new_data_init(&data);
76 data.driver = __FILE__;
77 data.module = m;
78 pa_proplist_sets(data.proplist, PA_PROP_APPLICATION_NAME, cname);
79 client = pa_client_new(core, &data);
80 pa_client_new_data_done(&data);
81
82 if (!client)
83 return NULL;
84
85 c = pa_xnew0(pa_cli, 1);
86 c->core = core;
87 c->client = client;
88 pa_assert_se(c->line = pa_ioline_new(io));
89
90 c->client->kill = client_kill;
91 c->client->userdata = c;
92
93 pa_ioline_set_callback(c->line, line_callback, c);
94
95 return c;
96 }
97
98 void pa_cli_free(pa_cli *c) {
99 pa_assert(c);
100
101 pa_ioline_close(c->line);
102 pa_ioline_unref(c->line);
103 pa_client_free(c->client);
104 pa_xfree(c->last_line);
105 pa_xfree(c);
106 }
107
108 static void client_kill(pa_client *client) {
109 pa_cli *c;
110
111 pa_assert(client);
112 pa_assert_se(c = client->userdata);
113
114 pa_log_debug("CLI client killed.");
115
116 if (c->defer_kill)
117 c->kill_requested = true;
118 else if (c->eof_callback)
119 c->eof_callback(c, c->userdata);
120 }
121
122 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
123 pa_strbuf *buf;
124 pa_cli *c = userdata;
125 char *p;
126
127 pa_assert(line);
128 pa_assert(c);
129
130 if (!s) {
131 pa_log_debug("CLI got EOF from user.");
132
133 if (c->eof_callback)
134 c->eof_callback(c, c->userdata);
135
136 return;
137 }
138
139 /* Magic command, like they had in AT Hayes Modems! Those were the good days! */
140 if (pa_streq(s, "/"))
141 s = c->last_line;
142 else if (s[0]) {
143 pa_xfree(c->last_line);
144 c->last_line = pa_xstrdup(s);
145 }
146
147 pa_assert_se(buf = pa_strbuf_new());
148 c->defer_kill++;
149 if (pa_streq(s, "hello")) {
150 pa_strbuf_printf(buf, "Welcome to PulseAudio %s! "
151 "Use \"help\" for usage information.\n", PACKAGE_VERSION);
152 c->interactive = true;
153 }
154 else
155 pa_cli_command_execute_line(c->core, s, buf, &c->fail);
156 c->defer_kill--;
157 pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
158 pa_xfree(p);
159
160 if (c->kill_requested) {
161 if (c->eof_callback)
162 c->eof_callback(c, c->userdata);
163 } else if (c->interactive)
164 pa_ioline_puts(line, PROMPT);
165 }
166
167 void pa_cli_set_eof_callback(pa_cli *c, pa_cli_eof_cb_t cb, void *userdata) {
168 pa_assert(c);
169
170 c->eof_callback = cb;
171 c->userdata = userdata;
172 }
173
174 pa_module *pa_cli_get_module(pa_cli *c) {
175 pa_assert(c);
176
177 return c->client->module;
178 }