Skip to content

Commit cb00407

Browse files
committed
Manual resolution slider for when paused
closes #38
1 parent ed00469 commit cb00407

File tree

6 files changed

+289
-61
lines changed

6 files changed

+289
-61
lines changed

CMakeLists.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ CPMAddPackage(
8080
add_library(lodepng STATIC "${lodepng_SOURCE_DIR}/lodepng.cpp")
8181
target_include_directories(lodepng PUBLIC "${lodepng_SOURCE_DIR}")
8282

83-
IF(WIN32)
84-
CPMAddPackage(
85-
NAME tray
86-
URL https://github.com/Soundux/traypp/archive/698db7d58dd450cc9e30dc12d3bd0c5ca4d6a5b1.zip
87-
)
88-
ENDIF()
89-
9083
set(CMAKE_SKIP_BUILD_RPATH FALSE)
9184
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
9285
set(CMAKE_INSTALL_RPATH $ORIGIN)
@@ -107,16 +100,12 @@ if(WIN32)
107100
link_directories("${OPENVR_CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
108101
endif()
109102
if(WIN32)
110-
add_executable("${PROJECT_NAME}" ${GUI_TYPE} "src/main.cpp" "src/pathtools_excerpt.cpp" "src/setup.cpp")
103+
add_executable("${PROJECT_NAME}" ${GUI_TYPE} "src/main.cpp" "src/pathtools_excerpt.cpp" "src/setup.cpp" "src/tray_windows.c")
111104
else()
112105
add_executable("${PROJECT_NAME}" ${GUI_TYPE} "src/main.cpp" "src/setup.cpp")
113106
endif()
114107

115108
target_link_libraries("${PROJECT_NAME}" openvr_api fmt::fmt-header-only simpleini imgui lodepng Threads::Threads)
116-
if(TARGET tray)
117-
target_link_libraries("${PROJECT_NAME}" tray)
118-
endif()
119-
120109
target_include_directories("${PROJECT_NAME}" PRIVATE ${CMAKE_CURRENT_BINARY_DIR} PUBLIC "${openvr_SOURCE_DIR}/headers")
121110
target_compile_features("${PROJECT_NAME}" PRIVATE cxx_std_17)
122111

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ We also use:
104104
- The [fmt](https://github.com/fmtlib/fmt) library which is available under a Boost-like license.
105105
- [DearImGui](https://github.com/ocornut/imgui) which is available under the MIT license.
106106
- [lodepng](https://github.com/lvandeve/lodepng/blob/master/LICENSE) which is available under the zlib license.
107-
- [Traypp](https://github.com/Soundux/traypp) which is available under the MIT license.
107+
- A [tray fork](https://github.com/dmikushin/tray) which is available under the MIT license.

resources/ThirdPartyLicenses.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ freely, subject to the following restrictions:
154154
3. This notice may not be removed or altered from any source
155155
distribution.
156156

157-
---------- lodepng License ----------
157+
---------- traypp License ----------
158158
MIT License
159159

160-
Copyright (c) 2021 Soundux
160+
Copyright (c) 2017 Serge Zaitsev
161161

162162
Permission is hereby granted, free of charge, to any person obtaining a copy
163163
of this software and associated documentation files (the "Software"), to deal
@@ -175,4 +175,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
175175
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
176176
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
177177
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
178-
SOFTWARE.
178+
SOFTWARE.

src/main.cpp

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434
#include "lodepng.h"
3535

3636
// Tray icon
37-
#if __has_include(<tray.hpp>)
38-
#include <tray.hpp>
39-
#define TRAY_ICON
40-
#endif
37+
#include "tray.h"
4138

4239
#ifndef WIN32
4340
#define HMODULE void *
@@ -69,6 +66,10 @@ static constexpr const int mainWindowHeight = 304;
6966

7067
static constexpr const float bitsToGB = 1073741824;
7168

69+
GLFWwindow *glfwWindow;
70+
71+
bool trayQuit = false;
72+
7273
#pragma region Config
7374
#pragma region Default settings
7475
// Initialization
@@ -86,7 +87,7 @@ int maxRes = 250;
8687
int resIncreaseThreshold = 80;
8788
int resDecreaseThreshold = 88;
8889
int resIncreaseMin = 3;
89-
int resDecreaseMin = 6;
90+
int resDecreaseMin = 5;
9091
int resIncreaseScale = 140;
9192
int resDecreaseScale = 140;
9293
float minCpuTimeThreshold = 0.6f;
@@ -152,7 +153,7 @@ bool loadSettings()
152153
if (dataAverageSamples > 128)
153154
dataAverageSamples = 128; // Max stored by OpenVR
154155
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');
156157
disabledAppsSet = multilineStringToSet(disabledApps);
157158
// Resolution
158159
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)
316317
return !inDashboard && !isCurrentAppDisabled && !manualRes && !(resetOnThreshold && cpuTime < minCpuTimeThreshold);
317318
}
318319

319-
void printLine(GLFWwindow *window, std::string text, long duration)
320+
void printLine(std::string text, long duration)
320321
{
321322
long startTime = getCurrentTimeMillis();
322323

323-
while (getCurrentTimeMillis() < startTime + duration && !glfwWindowShouldClose(window))
324+
while (getCurrentTimeMillis() < startTime + duration && !glfwWindowShouldClose(glfwWindow))
324325
{
325326
glfwPollEvents();
326327

@@ -346,14 +347,14 @@ void printLine(GLFWwindow *window, std::string text, long duration)
346347
ImGui::Render();
347348
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
348349

349-
glfwSwapBuffers(window);
350+
glfwSwapBuffers(glfwWindow);
350351

351352
// Sleep to display the text for a set duration
352353
std::this_thread::sleep_for(refreshIntervalBackground);
353354
}
354355
}
355356

356-
void cleanup(GLFWwindow *window, nvmlLib nvmlLibrary)
357+
void cleanup(nvmlLib nvmlLibrary)
357358
{
358359
// OpenVR cleanup
359360
vr::VR_Shutdown();
@@ -379,7 +380,7 @@ void cleanup(GLFWwindow *window, nvmlLib nvmlLibrary)
379380
ImGui_ImplOpenGL3_Shutdown();
380381
ImGui_ImplGlfw_Shutdown();
381382
ImGui::DestroyContext();
382-
glfwDestroyWindow(window);
383+
glfwDestroyWindow(glfwWindow);
383384
glfwTerminate();
384385
}
385386

@@ -420,10 +421,10 @@ int main(int argc, char *argv[])
420421
glfwWindowHint(GLFW_RESIZABLE, false);
421422

422423
// 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)
425426
return 1;
426-
glfwMakeContextCurrent(window);
427+
glfwMakeContextCurrent(glfwWindow);
427428

428429
// Setup Dear ImGui context
429430
IMGUI_CHECKVERSION();
@@ -438,7 +439,7 @@ int main(int argc, char *argv[])
438439
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.03, 0.03, 0.03, 1));
439440

440441
// Setup Platform/Renderer backends
441-
ImGui_ImplGlfw_InitForOpenGL(window, true);
442+
ImGui_ImplGlfw_InitForOpenGL(glfwWindow, true);
442443
#ifdef __EMSCRIPTEN__
443444
ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas");
444445
#endif
@@ -450,7 +451,7 @@ int main(int argc, char *argv[])
450451
unsigned test = lodepng_decode32_file(&(icon.pixels), &(iconWidth), &(iconHeight), iconPath);
451452
icon.width = (int)iconWidth;
452453
icon.height = (int)iconHeight;
453-
glfwSetWindowIcon(window, 1, &icon);
454+
glfwSetWindowIcon(glfwWindow, 1, &icon);
454455
#pragma endregion
455456

456457
#pragma region VR init
@@ -460,12 +461,12 @@ int main(int argc, char *argv[])
460461
if (init_error)
461462
{
462463
system = nullptr;
463-
printLine(window, VR_GetVRInitErrorAsEnglishDescription(init_error), 6000l);
464+
printLine(VR_GetVRInitErrorAsEnglishDescription(init_error), 6000l);
464465
return EXIT_FAILURE;
465466
}
466467
if (!VRCompositor())
467468
{
468-
printLine(window, "Failed to initialize VR compositor.", 6000l);
469+
printLine("Failed to initialize VR compositor.", 6000l);
469470
return EXIT_FAILURE;
470471
}
471472
#pragma endregion
@@ -477,13 +478,13 @@ int main(int argc, char *argv[])
477478
// Set auto-start
478479
int autoStartResult = handle_setup(autoStart);
479480
if (autoStartResult != 0)
480-
printLine(window, fmt::format("Error toggling auto-start ({}) ", autoStartResult), 6000l);
481+
printLine(fmt::format("Error toggling auto-start ({}) ", autoStartResult), 6000l);
481482

482483
// Minimize or hide the window according to config
483484
if (minimizeOnStart == 1) // Minimize
484-
glfwIconifyWindow(window);
485+
glfwIconifyWindow(glfwWindow);
485486
else if (minimizeOnStart == 2) // Hide
486-
glfwHideWindow(window);
487+
glfwHideWindow(glfwWindow);
487488

488489
// Make sure we can set resolution ourselves (Custom instead of Auto)
489490
vr::VRSettings()->SetInt32(vr::k_pch_SteamVR_Section,
@@ -555,21 +556,32 @@ int main(int argc, char *argv[])
555556
nvmlEnabled = false;
556557
}
557558
#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
573585

574586
// Initialize loop variables
575587
Compositor_FrameTiming *frameTiming = new vr::Compositor_FrameTiming[dataAverageSamples];
@@ -595,7 +607,7 @@ int main(int argc, char *argv[])
595607
bool prevAutoStart = autoStart;
596608

597609
// event loop
598-
while (!glfwWindowShouldClose(window) && !openvrQuit)
610+
while (!glfwWindowShouldClose(glfwWindow) && !openvrQuit && !trayQuit)
599611
{
600612
// Get current time
601613
long currentTime = getCurrentTimeMillis();
@@ -828,7 +840,7 @@ int main(int argc, char *argv[])
828840
ImGui::SameLine(0, 10);
829841
if (manualRes)
830842
{
831-
ImGui::PushItemWidth(172);
843+
ImGui::PushItemWidth(192);
832844
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
833845
if (ImGui::SliderInt("", &newRes, 20, 500, "%d", ImGuiSliderFlags_AlwaysClamp))
834846
{
@@ -905,7 +917,7 @@ int main(int argc, char *argv[])
905917
if (ImGui::CollapsingHeader("General"))
906918
{
907919
if (ImGui::InputInt("Resolution change delay ms", &resChangeDelayMs, 100))
908-
resChangeDelayMs = std::max(resChangeDelayMs, 10);
920+
resChangeDelayMs = std::max(resChangeDelayMs, 100);
909921
addTooltip("Delay in milliseconds between resolution changes.");
910922

911923
if (ImGui::InputInt("Data average samples.", &dataAverageSamples, 2))
@@ -1043,7 +1055,7 @@ int main(int argc, char *argv[])
10431055
ImGui::Render();
10441056
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
10451057

1046-
glfwSwapBuffers(window);
1058+
glfwSwapBuffers(glfwWindow);
10471059
#pragma endregion
10481060

10491061
// Check if OpenVR is quitting so we can quit alongside it
@@ -1060,7 +1072,7 @@ int main(int argc, char *argv[])
10601072

10611073
// Calculate how long to sleep for depending on if the window is focused or not.
10621074
std::chrono::milliseconds sleepTime;
1063-
if (glfwGetWindowAttrib(window, GLFW_FOCUSED))
1075+
if (glfwGetWindowAttrib(glfwWindow, GLFW_FOCUSED))
10641076
sleepTime = refreshIntervalFocused;
10651077
else
10661078
sleepTime = refreshIntervalBackground;
@@ -1069,11 +1081,11 @@ int main(int argc, char *argv[])
10691081
std::this_thread::sleep_for(sleepTime);
10701082
}
10711083

1072-
cleanup(window, nvmlLibrary);
1084+
cleanup(nvmlLibrary);
10731085

1074-
#if defined(TRAY_ICON)
1075-
tray.exit();
1076-
#endif // TRAY_ICON
1086+
#if defined(_WIN32)
1087+
tray_exit();
1088+
#endif // _WIN32
10771089

10781090
return 0;
10791091
}

src/tray.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef TRAY_H
2+
#define TRAY_H
3+
4+
#ifdef __cplusplus
5+
extern "C"
6+
{
7+
#endif
8+
9+
struct tray {
10+
const char *icon_filepath;
11+
const char *tooltip;
12+
void (*cb)(struct tray *); // called on left click, leave null to just open menu
13+
struct tray_menu_item *menu;
14+
};
15+
16+
struct tray_menu_item {
17+
const char *text;
18+
int disabled;
19+
int checked;
20+
void (*cb)(struct tray_menu_item *);
21+
struct tray_menu_item *submenu;
22+
};
23+
24+
struct tray * tray_get_instance();
25+
26+
int tray_init(struct tray *tray);
27+
28+
int tray_loop(int blocking);
29+
30+
void tray_update(struct tray *tray);
31+
32+
void tray_exit(void);
33+
34+
#ifdef __cplusplus
35+
} // extern "C"
36+
#endif
37+
38+
#endif /* TRAY_H */

0 commit comments

Comments
 (0)