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