]> code.delx.au - pulseaudio/blob - src/tests/mainloop-test.c
cd54bcfe58f2586cc05cb67d95a5227351c8fc24
[pulseaudio] / src / tests / mainloop-test.c
1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/time.h>
27 #include <assert.h>
28
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/gccmacro.h>
32
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/core-rtclock.h>
35
36 #ifdef GLIB_MAIN_LOOP
37
38 #include <glib.h>
39 #include <pulse/glib-mainloop.h>
40
41 static GMainLoop* glib_main_loop = NULL;
42
43 #else /* GLIB_MAIN_LOOP */
44 #include <pulse/mainloop.h>
45 #endif /* GLIB_MAIN_LOOP */
46
47 static pa_defer_event *de;
48
49 static void iocb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
50 unsigned char c;
51 pa_assert_se(read(fd, &c, sizeof(c)) >= 0);
52 fprintf(stderr, "IO EVENT: %c\n", c < 32 ? '.' : c);
53 a->defer_enable(de, 1);
54 }
55
56 static void dcb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
57 fprintf(stderr, "DEFER EVENT\n");
58 a->defer_enable(e, 0);
59 }
60
61 static void tcb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
62 fprintf(stderr, "TIME EVENT\n");
63
64 #if defined(GLIB_MAIN_LOOP)
65 g_main_loop_quit(glib_main_loop);
66 #else
67 a->quit(a, 0);
68 #endif
69 }
70
71 int main(int argc, char *argv[]) {
72 pa_mainloop_api *a;
73 pa_io_event *ioe;
74 pa_time_event *te;
75 struct timeval tv;
76
77 #ifdef GLIB_MAIN_LOOP
78 pa_glib_mainloop *g;
79
80 glib_main_loop = g_main_loop_new(NULL, FALSE);
81 assert(glib_main_loop);
82
83 g = pa_glib_mainloop_new(NULL);
84 assert(g);
85
86 a = pa_glib_mainloop_get_api(g);
87 assert(a);
88 #else /* GLIB_MAIN_LOOP */
89 pa_mainloop *m;
90
91 m = pa_mainloop_new();
92 assert(m);
93
94 a = pa_mainloop_get_api(m);
95 assert(a);
96 #endif /* GLIB_MAIN_LOOP */
97
98 ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL);
99 assert(ioe);
100
101 de = a->defer_new(a, dcb, NULL);
102 assert(de);
103
104 te = a->time_new(a, pa_timeval_rtstore(&tv, pa_rtclock_now() + 2 * PA_USEC_PER_SEC, TRUE), tcb, NULL);
105
106 #if defined(GLIB_MAIN_LOOP)
107 g_main_loop_run(glib_main_loop);
108 #else
109 pa_mainloop_run(m, NULL);
110 #endif
111
112 a->time_free(te);
113 a->defer_free(de);
114 a->io_free(ioe);
115
116 #ifdef GLIB_MAIN_LOOP
117 pa_glib_mainloop_free(g);
118 g_main_loop_unref(glib_main_loop);
119 #else
120 pa_mainloop_free(m);
121 #endif
122
123 return 0;
124 }