]> code.delx.au - pulseaudio/blob - src/tests/mainloop-test.c
671adeff293c3359ceecf843a2cb176acd206257
[pulseaudio] / src / tests / mainloop-test.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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 published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
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 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <assert.h>
30
31 #include <pulse/timeval.h>
32
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/gccmacro.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 #if GLIB_MAJOR_VERSION >= 2
44 #define GLIB20
45 #else
46 #undef GLIB20
47 #endif
48
49
50 #else /* GLIB_MAIN_LOOP */
51 #include <pulse/mainloop.h>
52 #endif /* GLIB_MAIN_LOOP */
53
54 static pa_defer_event *de;
55
56 static void iocb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
57 unsigned char c;
58 read(fd, &c, sizeof(c));
59 fprintf(stderr, "IO EVENT: %c\n", c < 32 ? '.' : c);
60 a->defer_enable(de, 1);
61 }
62
63 static void dcb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
64 fprintf(stderr, "DEFER EVENT\n");
65 a->defer_enable(e, 0);
66 }
67
68 static void tcb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
69 fprintf(stderr, "TIME EVENT\n");
70
71 #if defined(GLIB_MAIN_LOOP) && defined(GLIB20)
72 g_main_loop_quit(glib_main_loop);
73 #elif defined(GLIB_MAIN_LOOP)
74 g_main_quit(glib_main_loop);
75 #else
76 a->quit(a, 0);
77 #endif
78 }
79
80 int main(PA_GCC_UNUSED int argc, PA_GCC_UNUSED char *argv[]) {
81 pa_mainloop_api *a;
82 pa_io_event *ioe;
83 pa_time_event *te;
84 struct timeval tv;
85
86 #ifdef GLIB_MAIN_LOOP
87 pa_glib_mainloop *g;
88
89 #ifdef GLIB20
90 glib_main_loop = g_main_loop_new(NULL, FALSE);
91 assert(glib_main_loop);
92
93 g = pa_glib_mainloop_new(NULL);
94 #else /* GLIB20 */
95 glib_main_loop = g_main_new(FALSE);
96 assert(glib_main_loop);
97
98 g = pa_glib_mainloop_new();
99 #endif /* GLIB20 */
100 assert(g);
101
102 a = pa_glib_mainloop_get_api(g);
103 assert(a);
104 #else /* GLIB_MAIN_LOOP */
105 pa_mainloop *m;
106
107 m = pa_mainloop_new();
108 assert(m);
109
110 a = pa_mainloop_get_api(m);
111 assert(a);
112 #endif /* GLIB_MAIN_LOOP */
113
114 ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL);
115 assert(ioe);
116
117 de = a->defer_new(a, dcb, NULL);
118 assert(de);
119
120 pa_gettimeofday(&tv);
121 tv.tv_sec += 10;
122 te = a->time_new(a, &tv, tcb, NULL);
123
124 #if defined(GLIB_MAIN_LOOP) && defined(GLIB20)
125 g_main_loop_run(glib_main_loop);
126 #elif defined(GLIB_MAIN_LOOP)
127 g_main_run(glib_main_loop);
128 #else
129 pa_mainloop_run(m, NULL);
130 #endif
131
132 a->time_free(te);
133 a->defer_free(de);
134 a->io_free(ioe);
135
136 #ifdef GLIB_MAIN_LOOP
137 pa_glib_mainloop_free(g);
138 #ifdef GLIB20
139 g_main_loop_unref(glib_main_loop);
140 #else
141 g_main_destroy(glib_main_loop);
142 #endif
143 #else
144 pa_mainloop_free(m);
145 #endif
146
147 return 0;
148 }