Made the Win10 share work again, work in progress

This commit is contained in:
Robin Krom 2020-02-06 22:28:33 +01:00
parent ecad3856cf
commit 19b6576311
10 changed files with 619 additions and 178 deletions

View file

@ -11,8 +11,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.Contracts" version="10.0.18362.2005" /> <PackageReference Include="Microsoft.Windows.SDK.Contracts" version="10.0.18362.2005" />
<PackageReference Include="System.Reactive" Version="4.3.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\GreenshotPlugin\GreenshotPlugin.csproj" /> <ProjectReference Include="..\GreenshotPlugin\GreenshotPlugin.csproj" />

View file

@ -1,66 +1,106 @@
using System.IO; /*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.IO;
using Windows.Storage.Streams; using Windows.Storage.Streams;
namespace GreenshotWin10Plugin namespace GreenshotWin10Plugin
{ {
/// <summary>
/// This is an IRandomAccessStream implementation which uses a MemoryStream
/// </summary>
public sealed class MemoryRandomAccessStream : MemoryStream, IRandomAccessStream public sealed class MemoryRandomAccessStream : MemoryStream, IRandomAccessStream
{ {
/// <summary>
/// Default constructor
/// </summary>
public MemoryRandomAccessStream() public MemoryRandomAccessStream()
{ {
} }
/// <summary>
/// Constructor where also bytes are already passed
/// </summary>
/// <param name="bytes">byte array</param>
public MemoryRandomAccessStream(byte[] bytes) public MemoryRandomAccessStream(byte[] bytes)
{ {
Write(bytes, 0, bytes.Length); Write(bytes, 0, bytes.Length);
} }
public IInputStream GetInputStreamAt(ulong position) /// <inheritdoc />
public IInputStream GetInputStreamAt(ulong position)
{ {
Seek((long)position, SeekOrigin.Begin); Seek((long)position, SeekOrigin.Begin);
return this.AsInputStream(); return this.AsInputStream();
} }
public IOutputStream GetOutputStreamAt(ulong position) /// <inheritdoc />
public IOutputStream GetOutputStreamAt(ulong position)
{ {
Seek((long)position, SeekOrigin.Begin); Seek((long)position, SeekOrigin.Begin);
return this.AsOutputStream(); return this.AsOutputStream();
} }
ulong IRandomAccessStream.Position => (ulong)Position; /// <inheritdoc />
ulong IRandomAccessStream.Position => (ulong)Position;
public ulong Size /// <inheritdoc />
public ulong Size
{ {
get { return (ulong)Length; } get { return (ulong)Length; }
set { SetLength((long)value); } set { SetLength((long)value); }
} }
public IRandomAccessStream CloneStream() /// <inheritdoc />
public IRandomAccessStream CloneStream()
{ {
var cloned = new MemoryRandomAccessStream(); var cloned = new MemoryRandomAccessStream();
CopyTo(cloned); CopyTo(cloned);
return cloned; return cloned;
} }
public void Seek(ulong position) /// <inheritdoc />
public void Seek(ulong position)
{ {
Seek((long)position, SeekOrigin.Begin); Seek((long)position, SeekOrigin.Begin);
} }
public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) /// <inheritdoc />
public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
{ {
var inputStream = GetInputStreamAt(0); var inputStream = GetInputStreamAt(0);
return inputStream.ReadAsync(buffer, count, options); return inputStream.ReadAsync(buffer, count, options);
} }
Windows.Foundation.IAsyncOperation<bool> IOutputStream.FlushAsync() /// <inheritdoc />
Windows.Foundation.IAsyncOperation<bool> IOutputStream.FlushAsync()
{ {
var outputStream = GetOutputStreamAt(0); var outputStream = GetOutputStreamAt(0);
return outputStream.FlushAsync(); return outputStream.FlushAsync();
} }
public Windows.Foundation.IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) /// <inheritdoc />
public Windows.Foundation.IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)
{ {
var outputStream = GetOutputStreamAt(0); var outputStream = GetOutputStreamAt(0);
return outputStream.WriteAsync(buffer); return outputStream.WriteAsync(buffer);

View file

@ -1,20 +1,20 @@
/* /*
* Greenshot - a free and open source screenshot tool * Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom * Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
* *
* For more information see: http://getgreenshot.org/ * For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or * the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -36,12 +36,18 @@ namespace GreenshotWin10Plugin.Native
private readonly IDataTransferManagerInterOp _dataTransferManagerInterOp; private readonly IDataTransferManagerInterOp _dataTransferManagerInterOp;
private readonly IntPtr _windowHandle; private readonly IntPtr _windowHandle;
/// <summary>
/// The DataTransferManager
/// </summary>
public DataTransferManager DataTransferManager public DataTransferManager DataTransferManager
{ {
get; get;
private set; private set;
} }
/// <summary>
/// Constructor which takes a handle to initialize
/// </summary>
/// <param name="handle"></param>
public DataTransferManagerHelper(IntPtr handle) public DataTransferManagerHelper(IntPtr handle)
{ {
//TODO: Add a check for failure here. This will fail for versions of Windows below Windows 10 //TODO: Add a check for failure here. This will fail for versions of Windows below Windows 10
@ -52,8 +58,8 @@ namespace GreenshotWin10Plugin.Native
_windowHandle = handle; _windowHandle = handle;
var riid = new Guid(DataTransferManagerId); var riid = new Guid(DataTransferManagerId);
var hresult = _dataTransferManagerInterOp.GetForWindow(_windowHandle, riid, out var dataTransferManager); var hresult = _dataTransferManagerInterOp.GetForWindow(_windowHandle, riid, out var dataTransferManager);
if (hresult != 0) if (hresult.Failed())
{ {
Log.WarnFormat("HResult for GetForWindow: {0}", hresult); Log.WarnFormat("HResult for GetForWindow: {0}", hresult);
} }
@ -66,9 +72,9 @@ namespace GreenshotWin10Plugin.Native
public void ShowShareUi() public void ShowShareUi()
{ {
var hresult = _dataTransferManagerInterOp.ShowShareUIForWindow(_windowHandle); var hresult = _dataTransferManagerInterOp.ShowShareUIForWindow(_windowHandle);
if (hresult != 0) if (hresult.Failed())
{ {
Log.WarnFormat("HResult for ShowShareUIForWindow: {0}", hresult); Log.WarnFormat("HResult for ShowShareUIForWindow: {0}", hresult);
} }
else else
{ {

View file

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GreenshotWin10Plugin.Native
{
/// <summary>
/// The HRESULT represents Windows error codes
/// See <a href="https://en.wikipedia.org/wiki/HRESULT">wikipedia</a>
/// </summary>
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum HResult
{
#pragma warning disable 1591
S_OK = 0,
S_FALSE = 1,
E_FAIL = unchecked((int)0x80004005),
E_INVALIDARG = unchecked((int)0x80070057),
E_NOTIMPL = unchecked((int)0x80004001),
E_POINTER = unchecked((int)0x80004003),
E_PENDING = unchecked((int)0x8000000A),
E_NOINTERFACE = unchecked((int)0x80004002),
E_ABORT = unchecked((int)0x80004004),
E_ACCESSDENIED = unchecked((int)0x80070006),
E_HANDLE = unchecked((int)0x80070006),
E_UNEXPECTED = unchecked((int)0x8000FFFF),
E_FILENOTFOUND = unchecked((int)0x80070002),
E_PATHNOTFOUND = unchecked((int)0x80070003),
E_INVALID_DATA = unchecked((int)0x8007000D),
E_OUTOFMEMORY = unchecked((int)0x8007000E),
E_INSUFFICIENT_BUFFER = unchecked((int)0x8007007A),
WSAECONNABORTED = unchecked((int)0x80072745),
WSAECONNRESET = unchecked((int)0x80072746),
ERROR_TOO_MANY_CMDS = unchecked((int)0x80070038),
ERROR_NOT_SUPPORTED = unchecked((int)0x80070032),
TYPE_E_ELEMENTNOTFOUND = unchecked((int)0x8002802B)
#pragma warning restore 1591
}
}

View file

@ -0,0 +1,64 @@
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 1 of the License, or
// (at your option) any later version.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
namespace GreenshotWin10Plugin.Native
{
/// <summary>
/// Extensions to handle the HResult
/// </summary>
public static class HResultExtensions
{
/// <summary>
/// Test if the HResult respresents a fail
/// </summary>
/// <param name="hResult">HResult</param>
/// <returns>bool</returns>
[Pure]
public static bool Failed(this HResult hResult)
{
return hResult < 0;
}
/// <summary>
/// Test if the HResult respresents a success
/// </summary>
/// <param name="hResult">HResult</param>
/// <returns>bool</returns>
[Pure]
public static bool Succeeded(this HResult hResult)
{
return hResult >= HResult.S_OK;
}
/// <summary>
/// Throw an exception on Failure
/// </summary>
/// <param name="hResult">HResult</param>
public static void ThrowOnFailure(this HResult hResult)
{
if (Failed(hResult))
{
throw Marshal.GetExceptionForHR((int)hResult);
}
}
}
}

View file

@ -1,23 +1,21 @@
/* // Greenshot - a free and open source screenshot tool
* Greenshot - a free and open source screenshot tool // Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom //
* // For more information see: http://getgreenshot.org/
* For more information see: http://getgreenshot.org/ // The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ //
* // This program is free software: you can redistribute it and/or modify
* This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by
* it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 1 of the License, or
* the Free Software Foundation, either version 1 of the License, or // (at your option) any later version.
* (at your option) any later version. //
* // This program is distributed in the hope that it will be useful,
* This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of
* but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details.
* GNU General Public License for more details. //
* // You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -43,7 +41,7 @@ namespace GreenshotWin10Plugin.Native
/// <param name="pDataTransferManager">The DataTransferManager instance for this window handle</param> /// <param name="pDataTransferManager">The DataTransferManager instance for this window handle</param>
/// <returns>HRESULT</returns> /// <returns>HRESULT</returns>
[PreserveSig] [PreserveSig]
uint GetForWindow([In] IntPtr appWindow, [In] ref Guid riid, [Out] out DataTransferManager pDataTransferManager); HResult GetForWindow([In] IntPtr appWindow, [In] ref Guid riid, [Out] out DataTransferManager pDataTransferManager);
/// <summary> /// <summary>
/// Show the share flyout for the window identified by a window handle /// Show the share flyout for the window identified by a window handle
@ -51,7 +49,7 @@ namespace GreenshotWin10Plugin.Native
/// <param name="appWindow">The window handle</param> /// <param name="appWindow">The window handle</param>
/// <returns>HRESULT</returns> /// <returns>HRESULT</returns>
[PreserveSig] [PreserveSig]
uint ShowShareUIForWindow(IntPtr appWindow); HResult ShowShareUIForWindow(IntPtr appWindow);
} }
} }

View file

@ -0,0 +1,70 @@
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 1 of the License, or
// (at your option) any later version.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using GreenshotPlugin.UnmanagedHelpers;
using System;
namespace Greenshot.Addon.Win10.Native
{
/// <summary>
/// Container for the windows messages
/// </summary>
public class WindowMessageInfo
{
/// <summary>
/// IntPtr with the Handle of the window
/// </summary>
public IntPtr Handle { get; private set; }
/// <summary>
/// WindowsMessages which is the actual message
/// </summary>
public WindowsMessages Message { get; private set; }
/// <summary>
/// IntPtr with the word-param
/// </summary>
public IntPtr WordParam { get; private set; }
/// <summary>
/// IntPtr with the long-param
/// </summary>
public IntPtr LongParam { get; private set; }
/// <summary>
/// Factory method for the Window Message Info
/// </summary>
/// <param name="hwnd">IntPtr with the Handle of the window</param>
/// <param name="msg">WindowsMessages which is the actual message</param>
/// <param name="wParam">IntPtr with the word-param</param>
/// <param name="lParam">IntPtr with the long-param</param>
/// <param name="handled"></param>
/// <returns>WindowMessageInfo</returns>
public static WindowMessageInfo Create(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
return new WindowMessageInfo
{
Handle = hwnd,
Message = (WindowsMessages)msg,
WordParam = wParam,
LongParam = lParam
};
}
}
}

View file

@ -0,0 +1,137 @@
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 1 of the License, or
// (at your option) any later version.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Interop;
namespace Greenshot.Addon.Win10.Native
{
/// <summary>
/// A monitor for window messages
/// </summary>
public static class WindowMessageMonitor
{
/// <summary>
/// Create a HwndSource for the specified Window
/// </summary>
/// <param name="window">Window</param>
/// <returns>HwndSource</returns>
private static HwndSource ToHwndSource(this Window window)
{
IntPtr windowHandle = new WindowInteropHelper(window).Handle;
if (windowHandle == IntPtr.Zero)
{
return null;
}
return HwndSource.FromHwnd(windowHandle);
}
/// <summary>
/// Create an observable for the specified window
/// </summary>
public static IObservable<WindowMessageInfo> WindowMessages(this Window window)
{
return WindowMessages(window, null);
}
/// <summary>
/// Create an observable for the specified HwndSource
/// </summary>
public static IObservable<WindowMessageInfo> WindowMessages(this HwndSource hwndSource)
{
return WindowMessages(null, hwndSource);
}
/// <summary>
/// Create an observable for the specified window or HwndSource
/// </summary>
/// <param name="window">Window</param>
/// <param name="hwndSource">HwndSource</param>
/// <returns>IObservable</returns>
private static IObservable<WindowMessageInfo> WindowMessages(Window window, HwndSource hwndSource)
{
if (window == null && hwndSource == null)
{
throw new NotSupportedException("One of Window or HwndSource must be supplied");
}
if (window != null && hwndSource != null)
{
throw new NotSupportedException("Either Window or HwndSource must be supplied");
}
return Observable.Create<WindowMessageInfo>(observer =>
{
// This handles the message, and generates the observable OnNext
IntPtr WindowMessageHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
observer.OnNext(WindowMessageInfo.Create(hwnd, msg, wParam, lParam, ref handled));
// ReSharper disable once AccessToDisposedClosure
if (hwndSource.IsDisposed)
{
observer.OnCompleted();
}
return IntPtr.Zero;
}
void HwndSourceDisposedHandle(object sender, EventArgs e)
{
observer.OnCompleted();
}
void RegisterHwndSource()
{
hwndSource.Disposed += HwndSourceDisposedHandle;
hwndSource.AddHook(WindowMessageHandler);
}
if (window != null)
{
hwndSource = window.ToHwndSource();
}
if (hwndSource != null)
{
RegisterHwndSource();
}
else
{
// No, try to get it later
window.SourceInitialized += (sender, args) =>
{
hwndSource = window.ToHwndSource();
RegisterHwndSource();
};
}
return Disposable.Create(() =>
{
hwndSource.Disposed -= HwndSourceDisposedHandle;
hwndSource.RemoveHook(WindowMessageHandler);
hwndSource.Dispose();
});
})
// Make sure there is always a value produced when connecting
.Publish()
.RefCount();
}
}
}

View file

@ -1,20 +1,20 @@
/* /*
* Greenshot - a free and open source screenshot tool * Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
* *
* For more information see: http://getgreenshot.org/ * For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or * the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -31,7 +31,6 @@ namespace GreenshotWin10Plugin
/// </summary> /// </summary>
public class Win10Plugin : IGreenshotPlugin public class Win10Plugin : IGreenshotPlugin
{ {
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
@ -67,7 +66,7 @@ namespace GreenshotWin10Plugin
{ {
yield break; yield break;
} }
/// <summary> /// <summary>
/// Implementation of the IGreenshotPlugin.Initialize /// Implementation of the IGreenshotPlugin.Initialize
/// </summary> /// </summary>

View file

@ -1,178 +1,261 @@
/* /*
* Greenshot - a free and open source screenshot tool * Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
* *
* For more information see: http://getgreenshot.org/ * For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or * the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Streams; using Windows.Storage.Streams;
using Greenshot.Addon.Win10.Native;
using Color = Windows.UI.Color;
using System.Reactive.Linq;
using GreenshotPlugin.UnmanagedHelpers;
using Greenshot.Plugin; using Greenshot.Plugin;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
using GreenshotWin10Plugin.Native;
using System.Threading.Tasks;
using Windows.Storage;
using Color = Windows.UI.Color;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using GreenshotWin10Plugin.Native;
namespace GreenshotWin10Plugin namespace GreenshotWin10Plugin
{ {
/// <summary> /// <summary>
/// This uses the Share from Windows 10 to make the capture available to apps. /// This uses the Share from Windows 10 to make the capture available to apps.
/// </summary> /// </summary>
public class Win10ShareDestination : AbstractDestination public class Win10ShareDestination : AbstractDestination
{ {
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Win10ShareDestination)); private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Win10ShareDestination));
public override string Designation { get; } = "WIN10Share"; public override string Designation { get; } = "WIN10Share";
public override string Description { get; } = "Windows 10 share"; public override string Description { get; } = "Windows 10 share";
/// <summary> /// <summary>
/// Icon for the App-share, the icon was found via: http://help4windows.com/windows_8_shell32_dll.shtml /// Icon for the App-share, the icon was found via: http://help4windows.com/windows_8_shell32_dll.shtml
/// </summary> /// </summary>
public override Image DisplayIcon => PluginUtils.GetCachedExeIcon(FilenameHelper.FillCmdVariables(@"%windir%\system32\shell32.dll"), 238); public override Image DisplayIcon => PluginUtils.GetCachedExeIcon(FilenameHelper.FillCmdVariables(@"%windir%\system32\shell32.dll"), 238);
/// <summary> private class ShareInfo
/// Share the screenshot with a windows app {
/// </summary> public string ApplicationName { get; set; }
/// <param name="manuallyInitiated"></param> public bool AreShareProvidersRequested { get; set; }
/// <param name="surface"></param> public bool IsDeferredFileCreated { get; set; }
/// <param name="captureDetails"></param> public DataPackageOperation CompletedWithOperation { get; set; }
/// <returns>ExportInformation</returns> public string AcceptedFormat { get; set; }
public bool IsDestroyed { get; set; }
public bool IsShareCompleted { get; set; }
public TaskCompletionSource<bool> ShareTask { get; } = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
public bool IsDataRequested { get; set; }
}
/// <summary>
/// Share the screenshot with a windows app
/// </summary>
/// <param name="manuallyInitiated"></param>
/// <param name="surface"></param>
/// <param name="captureDetails"></param>
/// <returns>ExportInformation</returns>
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails)
{ {
var exportInformation = new ExportInformation(Designation, Description); var exportInformation = new ExportInformation(Designation, Description);
try try
{ {
var handle = PluginUtils.Host.GreenshotForm.Handle; var triggerWindow = new Window
{
WindowState = WindowState.Normal,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
Width = 400,
Height = 400
};
var exportTarget = Task.Run(async () => triggerWindow.Show();
{ var shareInfo = new ShareInfo();
var taskCompletionSource = new TaskCompletionSource<string>();
using var imageStream = new MemoryRandomAccessStream(); // This is a bad trick, but don't know how else to do it.
using var logoStream = new MemoryRandomAccessStream(); // Wait for the focus to return, and depending on the state close the window!
using var thumbnailStream = new MemoryRandomAccessStream(); triggerWindow.WindowMessages()
var outputSettings = new SurfaceOutputSettings(); .Where(m => m.Message == WindowsMessages.WM_SETFOCUS).Delay(TimeSpan.FromSeconds(1))
outputSettings.PreventGreenshotFormat(); .Subscribe(info =>
// Create capture for export
ImageOutput.SaveToStream(surface, imageStream, outputSettings);
imageStream.Position = 0;
Log.Info("Created RandomAccessStreamReference for the image");
var imageRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(imageStream);
RandomAccessStreamReference thumbnailRandomAccessStreamReference;
RandomAccessStreamReference logoRandomAccessStreamReference;
// Create thumbnail
using (var tmpImageForThumbnail = surface.GetImageForExport())
{ {
using var thumbnail = ImageHelper.CreateThumbnail(tmpImageForThumbnail, 240, 160); if (shareInfo.ApplicationName != null)
ImageOutput.SaveToStream(thumbnail, null, thumbnailStream, outputSettings);
thumbnailStream.Position = 0;
thumbnailRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(thumbnailStream);
Log.Info("Created RandomAccessStreamReference for the thumbnail");
}
// Create logo
using (var logo = GreenshotResources.getGreenshotIcon().ToBitmap())
{
using var logoThumbnail = ImageHelper.CreateThumbnail(logo, 30, 30);
ImageOutput.SaveToStream(logoThumbnail, null, logoStream, outputSettings);
logoStream.Position = 0;
logoRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(logoStream);
Log.Info("Created RandomAccessStreamReference for the logo");
}
string applicationName = null;
var dataTransferManagerHelper = new DataTransferManagerHelper(handle);
dataTransferManagerHelper.DataTransferManager.TargetApplicationChosen += (dtm, args) =>
{
Log.InfoFormat("Trying to share with {0}", args.ApplicationName);
applicationName = args.ApplicationName;
};
var filename = FilenameHelper.GetFilename(OutputFormat.png, captureDetails);
var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest =>
{
// Information on how was found here: https://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/
Log.DebugFormat("Creating deferred file {0}", filename);
try
{ {
using (var deferredStream = streamedFileDataRequest.AsStreamForWrite()) return;
{
await imageStream.CopyToAsync(deferredStream).ConfigureAwait(false);
await imageStream.FlushAsync().ConfigureAwait(false);
}
// Signal that the stream is ready
streamedFileDataRequest.Dispose();
} }
catch (Exception)
{ shareInfo.ShareTask.TrySetResult(false);
streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete); });
} var windowHandle = new WindowInteropHelper(triggerWindow).Handle;
// Signal transfer ready to the await down below
taskCompletionSource.TrySetResult(applicationName); Share(shareInfo, windowHandle, surface, captureDetails).GetAwaiter().GetResult();
}, imageRandomAccessStreamReference).AsTask().ConfigureAwait(false); Log.Debug("Sharing finished, closing window.");
triggerWindow.Close();
dataTransferManagerHelper.DataTransferManager.DataRequested += (sender, args) => if (string.IsNullOrWhiteSpace(shareInfo.ApplicationName))
{
exportInformation.ExportMade = false;
}
else
{
exportInformation.ExportMade = true;
exportInformation.DestinationDescription = shareInfo.ApplicationName;
}
}
catch (Exception ex)
{
exportInformation.ExportMade = false;
exportInformation.ErrorMessage = ex.Message;
}
ProcessExport(exportInformation, surface);
return exportInformation;
}
/// <summary>
/// Share the surface by using the Share-UI of Windows 10
/// </summary>
/// <param name="shareInfo">ShareInfo</param>
/// <param name="handle">IntPtr with the handle for the hosting window</param>
/// <param name="surface">ISurface with the bitmap to share</param>
/// <param name="captureDetails">ICaptureDetails</param>
/// <returns>Task with string, which describes the application which was used to share with</returns>
private async Task Share(ShareInfo shareInfo, IntPtr handle, ISurface surface, ICaptureDetails captureDetails)
{
using (var imageStream = new MemoryRandomAccessStream())
using (var logoStream = new MemoryRandomAccessStream())
using (var thumbnailStream = new MemoryRandomAccessStream())
{
var outputSettings = new SurfaceOutputSettings();
outputSettings.PreventGreenshotFormat();
// Create capture for export
ImageOutput.SaveToStream(surface, imageStream, outputSettings);
imageStream.Position = 0;
Log.Debug("Created RandomAccessStreamReference for the image");
var imageRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(imageStream);
// Create thumbnail
RandomAccessStreamReference thumbnailRandomAccessStreamReference;
using (var tmpImageForThumbnail = surface.GetImageForExport())
using (var thumbnail = ImageHelper.CreateThumbnail(tmpImageForThumbnail, 240, 160))
{
ImageOutput.SaveToStream(thumbnail, null, thumbnailStream, outputSettings);
thumbnailStream.Position = 0;
thumbnailRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(thumbnailStream);
Log.Debug("Created RandomAccessStreamReference for the thumbnail");
}
// Create logo
RandomAccessStreamReference logoRandomAccessStreamReference;
using (var logo = GreenshotResources.getGreenshotIcon().ToBitmap())
using (var logoThumbnail = ImageHelper.CreateThumbnail(logo, 30, 30))
{
ImageOutput.SaveToStream(logoThumbnail, null, logoStream, outputSettings);
logoStream.Position = 0;
logoRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(logoStream);
Log.Info("Created RandomAccessStreamReference for the logo");
}
var dataTransferManagerHelper = new DataTransferManagerHelper(handle);
dataTransferManagerHelper.DataTransferManager.ShareProvidersRequested += (sender, args) =>
{
shareInfo.AreShareProvidersRequested = true;
Log.DebugFormat("Share providers requested: {0}", string.Join(",", args.Providers.Select(p => p.Title)));
};
dataTransferManagerHelper.DataTransferManager.TargetApplicationChosen += (dtm, args) =>
{
shareInfo.ApplicationName = args.ApplicationName;
Log.DebugFormat("TargetApplicationChosen: {0}", args.ApplicationName);
};
var filename = FilenameHelper.GetFilename(OutputFormat.png, captureDetails);
var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest =>
{
shareInfo.IsDeferredFileCreated = true;
// Information on the "how" was found here: https://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/
Log.DebugFormat("Creating deferred file {0}", filename);
try
{ {
var deferral = args.Request.GetDeferral(); using (var deferredStream = streamedFileDataRequest.AsStreamForWrite())
args.Request.Data.OperationCompleted += (dp, eventArgs) => {
await imageStream.CopyToAsync(deferredStream).ConfigureAwait(false);
await imageStream.FlushAsync().ConfigureAwait(false);
}
// Signal that the stream is ready
streamedFileDataRequest.Dispose();
// Signal that the action is ready, bitmap was exported
shareInfo.ShareTask.TrySetResult(true);
}
catch (Exception)
{
streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete);
}
}, imageRandomAccessStreamReference).AsTask().ConfigureAwait(false);
dataTransferManagerHelper.DataTransferManager.DataRequested += (dataTransferManager, dataRequestedEventArgs) =>
{
var deferral = dataRequestedEventArgs.Request.GetDeferral();
try
{
shareInfo.IsDataRequested = true;
Log.DebugFormat("DataRequested with operation {0}", dataRequestedEventArgs.Request.Data.RequestedOperation);
var dataPackage = dataRequestedEventArgs.Request.Data;
dataPackage.OperationCompleted += (dp, eventArgs) =>
{ {
Log.DebugFormat("OperationCompleted: {0}, shared with", eventArgs.Operation); Log.DebugFormat("OperationCompleted: {0}, shared with", eventArgs.Operation);
taskCompletionSource.TrySetResult(applicationName); shareInfo.CompletedWithOperation = eventArgs.Operation;
shareInfo.AcceptedFormat = eventArgs.AcceptedFormatId;
shareInfo.ShareTask.TrySetResult(true);
};
dataPackage.Destroyed += (dp, o) =>
{
shareInfo.IsDestroyed = true;
Log.Debug("Destroyed");
shareInfo.ShareTask.TrySetResult(true);
};
dataPackage.ShareCompleted += (dp, shareCompletedEventArgs) =>
{
shareInfo.IsShareCompleted = true;
Log.Debug("ShareCompleted");
shareInfo.ShareTask.TrySetResult(true);
}; };
var dataPackage = args.Request.Data;
dataPackage.Properties.Title = captureDetails.Title; dataPackage.Properties.Title = captureDetails.Title;
dataPackage.Properties.ApplicationName = "Greenshot"; dataPackage.Properties.ApplicationName = "Greenshot";
dataPackage.Properties.Description = "Share a screenshot"; dataPackage.Properties.Description = "Share a screenshot";
dataPackage.Properties.Thumbnail = thumbnailRandomAccessStreamReference; dataPackage.Properties.Thumbnail = thumbnailRandomAccessStreamReference;
dataPackage.Properties.Square30x30Logo = logoRandomAccessStreamReference; dataPackage.Properties.Square30x30Logo = logoRandomAccessStreamReference;
dataPackage.Properties.LogoBackgroundColor = Color.FromArgb(0xff, 0x3d, 0x3d, 0x3d); dataPackage.Properties.LogoBackgroundColor = Color.FromArgb(0xff, 0x3d, 0x3d, 0x3d);
dataPackage.SetStorageItems(new List<IStorageItem> { storageFile }); dataPackage.SetStorageItems(new[] {storageFile});
dataPackage.SetBitmap(imageRandomAccessStreamReference); dataPackage.SetBitmap(imageRandomAccessStreamReference);
dataPackage.Destroyed += (dp, o) => }
{ finally
Log.Debug("Destroyed."); {
};
deferral.Complete(); deferral.Complete();
}; Log.Debug("Called deferral.Complete()");
dataTransferManagerHelper.ShowShareUi(); }
return await taskCompletionSource.Task.ConfigureAwait(false); };
}).Result; dataTransferManagerHelper.ShowShareUi();
if (string.IsNullOrWhiteSpace(exportTarget)) Log.Debug("ShowShareUi finished.");
{ await shareInfo.ShareTask.Task.ConfigureAwait(false);
exportInformation.ExportMade = false; }
} }
else }
{
exportInformation.ExportMade = true;
exportInformation.DestinationDescription = exportTarget;
}
}
catch (Exception ex)
{
exportInformation.ExportMade = false;
exportInformation.ErrorMessage = ex.Message;
}
ProcessExport(exportInformation, surface);
return exportInformation;
}
}
} }