-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
451 lines (351 loc) · 13 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "pch.h"
#include "config.hpp"
#include "resource.h"
#include <CommCtrl.h>
#include <Mile.Xaml.h>
//#include "Xaml/App.h"
#include "Xaml/SettingsPage.h"
#include "BooleanToVisibilityConverter.g.h"
import dcomp;
import danmaku;
import server;
#pragma comment(lib, "Comctl32.lib")
class SettingsWnd : public ATL::CWindowImpl<SettingsWnd> {
public:
BEGIN_MSG_MAP(SettingsWnd)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_CLOSE, OnClose)
MESSAGE_HANDLER(WM_GETMINMAXINFO, OnMinMaxInfo)
END_MSG_MAP()
DECLARE_WND_SUPERCLASS(_T("Danmaku.SettingsWindow"), _T("Mile.Xaml.ContentWindow"));
LRESULT OnCreate(UINT, WPARAM, LPARAM, BOOL &handled) {
handled = false;
SetIcon(LoadIcon(_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(IDI_SETTINGS)), TRUE);
return S_OK;
}
LRESULT OnClose(UINT, WPARAM, LPARAM, BOOL &) {
// overrides default, no quit message, just hide it
ShowWindow(false);
return S_OK;
}
LRESULT OnMinMaxInfo(UINT, WPARAM, LPARAM lParam, BOOL &) {
LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
auto dpi = GetDpiForWindow(m_hWnd);
lpMMI->ptMinTrackSize.x = 760 * dpi / 96;
lpMMI->ptMinTrackSize.y = 400 * dpi / 96;
return S_OK;
}
static SettingsWnd &singleton() {
static SettingsWnd wnd;
return wnd;
}
static LPCTSTR GetWndCaption() {
return _T("Danmaku Server Settings");
}
static DWORD GetWndStyle(DWORD dwStyle) {
return dwStyle | WS_OVERLAPPEDWINDOW;
}
static DWORD GetWndExStyle(DWORD dwExStyle) {
return dwExStyle | WS_EX_TOPMOST;
}
};
class ShellIconWnd
: public ATL::CWindowImpl<ShellIconWnd> {
public:
DECLARE_WND_CLASS(_T("ShellIconHiddenWindow"));
private:
BEGIN_MSG_MAP(ShellIconWnd)
MESSAGE_HANDLER(WM_USER, OnNIProc)
END_MSG_MAP()
static constexpr // {B742EA6E-FAB9-47A0-9FED-7865D3E3F430}
const GUID GUID_TRAY_ICON =
{ 0xb742ea6e, 0xfab9, 0x47a0, { 0x9f, 0xed, 0x78, 0x65, 0xd3, 0xe3, 0xf4, 0x30 } };
void ShowAbout() {
TASKDIALOGCONFIG td{};
td.cbSize = sizeof(td);
td.hwndParent = m_hWnd;
td.hInstance = GetModuleHandle(NULL);
td.dwFlags = TDF_ENABLE_HYPERLINKS;
td.pszMainIcon = MAKEINTRESOURCE(m_CurrentIconResource);
td.pszWindowTitle = L"About";
td.pszMainInstruction = L"Danmaku Server 1.5.5";
td.pszContent =
LR"(Please check our <a href="https://github.com/seven-mile/DanmakuServer">GitHub Repo</a> for more information.
The danmaku server is hosted on :7654 port, access <a href="http://localhost:7654/danmaku?name=danmaku%20server&content=hello,%20world%F0%9F%92%98">this url</a> for sample danmaku effect.)";
td.nDefaultButton = TDCBF_OK_BUTTON;
td.pfCallback = [](HWND hwnd, UINT msg, WPARAM, LPARAM lParam, LONG_PTR) {
if (msg == TDN_HYPERLINK_CLICKED) {
ShellExecute(hwnd, L"open", (LPCWSTR)lParam, NULL, NULL, SW_SHOW);
}
return S_OK;
};
winrt::check_hresult(TaskDialogIndirect(&td, NULL, NULL, NULL));
}
void ShowSettings() {
SettingsWnd::singleton().ShowWindow(true);
SettingsWnd::singleton().SetActiveWindow();
}
HRESULT CreateShell() {
RECT rect{};
// Create a hidden window (using CWindowImpl)
HWND hWnd = Create(NULL, rect, L"ShellIconHiddenWindow", WS_POPUP);
if (hWnd != 0) // Was created?
{
// Add the icon into the shell
ShellNotify(NIM_ADD);
ShellNotify(NIM_SETVERSION);
m_bIconInited = true;
return S_OK;
} else {
return HRESULT_FROM_WIN32(GetLastError());
}
}
void DestroyShell() {
if (m_bIconInited) {
ShellNotify(NIM_DELETE); // Remove the icon
if (m_hWnd != NULL) {
// Get rid of the hidden window
DestroyWindow();
}
}
}
void ShellNotify(DWORD msg) {
NOTIFYICONDATA notifyIconData{};
notifyIconData.cbSize = sizeof(notifyIconData);
notifyIconData.guidItem = GUID_TRAY_ICON;
notifyIconData.uVersion = NOTIFYICON_VERSION_4;
notifyIconData.hWnd = m_hWnd;
notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP | NIF_GUID;
notifyIconData.uCallbackMessage = WM_USER;
notifyIconData.uID = 0;
notifyIconData.hIcon = ::LoadIcon(GetModuleHandle(NULL),
MAKEINTRESOURCE(m_CurrentIconResource));
::lstrcpyn(notifyIconData.szTip, m_CurrentText.c_str(),
ARRAYSIZE(notifyIconData.szTip)); // Limit to 64 chars
if (!::Shell_NotifyIcon(msg, ¬ifyIconData)) {
TASKDIALOGCONFIG td{};
td.cbSize = sizeof(td);
td.hwndParent = m_hWnd;
td.hInstance = GetModuleHandle(NULL);
td.dwFlags = TDF_ENABLE_HYPERLINKS;
td.pszMainIcon = TD_ERROR_ICON;
td.pszWindowTitle = L"Error";
td.pszMainInstruction = L"Failed to create notify icon";
td.pszContent =
LR"(Are you opening a *second* instance of DanmakuServer?
Or, have you changed the binary location of a unpackaged installation? Refer to <a href="https://support.microsoft.com/en-gb/topic/system-icons-do-not-appear-in-the-notification-area-in-windows-vista-or-in-windows-7-until-you-restart-the-computer-eed17e13-f80a-fde3-39de-2adfc94d56e1">this article</a> for a workaround.
If restarting explorer can't solve this problem, please create an issue on GitHub.)";
td.nDefaultButton = TDCBF_OK_BUTTON;
td.pfCallback = [](HWND hwnd, UINT msg, WPARAM, LPARAM lParam, LONG_PTR) {
if (msg == TDN_HYPERLINK_CLICKED) {
ShellExecute(hwnd, L"open", (LPCWSTR)lParam, NULL, NULL, SW_SHOW);
}
return S_OK;
};
winrt::check_hresult(TaskDialogIndirect(&td, NULL, NULL, NULL));
PostQuitMessage(-1);
}
}
LRESULT OnNIProc(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam,
BOOL & /*bHandled*/) {
switch (LOWORD(lParam)) {
case NIN_SELECT:
// for NOTIFYICON_VERSION_4 clients, NIN_SELECT is prerable to listening
// to mouse clicks and key presses directly.
break;
case NIN_BALLOONTIMEOUT:
break;
case NIN_BALLOONUSERCLICK:
break;
case WM_CONTEXTMENU: {
POINT const pt = {LOWORD(wParam), HIWORD(wParam)};
int cmd = ShowPopupMenu(IDM_TRAY, pt);
if (cmd == IDMI_EXIT) {
PostQuitMessage(0);
} else if (cmd == IDMI_ABOUT) {
ShowAbout();
} else if (cmd == IDMI_SETTINGS) {
ShowSettings();
}
} break;
}
return 0;
}
public:
static ShellIconWnd &singleton() {
static ShellIconWnd wnd{};
return wnd;
}
void SetShellTipText(std::wstring const &TipText) {
// Save this text for when we update
m_CurrentText = TipText;
ShellNotify(NIM_MODIFY);
}
void SetShellIcon(WORD IconResource) {
// Save this icon resource for when we update
m_CurrentIconResource = IconResource;
ShellNotify(NIM_MODIFY);
}
void SetShellVisible(bool bVisible = true) {
if (bVisible == true) // User wants to show the icon in the shell
{
if (m_bVisible == false) // Doesn't already exist?
{
// Create the shell, and timer (if applicable)
CreateShell();
} // Otherwise, well you already have icon in the shell. :-)
} else // User wants rid of the icon
{
if (m_bVisible == true) // Is it there already?
{
DestroyShell(); // Get rid
}
}
m_bVisible = bVisible;
}
int ShowPopupMenu(WORD PopupMenuResource, POINT pt) {
HMENU hMenu, hPopup = 0;
hMenu = ::LoadMenu(GetModuleHandle(NULL),
MAKEINTRESOURCE(PopupMenuResource));
if (hMenu != 0) {
//POINT pt;
//::GetCursorPos(&pt);
// TrackPopupMenu cannot display the menu bar so get
// a handle to the first shortcut menu.
hPopup = ::GetSubMenu(hMenu, 0);
// To display a context menu for a notification icon, the
// current window must be the foreground window before the
// application calls TrackPopupMenu or TrackPopupMenuEx. Otherwise,
// the menu will not disappear when the user clicks outside of the menu
// or the window that created the menu (if it is visible).
::SetForegroundWindow(m_hWnd);
int cmd = ::TrackPopupMenu(hPopup, TPM_RIGHTBUTTON | TPM_RETURNCMD, pt.x,
pt.y, 0, m_hWnd, NULL);
// See MS KB article Q135788
::PostMessage(m_hWnd, WM_NULL, 0, 0);
// Clear up the menu, we're not longer using it.
::DestroyMenu(hMenu);
return cmd;
}
return 0;
}
private:
bool m_bVisible;
bool m_bIconInited;
int m_CurrentIconResource;
std::wstring m_CurrentText;
};
class DanmakuWnd : public ATL::CWindowImpl<DanmakuWnd> {
using Base = ATL::CWindowImpl<DanmakuWnd>;
comp::Compositor compositor{nullptr};
comp::ContainerVisual root{nullptr};
uicore::CoreDispatcher dispatcher{nullptr};
danmaku::DanmakuManager &danmaku_mgr{danmaku::DanmakuManager::singleton()};
std::unique_ptr<server::DanmakuServer> danmaku_srv;
public:
BEGIN_MSG_MAP(DanmakuWnd)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_CLOSE, OnClose)
END_MSG_MAP()
DECLARE_WND_CLASS(_T("DanmakuOverlay"));
static DanmakuWnd &singleton() {
static DanmakuWnd wnd;
return wnd;
}
void TestDanmakuSingleIssueMode() {
static std::default_random_engine rd{998244353};
static std::wstring lorem{L"Lorem ipsum dolor sit amet Allocated, linux? kernel function is that"};
static std::uniform_int_distribution text_gen{1, (int)lorem.size()};
static auto gen_text = [](){
std::wstring text{wchar_t(L'A' + (text_gen(rd) % 26))};
for (int i = 0; i < text_gen(rd); ++i) {
text += lorem[text_gen(rd)];
}
return text;
};
threading::ThreadPoolTimer::CreatePeriodicTimer([this](auto const &){
for (int i=0; i<20; i++) {
danmaku_mgr.IssueDanmaku(gen_text());
}
}, 300ms);
}
LRESULT OnCreate(UINT, WPARAM, LPARAM, BOOL &) {
// Refer to https://github.com/dechamps/RudeWindowFixer
winrt::check_bool(SetPropW(m_hWnd, L"NonRudeHWND", INVALID_HANDLE_VALUE));
SetIcon(LoadIcon(_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(IDI_DANMAKU)), TRUE);
dispatcher = dcomp::GetOrCreateCoreDispatcher();
std::tie(compositor, root) = dcomp::CreateCompositorForWindow(m_hWnd);
danmaku_mgr.Initialize(compositor, root);
//TestDanmakuSingleIssueMode();
//TestDanmakuCanvasMode();
danmaku_srv = std::make_unique<server::DanmakuServer>([&](std::wstring const &danmaku){
danmaku_mgr.IssueDanmaku(danmaku);
});
return 0;
}
LRESULT OnClose(UINT, WPARAM, LPARAM, BOOL &) {
PostQuitMessage(0);
return 0;
}
static LPCTSTR GetWndCaption() {
return _T("Danmaku Overlay");
}
static DWORD GetWndStyle(DWORD dwStyle) {
return dwStyle | WS_POPUPWINDOW;
}
static DWORD GetWndExStyle(DWORD dwStyle) {
return dwStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST
| WS_EX_NOREDIRECTIONBITMAP | WS_EX_TOOLWINDOW;
}
};
int APIENTRY wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) {
auto &wnd = DanmakuWnd::singleton();
auto &tray_icon = ShellIconWnd::singleton();
auto &settings = SettingsWnd::singleton();
winrt::check_hresult(::MileXamlGlobalInitialize());
//auto xaml_app = winrt::make<winrt::DanmakuServer::implementation::App>();
auto app = winrt::Windows::UI::Xaml::Application::Current();
{
winrt::Windows::UI::Xaml::ResourceDictionary dict;
dict.Source(found::Uri(L"ms-appx:///Mile.Xaml.Styles.SunValley.xbf"));
app.Resources().MergedDictionaries().Append(
dict
);
//<local:BooleanToVisibilityConverter x:Key="TrueToVisibleConverter" Positive="True" />
//<local:BooleanToVisibilityConverter x:Key="FalseToVisibleConverter" Positive="False" />
winrt::DanmakuServer::BooleanToVisibilityConverter posi_conv, nega_conv;
posi_conv.Positive(true);
nega_conv.Positive(false);
app.Resources().Insert(winrt::box_value(L"TrueToVisibleConverter"), posi_conv);
app.Resources().Insert(winrt::box_value(L"FalseToVisibleConverter"), nega_conv);
}
tray_icon.SetShellVisible();
tray_icon.SetShellIcon(IDI_DANMAKU);
tray_icon.SetShellTipText(L"DanmakuServer listening on :7654");
auto scr_width = GetSystemMetrics(SM_CXSCREEN);
auto scr_height = GetSystemMetrics(SM_CYSCREEN);
{
RECT rect_full{0, 0, scr_width, scr_height};
wnd.Create(NULL, rect_full);
}
{
constexpr auto SETTINGS_WIDTH = 1200, SETTINGS_HEIGHT = 900;
RECT rect_settings{
(scr_width - SETTINGS_WIDTH) / 2,
(scr_height - SETTINGS_HEIGHT) / 2,
(scr_width + SETTINGS_WIDTH) / 2,
(scr_height + SETTINGS_HEIGHT) / 2};
auto page = winrt::make<winrt::DanmakuServer::implementation::SettingsPage>();
settings.Create(NULL, rect_settings, NULL, 0, 0, 0U, winrt::get_abi(page));
}
wnd.ShowWindow(true);
wnd.SetActiveWindow();
auto dispatcher = dcomp::GetOrCreateCoreDispatcher();
dispatcher.ProcessEvents(uicore::CoreProcessEventsOption::ProcessUntilQuit);
wnd.DestroyWindow();
settings.DestroyWindow();
// destroy icon for window release
tray_icon.SetShellVisible(false);
winrt::check_hresult(::MileXamlGlobalUninitialize());
return 0;
}