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