1 | /* |
---|
2 | * linc-source.c: This file is part of the linc library. |
---|
3 | * |
---|
4 | * Authors: |
---|
5 | * Owen Taylor (owen@redhat.com) |
---|
6 | * Michael Meeks (michael@ximian.com) |
---|
7 | * |
---|
8 | * Copyright 1998, 2001, Red Hat, Inc., Ximian, Inc., |
---|
9 | */ |
---|
10 | |
---|
11 | #include <sys/types.h> |
---|
12 | #include <sys/stat.h> |
---|
13 | #include <stdio.h> |
---|
14 | #include <unistd.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <string.h> |
---|
17 | #include <fcntl.h> |
---|
18 | |
---|
19 | #include <glib.h> |
---|
20 | #include "linc-debug.h" |
---|
21 | #include "linc-private.h" |
---|
22 | |
---|
23 | static gboolean |
---|
24 | linc_source_prepare (GSource *source, |
---|
25 | gint *timeout) |
---|
26 | { |
---|
27 | *timeout = -1; |
---|
28 | |
---|
29 | return FALSE; |
---|
30 | } |
---|
31 | |
---|
32 | static gboolean |
---|
33 | linc_source_check (GSource *source) |
---|
34 | { |
---|
35 | LincUnixWatch *watch = (LincUnixWatch *)source; |
---|
36 | |
---|
37 | return watch->pollfd.revents & watch->condition; |
---|
38 | } |
---|
39 | |
---|
40 | static gboolean |
---|
41 | linc_source_dispatch (GSource *source, |
---|
42 | GSourceFunc callback, |
---|
43 | gpointer user_data) |
---|
44 | |
---|
45 | { |
---|
46 | GIOFunc func; |
---|
47 | LincUnixWatch *watch = (LincUnixWatch *) source; |
---|
48 | |
---|
49 | if (!callback) |
---|
50 | g_error ("No callback"); |
---|
51 | |
---|
52 | func = (GIOFunc) callback; |
---|
53 | |
---|
54 | return (*func) (watch->channel, |
---|
55 | watch->pollfd.revents & watch->condition, |
---|
56 | user_data); |
---|
57 | } |
---|
58 | |
---|
59 | static void |
---|
60 | linc_source_finalize (GSource *source) |
---|
61 | { |
---|
62 | d_printf ("Finalize source %p", source); |
---|
63 | } |
---|
64 | |
---|
65 | GSourceFuncs linc_source_watch_funcs = { |
---|
66 | linc_source_prepare, |
---|
67 | linc_source_check, |
---|
68 | linc_source_dispatch, |
---|
69 | linc_source_finalize |
---|
70 | }; |
---|
71 | |
---|
72 | static void |
---|
73 | linc_source_set_condition (LincUnixWatch *watch, |
---|
74 | GIOCondition condition) |
---|
75 | { |
---|
76 | if (watch) { |
---|
77 | watch->pollfd.events = condition; |
---|
78 | watch->condition = condition; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | static GSource * |
---|
83 | linc_source_create_watch (GMainContext *context, |
---|
84 | int fd, |
---|
85 | GIOChannel *channel, |
---|
86 | GIOCondition condition, |
---|
87 | GIOFunc func, |
---|
88 | gpointer user_data) |
---|
89 | { |
---|
90 | GSource *source; |
---|
91 | LincUnixWatch *watch; |
---|
92 | |
---|
93 | source = g_source_new (&linc_source_watch_funcs, |
---|
94 | sizeof (LincUnixWatch)); |
---|
95 | watch = (LincUnixWatch *) source; |
---|
96 | |
---|
97 | watch->pollfd.fd = fd; |
---|
98 | watch->channel = channel; |
---|
99 | |
---|
100 | linc_source_set_condition (watch, condition); |
---|
101 | |
---|
102 | g_source_set_can_recurse (source, TRUE); |
---|
103 | g_source_add_poll (source, &watch->pollfd); |
---|
104 | |
---|
105 | g_source_set_callback (source, (GSourceFunc) func, |
---|
106 | user_data, NULL); |
---|
107 | g_source_attach (source, context); |
---|
108 | |
---|
109 | return source; |
---|
110 | } |
---|
111 | |
---|
112 | /* |
---|
113 | * Wrappers to make listening on two mainloops simpler |
---|
114 | */ |
---|
115 | |
---|
116 | LincWatch * |
---|
117 | linc_io_add_watch_fd (int fd, |
---|
118 | GIOCondition condition, |
---|
119 | GIOFunc func, |
---|
120 | gpointer user_data) |
---|
121 | { |
---|
122 | LincWatch *w; |
---|
123 | |
---|
124 | w = g_new (LincWatch, 1); |
---|
125 | |
---|
126 | /* Linc loop */ |
---|
127 | w->linc_source = linc_source_create_watch ( |
---|
128 | linc_main_get_context (), fd, NULL, |
---|
129 | condition, func, user_data); |
---|
130 | |
---|
131 | /* Main loop */ |
---|
132 | w->main_source = linc_source_create_watch ( |
---|
133 | NULL, fd, NULL, condition, func, user_data); |
---|
134 | |
---|
135 | return w; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * linc_io_add_watch: |
---|
140 | * @channel: the GIOChannel to watch |
---|
141 | * @condition: the condition mask to watch for |
---|
142 | * @func: the function to invoke when a condition is met |
---|
143 | * @user_data: a user data closure |
---|
144 | * |
---|
145 | * This routine creates a watch on an IO channel that operates both in |
---|
146 | * the standard glib mainloop, but also in the 'linc' mainloop so we |
---|
147 | * can iterate that without causing re-enterancy. |
---|
148 | * |
---|
149 | * This method is deprecated. |
---|
150 | * |
---|
151 | * Return value: a pointer identifying the watch. |
---|
152 | **/ |
---|
153 | LincWatch * |
---|
154 | linc_io_add_watch (GIOChannel *channel, |
---|
155 | GIOCondition condition, |
---|
156 | GIOFunc func, |
---|
157 | gpointer user_data) |
---|
158 | { |
---|
159 | LincWatch *w; |
---|
160 | int fd = g_io_channel_unix_get_fd(channel); |
---|
161 | |
---|
162 | w = g_new (LincWatch, 1); |
---|
163 | |
---|
164 | /* Linc loop */ |
---|
165 | w->linc_source = linc_source_create_watch ( |
---|
166 | linc_main_get_context (), fd, channel, |
---|
167 | condition, func, user_data); |
---|
168 | |
---|
169 | /* Main loop */ |
---|
170 | w->main_source = linc_source_create_watch ( |
---|
171 | NULL, fd, channel, condition, func, user_data); |
---|
172 | |
---|
173 | return w; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * linc_io_remove_watch: |
---|
178 | * @watch: the handle of a watch on a GIOChannel |
---|
179 | * |
---|
180 | * This removes a watch by it's handle in @w |
---|
181 | **/ |
---|
182 | void |
---|
183 | linc_io_remove_watch (LincWatch *w) |
---|
184 | { |
---|
185 | if (w) { |
---|
186 | linc_source_set_condition ((LincUnixWatch *) w->main_source, 0); |
---|
187 | linc_source_set_condition ((LincUnixWatch *) w->linc_source, 0); |
---|
188 | |
---|
189 | g_source_destroy (w->main_source); |
---|
190 | g_source_unref (w->main_source); |
---|
191 | |
---|
192 | g_source_destroy (w->linc_source); |
---|
193 | g_source_unref (w->linc_source); |
---|
194 | |
---|
195 | g_free (w); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | void |
---|
200 | linc_watch_set_condition (LincWatch *w, |
---|
201 | GIOCondition condition) |
---|
202 | { |
---|
203 | if (w) { |
---|
204 | linc_source_set_condition ( |
---|
205 | (LincUnixWatch *) w->linc_source, |
---|
206 | condition); |
---|
207 | |
---|
208 | linc_source_set_condition ( |
---|
209 | (LincUnixWatch *) w->main_source, |
---|
210 | condition); |
---|
211 | } |
---|
212 | } |
---|