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);
55 LRESULT CALLBACK ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
65 * Save the width and height
67 GetClientRect(hDlg, &rc);
68 nProcessPageWidth = rc.right;
69 nProcessPageHeight = rc.bottom;
71 /* Update window position */
72 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
75 * Get handles to the controls
77 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
78 hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
79 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
80 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
83 * Set the font, title, and extended window styles for the list control
85 SendMessage(hProcessPageListCtrl, WM_SETFONT, SendMessage(hProcessPage, WM_GETFONT, 0, 0), TRUE);
86 SetWindowText(hProcessPageListCtrl, _T("Processes"));
87 ListView_SetExtendedListViewStyle(hProcessPageListCtrl, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
92 * Subclass the process list control so we can intercept WM_ERASEBKGND
94 OldProcessListWndProc = SetWindowLong(hProcessPageListCtrl, GWL_WNDPROC, (LONG)ProcessListWndProc);
96 /* Start our refresh thread */
97 CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL);
102 /* Close the event handle, this will make the */
103 /* refresh thread exit when the wait fails */
104 CloseHandle(hProcessPageEvent);
106 SaveColumnSettings();
114 if (wParam == SIZE_MINIMIZED)
119 nXDifference = cx - nProcessPageWidth;
120 nYDifference = cy - nProcessPageHeight;
121 nProcessPageWidth = cx;
122 nProcessPageHeight = cy;
124 /* Reposition the application page's controls */
125 GetWindowRect(hProcessPageListCtrl, &rc);
126 cx = (rc.right - rc.left) + nXDifference;
127 cy = (rc.bottom - rc.top) + nYDifference;
128 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
129 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
131 GetClientRect(hProcessPageEndProcessButton, &rc);
132 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
133 cx = rc.left + nXDifference;
134 cy = rc.top + nYDifference;
135 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
136 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
138 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
139 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
141 cy = rc.top + nYDifference;
142 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
143 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
149 ProcessPageOnNotify(wParam, lParam);
156 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
166 IO_COUNTERS iocounters;
169 idctrl = (int) wParam;
170 pnmh = (LPNMHDR) lParam;
171 pnmv = (LPNMLISTVIEW) lParam;
172 pnmdi = (NMLVDISPINFO*) lParam;
173 pnmhdr = (LPNMHEADER) lParam;
175 if (pnmh->hwndFrom == hProcessPageListCtrl)
180 case LVN_ITEMCHANGED:
185 case LVN_GETDISPINFO:
187 if (!(pnmdi->item.mask & LVIF_TEXT))
190 ColumnIndex = pnmdi->item.iSubItem;
191 Index = pnmdi->item.iItem;
193 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
194 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
195 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
196 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetProcessId(Index));
197 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
198 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
199 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
200 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetSessionId(Index));
201 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
202 wsprintf(pnmdi->item.pszText, _T("%02d"), PerfDataGetCPUUsage(Index));
203 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
209 time = PerfDataGetCPUTime(Index);
211 dwHours = (DWORD)(time.QuadPart / 36000000000L);
212 dwMinutes = (DWORD)((time.QuadPart % 36000000000L) / 600000000L);
213 dwSeconds = (DWORD)(((time.QuadPart % 36000000000L) % 600000000L) / 10000000L);
215 dwHours = (DWORD)(time.QuadPart / 36000000000LL);
216 dwMinutes = (DWORD)((time.QuadPart % 36000000000LL) / 600000000LL);
217 dwSeconds = (DWORD)(((time.QuadPart % 36000000000LL) % 600000000LL) / 10000000LL);
219 wsprintf(pnmdi->item.pszText, _T("%d:%02d:%02d"), dwHours, dwMinutes, dwSeconds);
221 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
223 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeBytes(Index) / 1024);
224 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
225 _tcscat(pnmdi->item.pszText, _T(" K"));
227 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
229 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
230 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
231 _tcscat(pnmdi->item.pszText, _T(" K"));
233 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
235 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeDelta(Index) / 1024);
236 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
237 _tcscat(pnmdi->item.pszText, _T(" K"));
239 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
241 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCount(Index));
242 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
244 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
246 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCountDelta(Index));
247 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
249 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
251 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
252 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
253 _tcscat(pnmdi->item.pszText, _T(" K"));
255 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
257 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPagedPoolUsagePages(Index) / 1024);
258 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
259 _tcscat(pnmdi->item.pszText, _T(" K"));
261 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
263 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
264 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
265 _tcscat(pnmdi->item.pszText, _T(" K"));
267 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
268 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetBasePriority(Index));
269 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
271 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetHandleCount(Index));
272 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
274 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
276 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetThreadCount(Index));
277 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
279 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
281 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetUSERObjectCount(Index));
282 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
284 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
286 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetGDIObjectCount(Index));
287 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
289 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
291 PerfDataGetIOCounters(Index, &iocounters);
292 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadOperationCount); */
294 #define _ui64toa _ui64tow
297 _ui64toa(iocounters.ReadOperationCount, pnmdi->item.pszText, 10);
298 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
300 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
302 PerfDataGetIOCounters(Index, &iocounters);
303 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteOperationCount); */
304 _ui64toa(iocounters.WriteOperationCount, pnmdi->item.pszText, 10);
305 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
307 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
309 PerfDataGetIOCounters(Index, &iocounters);
310 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherOperationCount); */
311 _ui64toa(iocounters.OtherOperationCount, pnmdi->item.pszText, 10);
312 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
314 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
316 PerfDataGetIOCounters(Index, &iocounters);
317 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadTransferCount); */
318 _ui64toa(iocounters.ReadTransferCount, pnmdi->item.pszText, 10);
319 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
321 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
323 PerfDataGetIOCounters(Index, &iocounters);
324 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteTransferCount); */
325 _ui64toa(iocounters.WriteTransferCount, pnmdi->item.pszText, 10);
326 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
328 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
330 PerfDataGetIOCounters(Index, &iocounters);
331 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherTransferCount); */
332 _ui64toa(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
333 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
340 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
342 memset(&lvitem, 0, sizeof(LVITEM));
344 lvitem.mask = LVIF_STATE;
345 lvitem.stateMask = LVIS_SELECTED;
346 lvitem.iItem = Index;
348 ListView_GetItem(hProcessPageListCtrl, &lvitem);
350 if (lvitem.state & LVIS_SELECTED)
354 if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) &&
355 (PerfDataGetProcessId(Index) != 0))
357 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
364 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
371 * FIXME: Fix the column sorting
373 *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
374 *bSortAscending = !bSortAscending;
379 case HDN_ITEMCHANGED:
381 UpdateColumnDataHints();
387 UpdateColumnDataHints();
396 void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount)
401 for (i=0,j=0; i<(_tcslen(strNumber) % 3); i++, j++)
402 temp[j] = strNumber[i];
403 for (k=0; i<_tcslen(strNumber); i++,j++,k++) {
404 if ((k % 3 == 0) && (j > 0))
406 temp[j] = strNumber[i];
409 _tcsncpy(strNumber, temp, nMaxCount);
412 void ProcessPageShowContextMenu(DWORD dwProcessId)
420 DWORD dwProcessPriorityClass;
421 TCHAR strDebugger[260];
422 DWORD dwDebuggerSize;
426 memset(&si, 0, sizeof(SYSTEM_INFO));
431 hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_PROCESS_PAGE_CONTEXT));
432 hSubMenu = GetSubMenu(hMenu, 0);
433 hPriorityMenu = GetSubMenu(hSubMenu, 4);
435 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
436 dwProcessPriorityClass = GetPriorityClass(hProcess);
437 CloseHandle(hProcess);
439 if (si.dwNumberOfProcessors < 2)
440 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
442 if (!AreDebugChannelsSupported())
443 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
445 switch (dwProcessPriorityClass) {
446 case REALTIME_PRIORITY_CLASS:
447 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
449 case HIGH_PRIORITY_CLASS:
450 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
452 case ABOVE_NORMAL_PRIORITY_CLASS:
453 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
455 case NORMAL_PRIORITY_CLASS:
456 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
458 case BELOW_NORMAL_PRIORITY_CLASS:
459 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
461 case IDLE_PRIORITY_CLASS:
462 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
466 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
468 dwDebuggerSize = 260;
469 if (RegQueryValueEx(hKey, _T("Debugger"), NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
471 for (Idx=0; Idx<_tcslen(strDebugger); Idx++)
472 strDebugger[Idx] = toupper(strDebugger[Idx]);
474 if (_tcsstr(strDebugger, _T("DRWTSN32")))
475 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
478 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
482 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
484 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
488 void RefreshProcessPage(void)
490 /* Signal the event so that our refresh thread */
491 /* will wake up and refresh the process page */
492 SetEvent(hProcessPageEvent);
495 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
497 ULONG OldProcessorUsage = 0;
498 ULONG OldProcessCount = 0;
500 /* Create the event */
501 hProcessPageEvent = CreateEvent(NULL, TRUE, TRUE, _T("Process Page Event"));
503 /* If we couldn't create the event then exit the thread */
504 if (!hProcessPageEvent)
510 /* Wait on the event */
511 dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
513 /* If the wait failed then the event object must have been */
514 /* closed and the task manager is exiting so exit this thread */
515 if (dwWaitVal == WAIT_FAILED)
518 if (dwWaitVal == WAIT_OBJECT_0) {
521 /* Reset our event */
522 ResetEvent(hProcessPageEvent);
524 if ((ULONG)SendMessage(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
525 SendMessage(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
527 if (IsWindowVisible(hProcessPage))
528 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
530 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
531 OldProcessorUsage = PerfDataGetProcessorUsage();
532 wsprintf(text, _T("CPU Usage: %3d%%"), OldProcessorUsage);
533 SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
535 if (OldProcessCount != PerfDataGetProcessCount()) {
536 OldProcessCount = PerfDataGetProcessCount();
537 wsprintf(text, _T("Processes: %d"), OldProcessCount);
538 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);