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, see <http://www.gnu.org/licenses/>.
16 ***/
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <assert.h>
26 #include <check.h>
27
28 #include <pulse/rtclock.h>
29 #include <pulse/timeval.h>
30
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/core-rtclock.h>
33
34 #ifdef GLIB_MAIN_LOOP
35
36 #include <glib.h>
37 #include <pulse/glib-mainloop.h>
38
39 static GMainLoop* glib_main_loop = NULL;
40
41 #else /* GLIB_MAIN_LOOP */
42 #include <pulse/mainloop.h>
43 #endif /* GLIB_MAIN_LOOP */
44
45 static pa_defer_event *de;
46
iocb(pa_mainloop_api * a,pa_io_event * e,int fd,pa_io_event_flags_t f,void * userdata)47 static void iocb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
48 unsigned char c;
49 pa_assert_se(read(fd, &c, sizeof(c)) >= 0);
50 fprintf(stderr, "IO EVENT: %c\n", c < 32 ? '.' : c);
51 a->defer_enable(de, 1);
52 }
53
dcb(pa_mainloop_api * a,pa_defer_event * e,void * userdata)54 static void dcb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
55 fprintf(stderr, "DEFER EVENT\n");
56 a->defer_enable(e, 0);
57 }
58
tcb(pa_mainloop_api * a,pa_time_event * e,const struct timeval * tv,void * userdata)59 static void tcb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
60 fprintf(stderr, "TIME EVENT\n");
61
62 #if defined(GLIB_MAIN_LOOP)
63 g_main_loop_quit(glib_main_loop);
64 #else
65 a->quit(a, 0);
66 #endif
67 }
68
START_TEST(mainloop_test)69 START_TEST (mainloop_test) {
70 pa_mainloop_api *a;
71 pa_io_event *ioe;
72 pa_time_event *te;
73 struct timeval tv;
74
75 #ifdef GLIB_MAIN_LOOP
76 pa_glib_mainloop *g;
77
78 glib_main_loop = g_main_loop_new(NULL, FALSE);
79 fail_if(!glib_main_loop);
80
81 g = pa_glib_mainloop_new(NULL);
82 fail_if(!g);
83
84 a = pa_glib_mainloop_get_api(g);
85 fail_if(!a);
86 #else /* GLIB_MAIN_LOOP */
87 pa_mainloop *m;
88
89 m = pa_mainloop_new();
90 fail_if(!m);
91
92 a = pa_mainloop_get_api(m);
93 fail_if(!a);
94 #endif /* GLIB_MAIN_LOOP */
95
96 ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL);
97 fail_if(!ioe);
98
99 de = a->defer_new(a, dcb, NULL);
100 fail_if(!de);
101
102 te = a->time_new(a, pa_timeval_rtstore(&tv, pa_rtclock_now() + 2 * PA_USEC_PER_SEC, true), tcb, NULL);
103
104 #if defined(GLIB_MAIN_LOOP)
105 g_main_loop_run(glib_main_loop);
106 #else
107 pa_mainloop_run(m, NULL);
108 #endif
109
110 a->time_free(te);
111 a->defer_free(de);
112 a->io_free(ioe);
113
114 #ifdef GLIB_MAIN_LOOP
115 pa_glib_mainloop_free(g);
116 g_main_loop_unref(glib_main_loop);
117 #else
118 pa_mainloop_free(m);
119 #endif
120 }
121 END_TEST
122
main(int argc,char * argv[])123 int main(int argc, char *argv[]) {
124 int failed = 0;
125 Suite *s;
126 TCase *tc;
127 SRunner *sr;
128
129 s = suite_create("MainLoop");
130 tc = tcase_create("mainloop");
131 tcase_add_test(tc, mainloop_test);
132 suite_add_tcase(s, tc);
133
134 sr = srunner_create(s);
135 srunner_run_all(sr, CK_NORMAL);
136 failed = srunner_ntests_failed(sr);
137 srunner_free(sr);
138
139 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
140 }
141