6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
38 HWND hProcessPage; /* Process List Property Page */
40 HWND hProcessPageListCtrl; /* Process ListCtrl Window */
41 HWND hProcessPageHeaderCtrl; /* Process Header Control */
42 HWND hProcessPageEndProcessButton; /* Process End Process button */
43 HWND hProcessPageShowAllProcessesButton;/* Process Show All Processes checkbox */
45 static int nProcessPageWidth;
46 static int nProcessPageHeight;
48 static HANDLE hProcessPageEvent = NULL; /* When this event becomes signaled then we refresh the process list */
50 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam);
51 void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount);
52 void ProcessPageShowContextMenu(DWORD dwProcessId);
53 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter);
56 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
66 * Save the width and height
68 GetClientRect(hDlg, &rc);
69 nProcessPageWidth = rc.right;
70 nProcessPageHeight = rc.bottom;
72 /* Update window position */
73 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
76 * Get handles to the controls
78 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
79 hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
80 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
81 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
84 * Set the font, title, and extended window styles for the list control
86 SendMessage(hProcessPageListCtrl, WM_SETFONT, SendMessage(hProcessPage, WM_GETFONT, 0, 0), TRUE);
87 SetWindowText(hProcessPageListCtrl, _T("Processes"));
88 ListView_SetExtendedListViewStyle(hProcessPageListCtrl, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
93 * Subclass the process list control so we can intercept WM_ERASEBKGND
95 OldProcessListWndProc = (WNDPROC)SetWindowLongPtr(hProcessPageListCtrl, GWLP_WNDPROC, (LONG_PTR)ProcessListWndProc);
97 /* Start our refresh thread */
98 CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL);
103 /* Close the event handle, this will make the */
104 /* refresh thread exit when the wait fails */
105 CloseHandle(hProcessPageEvent);
107 SaveColumnSettings();
112 /* Handle the button clicks */
113 switch (LOWORD(wParam))
116 ProcessPage_OnEndProcess();
121 if (wParam == SIZE_MINIMIZED)
126 nXDifference = cx - nProcessPageWidth;
127 nYDifference = cy - nProcessPageHeight;
128 nProcessPageWidth = cx;
129 nProcessPageHeight = cy;
131 /* Reposition the application page's controls */
132 GetWindowRect(hProcessPageListCtrl, &rc);
133 cx = (rc.right - rc.left) + nXDifference;
134 cy = (rc.bottom - rc.top) + nYDifference;
135 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
136 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
138 GetClientRect(hProcessPageEndProcessButton, &rc);
139 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
140 cx = rc.left + nXDifference;
141 cy = rc.top + nYDifference;
142 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
143 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
145 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
146 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
148 cy = rc.top + nYDifference;
149 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
150 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
156 ProcessPageOnNotify(wParam, lParam);
163 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
173 IO_COUNTERS iocounters;
176 idctrl = (int) wParam;
177 pnmh = (LPNMHDR) lParam;
178 pnmv = (LPNMLISTVIEW) lParam;
179 pnmdi = (NMLVDISPINFO*) lParam;
180 pnmhdr = (LPNMHEADER) lParam;
182 if (pnmh->hwndFrom == hProcessPageListCtrl)
187 case LVN_ITEMCHANGED:
192 case LVN_GETDISPINFO:
194 if (!(pnmdi->item.mask & LVIF_TEXT))
197 ColumnIndex = pnmdi->item.iSubItem;
198 Index = pnmdi->item.iItem;
200 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
201 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
202 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
203 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetProcessId(Index));
204 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
205 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
206 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
207 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetSessionId(Index));
208 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
209 wsprintf(pnmdi->item.pszText, _T("%02d"), PerfDataGetCPUUsage(Index));
210 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
217 time = PerfDataGetCPUTime(Index);
218 secs = time.QuadPart / 10000000;
219 dwHours = secs / 3600;
220 dwMinutes = (secs % 3600) / 60;
221 dwSeconds = (secs % 3600) % 60;
222 wsprintf(pnmdi->item.pszText, _T("%d:%02d:%02d"), dwHours, dwMinutes, dwSeconds);
224 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
226 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeBytes(Index) / 1024);
227 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
228 _tcscat(pnmdi->item.pszText, _T(" K"));
230 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
232 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
233 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
234 _tcscat(pnmdi->item.pszText, _T(" K"));
236 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
238 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeDelta(Index) / 1024);
239 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
240 _tcscat(pnmdi->item.pszText, _T(" K"));
242 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
244 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCount(Index));
245 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
247 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
249 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCountDelta(Index));
250 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
252 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
254 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
255 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
256 _tcscat(pnmdi->item.pszText, _T(" K"));
258 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
260 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPagedPoolUsagePages(Index) / 1024);
261 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
262 _tcscat(pnmdi->item.pszText, _T(" K"));
264 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
266 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
267 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
268 _tcscat(pnmdi->item.pszText, _T(" K"));
270 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
271 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetBasePriority(Index));
272 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
274 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetHandleCount(Index));
275 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
277 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
279 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetThreadCount(Index));
280 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
282 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
284 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetUSERObjectCount(Index));
285 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
287 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
289 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetGDIObjectCount(Index));
290 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
292 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
294 PerfDataGetIOCounters(Index, &iocounters);
295 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadOperationCount); */
297 #define _ui64toa _ui64tow
300 _ui64toa(iocounters.ReadOperationCount, pnmdi->item.pszText, 10);
301 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
303 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
305 PerfDataGetIOCounters(Index, &iocounters);
306 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteOperationCount); */
307 _ui64toa(iocounters.WriteOperationCount, pnmdi->item.pszText, 10);
308 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
310 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
312 PerfDataGetIOCounters(Index, &iocounters);
313 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherOperationCount); */
314 _ui64toa(iocounters.OtherOperationCount, pnmdi->item.pszText, 10);
315 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
317 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
319 PerfDataGetIOCounters(Index, &iocounters);
320 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadTransferCount); */
321 _ui64toa(iocounters.ReadTransferCount, pnmdi->item.pszText, 10);
322 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
324 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
326 PerfDataGetIOCounters(Index, &iocounters);
327 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteTransferCount); */
328 _ui64toa(iocounters.WriteTransferCount, pnmdi->item.pszText, 10);
329 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
331 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
333 PerfDataGetIOCounters(Index, &iocounters);
334 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherTransferCount); */
335 _ui64toa(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
336 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
343 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
345 memset(&lvitem, 0, sizeof(LVITEM));
347 lvitem.mask = LVIF_STATE;
348 lvitem.stateMask = LVIS_SELECTED;
349 lvitem.iItem = Index;
351 ListView_GetItem(hProcessPageListCtrl, &lvitem);
353 if (lvitem.state & LVIS_SELECTED)
357 if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) &&
358 (PerfDataGetProcessId(Index) != 0))
360 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
367 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
374 * FIXME: Fix the column sorting
376 *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
377 *bSortAscending = !bSortAscending;
382 case HDN_ITEMCHANGED:
384 UpdateColumnDataHints();
390 UpdateColumnDataHints();
399 void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount)
404 for (i=0,j=0; i<(_tcslen(strNumber) % 3); i++, j++)
405 temp[j] = strNumber[i];
406 for (k=0; i<_tcslen(strNumber); i++,j++,k++) {
407 if ((k % 3 == 0) && (j > 0))
409 temp[j] = strNumber[i];
412 _tcsncpy(strNumber, temp, nMaxCount);
415 void ProcessPageShowContextMenu(DWORD dwProcessId)
423 DWORD dwProcessPriorityClass;
424 TCHAR strDebugger[260];
425 DWORD dwDebuggerSize;
429 memset(&si, 0, sizeof(SYSTEM_INFO));
434 hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_PROCESS_PAGE_CONTEXT));
435 hSubMenu = GetSubMenu(hMenu, 0);
436 hPriorityMenu = GetSubMenu(hSubMenu, 4);
438 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
439 dwProcessPriorityClass = GetPriorityClass(hProcess);
440 CloseHandle(hProcess);
442 if (si.dwNumberOfProcessors < 2)
443 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
445 if (!AreDebugChannelsSupported())
446 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
448 switch (dwProcessPriorityClass) {
449 case REALTIME_PRIORITY_CLASS:
450 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
452 case HIGH_PRIORITY_CLASS:
453 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
455 case ABOVE_NORMAL_PRIORITY_CLASS:
456 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
458 case NORMAL_PRIORITY_CLASS:
459 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
461 case BELOW_NORMAL_PRIORITY_CLASS:
462 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
464 case IDLE_PRIORITY_CLASS:
465 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
469 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
471 dwDebuggerSize = 260;
472 if (RegQueryValueEx(hKey, _T("Debugger"), NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
474 for (Idx=0; Idx<_tcslen(strDebugger); Idx++)
475 strDebugger[Idx] = toupper(strDebugger[Idx]);
477 if (_tcsstr(strDebugger, _T("DRWTSN32")))
478 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
481 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
485 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
487 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
491 void RefreshProcessPage(void)
493 /* Signal the event so that our refresh thread */
494 /* will wake up and refresh the process page */
495 SetEvent(hProcessPageEvent);
498 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
500 ULONG OldProcessorUsage = 0;
501 ULONG OldProcessCount = 0;
503 /* Create the event */
504 hProcessPageEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
506 /* If we couldn't create the event then exit the thread */
507 if (!hProcessPageEvent)
513 /* Wait on the event */
514 dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
516 /* If the wait failed then the event object must have been */
517 /* closed and the task manager is exiting so exit this thread */
518 if (dwWaitVal == WAIT_FAILED)
521 if (dwWaitVal == WAIT_OBJECT_0) {
524 /* Reset our event */
525 ResetEvent(hProcessPageEvent);
527 if ((ULONG)SendMessage(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
528 SendMessage(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
530 if (IsWindowVisible(hProcessPage))
531 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
533 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
534 OldProcessorUsage = PerfDataGetProcessorUsage();
535 wsprintf(text, _T("CPU Usage: %3d%%"), OldProcessorUsage);
536 SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
538 if (OldProcessCount != PerfDataGetProcessCount()) {
539 OldProcessCount = PerfDataGetProcessCount();
540 wsprintf(text, _T("Processes: %d"), OldProcessCount);
541 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);