Update interface to support --url on all platforms

This commit is contained in:
Ryan Huffman 2015-12-22 16:27:33 -08:00
parent 2f45d0dff5
commit c396caee5e

View file

@ -11,7 +11,10 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QLocalSocket>
#include <QLocalServer>
#include <QSettings> #include <QSettings>
#include <QSharedMemory>
#include <QTranslator> #include <QTranslator>
#include <SharedUtil.h> #include <SharedUtil.h>
@ -19,6 +22,7 @@
#include "AddressManager.h" #include "AddressManager.h"
#include "Application.h" #include "Application.h"
#include "InterfaceLogging.h" #include "InterfaceLogging.h"
#include "MainWindow.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
static BOOL CALLBACK enumWindowsCallback(HWND hWnd, LPARAM lParam) { static BOOL CALLBACK enumWindowsCallback(HWND hWnd, LPARAM lParam) {
@ -38,46 +42,46 @@ static BOOL CALLBACK enumWindowsCallback(HWND hWnd, LPARAM lParam) {
#endif #endif
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
#ifdef Q_OS_WIN QString applicationName = "High Fidelity Interface";
// Run only one instance of Interface at a time.
HANDLE mutex = CreateMutex(NULL, FALSE, "High Fidelity Interface - " + qgetenv("USERNAME"));
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) {
// Show other instance.
SendMessage(otherInstance, UWM_SHOW_APPLICATION, 0, 0);
// Send command line --url value to other instance. // Try to create a shared memory block - if it can't be created, there is an instance of
if (argc >= 3) { // interface already running.
QStringList arguments; QSharedMemory sharedMemory { applicationName };
for (int i = 0; i < argc; i += 1) { if (!sharedMemory.create(1, QSharedMemory::ReadOnly)) {
arguments << argv[i]; // Connect to and send message to existing interface instance
} QLocalSocket socket;
QCommandLineParser parser; socket.connectToServer(applicationName);
QCommandLineOption urlOption("url", "", "value");
parser.addOption(urlOption);
parser.process(arguments);
if (parser.isSet(urlOption)) { // Try to connect - if we can't connect, interface has probably just gone down
QUrl url = QUrl(parser.value(urlOption)); if (socket.waitForConnected(100)) {
if (url.isValid() && url.scheme() == HIFI_URL_SCHEME) {
QByteArray urlBytes = url.toString().toLatin1(); QStringList arguments;
const char* urlChars = urlBytes.data(); for (int i = 0; i < argc; ++i) {
COPYDATASTRUCT cds; arguments << argv[i];
cds.cbData = urlBytes.length() + 1; }
cds.lpData = (PVOID)urlChars;
SendMessage(otherInstance, WM_COPYDATA, 0, (LPARAM)&cds); QCommandLineParser parser;
} QCommandLineOption urlOption("url", "", "value");
parser.addOption(urlOption);
parser.process(arguments);
if (parser.isSet(urlOption)) {
QUrl url = QUrl(parser.value(urlOption));
if (url.isValid() && url.scheme() == HIFI_URL_SCHEME) {
socket.write(url.toString().toUtf8());
socket.waitForBytesWritten(5000);
} }
} }
socket.close();
} }
return 0;
qDebug() << "Interface instance appears to be running, exiting";
return EXIT_SUCCESS;
} }
#endif
QElapsedTimer startupTime; QElapsedTimer startupTime;
startupTime.start(); startupTime.start();
@ -102,18 +106,45 @@ int main(int argc, const char* argv[]) {
QSettings::setDefaultFormat(QSettings::IniFormat); QSettings::setDefaultFormat(QSettings::IniFormat);
Application app(argc, const_cast<char**>(argv), startupTime); Application app(argc, const_cast<char**>(argv), startupTime);
// Setup local server
QLocalServer server { &app };
// We have already acquired the shared memory block, so we can safely remove
// any existing servers. This can occur on Unix platforms after a crash.
server.removeServer(applicationName);
server.listen(applicationName);
QObject::connect(&server, &QLocalServer::newConnection, qApp, [&app, &server]() {
qDebug() << "Got connection on local server";
auto socket = server.nextPendingConnection();
QObject::connect(socket, &QLocalSocket::readyRead, qApp, [&app, socket]() {
auto message = socket->readAll();
socket->close();
qDebug() << "Read from connection: " << message;
// If we received a message, try to open it as a URL
if (message.length() > 0) {
qApp->openUrl(QString::fromUtf8(message));
}
});
qApp->getWindow()->raise();
qApp->getWindow()->activateWindow();
});
QTranslator translator; QTranslator translator;
translator.load("i18n/interface_en"); translator.load("i18n/interface_en");
app.installTranslator(&translator); app.installTranslator(&translator);
qCDebug(interfaceapp, "Created QT Application."); qCDebug(interfaceapp, "Created QT Application.");
exitCode = app.exec(); exitCode = app.exec();
server.close();
} }
Application::shutdownPlugins(); Application::shutdownPlugins();
#ifdef Q_OS_WIN
ReleaseMutex(mutex);
#endif
qCDebug(interfaceapp, "Normal exit."); qCDebug(interfaceapp, "Normal exit.");
return exitCode; return exitCode;