Overte C++ Documentation
ApplicationEventHandler.h
1 //
2 // ApplicationEventHandler.h
3 // interface/src
4 //
5 // Split from Application.cpp by HifiExperiments on 3/30/24
6 // Created by Andrzej Kapolka on 5/10/13.
7 // Copyright 2013 High Fidelity, Inc.
8 // Copyright 2020 Vircadia contributors.
9 // Copyright 2022-2023 Overte e.V.
10 //
11 // Distributed under the Apache License, Version 2.0.
12 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
13 // SPDX-License-Identifier: Apache-2.0
14 //
15 
16 #ifndef hifi_ApplicationEventHandler_h
17 #define hifi_ApplicationEventHandler_h
18 
19 #include <QtCore/QAbstractNativeEventFilter>
20 #include <qsystemdetection.h>
21 
22 #include <MainWindow.h>
23 
24 #include "Application.h"
25 
26 #ifdef Q_OS_WIN
27 static const UINT UWM_IDENTIFY_INSTANCES =
28  RegisterWindowMessage("UWM_IDENTIFY_INSTANCES_{8AB82783-B74A-4258-955B-8188C22AA0D6}_" + qgetenv("USERNAME"));
29 static const UINT UWM_SHOW_APPLICATION =
30  RegisterWindowMessage("UWM_SHOW_APPLICATION_{71123FD6-3DA8-4DC1-9C27-8A12A6250CBA}_" + qgetenv("USERNAME"));
31 
32 class MyNativeEventFilter : public QAbstractNativeEventFilter {
33 public:
34  static MyNativeEventFilter& getInstance() {
35  static MyNativeEventFilter staticInstance;
36  return staticInstance;
37  }
38 
39  bool nativeEventFilter(const QByteArray &eventType, void* msg, long* result) Q_DECL_OVERRIDE {
40  if (eventType == "windows_generic_MSG") {
41  MSG* message = (MSG*)msg;
42 
43  if (message->message == UWM_IDENTIFY_INSTANCES) {
44  *result = UWM_IDENTIFY_INSTANCES;
45  return true;
46  }
47 
48  if (message->message == UWM_SHOW_APPLICATION) {
49  MainWindow* applicationWindow = qApp->getWindow();
50  if (applicationWindow->isMinimized()) {
51  applicationWindow->showNormal(); // Restores to windowed or maximized state appropriately.
52  }
53  qApp->setActiveWindow(applicationWindow); // Flashes the taskbar icon if not focus.
54  return true;
55  }
56 
57  // Attempting to close MIDI interfaces of a hot-unplugged device can result in audio-driver deadlock.
58  // Detecting MIDI devices that have been added/removed after starting Inteface has been disabled.
59  // https://support.microsoft.com/en-us/help/4460006/midi-device-app-hangs-when-former-midi-api-is-used
60 #if 0
61  if (message->message == WM_DEVICECHANGE) {
62  const float MIN_DELTA_SECONDS = 2.0f; // de-bounce signal
63  static float lastTriggerTime = 0.0f;
64  const float deltaSeconds = secTimestampNow() - lastTriggerTime;
65  lastTriggerTime = secTimestampNow();
66  if (deltaSeconds > MIN_DELTA_SECONDS) {
67  Midi::USBchanged(); // re-scan the MIDI bus
68  }
69  }
70 #endif
71  }
72  return false;
73  }
74 };
75 #endif
76 
77 #endif // hifi_ApplicationEventHandler_h