support added to force single instance

This commit is contained in:
Frank Skare
2019-06-16 10:00:14 +02:00
parent 1bf8e568c5
commit 63ee598ebd
9 changed files with 122 additions and 36 deletions

View File

@@ -7,6 +7,8 @@ using System.Reflection;
using System.Windows.Forms;
using System.Windows.Interop;
using VB = Microsoft.VisualBasic;
namespace mpvnet
{
public class Command
@@ -46,7 +48,7 @@ namespace mpvnet
MainForm.Instance.Invoke(new Action(() => {
using (var d = new OpenFileDialog() { Multiselect = true })
if (d.ShowDialog() == DialogResult.OK)
mp.LoadFiles(d.FileNames);
mp.Load(d.FileNames);
}));
}
@@ -181,9 +183,9 @@ namespace mpvnet
public static void execute_mpv_command(string[] args)
{
MainForm.Instance.Invoke(new Action(() => {
string command = Microsoft.VisualBasic.Interaction.InputBox("Enter a mpv command to be executed.", "Execute Command", RegistryHelp.GetString("HKCU\\Software\\" + Application.ProductName, "RecentExecutedCommand"));
string command = VB.Interaction.InputBox("Enter a mpv command to be executed.", "Execute Command", RegistryHelp.GetString(App.RegPath, "RecentExecutedCommand"));
if (string.IsNullOrEmpty(command)) return;
RegistryHelp.SetObject("HKCU\\Software\\" + Application.ProductName, "RecentExecutedCommand", command);
RegistryHelp.SetObject(App.RegPath, "RecentExecutedCommand", command);
mp.command_string(command, false);
}));
}
@@ -191,9 +193,9 @@ namespace mpvnet
public static void open_url(string[] args)
{
MainForm.Instance.Invoke(new Action(() => {
string command = Microsoft.VisualBasic.Interaction.InputBox("Enter URL to be opened.");
string command = VB.Interaction.InputBox("Enter URL to be opened.");
if (string.IsNullOrEmpty(command)) return;
mp.LoadFiles(command);
mp.Load(command);
}));
}

View File

@@ -18,6 +18,7 @@ namespace mpvnet
public class App
{
public static string ConfFilePath { get; } = mp.ConfFolder + "\\mpvnet.conf";
public static string RegPath { get; } = "HKCU\\Software\\" + Application.ProductName;
public static string ClipboardMonitoring { get; set; } = "yes";
public static string[] VideoTypes { get; } = "mkv mp4 mpg avi mov webm vob wmv flv avs 264 h264 asf webm mpeg mpv y4m avc hevc 265 h265 m2v m2ts vpy mts m4v".Split(' ');
@@ -25,6 +26,7 @@ namespace mpvnet
public static string[] SubtitleTypes { get; } = "srt ass idx sup ttxt ssa smi".Split(' ');
public static string DarkMode { get; set; } = "always";
public static string ProcessInstance { get; set; } = "single";
public static bool IsDarkMode {
get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always";
@@ -57,12 +59,9 @@ namespace mpvnet
{
switch (name)
{
case "dark-mode":
DarkMode = value;
break;
case "clipboard-monitoring":
ClipboardMonitoring = value;
break;
case "clipboard-monitoring": ClipboardMonitoring = value; break;
case "process-instance": ProcessInstance = value; break;
case "dark-mode": DarkMode = value; break;
}
}
@@ -363,4 +362,15 @@ namespace mpvnet
Math.Abs(screenPos.Y - Control.MousePosition.Y) > 10;
}
}
public class SingleProcess
{
public static int Message { get; } = RegisterWindowMessage("mpvnet_IPC");
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern int RegisterWindowMessage(string id);
[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(int dwProcessId);
}
}

View File

@@ -1,5 +1,10 @@
using System;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace mpvnet
{
@@ -8,15 +13,43 @@ namespace mpvnet
[STAThread]
static void Main()
{
Mutex mutex = new Mutex(true, "mpvnetProcessInstance", out bool isFirst);
try
{
string[] args = Environment.GetCommandLineArgs();
string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
if (args.Length == 3 && args[1] == "--reg-file-assoc")
if (args.Length == 2 && args[0] == "--reg-file-assoc")
{
if (args[2] == "audio") FileAssociation.Register(App.AudioTypes);
if (args[2] == "video") FileAssociation.Register(App.VideoTypes);
if (args[2] == "unreg") FileAssociation.Unregister();
if (args[1] == "audio") FileAssociation.Register(App.AudioTypes);
if (args[1] == "video") FileAssociation.Register(App.VideoTypes);
if (args[1] == "unreg") FileAssociation.Unregister();
return;
}
App.Init();
if ((App.ProcessInstance == "single" || App.ProcessInstance == "queue") && !isFirst)
{
List<string> files = new List<string>();
foreach (string arg in args)
if (!arg.StartsWith("--") && (File.Exists(arg) || arg == "-" || arg.StartsWith("http")))
files.Add(arg);
if (files.Count > 0)
RegistryHelp.SetObject(App.RegPath, "ShellFiles", files.ToArray());
RegistryHelp.SetObject(App.RegPath, "ProcessInstanceMode", App.ProcessInstance);
foreach(Process process in Process.GetProcessesByName("mpvnet"))
{
try {
SingleProcess.AllowSetForegroundWindow(process.Id);
Native.SendMessage(process.MainWindowHandle, SingleProcess.Message, IntPtr.Zero, IntPtr.Zero);
} catch {}
}
return;
}
@@ -28,6 +61,10 @@ namespace mpvnet
{
Msg.ShowException(ex);
}
finally
{
mutex.Dispose();
}
}
}
}