4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
38 pa_anotify_cb_t callback;
41 pa_io_event *io_event;
42 pa_defer_event *defer_event;
44 uint8_t queued_events[EVENTS_MAX];
45 unsigned n_queued_events, queue_index;
48 static void dispatch_event(pa_anotify *a) {
50 assert(a->queue_index < a->n_queued_events);
52 a->callback(a->queued_events[a->queue_index++], a->userdata);
54 if (a->queue_index >= a->n_queued_events) {
55 a->n_queued_events = 0;
58 a->api->io_enable(a->io_event, PA_IO_EVENT_INPUT);
59 a->api->defer_enable(a->defer_event, 0);
61 a->api->io_enable(a->io_event, 0);
62 a->api->defer_enable(a->defer_event, 1);
66 static void io_callback(
70 pa_io_event_flags_t events,
73 pa_anotify *a = userdata;
77 assert(events == PA_IO_EVENT_INPUT);
78 assert(a->n_queued_events == 0);
80 r = read(fd, a->queued_events, sizeof(a->queued_events));
83 a->n_queued_events = (unsigned) r;
86 /* Only dispatch a single event */
90 static void defer_callback(pa_mainloop_api *api, pa_defer_event *e, void *userdata) {
91 pa_anotify *a = userdata;
97 pa_anotify *pa_anotify_new(pa_mainloop_api*api, pa_anotify_cb_t cb, void *userdata) {
103 a = pa_xnew(pa_anotify, 1);
105 if (pipe(a->fds) < 0) {
112 a->userdata = userdata;
114 a->io_event = api->io_new(api, a->fds[0], PA_IO_EVENT_INPUT, io_callback, a);
115 a->defer_event = api->defer_new(api, defer_callback, a);
116 a->api->defer_enable(a->defer_event, 0);
118 a->n_queued_events = 0;
123 void pa_anotify_free(pa_anotify *a) {
126 a->api->io_free(a->io_event);
127 a->api->defer_free(a->defer_event);
137 int pa_anotify_signal(pa_anotify *a, uint8_t event) {
141 r = write(a->fds[1], &event, 1);
142 return r != 1 ? -1 : 0;