Skip to content

Commit

Permalink
feat: 完善vdd的状态管理
Browse files Browse the repository at this point in the history
  • Loading branch information
qiin2333 committed Feb 15, 2025
1 parent c5d2f29 commit ea3821b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/display_device/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ namespace display_device {
}

std::chrono::steady_clock::time_point last_toggle_time;
std::chrono::milliseconds debounce_interval { 5000 }; // 5000毫秒防抖间隔
std::chrono::milliseconds debounce_interval { 9000 }; // 9000毫秒防抖间隔

void
session_t::toggle_display_power() {
Expand Down
2 changes: 1 addition & 1 deletion src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ bool display_cursor = true;

#ifdef _WIN32
nvprefs::nvprefs_interface nvprefs_instance;
std::string zako_name = "VDD by MTT";
std::string zako_name = "Zako HDR";
std::string zako_device_id;
#endif
94 changes: 66 additions & 28 deletions src/system_tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,48 @@
#include "src/display_device/display_device.h"
#include "src/entry_handler.h"
#include "version.h"
#include <chrono>
#include <future>
#include <thread>

using namespace std::literals;

// system_tray namespace
namespace system_tray {
static std::atomic<bool> tray_initialized = false;

// 前向声明所有回调函数
void tray_open_ui_cb(struct tray_menu *item);
void tray_toggle_display_cb(struct tray_menu *item);
void tray_reset_display_device_config_cb(struct tray_menu *item);
void tray_restart_cb(struct tray_menu *item);
void tray_quit_cb(struct tray_menu *item);

// 菜单数组声明
static struct tray_menu tray_menus[] = {
{ .text = "Open Sunshine", .cb = tray_open_ui_cb },
{ .text = "-" },
{ .text = "VDD Monitor Toggle", .checked = 0, .cb = tray_toggle_display_cb },
// { .text = "Donate",
// .submenu =
// (struct tray_menu[]) {
// { .text = "GitHub Sponsors", .cb = tray_donate_github_cb },
// { .text = "Patreon", .cb = tray_donate_patreon_cb },
// { .text = "PayPal", .cb = tray_donate_paypal_cb },
// { .text = nullptr } } },
{ .text = "-" },
#ifdef _WIN32
{ .text = "Reset Display Device Config", .cb = tray_reset_display_device_config_cb },
#endif
{ .text = "Restart", .cb = tray_restart_cb },
{ .text = "Quit", .cb = tray_quit_cb },
{ .text = nullptr }
};

static struct tray tray = {
.icon = TRAY_ICON,
.tooltip = PROJECT_NAME,
.menu = nullptr, // 先初始化为空指针,后续再赋值
.menu = tray_menus,
.iconPathCount = 4,
.allIconPaths = { TRAY_ICON, TRAY_ICON_LOCKED, TRAY_ICON_PLAYING, TRAY_ICON_PAUSING },
};
Expand Down Expand Up @@ -119,37 +150,42 @@ namespace system_tray {
lifetime::exit_sunshine(0, true);
}

class AsyncTask {
public:
~AsyncTask() { if (fut.valid()) fut.wait(); }
void launch(std::function<void()> task) {
// 确保之前的任务已完成
if (fut.valid()) {
fut.wait();
}
fut = std::async(std::launch::async, task);
}
private:
std::future<void> fut;
};

static AsyncTask async_task;

void
tray_toggle_display_cb(struct tray_menu *item) {
// 添加冷却状态检查
if (system_tray::tray_menus[2].disabled) {
return;
}

BOOST_LOG(info) << "Toggling display power from system tray"sv;
display_device::session_t::get().toggle_display_power();
}

// 将菜单数组声明为静态变量
static struct tray_menu tray_menus[] = {
{ .text = "Open Sunshine", .cb = tray_open_ui_cb },
{ .text = "-" },
{ .text = "VDD Monitor Toggle", .checked = 0, .cb = tray_toggle_display_cb },
// { .text = "Donate",
// .submenu =
// (struct tray_menu[]) {
// { .text = "GitHub Sponsors", .cb = tray_donate_github_cb },
// { .text = "Patreon", .cb = tray_donate_patreon_cb },
// { .text = "PayPal", .cb = tray_donate_paypal_cb },
// { .text = nullptr } } },
{ .text = "-" },
#ifdef _WIN32
{ .text = "Reset Display Device Config", .cb = tray_reset_display_device_config_cb },
#endif
{ .text = "Restart", .cb = tray_restart_cb },
{ .text = "Quit", .cb = tray_quit_cb },
{ .text = nullptr }
};

// 在菜单数组初始化后设置 tray.menu
__attribute__((constructor)) static void
init_tray_menu() {
tray.menu = tray_menus;

// 添加10秒禁用状态
system_tray::tray_menus[2].disabled = 1;
tray_update(&tray);

// 使用RAII包装器管理future对象
async_task.launch([]{
std::this_thread::sleep_for(10s);
system_tray::tray_menus[2].disabled = 0;
tray_update(&tray);
});
}

int
Expand Down Expand Up @@ -372,6 +408,8 @@ namespace system_tray {
}
// 更新显示器切换菜单项的勾选状态
tray_menus[2].checked = checked;
// 同时更新禁用状态(冷却期间保持禁用)
tray_menus[2].disabled = checked ? 0 : tray_menus[2].disabled;
tray_update(&tray);
}

Expand Down
2 changes: 1 addition & 1 deletion src_assets/windows/assets/apps.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
{
"name": "禁用虚拟显示器",
"cmd": "C:\\Program Files\\Sunshine\\tools\\device-toggler.exe 2 2 \"VirtualDisplayDriver\"",
"cmd": "C:\\Program Files\\Sunshine\\tools\\device-toggler.exe 2 2 \"Zako Display Adapter\"",
"elevated": "true"
},
{
Expand Down
Binary file modified src_assets/windows/misc/vdd/driver/MttVDD.inf
Binary file not shown.
Binary file modified src_assets/windows/misc/vdd/driver/mttvdd.cat
Binary file not shown.
2 changes: 1 addition & 1 deletion src_assets/windows/misc/vdd/driver/vdd_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<g_refresh_rate>120</g_refresh_rate>
<g_refresh_rate>144</g_refresh_rate>
<g_refresh_rate>165</g_refresh_rate>
<g_refresh_rate>244</g_refresh_rate>
<g_refresh_rate>240</g_refresh_rate>
</global>
<resolutions>
<resolution>
Expand Down

0 comments on commit ea3821b

Please sign in to comment.