1 // GLib.Idle.cs - Idle class implementation 2 // 3 // Author: Mike Kestner <mkestner@speakeasy.net> 4 // Rachel Hestilow <hestilow@ximian.com> 5 // 6 // Copyright (c) 2002 Mike Kestner 7 // Copyright (c) Rachel Hestilow 8 // 9 // This program is free software; you can redistribute it and/or 10 // modify it under the terms of version 2 of the Lesser GNU General 11 // Public License as published by the Free Software Foundation. 12 // 13 // This program is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 // Lesser General Public License for more details. 17 // 18 // You should have received a copy of the GNU Lesser General Public 19 // License along with this program; if not, write to the 20 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 21 // Boston, MA 02111-1307, USA. 22 23 24 namespace GLib { 25 26 using System; 27 using System.Collections; 28 using System.Collections.Generic; 29 using System.Runtime.InteropServices; 30 IdleHandler()31 public delegate bool IdleHandler (); 32 33 public class Idle { 34 35 internal class IdleProxy : SourceProxy { 36 internal readonly IdleHandler real_handler; IdleProxy(IdleHandler real)37 public IdleProxy (IdleHandler real) 38 { 39 real_handler = real; 40 } 41 Invoke(IntPtr data)42 protected override bool Invoke (IntPtr data) 43 { 44 return real_handler (); 45 } 46 } 47 Idle()48 private Idle () 49 { 50 } 51 52 [DllImport("libglib-2.0-0.dll", CallingConvention=CallingConvention.Cdecl)] g_idle_add(SourceProxy.GSourceFuncInternal d, IntPtr data)53 static extern uint g_idle_add (SourceProxy.GSourceFuncInternal d, IntPtr data); 54 Add(IdleHandler hndlr)55 public static uint Add (IdleHandler hndlr) 56 { 57 IdleProxy p = new IdleProxy (hndlr); 58 p.ID = g_idle_add (p.Handler, IntPtr.Zero); 59 Source.Add (p); 60 61 return p.ID; 62 } 63 64 [DllImport("libglib-2.0-0.dll", CallingConvention=CallingConvention.Cdecl)] g_source_remove(uint id)65 static extern bool g_source_remove (uint id); 66 Remove(IdleHandler hndlr)67 public static bool Remove (IdleHandler hndlr) 68 { 69 bool result = false; 70 71 lock (Source.source_handlers) { 72 foreach (KeyValuePair<uint, SourceProxy> kvp in Source.source_handlers) { 73 uint code = kvp.Key; 74 IdleProxy p = kvp.Value as IdleProxy; 75 76 if (p != null && p.real_handler == hndlr) { 77 result = g_source_remove (code); 78 p.Remove (); 79 } 80 } 81 } 82 83 return result; 84 } 85 } 86 } 87 88