From a572bd85530b25b6992bb2444fb181e9b602ac4f Mon Sep 17 00:00:00 2001 From: Frank Skare Date: Wed, 31 Jul 2019 08:01:03 +0200 Subject: [PATCH] new setting remember-volume added --- Changelog.md | 9 +- mpv.net/Misc/App.cs | 125 +++++++++++++++++++++++++++ mpv.net/Misc/Misc.cs | 95 -------------------- mpv.net/Resources/mpvConf.txt | 7 +- mpv.net/Resources/mpvNetConfToml.txt | 10 ++- mpv.net/WPF/ConfWindow.xaml.cs | 7 +- mpv.net/WinForms/MainForm.cs | 40 +++++---- mpv.net/mpv.net.csproj | 1 + mpv.net/mpv/mp.cs | 11 ++- 9 files changed, 173 insertions(+), 132 deletions(-) create mode 100644 mpv.net/Misc/App.cs diff --git a/Changelog.md b/Changelog.md index df534d9..9e5077a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -13,9 +13,8 @@ - 'Tools > Manage File Associations' was replaced by 'Tools > OS Setup', it has now a feature to add and remove mpv.net to and from the Path environment variable and the OS default apps settings can be opened (Win 10 only) -- startup folder and config folder beeing identical is no longer - a supported scenaria because it's a brain-dead idea -- Error messages are shown when unknown scripts and extensions are found in the startup folder +- startup folder and config folder beeing identical is no longer a supported scenaria +- error messages are shown when unknown scripts and extensions are found in the startup folder because user scripts and extensions are supposed to be located in the config folder instead ### 5.0 @@ -99,6 +98,10 @@ the task bar because this is the WinForms default behavier. This was fixed by calling Spy++ to the rescue and adding WS_MINIMIZEBOX in CreateParams +- changing from maximized to fullscreen did not work +- the search field in the config editor was not always remembered +- new setting remember-volume added, saves volume and mute on exit + and restores it on start. ### 4.6 diff --git a/mpv.net/Misc/App.cs b/mpv.net/Misc/App.cs new file mode 100644 index 0000000..be333ea --- /dev/null +++ b/mpv.net/Misc/App.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Windows.Forms; + +namespace mpvnet +{ + public class App + { + public static string RegPath { get; } = @"HKCU\Software\" + Application.ProductName; + public static string ConfPath { get; } = mp.ConfigFolder + "\\mpvnet.conf"; + public static string DarkMode { get; set; } = "always"; + public static string ProcessInstance { get; set; } = "single"; + public static string DarkColor { get; set; } + public static string LightColor { get; set; } + + public static string[] VideoTypes { get; } = "264 265 asf avc avi avs flv h264 h265 hevc m2ts m2v m4v mkv mov mp4 mpeg mpg mpv mts ts vob vpy webm webm wmv y4m".Split(' '); + public static string[] AudioTypes { get; } = "mp3 mp2 ac3 ogg opus flac wav w64 m4a dts dtsma dtshr dtshd eac3 thd thd+ac3 mka aac mpa".Split(' '); + public static string[] ImageTypes { get; } = "jpg bmp gif png".Split(' '); + public static string[] SubtitleTypes { get; } = "srt ass idx sup ttxt ssa smi".Split(' '); + public static string[] UrlWhitelist { get; set; } = { "tube", "vimeo", "ard", "zdf" }; + + public static bool RememberHeight { get; set; } = true; + public static bool RememberPosition { get; set; } + public static bool DebugMode { get; set; } + public static bool IsStartedFromTerminal { get; } = Environment.GetEnvironmentVariable("_started_from_console") == "yes"; + public static bool RememberVolume { get; set; } + + public static int StartThreshold { get; set; } = 1500; + + public static bool IsDarkMode { + get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always"; + } + + public static void Init() + { + string dummy = mp.ConfigFolder; + var dummy2 = mp.Conf; + + foreach (var i in Conf) + ProcessProperty(i.Key, i.Value); + + if (App.DebugMode) + { + try + { + string filePath = mp.ConfigFolder + "\\mpvnet-debug.log"; + if (File.Exists(filePath)) File.Delete(filePath); + Trace.Listeners.Add(new TextWriterTraceListener(filePath)); + Trace.AutoFlush = true; + //if (App.DebugMode) Trace.WriteLine(""); + } + catch (Exception e) + { + Msg.ShowException(e); + } + } + + mp.Shutdown += Shutdown; + mp.Initialized += Initialized; + } + + private static void Initialized() + { + if (RememberVolume) + { + mp.set_property_int("volume", RegHelp.GetInt(App.RegPath, "Volume")); + mp.set_property_string("mute", RegHelp.GetString(App.RegPath, "Mute")); + } + } + + private static void Shutdown() + { + if (RememberVolume) + { + RegHelp.SetObject(App.RegPath, "Volume", mp.get_property_int("volume")); + RegHelp.SetObject(App.RegPath, "Mute", mp.get_property_string("mute")); + } + } + + static Dictionary _Conf; + + public static Dictionary Conf { + get { + if (_Conf == null) + { + _Conf = new Dictionary(); + + if (File.Exists(ConfPath)) + foreach (string i in File.ReadAllLines(ConfPath)) + if (i.Contains("=") && !i.StartsWith("#")) + _Conf[i.Substring(0, i.IndexOf("=")).Trim()] = i.Substring(i.IndexOf("=") + 1).Trim(); + } + return _Conf; + } + } + + public static void UnknownModule(string path) + { + Msg.ShowError("Failed to load script or extension", "Only scripts and extensions that ship with mpv.net are allowed in \\scripts or \\extensions.\n\nUser scripts or extensions have to use \\scripts or \\extensions.\n\nNever copy a new mpv.net version over a old mpv.net version.\n\nNever install a new mpv.net version on top of a old mpv.net version.\n\n" + path); + } + + public static bool ProcessProperty(string name, string value) + { + switch (name) // return true instead of break! + { + case "remember-position": RememberPosition = value == "yes"; return true; + case "start-size": RememberHeight = value == "previous"; return true; + case "process-instance": ProcessInstance = value; return true; + case "dark-mode": DarkMode = value; return true; + case "debug-mode": DebugMode = value == "yes"; return true; + case "dark-color": DarkColor = value.Trim('\'', '"'); return true; + case "light-color": LightColor = value.Trim('\'', '"'); return true; + case "url-whitelist": UrlWhitelist = value.Split(' ', ',', ';'); return true; + case "remember-volume": RememberVolume = value == "yes"; return true; + case "start-threshold": + int.TryParse(value, out int result); + StartThreshold = result; + return true; + } + return false; + } + } +} \ No newline at end of file diff --git a/mpv.net/Misc/Misc.cs b/mpv.net/Misc/Misc.cs index 88e1c74..0a7045c 100644 --- a/mpv.net/Misc/Misc.cs +++ b/mpv.net/Misc/Misc.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; -using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; @@ -16,100 +15,6 @@ using Microsoft.Win32; namespace mpvnet { - public class App - { - public static string RegPath { get; } = @"HKCU\Software\" + Application.ProductName; - public static string ConfPath { get; } = mp.ConfigFolder + "\\mpvnet.conf"; - public static string DarkMode { get; set; } = "always"; - public static string ProcessInstance { get; set; } = "single"; - public static string DarkColor { get; set; } - public static string LightColor { get; set; } - - public static string[] VideoTypes { get; } = "264 265 asf avc avi avs flv h264 h265 hevc m2ts m2v m4v mkv mov mp4 mpeg mpg mpv mts ts vob vpy webm webm wmv y4m".Split(' '); - public static string[] AudioTypes { get; } = "mp3 mp2 ac3 ogg opus flac wav w64 m4a dts dtsma dtshr dtshd eac3 thd thd+ac3 mka aac mpa".Split(' '); - public static string[] ImageTypes { get; } = "jpg bmp gif png".Split(' '); - public static string[] SubtitleTypes { get; } = "srt ass idx sup ttxt ssa smi".Split(' '); - public static string[] UrlWhitelist { get; set; } = { "tube", "vimeo", "ard", "zdf" }; - - public static bool RememberHeight { get; set; } = true; - public static bool RememberPosition { get; set; } - public static bool DebugMode { get; set; } - public static bool IsStartedFromTerminal { get; } = Environment.GetEnvironmentVariable("_started_from_console") == "yes"; - - public static int StartThreshold { get; set; } = 1500; - - public static bool IsDarkMode { - get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always"; - } - - public static void Init() - { - string dummy = mp.ConfigFolder; - var dummy2 = mp.Conf; - - foreach (var i in Conf) - ProcessProperty(i.Key, i.Value); - - if (App.DebugMode) - { - try - { - string filePath = mp.ConfigFolder + "\\mpvnet-debug.log"; - if (File.Exists(filePath)) File.Delete(filePath); - Trace.Listeners.Add(new TextWriterTraceListener(filePath)); - Trace.AutoFlush = true; - //if (App.DebugMode) Trace.WriteLine(""); - } - catch (Exception e) - { - Msg.ShowException(e); - } - } - } - - static Dictionary _Conf; - - public static Dictionary Conf { - get { - if (_Conf == null) - { - _Conf = new Dictionary(); - - if (File.Exists(ConfPath)) - foreach (string i in File.ReadAllLines(ConfPath)) - if (i.Contains("=") && !i.StartsWith("#")) - _Conf[i.Substring(0, i.IndexOf("=")).Trim()] = i.Substring(i.IndexOf("=") + 1).Trim(); - } - return _Conf; - } - } - - public static void UnknownModule(string path) - { - Msg.ShowError("Failed to load script or extension", "Only scripts and extensions that ship with mpv.net are allowed in \\scripts or \\extensions.\n\nUser scripts or extensions have to use \\scripts or \\extensions.\n\nNever copy a new mpv.net version over a old mpv.net version.\n\nNever install a new mpv.net version on top of a old mpv.net version.\n\n" + path); - } - - public static bool ProcessProperty(string name, string value) - { - switch (name) // return true instead of break! - { - case "remember-position": RememberPosition = value == "yes"; return true; - case "start-size": RememberHeight = value == "previous"; return true; - case "process-instance": ProcessInstance = value; return true; - case "dark-mode": DarkMode = value; return true; - case "debug-mode": DebugMode = value == "yes"; return true; - case "dark-color": DarkColor = value.Trim('\'', '"'); return true; - case "light-color": LightColor = value.Trim('\'', '"'); return true; - case "url-whitelist": UrlWhitelist = value.Split(' ', ',', ';'); return true; - case "start-threshold": - int.TryParse(value, out int result); - StartThreshold = result; - return true; - } - return false; - } - } - public class Sys { public static bool IsDarkTheme { diff --git a/mpv.net/Resources/mpvConf.txt b/mpv.net/Resources/mpvConf.txt index f5d13de..4ea22eb 100644 --- a/mpv.net/Resources/mpvConf.txt +++ b/mpv.net/Resources/mpvConf.txt @@ -1,9 +1,4 @@ - -# manual: https://mpv.io/manual/master/ - -# defaults: https://github.com/stax76/mpv.net/blob/master/mpv.net/Resources/mpvConf.txt - -input-ar-delay = 500 +input-ar-delay = 500 input-ar-rate = 20 volume = 50 hwdec = yes diff --git a/mpv.net/Resources/mpvNetConfToml.txt b/mpv.net/Resources/mpvNetConfToml.txt index 830a297..0047fd7 100644 --- a/mpv.net/Resources/mpvNetConfToml.txt +++ b/mpv.net/Resources/mpvNetConfToml.txt @@ -59,6 +59,14 @@ help = "Threshold in milliseconds to wait for libmpv returning the video resolut name = "remember-position" default = "no" filter = "Screen" -help = "Setting to save the window position on exit. (mpv.net specific setting)" +help = "Save the window position on exit. (mpv.net specific setting)" +options = [{ name = "yes" }, + { name = "no" }] + +[[settings]] +name = "remember-volume" +default = "no" +filter = "Audio" +help = "Save volume and mute on exit and restore it on start. (mpv.net specific setting)" options = [{ name = "yes" }, { name = "no" }] \ No newline at end of file diff --git a/mpv.net/WPF/ConfWindow.xaml.cs b/mpv.net/WPF/ConfWindow.xaml.cs index f9d1e06..cf56589 100644 --- a/mpv.net/WPF/ConfWindow.xaml.cs +++ b/mpv.net/WPF/ConfWindow.xaml.cs @@ -29,7 +29,7 @@ namespace mpvnet LoadSettings(NetSettingsDefinitions, NetConf); InitialContent = GetContent(mp.ConfPath, Conf, SettingsDefinitions) + GetContent(App.ConfPath, NetConf, NetSettingsDefinitions); - SearchControl.Text = RegHelp.GetString(App.RegPath, "config editor search"); + SearchControl.Text = RegHelp.GetString(App.RegPath, "ConfigEditorSearch"); if (App.IsDarkMode) { @@ -124,14 +124,13 @@ namespace mpvnet protected override void OnClosed(EventArgs e) { base.OnClosed(e); + RegHelp.SetObject(App.RegPath, "ConfigEditorSearch", SearchControl.Text); string content = GetContent(mp.ConfPath, Conf, SettingsDefinitions); string netContent = GetContent(App.ConfPath, NetConf, NetSettingsDefinitions); if (InitialContent == content + netContent) return; - string header = "\r\n# manual: https://mpv.io/manual/master/\r\n\r\n# defaults: https://github.com/stax76/mpv.net/blob/master/mpv.net/Resources/mpvConf.txt\r\n\r\n"; - File.WriteAllText(mp.ConfPath, header + content); + File.WriteAllText(mp.ConfPath, content); File.WriteAllText(App.ConfPath, netContent); Msg.Show("Changes will be available on next mpv.net startup."); - RegHelp.SetObject(App.RegPath, "config editor search", SearchControl.Text); } string GetContent(string filePath, Dictionary confSettings, List settings) diff --git a/mpv.net/WinForms/MainForm.cs b/mpv.net/WinForms/MainForm.cs index 0d3f822..594a6c3 100644 --- a/mpv.net/WinForms/MainForm.cs +++ b/mpv.net/WinForms/MainForm.cs @@ -73,8 +73,7 @@ namespace mpvnet mp.VideoSizeChanged += VideoSizeChanged; mp.FileLoaded += FileLoaded; mp.Idle += Idle; - mp.VideoSizeAutoResetEvent.WaitOne(App.StartThreshold); - if (Height < FontHeight * 4) SetFormPosAndSize(); + mp.observe_property_bool("fullscreen", PropChangeFullscreen); mp.observe_property_bool("ontop", PropChangeOnTop); mp.observe_property_bool("border", PropChangeBorder); @@ -82,6 +81,9 @@ namespace mpvnet mp.observe_property_string("aid", PropChangeAid); mp.observe_property_string("vid", PropChangeVid); mp.observe_property_int("edition", PropChangeEdition); + + mp.VideoSizeAutoResetEvent.WaitOne(App.StartThreshold); + if (Height < FontHeight * 4) SetFormPosAndSize(); } catch (Exception ex) { @@ -308,7 +310,7 @@ namespace mpvnet if (enabled) { - if (WindowState != FormWindowState.Maximized) + if (WindowState != FormWindowState.Maximized || FormBorderStyle != FormBorderStyle.None) { FormBorderStyle = FormBorderStyle.None; WindowState = FormWindowState.Maximized; @@ -465,22 +467,6 @@ namespace mpvnet base.WndProc(ref m); } - protected override void OnDragEnter(DragEventArgs e) - { - base.OnDragEnter(e); - if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent(DataFormats.Text)) - e.Effect = DragDropEffects.Copy; - } - - protected override void OnDragDrop(DragEventArgs e) - { - base.OnDragDrop(e); - if (e.Data.GetDataPresent(DataFormats.FileDrop)) - mp.Load(e.Data.GetData(DataFormats.FileDrop) as String[], true, Control.ModifierKeys.HasFlag(Keys.Control)); - if (e.Data.GetDataPresent(DataFormats.Text)) - mp.Load(new[] { e.Data.GetData(DataFormats.Text).ToString() }, true, Control.ModifierKeys.HasFlag(Keys.Control)); - } - void Timer_Tick(object sender, EventArgs e) { if (CursorHelp.IsPosDifferent(LastCursorPosChanged)) @@ -586,6 +572,22 @@ namespace mpvnet mp.commandv("quit"); } + protected override void OnDragEnter(DragEventArgs e) + { + base.OnDragEnter(e); + if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent(DataFormats.Text)) + e.Effect = DragDropEffects.Copy; + } + + protected override void OnDragDrop(DragEventArgs e) + { + base.OnDragDrop(e); + if (e.Data.GetDataPresent(DataFormats.FileDrop)) + mp.Load(e.Data.GetData(DataFormats.FileDrop) as String[], true, Control.ModifierKeys.HasFlag(Keys.Control)); + if (e.Data.GetDataPresent(DataFormats.Text)) + mp.Load(new[] { e.Data.GetData(DataFormats.Text).ToString() }, true, Control.ModifierKeys.HasFlag(Keys.Control)); + } + protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); diff --git a/mpv.net/mpv.net.csproj b/mpv.net/mpv.net.csproj index 8872699..d0d0ec5 100644 --- a/mpv.net/mpv.net.csproj +++ b/mpv.net/mpv.net.csproj @@ -114,6 +114,7 @@ + MSBuild:Compile diff --git a/mpv.net/mpv/mp.cs b/mpv.net/mpv/mp.cs index 4e35c27..276ba3d 100644 --- a/mpv.net/mpv/mp.cs +++ b/mpv.net/mpv/mp.cs @@ -54,6 +54,8 @@ namespace mpvnet public static event Action QueueOverflow; // MPV_EVENT_QUEUE_OVERFLOW public static event Action Hook; // MPV_EVENT_HOOK + public static event Action Initialized; + public static IntPtr Handle { get; set; } public static IntPtr WindowHandle { get; set; } public static Extension Extension { get; set; } @@ -77,7 +79,7 @@ namespace mpvnet public static bool Fullscreen { get; set; } public static bool Border { get; set; } = true; - + public static int Screen { get; set; } = -1; public static int Edition { get; set; } @@ -89,6 +91,7 @@ namespace mpvnet { LoadLibrary("mpv-1.dll"); Handle = mpv_create(); + Task.Run(() => { EventLoop(); }); if (App.IsStartedFromTerminal) { @@ -96,17 +99,17 @@ namespace mpvnet set_property_string("msg-level", "osd/libass=fatal"); } + set_property_string("wid", MainForm.Hwnd.ToString()); set_property_string("config-dir", ConfigFolder); set_property_string("osc", "yes"); set_property_string("config", "yes"); - set_property_string("wid", MainForm.Hwnd.ToString()); set_property_string("force-window", "yes"); set_property_string("input-media-keys", "yes"); mpv_initialize(Handle); + Initialized?.Invoke(); ShowLogo(); LoadMpvScripts(); ProcessCommandLine(); - Task.Run(() => { EventLoop(); }); } public static void ProcessProperty(string name, string value) @@ -556,7 +559,7 @@ namespace mpvnet StringPropChangeActions.Add(new KeyValuePair>(name, action)); } - protected static void ProcessCommandLine() + public static void ProcessCommandLine() { var args = Environment.GetCommandLineArgs().Skip(1); List files = new List();