From 12b4089c31ca109523030d58f4d9295a81b91964 Mon Sep 17 00:00:00 2001 From: AntonyCorbett Date: Sun, 23 Dec 2018 09:21:35 +0000 Subject: [PATCH] better handling of mirror window error --- OnlyM/Services/WebDisplayManager.cs | 59 ++++++++++++++++-------- OnlyMMirror/OnlyMMirror.cpp | 71 ++++++++++++++--------------- 2 files changed, 72 insertions(+), 58 deletions(-) diff --git a/OnlyM/Services/WebDisplayManager.cs b/OnlyM/Services/WebDisplayManager.cs index fd1fbb6b4..6db0d466c 100644 --- a/OnlyM/Services/WebDisplayManager.cs +++ b/OnlyM/Services/WebDisplayManager.cs @@ -37,7 +37,7 @@ internal sealed class WebDisplayManager private Process _mirrorProcess; public WebDisplayManager( - ChromiumWebBrowser browser, + ChromiumWebBrowser browser, FrameworkElement browserGrid, IDatabaseService databaseService, IOptionsService optionsService, @@ -59,7 +59,7 @@ internal sealed class WebDisplayManager public event EventHandler StatusEvent; public void ShowWeb( - string mediaItemFilePath, + string mediaItemFilePath, Guid mediaItemId, bool showMirror, ScreenPosition screenPosition) @@ -88,11 +88,11 @@ internal sealed class WebDisplayManager var urlHelper = new WebShortcutHelper(mediaItemFilePath); webAddress = urlHelper.Uri; } - + _currentMediaItemUrl = webAddress; RemoveAnimation(); - + _browserGrid.Visibility = Visibility.Visible; _browser.Load(webAddress); @@ -157,7 +157,7 @@ public void ShowMirror() Log.Logger.Debug("Cannot get monitor"); return; } - + if (mediaMonitor.MonitorId.Equals(onlyMMonitor.MonitorId)) { Log.Logger.Error("Cannot display mirror since OnlyM and Media window share a monitor"); @@ -167,31 +167,33 @@ public void ShowMirror() Log.Logger.Debug($"Main monitor = {onlyMMonitor.Monitor.DeviceName}"); Log.Logger.Debug($"Media monitor = {mediaMonitor.Monitor.DeviceName}"); - StatusEvent?.Invoke(this, new WebBrowserProgressEventArgs { Description = Properties.Resources.LAUNCHING_MIRROR }); + StatusEvent?.Invoke(this, new WebBrowserProgressEventArgs {Description = Properties.Resources.LAUNCHING_MIRROR}); try { var zoomStr = _optionsService.MirrorZoom.ToString(CultureInfo.InvariantCulture); var hotKey = _optionsService.MirrorHotKey; - var commandLineArgs = $"{onlyMMonitor.Monitor.DeviceName} {mediaMonitor.Monitor.DeviceName} {zoomStr} {hotKey}"; + var commandLineArgs = + $"{onlyMMonitor.Monitor.DeviceName} {mediaMonitor.Monitor.DeviceName} {zoomStr} {hotKey}"; Log.Logger.Debug($"Starting mirror exe with args = {commandLineArgs}"); - _mirrorProcess = Process.Start(mirrorExePath, commandLineArgs); - - if (_mirrorProcess == null) + _mirrorProcess = new Process { - Log.Logger.Error("Could not launch mirror - process is null"); - } - else if (_mirrorProcess.HasExited) - { - Log.Logger.Error($"Could not launch mirror - process exited with exit code {_mirrorProcess.ExitCode}"); - - if (_mirrorProcess.ExitCode == 5) + StartInfo = { - _snackbarService.EnqueueWithOk(Properties.Resources.CHANGE_MIRROR_HOTKEY); - } + FileName = mirrorExePath, + Arguments = commandLineArgs + }, + EnableRaisingEvents = true + }; + + _mirrorProcess.Exited += HandleMirrorProcessExited; + + if (!_mirrorProcess.Start()) + { + Log.Logger.Error("Could not launch mirror"); } } catch (Exception ex) @@ -204,7 +206,7 @@ public void ShowMirror() { DispatcherHelper.CheckBeginInvokeOnUI(() => { - StatusEvent?.Invoke(this, new WebBrowserProgressEventArgs { Description = string.Empty }); + StatusEvent?.Invoke(this, new WebBrowserProgressEventArgs {Description = string.Empty}); }); }); } @@ -399,5 +401,22 @@ private void HandleBrowserLoadError(object sender, LoadErrorEventArgs e) _browser.LoadHtml(body, e.FailedUrl); } + + private void HandleMirrorProcessExited(object sender, EventArgs e) + { + if (_mirrorProcess.ExitCode == 0) + { + Log.Logger.Debug("Mirror process closed normally"); + } + else + { + Log.Logger.Error($"Mirror process exited with exit code {_mirrorProcess.ExitCode}"); + + if (_mirrorProcess.ExitCode == 5) + { + _snackbarService.EnqueueWithOk(Properties.Resources.CHANGE_MIRROR_HOTKEY); + } + } + } } } diff --git a/OnlyMMirror/OnlyMMirror.cpp b/OnlyMMirror/OnlyMMirror.cpp index bb77fd9e6..1da48e1ae 100644 --- a/OnlyMMirror/OnlyMMirror.cpp +++ b/OnlyMMirror/OnlyMMirror.cpp @@ -66,6 +66,12 @@ int APIENTRY WinMain(_In_ HINSTANCE hInstance, return 3; } + if (!InitHotKey()) + { + // NB - this exit code is used in OnlyM (WebDisplayManager) + return 5; + } + int rv = 0; HANDLE hMutex = ::CreateMutex(0, TRUE, "OnlyMMirrorMutex"); @@ -74,54 +80,43 @@ int APIENTRY WinMain(_In_ HINSTANCE hInstance, ShowWindow(hwndHost, nCmdShow | SW_SHOWNA); UpdateWindow(hwndHost); - RECT originalRect; - ::GetWindowRect(hwndHost, &originalRect); - PositionCursor(); - if (!InitHotKey()) - { - // NB - this exit code is used in OnlyM (WebDisplayManager) - rv = 5; - } - else - { - TCHAR caption[64]; - sprintf_s(caption, "%s (ALT+%c to close)", WindowTitle, HotKey); + TCHAR caption[64]; + sprintf_s(caption, "%s (ALT+%c to close)", WindowTitle, HotKey); - ::SetWindowText(hwndHost, caption); - UINT_PTR timerId = SetTimer(hwndHost, 0, timerInterval, UpdateMirrorWindow); + ::SetWindowText(hwndHost, caption); + UINT_PTR timerId = SetTimer(hwndHost, 0, timerInterval, UpdateMirrorWindow); - MSG msg; - BOOL bRet; - while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) + MSG msg; + BOOL bRet; + while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) + { + if (bRet == -1) { - if (bRet == -1) - { - break; - } - - if (msg.message == WM_HOTKEY) - { - // we only register one hotkey - // so its value doesn't matter - break; - } - - TranslateMessage(&msg); - DispatchMessage(&msg); + break; } - // Shut down. - KillTimer(NULL, timerId); - MagUninitialize(); - - // find OnlyM window and reposition cursor over it... - RepositionCursor(); + if (msg.message == WM_HOTKEY) + { + // we only register one hotkey + // so its value doesn't matter + break; + } - rv = (int)msg.wParam; + TranslateMessage(&msg); + DispatchMessage(&msg); } + // Shut down. + KillTimer(NULL, timerId); + MagUninitialize(); + + // find OnlyM window and reposition cursor over it... + RepositionCursor(); + + rv = (int)msg.wParam; + ::CloseHandle(hMutex); } else