]> code.delx.au - pulseaudio/blob - polyp/glib-test.c
99196c4a68acb5527e91bbbbb8229fd5346bea7c
[pulseaudio] / polyp / glib-test.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/time.h>
4 #include <assert.h>
5
6 /*#define GLIB_MAIN_LOOP*/
7
8 #ifdef GLIB_MAIN_LOOP
9 #include <glib.h>
10 #include "glib-mainloop.h"
11 static GMainLoop* glib_main_loop = NULL;
12 #else
13 #include "mainloop.h"
14 #endif
15
16 static void iocb(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
17 unsigned char c;
18 read(fd, &c, sizeof(c));
19 fprintf(stderr, "IO EVENT: %c\n", c < 32 ? '.' : c);
20 }
21
22 static void dcb(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata) {
23 fprintf(stderr, "DEFER EVENT\n");
24 a->defer_enable(e, 0);
25 }
26
27 static void tcb(struct pa_mainloop_api*a, struct pa_time_event *e, const struct timeval *tv, void *userdata) {
28 fprintf(stderr, "TIME EVENT\n");
29
30 #ifdef GLIB_MAIN_LOOP
31 g_main_loop_quit(glib_main_loop);
32 #else
33 a->quit(a, 0);
34 #endif
35 }
36
37 int main(int argc, char *argv[]) {
38 struct pa_mainloop_api *a;
39 struct pa_io_event *ioe;
40 struct pa_defer_event *de;
41 struct pa_time_event *te;
42 struct timeval tv;
43
44 #ifdef GLIB_MAIN_LOOP
45 struct pa_glib_mainloop *g;
46 glib_main_loop = g_main_loop_new(NULL, FALSE);
47 assert(glib_main_loop);
48
49 g = pa_glib_mainloop_new(NULL);
50 assert(g);
51
52 a = pa_glib_mainloop_get_api(g);
53 assert(a);
54 #else
55 struct pa_mainloop *m;
56
57 m = pa_mainloop_new();
58 assert(m);
59
60 a = pa_mainloop_get_api(m);
61 assert(a);
62 #endif
63
64 ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL);
65 assert(ioe);
66
67 de = a->defer_new(a, dcb, NULL);
68 assert(de);
69
70 gettimeofday(&tv, NULL);
71 tv.tv_sec += 10;
72 te = a->time_new(a, &tv, tcb, NULL);
73
74 #ifdef GLIB_MAIN_LOOP
75 g_main_loop_run(glib_main_loop);
76 #else
77 pa_mainloop_run(m, NULL);
78 #endif
79
80 a->time_free(te);
81 a->defer_free(de);
82 a->io_free(ioe);
83
84 #ifdef GLIB_MAIN_LOOP
85 pa_glib_mainloop_free(g);
86 g_main_loop_unref(glib_main_loop);
87 #else
88 pa_mainloop_free(m);
89 #endif
90
91 return 0;
92 }