Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulsecore / anotify.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as
10   published by the Free Software Foundation; either version 2.1 of the
11   License, or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public
19   License along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <assert.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include "anotify.h"
35
36 #define EVENTS_MAX 16
37
38 struct pa_anotify {
39     pa_mainloop_api *api;
40     pa_anotify_cb_t callback;
41     void *userdata;
42     int fds[2];
43     pa_io_event *io_event;
44     pa_defer_event *defer_event;
45
46     uint8_t queued_events[EVENTS_MAX];
47     unsigned n_queued_events, queue_index;
48 };
49
50 static void dispatch_event(pa_anotify *a) {
51     assert(a);
52     assert(a->queue_index < a->n_queued_events);
53
54     a->callback(a->queued_events[a->queue_index++], a->userdata);
55
56     if (a->queue_index >= a->n_queued_events) {
57         a->n_queued_events = 0;
58         a->queue_index = 0;
59
60         a->api->io_enable(a->io_event, PA_IO_EVENT_INPUT);
61         a->api->defer_enable(a->defer_event, 0);
62     } else {
63         a->api->io_enable(a->io_event, 0);
64         a->api->defer_enable(a->defer_event, 1);
65     }
66 }
67
68 static void io_callback(
69         pa_mainloop_api *api,
70         pa_io_event *e,
71         int fd,
72         pa_io_event_flags_t events,
73         void *userdata) {
74
75     pa_anotify *a = userdata;
76     ssize_t r;
77
78     assert(a);
79     assert(events == PA_IO_EVENT_INPUT);
80     assert(a->n_queued_events == 0);
81
82     r = read(fd, a->queued_events, sizeof(a->queued_events));
83     assert(r > 0);
84
85     a->n_queued_events = (unsigned) r;
86     a->queue_index = 0;
87
88     /* Only dispatch a single event */
89     dispatch_event(a);
90 }
91
92 static void defer_callback(pa_mainloop_api *api, pa_defer_event *e, void *userdata) {
93     pa_anotify *a = userdata;
94     assert(a);
95
96     dispatch_event(a);
97 }
98
99 pa_anotify *pa_anotify_new(pa_mainloop_api*api, pa_anotify_cb_t cb, void *userdata) {
100     pa_anotify *a;
101
102     assert(api);
103     assert(cb);
104
105     a = pa_xnew(pa_anotify, 1);
106
107     if (pipe(a->fds) < 0) {
108         pa_xfree(a);
109         return NULL;
110     }
111
112     a->api = api;
113     a->callback = cb;
114     a->userdata = userdata;
115
116     a->io_event = api->io_new(api, a->fds[0], PA_IO_EVENT_INPUT, io_callback, a);
117     a->defer_event = api->defer_new(api, defer_callback, a);
118     a->api->defer_enable(a->defer_event, 0);
119
120     a->n_queued_events = 0;
121
122     return a;
123 }
124
125 void pa_anotify_free(pa_anotify *a) {
126     assert(a);
127
128     a->api->io_free(a->io_event);
129     a->api->defer_free(a->defer_event);
130
131     if (a->fds[0] >= 0)
132         close(a->fds[0]);
133     if (a->fds[1] >= 0)
134         close(a->fds[1]);
135
136     pa_xfree(a);
137 }
138
139 int pa_anotify_signal(pa_anotify *a, uint8_t event) {
140     ssize_t r;
141     assert(a);
142
143     r = write(a->fds[1], &event, 1);
144     return r != 1 ? -1 : 0;
145 }