-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
8,732 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,4 +360,6 @@ MigrationBackup/ | |
.ionide/ | ||
|
||
# Fody - auto-generated XML schema | ||
FodyWeavers.xsd | ||
FodyWeavers.xsd | ||
|
||
*.psd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
108
VRCPictureToClipboard/VRCPictureClipboardApplicationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.