summaryrefslogtreecommitdiffstats
path: root/polyp/subscribe.c
blob: d3b97de7e5de6e3f2d2e84ed7f3d71deef0f379b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  polypaudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <assert.h>

#include "queue.h"
#include "subscribe.h"
#include "xmalloc.h"
#include "log.h"

struct pa_subscription {
    struct pa_core *core;
    int dead;
    void (*callback)(struct pa_core *c, enum pa_subscription_event_type t, uint32_t index, void *userdata);
    void *userdata;
    enum pa_subscription_mask mask;

    struct pa_subscription *prev, *next;
};

struct pa_subscription_event {
    enum pa_subscription_event_type type;
    uint32_t index;
};

static void sched_event(struct pa_core *c);

struct pa_subscription* pa_subscription_new(struct pa_core *c, enum pa_subscription_mask m, void (*callback)(struct pa_core *c, enum pa_subscription_event_type t, uint32_t index, void *userdata), void *userdata) {
    struct pa_subscription *s;
    assert(c);

    s = pa_xmalloc(sizeof(struct pa_subscription));
    s->core = c;
    s->dead = 0;
    s->callback = callback;
    s->userdata = userdata;
    s->mask = m;

    if ((s->next = c->subscriptions))
        s->next->prev = s;
    s->prev = NULL;
    c->subscriptions = s;
    return s;
}

void pa_subscription_free(struct pa_subscription*s) {
    assert(s && !s->dead);
    s->dead = 1;
    sched_event(s->core);
}

static void free_item(struct pa_subscription *s) {
    assert(s && s->core);

    if (s->prev)
        s->prev->next = s->next;
    else
        s->core->subscriptions = s->next;
            
    if (s->next)
        s->next->prev = s->prev;
    
    pa_xfree(s);
}

void pa_subscription_free_all(struct pa_core *c) {
    struct pa_subscription_event *e;
    assert(c);
    
    while (c->subscriptions)
        free_item(c->subscriptions);

    if (c->subscription_event_queue) {
        while ((e = pa_queue_pop(c->subscription_event_queue)))
            pa_xfree(e);
        
        pa_queue_free(c->subscription_event_queue, NULL, NULL);
        c->subscription_event_queue = NULL;
    }

    if (c->subscription_defer_event) {
        c->mainloop->defer_free(c->subscription_defer_event);
        c->subscription_defer_event = NULL;
    }
}

/*static void dump_event(struct pa_subscription_event*e) {
    switch (e->type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
        case PA_SUBSCRIPTION_EVENT_SINK:
            pa_log(__FILE__": SINK_EVENT");
            break;
        case PA_SUBSCRIPTION_EVENT_SOURCE:
            pa_log(__FILE__": SOURCE_EVENT");
            break;
        case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
            pa_log(__FILE__": SINK_INPUT_EVENT");
            break;
        case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
            pa_log(__FILE__": SOURCE_OUTPUT_EVENT");
            break;
        case PA_SUBSCRIPTION_EVENT_MODULE:
            pa_log(__FILE__": MODULE_EVENT");
            break;
        case PA_SUBSCRIPTION_EVENT_CLIENT:
            pa_log(__FILE__": CLIENT_EVENT");
            break;
        default:
            pa_log(__FILE__": OTHER");
            break;
    }

    switch (e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
        case PA_SUBSCRIPTION_EVENT_NEW:
            pa_log(__FILE__":  NEW");
            break;
        case PA_SUBSCRIPTION_EVENT_CHANGE:
            pa_log(__FILE__":  CHANGE");
            break;
        case PA_SUBSCRIPTION_EVENT_REMOVE:
            pa_log(__FILE__":  REMOVE");
            break;
        default:
            pa_log(__FILE__":  OTHER");
            break;
    }

    pa_log(__FILE__":  %u\n", e->index);
}*/

static void defer_cb(struct pa_mainloop_api *m, struct pa_defer_event *e, void *userdata) {
    struct pa_core *c = userdata;
    struct pa_subscription *s;
    assert(c && c->subscription_defer_event == e && c->mainloop == m);

    c->mainloop->defer_enable(c->subscription_defer_event, 0);


    /* Dispatch queued events */
    
    if (c->subscription_event_queue) {
        struct pa_subscription_event *e;
        
        while ((e = pa_queue_pop(c->subscription_event_queue))) {
            struct pa_subscription *s;

            for (s = c->subscriptions; s; s = s->next) {

                if (!s->dead && pa_subscription_match_flags(s->mask, e->type))
                    s->callback(c, e->type, e->index, s->userdata);
            }
            
            pa_xfree(e);
        }
    }

    /* Remove dead subscriptions */
    
    s = c->subscriptions;
    while (s) {
        struct pa_subscription *n = s->next;
        if (s->dead)
            free_item(s);
        s = n;
    }
}

static void sched_event(struct pa_core *c) {
    assert(c);

    if (!c->subscription_defer_event) {
        c->subscription_defer_event = c->mainloop->defer_new(c->mainloop, defer_cb, c);
        assert(c->subscription_defer_event);
    }
        
    c->mainloop->defer_enable(c->subscription_defer_event, 1);
}


void pa_subscription_post(struct pa_core *c, enum pa_subscription_event_type t, uint32_t index) {
    struct pa_subscription_event *e;
    assert(c);

    e = pa_xmalloc(sizeof(struct pa_subscription_event));
    e->type = t;
    e->index = index;

    if (!c->subscription_event_queue) {
        c->subscription_event_queue = pa_queue_new();
        assert(c->subscription_event_queue);
    }
    
    pa_queue_push(c->subscription_event_queue, e);
    sched_event(c);
}