Skip to content

Commit

Permalink
GTK# implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyVignelles committed Dec 1, 2018
1 parent 9cf2464 commit f55b986
Show file tree
Hide file tree
Showing 19 changed files with 835 additions and 35 deletions.
5 changes: 4 additions & 1 deletion Directory.Build.props
Expand Up @@ -6,7 +6,10 @@
<DefineConstants>$(DefineConstants);NETSTANDARD1_1;NETSTANDARD</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net40'">
<DefineConstants>$(DefineConstants);NET40</DefineConstants>
<DefineConstants>$(DefineConstants);NET40;NET</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net471'">
<DefineConstants>$(DefineConstants);NET471;NET</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0.16299'">
<DefineConstants>$(DefineConstants);NETFX_CORE;XAML;WINDOWS_UWP;WINDOWS</DefineConstants>
Expand Down
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms.Platform.GTK" Version="3.2.0.871581" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibVLCSharp.Forms\LibVLCSharp.Forms.csproj" />
<ProjectReference Include="..\LibVLCSharp.GTK\LibVLCSharp.GTK.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="gdk-sharp">
<SpecificVersion>False</SpecificVersion>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Windows'))">C:\Program Files (x86)\GtkSharp\2.12\lib\gtk-sharp-2.0\gdk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Linux'))">/usr/lib/cli/gdk-sharp-2.0/gdk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('OSX'))">/Library/Frameworks/Mono.framework//Versions/5.12.0/lib/mono/gtk-sharp-2.0/gdk-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="glib-sharp">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Windows'))">C:\Program Files (x86)\GtkSharp\2.12\lib\gtk-sharp-2.0\glib-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Linux'))">/usr/lib/cli/glib-sharp-2.0/glib-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('OSX'))">/Library/Frameworks/Mono.framework//Versions/5.12.0/lib/mono/gtk-sharp-2.0/glib-sharp.dll</HintPath>
</Reference>
<Reference Include="gtk-sharp">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Windows'))">C:\Program Files (x86)\GtkSharp\2.12\lib\gtk-sharp-2.0\gtk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Linux'))">/usr/lib/cli/gtk-sharp-2.0/gtk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('OSX'))">/Library/Frameworks/Mono.framework//Versions/5.12.0/lib/mono/gtk-sharp-2.0/gtk-sharp.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions LibVLCSharp.Forms.Platforms.GTK/VideoViewRenderer.cs
@@ -0,0 +1,51 @@
using LibVLCSharp.Forms.Platforms.GTK;
using LibVLCSharp.Forms.Shared;
using LibVLCSharp.Shared;
using Xamarin.Forms;
using Xamarin.Forms.Platform.GTK;

[assembly: ExportRenderer(typeof(LibVLCSharp.Forms.Shared.VideoView), typeof(VideoViewRenderer))]
namespace LibVLCSharp.Forms.Platforms.GTK
{
public class VideoViewRenderer : ViewRenderer<LibVLCSharp.Forms.Shared.VideoView, LibVLCSharp.GTK.VideoView>
{
LibVLCSharp.GTK.VideoView _videoView;

protected override void OnElementChanged(ElementChangedEventArgs<VideoView> e)
{
base.OnElementChanged(e);

if (Control == null)
{
_videoView = new LibVLCSharp.GTK.VideoView();
SetNativeControl(_videoView);
}

if (e.OldElement != null)
{
e.OldElement.MediaPlayerChanged -= OnMediaPlayerChanged;
}

if (e.NewElement != null)
{
e.NewElement.MediaPlayerChanged += OnMediaPlayerChanged;
}
}

private void OnMediaPlayerChanged(object sender, MediaPlayerChangedEventArgs e)
{
if (Control == null)
{
return;
}
Control.MediaPlayer = e.NewMediaPlayer;
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

_videoView?.Dispose();
}
}
}
33 changes: 33 additions & 0 deletions LibVLCSharp.GTK/LibVLCSharp.GTK.csproj
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Reference Include="gdk-sharp">
<SpecificVersion>False</SpecificVersion>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Windows'))">C:\Program Files (x86)\GtkSharp\2.12\lib\gtk-sharp-2.0\gdk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Linux'))">/usr/lib/cli/gdk-sharp-2.0/gdk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('OSX'))">/Library/Frameworks/Mono.framework//Versions/5.12.0/lib/mono/gtk-sharp-2.0/gdk-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="glib-sharp">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Windows'))">C:\Program Files (x86)\GtkSharp\2.12\lib\gtk-sharp-2.0\glib-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Linux'))">/usr/lib/cli/glib-sharp-2.0/glib-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('OSX'))">/Library/Frameworks/Mono.framework//Versions/5.12.0/lib/mono/gtk-sharp-2.0/glib-sharp.dll</HintPath>
</Reference>
<Reference Include="gtk-sharp">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Windows'))">C:\Program Files (x86)\GtkSharp\2.12\lib\gtk-sharp-2.0\gtk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('Linux'))">/usr/lib/cli/gtk-sharp-2.0/gtk-sharp.dll</HintPath>
<HintPath Condition="$([MSBuild]::IsOsPlatform('OSX'))">/Library/Frameworks/Mono.framework//Versions/5.12.0/lib/mono/gtk-sharp-2.0/gtk-sharp.dll</HintPath>
</Reference>
<ProjectReference Include="..\LibVLCSharp\LibVLCSharp.csproj" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
</ItemGroup>

</Project>
122 changes: 122 additions & 0 deletions LibVLCSharp.GTK/VideoView.cs
@@ -0,0 +1,122 @@
using System;
using System.Runtime.InteropServices;
using Gdk;
using Gtk;
using LibVLCSharp.Shared;

namespace LibVLCSharp.GTK
{
public class VideoView : DrawingArea, IVideoView
{
struct Native
{
/// <summary>
/// Gets the window's HWND
/// </summary>
/// <remarks>Window only</remarks>
/// <param name="gdkWindow">The pointer to the GdkWindow object</param>
/// <returns>The window's HWND</returns>
[DllImport("libgdk-win32-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr gdk_win32_drawable_get_handle(IntPtr gdkWindow);

/// <summary>
/// Gets the window's XID
/// </summary>
/// <remarks>Linux X11 only</remarks>
/// <param name="gdkWindow">The pointer to the GdkWindow object</param>
/// <returns>The window's XID</returns>
[DllImport("libgdk-x11-2.0.so.0", CallingConvention = CallingConvention.Cdecl)]
internal static extern uint gdk_x11_drawable_get_xid(IntPtr gdkWindow);

[DllImport("libgdk-quartz-2.0.0.dylib")]
internal static extern IntPtr gdk_quartz_window_get_nsview(IntPtr handle);
}

private MediaPlayer _mediaPlayer;

public VideoView(MediaPlayer mediaPlayer = null)
{
_mediaPlayer = mediaPlayer;
Color black = Color.Zero;
Color.Parse("black", ref black);
ModifyBg(StateType.Normal, black);

Realized += (s, e) => Attach();
}

public MediaPlayer MediaPlayer
{
get
{
return _mediaPlayer;
}
set
{
if (ReferenceEquals(_mediaPlayer, value))
{
return;
}

Detach();
_mediaPlayer = value;
Attach();
}
}

void Attach()
{
if (!IsRealized || _mediaPlayer == null)
{
return;
}

if (PlatformHelper.IsWindows)
{
MediaPlayer.Hwnd = Native.gdk_win32_drawable_get_handle(GdkWindow.Handle);
}
else if (PlatformHelper.IsLinux)
{
MediaPlayer.XWindow = Native.gdk_x11_drawable_get_xid(GdkWindow.Handle);
}
else if (PlatformHelper.IsMac)
{
MediaPlayer.NsObject = Native.gdk_quartz_window_get_nsview(GdkWindow.Handle);
}
else
{
throw new PlatformNotSupportedException();
}
}

void Detach()
{
if (!IsRealized || _mediaPlayer == null)
{
return;
}

if (PlatformHelper.IsWindows)
{
MediaPlayer.Hwnd = IntPtr.Zero;
}
else if (PlatformHelper.IsLinux)
{
MediaPlayer.XWindow = 0;
}
else if (PlatformHelper.IsMac)
{
MediaPlayer.NsObject = IntPtr.Zero;
}
else
{
throw new PlatformNotSupportedException();
}
}

public override void Dispose()
{
Detach();
base.Dispose();
}
}
}

0 comments on commit f55b986

Please sign in to comment.