Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulsecore / cli.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <stdlib.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/ioline.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/source.h>
39 #include <pulsecore/client.h>
40 #include <pulsecore/sink-input.h>
41 #include <pulsecore/source-output.h>
42 #include <pulsecore/tokenizer.h>
43 #include <pulsecore/strbuf.h>
44 #include <pulsecore/namereg.h>
45 #include <pulsecore/cli-text.h>
46 #include <pulsecore/cli-command.h>
47 #include <pulsecore/log.h>
48
49 #include "cli.h"
50
51 #define PROMPT ">>> "
52
53 struct pa_cli {
54     pa_core *core;
55     pa_ioline *line;
56
57     void (*eof_callback)(pa_cli *c, void *userdata);
58     void *userdata;
59
60     pa_client *client;
61
62     int fail, kill_requested, defer_kill;
63 };
64
65 static void line_callback(pa_ioline *line, const char *s, void *userdata);
66 static void client_kill(pa_client *c);
67
68 pa_cli* pa_cli_new(pa_core *core, pa_iochannel *io, pa_module *m) {
69     char cname[256];
70     pa_cli *c;
71     assert(io);
72
73     c = pa_xmalloc(sizeof(pa_cli));
74     c->core = core;
75     c->line = pa_ioline_new(io);
76     assert(c->line);
77
78     c->userdata = NULL;
79     c->eof_callback = NULL;
80
81     pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
82     c->client = pa_client_new(core, __FILE__, cname);
83     assert(c->client);
84     c->client->kill = client_kill;
85     c->client->userdata = c;
86     c->client->owner = m;
87
88     pa_ioline_set_callback(c->line, line_callback, c);
89     pa_ioline_puts(c->line, "Welcome to PulseAudio! Use \"help\" for usage information.\n"PROMPT);
90
91     c->fail = c->kill_requested = c->defer_kill = 0;
92
93     return c;
94 }
95
96 void pa_cli_free(pa_cli *c) {
97     assert(c);
98     pa_ioline_close(c->line);
99     pa_ioline_unref(c->line);
100     pa_client_free(c->client);
101     pa_xfree(c);
102 }
103
104 static void client_kill(pa_client *client) {
105     pa_cli *c;
106     assert(client && client->userdata);
107     c = client->userdata;
108
109     pa_log_debug("CLI client killed.");
110     if (c->defer_kill)
111         c->kill_requested = 1;
112     else {
113         if (c->eof_callback)
114             c->eof_callback(c, c->userdata);
115     }
116 }
117
118 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
119     pa_strbuf *buf;
120     pa_cli *c = userdata;
121     char *p;
122     assert(line && c);
123
124     if (!s) {
125         pa_log_debug("CLI got EOF from user.");
126         if (c->eof_callback)
127             c->eof_callback(c, c->userdata);
128
129         return;
130     }
131
132     buf = pa_strbuf_new();
133     assert(buf);
134     c->defer_kill++;
135     pa_cli_command_execute_line(c->core, s, buf, &c->fail);
136     c->defer_kill--;
137     pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf));
138     pa_xfree(p);
139
140     if (c->kill_requested) {
141         if (c->eof_callback)
142             c->eof_callback(c, c->userdata);
143     } else
144         pa_ioline_puts(line, PROMPT);
145 }
146
147 void pa_cli_set_eof_callback(pa_cli *c, void (*cb)(pa_cli*c, void *userdata), void *userdata) {
148     assert(c);
149     c->eof_callback = cb;
150     c->userdata = userdata;
151 }