4272 lines · 168.0 KB · C++
RawDownload
////  Application.cpp//  interface/src////  Created by Andrzej Kapolka on 5/10/13.//  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 <sstream> #include <stdlib.h>#include <cmath>#include <math.h> #include <glm/glm.hpp>#include <glm/gtx/component_wise.hpp>#include <glm/gtx/quaternion.hpp>#include <glm/gtx/vector_angle.hpp> // include this before QGLWidget, which includes an earlier version of OpenGL#include "InterfaceConfig.h" #include <QAbstractNativeEventFilter>#include <QActionGroup>#include <QColorDialog>#include <QDesktopWidget>#include <QCheckBox>#include <QImage>#include <QInputDialog>#include <QKeyEvent>#include <QMenuBar>#include <QMouseEvent>#include <QNetworkReply>#include <QNetworkDiskCache>#include <QOpenGLFramebufferObject>#include <QObject>#include <QWheelEvent>#include <QScreen>#include <QShortcut>#include <QSystemTrayIcon>#include <QTimer>#include <QUrl>#include <QWindow>#include <QtDebug>#include <QFileDialog>#include <QDesktopServices>#include <QXmlStreamReader>#include <QXmlStreamAttributes>#include <QMediaPlayer>#include <QMimeData>#include <QMessageBox>#include <QJsonDocument> #include <AddressManager.h>#include <AccountManager.h>#include <AmbientOcclusionEffect.h>#include <AudioInjector.h>#include <DeferredLightingEffect.h>#include <DependencyManager.h>#include <EntityScriptingInterface.h>#include <GlowEffect.h>#include <HFActionEvent.h>#include <HFBackEvent.h>#include <LogHandler.h>#include <MainWindow.h>#include <ModelEntityItem.h>#include <NetworkAccessManager.h>#include <NetworkingConstants.h>#include <OctalCode.h>#include <OctreeSceneStats.h>#include <PacketHeaders.h>#include <PathUtils.h>#include <PerfStat.h>#include <PhysicsEngine.h>#include <ProgramObject.h>#include <ResourceCache.h>#include <ScriptCache.h>#include <SettingHandle.h>#include <SoundCache.h>#include <TextRenderer.h>#include <UserActivityLogger.h>#include <UUID.h> #include <SceneScriptingInterface.h> #include "Application.h"#include "AudioClient.h"#include "DiscoverabilityManager.h"#include "InterfaceVersion.h"#include "LODManager.h"#include "Menu.h"#include "ModelPackager.h"#include "Util.h" #include "avatar/AvatarManager.h" #include "audio/AudioToolBox.h"#include "audio/AudioIOStatsRenderer.h"#include "audio/AudioScope.h" #include "devices/DdeFaceTracker.h"#include "devices/Faceshift.h"#include "devices/Leapmotion.h"#include "devices/RealSense.h"#include "devices/MIDIManager.h"#include "devices/OculusManager.h"#include "devices/TV3DManager.h"#include "devices/Visage.h" #include "gpu/Batch.h"#include "gpu/GLBackend.h" #include "scripting/AccountScriptingInterface.h"#include "scripting/AudioDeviceScriptingInterface.h"#include "scripting/ClipboardScriptingInterface.h"#include "scripting/HMDScriptingInterface.h"#include "scripting/JoystickScriptingInterface.h"#include "scripting/GlobalServicesScriptingInterface.h"#include "scripting/LocationScriptingInterface.h"#include "scripting/MenuScriptingInterface.h"#include "scripting/SettingsScriptingInterface.h"#include "scripting/WindowScriptingInterface.h"#include "scripting/WebWindowClass.h" #if defined(Q_OS_MAC) || defined(Q_OS_WIN)#include "SpeechRecognizer.h"#endif #include "ui/DataWebDialog.h"#include "ui/DialogsManager.h"#include "ui/InfoView.h"#include "ui/LoginDialog.h"#include "ui/Snapshot.h"#include "ui/StandAloneJSConsole.h"#include "ui/Stats.h" // ON WIndows PC, NVidia Optimus laptop, we want to enable NVIDIA GPU#if defined(Q_OS_WIN)extern "C" { _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;}#endif using namespace std; //  Starfield informationstatic unsigned STARFIELD_NUM_STARS = 50000;static unsigned STARFIELD_SEED = 1; const qint64 MAXIMUM_CACHE_SIZE = 10 * BYTES_PER_GIGABYTES;  // 10GB static QTimer* locationUpdateTimer = NULL;static QTimer* balanceUpdateTimer = NULL;static QTimer* identityPacketTimer = NULL;static QTimer* billboardPacketTimer = NULL;static QTimer* checkFPStimer = NULL;static QTimer* idleTimer = NULL; const QString CHECK_VERSION_URL = "https://highfidelity.com/latestVersion.xml";const QString SKIP_FILENAME = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/hifi.skipversion"; const QString DEFAULT_SCRIPTS_JS_URL = "http://s3.amazonaws.com/hifi-public/scripts/defaultScripts.js"; #ifdef Q_OS_WINclass 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 == UWM_SHOW_APPLICATION) {                MainWindow* applicationWindow = Application::getInstance()->getWindow();                if (applicationWindow->isMinimized()) {                    applicationWindow->showNormal();  // Restores to windowed or maximized state appropriately.                }                Application::getInstance()->setActiveWindow(applicationWindow);  // Flashes the taskbar icon if not focus.                return true;            }             if (message->message == WM_COPYDATA) {                COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)(message->lParam);                QUrl url = QUrl((const char*)(pcds->lpData));                if (url.isValid() && url.scheme() == HIFI_URL_SCHEME) {                    DependencyManager::get<AddressManager>()->handleLookupString(url.toString());                    return true;                }            }        }        return false;    }};#endif void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {    QString logMessage = LogHandler::getInstance().printMessage((LogMsgType) type, context, message);        if (!logMessage.isEmpty()) {        Application::getInstance()->getLogger()->addMessage(qPrintable(logMessage + "\n"));    }} bool setupEssentials(int& argc, char** argv) {    unsigned int listenPort = 0; // bind to an ephemeral port by default    const char** constArgv = const_cast<const char**>(argv);    const char* portStr = getCmdOption(argc, constArgv, "--listenPort");    if (portStr) {        listenPort = atoi(portStr);    }    // Set build version    QCoreApplication::setApplicationVersion(BUILD_VERSION);        DependencyManager::registerInheritance<LimitedNodeList, NodeList>();    DependencyManager::registerInheritance<AvatarHashMap, AvatarManager>();        // Set dependencies    auto addressManager = DependencyManager::set<AddressManager>();    auto nodeList = DependencyManager::set<NodeList>(NodeType::Agent, listenPort);    auto geometryCache = DependencyManager::set<GeometryCache>();    auto scriptCache = DependencyManager::set<ScriptCache>();    auto soundCache = DependencyManager::set<SoundCache>();    auto glowEffect = DependencyManager::set<GlowEffect>();    auto faceshift = DependencyManager::set<Faceshift>();    auto audio = DependencyManager::set<AudioClient>();    auto audioScope = DependencyManager::set<AudioScope>();    auto audioIOStatsRenderer = DependencyManager::set<AudioIOStatsRenderer>();    auto deferredLightingEffect = DependencyManager::set<DeferredLightingEffect>();    auto ambientOcclusionEffect = DependencyManager::set<AmbientOcclusionEffect>();    auto textureCache = DependencyManager::set<TextureCache>();    auto animationCache = DependencyManager::set<AnimationCache>();    auto visage = DependencyManager::set<Visage>();    auto ddeFaceTracker = DependencyManager::set<DdeFaceTracker>();    auto modelBlender = DependencyManager::set<ModelBlender>();    auto audioToolBox = DependencyManager::set<AudioToolBox>();    auto avatarManager = DependencyManager::set<AvatarManager>();    auto lodManager = DependencyManager::set<LODManager>();    auto jsConsole = DependencyManager::set<StandAloneJSConsole>();    auto dialogsManager = DependencyManager::set<DialogsManager>();    auto bandwidthRecorder = DependencyManager::set<BandwidthRecorder>();    auto resourceCacheSharedItems = DependencyManager::set<ResourceCacheSharedItems>();    auto entityScriptingInterface = DependencyManager::set<EntityScriptingInterface>();    auto windowScriptingInterface = DependencyManager::set<WindowScriptingInterface>();#if defined(Q_OS_MAC) || defined(Q_OS_WIN)    auto speechRecognizer = DependencyManager::set<SpeechRecognizer>();#endif    auto discoverabilityManager = DependencyManager::set<DiscoverabilityManager>();        return true;} Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) :        QApplication(argc, argv),        _dependencyManagerIsSetup(setupEssentials(argc, argv)),        _window(new MainWindow(desktop())),        _toolWindow(NULL),        _datagramProcessor(),        _undoStack(),        _undoStackScriptingInterface(&_undoStack),        _frameCount(0),        _fps(60.0f),        _justStarted(true),        _physicsEngine(glm::vec3(0.0f)),        _entities(true, this, this),        _entityClipboardRenderer(false, this, this),        _entityClipboard(),        _viewFrustum(),        _lastQueriedViewFrustum(),        _lastQueriedTime(usecTimestampNow()),        _mirrorViewRect(QRect(MIRROR_VIEW_LEFT_PADDING, MIRROR_VIEW_TOP_PADDING, MIRROR_VIEW_WIDTH, MIRROR_VIEW_HEIGHT)),        _firstRun("firstRun", true),        _previousScriptLocation("LastScriptLocation"),        _scriptsLocationHandle("scriptsLocation"),        _fieldOfView("fieldOfView", DEFAULT_FIELD_OF_VIEW_DEGREES),        _viewTransform(),        _scaleMirror(1.0f),        _rotateMirror(0.0f),        _raiseMirror(0.0f),        _cursorVisible(true),        _lastMouseMove(usecTimestampNow()),        _lastMouseMoveWasSimulated(false),        _touchAvgX(0.0f),        _touchAvgY(0.0f),        _isTouchPressed(false),        _mousePressed(false),        _enableProcessOctreeThread(true),        _octreeProcessor(),        _nodeBoundsDisplay(this),        _applicationOverlay(),        _runningScriptsWidget(NULL),        _runningScriptsWidgetWasVisible(false),        _trayIcon(new QSystemTrayIcon(_window)),        _lastNackTime(usecTimestampNow()),        _lastSendDownstreamAudioStats(usecTimestampNow()),        _isVSyncOn(true),        _aboutToQuit(false),        _notifiedPacketVersionMismatchThisDomain(false),        _domainConnectionRefusals(QList<QString>()){#ifdef Q_OS_WIN    installNativeEventFilter(&MyNativeEventFilter::getInstance());#endif     _logger = new FileLogger(this);  // After setting organization name in order to get correct directory    qInstallMessageHandler(messageHandler);     QFontDatabase::addApplicationFont(PathUtils::resourcesPath() + "styles/Inconsolata.otf");    _window->setWindowTitle("Interface");        Model::setAbstractViewStateInterface(this); // The model class will sometimes need to know view state details from us        auto nodeList = DependencyManager::get<NodeList>();     _myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();     _applicationStartupTime = startup_time;     qDebug() << "[VERSION] Build sequence: " << qPrintable(applicationVersion());     _bookmarks = new Bookmarks();  // Before setting up the menu     // call Menu getInstance static method to set up the menu    _window->setMenuBar(Menu::getInstance());     _runningScriptsWidget = new RunningScriptsWidget(_window);        // start the nodeThread so its event loop is running    QThread* nodeThread = new QThread(this);    nodeThread->setObjectName("Datagram Processor Thread");    nodeThread->start();     // make sure the node thread is given highest priority    nodeThread->setPriority(QThread::TimeCriticalPriority);        _datagramProcessor = new DatagramProcessor(nodeList.data());        // have the NodeList use deleteLater from DM customDeleter    nodeList->setCustomDeleter([](Dependency* dependency) {        static_cast<NodeList*>(dependency)->deleteLater();    });        // put the NodeList and datagram processing on the node thread    nodeList->moveToThread(nodeThread);     // geometry background downloads need to happen on the Datagram Processor Thread.  The idle loop will    // emit checkBackgroundDownloads to cause the GeometryCache to check it's queue for requested background    // downloads.    QSharedPointer<GeometryCache> geometryCacheP = DependencyManager::get<GeometryCache>();    ResourceCache *geometryCache = geometryCacheP.data();    connect(this, &Application::checkBackgroundDownloads, geometryCache, &ResourceCache::checkAsynchronousGets);     // connect the DataProcessor processDatagrams slot to the QUDPSocket readyRead() signal    connect(&nodeList->getNodeSocket(), &QUdpSocket::readyRead, _datagramProcessor, &DatagramProcessor::processDatagrams);     // put the audio processing on a separate thread    QThread* audioThread = new QThread();    audioThread->setObjectName("Audio Thread");        auto audioIO = DependencyManager::get<AudioClient>();        audioIO->setPositionGetter(getPositionForAudio);    audioIO->setOrientationGetter(getOrientationForAudio);        audioIO->moveToThread(audioThread);    connect(audioThread, &QThread::started, audioIO.data(), &AudioClient::start);    connect(audioIO.data(), &AudioClient::destroyed, audioThread, &QThread::quit);    connect(audioThread, &QThread::finished, audioThread, &QThread::deleteLater);    connect(audioIO.data(), &AudioClient::muteToggled, this, &Application::audioMuteToggled);     audioThread->start();        const DomainHandler& domainHandler = nodeList->getDomainHandler();     connect(&domainHandler, SIGNAL(hostnameChanged(const QString&)), SLOT(domainChanged(const QString&)));    connect(&domainHandler, SIGNAL(connectedToDomain(const QString&)), SLOT(connectedToDomain(const QString&)));    connect(&domainHandler, SIGNAL(connectedToDomain(const QString&)), SLOT(updateWindowTitle()));    connect(&domainHandler, SIGNAL(disconnectedFromDomain()), SLOT(updateWindowTitle()));    connect(&domainHandler, SIGNAL(disconnectedFromDomain()), SLOT(clearDomainOctreeDetails()));    connect(&domainHandler, &DomainHandler::settingsReceived, this, &Application::domainSettingsReceived);    connect(&domainHandler, &DomainHandler::hostnameChanged,            DependencyManager::get<AddressManager>().data(), &AddressManager::storeCurrentAddress);     // update our location every 5 seconds in the metaverse server, assuming that we are authenticated with one    const qint64 DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS = 5 * 1000;     locationUpdateTimer = new QTimer(this);    auto discoverabilityManager = DependencyManager::get<DiscoverabilityManager>();    connect(locationUpdateTimer, &QTimer::timeout, discoverabilityManager.data(), &DiscoverabilityManager::updateLocation);    locationUpdateTimer->start(DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS);        // if we get a domain change, immediately attempt update location in metaverse server    connect(&nodeList->getDomainHandler(), &DomainHandler::connectedToDomain,            discoverabilityManager.data(), &DiscoverabilityManager::updateLocation);     connect(nodeList.data(), &NodeList::nodeAdded, this, &Application::nodeAdded);    connect(nodeList.data(), &NodeList::nodeKilled, this, &Application::nodeKilled);    connect(nodeList.data(), SIGNAL(nodeKilled(SharedNodePointer)), SLOT(nodeKilled(SharedNodePointer)));    connect(nodeList.data(), &NodeList::uuidChanged, _myAvatar, &MyAvatar::setSessionUUID);    connect(nodeList.data(), &NodeList::limitOfSilentDomainCheckInsReached, nodeList.data(), &NodeList::reset);    connect(nodeList.data(), &NodeList::packetVersionMismatch, this, &Application::notifyPacketVersionMismatch);     // connect to appropriate slots on AccountManager    AccountManager& accountManager = AccountManager::getInstance();     const qint64 BALANCE_UPDATE_INTERVAL_MSECS = 5 * 1000;     balanceUpdateTimer = new QTimer(this);    connect(balanceUpdateTimer, &QTimer::timeout, &accountManager, &AccountManager::updateBalance);    balanceUpdateTimer->start(BALANCE_UPDATE_INTERVAL_MSECS);     connect(&accountManager, &AccountManager::balanceChanged, this, &Application::updateWindowTitle);     auto dialogsManager = DependencyManager::get<DialogsManager>();    connect(&accountManager, &AccountManager::authRequired, dialogsManager.data(), &DialogsManager::showLoginDialog);    connect(&accountManager, &AccountManager::usernameChanged, this, &Application::updateWindowTitle);     // set the account manager's root URL and trigger a login request if we don't have the access token    accountManager.setAuthURL(NetworkingConstants::METAVERSE_SERVER_URL);    UserActivityLogger::getInstance().launch(applicationVersion());     // once the event loop has started, check and signal for an access token    QMetaObject::invokeMethod(&accountManager, "checkAndSignalForAccessToken", Qt::QueuedConnection);        auto addressManager = DependencyManager::get<AddressManager>();        // use our MyAvatar position and quat for address manager path    addressManager->setPositionGetter(getPositionForPath);    addressManager->setOrientationGetter(getOrientationForPath);        connect(addressManager.data(), &AddressManager::rootPlaceNameChanged, this, &Application::updateWindowTitle);     #ifdef _WIN32    WSADATA WsaData;    int wsaresult = WSAStartup(MAKEWORD(2,2), &WsaData);    #endif     // tell the NodeList instance who to tell the domain server we care about    nodeList->addSetOfNodeTypesToNodeInterestSet(NodeSet() << NodeType::AudioMixer << NodeType::AvatarMixer                                                 << NodeType::EntityServer);     // connect to the packet sent signal of the _entityEditSender    connect(&_entityEditSender, &EntityEditPacketSender::packetSent, this, &Application::packetSent);     // send the identity packet for our avatar each second to our avatar mixer    identityPacketTimer = new QTimer();    connect(identityPacketTimer, &QTimer::timeout, _myAvatar, &MyAvatar::sendIdentityPacket);    identityPacketTimer->start(AVATAR_IDENTITY_PACKET_SEND_INTERVAL_MSECS);     // send the billboard packet for our avatar every few seconds    billboardPacketTimer = new QTimer();    connect(billboardPacketTimer, &QTimer::timeout, _myAvatar, &MyAvatar::sendBillboardPacket);    billboardPacketTimer->start(AVATAR_BILLBOARD_PACKET_SEND_INTERVAL_MSECS);     QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);    QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();    QNetworkDiskCache* cache = new QNetworkDiskCache();    cache->setMaximumCacheSize(MAXIMUM_CACHE_SIZE);    cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "interfaceCache");    networkAccessManager.setCache(cache);        ResourceCache::setRequestLimit(3);     _window->setCentralWidget(_glWidget);     _window->restoreGeometry();     _window->setVisible(true);    _glWidget->setFocusPolicy(Qt::StrongFocus);    _glWidget->setFocus();     // enable mouse tracking; otherwise, we only get drag events    _glWidget->setMouseTracking(true);     _fullscreenMenuWidget->setParent(_glWidget);    _menuBarHeight = Menu::getInstance()->height();    if (Menu::getInstance()->isOptionChecked(MenuOption::Fullscreen)) {        setFullscreen(true);  // Initialize menu bar show/hide    }     _toolWindow = new ToolWindow();    _toolWindow->setWindowFlags(_toolWindow->windowFlags() | Qt::WindowStaysOnTopHint);    _toolWindow->setWindowTitle("Tools");     // initialization continues in initializeGL when OpenGL context is ready     // Tell our entity edit sender about our known jurisdictions    _entityEditSender.setServerJurisdictions(&_entityServerJurisdictions);     // For now we're going to set the PPS for outbound packets to be super high, this is

Lines 1–500 of 4272.