From ab758648c78b0592ac07a211beea74d5adf749ae Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 5 Feb 2015 16:44:26 -0800 Subject: [PATCH] Show and focus on the other Interface instance --- interface/src/Application.cpp | 29 +++++++++++++++++++++++++++++ interface/src/Application.h | 5 +++++ interface/src/main.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 3c1806b00b..03921ec689 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -23,6 +23,7 @@ // include this before QGLWidget, which includes an earlier version of OpenGL #include "InterfaceConfig.h" +#include #include #include #include @@ -147,6 +148,30 @@ const QString SKIP_FILENAME = QStandardPaths::writableLocation(QStandardPaths::D const QString DEFAULT_SCRIPTS_JS_URL = "http://s3.amazonaws.com/hifi-public/scripts/defaultScripts.js"; +#ifdef Q_OS_WIN +class MyNativeEventFilter : public QAbstractNativeEventFilter { +public: + static MyNativeEventFilter& getInstance() { + static MyNativeEventFilter staticInstance; + return staticInstance; + } + + bool nativeEventFilter(const QByteArray &eventType, void* msg, long* result) Q_DECL_OVERRIDE { + if (eventType == "windows_generic_MSG") { + MSG* message = (MSG*)msg; + if (message->message == UWM_IDENTIFY_INSTANCES) { + *result = UWM_IDENTIFY_INSTANCES; + return true; + } + if (message->message == WM_SHOWWINDOW) { + Application::getInstance()->getWindow()->showNormal(); + } + } + return false; + } +}; +#endif + void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { QString logMessage = LogHandler::getInstance().printMessage((LogMsgType) type, context, message); @@ -244,6 +269,10 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : _aboutToQuit(false), _notifiedPacketVersionMismatchThisDomain(false) { +#ifdef Q_OS_WIN + installNativeEventFilter(&MyNativeEventFilter::getInstance()); +#endif + _logger = new FileLogger(this); // After setting organization name in order to get correct directory qInstallMessageHandler(messageHandler); diff --git a/interface/src/Application.h b/interface/src/Application.h index 113a09e874..86fe4f1411 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -118,6 +118,11 @@ static const quint64 TOO_LONG_SINCE_LAST_SEND_DOWNSTREAM_AUDIO_STATS = 1 * USECS static const QString INFO_HELP_PATH = "html/interface-welcome-allsvg.html"; static const QString INFO_EDIT_ENTITIES_PATH = "html/edit-entities-commands.html"; +#ifdef Q_OS_WIN +static const UINT UWM_IDENTIFY_INSTANCES = + RegisterWindowMessage("UWM_IDENTIFY_INSTANCES_{8AB82783-B74A-4258-955B-8188C22AA0D6}"); +#endif + class Application; #if defined(qApp) #undef qApp diff --git a/interface/src/main.cpp b/interface/src/main.cpp index ae2ba7f576..0f20f09e25 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -17,6 +17,24 @@ #include "Application.h" +#ifdef Q_OS_WIN +static BOOL CALLBACK enumWindowsCallback(HWND hWnd, LPARAM lParam) { + const UINT TIMEOUT = 200; // ms + DWORD response; + LRESULT result = SendMessageTimeout(hWnd, UWM_IDENTIFY_INSTANCES, 0, 0, SMTO_BLOCK | SMTO_ABORTIFHUNG, TIMEOUT, &response); + if (result == 0) { // Timeout; continue search. + return TRUE; + } + if (response == UWM_IDENTIFY_INSTANCES) { + HWND* target = (HWND*)lParam; + *target = hWnd; + return FALSE; // Found; terminate search. + } + return TRUE; // Not found; continue search. +} +#endif + + int main(int argc, const char * argv[]) { #ifdef Q_OS_WIN @@ -25,6 +43,12 @@ int main(int argc, const char * argv[]) { DWORD result = GetLastError(); if (result == ERROR_ALREADY_EXISTS || result == ERROR_ACCESS_DENIED) { // Interface is already running. + HWND otherInstance = NULL; + EnumWindows(enumWindowsCallback, (LPARAM)&otherInstance); + if (otherInstance) { + ShowWindow(otherInstance, SW_RESTORE); + SetForegroundWindow(otherInstance); + } return 0; } #endif