From 4284cf487665dd2013b8c95cf2195980ac8106df Mon Sep 17 00:00:00 2001 From: sr55 Date: Tue, 19 Apr 2016 21:52:27 +0100 Subject: [PATCH] WinGui: Some initial work to make the HandBrake.ApplicationServices library more portable. --- .../HandBrake.ApplicationServices.csproj | 1 - .../Utilities/LanguageUtilities.cs | 27 +- .../Utilities/SystemInfo.cs | 95 +---- win/CS/HandBrake.sln | 16 +- .../Converters/Video/VideoEncoderConverter.cs | 1 + win/CS/HandBrakeWPF/HandBrakeWPF.csproj | 2 + .../Services/PrePostActionService.cs | 1 + win/CS/HandBrakeWPF/Utilities/SystemInfo.cs | 114 +++++ .../Utilities/Win32.cs | 398 +++++++++--------- .../ViewModels/OptionsViewModel.cs | 1 + 10 files changed, 334 insertions(+), 322 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Utilities/SystemInfo.cs rename win/CS/{HandBrake.ApplicationServices => HandBrakeWPF}/Utilities/Win32.cs (95%) diff --git a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj index 8d023b901..14ba10545 100644 --- a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj +++ b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj @@ -189,7 +189,6 @@ - True True diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/LanguageUtilities.cs b/win/CS/HandBrake.ApplicationServices/Utilities/LanguageUtilities.cs index 12665c062..49c1c488d 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/LanguageUtilities.cs +++ b/win/CS/HandBrake.ApplicationServices/Utilities/LanguageUtilities.cs @@ -227,31 +227,6 @@ namespace HandBrake.ApplicationServices.Utilities return languageMap; } - /// - /// The get language codes. - /// - /// - /// The user languages. - /// - /// - /// The . - /// - public static List GetLanguageCodes(StringCollection userLanguages) - { - // Translate to Iso Codes - List iso6392Codes = new List(); - foreach (var item in userLanguages) - { - string isoCode; - if (LanguageUtilities.MapLanguages().TryGetValue(item, out isoCode)) - { - iso6392Codes.Add(isoCode); - } - } - - return iso6392Codes; - } - /// /// The get language codes. /// @@ -261,7 +236,7 @@ namespace HandBrake.ApplicationServices.Utilities /// /// The . /// - public static List GetLanguageCodes(IList userLanguages) + public static List GetLanguageCodes(IEnumerable userLanguages) { // Translate to Iso Codes List iso6392Codes = new List(); diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/SystemInfo.cs b/win/CS/HandBrake.ApplicationServices/Utilities/SystemInfo.cs index ce5e5506f..8ccf04462 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/SystemInfo.cs +++ b/win/CS/HandBrake.ApplicationServices/Utilities/SystemInfo.cs @@ -10,58 +10,14 @@ namespace HandBrake.ApplicationServices.Utilities { using System; - using System.Collections.Generic; - using System.Management; - using System.Windows.Forms; - using HandBrake.ApplicationServices.Interop.HbLib; - - using Microsoft.Win32; + using Interop.HbLib; /// /// The System Information. /// public class SystemInfo { - /// - /// Gets the total physical ram in a system - /// - /// The total memory in the system - public static ulong TotalPhysicalMemory - { - get - { - Win32.MEMORYSTATUSEX memStat = new Win32.MEMORYSTATUSEX { dwLength = 64 }; - Win32.GlobalMemoryStatusEx(ref memStat); - - ulong value = memStat.ullTotalPhys / 1024 / 1024; - return value; - } - } - - /// - /// Gets the number of CPU Cores - /// - /// Object - public static object GetCpuCount - { - get - { - RegistryKey regKey = Registry.LocalMachine; - regKey = regKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); - return regKey == null ? 0 : regKey.GetValue("ProcessorNameString"); - } - } - - /// - /// Gets the System screen size information. - /// - /// System.Windows.Forms.Scree - public static Screen ScreenBounds - { - get { return Screen.PrimaryScreen; } - } - /// /// Gets a value indicating whether is qsv available. /// @@ -118,54 +74,5 @@ namespace HandBrake.ApplicationServices.Utilities } } } - - /// - /// Gets the get gpu driver version. - /// - public static List GetGPUInfo - { - get - { - List gpuInfo = new List(); - - try - { - ManagementObjectSearcher searcher = - new ManagementObjectSearcher("select * from " + "Win32_VideoController"); - - foreach (ManagementObject share in searcher.Get()) - { - string gpu = string.Empty, version = string.Empty; - - foreach (PropertyData PC in share.Properties) - { - if (!string.IsNullOrEmpty(PC.Name) && PC.Value != null) - { - if (PC.Name.Equals("DriverVersion")) version = PC.Value.ToString(); - if (PC.Name.Equals("Name")) gpu = PC.Value.ToString(); - } - } - - if (string.IsNullOrEmpty(gpu)) - { - gpu = "Unknown GPU"; - } - - if (string.IsNullOrEmpty(version)) - { - version = "Unknown Driver Version"; - } - - gpuInfo.Add(string.Format("{0} - {1}", gpu, version)); - } - } - catch (Exception) - { - // Do Nothing. We couldn't get GPU Information. - } - - return gpuInfo; - } - } } } diff --git a/win/CS/HandBrake.sln b/win/CS/HandBrake.sln index 42054c746..7365a6202 100644 --- a/win/CS/HandBrake.sln +++ b/win/CS/HandBrake.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 +# Visual Studio 14 +VisualStudioVersion = 14.0.25123.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HandBrake.ApplicationServices", "HandBrake.ApplicationServices\HandBrake.ApplicationServices.csproj", "{087A2BA8-BAC2-4577-A46F-07FF9D420016}" EndProject @@ -14,24 +14,36 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|ARM = Debug|ARM Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|ARM = Release|ARM Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Debug|Any CPU.ActiveCfg = Debug|x86 + {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Debug|ARM.ActiveCfg = Debug|x86 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Debug|x64.ActiveCfg = Debug|x64 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Debug|x64.Build.0 = Debug|x64 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Debug|x86.ActiveCfg = Debug|x86 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Debug|x86.Build.0 = Debug|x86 + {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Release|Any CPU.ActiveCfg = Release|x86 + {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Release|ARM.ActiveCfg = Release|x86 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Release|x64.ActiveCfg = Release|x64 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Release|x64.Build.0 = Release|x64 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Release|x86.ActiveCfg = Release|x86 {087A2BA8-BAC2-4577-A46F-07FF9D420016}.Release|x86.Build.0 = Release|x86 + {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Debug|Any CPU.ActiveCfg = Debug|x86 + {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Debug|ARM.ActiveCfg = Debug|x86 {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Debug|x64.ActiveCfg = Debug|x64 {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Debug|x64.Build.0 = Debug|x64 {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Debug|x86.ActiveCfg = Debug|x86 {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Debug|x86.Build.0 = Debug|x86 + {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Release|Any CPU.ActiveCfg = Release|x86 + {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Release|ARM.ActiveCfg = Release|x86 {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Release|x64.ActiveCfg = Release|x64 {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Release|x64.Build.0 = Release|x64 {DADE66CB-0E12-4959-ADE5-0ACD31D27C59}.Release|x86.ActiveCfg = Release|x86 diff --git a/win/CS/HandBrakeWPF/Converters/Video/VideoEncoderConverter.cs b/win/CS/HandBrakeWPF/Converters/Video/VideoEncoderConverter.cs index 489b787b1..b89125674 100644 --- a/win/CS/HandBrakeWPF/Converters/Video/VideoEncoderConverter.cs +++ b/win/CS/HandBrakeWPF/Converters/Video/VideoEncoderConverter.cs @@ -23,6 +23,7 @@ namespace HandBrakeWPF.Converters.Video using EncodeTask = HandBrakeWPF.Services.Encode.Model.EncodeTask; using OutputFormat = HandBrakeWPF.Services.Encode.Model.Models.OutputFormat; + using SystemInfo = HandBrake.ApplicationServices.Utilities.SystemInfo; /// /// Video Encoder Converter diff --git a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj index 5869f24bb..358c1109c 100644 --- a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj +++ b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj @@ -253,6 +253,8 @@ + + diff --git a/win/CS/HandBrakeWPF/Services/PrePostActionService.cs b/win/CS/HandBrakeWPF/Services/PrePostActionService.cs index 1d54d1e6f..4e3f4f30f 100644 --- a/win/CS/HandBrakeWPF/Services/PrePostActionService.cs +++ b/win/CS/HandBrakeWPF/Services/PrePostActionService.cs @@ -20,6 +20,7 @@ namespace HandBrakeWPF.Services using HandBrakeWPF.EventArgs; using HandBrakeWPF.Services.Interfaces; using HandBrakeWPF.Services.Queue.Interfaces; + using HandBrakeWPF.Utilities; using HandBrakeWPF.ViewModels.Interfaces; using EncodeCompletedEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs; diff --git a/win/CS/HandBrakeWPF/Utilities/SystemInfo.cs b/win/CS/HandBrakeWPF/Utilities/SystemInfo.cs new file mode 100644 index 000000000..9ed362e32 --- /dev/null +++ b/win/CS/HandBrakeWPF/Utilities/SystemInfo.cs @@ -0,0 +1,114 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// The System Information. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Utilities +{ + using System; + using System.Collections.Generic; + using System.Management; + using System.Windows.Forms; + + using HandBrake.ApplicationServices.Interop.HbLib; + + using Microsoft.Win32; + + /// + /// The System Information. + /// + public class SystemInfo + { + /// + /// Gets the total physical ram in a system + /// + /// The total memory in the system + public static ulong TotalPhysicalMemory + { + get + { + Win32.MEMORYSTATUSEX memStat = new Win32.MEMORYSTATUSEX { dwLength = 64 }; + Win32.GlobalMemoryStatusEx(ref memStat); + + ulong value = memStat.ullTotalPhys / 1024 / 1024; + return value; + } + } + + /// + /// Gets the number of CPU Cores + /// + /// Object + public static object GetCpuCount + { + get + { + RegistryKey regKey = Registry.LocalMachine; + regKey = regKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); + return regKey == null ? 0 : regKey.GetValue("ProcessorNameString"); + } + } + + /// + /// Gets the System screen size information. + /// + /// System.Windows.Forms.Scree + public static Screen ScreenBounds + { + get { return Screen.PrimaryScreen; } + } + + /// + /// Gets the get gpu driver version. + /// + public static List GetGPUInfo + { + get + { + List gpuInfo = new List(); + + try + { + ManagementObjectSearcher searcher = + new ManagementObjectSearcher("select * from " + "Win32_VideoController"); + + foreach (ManagementObject share in searcher.Get()) + { + string gpu = string.Empty, version = string.Empty; + + foreach (PropertyData PC in share.Properties) + { + if (!string.IsNullOrEmpty(PC.Name) && PC.Value != null) + { + if (PC.Name.Equals("DriverVersion")) version = PC.Value.ToString(); + if (PC.Name.Equals("Name")) gpu = PC.Value.ToString(); + } + } + + if (string.IsNullOrEmpty(gpu)) + { + gpu = "Unknown GPU"; + } + + if (string.IsNullOrEmpty(version)) + { + version = "Unknown Driver Version"; + } + + gpuInfo.Add(string.Format("{0} - {1}", gpu, version)); + } + } + catch (Exception) + { + // Do Nothing. We couldn't get GPU Information. + } + + return gpuInfo; + } + } + } +} diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/Win32.cs b/win/CS/HandBrakeWPF/Utilities/Win32.cs similarity index 95% rename from win/CS/HandBrake.ApplicationServices/Utilities/Win32.cs rename to win/CS/HandBrakeWPF/Utilities/Win32.cs index 8a870adfb..a65a5e09d 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/Win32.cs +++ b/win/CS/HandBrakeWPF/Utilities/Win32.cs @@ -1,199 +1,199 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// -// -// Win32 API calls -// -// Disable Stylecop for this file -// -------------------------------------------------------------------------------------------------------------------- - -namespace HandBrake.ApplicationServices.Utilities -{ - using System; - using System.Runtime.InteropServices; - using System.Windows.Threading; - - /// - /// Win32 API calls - /// - public class Win32 - { - /// - /// Used to force UI Thread. - /// - private static Action executor = action => action(); - - /// - /// Set the Forground Window - /// - /// - /// The h wnd. - /// - /// - /// A Boolean true when complete. - /// - [DllImport("user32.dll")] - public static extern bool SetForegroundWindow(int hWnd); - - /// - /// Lock the workstation - /// - [DllImport("user32.dll")] - public static extern void LockWorkStation(); - - /// - /// Exit Windows - /// - /// - /// The u flags. - /// - /// - /// The dw reason. - /// - /// - /// an integer - /// - [DllImport("user32.dll")] - public static extern int ExitWindowsEx(int uFlags, int dwReason); - - /// - /// Memory Status EX Struct - /// - public struct MEMORYSTATUSEX - { - public int dwLength; - - public int dwMemoryLoad; - - public ulong ullTotalPhys; - - public ulong ullAvailPhys; - - public ulong ullTotalPageFile; - - public ulong ullAvailPageFile; - - public ulong ullTotalVirtual; - - public ulong ullAvailVirtual; - - public ulong ullAvailExtendedVirtual; - } - - /// - /// Get the System Memory information - /// - /// - /// The lp buffer. - /// - /// - /// A boolean. - /// - [DllImport("kernel32.dll", SetLastError = true)] - public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); - - /// - /// Generate a Console Ctrl Event - /// - /// - /// The sigevent. - /// - /// - /// The dw process group id. - /// - /// - /// Bool true is sucess - /// - [DllImport("kernel32.dll", SetLastError = true)] - public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId); - - /// - /// Console Ctrl Event - /// - public enum ConsoleCtrlEvent - { - /// - /// Ctrl - C - /// - CTRL_C = 0, - - /// - /// Ctrl - Break - /// - CTRL_BREAK = 1, - - /// - /// Ctrl - Close - /// - CTRL_CLOSE = 2, - } - - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); - - /// - /// Execution State - /// - [Flags] - public enum EXECUTION_STATE : uint - { - ES_SYSTEM_REQUIRED = 0x00000001, - - ES_CONTINUOUS = 0x80000000, - - ES_AWAYMODE_REQUIRED = 0x00000040 - } - - /// - /// Initializes static members of the class. - /// - static Win32() - { - InitializeWithDispatcher(); - } - - /// - /// Prevent the system from sleeping - /// - public static void PreventSleep() - { - executor( - () => SetThreadExecutionState( - EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED - | EXECUTION_STATE.ES_AWAYMODE_REQUIRED)); - } - - /// - /// Allow the system to sleep. - /// - public static void AllowSleep() - { - executor(() => SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)); - } - - /// - /// Initializes the framework using the current dispatcher. - /// - public static void InitializeWithDispatcher() - { - var dispatcher = Dispatcher.CurrentDispatcher; - - SetUIThreadMarshaller(action => - { - if (dispatcher.CheckAccess()) - action(); - else dispatcher.Invoke(action); - }); - } - - /// - /// Sets a custom UI thread marshaller. - /// - /// The marshaller. - public static void SetUIThreadMarshaller(Action marshaller) - { - executor = marshaller; - } - } -} +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Win32 API calls +// +// Disable Stylecop for this file +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Utilities +{ + using System; + using System.Runtime.InteropServices; + using System.Windows.Threading; + + /// + /// Win32 API calls + /// + public class Win32 + { + /// + /// Used to force UI Thread. + /// + private static Action executor = action => action(); + + /// + /// Set the Forground Window + /// + /// + /// The h wnd. + /// + /// + /// A Boolean true when complete. + /// + [DllImport("user32.dll")] + public static extern bool SetForegroundWindow(int hWnd); + + /// + /// Lock the workstation + /// + [DllImport("user32.dll")] + public static extern void LockWorkStation(); + + /// + /// Exit Windows + /// + /// + /// The u flags. + /// + /// + /// The dw reason. + /// + /// + /// an integer + /// + [DllImport("user32.dll")] + public static extern int ExitWindowsEx(int uFlags, int dwReason); + + /// + /// Memory Status EX Struct + /// + public struct MEMORYSTATUSEX + { + public int dwLength; + + public int dwMemoryLoad; + + public ulong ullTotalPhys; + + public ulong ullAvailPhys; + + public ulong ullTotalPageFile; + + public ulong ullAvailPageFile; + + public ulong ullTotalVirtual; + + public ulong ullAvailVirtual; + + public ulong ullAvailExtendedVirtual; + } + + /// + /// Get the System Memory information + /// + /// + /// The lp buffer. + /// + /// + /// A boolean. + /// + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); + + /// + /// Generate a Console Ctrl Event + /// + /// + /// The sigevent. + /// + /// + /// The dw process group id. + /// + /// + /// Bool true is sucess + /// + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId); + + /// + /// Console Ctrl Event + /// + public enum ConsoleCtrlEvent + { + /// + /// Ctrl - C + /// + CTRL_C = 0, + + /// + /// Ctrl - Break + /// + CTRL_BREAK = 1, + + /// + /// Ctrl - Close + /// + CTRL_CLOSE = 2, + } + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); + + /// + /// Execution State + /// + [Flags] + public enum EXECUTION_STATE : uint + { + ES_SYSTEM_REQUIRED = 0x00000001, + + ES_CONTINUOUS = 0x80000000, + + ES_AWAYMODE_REQUIRED = 0x00000040 + } + + /// + /// Initializes static members of the class. + /// + static Win32() + { + InitializeWithDispatcher(); + } + + /// + /// Prevent the system from sleeping + /// + public static void PreventSleep() + { + executor( + () => SetThreadExecutionState( + EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED + | EXECUTION_STATE.ES_AWAYMODE_REQUIRED)); + } + + /// + /// Allow the system to sleep. + /// + public static void AllowSleep() + { + executor(() => SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)); + } + + /// + /// Initializes the framework using the current dispatcher. + /// + public static void InitializeWithDispatcher() + { + var dispatcher = Dispatcher.CurrentDispatcher; + + SetUIThreadMarshaller(action => + { + if (dispatcher.CheckAccess()) + action(); + else dispatcher.Invoke(action); + }); + } + + /// + /// Sets a custom UI thread marshaller. + /// + /// The marshaller. + public static void SetUIThreadMarshaller(Action marshaller) + { + executor = marshaller; + } + } +} diff --git a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs index c28eea1f8..a60be1aef 100644 --- a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs @@ -31,6 +31,7 @@ namespace HandBrakeWPF.ViewModels using Ookii.Dialogs.Wpf; using Execute = Caliburn.Micro.Execute; + using SystemInfo = HandBrake.ApplicationServices.Utilities.SystemInfo; /// /// The Options View Model -- 2.40.0