]> code.delx.au - pulseaudio/blob - polyp/cli.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / cli.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 of the License,
9 or (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU Lesser 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
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 <assert.h>
29 #include <stdlib.h>
30
31 #include "ioline.h"
32 #include "cli.h"
33 #include "module.h"
34 #include "sink.h"
35 #include "source.h"
36 #include "client.h"
37 #include "sink-input.h"
38 #include "source-output.h"
39 #include "tokenizer.h"
40 #include "strbuf.h"
41 #include "namereg.h"
42 #include "cli-text.h"
43 #include "cli-command.h"
44 #include "xmalloc.h"
45 #include "log.h"
46
47 struct pa_cli {
48 struct pa_core *core;
49 struct pa_ioline *line;
50
51 void (*eof_callback)(struct pa_cli *c, void *userdata);
52 void *userdata;
53
54 struct pa_client *client;
55
56 int fail, verbose, kill_requested, defer_kill;
57 };
58
59 static void line_callback(struct pa_ioline *line, const char *s, void *userdata);
60
61 static const char prompt[] = ">>> ";
62
63 static void client_kill(struct pa_client *c);
64
65 struct pa_cli* pa_cli_new(struct pa_core *core, struct pa_iochannel *io, struct pa_module *m) {
66 char cname[256];
67 struct pa_cli *c;
68 assert(io);
69
70 c = pa_xmalloc(sizeof(struct pa_cli));
71 c->core = core;
72 c->line = pa_ioline_new(io);
73 assert(c->line);
74
75 c->userdata = NULL;
76 c->eof_callback = NULL;
77
78 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
79 c->client = pa_client_new(core, "CLI", cname);
80 assert(c->client);
81 c->client->kill = client_kill;
82 c->client->userdata = c;
83 c->client->owner = m;
84
85 pa_ioline_set_callback(c->line, line_callback, c);
86 pa_ioline_puts(c->line, "Welcome to polypaudio! Use \"help\" for usage information.\n");
87 pa_ioline_puts(c->line, prompt);
88
89 c->fail = c->kill_requested = c->defer_kill = 0;
90 c->verbose = 1;
91
92 return c;
93 }
94
95 void pa_cli_free(struct pa_cli *c) {
96 assert(c);
97 pa_ioline_free(c->line);
98 pa_client_free(c->client);
99 pa_xfree(c);
100 }
101
102 static void client_kill(struct pa_client *client) {
103 struct pa_cli *c;
104 assert(client && client->userdata);
105 c = client->userdata;
106
107 pa_log(__FILE__": CLI client killed.\n");
108 if (c->defer_kill)
109 c->kill_requested = 1;
110 else {
111 if (c->eof_callback)
112 c->eof_callback(c, c->userdata);
113 }
114 }
115
116 static void line_callback(struct pa_ioline *line, const char *s, void *userdata) {
117 struct pa_strbuf *buf;
118 struct pa_cli *c = userdata;
119 char *p;
120 assert(line && c);
121
122 if (!s) {
123 pa_log(__FILE__": CLI got EOF from user.\n");
124 if (c->eof_callback)
125 c->eof_callback(c, c->userdata);
126
127 return;
128 }
129
130 buf = pa_strbuf_new();
131 assert(buf);
132 c->defer_kill++;
133 pa_cli_command_execute_line(c->core, s, buf, &c->fail, &c->verbose);
134 c->defer_kill--;
135 pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
136 pa_xfree(p);
137
138 if (c->kill_requested) {
139 if (c->eof_callback)
140 c->eof_callback(c, c->userdata);
141 } else
142 pa_ioline_puts(line, prompt);
143 }
144
145 void pa_cli_set_eof_callback(struct pa_cli *c, void (*cb)(struct pa_cli*c, void *userdata), void *userdata) {
146 assert(c && cb);
147 c->eof_callback = cb;
148 c->userdata = userdata;
149 }