3 * @brief Common LibVLC objects marshalling utilities
7 /**********************************************************************
8 * Copyright (C) 2007-2009 RĂ©mi Denis-Courmont. *
9 * This program is free software; you can redistribute and/or modify *
10 * it under the terms of the GNU General Public License as published *
11 * by the Free Software Foundation; version 2 of the license, or (at *
12 * your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17 * See the GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, you can get it from: *
21 * http://www.gnu.org/copyleft/gpl.html *
22 **********************************************************************/
25 using System.Collections;
26 using System.Collections.Generic;
27 using System.Runtime.InteropServices;
29 namespace VideoLAN.LibVLC
32 * @brief NonNullHandle: abstract safe handle class for non-NULL pointers
34 * Microsoft.* namespace has a similar class. However we want to use the
35 * System.* namespace only.
37 internal abstract class NonNullHandle : SafeHandle
39 protected NonNullHandle ()
40 : base (IntPtr.Zero, true)
45 * System.Runtime.InteropServices.SafeHandle::IsInvalid.
47 public override bool IsInvalid
51 return handle == IntPtr.Zero;
56 * Destroys an handle. Cannot fail.
58 protected abstract void Destroy ();
61 * System.Runtime.InteropServices.SafeHandle::ReleaseHandle.
63 protected override bool ReleaseHandle ()
71 * @brief BaseObject: generic wrapper around a safe LibVLC handle.
74 * This is the baseline for all managed LibVLC objects. It wraps:
75 * - an unmanaged LibVLC pointer,
76 * - a native exception structure.
78 public class BaseObject : IDisposable
80 internal NativeException ex; /**< buffer for LibVLC exceptions */
81 internal SafeHandle handle; /**< wrapped safe handle */
83 internal BaseObject ()
85 ex = new NativeException ();
90 * Checks if the LibVLC run-time raised an exception
91 * If so, raises a CIL exception.
93 internal void Raise ()
99 * IDisposable::Dispose.
101 public void Dispose ()
104 GC.SuppressFinalize (this);
108 * Releases unmanaged resources associated with the object.
109 * @param disposing true if the disposing the object explicitly,
110 * false if finalizing the object inside the GC.
112 protected virtual void Dispose (bool disposing)
125 internal class EventManagerHandle : NonNullHandle
127 protected override void Destroy ()
134 * @brief EventingObject: wrapper around an eventing LibVLC handle.
137 * This is the base class for all managed LibVLC objects which do have an
140 public abstract class EventingObject : BaseObject
142 private Dictionary<Delegate, IntPtr> events;
143 /**< references to our unmanaged function pointers */
145 internal EventingObject () : base ()
147 events = new Dictionary<Delegate, IntPtr> ();
151 * Releases unmanaged resources associated with the object.
152 * @param disposing true if the disposing the object explicitly,
153 * false if finalizing the object inside the GC.
155 protected override void Dispose (bool disposing)
158 base.Dispose (disposing);
162 * @return the unmanaged event manager for this object
164 internal abstract EventManagerHandle GetManager ();
167 * Registers an event handler.
168 * @param type event type to register to
169 * @param callback callback to invoke when the event occurs
172 * For simplicity, we require distinct callbacks for each event type.
173 * This is hardly an issue since most events have different formats.
175 internal void Attach (EventType type, Delegate callback)
177 EventManagerHandle manager;
178 IntPtr cb = Marshal.GetFunctionPointerForDelegate (callback);
181 /* If things go wrong, we will leak the callback thunk... until
182 * this object is destroyed anyway. If we added the thunk _after_
183 * the critical section, the native code could try to jump to a
184 * non-existent address, which is much worse. */
185 events.Add (callback, cb);
188 handle.DangerousAddRef (ref unref);
189 manager = GetManager ();
190 LibVLC.EventAttach (manager, type, cb, IntPtr.Zero, ex);
195 handle.DangerousRelease ();
200 internal void Detach (EventType type, Delegate callback)
202 EventManagerHandle manager;
203 IntPtr cb = events[callback];
208 handle.DangerousAddRef (ref unref);
209 manager = GetManager ();
210 LibVLC.EventDetach (manager, type, cb, IntPtr.Zero, ex);
215 handle.DangerousRelease ();
218 events.Remove (callback);