2009-09-16 06:38:59 +00:00
|
|
|
// Gst.GLib.ToggleRef.cs - Gst.GLib ToggleRef class implementation
|
2009-07-31 08:16:55 +00:00
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@novell.com>
|
|
|
|
//
|
|
|
|
// Copyright <c> 2007 Novell, Inc.
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the Lesser GNU General
|
|
|
|
// Public License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
|
2009-08-05 14:48:13 +00:00
|
|
|
namespace Gst.GLib {
|
2009-07-31 08:16:55 +00:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
internal class ToggleRef {
|
|
|
|
|
|
|
|
bool hardened;
|
|
|
|
IntPtr handle;
|
|
|
|
object reference;
|
|
|
|
GCHandle gch;
|
|
|
|
Hashtable signals;
|
|
|
|
|
2009-09-16 06:38:59 +00:00
|
|
|
public ToggleRef (Gst.GLib.Object target)
|
2009-07-31 08:16:55 +00:00
|
|
|
{
|
|
|
|
handle = target.Handle;
|
|
|
|
gch = GCHandle.Alloc (this);
|
|
|
|
reference = target;
|
|
|
|
g_object_add_toggle_ref (target.Handle, ToggleNotifyCallback, (IntPtr) gch);
|
|
|
|
g_object_unref (target.Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsAlive {
|
|
|
|
get {
|
|
|
|
if (reference is WeakReference) {
|
|
|
|
WeakReference weak = reference as WeakReference;
|
|
|
|
return weak.IsAlive;
|
|
|
|
} else if (reference == null)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IntPtr Handle {
|
|
|
|
get {
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Hashtable Signals {
|
|
|
|
get {
|
|
|
|
if (signals == null)
|
|
|
|
signals = new Hashtable ();
|
|
|
|
return signals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-16 06:38:59 +00:00
|
|
|
public Gst.GLib.Object Target {
|
2009-07-31 08:16:55 +00:00
|
|
|
get {
|
|
|
|
if (reference == null)
|
|
|
|
return null;
|
2009-09-16 06:38:59 +00:00
|
|
|
else if (reference is Gst.GLib.Object)
|
|
|
|
return reference as Gst.GLib.Object;
|
2009-07-31 08:16:55 +00:00
|
|
|
|
|
|
|
WeakReference weak = reference as WeakReference;
|
2009-09-16 06:38:59 +00:00
|
|
|
return weak.Target as Gst.GLib.Object;
|
2009-07-31 08:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Free ()
|
|
|
|
{
|
|
|
|
Signal[] signals = new Signal [Signals.Count];
|
|
|
|
Signals.Values.CopyTo (signals, 0);
|
|
|
|
foreach (Signal s in signals)
|
|
|
|
s.Free ();
|
|
|
|
if (hardened)
|
|
|
|
g_object_unref (handle);
|
|
|
|
else
|
|
|
|
g_object_remove_toggle_ref (handle, ToggleNotifyCallback, (IntPtr) gch);
|
|
|
|
reference = null;
|
|
|
|
gch.Free ();
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void Harden ()
|
|
|
|
{
|
|
|
|
// Added for the benefit of GnomeProgram. It releases a final ref in
|
|
|
|
// an atexit handler which causes toggle ref notifications to occur after
|
|
|
|
// our delegates are gone, so we need a mechanism to override the
|
|
|
|
// notifications. This method effectively leaks all objects which invoke it,
|
|
|
|
// but since it is only used by Gnome.Program, which is a singleton object
|
|
|
|
// with program duration persistence, who cares.
|
|
|
|
|
|
|
|
g_object_ref (handle);
|
|
|
|
g_object_remove_toggle_ref (handle, ToggleNotifyCallback, (IntPtr) gch);
|
|
|
|
if (reference is WeakReference)
|
|
|
|
reference = (reference as WeakReference).Target;
|
|
|
|
hardened = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Toggle (bool is_last_ref)
|
|
|
|
{
|
2009-09-16 06:38:59 +00:00
|
|
|
if (is_last_ref && reference is Gst.GLib.Object)
|
2009-07-31 08:16:55 +00:00
|
|
|
reference = new WeakReference (reference);
|
|
|
|
else if (!is_last_ref && reference is WeakReference) {
|
|
|
|
WeakReference weak = reference as WeakReference;
|
|
|
|
if (weak.IsAlive)
|
|
|
|
reference = weak.Target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-11 06:37:33 +00:00
|
|
|
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
|
2009-07-31 08:16:55 +00:00
|
|
|
delegate void ToggleNotifyHandler (IntPtr data, IntPtr handle, bool is_last_ref);
|
|
|
|
|
|
|
|
static void RefToggled (IntPtr data, IntPtr handle, bool is_last_ref)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
GCHandle gch = (GCHandle) data;
|
|
|
|
ToggleRef tref = gch.Target as ToggleRef;
|
|
|
|
tref.Toggle (is_last_ref);
|
|
|
|
} catch (Exception e) {
|
|
|
|
ExceptionManager.RaiseUnhandledException (e, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ToggleNotifyHandler toggle_notify_callback;
|
|
|
|
static ToggleNotifyHandler ToggleNotifyCallback {
|
|
|
|
get {
|
|
|
|
if (toggle_notify_callback == null)
|
|
|
|
toggle_notify_callback = new ToggleNotifyHandler (RefToggled);
|
|
|
|
return toggle_notify_callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-11 06:37:33 +00:00
|
|
|
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2009-07-31 08:16:55 +00:00
|
|
|
static extern void g_object_add_toggle_ref (IntPtr raw, ToggleNotifyHandler notify_cb, IntPtr data);
|
|
|
|
|
2009-09-11 06:37:33 +00:00
|
|
|
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2009-07-31 08:16:55 +00:00
|
|
|
static extern void g_object_remove_toggle_ref (IntPtr raw, ToggleNotifyHandler notify_cb, IntPtr data);
|
|
|
|
|
2009-09-11 06:37:33 +00:00
|
|
|
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2009-07-31 08:16:55 +00:00
|
|
|
static extern IntPtr g_object_ref (IntPtr raw);
|
|
|
|
|
2009-09-11 06:37:33 +00:00
|
|
|
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2009-07-31 08:16:55 +00:00
|
|
|
static extern void g_object_unref (IntPtr raw);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|