2 * libvlc.cs - libvlc-control CIL bindings
7 /**********************************************************************
8 * Copyright (C) 2007 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.Runtime.InteropServices;
27 namespace VideoLAN.VLC
29 internal class MediaControlAPI
31 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_new")]
32 public static extern MediaControlHandle New (int argc, U8String[] argv, ref NativeException e);
33 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_exit")]
34 public static extern void Exit (IntPtr self);
36 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_start")]
37 public static extern void Start (MediaControlHandle self, IntPtr pos, ref NativeException e);
38 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_pause")]
39 public static extern void Pause (MediaControlHandle self, IntPtr dummy, ref NativeException e);
40 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_resume")]
41 public static extern void Resume (MediaControlHandle self, IntPtr dummy, ref NativeException e);
42 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_stop")]
43 public static extern void Stop (MediaControlHandle self, IntPtr dummy, ref NativeException e);
45 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_add_item")]
46 public static extern void PlaylistAddItem (MediaControlHandle self, U8String mrl, ref NativeException e);
47 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_clear")]
48 public static extern void PlaylistClear (MediaControlHandle self, ref NativeException e);
49 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_get_list")]
50 public static extern IntPtr PlaylistGetList (MediaControlHandle self, ref NativeException e);
51 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_next_item")]
52 public static extern void PlaylistNextItem (MediaControlHandle self, ref NativeException e);
56 * Abstract safe handle class for non-NULL pointers
57 * (Microsoft.* namespace has a similar class, but lets stick to System.*)
59 internal abstract class NonNullHandle : SafeHandle
61 protected NonNullHandle ()
62 : base (IntPtr.Zero, true)
66 public override bool IsInvalid
70 return IsClosed || (handle == IntPtr.Zero);
75 internal sealed class MediaControlHandle : NonNullHandle
77 private MediaControlHandle ()
81 protected override bool ReleaseHandle ()
83 MediaControlAPI.Exit (handle);
89 * Wrapper around native UTF-8 nul-terminated character arrays
91 [StructLayout (LayoutKind.Sequential)]
92 internal sealed struct U8String
96 public U8String (string value)
98 byte[] bytes = System.Text.Encoding.UTF8.GetBytes (value);
99 mb_str = new byte[bytes.Length + 1];
100 Array.Copy (bytes, mb_str, bytes.Length);
101 mb_str[bytes.Length] = 0;
104 public U8String (IntPtr ptr)
106 if (ptr == IntPtr.Zero)
110 while (Marshal.ReadByte (ptr, i) != 0)
114 mb_str = new byte[i];
115 Marshal.Copy (ptr, mb_str, 0, i);
118 public override string ToString ()
123 byte[] bytes = new byte[mb_str.Length - 1];
124 Array.Copy (mb_str, bytes, bytes.Length);
126 return System.Text.Encoding.UTF8.GetString (bytes);
132 * LibVLC native exception structure
134 [StructLayout (LayoutKind.Sequential)]
135 internal sealed struct NativeException
138 public IntPtr message;
140 public string Message
144 return new U8String (message).ToString ();
148 [DllImport ("libvlc-control.dll")]
149 static extern void mediacontrol_exception_init (ref NativeException e);
150 [DllImport ("libvlc-control.dll")]
151 static extern void mediacontrol_exception_cleanup (ref NativeException e);
153 public static NativeException Prepare ()
155 NativeException e = new NativeException ();
156 mediacontrol_exception_init (ref e);
160 public void Consume ()
173 e = new PositionKeyNotSupportedException (m);
176 e = new PositionOriginNotSupportedException (m);
179 e = new InvalidPositionException (m);
182 e = new PlaylistException (m);
185 e = new InternalException (m);
188 e = new MediaException (m);
196 mediacontrol_exception_cleanup (ref this);