source: roaraudio/libroarpulse/context.c @ 3392:842c8d8f99a9

Last change on this file since 3392:842c8d8f99a9 was 3392:842c8d8f99a9, checked in by phi, 14 years ago

wrote most of the context code, should work now

File size: 7.7 KB
Line 
1//context.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from libpulse*. They are mostly copyrighted by:
7 *  Lennart Poettering <poettering@users.sourceforge.net> and
8 *  Pierre Ossman <drzeus@drzeus.cx>
9 *
10 *  This file is part of libroarpulse a part of RoarAudio,
11 *  a cross-platform sound system for both, home and professional use.
12 *  See README for details.
13 *
14 *  This file is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License version 3
16 *  as published by the Free Software Foundation.
17 *
18 *  RoarAudio is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this software; see the file COPYING.  If not, write to
25 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 *  NOTE for everyone want's to change something and send patches:
28 *  read README and HACKING! There a addition information on
29 *  the license of this document you need to read before you send
30 *  any patches.
31 *
32 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
33 *  or libpulse*:
34 *  The libs libroaresd, libroararts and libroarpulse link this libroar
35 *  and are therefore GPL. Because of this it may be illigal to use
36 *  them with any software that uses libesd, libartsc or libpulse*.
37 */
38
39#include <libroarpulse/libroarpulse.h>
40
41struct _roar_pa_cb_st {
42 union {
43  pa_context_success_cb_t scb;
44  pa_context_notify_cb_t  ncb;
45 } cb;
46 void * userdata;
47};
48
49#define _call_cbs(x,c,s) ((x).cb.scb == NULL ? (void)0 : (x).cb.scb((c), (s), (x).userdata))
50#define _call_cbn(x,c)   ((x).cb.ncb == NULL ? (void)0 : (x).cb.ncb((c), (x).userdata))
51
52struct pa_context {
53 size_t refc;
54 struct roar_connection con;
55 char * server;
56 char * name;
57 int state;
58 int errnum;
59 struct {
60  struct _roar_pa_cb_st set_name;
61  struct _roar_pa_cb_st state_change;
62 } cb;
63};
64
65// older versions:
66typedef void pa_proplist;
67pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist);
68
69void pa_context_set_state(pa_context *c, pa_context_state_t st);
70
71pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name) {
72 return pa_context_new_with_proplist(mainloop, name, NULL);
73}
74
75pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist) {
76 pa_context * c;
77
78 if ( proplist != NULL )
79  return NULL;
80
81 if ( (c = roar_mm_malloc(sizeof(pa_context))) == NULL )
82  return NULL;
83
84 memset(c, 0, sizeof(pa_context));
85
86 c->refc  = 1;
87
88 c->state = PA_CONTEXT_UNCONNECTED;
89
90 c->errnum = PA_OK;
91
92 return NULL;
93}
94
95static void _context_free(pa_context *c) {
96 pa_context_disconnect(c);
97
98 if ( c->server != NULL )
99  roar_mm_free(c->server);
100
101 if ( c->name != NULL )
102  roar_mm_free(c->name);
103
104 roar_mm_free(c);
105}
106
107pa_context* pa_context_ref(pa_context *c) {
108 c->refc++;
109 return c;
110}
111
112void pa_context_unref(pa_context *c) {
113 c->refc--;
114
115 if ( c->refc < 1 )
116  _context_free(c);
117}
118
119int pa_context_connect(
120        pa_context *c,
121        const char *server,
122        pa_context_flags_t flags,
123        const pa_spawn_api *api) {
124
125 if ( c == NULL )
126  return -1;
127
128 if ( c->state != PA_CONTEXT_UNCONNECTED ) {
129  c->errnum = PA_ERR_BADSTATE;
130  pa_context_set_state(c, PA_CONTEXT_FAILED);
131  return -1;
132 }
133
134 // we do currently not support to spawn a daemon, so we ignore flags and api.
135
136 server = roar_pa_find_server((char*)server);
137
138 if ( roar_simple_connect(&(c->con), (char*)server,
139                          c->name != NULL ? c->name : "libroarpulse [pa_context_connect()]") == -1 ) {
140  c->errnum = PA_ERR_CONNECTIONREFUSED;
141  pa_context_set_state(c, PA_CONTEXT_FAILED);
142  return -1;
143 }
144
145 c->server = roar_mm_strdup(server);
146 pa_context_set_state(c, PA_CONTEXT_READY);
147
148 _call_cbs(c->cb.set_name, c, 1);
149
150 return 0;
151}
152
153void pa_context_disconnect(pa_context *c) {
154 if ( c == NULL )
155  return;
156
157 if ( c->state != PA_CONTEXT_READY )
158  return;
159
160 roar_disconnect(&(c->con));
161
162 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
163}
164
165
166
167/** Set a callback function that is called whenever the context status changes */
168void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
169 if ( c == NULL )
170  return;
171
172 c->cb.state_change.cb.ncb   = cb;
173 c->cb.state_change.userdata = userdata;
174}
175
176void pa_context_set_state(pa_context *c, pa_context_state_t st) {
177 if ( c == NULL )
178  return;
179
180 c->state = st;
181
182 _call_cbn(c->cb.state_change, c);
183}
184
185/** Return the error number of the last failed operation */
186int pa_context_errno(pa_context *c) {
187 if ( c == NULL )
188  return PA_ERR_INVALID;
189
190 return c->errnum;
191}
192
193/** Return non-zero if some data is pending to be written to the connection */
194int pa_context_is_pending(pa_context *c) {
195 return 0;
196}
197
198/** Return the current context status */
199pa_context_state_t pa_context_get_state(pa_context *c) {
200 if ( c == NULL )
201  return PA_CONTEXT_FAILED;
202
203 return c->state;
204}
205
206/** Drain the context. If there is nothing to drain, the function returns NULL */
207pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
208
209 if ( cb != NULL )
210  cb(c, userdata);
211
212 return NULL;
213}
214
215/** Tell the daemon to exit. The returned operation is unlikely to
216 * complete succesfully, since the daemon probably died before
217 * returning a success notification */
218pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata) {
219 int s = 1;
220
221 if ( c == NULL )
222  return NULL;
223
224 if ( c->state == PA_CONTEXT_READY ) {
225  if ( roar_exit(&(c->con)) == -1 ) {
226   c->errnum = PA_ERR_INTERNAL;
227   s = 0;
228  }
229 } else {
230  c->errnum = PA_ERR_BADSTATE;
231  s = 0;
232 }
233
234 if ( cb != NULL )
235  cb(c, s, userdata);
236
237 return NULL;
238}
239
240/** Set the name of the default sink. \since 0.4 */
241pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
242 if ( c != NULL )
243  c->errnum = PA_ERR_NOTSUPPORTED;
244
245 if ( cb != NULL )
246  cb(c, 0, userdata);
247
248 return NULL;
249}
250
251/** Set the name of the default source. \since 0.4 */
252pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
253 if ( c != NULL )
254  c->errnum = PA_ERR_NOTSUPPORTED;
255
256 if ( cb != NULL )
257  cb(c, 0, userdata);
258
259 return NULL;
260}
261
262/** Returns 1 when the connection is to a local daemon. Returns negative when no connection has been made yet. \since 0.5 */
263int pa_context_is_local(pa_context *c) {
264 if ( c == NULL )
265  return -1;
266
267 if ( c->state != PA_CONTEXT_READY )
268  return -1;
269
270 // how is /local/ defined?
271 return 0;
272}
273
274/** Set a different application name for context on the server. \since 0.5 */
275pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
276 if ( c == NULL )
277  return NULL;
278
279 if ( c->state != PA_CONTEXT_UNCONNECTED ) {
280  c->errnum = PA_ERR_BADSTATE;
281
282  if ( cb != NULL )
283   cb(c, 0, userdata);
284
285  return NULL;
286 }
287
288 c->name = roar_mm_strdup(name);
289 c->cb.set_name.cb.scb   = cb;
290 c->cb.set_name.userdata = userdata;
291
292 return NULL;
293}
294
295/** Return the server name this context is connected to. \since 0.7 */
296const char* pa_context_get_server(pa_context *c) {
297 if ( c == NULL )
298  return NULL;
299
300 return c->server;
301}
302
303/** Return the protocol version of the library. \since 0.8 */
304uint32_t pa_context_get_protocol_version(pa_context *c) {
305 return 0;
306}
307
308/** Return the protocol version of the connected server. \since 0.8 */
309uint32_t pa_context_get_server_protocol_version(pa_context *c) {
310 return 0;
311}
312
313
314//ll
Note: See TracBrowser for help on using the repository browser.