// // main.cpp // interface/src // // Copyright 2013 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // #include #include #include #include #include #include #include #include #include #include "AddressManager.h" #include "Application.h" #include "InterfaceLogging.h" #include "MainWindow.h" #ifdef Q_OS_WIN static BOOL CALLBACK enumWindowsCallback(HWND hWnd, LPARAM lParam) { const UINT TIMEOUT = 200; // ms DWORD_PTR 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[]) { QString applicationName = "High Fidelity Interface"; // Try to create a shared memory block - if it can't be created, there is an instance of // interface already running. QSharedMemory sharedMemory { applicationName }; if (!sharedMemory.create(1, QSharedMemory::ReadOnly)) { // Connect to and send message to existing interface instance QLocalSocket socket; socket.connectToServer(applicationName); // Try to connect - if we can't connect, interface has probably just gone down if (socket.waitForConnected(100)) { QStringList arguments; for (int i = 0; i < argc; ++i) { arguments << argv[i]; } 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(); } qDebug() << "Interface instance appears to be running, exiting"; return EXIT_SUCCESS; } QElapsedTimer startupTime; startupTime.start(); // Debug option to demonstrate that the client's local time does not // need to be in sync with any other network node. This forces clock // skew for the individual client const char* CLOCK_SKEW = "--clockSkew"; const char* clockSkewOption = getCmdOption(argc, argv, CLOCK_SKEW); if (clockSkewOption) { int clockSkew = atoi(clockSkewOption); usecTimestampNowForceClockSkew(clockSkew); qCDebug(interfaceapp, "clockSkewOption=%s clockSkew=%d", clockSkewOption, clockSkew); } // Oculus initialization MUST PRECEDE OpenGL context creation. // The nature of the Application constructor means this has to be either here, // or in the main window ctor, before GL startup. Application::initPlugins(); int exitCode; { QSettings::setDefaultFormat(QSettings::IniFormat); Application app(argc, const_cast(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, [&server]() { qDebug() << "Got connection on local server"; auto socket = server.nextPendingConnection(); QObject::connect(socket, &QLocalSocket::readyRead, qApp, [&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; translator.load("i18n/interface_en"); app.installTranslator(&translator); qCDebug(interfaceapp, "Created QT Application."); exitCode = app.exec(); server.close(); } Application::shutdownPlugins(); qCDebug(interfaceapp, "Normal exit."); return exitCode; }