]> code.delx.au - pulseaudio/blob - polyp/polyplib-operation.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / polyplib-operation.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 Lesser 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 Lesser 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 <assert.h>
27
28 #include "xmalloc.h"
29 #include "polyplib-internal.h"
30 #include "polyplib-operation.h"
31
32 struct pa_operation *pa_operation_new(struct pa_context *c, struct pa_stream *s) {
33 struct pa_operation *o;
34 assert(c);
35
36 o = pa_xmalloc(sizeof(struct pa_operation));
37 o->ref = 1;
38 o->context = pa_context_ref(c);
39 o->stream = s ? pa_stream_ref(s) : NULL;
40
41 o->state = PA_OPERATION_RUNNING;
42 o->userdata = NULL;
43 o->callback = NULL;
44
45 PA_LLIST_PREPEND(struct pa_operation, o->context->operations, o);
46 return pa_operation_ref(o);
47 }
48
49 struct pa_operation *pa_operation_ref(struct pa_operation *o) {
50 assert(o && o->ref >= 1);
51 o->ref++;
52 return o;
53 }
54
55 void pa_operation_unref(struct pa_operation *o) {
56 assert(o && o->ref >= 1);
57
58 if ((--(o->ref)) == 0) {
59 assert(!o->context);
60 assert(!o->stream);
61 free(o);
62 }
63 }
64
65 static void operation_set_state(struct pa_operation *o, enum pa_operation_state st) {
66 assert(o && o->ref >= 1);
67
68 if (st == o->state)
69 return;
70
71 if (!o->context)
72 return;
73
74 o->state = st;
75
76 if ((o->state == PA_OPERATION_DONE) || (o->state == PA_OPERATION_CANCELED)) {
77 PA_LLIST_REMOVE(struct pa_operation, o->context->operations, o);
78 pa_context_unref(o->context);
79 if (o->stream)
80 pa_stream_unref(o->stream);
81 o->context = NULL;
82 o->stream = NULL;
83 o->callback = NULL;
84 o->userdata = NULL;
85
86 pa_operation_unref(o);
87 }
88 }
89
90 void pa_operation_cancel(struct pa_operation *o) {
91 assert(o && o->ref >= 1);
92 operation_set_state(o, PA_OPERATION_CANCELED);
93 }
94
95 void pa_operation_done(struct pa_operation *o) {
96 assert(o && o->ref >= 1);
97 operation_set_state(o, PA_OPERATION_DONE);
98 }
99
100 enum pa_operation_state pa_operation_get_state(struct pa_operation *o) {
101 assert(o && o->ref >= 1);
102 return o->state;
103 }