]> code.delx.au - pulseaudio/blob - src/tests/mainloop-test.c
Reorganised the source tree. We now have src/ with a couple of subdirs:
[pulseaudio] / src / tests / mainloop-test.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 <polypcore/util.h>
32 #include <polypcore/gccmacro.h>
33
34 #ifdef GLIB_MAIN_LOOP
35
36 #include <glib.h>
37 #include <polyp/glib-mainloop.h>
38
39 static GMainLoop* glib_main_loop = NULL;
40
41 #if GLIB_MAJOR_VERSION >= 2
42 #define GLIB20
43 #else
44 #undef GLIB20
45 #endif
46
47
48 #else /* GLIB_MAIN_LOOP */
49 #include <polyp/mainloop.h>
50 #endif /* GLIB_MAIN_LOOP */
51
52 static pa_defer_event *de;
53
54 static void iocb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
55 unsigned char c;
56 read(fd, &c, sizeof(c));
57 fprintf(stderr, "IO EVENT: %c\n", c < 32 ? '.' : c);
58 a->defer_enable(de, 1);
59 }
60
61 static void dcb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
62 fprintf(stderr, "DEFER EVENT\n");
63 a->defer_enable(e, 0);
64 }
65
66 static void tcb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
67 fprintf(stderr, "TIME EVENT\n");
68
69 #if defined(GLIB_MAIN_LOOP) && defined(GLIB20)
70 g_main_loop_quit(glib_main_loop);
71 #elif defined(GLIB_MAIN_LOOP)
72 g_main_quit(glib_main_loop);
73 #else
74 a->quit(a, 0);
75 #endif
76 }
77
78 int main(PA_GCC_UNUSED int argc, PA_GCC_UNUSED char *argv[]) {
79 pa_mainloop_api *a;
80 pa_io_event *ioe;
81 pa_time_event *te;
82 struct timeval tv;
83
84 #ifdef GLIB_MAIN_LOOP
85 pa_glib_mainloop *g;
86
87 #ifdef GLIB20
88 glib_main_loop = g_main_loop_new(NULL, FALSE);
89 assert(glib_main_loop);
90
91 g = pa_glib_mainloop_new(NULL);
92 #else /* GLIB20 */
93 glib_main_loop = g_main_new(FALSE);
94 assert(glib_main_loop);
95
96 g = pa_glib_mainloop_new();
97 #endif /* GLIB20 */
98 assert(g);
99
100 a = pa_glib_mainloop_get_api(g);
101 assert(a);
102 #else /* GLIB_MAIN_LOOP */
103 pa_mainloop *m;
104
105 m = pa_mainloop_new();
106 assert(m);
107
108 a = pa_mainloop_get_api(m);
109 assert(a);
110 #endif /* GLIB_MAIN_LOOP */
111
112 ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL);
113 assert(ioe);
114
115 de = a->defer_new(a, dcb, NULL);
116 assert(de);
117
118 pa_gettimeofday(&tv);
119 tv.tv_sec += 10;
120 te = a->time_new(a, &tv, tcb, NULL);
121
122 #if defined(GLIB_MAIN_LOOP) && defined(GLIB20)
123 g_main_loop_run(glib_main_loop);
124 #elif defined(GLIB_MAIN_LOOP)
125 g_main_run(glib_main_loop);
126 #else
127 pa_mainloop_run(m, NULL);
128 #endif
129
130 a->time_free(te);
131 a->defer_free(de);
132 a->io_free(ioe);
133
134 #ifdef GLIB_MAIN_LOOP
135 pa_glib_mainloop_free(g);
136 #ifdef GLIB20
137 g_main_loop_unref(glib_main_loop);
138 #else
139 g_main_destroy(glib_main_loop);
140 #endif
141 #else
142 pa_mainloop_free(m);
143 #endif
144
145 return 0;
146 }