]> code.delx.au - pulseaudio/blob - polyp/pactl.c
rename src to polyp
[pulseaudio] / polyp / pactl.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 <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "polyplib.h"
35 #include "polyplib-error.h"
36 #include "mainloop.h"
37 #include "mainloop-signal.h"
38
39 static struct pa_context *context = NULL;
40 static struct pa_mainloop_api *mainloop_api = NULL;
41
42 static enum {
43 NONE,
44 EXIT,
45 STAT
46 } action = NONE;
47
48 static void quit(int ret) {
49 assert(mainloop_api);
50 mainloop_api->quit(mainloop_api, ret);
51 }
52
53 static void context_die_callback(struct pa_context *c, void *userdata) {
54 assert(c);
55 fprintf(stderr, "Connection to server shut down, exiting.\n");
56 quit(1);
57 }
58
59 static void context_drain_complete(struct pa_context *c, void *userdata) {
60 assert(c);
61 fprintf(stderr, "Connection to server shut down, exiting.\n");
62 quit(0);
63 }
64
65 static void drain(void) {
66 if (pa_context_drain(context, context_drain_complete, NULL) < 0)
67 quit(0);
68 }
69
70 static void stat_callback(struct pa_context *c, uint32_t blocks, uint32_t total, void *userdata) {
71 if (blocks == (uint32_t) -1) {
72 fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
73 quit(1);
74 return;
75 }
76
77 fprintf(stderr, "Currently in use: %u blocks containing %u bytes total.\n", blocks, total);
78 drain();
79 }
80
81 static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
82 assert(c);
83
84 if (!success) {
85 fprintf(stderr, "Connection failed: %s\n", pa_strerror(pa_context_errno(c)));
86 goto fail;
87 }
88
89 fprintf(stderr, "Connection established.\n");
90
91 if (action == STAT)
92 pa_context_stat(c, stat_callback, NULL);
93 else {
94 assert(action == EXIT);
95 pa_context_exit(c);
96 drain();
97 }
98
99 return;
100
101 fail:
102 quit(1);
103 }
104
105 static void exit_signal_callback(void *id, int sig, void *userdata) {
106 fprintf(stderr, "Got SIGINT, exiting.\n");
107 quit(0);
108
109 }
110
111 int main(int argc, char *argv[]) {
112 struct pa_mainloop* m = NULL;
113 int ret = 1, r;
114
115 if (argc >= 2) {
116 if (!strcmp(argv[1], "stat"))
117 action = STAT;
118 else if (!strcmp(argv[1], "exit"))
119 action = EXIT;
120 }
121
122 if (action == NONE) {
123 fprintf(stderr, "No valid action specified. Use one of: stat, exit\n");
124 goto quit;
125 }
126
127 if (!(m = pa_mainloop_new())) {
128 fprintf(stderr, "pa_mainloop_new() failed.\n");
129 goto quit;
130 }
131
132 mainloop_api = pa_mainloop_get_api(m);
133
134 r = pa_signal_init(mainloop_api);
135 assert(r == 0);
136 pa_signal_register(SIGINT, exit_signal_callback, NULL);
137 signal(SIGPIPE, SIG_IGN);
138
139 if (!(context = pa_context_new(mainloop_api, argv[0]))) {
140 fprintf(stderr, "pa_context_new() failed.\n");
141 goto quit;
142 }
143
144 if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
145 fprintf(stderr, "pa_context_connext() failed.\n");
146 goto quit;
147 }
148
149 pa_context_set_die_callback(context, context_die_callback, NULL);
150
151 if (pa_mainloop_run(m, &ret) < 0) {
152 fprintf(stderr, "pa_mainloop_run() failed.\n");
153 goto quit;
154 }
155
156 quit:
157 if (context)
158 pa_context_free(context);
159
160 if (m) {
161 pa_signal_done();
162 pa_mainloop_free(m);
163 }
164
165 return ret;
166 }