]> code.delx.au - pulseaudio/blob - polyp/pdispatch.c
add initial glib mainloop adapter
[pulseaudio] / 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
34 /*#define DEBUG_OPCODES*/
35
36 #ifdef DEBUG_OPCODES
37
38 static const char *command_names[PA_COMMAND_MAX] = {
39 [PA_COMMAND_ERROR] = "ERROR",
40 [PA_COMMAND_TIMEOUT] = "TIMEOUT",
41 [PA_COMMAND_REPLY] = "REPLY",
42 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = "CREATE_PLAYBACK_STREAM",
43 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = "DELETE_PLAYBACK_STREAM",
44 [PA_COMMAND_CREATE_RECORD_STREAM] = "CREATE_RECORD_STREAM",
45 [PA_COMMAND_DELETE_RECORD_STREAM] = "DELETE_RECORD_STREAM",
46 [PA_COMMAND_AUTH] = "AUTH",
47 [PA_COMMAND_REQUEST] = "REQUEST",
48 [PA_COMMAND_EXIT] = "EXIT",
49 [PA_COMMAND_SET_NAME] = "SET_NAME",
50 [PA_COMMAND_LOOKUP_SINK] = "LOOKUP_SINK",
51 [PA_COMMAND_LOOKUP_SOURCE] = "LOOKUP_SOURCE",
52 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = "DRAIN_PLAYBACK_STREAM",
53 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = "PLAYBACK_STREAM_KILLED",
54 [PA_COMMAND_RECORD_STREAM_KILLED] = "RECORD_STREAM_KILLED",
55 [PA_COMMAND_STAT] = "STAT",
56 [PA_COMMAND_GET_PLAYBACK_LATENCY] = "PLAYBACK_LATENCY",
57 [PA_COMMAND_CREATE_UPLOAD_STREAM] = "CREATE_UPLOAD_STREAM",
58 [PA_COMMAND_DELETE_UPLOAD_STREAM] = "DELETE_UPLOAD_STREAM",
59 [PA_COMMAND_FINISH_UPLOAD_STREAM] = "FINISH_UPLOAD_STREAM",
60 [PA_COMMAND_PLAY_SAMPLE] = "PLAY_SAMPLE",
61 [PA_COMMAND_REMOVE_SAMPLE] = "REMOVE_SAMPLE",
62 };
63
64 #endif
65
66 struct reply_info {
67 struct pa_pdispatch *pdispatch;
68 struct reply_info *next, *previous;
69 void (*callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
70 void *userdata;
71 uint32_t tag;
72 struct pa_time_event *time_event;
73 int callback_is_running;
74 };
75
76 struct pa_pdispatch {
77 struct pa_mainloop_api *mainloop;
78 const struct pa_pdispatch_command *command_table;
79 unsigned n_commands;
80 struct reply_info *replies;
81 void (*drain_callback)(struct pa_pdispatch *pd, void *userdata);
82 void *drain_userdata;
83 int in_use, shall_free;
84 };
85
86 static void reply_info_free(struct reply_info *r) {
87 assert(r && r->pdispatch && r->pdispatch->mainloop);
88
89 if (r->pdispatch)
90 r->pdispatch->mainloop->time_free(r->time_event);
91
92 if (r->previous)
93 r->previous->next = r->next;
94 else
95 r->pdispatch->replies = r->next;
96
97 if (r->next)
98 r->next->previous = r->previous;
99
100 pa_xfree(r);
101 }
102
103 struct pa_pdispatch* pa_pdispatch_new(struct pa_mainloop_api *mainloop, const struct pa_pdispatch_command*table, unsigned entries) {
104 struct pa_pdispatch *pd;
105 assert(mainloop);
106
107 assert((entries && table) || (!entries && !table));
108
109 pd = pa_xmalloc(sizeof(struct pa_pdispatch));
110 pd->mainloop = mainloop;
111 pd->command_table = table;
112 pd->n_commands = entries;
113 pd->replies = NULL;
114 pd->drain_callback = NULL;
115 pd->drain_userdata = NULL;
116
117 pd->in_use = pd->shall_free = 0;
118
119 return pd;
120 }
121
122 void pa_pdispatch_free(struct pa_pdispatch *pd) {
123 assert(pd);
124
125 if (pd->in_use) {
126 pd->shall_free = 1;
127 return;
128 }
129
130 while (pd->replies)
131 reply_info_free(pd->replies);
132 pa_xfree(pd);
133 }
134
135 int pa_pdispatch_run(struct pa_pdispatch *pd, struct pa_packet*packet, void *userdata) {
136 uint32_t tag, command;
137 struct pa_tagstruct *ts = NULL;
138 int ret = -1;
139 assert(pd && packet && packet->data && !pd->in_use);
140
141 if (packet->length <= 8)
142 goto finish;
143
144 ts = pa_tagstruct_new(packet->data, packet->length);
145 assert(ts);
146
147 if (pa_tagstruct_getu32(ts, &command) < 0 ||
148 pa_tagstruct_getu32(ts, &tag) < 0)
149 goto finish;
150
151 #ifdef DEBUG_OPCODES
152 fprintf(stderr, __FILE__": Recieved opcode <%s>\n", command_names[command]);
153 #endif
154
155 if (command == PA_COMMAND_ERROR || command == PA_COMMAND_REPLY) {
156 struct reply_info *r;
157
158 for (r = pd->replies; r; r = r->next)
159 if (r->tag == tag)
160 break;
161
162 if (r) {
163 pd->in_use = r->callback_is_running = 1;
164 assert(r->callback);
165 r->callback(r->pdispatch, command, tag, ts, r->userdata);
166 pd->in_use = r->callback_is_running = 0;
167 reply_info_free(r);
168
169 if (pd->shall_free)
170 pa_pdispatch_free(pd);
171 else {
172 if (pd->drain_callback && !pa_pdispatch_is_pending(pd))
173 pd->drain_callback(pd, pd->drain_userdata);
174 }
175 }
176
177 } else if (pd->command_table && command < pd->n_commands) {
178 const struct pa_pdispatch_command *c = pd->command_table+command;
179
180 if (c->proc)
181 c->proc(pd, command, tag, ts, userdata);
182 } else
183 goto finish;
184
185 ret = 0;
186
187 finish:
188 if (ts)
189 pa_tagstruct_free(ts);
190
191 return ret;
192 }
193
194 static void timeout_callback(struct pa_mainloop_api*m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
195 struct reply_info*r = userdata;
196 assert (r && r->time_event == e && r->pdispatch && r->pdispatch->mainloop == m && r->callback);
197
198 r->callback(r->pdispatch, PA_COMMAND_TIMEOUT, r->tag, NULL, r->userdata);
199 reply_info_free(r);
200
201 if (r->pdispatch->drain_callback && !pa_pdispatch_is_pending(r->pdispatch))
202 r->pdispatch->drain_callback(r->pdispatch, r->pdispatch->drain_userdata);
203 }
204
205 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) {
206 struct reply_info *r;
207 struct timeval tv;
208 assert(pd && cb);
209
210 r = pa_xmalloc(sizeof(struct reply_info));
211 r->pdispatch = pd;
212 r->callback = cb;
213 r->userdata = userdata;
214 r->tag = tag;
215 r->callback_is_running = 0;
216
217 gettimeofday(&tv, NULL);
218 tv.tv_sec += timeout;
219
220 r->time_event = pd->mainloop->time_new(pd->mainloop, &tv, timeout_callback, r);
221 assert(r->time_event);
222
223 r->previous = NULL;
224 r->next = pd->replies;
225 if (r->next)
226 r->next->previous = r;
227 pd->replies = r;
228 }
229
230 int pa_pdispatch_is_pending(struct pa_pdispatch *pd) {
231 assert(pd);
232
233 return !!pd->replies;
234 }
235
236 void pa_pdispatch_set_drain_callback(struct pa_pdispatch *pd, void (*cb)(struct pa_pdispatch *pd, void *userdata), void *userdata) {
237 assert(pd);
238 assert(!cb || pa_pdispatch_is_pending(pd));
239
240 pd->drain_callback = cb;
241 pd->drain_userdata = userdata;
242 }
243
244 void pa_pdispatch_unregister_reply(struct pa_pdispatch *pd, void *userdata) {
245 struct reply_info *r, *n;
246 assert(pd);
247
248 for (r = pd->replies; r; r = n) {
249 n = r->next;
250
251 if (!r->callback_is_running && r->userdata == userdata) /* when this item's callback is currently running it is destroyed anyway in the very near future */
252 reply_info_free(r);
253 }
254 }