Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Application.cpp #939

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10,654 changes: 1,752 additions & 8,902 deletions interface/src/Application.cpp

Large diffs are not rendered by default.

1,037 changes: 527 additions & 510 deletions interface/src/Application.h

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions interface/src/ApplicationEventHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// ApplicationEventHandler.h
// interface/src
//
// Split from Application.cpp by HifiExperiments on 3/30/24
// Created by Andrzej Kapolka on 5/10/13.
// Copyright 2013 High Fidelity, Inc.
// Copyright 2020 Vircadia contributors.
// Copyright 2022-2023 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// SPDX-License-Identifier: Apache-2.0
//

#ifndef hifi_ApplicationEventHandler_h
#define hifi_ApplicationEventHandler_h

#include <QtCore/QAbstractNativeEventFilter>
#include <qsystemdetection.h>

#include <MainWindow.h>

#include "Application.h"

#ifdef Q_OS_WIN
static const UINT UWM_IDENTIFY_INSTANCES =
RegisterWindowMessage("UWM_IDENTIFY_INSTANCES_{8AB82783-B74A-4258-955B-8188C22AA0D6}_" + qgetenv("USERNAME"));
static const UINT UWM_SHOW_APPLICATION =
RegisterWindowMessage("UWM_SHOW_APPLICATION_{71123FD6-3DA8-4DC1-9C27-8A12A6250CBA}_" + qgetenv("USERNAME"));

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 == UWM_SHOW_APPLICATION) {
MainWindow* applicationWindow = qApp->getWindow();
if (applicationWindow->isMinimized()) {
applicationWindow->showNormal(); // Restores to windowed or maximized state appropriately.
}
qApp->setActiveWindow(applicationWindow); // Flashes the taskbar icon if not focus.
return true;
}

// Attempting to close MIDI interfaces of a hot-unplugged device can result in audio-driver deadlock.
// Detecting MIDI devices that have been added/removed after starting Inteface has been disabled.
// https://support.microsoft.com/en-us/help/4460006/midi-device-app-hangs-when-former-midi-api-is-used
#if 0
if (message->message == WM_DEVICECHANGE) {
const float MIN_DELTA_SECONDS = 2.0f; // de-bounce signal
static float lastTriggerTime = 0.0f;
const float deltaSeconds = secTimestampNow() - lastTriggerTime;
lastTriggerTime = secTimestampNow();
if (deltaSeconds > MIN_DELTA_SECONDS) {
Midi::USBchanged(); // re-scan the MIDI bus
}
}
#endif
}
return false;
}
};
#endif

#endif // hifi_ApplicationEventHandler_h
67 changes: 67 additions & 0 deletions interface/src/ApplicationMeshProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// ApplicationMeshProvider.h
// interface/src
//
// Split from Application.cpp by HifiExperiments on 3/30/24
// Created by Andrzej Kapolka on 5/10/13.
// Copyright 2013 High Fidelity, Inc.
// Copyright 2020 Vircadia contributors.
// Copyright 2022-2023 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// SPDX-License-Identifier: Apache-2.0
//

#ifndef hifi_ApplicationMeshProvider_h
#define hifi_ApplicationMeshProvider_h

#include <graphics/Forward.h>

class ApplicationMeshProvider : public scriptable::ModelProviderFactory {
public:
virtual scriptable::ModelProviderPointer lookupModelProvider(const QUuid& uuid) override {
bool success;
if (auto nestable = DependencyManager::get<SpatialParentFinder>()->find(uuid, success).lock()) {
auto type = nestable->getNestableType();
#ifdef SCRIPTABLE_MESH_DEBUG
qCDebug(interfaceapp) << "ApplicationMeshProvider::lookupModelProvider" << uuid << SpatiallyNestable::nestableTypeToString(type);
#endif
switch (type) {
case NestableType::Entity:
return getEntityModelProvider(static_cast<EntityItemID>(uuid));
case NestableType::Avatar:
return getAvatarModelProvider(uuid);
}
}
return nullptr;
}

private:
scriptable::ModelProviderPointer getEntityModelProvider(EntityItemID entityID) {
scriptable::ModelProviderPointer provider;
auto entityTreeRenderer = qApp->getEntities();
auto entityTree = entityTreeRenderer->getTree();
if (auto entity = entityTree->findEntityByID(entityID)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some places, for example in EntitySendThread there's withReadLock around findEntityID call. getEntityModelProvider is called from Graphics.canUpdateModel so it can be called from any script engine thread. I wonder if it needs a lock here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe as a later PR we should do a thread safety audit of all the places where entity tree is used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow great catch. EntityTree::findEntityByID actually calls EntityTree::findEntityByEntityItemID which does internally use a read lock, but then EntityTreeRenderer::renderableForEntityId looks like it needs it? I filed #1015

if (auto renderer = entityTreeRenderer->renderableForEntityId(entityID)) {
provider = std::dynamic_pointer_cast<scriptable::ModelProvider>(renderer);
provider->modelProviderType = NestableType::Entity;
} else {
qCWarning(interfaceapp) << "no renderer for entity ID" << entityID.toString();
}
}
return provider;
}

scriptable::ModelProviderPointer getAvatarModelProvider(QUuid sessionUUID) {
scriptable::ModelProviderPointer provider;
auto avatarManager = DependencyManager::get<AvatarManager>();
if (auto avatar = avatarManager->getAvatarBySessionID(sessionUUID)) {
provider = std::dynamic_pointer_cast<scriptable::ModelProvider>(avatar);
provider->modelProviderType = NestableType::Avatar;
}
return provider;
}
};

#endif // hifi_ApplicationMeshProvider_h
Loading
Loading