34
34
#include " lodepng.h"
35
35
36
36
// Tray icon
37
- #if __has_include(<tray.hpp>)
38
- #include < tray.hpp>
39
- #define TRAY_ICON
40
- #endif
37
+ #include " tray.h"
41
38
42
39
#ifndef WIN32
43
40
#define HMODULE void *
@@ -69,6 +66,10 @@ static constexpr const int mainWindowHeight = 304;
69
66
70
67
static constexpr const float bitsToGB = 1073741824 ;
71
68
69
+ GLFWwindow *glfwWindow;
70
+
71
+ bool trayQuit = false ;
72
+
72
73
#pragma region Config
73
74
#pragma region Default settings
74
75
// Initialization
@@ -86,7 +87,7 @@ int maxRes = 250;
86
87
int resIncreaseThreshold = 80 ;
87
88
int resDecreaseThreshold = 88 ;
88
89
int resIncreaseMin = 3 ;
89
- int resDecreaseMin = 6 ;
90
+ int resDecreaseMin = 5 ;
90
91
int resIncreaseScale = 140 ;
91
92
int resDecreaseScale = 140 ;
92
93
float minCpuTimeThreshold = 0 .6f ;
@@ -152,7 +153,7 @@ bool loadSettings()
152
153
if (dataAverageSamples > 128 )
153
154
dataAverageSamples = 128 ; // Max stored by OpenVR
154
155
disabledApps = ini.GetValue (" General" , " disabledApps" , disabledApps.c_str ());
155
- std::replace (disabledApps.begin (), disabledApps.end (), ' ' , ' \n ' ); // todo test
156
+ std::replace (disabledApps.begin (), disabledApps.end (), ' ' , ' \n ' );
156
157
disabledAppsSet = multilineStringToSet (disabledApps);
157
158
// Resolution
158
159
initialRes = std::stoi (ini.GetValue (" Resolution" , " initialRes" , std::to_string (initialRes).c_str ()));
@@ -316,11 +317,11 @@ bool shouldAdjustResolution(std::string appKey, bool manualRes, float cpuTime)
316
317
return !inDashboard && !isCurrentAppDisabled && !manualRes && !(resetOnThreshold && cpuTime < minCpuTimeThreshold);
317
318
}
318
319
319
- void printLine (GLFWwindow *window, std::string text, long duration)
320
+ void printLine (std::string text, long duration)
320
321
{
321
322
long startTime = getCurrentTimeMillis ();
322
323
323
- while (getCurrentTimeMillis () < startTime + duration && !glfwWindowShouldClose (window ))
324
+ while (getCurrentTimeMillis () < startTime + duration && !glfwWindowShouldClose (glfwWindow ))
324
325
{
325
326
glfwPollEvents ();
326
327
@@ -346,14 +347,14 @@ void printLine(GLFWwindow *window, std::string text, long duration)
346
347
ImGui::Render ();
347
348
ImGui_ImplOpenGL3_RenderDrawData (ImGui::GetDrawData ());
348
349
349
- glfwSwapBuffers (window );
350
+ glfwSwapBuffers (glfwWindow );
350
351
351
352
// Sleep to display the text for a set duration
352
353
std::this_thread::sleep_for (refreshIntervalBackground);
353
354
}
354
355
}
355
356
356
- void cleanup (GLFWwindow *window, nvmlLib nvmlLibrary)
357
+ void cleanup (nvmlLib nvmlLibrary)
357
358
{
358
359
// OpenVR cleanup
359
360
vr::VR_Shutdown ();
@@ -379,7 +380,7 @@ void cleanup(GLFWwindow *window, nvmlLib nvmlLibrary)
379
380
ImGui_ImplOpenGL3_Shutdown ();
380
381
ImGui_ImplGlfw_Shutdown ();
381
382
ImGui::DestroyContext ();
382
- glfwDestroyWindow (window );
383
+ glfwDestroyWindow (glfwWindow );
383
384
glfwTerminate ();
384
385
}
385
386
@@ -420,10 +421,10 @@ int main(int argc, char *argv[])
420
421
glfwWindowHint (GLFW_RESIZABLE, false );
421
422
422
423
// Create window with graphics context
423
- GLFWwindow *window = glfwCreateWindow (mainWindowWidth, mainWindowHeight, fmt::format (" OVR Dynamic Resolution {}" , version).c_str (), nullptr , nullptr );
424
- if (window == nullptr )
424
+ glfwWindow = glfwCreateWindow (mainWindowWidth, mainWindowHeight, fmt::format (" OVR Dynamic Resolution {}" , version).c_str (), nullptr , nullptr );
425
+ if (glfwWindow == nullptr )
425
426
return 1 ;
426
- glfwMakeContextCurrent (window );
427
+ glfwMakeContextCurrent (glfwWindow );
427
428
428
429
// Setup Dear ImGui context
429
430
IMGUI_CHECKVERSION ();
@@ -438,7 +439,7 @@ int main(int argc, char *argv[])
438
439
ImGui::PushStyleColor (ImGuiCol_WindowBg, ImVec4 (0.03 , 0.03 , 0.03 , 1 ));
439
440
440
441
// Setup Platform/Renderer backends
441
- ImGui_ImplGlfw_InitForOpenGL (window , true );
442
+ ImGui_ImplGlfw_InitForOpenGL (glfwWindow , true );
442
443
#ifdef __EMSCRIPTEN__
443
444
ImGui_ImplGlfw_InstallEmscriptenCallbacks (window, " #canvas" );
444
445
#endif
@@ -450,7 +451,7 @@ int main(int argc, char *argv[])
450
451
unsigned test = lodepng_decode32_file (&(icon.pixels ), &(iconWidth), &(iconHeight), iconPath);
451
452
icon.width = (int )iconWidth;
452
453
icon.height = (int )iconHeight;
453
- glfwSetWindowIcon (window , 1 , &icon);
454
+ glfwSetWindowIcon (glfwWindow , 1 , &icon);
454
455
#pragma endregion
455
456
456
457
#pragma region VR init
@@ -460,12 +461,12 @@ int main(int argc, char *argv[])
460
461
if (init_error)
461
462
{
462
463
system = nullptr ;
463
- printLine (window, VR_GetVRInitErrorAsEnglishDescription (init_error), 6000l );
464
+ printLine (VR_GetVRInitErrorAsEnglishDescription (init_error), 6000l );
464
465
return EXIT_FAILURE;
465
466
}
466
467
if (!VRCompositor ())
467
468
{
468
- printLine (window, " Failed to initialize VR compositor." , 6000l );
469
+ printLine (" Failed to initialize VR compositor." , 6000l );
469
470
return EXIT_FAILURE;
470
471
}
471
472
#pragma endregion
@@ -477,13 +478,13 @@ int main(int argc, char *argv[])
477
478
// Set auto-start
478
479
int autoStartResult = handle_setup (autoStart);
479
480
if (autoStartResult != 0 )
480
- printLine (window, fmt::format (" Error toggling auto-start ({}) " , autoStartResult), 6000l );
481
+ printLine (fmt::format (" Error toggling auto-start ({}) " , autoStartResult), 6000l );
481
482
482
483
// Minimize or hide the window according to config
483
484
if (minimizeOnStart == 1 ) // Minimize
484
- glfwIconifyWindow (window );
485
+ glfwIconifyWindow (glfwWindow );
485
486
else if (minimizeOnStart == 2 ) // Hide
486
- glfwHideWindow (window );
487
+ glfwHideWindow (glfwWindow );
487
488
488
489
// Make sure we can set resolution ourselves (Custom instead of Auto)
489
490
vr::VRSettings ()->SetInt32 (vr::k_pch_SteamVR_Section,
@@ -555,21 +556,32 @@ int main(int argc, char *argv[])
555
556
nvmlEnabled = false ;
556
557
}
557
558
#pragma endregion
558
- #if defined(TRAY_ICON)
559
- // Add tray icon
560
- Tray::Tray tray (" OVRDR" , " icon.ico" );
561
- // Construct menu
562
- tray.addEntries (Tray::Button (" Show" , [&]
563
- { glfwShowWindow (window); }),
564
- Tray::Button (" Hide" , [&]
565
- { glfwHideWindow (window); }),
566
- Tray::Separator (),
567
- Tray::Button (" Exit" , [&]
568
- { cleanup (window, nvmlLibrary); tray.exit (); return 0 ; }));
569
-
570
- // Run in a thread
571
- std::thread trayThread (&Tray::Tray::run, &tray);
572
- #endif // TRAY_ICON
559
+ #if defined(_WIN32)
560
+ const char *hideToggleText = " Hide" ;
561
+ if (minimizeOnStart == 2 )
562
+ {
563
+ hideToggleText = " Show" ;
564
+ }
565
+
566
+ tray trayInstance = {
567
+ " icon.ico" ,
568
+ " OVR Dynamic Resolution" ,
569
+ [](tray *trayInstance)
570
+ { trayInstance->menu ->text = " Hide" ; tray_update (trayInstance); glfwShowWindow (glfwWindow); },
571
+ new tray_menu_item[5 ]{
572
+ {hideToggleText, 0 , 0 , [](tray_menu_item *item)
573
+ { if (item->text == " Hide" ) { item->text = " Show" ; tray_update (tray_get_instance ()); glfwHideWindow (glfwWindow); }
574
+ else { item->text = " Hide" ; tray_update (tray_get_instance ()); glfwShowWindow (glfwWindow); } }},
575
+ {" -" , 0 , 0 , nullptr },
576
+ {" Quit" , 0 , 0 , [](tray_menu_item *item)
577
+ { trayQuit = true ; }},
578
+ {nullptr , 0 , 0 , nullptr }}};
579
+
580
+ tray_init (&trayInstance);
581
+
582
+ std::thread trayThread ([&]
583
+ { while (tray_loop (1 ) == 0 ); trayQuit = true ; });
584
+ #endif // _WIN32
573
585
574
586
// Initialize loop variables
575
587
Compositor_FrameTiming *frameTiming = new vr::Compositor_FrameTiming[dataAverageSamples];
@@ -595,7 +607,7 @@ int main(int argc, char *argv[])
595
607
bool prevAutoStart = autoStart;
596
608
597
609
// event loop
598
- while (!glfwWindowShouldClose (window ) && !openvrQuit)
610
+ while (!glfwWindowShouldClose (glfwWindow ) && !openvrQuit && !trayQuit )
599
611
{
600
612
// Get current time
601
613
long currentTime = getCurrentTimeMillis ();
@@ -828,7 +840,7 @@ int main(int argc, char *argv[])
828
840
ImGui::SameLine (0 , 10 );
829
841
if (manualRes)
830
842
{
831
- ImGui::PushItemWidth (172 );
843
+ ImGui::PushItemWidth (192 );
832
844
ImGui::PushStyleVar (ImGuiStyleVar_FramePadding, ImVec2 (0 , 0 ));
833
845
if (ImGui::SliderInt (" " , &newRes, 20 , 500 , " %d" , ImGuiSliderFlags_AlwaysClamp))
834
846
{
@@ -905,7 +917,7 @@ int main(int argc, char *argv[])
905
917
if (ImGui::CollapsingHeader (" General" ))
906
918
{
907
919
if (ImGui::InputInt (" Resolution change delay ms" , &resChangeDelayMs, 100 ))
908
- resChangeDelayMs = std::max (resChangeDelayMs, 10 );
920
+ resChangeDelayMs = std::max (resChangeDelayMs, 100 );
909
921
addTooltip (" Delay in milliseconds between resolution changes." );
910
922
911
923
if (ImGui::InputInt (" Data average samples." , &dataAverageSamples, 2 ))
@@ -1043,7 +1055,7 @@ int main(int argc, char *argv[])
1043
1055
ImGui::Render ();
1044
1056
ImGui_ImplOpenGL3_RenderDrawData (ImGui::GetDrawData ());
1045
1057
1046
- glfwSwapBuffers (window );
1058
+ glfwSwapBuffers (glfwWindow );
1047
1059
#pragma endregion
1048
1060
1049
1061
// Check if OpenVR is quitting so we can quit alongside it
@@ -1060,7 +1072,7 @@ int main(int argc, char *argv[])
1060
1072
1061
1073
// Calculate how long to sleep for depending on if the window is focused or not.
1062
1074
std::chrono::milliseconds sleepTime;
1063
- if (glfwGetWindowAttrib (window , GLFW_FOCUSED))
1075
+ if (glfwGetWindowAttrib (glfwWindow , GLFW_FOCUSED))
1064
1076
sleepTime = refreshIntervalFocused;
1065
1077
else
1066
1078
sleepTime = refreshIntervalBackground;
@@ -1069,11 +1081,11 @@ int main(int argc, char *argv[])
1069
1081
std::this_thread::sleep_for (sleepTime);
1070
1082
}
1071
1083
1072
- cleanup (window, nvmlLibrary);
1084
+ cleanup (nvmlLibrary);
1073
1085
1074
- #if defined(TRAY_ICON )
1075
- tray. exit ();
1076
- #endif // TRAY_ICON
1086
+ #if defined(_WIN32 )
1087
+ tray_exit ();
1088
+ #endif // _WIN32
1077
1089
1078
1090
return 0 ;
1079
1091
}
0 commit comments