1 // GLib.Source.cs - Source class implementation
2 //
3 // Author: Duncan Mak  <duncan@ximian.com>
4 //
5 // Copyright (c) 2002 Mike Kestner
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of version 2 of the Lesser GNU General
9 // Public License as published by the Free Software Foundation.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this program; if not, write to the
18 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
20 
21 
22 namespace GLib {
23 
24 	using System;
25 	using System.Collections;
26 	using System.Runtime.InteropServices;
27 
GSourceFunc()28 	public delegate bool GSourceFunc ();
29 
30 	//
31 	// Base class for IdleProxy and TimeoutProxy
32 	//
33 	internal class SourceProxy {
34 		internal Delegate real_handler;
35 		internal Delegate proxy_handler;
36 		internal uint ID;
37 
Remove()38 		internal void Remove ()
39 		{
40 			lock (Source.source_handlers)
41 				Source.source_handlers.Remove (ID);
42 			real_handler = null;
43 			proxy_handler = null;
44 		}
45 	}
46 
47 	public partial class Source : GLib.Opaque {
48 
Source()49 		private Source () {}
50 
51 		internal static Hashtable source_handlers = new Hashtable ();
52 
53 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_remove(uint tag)54 		static extern bool g_source_remove (uint tag);
55 
Remove(uint tag)56 		public static bool Remove (uint tag)
57 		{
58 			lock (Source.source_handlers)
59 				source_handlers.Remove (tag);
60 			return g_source_remove (tag);
61 		}
62 
63 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_context(IntPtr raw)64 		static extern IntPtr g_source_get_context(IntPtr raw);
65 
66 		public GLib.MainContext Context {
67 			get  {
68 				IntPtr raw_ret = g_source_get_context(Handle);
69 				GLib.MainContext ret = raw_ret == IntPtr.Zero ? null : new MainContext (raw_ret);
70 				return ret;
71 			}
72 		}
73 
74 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_priority(IntPtr raw)75 		static extern int g_source_get_priority(IntPtr raw);
76 
77 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_set_priority(IntPtr raw, int priority)78 		static extern void g_source_set_priority(IntPtr raw, int priority);
79 
80 		public int Priority {
81 			get  {
82 				int raw_ret = g_source_get_priority(Handle);
83 				int ret = raw_ret;
84 				return ret;
85 			}
86 			set  {
87 				g_source_set_priority(Handle, value);
88 			}
89 		}
90 
91 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_name(IntPtr raw)92 		static extern IntPtr g_source_get_name(IntPtr raw);
93 
94 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_set_name(IntPtr raw, IntPtr name)95 		static extern void g_source_set_name(IntPtr raw, IntPtr name);
96 
97 		public string Name {
98 			get  {
99 				IntPtr raw_ret = g_source_get_name(Handle);
100 				string ret = GLib.Marshaller.Utf8PtrToString (raw_ret);
101 				return ret;
102 			}
103 			set  {
104 				IntPtr native_value = GLib.Marshaller.StringToPtrGStrdup (value);
105 				g_source_set_name(Handle, native_value);
106 				GLib.Marshaller.Free (native_value);
107 			}
108 		}
109 
110 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_type()111 		static extern IntPtr g_source_get_type();
112 
113 		public static GLib.GType GType {
114 			get {
115 				IntPtr raw_ret = g_source_get_type();
116 				GLib.GType ret = new GLib.GType(raw_ret);
117 				return ret;
118 			}
119 		}
120 
121 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_add_child_source(IntPtr raw, IntPtr child_source)122 		static extern void g_source_add_child_source(IntPtr raw, IntPtr child_source);
123 
AddChildSource(GLib.Source child_source)124 		public void AddChildSource(GLib.Source child_source) {
125 			g_source_add_child_source(Handle, child_source == null ? IntPtr.Zero : child_source.Handle);
126 		}
127 
128 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_add_poll(IntPtr raw, IntPtr fd)129 		static extern void g_source_add_poll(IntPtr raw, IntPtr fd);
130 
AddPoll(GLib.PollFD fd)131 		public void AddPoll(GLib.PollFD fd) {
132 			IntPtr native_fd = GLib.Marshaller.StructureToPtrAlloc (fd);
133 			g_source_add_poll(Handle, native_fd);
134 			fd = GLib.PollFD.New (native_fd);
135 			Marshal.FreeHGlobal (native_fd);
136 		}
137 
138 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_attach(IntPtr raw, IntPtr context)139 		static extern uint g_source_attach(IntPtr raw, IntPtr context);
140 
Attach(GLib.MainContext context)141 		public uint Attach(GLib.MainContext context) {
142 			uint raw_ret = g_source_attach(Handle, context == null ? IntPtr.Zero : context.Handle);
143 			uint ret = raw_ret;
144 			return ret;
145 		}
146 
Attach()147 		uint Attach() {
148 			return Attach (null);
149 		}
150 
151 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_can_recurse(IntPtr raw)152 		static extern bool g_source_get_can_recurse(IntPtr raw);
153 
154 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_set_can_recurse(IntPtr raw, bool can_recurse)155 		static extern void g_source_set_can_recurse(IntPtr raw, bool can_recurse);
156 
157 		public bool CanRecurse {
158 			get {
159 				bool raw_ret = g_source_get_can_recurse(Handle);
160 				bool ret = raw_ret;
161 				return ret;
162 			}
163 			set {
164 				g_source_set_can_recurse(Handle, value);
165 			}
166 		}
167 
168 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_current_time(IntPtr raw, IntPtr timeval)169 		static extern void g_source_get_current_time(IntPtr raw, IntPtr timeval);
170 
171 		[Obsolete]
GetCurrentTime(GLib.TimeVal timeval)172 		public void GetCurrentTime(GLib.TimeVal timeval) {
173 			IntPtr native_timeval = GLib.Marshaller.StructureToPtrAlloc (timeval);
174 			g_source_get_current_time(Handle, native_timeval);
175 			timeval = GLib.TimeVal.New (native_timeval);
176 			Marshal.FreeHGlobal (native_timeval);
177 		}
178 
179 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_id(IntPtr raw)180 		static extern uint g_source_get_id(IntPtr raw);
181 
182 		public uint Id {
183 			get {
184 				uint raw_ret = g_source_get_id(Handle);
185 				uint ret = raw_ret;
186 				return ret;
187 			}
188 		}
189 
190 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_ready_time(IntPtr raw)191 		static extern long g_source_get_ready_time(IntPtr raw);
192 
193 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_set_ready_time(IntPtr raw, long ready_time)194 		static extern void g_source_set_ready_time(IntPtr raw, long ready_time);
195 
196 		public long ReadyTime {
197 			get {
198 				long raw_ret = g_source_get_ready_time(Handle);
199 				long ret = raw_ret;
200 				return ret;
201 			}
202 			set {
203 				g_source_set_ready_time(Handle, value);
204 			}
205 		}
206 
207 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_get_time(IntPtr raw)208 		static extern long g_source_get_time(IntPtr raw);
209 
210 		public long Time {
211 			get {
212 				long raw_ret = g_source_get_time(Handle);
213 				long ret = raw_ret;
214 				return ret;
215 			}
216 		}
217 
218 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_is_destroyed(IntPtr raw)219 		static extern bool g_source_is_destroyed(IntPtr raw);
220 
221 		public bool IsDestroyed {
222 			get {
223 				bool raw_ret = g_source_is_destroyed(Handle);
224 				bool ret = raw_ret;
225 				return ret;
226 			}
227 		}
228 
229 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_modify_unix_fd(IntPtr raw, IntPtr tag, int new_events)230 		static extern void g_source_modify_unix_fd(IntPtr raw, IntPtr tag, int new_events);
231 
ModifyUnixFd(IntPtr tag, GLib.IOCondition new_events)232 		public void ModifyUnixFd(IntPtr tag, GLib.IOCondition new_events) {
233 			g_source_modify_unix_fd(Handle, tag, (int) new_events);
234 		}
235 
236 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_query_unix_fd(IntPtr raw, IntPtr tag)237 		static extern int g_source_query_unix_fd(IntPtr raw, IntPtr tag);
238 
QueryUnixFd(IntPtr tag)239 		public GLib.IOCondition QueryUnixFd(IntPtr tag) {
240 			int raw_ret = g_source_query_unix_fd(Handle, tag);
241 			GLib.IOCondition ret = (GLib.IOCondition) raw_ret;
242 			return ret;
243 		}
244 
245 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_remove_child_source(IntPtr raw, IntPtr child_source)246 		static extern void g_source_remove_child_source(IntPtr raw, IntPtr child_source);
247 
RemoveChildSource(GLib.Source child_source)248 		public void RemoveChildSource(GLib.Source child_source) {
249 			g_source_remove_child_source(Handle, child_source == null ? IntPtr.Zero : child_source.Handle);
250 		}
251 
252 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_remove_poll(IntPtr raw, IntPtr fd)253 		static extern void g_source_remove_poll(IntPtr raw, IntPtr fd);
254 
RemovePoll(GLib.PollFD fd)255 		public void RemovePoll(GLib.PollFD fd) {
256 			IntPtr native_fd = GLib.Marshaller.StructureToPtrAlloc (fd);
257 			g_source_remove_poll(Handle, native_fd);
258 			fd = GLib.PollFD.New (native_fd);
259 			Marshal.FreeHGlobal (native_fd);
260 		}
261 
262 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_remove_unix_fd(IntPtr raw, IntPtr tag)263 		static extern void g_source_remove_unix_fd(IntPtr raw, IntPtr tag);
264 
RemoveUnixFd(IntPtr tag)265 		public void RemoveUnixFd(IntPtr tag) {
266 			g_source_remove_unix_fd(Handle, tag);
267 		}
268 
269 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_set_callback_indirect(IntPtr raw, IntPtr callback_data, IntPtr callback_funcs)270 		static extern void g_source_set_callback_indirect(IntPtr raw, IntPtr callback_data, IntPtr callback_funcs);
271 
SetCallbackIndirect(IntPtr callback_data, GLib.SourceCallbackFuncs callback_funcs)272 		public void SetCallbackIndirect(IntPtr callback_data, GLib.SourceCallbackFuncs callback_funcs) {
273 			IntPtr native_callback_funcs = GLib.Marshaller.StructureToPtrAlloc (callback_funcs);
274 			g_source_set_callback_indirect(Handle, callback_data, native_callback_funcs);
275 			callback_funcs = GLib.SourceCallbackFuncs.New (native_callback_funcs);
276 			Marshal.FreeHGlobal (native_callback_funcs);
277 		}
278 
279 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_set_funcs(IntPtr raw, IntPtr value)280 		static extern void g_source_set_funcs(IntPtr raw, IntPtr value);
281 
282 		public GLib.SourceFuncs Funcs {
283 			set {
284 				IntPtr native_value = GLib.Marshaller.StructureToPtrAlloc (value);
285 				g_source_set_funcs(Handle, native_value);
286 				value = GLib.SourceFuncs.New (native_value);
287 				Marshal.FreeHGlobal (native_value);
288 			}
289 		}
290 
291 		/*
292 		 * commented out because there is already a custom implementation for Remove
293 		 *
294 		[DllImport (Global.GLibNativeLib, CallingConvention = CallingConvention.Cdecl)]
295 		static extern bool g_source_remove(uint tag);
296 
297 		public static bool Remove(uint tag) {
298 			bool raw_ret = g_source_remove(tag);
299 			bool ret = raw_ret;
300 			return ret;
301 		}
302 		*/
303 
304 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_remove_by_funcs_user_data(IntPtr funcs, IntPtr user_data)305 		static extern bool g_source_remove_by_funcs_user_data(IntPtr funcs, IntPtr user_data);
306 
RemoveByFuncsUserData(GLib.SourceFuncs funcs, IntPtr user_data)307 		public static bool RemoveByFuncsUserData(GLib.SourceFuncs funcs, IntPtr user_data) {
308 			IntPtr native_funcs = GLib.Marshaller.StructureToPtrAlloc (funcs);
309 			bool raw_ret = g_source_remove_by_funcs_user_data(native_funcs, user_data);
310 			bool ret = raw_ret;
311 			funcs = GLib.SourceFuncs.New (native_funcs);
312 			Marshal.FreeHGlobal (native_funcs);
313 			return ret;
314 		}
315 
316 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_remove_by_user_data(IntPtr user_data)317 		static extern bool g_source_remove_by_user_data(IntPtr user_data);
318 
RemoveByUserData(IntPtr user_data)319 		public static bool RemoveByUserData(IntPtr user_data) {
320 			bool raw_ret = g_source_remove_by_user_data(user_data);
321 			bool ret = raw_ret;
322 			return ret;
323 		}
324 
325 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_set_name_by_id(uint tag, IntPtr name)326 		static extern void g_source_set_name_by_id(uint tag, IntPtr name);
327 
SetNameById(uint tag, string name)328 		public static void SetNameById(uint tag, string name) {
329 			IntPtr native_name = GLib.Marshaller.StringToPtrGStrdup (name);
330 			g_source_set_name_by_id(tag, native_name);
331 			GLib.Marshaller.Free (native_name);
332 		}
333 
Source(IntPtr raw)334 		public Source(IntPtr raw) : base(raw) {}
335 
336 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_new(IntPtr source_funcs, uint struct_size)337 		static extern IntPtr g_source_new(IntPtr source_funcs, uint struct_size);
338 
Source(GLib.SourceFuncs source_funcs, uint struct_size)339 		public Source (GLib.SourceFuncs source_funcs, uint struct_size)
340 		{
341 			IntPtr native_source_funcs = GLib.Marshaller.StructureToPtrAlloc (source_funcs);
342 			Raw = g_source_new(native_source_funcs, struct_size);
343 			source_funcs = GLib.SourceFuncs.New (native_source_funcs);
344 			Marshal.FreeHGlobal (native_source_funcs);
345 		}
346 
347 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_ref(IntPtr raw)348 		static extern IntPtr g_source_ref(IntPtr raw);
349 
Ref(IntPtr raw)350 		protected override void Ref (IntPtr raw)
351 		{
352 			if (!Owned) {
353 				g_source_ref (raw);
354 				Owned = true;
355 			}
356 		}
357 
358 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_unref(IntPtr raw)359 		static extern void g_source_unref(IntPtr raw);
360 
Unref(IntPtr raw)361 		protected override void Unref (IntPtr raw)
362 		{
363 			if (Owned) {
364 				g_source_unref (raw);
365 				Owned = false;
366 			}
367 		}
368 
369 		[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
g_source_destroy(IntPtr raw)370 		static extern void g_source_destroy(IntPtr raw);
371 
Free(IntPtr raw)372 		protected override void Free (IntPtr raw)
373 		{
374 			g_source_destroy (raw);
375 		}
376 
377 		class FinalizerInfo {
378 			IntPtr handle;
379 
FinalizerInfo(IntPtr handle)380 			public FinalizerInfo (IntPtr handle)
381 			{
382 				this.handle = handle;
383 			}
384 
Handler()385 			public bool Handler ()
386 			{
387 				g_source_destroy (handle);
388 				return false;
389 			}
390 		}
391 
~Source()392 		~Source ()
393 		{
394 			if (!Owned)
395 				return;
396 			FinalizerInfo info = new FinalizerInfo (Handle);
397 			GLib.Timeout.Add (50, new GLib.TimeoutHandler (info.Handler));
398 		}
399 	}
400 
401 }