Skip to content

Commit

Permalink
better handling of mirror window error
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyCorbett committed Dec 23, 2018
1 parent 010f7bc commit 12b4089
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 58 deletions.
59 changes: 39 additions & 20 deletions OnlyM/Services/WebDisplayManager.cs
Expand Up @@ -37,7 +37,7 @@ internal sealed class WebDisplayManager
private Process _mirrorProcess;

public WebDisplayManager(
ChromiumWebBrowser browser,
ChromiumWebBrowser browser,
FrameworkElement browserGrid,
IDatabaseService databaseService,
IOptionsService optionsService,
Expand All @@ -59,7 +59,7 @@ internal sealed class WebDisplayManager
public event EventHandler<WebBrowserProgressEventArgs> StatusEvent;

public void ShowWeb(
string mediaItemFilePath,
string mediaItemFilePath,
Guid mediaItemId,
bool showMirror,
ScreenPosition screenPosition)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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)
Expand All @@ -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});
});
});
}
Expand Down Expand Up @@ -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);
}
}
}
}
}
71 changes: 33 additions & 38 deletions OnlyMMirror/OnlyMMirror.cpp
Expand Up @@ -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");
Expand All @@ -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
Expand Down

0 comments on commit 12b4089

Please sign in to comment.