Skip to content

Commit

Permalink
V2 rewrite
Browse files Browse the repository at this point in the history
Including features by @jangxx to enhance functionality and usability.
  • Loading branch information
Duinrahaic authored Nov 12, 2023
2 parents 66c8372 + 3766732 commit 7fd23e3
Show file tree
Hide file tree
Showing 15 changed files with 8,732 additions and 60 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

*.psd
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# VRCPictureToClipboard

While this program runs in the background, it will automatically takes the latest picture from VRChat and copy it to the clipboard.
While this program runs in the background, it automatically takes the latest picture from VRChat and copies it to the clipboard.

Also while you are here please upvote this [feature](https://feedback.vrchat.com/feature-requests/p/picture-to-clipboard) to get this feature included into VRChat.


## Usage

After starting the program, it lives in the system tray.
Clicking on the tray icon gives opens a menu with a few different options:

- `Start/Attach to SteamVR`: If the program was opened with SteamVR not running, this option will launch it, or attach it to a running instance that was manually started.
- `Register with SteamVR`: "Installs" the software in SteamVR. This will put it into the list of possible startup overlay apps, so you can have SteamVR launch it automatically.
- `Unregister from SteamVR`: "Uninstalls" the software from SteamVR, so it can no longer be automatically started.
- `Pause`: Will pause/unpause the clipboard copying without closing the app.
- `Exit`
6 changes: 3 additions & 3 deletions VRCPictureToClipboard.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VRCPictureToClipboard", "VRCPictureToClipboard\VRCPictureToClipboard.csproj", "{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VRCPictureToClipboard", "VRCPictureToClipboard\VRCPictureToClipboard.csproj", "{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}.Debug|Any CPU.Build.0 = Release|Any CPU
{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F6C74A1-3B50-4C7A-AD0C-636518300CB6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
Expand Down
164 changes: 164 additions & 0 deletions VRCPictureToClipboard/OVRIntegration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Valve.VR;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace VRCPictureToClipboard
{
internal class OVRIntegration
{
public static string APPLICATION_KEY = "com.jangxx.vrc-picture-clipboard";

private CVRSystem? cVR;

private bool initialized = false;
public bool Initialized
{
get { return initialized; }
}

public event EventHandler? CloseRequested;

private CancellationTokenSource CancelTokenSource = new CancellationTokenSource();
private Thread? pollingThread = null;

public bool TryInit(bool startSteamVR = false)
{
if (cVR != null)
{
this.initialized = false;
return false;
}

if (pollingThread != null)
{
CancelTokenSource.Cancel();
pollingThread.Join();
pollingThread = null;
}

EVRInitError error = EVRInitError.None;
if (startSteamVR)
{
cVR = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay);
} else
{
cVR = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Background);
}

if (error != EVRInitError.None)
{
this.initialized = false;
return false;
}
else
{
this.initialized = true;

CancelTokenSource = new CancellationTokenSource();
pollingThread = new Thread(() => PollEvents());
pollingThread.IsBackground = true;
pollingThread.Name = "OpenVR Polling";
pollingThread.Start();

return true;
}
}

private void PollEvents()
{
VREvent_t evt = new VREvent_t();
uint eventSize = (uint)Marshal.SizeOf(evt);

while (true)
{
if (OpenVR.System.PollNextEvent(ref evt, eventSize))
{
if (evt.eventType == (uint)EVREventType.VREvent_Quit)
{
var handler = CloseRequested;
handler?.Invoke(this, new EventArgs());
}
}

// we do this instead of thread.sleep
if (CancelTokenSource.Token.WaitHandle.WaitOne(100))
{
return; // cancellation was requested
}
}
}

public void Shutdown()
{
if (cVR != null)
{
OpenVR.Shutdown();
cVR = null;
}
}

public bool IsInstalled()
{
if (cVR == null || !initialized)
{
return false;
}

return OpenVR.Applications.IsApplicationInstalled(APPLICATION_KEY);
}

public void InstallManifest()
{
if (cVR == null || !initialized)
{
return;
}

var executablePath = Application.ExecutablePath;
var executableDir = Path.GetDirectoryName(executablePath);

EVRApplicationError error = OpenVR.Applications.AddApplicationManifest(Path.Join(executableDir, "manifest.vrmanifest"), false);

if (error != EVRApplicationError.None)
{
MessageBox.Show("Error while registering with SteamVR: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} else
{
MessageBox.Show("Successfully registered with SteamVR", "Success");
}
}

public void UninstallManifest()
{
if (cVR == null || !initialized)
{
return;
}

StringBuilder sb = new StringBuilder("", 512);
EVRApplicationError error = EVRApplicationError.None;

OpenVR.Applications.GetApplicationPropertyString(APPLICATION_KEY, EVRApplicationProperty.WorkingDirectory_String, sb, 512, ref error);

var manifestPath = Path.Join(sb.ToString(), "manifest.vrmanifest");
error = OpenVR.Applications.RemoveApplicationManifest(manifestPath);

if (error != EVRApplicationError.None)
{
MessageBox.Show("Error while unregistering from SteamVR: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Successfully unregistered from SteamVR", "Success");
}
}
}
}
42 changes: 29 additions & 13 deletions VRCPictureToClipboard/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
using System;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;
using System.Windows.Controls;
using VRCPictureToClipboard;

IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices(services =>
namespace VRCPictureToClipboard
{
static class Program
{
services.AddHostedService<Watcher>();
})
.Build();
[STAThread]
public static void Main()
{
// only allow running one copy of this app at the same time
bool mutexSuccess = false;
var globalMutex = new Mutex(true, @"Local\VRCPictureToClipboard.exe", out mutexSuccess);

await host.RunAsync();
if (!mutexSuccess)
{
Debug.Print("App is already running. Quitting...");
globalMutex.Close();
return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new VRCPictureClipboardApplicationContext());

globalMutex.Close();
}
}
}
108 changes: 108 additions & 0 deletions VRCPictureToClipboard/VRCPictureClipboardApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VRCPictureToClipboard
{
internal class VRCPictureClipboardApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
private Watcher watcher;
private OVRIntegration ovr;

public VRCPictureClipboardApplicationContext()
{
var iconStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("VRCPictureToClipboard.icon.ico");

if (iconStream == null)
{
throw new Exception("Trayicon could not be loaded");
}

var icon = new Icon(iconStream);

trayIcon = new NotifyIcon()
{
Icon = icon,
ContextMenuStrip = new ContextMenuStrip(),
Visible = true,
Text = "VRC Picture To Clipboard",
};

watcher = new Watcher();

ovr = new OVRIntegration();
ovr.TryInit();

ovr.CloseRequested += CloseRequestedHandler;

setupTrayMenu();
}

private void setupTrayMenu()
{
trayIcon.ContextMenuStrip.Items.Clear();

if (ovr.Initialized)
{
if (ovr.IsInstalled())
{
trayIcon.ContextMenuStrip.Items.Add(new ToolStripMenuItem("Unregister from SteamVR", null, UnregisterSteamVR));
} else
{
trayIcon.ContextMenuStrip.Items.Add(new ToolStripMenuItem("Register with SteamVR", null, RegisterSteamVR));
}
} else
{
trayIcon.ContextMenuStrip.Items.Add(new ToolStripMenuItem("Start/Attach to SteamVR", null, AttachSteamVR));
}

trayIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator());

trayIcon.ContextMenuStrip.Items.Add(new ToolStripMenuItem("Pause", null, Pause));
trayIcon.ContextMenuStrip.Items.Add(new ToolStripMenuItem("Exit", null, Exit));
}

private void CloseRequestedHandler(object? sender, EventArgs args)
{
Exit(null, new EventArgs());
}

private void AttachSteamVR(object? sender, EventArgs e)
{
ovr.TryInit(true);
setupTrayMenu();
}

private void RegisterSteamVR(object? sender, EventArgs e)
{
ovr.InstallManifest();
setupTrayMenu();
}

private void UnregisterSteamVR(object? sender, EventArgs e)
{
ovr.UninstallManifest();
setupTrayMenu();
}

private void Pause(object? sender, EventArgs e)
{
watcher.SetPaused(!watcher.Paused);

((ToolStripMenuItem)sender!).Checked = watcher.Paused;
}

private void Exit(object? sender, EventArgs e)
{
trayIcon.Visible = false;
Application.Exit();
}
}
}
Loading

0 comments on commit 7fd23e3

Please sign in to comment.