88f85e0285310b54491204949aa500cdc4759417
[platform/upstream/pulseaudio.git] / polyp / pdispatch.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 <stdlib.h>
28 #include <assert.h>
29
30 #include "pdispatch.h"
31 #include "native-common.h"
32 #include "xmalloc.h"
33 #include "llist.h"
34 #include "log.h"
35
36 /*#define DEBUG_OPCODES*/
37
38 #ifdef DEBUG_OPCODES
39
40 static const char *command_names[PA_COMMAND_MAX] = {
41     [PA_COMMAND_ERROR] = "ERROR",
42     [PA_COMMAND_TIMEOUT] = "TIMEOUT",
43     [PA_COMMAND_REPLY] = "REPLY",
44     [PA_COMMAND_CREATE_PLAYBACK_STREAM] = "CREATE_PLAYBACK_STREAM",
45     [PA_COMMAND_DELETE_PLAYBACK_STREAM] = "DELETE_PLAYBACK_STREAM",
46     [PA_COMMAND_CREATE_RECORD_STREAM] = "CREATE_RECORD_STREAM",
47     [PA_COMMAND_DELETE_RECORD_STREAM] = "DELETE_RECORD_STREAM",
48     [PA_COMMAND_AUTH] = "AUTH",
49     [PA_COMMAND_REQUEST] = "REQUEST",
50     [PA_COMMAND_EXIT] = "EXIT",
51     [PA_COMMAND_SET_NAME] = "SET_NAME",
52     [PA_COMMAND_LOOKUP_SINK] = "LOOKUP_SINK",
53     [PA_COMMAND_LOOKUP_SOURCE] = "LOOKUP_SOURCE",
54     [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = "DRAIN_PLAYBACK_STREAM",
55     [PA_COMMAND_PLAYBACK_STREAM_KILLED] = "PLAYBACK_STREAM_KILLED",
56     [PA_COMMAND_RECORD_STREAM_KILLED] = "RECORD_STREAM_KILLED",
57     [PA_COMMAND_STAT] = "STAT",
58     [PA_COMMAND_GET_PLAYBACK_LATENCY] = "PLAYBACK_LATENCY",
59     [PA_COMMAND_CREATE_UPLOAD_STREAM] = "CREATE_UPLOAD_STREAM",
60     [PA_COMMAND_DELETE_UPLOAD_STREAM] = "DELETE_UPLOAD_STREAM",
61     [PA_COMMAND_FINISH_UPLOAD_STREAM] = "FINISH_UPLOAD_STREAM",
62     [PA_COMMAND_PLAY_SAMPLE] = "PLAY_SAMPLE",
63     [PA_COMMAND_REMOVE_SAMPLE] = "REMOVE_SAMPLE",
64     [PA_COMMAND_GET_SERVER_INFO] = "GET_SERVER_INFO",
65     [PA_COMMAND_GET_SINK_INFO] = "GET_SINK_INFO",
66     [PA_COMMAND_GET_SINK_INFO_LIST] = "GET_SINK_INFO_LIST",
67     [PA_COMMAND_GET_SOURCE_INFO] = "GET_SOURCE_INFO",
68     [PA_COMMAND_GET_SOURCE_INFO_LIST] = "GET_SOURCE_INFO_LIST",
69     [PA_COMMAND_GET_MODULE_INFO] = "GET_MODULE_INFO",
70     [PA_COMMAND_GET_MODULE_INFO_LIST] = "GET_MODULE_INFO_LIST",
71     [PA_COMMAND_GET_CLIENT_INFO] = "GET_CLIENT_INFO",
72     [PA_COMMAND_GET_CLIENT_INFO_LIST] = "GET_CLIENT_INFO_LIST",
73     [PA_COMMAND_GET_SAMPLE_INFO] = "GET_SAMPLE_INFO",
74     [PA_COMMAND_GET_SAMPLE_INFO_LIST] = "GET_SAMPLE_INFO_LIST",
75     [PA_COMMAND_GET_SINK_INPUT_INFO] = "GET_SINK_INPUT_INFO",
76     [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = "GET_SINK_INPUT_INFO_LIST",
77     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = "GET_SOURCE_OUTPUT_INFO",
78     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = "GET_SOURCE_OUTPUT_INFO_LIST",
79     [PA_COMMAND_SUBSCRIBE] = "SUBSCRIBE",
80     [PA_COMMAND_SUBSCRIBE_EVENT] = "SUBSCRIBE_EVENT",
81     [PA_COMMAND_SET_SINK_VOLUME] = "SET_SINK_VOLUME",
82     [PA_COMMAND_SET_SINK_INPUT_VOLUME] = "SET_SINK_INPUT_VOLUME",
83     [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = "TRIGGER_PLAYBACK_STREAM",
84     [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = "FLUSH_PLAYBACK_STREAM",
85     [PA_COMMAND_CORK_PLAYBACK_STREAM] = "CORK_PLAYBACK_STREAM",
86 };
87
88 #endif
89
90 struct reply_info {
91     struct pa_pdispatch *pdispatch;
92     PA_LLIST_FIELDS(struct reply_info);
93     void (*callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
94     void *userdata;
95     uint32_t tag;
96     struct pa_time_event *time_event;
97 };
98
99 struct pa_pdispatch {
100     int ref;
101     struct pa_mainloop_api *mainloop;
102     const struct pa_pdispatch_command *command_table;
103     unsigned n_commands;
104     PA_LLIST_HEAD(struct reply_info, replies);
105     void (*drain_callback)(struct pa_pdispatch *pd, void *userdata);
106     void *drain_userdata;
107 };
108
109 static void reply_info_free(struct reply_info *r) {
110     assert(r && r->pdispatch && r->pdispatch->mainloop);
111
112     if (r->time_event)
113         r->pdispatch->mainloop->time_free(r->time_event);
114     
115     PA_LLIST_REMOVE(struct reply_info, r->pdispatch->replies, r);
116     
117     pa_xfree(r);
118 }
119
120 struct pa_pdispatch* pa_pdispatch_new(struct pa_mainloop_api *mainloop, const struct pa_pdispatch_command*table, unsigned entries) {
121     struct pa_pdispatch *pd;
122     assert(mainloop);
123
124     assert((entries && table) || (!entries && !table));
125     
126     pd = pa_xmalloc(sizeof(struct pa_pdispatch));
127     pd->ref = 1;
128     pd->mainloop = mainloop;
129     pd->command_table = table;
130     pd->n_commands = entries;
131     PA_LLIST_HEAD_INIT(struct pa_reply_info, pd->replies);
132     pd->drain_callback = NULL;
133     pd->drain_userdata = NULL;
134
135     return pd;
136 }
137
138 void pdispatch_free(struct pa_pdispatch *pd) {
139     assert(pd);
140
141     while (pd->replies)
142         reply_info_free(pd->replies);
143     
144     pa_xfree(pd);
145 }
146
147 static void run_action(struct pa_pdispatch *pd, struct reply_info *r, uint32_t command, struct pa_tagstruct *ts) {
148     void (*callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
149     void *userdata;
150     uint32_t tag;
151     assert(r);
152
153     pa_pdispatch_ref(pd);
154     
155     callback = r->callback;
156     userdata = r->userdata;
157     tag = r->tag;
158     
159     reply_info_free(r);
160     
161     callback(pd, command, tag, ts, userdata);
162
163     if (pd->drain_callback && !pa_pdispatch_is_pending(pd))
164         pd->drain_callback(pd, pd->drain_userdata);
165
166     pa_pdispatch_unref(pd);
167 }
168
169 int pa_pdispatch_run(struct pa_pdispatch *pd, struct pa_packet*packet, void *userdata) {
170     uint32_t tag, command;
171     struct pa_tagstruct *ts = NULL;
172     int ret = -1;
173     assert(pd && packet && packet->data);
174
175     pa_pdispatch_ref(pd);
176     
177     if (packet->length <= 8)
178         goto finish;
179
180     ts = pa_tagstruct_new(packet->data, packet->length);
181     assert(ts);
182     
183     if (pa_tagstruct_getu32(ts, &command) < 0 ||
184         pa_tagstruct_getu32(ts, &tag) < 0)
185         goto finish;
186
187 #ifdef DEBUG_OPCODES
188 {
189     char t[256];
190     char const *p;
191     if (!(p = command_names[command]))
192         snprintf((char*) (p = t), sizeof(t), "%u", command);
193         
194     pa_log(__FILE__": Recieved opcode <%s>\n", p);
195 }
196 #endif
197
198     if (command == PA_COMMAND_ERROR || command == PA_COMMAND_REPLY) {
199         struct reply_info *r;
200
201         for (r = pd->replies; r; r = r->next)
202             if (r->tag == tag)
203                 break;
204
205         if (r)
206             run_action(pd, r, command, ts);
207
208     } else if (pd->command_table && (command < pd->n_commands) && pd->command_table[command].proc) {
209         const struct pa_pdispatch_command *c = pd->command_table+command;
210
211         c->proc(pd, command, tag, ts, userdata);
212     } else {
213         pa_log(__FILE__": Recieved unsupported command %u\n", command);
214         goto finish;
215     }
216
217     ret = 0;
218         
219 finish:
220     if (ts)
221         pa_tagstruct_free(ts);
222
223     pa_pdispatch_unref(pd);
224
225     return ret;
226 }
227
228 static void timeout_callback(struct pa_mainloop_api*m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
229     struct reply_info*r = userdata;
230     assert(r && r->time_event == e && r->pdispatch && r->pdispatch->mainloop == m && r->callback);
231
232     run_action(r->pdispatch, r, PA_COMMAND_TIMEOUT, NULL);
233 }
234
235 void pa_pdispatch_register_reply(struct pa_pdispatch *pd, uint32_t tag, int timeout, void (*cb)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata), void *userdata) {
236     struct reply_info *r;
237     struct timeval tv;
238     assert(pd && pd->ref >= 1 && cb);
239
240     r = pa_xmalloc(sizeof(struct reply_info));
241     r->pdispatch = pd;
242     r->callback = cb;
243     r->userdata = userdata;
244     r->tag = tag;
245     
246     gettimeofday(&tv, NULL);
247     tv.tv_sec += timeout;
248
249     r->time_event = pd->mainloop->time_new(pd->mainloop, &tv, timeout_callback, r);
250     assert(r->time_event);
251
252     PA_LLIST_PREPEND(struct reply_info, pd->replies, r);
253 }
254
255 int pa_pdispatch_is_pending(struct pa_pdispatch *pd) {
256     assert(pd);
257
258     return !!pd->replies;
259 }
260
261 void pa_pdispatch_set_drain_callback(struct pa_pdispatch *pd, void (*cb)(struct pa_pdispatch *pd, void *userdata), void *userdata) {
262     assert(pd);
263     assert(!cb || pa_pdispatch_is_pending(pd));
264
265     pd->drain_callback = cb;
266     pd->drain_userdata = userdata;
267 }
268
269 void pa_pdispatch_unregister_reply(struct pa_pdispatch *pd, void *userdata) {
270     struct reply_info *r, *n;
271     assert(pd);
272
273     for (r = pd->replies; r; r = n) {
274         n = r->next;
275
276         if (r->userdata == userdata) 
277             reply_info_free(r);
278     }
279 }
280
281 void pa_pdispatch_unref(struct pa_pdispatch *pd) {
282     assert(pd && pd->ref >= 1);
283
284     if (!(--(pd->ref)))
285         pdispatch_free(pd);
286 }
287
288 struct pa_pdispatch* pa_pdispatch_ref(struct pa_pdispatch *pd) {
289     assert(pd && pd->ref >= 1);
290     pd->ref++;
291     return pd;
292 }