rename src to polyp
[profile/ivi/pulseaudio.git] / 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 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 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 "clitext.h"
43 #include "cli-command.h"
44
45 struct pa_cli {
46     struct pa_core *core;
47     struct pa_ioline *line;
48
49     void (*eof_callback)(struct pa_cli *c, void *userdata);
50     void *userdata;
51
52     struct pa_client *client;
53
54     int fail, verbose, kill_requested, defer_kill;
55 };
56
57 static void line_callback(struct pa_ioline *line, const char *s, void *userdata);
58
59 static const char prompt[] = ">>> ";
60
61 static void client_kill(struct pa_client *c);
62
63 struct pa_cli* pa_cli_new(struct pa_core *core, struct pa_iochannel *io, struct pa_module *m) {
64     char cname[256];
65     struct pa_cli *c;
66     assert(io);
67
68     c = malloc(sizeof(struct pa_cli));
69     assert(c);
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");
86     pa_ioline_puts(c->line, prompt);
87
88     c->fail = c->kill_requested = c->defer_kill = 0;
89     c->verbose = 1;
90     
91     return c;
92 }
93
94 void pa_cli_free(struct pa_cli *c) {
95     assert(c);
96     pa_ioline_free(c->line);
97     pa_client_free(c->client);
98     free(c);
99 }
100
101 static void client_kill(struct pa_client *client) {
102     struct pa_cli *c;
103     assert(client && client->userdata);
104     c = client->userdata;
105     
106     fprintf(stderr, "CLI client killed.\n");
107     if (c->defer_kill)
108         c->kill_requested = 1;
109     else {
110         if (c->eof_callback)
111             c->eof_callback(c, c->userdata);
112     }
113 }
114
115 static void line_callback(struct pa_ioline *line, const char *s, void *userdata) {
116     struct pa_strbuf *buf;
117     struct pa_cli *c = userdata;
118     char *p;
119     assert(line && c);
120
121     if (!s) {
122         fprintf(stderr, "CLI got EOF from user.\n");
123         if (c->eof_callback)
124             c->eof_callback(c, c->userdata);
125
126         return;
127     }
128
129     buf = pa_strbuf_new();
130     assert(buf);
131     c->defer_kill++;
132     pa_cli_command_execute_line(c->core, s, buf, &c->fail, &c->verbose);
133     c->defer_kill--;
134     pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
135     free(p);
136
137     if (c->kill_requested) {
138         if (c->eof_callback)
139             c->eof_callback(c, c->userdata);
140     } else    
141         pa_ioline_puts(line, prompt);
142 }
143
144 void pa_cli_set_eof_callback(struct pa_cli *c, void (*cb)(struct pa_cli*c, void *userdata), void *userdata) {
145     assert(c && cb);
146     c->eof_callback = cb;
147     c->userdata = userdata;
148 }