basic cli interface
[profile/ivi/pulseaudio.git] / src / client.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "client.h"
7 #include "strbuf.h"
8
9 struct client *client_new(struct core *core, const char *protocol_name, char *name) {
10     struct client *c;
11     int r;
12     assert(core);
13
14     c = malloc(sizeof(struct client));
15     assert(c);
16     c->name = name ? strdup(name) : NULL;
17     c->core = core;
18     c->protocol_name = protocol_name;
19
20     c->kill = NULL;
21     c->userdata = NULL;
22
23     r = idxset_put(core->clients, c, &c->index);
24     assert(c->index != IDXSET_INVALID && r >= 0);
25
26     fprintf(stderr, "client: created %u \"%s\"\n", c->index, c->name);
27     
28     return c;
29 }
30
31 void client_free(struct client *c) {
32     assert(c && c->core);
33
34     idxset_remove_by_data(c->core->clients, c, NULL);
35     fprintf(stderr, "client: freed %u \"%s\"\n", c->index, c->name);
36     free(c->name);
37     free(c);
38 }
39
40 void client_kill(struct client *c) {
41     assert(c);
42     if (c->kill)
43         c->kill(c);
44 }
45
46 char *client_list_to_string(struct core *c) {
47     struct strbuf *s;
48     struct client *client;
49     uint32_t index = IDXSET_INVALID;
50     assert(c);
51
52     s = strbuf_new();
53     assert(s);
54
55     strbuf_printf(s, "%u client(s).\n", idxset_ncontents(c->clients));
56     
57     for (client = idxset_first(c->clients, &index); client; client = idxset_next(c->clients, &index))
58         strbuf_printf(s, "    index: %u, name: <%s>, protocol_name: <%s>\n", client->index, client->name, client->protocol_name);
59     
60     return strbuf_tostring_free(s);
61 }
62