]> code.delx.au - pulseaudio/blob - polyp/cli.c
* add first part of zeroconf publisher
[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 #define PROMPT ">>> "
48
49 struct pa_cli {
50 struct pa_core *core;
51 struct pa_ioline *line;
52
53 void (*eof_callback)(struct pa_cli *c, void *userdata);
54 void *userdata;
55
56 struct pa_client *client;
57
58 int fail, kill_requested, defer_kill;
59 };
60
61 static void line_callback(struct pa_ioline *line, const char *s, void *userdata);
62 static void client_kill(struct pa_client *c);
63
64 struct pa_cli* pa_cli_new(struct pa_core *core, struct pa_iochannel *io, struct pa_module *m) {
65 char cname[256];
66 struct pa_cli *c;
67 assert(io);
68
69 c = pa_xmalloc(sizeof(struct pa_cli));
70 c->core = core;
71 c->line = pa_ioline_new(io);
72 assert(c->line);
73
74 c->userdata = NULL;
75 c->eof_callback = NULL;
76
77 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
78 c->client = pa_client_new(core, "CLI", cname);
79 assert(c->client);
80 c->client->kill = client_kill;
81 c->client->userdata = c;
82 c->client->owner = m;
83
84 pa_ioline_set_callback(c->line, line_callback, c);
85 pa_ioline_puts(c->line, "Welcome to polypaudio! Use \"help\" for usage information.\n"PROMPT);
86
87 c->fail = c->kill_requested = c->defer_kill = 0;
88
89 return c;
90 }
91
92 void pa_cli_free(struct pa_cli *c) {
93 assert(c);
94 pa_ioline_close(c->line);
95 pa_ioline_unref(c->line);
96 pa_client_free(c->client);
97 pa_xfree(c);
98 }
99
100 static void client_kill(struct pa_client *client) {
101 struct pa_cli *c;
102 assert(client && client->userdata);
103 c = client->userdata;
104
105 pa_log(__FILE__": CLI client killed.\n");
106 if (c->defer_kill)
107 c->kill_requested = 1;
108 else {
109 if (c->eof_callback)
110 c->eof_callback(c, c->userdata);
111 }
112 }
113
114 static void line_callback(struct pa_ioline *line, const char *s, void *userdata) {
115 struct pa_strbuf *buf;
116 struct pa_cli *c = userdata;
117 char *p;
118 assert(line && c);
119
120 if (!s) {
121 pa_log(__FILE__": CLI got EOF from user.\n");
122 if (c->eof_callback)
123 c->eof_callback(c, c->userdata);
124
125 return;
126 }
127
128 buf = pa_strbuf_new();
129 assert(buf);
130 c->defer_kill++;
131 pa_cli_command_execute_line(c->core, s, buf, &c->fail);
132 c->defer_kill--;
133 pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
134 pa_xfree(p);
135
136 if (c->kill_requested) {
137 if (c->eof_callback)
138 c->eof_callback(c, c->userdata);
139 } else
140 pa_ioline_puts(line, PROMPT);
141 }
142
143 void pa_cli_set_eof_callback(struct pa_cli *c, void (*cb)(struct pa_cli*c, void *userdata), void *userdata) {
144 assert(c);
145 c->eof_callback = cb;
146 c->userdata = userdata;
147 }