Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
16 KiB

  1. /*****************************************************************************
  2. *
  3. * Perfmon.c - This is the WinMain module. It creates the main window and
  4. * the threads, and contains the main MainWndProc.
  5. *
  6. * Microsoft Confidential
  7. * Copyright (c) 1992-1993 Microsoft Corporation
  8. *
  9. * Authors -
  10. *
  11. * Russ Blake
  12. * Mike Moskowitz
  13. * Hon-Wah Chan
  14. * Bob Watson
  15. *
  16. ****************************************************************************/
  17. //==========================================================================//
  18. // Includes //
  19. //==========================================================================//
  20. #undef NOSYSCOMMANDS
  21. // DEFINE_GLOBALS will define all the globals listed in globals.h
  22. #define DEFINE_GLOBALS
  23. #include "perfmon.h"
  24. #include <commctrl.h> // for tool tip & tool bar definitions
  25. #include <stdio.h> // just for now
  26. #include "command.h"
  27. #include "graph.h"
  28. #include "log.h"
  29. #include "alert.h"
  30. #include "report.h" // for CreateReportWindow
  31. #include "legend.h"
  32. #include "init.h"
  33. #include "perfmops.h"
  34. #include "toolbar.h" // for CreateToolbar
  35. #include "status.h" // for CreatePMStatusWindow
  36. #include "utils.h"
  37. #include "fileopen.h" // for FileOpen
  38. #define dwToolbarStyle (WS_CHILD | WS_VISIBLE | TBS_NOCAPTION)
  39. extern TCHAR szInternational[] ;
  40. //==========================================================================//
  41. // Message Handlers //
  42. //==========================================================================//
  43. void
  44. static
  45. OnSize (
  46. HWND hWnd,
  47. WORD xWidth,
  48. WORD yHeight
  49. )
  50. /*
  51. Effect: Perform any actions needed when the main window is
  52. resized. In particular, size the four data windows,
  53. only one of which is visible right now.
  54. */
  55. {
  56. SizePerfmonComponents () ;
  57. }
  58. BOOL
  59. static
  60. ShowSysmonNotice (
  61. HWND hWnd
  62. )
  63. /*
  64. returns TRUE if perfmon should continue or
  65. FALSE if perfmon should exit
  66. */
  67. {
  68. BOOL bStatus;
  69. UINT nRet;
  70. DWORD dwSize;
  71. STARTUPINFO StartupInfo;
  72. PROCESS_INFORMATION ProcessInformation;
  73. WCHAR szString1[MAX_PATH * 2];
  74. WCHAR szString2[MAX_PATH * 2];
  75. WCHAR szCmdPath[MAX_PATH * 2];
  76. dwSize = sizeof(szString1) / sizeof(szString1[0]);
  77. LoadString (hInstance, SP_NOTICE_TEXT,
  78. szString1, dwSize);
  79. dwSize = sizeof(szString2) / sizeof(szString2[0]);
  80. LoadString (hInstance, SP_NOTICE_CAPTION,
  81. szString2, dwSize);
  82. nRet = MessageBoxW (
  83. hWnd,
  84. szString1,
  85. szString2,
  86. MB_OKCANCEL | MB_ICONEXCLAMATION);
  87. if (nRet == IDOK) {
  88. // then start the MMC with the sysmon console
  89. StartupInfo.cb = sizeof(StartupInfo);
  90. StartupInfo.lpReserved = NULL;
  91. StartupInfo.lpDesktop = NULL;
  92. StartupInfo.lpTitle = NULL;
  93. StartupInfo.dwX = CW_USEDEFAULT;
  94. StartupInfo.dwY = CW_USEDEFAULT;
  95. StartupInfo.dwXSize = CW_USEDEFAULT;
  96. StartupInfo.dwYSize = CW_USEDEFAULT;
  97. StartupInfo.dwXCountChars = 0;
  98. StartupInfo.dwYCountChars = 0;
  99. StartupInfo.dwFillAttribute = 0;
  100. StartupInfo.dwFlags = STARTF_USEPOSITION | STARTF_USESIZE;
  101. StartupInfo.wShowWindow = 0;
  102. StartupInfo.cbReserved2 = 0;
  103. StartupInfo.lpReserved2 = 0;
  104. StartupInfo.hStdInput = 0;
  105. StartupInfo.hStdOutput = 0;
  106. StartupInfo.hStdError = 0;
  107. memset (&ProcessInformation, 0, sizeof(ProcessInformation));
  108. dwSize = sizeof(szString1) / sizeof(szString1[0]);
  109. LoadString (hInstance, SP_SYSMON_CMDLINE,
  110. szString1, dwSize);
  111. ExpandEnvironmentStringsW (
  112. szString1,
  113. szCmdPath, (sizeof(szCmdPath)/sizeof(szCmdPath[0])));
  114. bStatus = CreateProcessW (
  115. NULL, szCmdPath,
  116. NULL, NULL, FALSE,
  117. 0,
  118. NULL,
  119. NULL,
  120. &StartupInfo,
  121. &ProcessInformation );
  122. // close the handles if they were opened
  123. if (ProcessInformation.hProcess != NULL) CloseHandle (ProcessInformation.hProcess);
  124. if (ProcessInformation.hThread != NULL) CloseHandle (ProcessInformation.hThread);
  125. if (bStatus) {
  126. // the process was created so
  127. return FALSE; // tell perfmon to exit
  128. } else {
  129. LONG lStatus;
  130. lStatus = GetLastError();
  131. dwSize = sizeof(szString2) / sizeof(szString2[0]);
  132. LoadString (hInstance, SP_SYSMON_CREATE_ERR,
  133. szString2, dwSize);
  134. nRet = MessageBoxW (hWnd,
  135. szString2,
  136. NULL,
  137. MB_OK | MB_ICONEXCLAMATION);
  138. return TRUE; // keep perfmon
  139. }
  140. } else {
  141. return TRUE;
  142. }
  143. }
  144. void
  145. static
  146. OnCreate (
  147. HWND hWnd
  148. )
  149. /*
  150. Effect: Perform all actions needed when the main window is
  151. created. In particular, create the three data windows,
  152. and show one of them.
  153. To Do: Check for proper creation. If not possible, we will
  154. need to abort creation of the program.
  155. Called By: MainWndProc only, in response to a WM_CREATE message.
  156. */
  157. {
  158. hWndGraph = CreateGraphWindow (hWnd) ;
  159. #ifdef ADVANCED_PERFMON
  160. hWndLog = CreateLogWindow (hWnd) ;
  161. hWndAlert = CreateAlertWindow (hWnd) ;
  162. hWndReport = CreateReportWindow (hWnd) ;
  163. #endif
  164. hWndStatus = CreatePMStatusWindow (hWnd) ;
  165. CreateToolbarWnd (hWnd) ;
  166. MinimumSize += WindowHeight (hWndToolbar) ;
  167. Options.bMenubar = TRUE ;
  168. Options.bToolbar = TRUE ;
  169. Options.bStatusbar = TRUE;
  170. Options.bAlwaysOnTop = FALSE ;
  171. // initialize to chart view - HWC
  172. iPerfmonView = IDM_VIEWCHART;
  173. ShowWindow (PerfmonViewWindow (), SW_SHOWNORMAL) ;
  174. }
  175. //==========================================================================//
  176. // Exported Functions //
  177. //==========================================================================//
  178. void
  179. MenuBarHit (
  180. WPARAM wParam
  181. )
  182. {
  183. if (wParam == MENUCLOSING) {
  184. StatusLineReady (hWndStatus) ;
  185. dwCurrentMenuID = 0 ;
  186. } else if (HIWORD(wParam) & MF_SYSMENU) {
  187. WORD SystemMenuItem = 0 ;
  188. switch (LOWORD (wParam)) {
  189. case SC_RESTORE:
  190. SystemMenuItem = IDM_SYSTEMRESTORE ;
  191. break ;
  192. case SC_SIZE:
  193. SystemMenuItem = IDM_SYSTEMSIZE ;
  194. break ;
  195. case SC_MOVE:
  196. SystemMenuItem = IDM_SYSTEMMOVE ;
  197. break ;
  198. case SC_MINIMIZE:
  199. SystemMenuItem = IDM_SYSTEMMINIMIZE ;
  200. break ;
  201. case SC_MAXIMIZE:
  202. SystemMenuItem = IDM_SYSTEMMAXIMIZE ;
  203. break ;
  204. case SC_CLOSE:
  205. SystemMenuItem = IDM_SYSTEMCLOSE ;
  206. break ;
  207. case SC_TASKLIST:
  208. SystemMenuItem = IDM_SYSTEMSWITCHTO ;
  209. break ;
  210. }
  211. if (SystemMenuItem) {
  212. StatusLine (hWndStatus, SystemMenuItem) ;
  213. dwCurrentMenuID = MenuIDToHelpID (SystemMenuItem) ;
  214. }
  215. } else {
  216. StatusLine (hWndStatus, LOWORD (wParam)) ;
  217. }
  218. }
  219. void
  220. OnDropFile (
  221. WPARAM wParam
  222. )
  223. {
  224. TCHAR FileName [FilePathLen + 1] ;
  225. LPTSTR pFileNameStart ;
  226. HANDLE hFindFile ;
  227. WIN32_FIND_DATA FindFileInfo ;
  228. int NameOffset ;
  229. int NumOfFiles = 0 ;
  230. NumOfFiles = DragQueryFile ((HDROP) wParam, 0xffffffff, NULL, 0) ;
  231. if (NumOfFiles > 0) {
  232. // we only open the first file for now
  233. DragQueryFile((HDROP) wParam, 0, FileName, FilePathLen) ;
  234. pFileNameStart = ExtractFileName (FileName) ;
  235. NameOffset = (int)(pFileNameStart - FileName) ;
  236. // convert short filename to long NTFS filename if necessary
  237. hFindFile = FindFirstFile (FileName, &FindFileInfo) ;
  238. if (hFindFile && hFindFile != INVALID_HANDLE_VALUE) {
  239. // append the file name back to the path name
  240. lstrcpy (&FileName[NameOffset], FindFileInfo.cFileName) ;
  241. FindClose (hFindFile) ;
  242. }
  243. FileOpen (hWndMain, (int)0, (LPTSTR)FileName) ;
  244. PrepareMenu (GetMenu (hWndMain));
  245. }
  246. DragFinish ((HDROP) wParam) ;
  247. }
  248. LRESULT
  249. APIENTRY
  250. MainWndProc (
  251. HWND hWnd,
  252. UINT message,
  253. WPARAM wParam,
  254. LPARAM lParam
  255. )
  256. {
  257. LRESULT lRetCode = 0L ;
  258. BOOL bCallDefWinProc = FALSE ;
  259. switch (LOWORD (message)) { // switch
  260. case WM_LBUTTONDBLCLK:
  261. ShowPerfmonMenu (!Options.bMenubar) ;
  262. if (Options.bMenubar) {
  263. PrepareMenu (GetMenu (hWnd)) ;
  264. }
  265. break ;
  266. case WM_COMMAND:
  267. if (PerfmonCommand (hWnd,wParam,lParam))
  268. return(0);
  269. else
  270. bCallDefWinProc = TRUE ;
  271. break;
  272. case WM_MENUSELECT:
  273. MenuBarHit (wParam) ;
  274. break ;
  275. case WM_NCHITTEST:
  276. /* if we have no title/menu bar, clicking and dragging the client
  277. * area moves the window. To do this, return HTCAPTION.
  278. * Note dragging not allowed if window maximized, or if caption
  279. * bar is present.
  280. */
  281. wParam = DefWindowProc(hWnd, message, wParam, lParam);
  282. if (!Options.bMenubar &&
  283. (wParam == HTCLIENT) &&
  284. !IsZoomed (hWndMain))
  285. return HTCAPTION ;
  286. else
  287. return wParam ;
  288. break ;
  289. case WM_SHOWWINDOW:
  290. PrepareMenu (GetMenu (hWnd)) ;
  291. break ;
  292. case WM_SIZE:
  293. OnSize (hWnd, LOWORD (lParam), HIWORD (lParam)) ;
  294. break ;
  295. case WM_GETMINMAXINFO:
  296. {
  297. MINMAXINFO *pMinMax ;
  298. pMinMax = (MINMAXINFO *) lParam ;
  299. pMinMax->ptMinTrackSize.x = MinimumSize ;
  300. pMinMax->ptMinTrackSize.y = MinimumSize ;
  301. }
  302. break ;
  303. case WM_NOTIFY:
  304. {
  305. LPTOOLTIPTEXT lpTTT = (LPTOOLTIPTEXT)lParam;
  306. if (lpTTT->hdr.code == TTN_NEEDTEXT) {
  307. LoadString (hInstance, (UINT)lpTTT->hdr.idFrom, lpTTT->szText,
  308. sizeof(lpTTT->szText)/sizeof(TCHAR));
  309. return TRUE;
  310. } else {
  311. bCallDefWinProc = FALSE ;
  312. break;
  313. }
  314. }
  315. case WM_F1DOWN:
  316. if (dwCurrentDlgID) {
  317. CallWinHelp (dwCurrentDlgID, hWnd) ;
  318. } else if (dwCurrentMenuID) {
  319. CallWinHelp (dwCurrentMenuID, hWnd) ;
  320. dwCurrentMenuID = 0 ;
  321. }
  322. break ;
  323. case WM_CREATE:
  324. #if 0 // no longer needed for NT5
  325. if (ShowSysmonNotice (hWnd)) {
  326. #endif
  327. OnCreate (hWnd) ;
  328. ViewChart (hWnd) ;
  329. PrepareMenu (GetMenu (hWnd)) ;
  330. #if 0 // no longer needed for NT5
  331. } else {
  332. PerfmonClose (hWnd);
  333. }
  334. #endif
  335. break ;
  336. case WM_DESTROY:
  337. WinHelp (hWndMain, pszHelpFile, HELP_QUIT, 0) ;
  338. PostQuitMessage (0);
  339. break ;
  340. case WM_QUERYENDSESSION:
  341. // please shut it down
  342. return (1) ;
  343. break ;
  344. case WM_ENDSESSION:
  345. if (wParam == TRUE) {
  346. // close any log file before closing down
  347. PerfmonClose (hWnd) ;
  348. return (1) ;
  349. } else
  350. bCallDefWinProc = TRUE ;
  351. break ;
  352. case WM_CLOSE:
  353. PerfmonClose (hWnd) ;
  354. break ;
  355. case WM_ACTIVATE:
  356. {
  357. int fActivate = LOWORD (wParam) ;
  358. bPerfmonIconic = (BOOL) HIWORD (wParam) ;
  359. if (bPerfmonIconic == 0 && fActivate != WA_INACTIVE) {
  360. // set focus on the Legend window
  361. if (iPerfmonView == IDM_VIEWCHART) {
  362. SetFocus (hWndGraphLegend) ;
  363. } else if (iPerfmonView == IDM_VIEWALERT) {
  364. SetFocus (hWndAlertLegend) ;
  365. } else if (iPerfmonView == IDM_VIEWLOG) {
  366. SetFocus (hWndLogEntries) ;
  367. } else if (iPerfmonView == IDM_VIEWREPORT) {
  368. SetFocus (hWndReport) ;
  369. }
  370. }
  371. }
  372. break ;
  373. case WM_SYSCOLORCHANGE:
  374. DeletePerfmonSystemObjects () ;
  375. CreatePerfmonSystemObjects () ;
  376. WindowInvalidate (PerfmonViewWindow()) ;
  377. break ;
  378. case WM_WININICHANGE:
  379. if (!lParam || strsamei((LPTSTR)lParam, szInternational)) {
  380. GetDateTimeFormats () ;
  381. }
  382. break ;
  383. case WM_DROPFILES:
  384. OnDropFile (wParam) ;
  385. return (0) ;
  386. break ;
  387. default:
  388. bCallDefWinProc = TRUE ;
  389. break;
  390. }
  391. if (bCallDefWinProc) {
  392. lRetCode = DefWindowProc (hWnd, message, wParam, lParam) ;
  393. }
  394. return (lRetCode);
  395. }
  396. int
  397. WinMain (
  398. HINSTANCE hCurrentInstance,
  399. HINSTANCE hPrevInstance,
  400. LPSTR lpszCmdLine,
  401. int nCmdShow
  402. )
  403. {
  404. MSG msg;
  405. if (!PerfmonInitialize (hCurrentInstance, hPrevInstance,
  406. lpszCmdLine, nCmdShow))
  407. return (FALSE) ;
  408. DragAcceptFiles (hWndMain, TRUE) ;
  409. while (GetMessage (&msg, NULL, 0, 0)) {
  410. if (!TranslateAccelerator(hWndMain, hAccelerators, &msg)) {
  411. TranslateMessage (&msg) ;
  412. DispatchMessage (&msg) ;
  413. }
  414. }
  415. return((int)msg.wParam);
  416. }
  417. LRESULT
  418. MessageFilterProc (
  419. int nCode,
  420. WPARAM wParam,
  421. LPARAM lParam
  422. )
  423. {
  424. LPMSG lpMsg = (LPMSG)lParam ;
  425. extern HHOOK lpMsgFilterProc ;
  426. if (nCode < 0) {
  427. return FALSE ;
  428. }
  429. if (nCode == MSGF_DIALOGBOX || nCode == MSGF_MENU) {
  430. if (lpMsg->message == WM_KEYDOWN && lpMsg->wParam == VK_F1) {
  431. PostMessage (hWndMain, WM_F1DOWN, nCode, 0L) ;
  432. return TRUE ;
  433. }
  434. }
  435. return (DefHookProc (nCode, wParam, (LPARAM)lpMsg, &lpMsgFilterProc)) ;
  436. }
  437. void
  438. SizePerfmonComponents (void)
  439. {
  440. RECT rectClient ;
  441. int xWidth, yHeight ;
  442. int yToolbarHeight ;
  443. int yStatusHeight ;
  444. int yViewHeight ;
  445. GetClientRect (hWndMain, &rectClient) ;
  446. xWidth = rectClient.right - rectClient.left ;
  447. yHeight = rectClient.bottom - rectClient.top ;
  448. if (Options.bToolbar) {
  449. SendMessage (hWndToolbar, WM_SIZE, 0, 0L) ;
  450. }
  451. yToolbarHeight = Options.bToolbar ? (WindowHeight (hWndToolbar) - 1) : 0 ;
  452. yStatusHeight = Options.bStatusbar ? StatusHeight (hWndStatus) : 0 ;
  453. if (Options.bStatusbar) {
  454. if (yToolbarHeight + yStatusHeight > yHeight) {
  455. // too small to display both toolbar and status bar
  456. // just display part of the status bar
  457. yStatusHeight = yHeight - yToolbarHeight ;
  458. }
  459. MoveWindow (hWndStatus,
  460. 0, yHeight - yStatusHeight, xWidth, yStatusHeight, TRUE) ;
  461. //WindowInvalidate (hWndStatus) ;
  462. }
  463. //WindowInvalidate (hWndMain) ;
  464. WindowShow (hWndStatus, Options.bStatusbar) ;
  465. WindowShow (hWndToolbar, Options.bToolbar) ;
  466. yViewHeight = yHeight - yStatusHeight - yToolbarHeight ;
  467. MoveWindow (hWndGraph,
  468. 0, yToolbarHeight,
  469. xWidth, yViewHeight,
  470. TRUE) ;
  471. MoveWindow (hWndAlert,
  472. 0, yToolbarHeight,
  473. xWidth, yViewHeight,
  474. TRUE) ;
  475. MoveWindow (hWndLog,
  476. 0, yToolbarHeight,
  477. xWidth, yViewHeight,
  478. TRUE) ;
  479. MoveWindow (hWndReport,
  480. 0, yToolbarHeight,
  481. xWidth, yViewHeight,
  482. TRUE) ;
  483. }