4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
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.
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.
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
32 #include <pulse/xmalloc.h>
40 pa_anotify_cb_t callback;
43 pa_io_event *io_event;
44 pa_defer_event *defer_event;
46 uint8_t queued_events[EVENTS_MAX];
47 unsigned n_queued_events, queue_index;
50 static void dispatch_event(pa_anotify *a) {
52 assert(a->queue_index < a->n_queued_events);
54 a->callback(a->queued_events[a->queue_index++], a->userdata);
56 if (a->queue_index >= a->n_queued_events) {
57 a->n_queued_events = 0;
60 a->api->io_enable(a->io_event, PA_IO_EVENT_INPUT);
61 a->api->defer_enable(a->defer_event, 0);
63 a->api->io_enable(a->io_event, 0);
64 a->api->defer_enable(a->defer_event, 1);
68 static void io_callback(
72 pa_io_event_flags_t events,
75 pa_anotify *a = userdata;
79 assert(events == PA_IO_EVENT_INPUT);
80 assert(a->n_queued_events == 0);
82 r = read(fd, a->queued_events, sizeof(a->queued_events));
85 a->n_queued_events = (unsigned) r;
88 /* Only dispatch a single event */
92 static void defer_callback(pa_mainloop_api *api, pa_defer_event *e, void *userdata) {
93 pa_anotify *a = userdata;
99 pa_anotify *pa_anotify_new(pa_mainloop_api*api, pa_anotify_cb_t cb, void *userdata) {
105 a = pa_xnew(pa_anotify, 1);
107 if (pipe(a->fds) < 0) {
114 a->userdata = userdata;
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);
120 a->n_queued_events = 0;
125 void pa_anotify_free(pa_anotify *a) {
128 a->api->io_free(a->io_event);
129 a->api->defer_free(a->defer_event);
139 int pa_anotify_signal(pa_anotify *a, uint8_t event) {
143 r = write(a->fds[1], &event, 1);
144 return r != 1 ? -1 : 0;