-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
448 lines (392 loc) · 16.3 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
// Copyright (c) 2012-2014 The PHP Desktop authors. All rights reserved.
// License: New BSD License.
// Website: http://code.google.com/p/phpdesktop/
#include "defines.h"
#include <Windows.h>
#include <crtdbg.h> // _ASSERT() macro
#include "mmsystem.h"
#include "resource.h"
#include "3rd/ProcessManage.h"
#include "misc/logger.h"
#include "misc/file_utils.h"
#include "misc/debug.h"
#include "msie/msie.h"
#include "msie/internet_features.h"
#include "msie/browser_window.h"
#include "settings.h"
#include "misc/instance.h"
#include "misc/version.h"
#include "misc/string_utils.h"
#include "web_server.h"
#include "mb/wke.h"
#include "mb/mb.hpp"
#pragma comment(lib,"winmm.lib")
#define BROWSER_GENERIC_TIMER 1
#ifdef _UNICODE
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#endif
Instance g_Instance;
int g_windowCount = 0;
HWND g_hwnd = 0;
HMENU hTrayMenu;
LPSHELLEXECUTEINFO shellInfo;
HINSTANCE g_hInstance = 0;
std::string g_cgiEnvironmentFromArgv = "";
NOTIFYICONDATA GetTrayData(HWND hwnd) {
static NOTIFYICONDATA tray = {0};
tray.hWnd = hwnd;
if (tray.cbSize) {
return tray;
}
nlohmann::json* appSettings = GetApplicationSettings();
std::string main_window_title = (*appSettings)["window"]["title"];
std::string startup_to_tray_message = (*appSettings)["window"]["startup_to_tray_message"];
tray.cbSize = sizeof(tray);
tray.uID = 1;
tray.uTimeout = 1000;
tray.uCallbackMessage = WM_TRAY_MESSAGE;
tray.hIcon = (HICON)LoadImage(g_hInstance, MAKEINTRESOURCE(IDR_MAINTRAY), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
wcscpy_s(tray.szTip, Utf8ToWide(main_window_title).c_str());
wcscpy_s(tray.szInfo, 256, Utf8ToWide(startup_to_tray_message).c_str());
wcscpy_s(tray.szInfoTitle, 64, Utf8ToWide(main_window_title).c_str());
tray.uFlags = NIF_ICON | NIF_INFO | NIF_MESSAGE | NIF_TIP;
tray.dwInfoFlags = NIIF_NONE;
return tray;
}
bool ShowTrayTip(HWND hwnd, std::string msg, int snd) {
NOTIFYICONDATA tray = GetTrayData(hwnd);
if (snd + 10000 > IDS_SNDMAX) {
snd = IDS_SNDMAX;
} else if (snd > 0) {
snd = snd + 10000;
}
if (snd > 0) {
PlaySound(MAKEINTRESOURCE(snd), g_hInstance, SND_ASYNC|SND_RESOURCE|SND_NODEFAULT);
}
wcscpy_s(tray.szInfo, 256, Utf8ToWide(msg).c_str());
return 0 != Shell_NotifyIcon(NIM_MODIFY, &tray);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
BrowserWindow* browser = 0;
UINT_PTR timer = 0;
BOOL b = 0;
WORD childEvent = 0;
HWND childHandle = 0;
HWND shellBrowserHandle = 0;
HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
// Settings
nlohmann::json* appSettings = GetApplicationSettings();
std::string title = (*appSettings)["window"]["title"];
std::string homepage = (*appSettings)["integration"]["homepage"];
bool minimize_to_tray = (*appSettings)["window"]["minimize_to_tray"];
if (g_windowCount > 1) {
minimize_to_tray = false;
}
switch (uMsg) {
case WM_SIZE:
browser = GetBrowserWindow(std::to_string((long long) hwnd));
if (browser) {
browser->OnResize(uMsg, wParam, lParam);
return 0;
} else {
FLOG_WARNING << "WindowProc(): event WM_SIZE: could not fetch BrowserWindow";
}
break;
case WM_CREATE:
AppendMenu(hSysMenu, MF_STRING, IDM_SETTING, TEXT("Setting"));
AppendMenu(hSysMenu, MF_STRING, IDM_ABOUT, TEXT("About"));
g_windowCount++;
browser = new BrowserWindow(hwnd, homepage);
StoreBrowserWindow(std::to_string((long long) hwnd), browser);
timer = SetTimer(hwnd, BROWSER_GENERIC_TIMER, 50, 0);
if (!timer) {
FLOG_WARNING << "WindowProc(): event WM_CREATE: SetTimer() failed";
}
return 0;
case WM_SYSCOMMAND:
if (wParam == IDM_ABOUT) {
MessageBox(NULL, L"welcome to use http mise bridge\nwebsite: http://github.com/csg800", ConvertW(title.c_str()), MB_OK | MB_ICONINFORMATION);
} else if (wParam == SC_MINIMIZE && minimize_to_tray) {
FLOG_DEBUG << "Minimize to tray";
ShowWindow(hwnd, SW_MINIMIZE);
Sleep(200);
ShowWindow(hwnd, SW_HIDE);
ShowTrayTip(hwnd, (*appSettings)["window"]["minimize_to_tray_message"].get<std::string>(), 0);
break;
} else {
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
break;
case WM_TRAY_MESSAGE:
if (lParam == WM_LBUTTONDOWN) {
FLOG_DEBUG << "Restore from tray";
ShowWindow(hwnd, SW_SHOW);
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
break;
} else if (lParam == WM_RBUTTONDOWN) {
POINT pt;
GetCursorPos(&pt);
SetForegroundWindow(hwnd);
int cmd = ::TrackPopupMenu(::GetSubMenu(hTrayMenu, 0), TPM_RETURNCMD, pt.x, pt.y, NULL, hwnd, NULL);
if (cmd == IDM_ABOUT) {
MessageBox(NULL, L"welcome to use http mise bridge\nwebsite: http://github.com/csg800", ConvertW(title.c_str()), MB_OK | MB_ICONINFORMATION);
} else if(cmd == IDM_SHOW) {
FLOG_DEBUG << "Restore from tray";
ShowWindow(hwnd, SW_SHOW);
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
}else if(cmd == IDM_EXIT) {
PostMessage(hwnd, WM_DESTROY, NULL, NULL);
}
}
break;
case WM_DESTROY:
g_windowCount--;
b = KillTimer(hwnd, BROWSER_GENERIC_TIMER);
_ASSERT(b);
RemoveBrowserWindow(std::to_string((long long) hwnd));
if (g_windowCount <= 0) {
StopWebServer();
if (NULL != shellInfo) {
closeProcess(shellInfo);
}
Shell_NotifyIcon(NIM_DELETE, &GetTrayData(g_hwnd));
// Cannot call PostQuitMessage as cookies won't be flushed to disk
// if application is closed immediately. See comment #2:
// https://code.google.com/p/phpdesktop/issues/detail?id=146
// I suppose that this PostQuitMessage was added here so that
// application is forced to exit cleanly in case Mongoose
// web server hanged while stopping it, as I recall such case.
// -------------------
// PostQuitMessage(0);
// -------------------
PostQuitMessage(0);
}
break;
case WM_TIMER:
if (wParam == BROWSER_GENERIC_TIMER) {
browser = GetBrowserWindow(std::to_string((long long) hwnd));
if (browser) {
browser->OnTimer(uMsg, wParam, lParam);
return 0;
} else {
FLOG_WARNING << "WindowProc(): event WM_TIMER failed: could not fetch BrowserWindow";
}
}
break;
case WM_GETMINMAXINFO:
browser = GetBrowserWindow(std::to_string((long long) hwnd));
if (browser) {
browser->OnGetMinMaxInfo(uMsg, wParam, lParam);
return 0;
} else {
// GetMinMaxInfo may fail during window creation, so
// log severity is only DEBUG.
FLOG_DEBUG << "WindowProc(): event WM_GETMINMAXINFO: could not fetch BrowserWindow";
}
break;
case WM_SETFOCUS:
browser = GetBrowserWindow(std::to_string((long long) hwnd));
if (browser) {
browser->SetFocus();
return 0;
} else {
FLOG_DEBUG << "WindowProc(): event WM_SETFOCUS: could not fetch BrowserWindow";
}
break;
/*
case WM_PARENTNOTIFY:
FLOG_DEBUG << "WM_PARENTNOTIFY";
browser = GetBrowserWindow(hwnd);
if (browser) {
childEvent = LOWORD(wParam);
// For example WM_LBUTTONDOWN.
FLOG_DEBUG << "childEvent = " << childEvent;
if (childEvent == WM_DESTROY) {
FLOG_DEBUG << "childEvent == WM_DESTROY";
childHandle = (HWND)HIWORD(wParam);
shellBrowserHandle = browser->GetShellBrowserHandle();
FLOG_DEBUG << "childHandle = " << childHandle;
FLOG_DEBUG << "shellBrowserHandle = " << shellBrowserHandle;
if (childHandle && shellBrowserHandle
&& childHandle == shellBrowserHandle) {
FLOG_DEBUG << "!!!!!!!!!!!!!!!!";
}
}
} else {
FLOG_DEBUG << "WindowProc(): event WM_PARENTNOTIFY: "
"could not fetch BrowserWindow";
}
break;
*/
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
bool ProcessKeyboardMessage(MSG* msg, std::string engine) {
if ("msie" == engine) {
if (msg->message == WM_KEYDOWN
|| msg->message == WM_KEYUP
|| msg->message == WM_SYSKEYDOWN
|| msg->message == WM_SYSKEYUP) {
HWND root = GetAncestor(msg->hwnd, GA_ROOT);
BrowserWindow* browser = GetBrowserWindow(std::to_string((long long) root));
if (browser) {
if (browser->TranslateAccelerator(msg))
return true;
} else {
FLOG_DEBUG << "ProcessKeyboardMessage(): could not fetch BrowserWindow";
}
}
} else {
}
return false;
}
void checkInstanceOnce(LPTSTR className) {
g_Instance.Initialize(className);
if (g_Instance.IsRunning()) {
HWND hwnd = FindWindow(className, NULL);
if (hwnd) {
if (IsIconic(hwnd)){
ShowWindow(hwnd, SW_RESTORE);
}
SetForegroundWindow(hwnd);
}
}
}
bool initMSIEEntry() {
nlohmann::json* settings = GetApplicationSettings();
const bool instance = (*settings)["window"]["instance"];
const std::string engine = (*settings)["integration"]["engine"];
// check ie min require
if (!CheckIeRequire()) {
FLOG_ERROR << "check ie min require failed, quit...";
return false;
}
// From the MSDN "WebBrowser Customization" docs:
// Your application should use OleInitialize rather than CoInitialize
// to start COM. OleInitialize enables support for the Clipboard,
// drag-and-drop operations, OLE, and in-place activation.
// See: http://msdn.microsoft.com/en-us/library/aa770041(v=vs.85).aspx
HRESULT hr = OleInitialize(NULL);
_ASSERT(SUCCEEDED(hr));
SetInternetFeatures();
return true;
}
bool initMBEntry() {
if (!wkeInitialize()) {
return false;
}
return true;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow) {
Process::setup_crash_reporting();
g_hInstance = hInstance;
nlohmann::json* settings = GetApplicationSettings();
// Debugging options.
bool show_console = (*settings)["integration"]["show_console"];
std::string log_level = (*settings)["integration"]["log_level"];
std::string log_file = (*settings)["integration"]["log_file"];
InitializeLogging(show_console, log_level, log_file);
FLOG_INFO << "--------------------------------------------------------";
FLOG_INFO << "Started application http msie bridge, engine :" << (*settings)["integration"]["engine"].get<std::string>();
GetAllHDSerialNumber();
if (log_file.length()) {
FLOG_INFO << "Logging to: " << log_file << ", Log level = " << FILELog::ToString(FILELog::ReportingLevel());
} else {
FLOG_INFO << "No logging file set, Log level = " << FILELog::ToString(FILELog::ReportingLevel());
}
// Command line arguments
LPWSTR *argv;
int argc;
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argv) {
for (int i = 0; i < argc; i++) {
std::string argument = WideToUtf8(std::wstring(argv[i]));
size_t pos = argument.find("=");
if (pos != std::string::npos) {
std::string name = argument.substr(0, pos);
std::string value = argument.substr(pos+1, std::string::npos);
if (name == "--cgi-environment" && value.length()) {
g_cgiEnvironmentFromArgv.assign(value);
}
}
}
} else {
FLOG_WARNING << "CommandLineToArgvW() failed";
}
// startup spec application
std::string startup = (*settings)["integration"]["startup"];
if ("" != startup) {
shellInfo = &exec(startup, 3, false);
if (NULL == shellInfo) {
FLOG_ERROR << "Startup spec application " << startup << " failed";
return -1;
}
}
// Single instance guid option.
const bool instance = (*settings)["window"]["instance"];
const std::string engine = (*settings)["integration"]["engine"];
const std::string homepage = (*settings)["integration"]["homepage"];
if (instance) {
if ("msie" == engine) {
checkInstanceOnce(CLASS_NAME_MSIE_EX);
} else {
checkInstanceOnce(CLASS_NAME_MB_EX);
}
}
HttpBridge::CMiniblink *window;
if ("msie" == engine) {
if (!initMSIEEntry()) {
FLOG_ERROR << "init engine msie failed";
return 1;
}
g_hwnd = CreateMainWindow(hInstance, nCmdShow, CLASS_NAME_MSIE_EX, WindowProc);
} else {
if (!initMBEntry()) {
FLOG_ERROR << "init engine mb failed";
return 1;
}
window = new HttpBridge::CMiniblink(hInstance, settings);
window->load(ConvertW(homepage.c_str()));
window->show();
g_hwnd = window->GetHWND();
}
if (!StartWebServer()) {
FatalError(NULL, "Could not start internal web server, Exiting application...");
}
hTrayMenu = LoadMenu(GetModuleHandle(0), MAKEINTRESOURCE(IDR_POPUP_MENU));
Shell_NotifyIcon(NIM_ADD, &GetTrayData(g_hwnd));
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
MSG msg;
int ret;
while ((ret = GetMessage(&msg, 0, 0, 0)) != 0) {
if (ret == -1) {
FLOG_ERROR << "WinMain.GetMessage() returned -1";
_ASSERT(false);
break;
} else {
if (!ProcessKeyboardMessage(&msg, engine) && !TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
if ("msie" == engine) {
OleUninitialize();
}
FLOG_INFO << "Ended application";
FLOG_INFO << "--------------------------------------------------------";
ShutdownLogging();
return ret;
}