sp_gui.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. #include "precompile.h"
  2. #include "sp_gui.h"
  3. #include "sp_def.h"
  4. #include "memutil.h"
  5. #include "log_define.h"
  6. #ifdef _WIN32
  7. #include <tchar.h>
  8. #include "resource1.h"
  9. #include <atlbase.h>
  10. #include <gdiplus.h>
  11. #pragma comment(lib,"gdiplus")
  12. using namespace Gdiplus;
  13. #include "resource1.h"
  14. #include "sp_checkEntity.h"
  15. #endif //_WIN32
  16. #include "SimpleString.h"
  17. #include <string>
  18. #include <list>
  19. using namespace std;
  20. #include "sp_env.h"
  21. #include "array.h"
  22. #include "sp_cfg.h"
  23. #include "SpBase.h"
  24. #include <winpr/wtypes.h>
  25. #define SHELL_WND_CLS "SpShell_Wnd"
  26. #define BLUE_COLOR RGB(0, 0, 255)
  27. #define WHITE_COLOR RGB(255,255, 255)
  28. #define RED_COLOR RGB(255,0, 0)
  29. #define WM_DISPLAY (WM_APP+1)
  30. #define WM_UNDISPLAY (WM_APP+2)
  31. #define FONT_SMALL_SIZE 20 // show message in a big font
  32. #define FONT_LARGE_SIZE 48 // show message in a big font
  33. int g_guiShowFirst = 1;
  34. struct sp_gui_t
  35. {
  36. #ifdef _WIN32
  37. HINSTANCE hInst;
  38. HWND hWnd;
  39. HWND hWndEditBox;
  40. HFONT hSmallFont;
  41. HFONT hLargeFont;
  42. //HPEN hPen;
  43. HBRUSH hBkBrush;
  44. BOOL bShow;
  45. HANDLE hThreadWorker;
  46. int iNotifyResult;
  47. HANDLE hEventNotify;
  48. BOOL bBlueScreen;
  49. ULONG_PTR pGDIToken;
  50. #endif // _WIN32
  51. };
  52. #ifdef _WIN32
  53. static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  54. static int GetTotalEntityCount()
  55. {
  56. auto env = sp_get_env();
  57. if (env == NULL) {
  58. return getEntityResource()->m_EntityCount;
  59. }
  60. auto cfg = env->cfg;
  61. if (cfg == NULL) {
  62. return getEntityResource()->m_EntityCount;
  63. }
  64. return cfg->shell_ini->arr_entity->nelts;
  65. }
  66. static const wchar_t *GetTerminalInfo()
  67. {
  68. static wchar_t wszInfo[256] = {};
  69. if (wszInfo[0] != 0)
  70. return wszInfo;
  71. auto env = sp_get_env();
  72. if (env == NULL) {
  73. return NULL;
  74. }
  75. auto cfg = env->cfg;
  76. if (cfg == NULL) {
  77. return NULL;
  78. }
  79. swprintf(wszInfo, L"%S %S V%d.%d.%d.%d",
  80. cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no,
  81. cfg->root_ini->site == NULL ? "" : cfg->root_ini->site,
  82. cfg->install_ini->install_version.major,
  83. cfg->install_ini->install_version.minor,
  84. cfg->install_ini->install_version.revision,
  85. cfg->install_ini->install_version.build);
  86. return wszInfo;
  87. }
  88. static int OnCreate(sp_gui_t *gui, HWND hWnd, WPARAM wParam, LPARAM lParam)
  89. {
  90. HDC hdc;
  91. long lfHeight;
  92. LPCREATESTRUCTA lpCreateStruct = (LPCREATESTRUCTA)lParam;
  93. int nLogPixelsY;
  94. //gui->hWndEditBox = CreateWindowA("STATIC", "", WS_VISIBLE | WS_CHILD/*|SS_CENTER*/, 20, 20,
  95. // lpCreateStruct->cx - 20, lpCreateStruct->cy - 20, hWnd, (HMENU)101, gui->hInst, NULL);
  96. //gui->hWndEditBox = CreateWindowA("EDIT", "", WS_VISIBLE | WS_CHILD | ES_READONLY | ES_MULTILINE | ES_AUTOVSCROLL,
  97. // 10, 10, lpCreateStruct->cx - 20, lpCreateStruct->cy - 20, hWnd, (HMENU)101, gui->hInst, NULL);
  98. hdc = GetDC(NULL);
  99. nLogPixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
  100. ReleaseDC(NULL, hdc);
  101. lfHeight = -MulDiv(FONT_SMALL_SIZE, nLogPixelsY, 72);
  102. //gui->hSmallFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
  103. gui->hSmallFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "微软雅黑");
  104. lfHeight = -MulDiv(FONT_LARGE_SIZE, nLogPixelsY, 72);
  105. //gui->hLargeFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
  106. gui->hLargeFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "微软雅黑");
  107. //SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT);
  108. return 0;
  109. }
  110. static int OnDestroy(sp_gui_t *gui, HWND hWnd, WPARAM wParam, LPARAM lParam)
  111. {
  112. DestroyWindow(gui->hWndEditBox);
  113. gui->hWndEditBox = NULL;
  114. DeleteObject(gui->hSmallFont);
  115. DeleteObject(gui->hLargeFont);
  116. PostQuitMessage(0);
  117. return 0;
  118. }
  119. typedef void (WINAPI *PSwitchToThisWindow)(HWND, BOOL);
  120. static int init(sp_gui_t *gui)
  121. {
  122. //GdiPlus初始化
  123. GdiplusStartupInput gdiplusInput;
  124. GdiplusStartup(&gui->pGDIToken, &gdiplusInput, NULL);
  125. // 窗口类注册
  126. WNDCLASSA wc = {0};
  127. gui->hInst = GetModuleHandleA(NULL);
  128. gui->hBkBrush = wc.hbrBackground = CreateSolidBrush(BLUE_COLOR);
  129. wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
  130. wc.hIcon = LoadIconA(NULL, MAKEINTRESOURCEA(IDI_APPLICATION));
  131. wc.hInstance = gui->hInst;
  132. wc.lpfnWndProc = &WndProc;
  133. wc.lpszClassName = SHELL_WND_CLS;
  134. wc.style = CS_HREDRAW | CS_VREDRAW;
  135. if (RegisterClassA(&wc) == 0)
  136. return -1;
  137. gui->hWnd = CreateWindowExA(WS_EX_TOOLWINDOW, wc.lpszClassName, "", WS_POPUP|WS_BORDER,
  138. 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, gui->hInst, gui);
  139. if (gui->hWnd == NULL)
  140. return Error_Unexpect;
  141. gui->bShow = TRUE;
  142. RegisterTouchWindow(gui->hWnd, 0); // support WM_TOUCH
  143. ShowWindow(gui->hWnd, g_guiShowFirst ? SW_SHOW : SW_HIDE);
  144. UpdateWindow(gui->hWnd);
  145. //BringWindowToTop(gui->hWnd);
  146. //SetWindowPos(gui->hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  147. return 0;
  148. }
  149. static int term(sp_gui_t *gui)
  150. {
  151. //DestroyWindow(gui->hWnd);
  152. //gui->hWnd = NULL;
  153. UnregisterClassA(SHELL_WND_CLS, gui->hInst);
  154. DeleteObject(gui->hBkBrush);
  155. gui->hBkBrush = NULL;
  156. //GdiPlus 取消初始化
  157. GdiplusShutdown(gui->pGDIToken);
  158. return 0;
  159. }
  160. static unsigned int __stdcall work_proc(void *param)
  161. {
  162. sp_gui_t *gui = (sp_gui_t*)param;
  163. int rc;
  164. MSG msg;
  165. rc = init(gui);
  166. gui->iNotifyResult = rc;
  167. SetEvent(gui->hEventNotify);
  168. if (rc != 0)
  169. return rc;
  170. //--> will disable the Display and Sleep Idle Timeouts .
  171. SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
  172. while (GetMessageA(&msg, NULL, 0, 0)) {
  173. TranslateMessage(&msg);
  174. DispatchMessageA(&msg);
  175. }
  176. SetThreadExecutionState(ES_CONTINUOUS);
  177. term(gui);
  178. return 0;
  179. }
  180. // 从资源中读出Logo
  181. static int LoadLogoImage(int nResourceID, Image *&pImage)
  182. {
  183. auto hMod = GetModuleHandle("spbase");
  184. auto hResInfo = FindResource(hMod, MAKEINTRESOURCE(nResourceID), TEXT("PNG"));
  185. if (hResInfo == NULL)
  186. return 1;
  187. auto hResData = LoadResource(hMod, hResInfo);
  188. if (hResData == NULL)
  189. return 2;
  190. auto pResData = LockResource(hResData);
  191. if (pResData == NULL)
  192. return 3;
  193. int iSize = SizeofResource(hMod, hResInfo);
  194. if (iSize <= 0)
  195. return 4;
  196. HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, iSize);
  197. if (!hMem)
  198. {
  199. UnlockResource(hResData);
  200. return 5;
  201. }
  202. LPVOID pTemp = ::GlobalLock(hMem);
  203. if (pTemp == NULL)
  204. {
  205. UnlockResource(hResData);
  206. GlobalFree(hMem);
  207. return 6;
  208. }
  209. // copy data
  210. memcpy(pTemp, pResData, iSize);
  211. ::GlobalUnlock(hMem);
  212. UnlockResource(hResData);
  213. CComPtr<IStream> spStream;
  214. HRESULT hr = ::CreateStreamOnHGlobal(hMem, TRUE, &spStream);
  215. if (!SUCCEEDED(hr))
  216. {
  217. GlobalFree(hMem);
  218. return 7;
  219. }
  220. pImage = Image::FromStream(spStream);
  221. if (pImage == NULL)
  222. return 8;
  223. return 0;
  224. }
  225. static int DrawConsoleTitle(Graphics &graphics, int nLeftWidth, int nRightWidth)
  226. {
  227. FontFamily fontFamily(L"宋体");
  228. Font font(&fontFamily, 18, FontStyleBold, UnitPoint);
  229. RectF rectF(nLeftWidth, 0, nRightWidth, nRightWidth / 3);
  230. StringFormat format;
  231. format.SetAlignment(StringAlignmentCenter);
  232. format.SetLineAlignment(StringAlignmentCenter);
  233. SolidBrush fontBrush(Color(255, 67, 67, 67));
  234. CSimpleStringW strMsg= TEXT("招商银行视频柜台");
  235. graphics.DrawString(strMsg, strMsg.GetLength(), &font, rectF, &format, &fontBrush);
  236. return 0;
  237. }
  238. static int DrawBackground(Graphics &graphics, int nLeftWidth, int nRightWidth, int nHeight)
  239. {
  240. // draw left
  241. SolidBrush leftBrush(Color(255, 101, 105, 108));
  242. Pen leftPen(&leftBrush);
  243. graphics.DrawRectangle(&leftPen, 0, 0, nLeftWidth, nHeight);
  244. graphics.FillRectangle(&leftBrush, 0, 0, nLeftWidth, nHeight);
  245. // draw right
  246. SolidBrush rightBrush(Color(255, 161, 163, 165));
  247. Pen rightPen(&rightBrush);
  248. graphics.DrawRectangle(&rightPen, nLeftWidth, 0, nRightWidth, nHeight);
  249. graphics.FillRectangle(&rightBrush, nLeftWidth, 0, nRightWidth, nHeight);
  250. // draw logo
  251. Image *pLogoImage = NULL;
  252. if (LoadLogoImage(IDB_VTMLOGO, pLogoImage) !=0)
  253. {
  254. // 直接输出可视柜台文字
  255. DrawConsoleTitle(graphics, nLeftWidth, nRightWidth);
  256. }
  257. else
  258. {
  259. graphics.DrawImage(pLogoImage, nLeftWidth + nRightWidth / 8, 20, nRightWidth * 3 / 4, nRightWidth * 3 / 4 / 3);
  260. }
  261. delete pLogoImage;
  262. return 0;
  263. }
  264. static int DrawLineString(Graphics &graphics, wstring &msg, Font *font, RectF &rect, StringFormat *format, Brush *brush, int nLineSpace, int &nNextLinePosY)
  265. {
  266. nNextLinePosY = rect.Y;
  267. // 一次最多32个
  268. CharacterRange crs[32];
  269. for (int i = 0; i < 32; i++)
  270. {
  271. crs[i].First = i;
  272. crs[i].Length = 1;
  273. }
  274. format->SetFormatFlags(StringFormatFlagsNoClip);
  275. // 当前输出区域
  276. RectF layoutRect = rect;
  277. int nDelta = 60; // 每行输出留白空间
  278. int nFrom = 0;
  279. int nLineCount = 0;
  280. int nTotalWidth = 0;
  281. int nLineHeight = 0;
  282. int nMsgLen = msg.length();
  283. while (nFrom + nLineCount < nMsgLen)
  284. {
  285. Region regions[32] = {};
  286. int nCount = 32;
  287. if (nFrom + nLineCount + nCount > nMsgLen)
  288. nCount = nMsgLen - nFrom - nLineCount;
  289. format->SetMeasurableCharacterRanges(nCount, crs);
  290. //tempMsg = msg.substr(nFrom + nLineCount, nCount);
  291. graphics.MeasureCharacterRanges(msg.data() + nFrom + nLineCount, nCount, font, layoutRect, format, nCount, regions);
  292. for (int i = 0; i < nCount; i++)
  293. {
  294. Rect r;
  295. regions[i].GetBounds(&r, &graphics);
  296. nTotalWidth += r.Width;
  297. if (r.Height > nLineHeight)
  298. nLineHeight = r.Height;
  299. if (nTotalWidth <= layoutRect.Width - nDelta && msg[nFrom + nLineCount] != TEXT('\r') && msg[nFrom + nLineCount] != TEXT('\n'))
  300. {
  301. nLineCount++;
  302. }
  303. else
  304. {
  305. if (msg[nFrom + nLineCount] == TEXT('\r') || msg[nFrom + nLineCount] == TEXT('\n'))
  306. {
  307. if (nFrom + nLineCount + 1 < nMsgLen
  308. && ((msg[nFrom + nLineCount] == TEXT('\r') && msg[nFrom + nLineCount + 1] == TEXT('\n'))
  309. || (msg[nFrom + nLineCount] == TEXT('\n') && msg[nFrom + nLineCount + 1] == TEXT('\r'))))
  310. {
  311. nLineCount += 2;
  312. i++;
  313. }
  314. else
  315. nLineCount++;
  316. }
  317. //auto wszLine = msg.substr(nFrom, nLineCount);
  318. graphics.DrawString(msg.data() + nFrom, nLineCount, font, layoutRect, format, brush);
  319. // reset vars
  320. nFrom += nLineCount;
  321. nLineCount = 0;
  322. nTotalWidth = 0;
  323. layoutRect.Y += nLineHeight + nLineSpace;
  324. //nLineHeight = 0;
  325. nNextLinePosY = layoutRect.Y;
  326. // 写满显示高度,直接返回
  327. if (layoutRect.Y >= rect.Y + rect.Height)
  328. return 0;
  329. }
  330. }
  331. }
  332. // output last line
  333. if (nLineCount > 0)
  334. {
  335. graphics.DrawString(msg.data() + nFrom, nLineCount, font, layoutRect, format, brush);
  336. nNextLinePosY += nLineHeight + nLineSpace;
  337. }
  338. return 0;
  339. }
  340. static int DrawRunningInfo(Graphics &graphics, int nLeftWidth, int nRightWidth, int nHeight)
  341. {
  342. // 全部输出
  343. // get lock
  344. auto entityRes = getEntityResource();
  345. while (InterlockedCompareExchange(&entityRes->m_Locking, 1, 0) == 1)
  346. {
  347. Sleep(100);
  348. }
  349. // draw left
  350. wstring strLeft;
  351. FontFamily fontFamily(L"微软雅黑");
  352. SolidBrush blackBrush(Color(255, 0, 0, 0));
  353. SolidBrush whiteBrush(Color(255, 255, 255, 255));
  354. SolidBrush redBrush(Color(255, 174, 0, 0));
  355. SolidBrush redBrush2(Color(255, 130, 0, 0));
  356. if (entityRes->m_InBlueScreenMode)
  357. {
  358. strLeft = entityRes->m_BlueScreenMsg;
  359. Font font(&fontFamily, 32, FontStyleBold, UnitPoint);
  360. StringFormat format;
  361. format.SetFormatFlags(StringFormatFlagsNoClip);
  362. format.SetLineAlignment(StringAlignmentCenter);
  363. RectF rectF(10, 10, nLeftWidth - 20, nHeight - 20);
  364. graphics.DrawString(strLeft.c_str(), strLeft.length(), &font, rectF, &format, &whiteBrush);
  365. }
  366. else
  367. {
  368. Font font(&fontFamily, 10, FontStyleRegular, UnitPoint);
  369. StringFormat format;
  370. int nNextLinePosY = 10;
  371. auto it = entityRes->m_OutputMsgs.rbegin();
  372. if (entityRes->m_InBrowseMode)
  373. {
  374. for (int i = 0; i < entityRes->m_SkipLineNum && i < (int)entityRes->m_OutputMsgs.size(); i++)
  375. it++;
  376. }
  377. for (; it != entityRes->m_OutputMsgs.rend() && nNextLinePosY < nHeight; it++)
  378. {
  379. auto &strLine = *it;
  380. // output
  381. Brush *brush = NULL;
  382. if (wcsstr(strLine.c_str(), L"] W:{") != NULL)
  383. brush = &whiteBrush;
  384. else if (wcsstr(strLine.c_str(), L"] E:{") != NULL)
  385. brush = &redBrush2;
  386. else
  387. brush = &blackBrush;
  388. RectF rectF(10, nNextLinePosY, nLeftWidth - 20, nHeight - 20);
  389. DrawLineString(graphics, strLine, &font, rectF, &format, brush, 5, nNextLinePosY);
  390. }
  391. }
  392. // draw right
  393. wstring strLostEntitys;
  394. wstring strSum;
  395. int nStartedCount = 0;
  396. int nLostCount = 0;
  397. int nToStartCount = 0;
  398. wchar_t strTmp[128] = {};
  399. for (int i = 0; i < entityRes->m_EntityCount; i++)
  400. {
  401. if (entityRes->m_EntitysInfo[i].EntityState >= 2 && entityRes->m_EntitysInfo[i].EntityState <= 4)
  402. {
  403. nStartedCount++;
  404. }
  405. else if (entityRes->m_EntitysInfo[i].EntityState == 6)
  406. {
  407. nLostCount++;
  408. swprintf_s(strTmp, 128, L" 模块[%s]启动失败\r\n", entityRes->m_EntitysInfo[i].EntityName.c_str());
  409. strLostEntitys += strTmp;
  410. }
  411. }
  412. // 输出终端号、场所、版本
  413. StringFormat centerFormat;
  414. centerFormat.SetAlignment(StringAlignmentCenter);
  415. auto wszTerminalInfo = GetTerminalInfo();
  416. if (wszTerminalInfo != NULL)
  417. {
  418. Font rfont(&fontFamily, 10, FontStyleRegular, UnitPoint);
  419. RectF rrectF(nLeftWidth + 10, 110, nRightWidth - 20, nHeight - 180);
  420. int nNextLinePosY;
  421. DrawLineString(graphics, wstring(wszTerminalInfo), &rfont, rrectF, &centerFormat, &blackBrush, 20, nNextLinePosY);
  422. }
  423. // 实体启动成功计数
  424. Font rfont(&fontFamily, 12, FontStyleBold, UnitPoint);
  425. swprintf_s(strTmp, 128, L"%d/%d 个模块启动成功", nStartedCount, GetTotalEntityCount());
  426. strSum = strTmp;
  427. int nNextLinePosY;
  428. RectF rrectF(nLeftWidth + 10, 140, nRightWidth - 20, nHeight - 180);
  429. DrawLineString(graphics, strSum, &rfont, rrectF, &centerFormat, &blackBrush, 20, nNextLinePosY);
  430. // 输出当前启动状态信息
  431. Font rfont2(&fontFamily, 11, FontStyleRegular, UnitPoint);
  432. if (entityRes->m_StartupInfo.length() > 0)
  433. {
  434. RectF rrectF2(nLeftWidth + 6, nNextLinePosY, nRightWidth, nHeight - 180);
  435. DrawLineString(graphics, entityRes->m_StartupInfo, &rfont2, rrectF2, &centerFormat, &whiteBrush, 5, nNextLinePosY);
  436. nNextLinePosY += 10;
  437. }
  438. // 输出实体启动错误
  439. StringFormat rformat2;
  440. {
  441. RectF rrectF2(nLeftWidth + 30, nNextLinePosY, nRightWidth, nHeight - 200);
  442. DrawLineString(graphics, strLostEntitys, &rfont2, rrectF2, &rformat2, &redBrush, 5, nNextLinePosY);
  443. }
  444. // 输出严重错误信息
  445. for (auto it2 = entityRes->m_FatalMsgs.rbegin(); it2!= entityRes->m_FatalMsgs.rend() && nNextLinePosY < nHeight; it2++)
  446. {
  447. RectF rrectF2(nLeftWidth + 30, nNextLinePosY, nRightWidth, nHeight - 200);
  448. wstring strLine = L"\r\n 【" + *it2+ L"】";
  449. DrawLineString(graphics, strLine, &rfont2, rrectF2, &rformat2, &redBrush, 5, nNextLinePosY);
  450. }
  451. // release lock
  452. InterlockedExchange(&entityRes->m_Locking, 0);
  453. return 0;
  454. }
  455. static int RepaintWindow(HWND hWnd, bool bCanUseCache)
  456. {
  457. static RECT lastRect = {};
  458. static HDC hBkgMemDC = NULL;
  459. if (!g_guiShowFirst)
  460. return 0;
  461. RECT rect = {};
  462. GetClientRect(hWnd, &rect);
  463. HDC hDC = GetDC(hWnd);
  464. if (lastRect.right == rect.right && lastRect.bottom == rect.bottom && hBkgMemDC != NULL && bCanUseCache)
  465. {
  466. // copy mem dc to hwnd dc
  467. //MessageBox(NULL, TEXT("use cached dc"), NULL, 0);
  468. BitBlt(hDC, 0, 0, rect.right, rect.bottom, hBkgMemDC, 0, 0, SRCCOPY);
  469. ReleaseDC(hWnd, hDC);
  470. return 0;
  471. }
  472. if (hBkgMemDC != NULL)
  473. {
  474. DeleteDC(hBkgMemDC);
  475. hBkgMemDC = NULL;
  476. }
  477. lastRect = rect;
  478. int nWidth = rect.right - rect.left;
  479. int nHeight = rect.bottom - rect.top;
  480. int nRightWidth = nWidth / 3;
  481. if (nRightWidth > 450)
  482. nRightWidth = 450;
  483. int nLeftWidth = nWidth - nRightWidth;
  484. hBkgMemDC = CreateCompatibleDC(hDC);
  485. auto hMemBitmap = CreateCompatibleBitmap(hDC, nWidth, nHeight);
  486. auto hOldObject = SelectObject(hBkgMemDC, hMemBitmap);
  487. Graphics graphics(hBkgMemDC);
  488. // draw background image
  489. DrawBackground(graphics, nLeftWidth, nRightWidth, nHeight);
  490. // draw output string
  491. DrawRunningInfo(graphics, nLeftWidth, nRightWidth, nHeight);
  492. // copy mem dc to hwnd dc
  493. BitBlt(hDC, 0, 0, rect.right, rect.bottom, hBkgMemDC, 0, 0, SRCCOPY);
  494. SelectObject(hBkgMemDC, hOldObject);
  495. DeleteObject(hMemBitmap);
  496. //DeleteDC(hBkgMemDC);
  497. ReleaseDC(hWnd, hDC);
  498. return 0;
  499. }
  500. static void SetBrowseModeShift(int nShiftLines)
  501. {
  502. if ((!getEntityResource()->m_InBrowseMode && nShiftLines <= 0) || getEntityResource()->m_OutputMsgs.size() <=20)
  503. return;
  504. // get lock
  505. auto entityRes = getEntityResource();
  506. while (InterlockedCompareExchange(&entityRes->m_Locking, 1, 0) == 1)
  507. {
  508. Sleep(100);
  509. }
  510. getEntityResource()->m_InBrowseMode = true;
  511. getEntityResource()->m_SkipLineNum += nShiftLines;
  512. if (nShiftLines > 0)
  513. {
  514. if (getEntityResource()->m_SkipLineNum + 20 > (int)getEntityResource()->m_OutputMsgs.size())
  515. {
  516. getEntityResource()->m_SkipLineNum = (int)getEntityResource()->m_OutputMsgs.size() - 20;
  517. }
  518. }
  519. else
  520. {
  521. if (getEntityResource()->m_SkipLineNum <= 0)
  522. {
  523. // 解除浏览模式
  524. getEntityResource()->m_SkipLineNum = 0;
  525. getEntityResource()->m_InBrowseMode = false;
  526. }
  527. }
  528. // release lock
  529. InterlockedExchange(&getEntityResource()->m_Locking, 0);
  530. }
  531. static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  532. {
  533. sp_gui_t *gui = NULL;
  534. static POINTS lastPoint = {};
  535. if (msg == WM_CREATE) {
  536. LPCREATESTRUCTA lpCreateStruct = (LPCREATESTRUCTA)lParam;
  537. gui = (sp_gui_t*)lpCreateStruct->lpCreateParams;
  538. SetWindowLongA(hWnd, GWL_USERDATA, (LONG)gui);
  539. }
  540. else {
  541. gui = (sp_gui_t*)GetWindowLongA(hWnd, GWL_USERDATA);
  542. }
  543. switch (msg) {
  544. case WM_CREATE:
  545. {
  546. if (OnCreate(gui, hWnd, wParam, lParam) != 0)
  547. return -1;
  548. }
  549. break;
  550. case WM_CLOSE:
  551. DestroyWindow(hWnd);
  552. break;
  553. case WM_MOUSEWHEEL:
  554. {
  555. getEntityResource()->m_LastShiftTick = GetTickCount();
  556. short sDelta = (short)HIWORD(wParam);
  557. SetBrowseModeShift(sDelta > 0 ? 10 : -10);
  558. RepaintWindow(hWnd, false);
  559. }
  560. break;
  561. case WM_TOUCH:
  562. {
  563. getEntityResource()->m_LastShiftTick = GetTickCount();
  564. DWORD nInputNum = (DWORD)wParam;
  565. TOUCHINPUT *tis = new TOUCHINPUT[nInputNum];\
  566. if (GetTouchInputInfo((HTOUCHINPUT)lParam, nInputNum, tis, sizeof(TOUCHINPUT)))
  567. {
  568. for (int i = 0; i < nInputNum; i++)
  569. {
  570. POINT pt = {};
  571. pt.x = TOUCH_COORD_TO_PIXEL(tis[i].x);
  572. pt.y = TOUCH_COORD_TO_PIXEL(tis[i].y);
  573. ScreenToClient(gui->hWnd, &pt);
  574. if (tis[i].dwFlags & TOUCHEVENTF_DOWN)
  575. {
  576. getEntityResource()->m_LastTouchID = tis[i].dwID;
  577. getEntityResource()->m_LastTouchYPos = pt.y;
  578. }
  579. else if (tis[i].dwFlags & TOUCHEVENTF_MOVE)
  580. {
  581. }
  582. else if (tis[i].dwFlags & TOUCHEVENTF_UP)
  583. {
  584. if (tis[i].dwID == getEntityResource()->m_LastTouchID)
  585. {
  586. SetBrowseModeShift(pt.y < getEntityResource()->m_LastTouchYPos ? 10 : -10);
  587. RepaintWindow(hWnd, false);
  588. getEntityResource()->m_LastTouchID = 0;
  589. getEntityResource()->m_LastTouchYPos = 0;
  590. }
  591. }
  592. }
  593. CloseTouchInputHandle((HTOUCHINPUT)lParam);
  594. delete[] tis;
  595. }
  596. }
  597. break;
  598. case WM_DESTROY:
  599. OnDestroy(gui, hWnd, wParam, lParam);
  600. break;
  601. case WM_PAINT:
  602. {
  603. PAINTSTRUCT ps;
  604. HDC hdc = BeginPaint(hWnd, &ps);
  605. RepaintWindow(hWnd, true);
  606. EndPaint(hWnd, &ps);
  607. }
  608. break;
  609. case WM_KEYDOWN:
  610. {
  611. if (wParam == VK_F3) { // hide
  612. SendMessageA(hWnd, WM_UNDISPLAY, 0, 0);
  613. }
  614. }
  615. break;
  616. case WM_CTLCOLORSTATIC:
  617. {
  618. HDC hDC = (HDC)wParam;
  619. HWND hWndControl = (HWND)lParam;
  620. SetBkColor(hDC, BLUE_COLOR);
  621. SetTextColor(hDC, WHITE_COLOR);
  622. return (LRESULT)gui->hBkBrush;
  623. }
  624. break;
  625. case WM_DISPLAY:
  626. {
  627. RepaintWindow(hWnd, false);
  628. if (getEntityResource()->m_InBlueScreenMode && !gui->bShow)
  629. {
  630. gui->bShow = TRUE;
  631. ShowWindow(gui->hWnd, SW_SHOW);
  632. }
  633. }
  634. break;
  635. case WM_UNDISPLAY:
  636. {
  637. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("WM_UNDISPLAY, show gui undisplay");
  638. ShowWindow(hWnd, SW_HIDE);
  639. UpdateWindow(hWnd);
  640. gui->bShow = FALSE;
  641. }
  642. break;
  643. default:
  644. return DefWindowProc(hWnd, msg, wParam, lParam);
  645. }
  646. return 0;
  647. }
  648. #endif //_WIN32
  649. void sp_gui_setShow(int isShowFirst)
  650. {
  651. g_guiShowFirst = isShowFirst;
  652. }
  653. int sp_gui_create(sp_gui_t **p_gui)
  654. {
  655. sp_gui_t *gui = ZALLOC_T(sp_gui_t);
  656. #ifdef RVC_OS_WIN
  657. gui->hEventNotify = CreateEventA(NULL, TRUE, FALSE, NULL);
  658. if (!gui->hEventNotify)
  659. goto on_error;
  660. gui->hThreadWorker = (HANDLE)_beginthreadex(NULL, 0, &work_proc, gui, 0, NULL);
  661. if (!gui->hThreadWorker)
  662. goto on_error;
  663. WaitForSingleObject(gui->hEventNotify, INFINITE);
  664. if (gui->iNotifyResult)
  665. goto on_error;
  666. #endif //RVC_OS_WIN
  667. *p_gui = gui;
  668. return 0;
  669. #ifdef RVC_OS_WIN
  670. on_error :
  671. if (gui->hThreadWorker)
  672. CloseHandle(gui->hThreadWorker);
  673. if (gui->hEventNotify)
  674. CloseHandle(gui->hEventNotify);
  675. free(gui);
  676. return Error_Unexpect;
  677. #endif //RVC_OS_WIN
  678. }
  679. void sp_gui_destroy(sp_gui_t *gui)
  680. {
  681. #ifdef _WIN32
  682. PostMessageA(gui->hWnd, WM_CLOSE, 0, 0);
  683. WaitForSingleObject(gui->hThreadWorker, INFINITE);
  684. CloseHandle(gui->hThreadWorker);
  685. CloseHandle(gui->hEventNotify);
  686. #endif //_WIN32
  687. free(gui);
  688. }
  689. #if defined(_MSC_VER)
  690. int sp_gui_show_running_info(sp_gui_t* gui, const char* pMsg, int type)
  691. {
  692. // get lock
  693. auto entityRes = getEntityResource();
  694. while (InterlockedCompareExchange(&entityRes->m_Locking, 1, 0) == 1)
  695. Sleep(100);
  696. SYSTEMTIME st = {};
  697. GetLocalTime(&st);
  698. wchar_t szBuf[1024] = {};
  699. CSimpleStringA curMsg = pMsg;
  700. CSimpleStringW dstMsg = CSimpleStringA2W(curMsg);
  701. dstMsg = dstMsg.SubString(0, 1000);
  702. if (type == 1) {
  703. // bluescreen
  704. entityRes->m_InBlueScreenMode = true;
  705. swprintf_s(szBuf, 1024, L"%s\r\n", dstMsg.GetData());
  706. entityRes->m_BlueScreenMsg = szBuf;
  707. } else if (type == 2) {
  708. // fatal error
  709. swprintf_s(szBuf, 1024, L"%s", dstMsg.GetData());
  710. entityRes->m_FatalMsgs.push_back(szBuf);
  711. if (entityRes->m_FatalMsgs.size() > 100)
  712. entityRes->m_FatalMsgs.pop_front();
  713. } else if (type == 3) {
  714. // important startup info
  715. swprintf_s(szBuf, 1024, L"%s", dstMsg.GetData());
  716. entityRes->m_StartupInfo = szBuf;
  717. } else {
  718. // startup info
  719. swprintf_s(szBuf, 1024, L"[%02d:%02d:%02d] %s\r\n", st.wHour, st.wMinute, st.wSecond, dstMsg.GetData());
  720. entityRes->m_OutputMsgs.push_back(szBuf);
  721. // 只保留最近500条
  722. if (entityRes->m_OutputMsgs.size() > 500)
  723. entityRes->m_OutputMsgs.pop_front();
  724. }
  725. // browse mode continues 30s
  726. if (entityRes->m_InBrowseMode && GetTickCount() - entityRes->m_LastShiftTick >= 30000) {
  727. entityRes->m_InBrowseMode = false;
  728. entityRes->m_SkipLineNum = 0;
  729. }
  730. // release lock
  731. InterlockedExchange(&entityRes->m_Locking, 0);
  732. if (type >= 1 || (!entityRes->m_InBlueScreenMode && !entityRes->m_InBrowseMode)) {
  733. if (!PostMessageA(gui->hWnd, WM_DISPLAY, NULL, NULL)) {
  734. return Error_Unexpect;
  735. }
  736. }
  737. return 0;
  738. }
  739. int sp_gui_show_entity_info(sp_gui_t* gui, const char* entity, int state)
  740. {
  741. auto strEntity = CSimpleStringA2W(CSimpleStringA(entity));
  742. auto entityRes = getEntityResource();
  743. int i = 0;
  744. for (; i < entityRes->m_EntityCount; i++) {
  745. if (wcsicmp(strEntity, entityRes->m_EntitysInfo[i].EntityName.c_str()) == 0) {
  746. entityRes->m_EntitysInfo[i].EntityState = state;
  747. break;
  748. }
  749. }
  750. if (i == entityRes->m_EntityCount && entityRes->m_EntityCount < 80) {
  751. // new entity
  752. entityRes->m_EntitysInfo[i].EntityName = strEntity;
  753. entityRes->m_EntitysInfo[i].EntityState = state;
  754. entityRes->m_EntityCount++;
  755. }
  756. if (!PostMessageA(gui->hWnd, WM_DISPLAY, NULL, NULL)) {
  757. return Error_Unexpect;
  758. }
  759. return 0;
  760. }
  761. int sp_gui_display(sp_gui_t* gui)
  762. {
  763. if (!gui->bShow) {
  764. gui->bShow = TRUE;
  765. HWND hShellWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
  766. if (hShellWnd != NULL)
  767. SendMessage(hShellWnd, WM_COMMAND, 419, 0);
  768. ShowWindow(gui->hWnd, SW_SHOW);
  769. }
  770. return 0;
  771. }
  772. int sp_gui_undisplay(sp_gui_t* gui)
  773. {
  774. getEntityResource()->m_InBlueScreenMode = false;
  775. if (!PostMessageA(gui->hWnd, WM_UNDISPLAY, 0, 0))
  776. return Error_Unexpect;
  777. return 0;
  778. }
  779. #else
  780. int sp_gui_create_format(sp_gui_format_t** p_gui)
  781. {
  782. int result = 0;
  783. sp_gui_format_t* gui = ZALLOC_T(sp_gui_format_t);
  784. sp_gui_t* inst = NULL;
  785. result = sp_gui_create(&inst);
  786. if (result == 0) {
  787. gui->gui_inst = inst;
  788. gui->display = &sp_gui_display;
  789. gui->hide = &sp_gui_undisplay;
  790. gui->show_entity_info = &sp_gui_show_entity_info;
  791. gui->show_running_info = &sp_gui_show_running_info;
  792. gui->post_message = &sp_gui_post_message;
  793. *p_gui = gui;
  794. } else {
  795. FREE(gui);
  796. }
  797. return result;
  798. }
  799. void sp_gui_destroy_format(sp_gui_format_t* p_gui)
  800. {
  801. sp_gui_destroy((sp_gui_t*)p_gui->gui_inst);
  802. FREE(p_gui);
  803. }
  804. int sp_gui_show_running_info(void* data, const char* pMsg, int type)
  805. {
  806. sp_gui_t* gui = (sp_gui_t*)data;
  807. return 0;
  808. }
  809. int sp_gui_show_entity_info(void* data, const char* entity, int state)
  810. {
  811. //void* data
  812. sp_gui_t* gui = (sp_gui_t*)data;
  813. return 0;
  814. }
  815. int sp_gui_display(void* data)
  816. {
  817. //void* data
  818. sp_gui_t* gui = (sp_gui_t*)data;
  819. return 0;
  820. }
  821. int sp_gui_undisplay(void* data)
  822. {
  823. //void* data
  824. sp_gui_t* gui = (sp_gui_t*)data;
  825. return 0;
  826. }
  827. void sp_gui_post_message(void* gui, unsigned long param1, unsigned long param2)
  828. {
  829. return;
  830. }
  831. #endif //_MSC_VER