]> code.delx.au - pulseaudio/blob - src/pulsecore/cli.c
Remove pa_bool_t and replace it with bool.
[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 char *last_line;
59 };
60
61 static void line_callback(pa_ioline *line, const char *s, void *userdata);
62 static void client_kill(pa_client *c);
63
64 pa_cli* pa_cli_new(pa_core *core, pa_iochannel *io, pa_module *m) {
65 char cname[256];
66 pa_cli *c;
67 pa_client_new_data data;
68 pa_client *client;
69
70 pa_assert(io);
71
72 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
73
74 pa_client_new_data_init(&data);
75 data.driver = __FILE__;
76 data.module = m;
77 pa_proplist_sets(data.proplist, PA_PROP_APPLICATION_NAME, cname);
78 client = pa_client_new(core, &data);
79 pa_client_new_data_done(&data);
80
81 if (!client)
82 return NULL;
83
84 c = pa_xnew(pa_cli, 1);
85 c->core = core;
86 c->client = client;
87 pa_assert_se(c->line = pa_ioline_new(io));
88
89 c->userdata = NULL;
90 c->eof_callback = NULL;
91
92 c->client->kill = client_kill;
93 c->client->userdata = c;
94
95 pa_ioline_set_callback(c->line, line_callback, c);
96 pa_ioline_puts(c->line, "Welcome to PulseAudio! Use \"help\" for usage information.\n"PROMPT);
97
98 c->fail = c->kill_requested = false;
99 c->defer_kill = 0;
100
101 c->last_line = NULL;
102
103 return c;
104 }
105
106 void pa_cli_free(pa_cli *c) {
107 pa_assert(c);
108
109 pa_ioline_close(c->line);
110 pa_ioline_unref(c->line);
111 pa_client_free(c->client);
112 pa_xfree(c->last_line);
113 pa_xfree(c);
114 }
115
116 static void client_kill(pa_client *client) {
117 pa_cli *c;
118
119 pa_assert(client);
120 pa_assert_se(c = client->userdata);
121
122 pa_log_debug("CLI client killed.");
123
124 if (c->defer_kill)
125 c->kill_requested = true;
126 else if (c->eof_callback)
127 c->eof_callback(c, c->userdata);
128 }
129
130 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
131 pa_strbuf *buf;
132 pa_cli *c = userdata;
133 char *p;
134
135 pa_assert(line);
136 pa_assert(c);
137
138 if (!s) {
139 pa_log_debug("CLI got EOF from user.");
140
141 if (c->eof_callback)
142 c->eof_callback(c, c->userdata);
143
144 return;
145 }
146
147 /* Magic command, like they had in AT Hayes Modems! Those were the good days! */
148 if (pa_streq(s, "/"))
149 s = c->last_line;
150 else if (s[0]) {
151 pa_xfree(c->last_line);
152 c->last_line = pa_xstrdup(s);
153 }
154
155 pa_assert_se(buf = pa_strbuf_new());
156 c->defer_kill++;
157 pa_cli_command_execute_line(c->core, s, buf, &c->fail);
158 c->defer_kill--;
159 pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
160 pa_xfree(p);
161
162 if (c->kill_requested) {
163 if (c->eof_callback)
164 c->eof_callback(c, c->userdata);
165 } else
166 pa_ioline_puts(line, PROMPT);
167 }
168
169 void pa_cli_set_eof_callback(pa_cli *c, pa_cli_eof_cb_t cb, void *userdata) {
170 pa_assert(c);
171
172 c->eof_callback = cb;
173 c->userdata = userdata;
174 }
175
176 pa_module *pa_cli_get_module(pa_cli *c) {
177 pa_assert(c);
178
179 return c->client->module;
180 }