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 */
51 static void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount)
56 for (i=0,j=0; i<(_tcslen(strNumber) % 3); i++, j++)
57 temp[j] = strNumber[i];
58 for (k=0; i<_tcslen(strNumber); i++,j++,k++) {
59 if ((k % 3 == 0) && (j > 0))
61 temp[j] = strNumber[i];
64 _tcsncpy(strNumber, temp, nMaxCount);
67 static void ProcessPageShowContextMenu(DWORD dwProcessId)
75 DWORD dwProcessPriorityClass;
76 TCHAR strDebugger[260];
81 memset(&si, 0, sizeof(SYSTEM_INFO));
86 hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_PROCESS_PAGE_CONTEXT));
87 hSubMenu = GetSubMenu(hMenu, 0);
88 hPriorityMenu = GetSubMenu(hSubMenu, 4);
90 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
91 dwProcessPriorityClass = GetPriorityClass(hProcess);
92 CloseHandle(hProcess);
94 if (si.dwNumberOfProcessors < 2)
95 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
97 if (!AreDebugChannelsSupported())
98 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
100 switch (dwProcessPriorityClass) {
101 case REALTIME_PRIORITY_CLASS:
102 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
104 case HIGH_PRIORITY_CLASS:
105 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
107 case ABOVE_NORMAL_PRIORITY_CLASS:
108 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
110 case NORMAL_PRIORITY_CLASS:
111 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
113 case BELOW_NORMAL_PRIORITY_CLASS:
114 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
116 case IDLE_PRIORITY_CLASS:
117 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
121 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
123 dwDebuggerSize = 260;
124 if (RegQueryValueEx(hKey, _T("Debugger"), NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
126 for (Idx=0; Idx<_tcslen(strDebugger); Idx++)
127 strDebugger[Idx] = toupper(strDebugger[Idx]);
129 if (_tcsstr(strDebugger, _T("DRWTSN32")))
130 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
133 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
137 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
139 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
143 static void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
153 IO_COUNTERS iocounters;
156 idctrl = (int) wParam;
157 pnmh = (LPNMHDR) lParam;
158 pnmv = (LPNMLISTVIEW) lParam;
159 pnmdi = (NMLVDISPINFO*) lParam;
160 pnmhdr = (LPNMHEADER) lParam;
162 if (pnmh->hwndFrom == hProcessPageListCtrl)
167 case LVN_ITEMCHANGED:
172 case LVN_GETDISPINFO:
174 if (!(pnmdi->item.mask & LVIF_TEXT))
177 ColumnIndex = pnmdi->item.iSubItem;
178 Index = pnmdi->item.iItem;
180 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
181 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
182 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
183 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetProcessId(Index));
184 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
185 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
186 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
187 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetSessionId(Index));
188 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
189 wsprintf(pnmdi->item.pszText, _T("%02d"), PerfDataGetCPUUsage(Index));
190 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
197 time = PerfDataGetCPUTime(Index);
198 secs = time.QuadPart / 10000000;
199 dwHours = secs / 3600;
200 dwMinutes = (secs % 3600) / 60;
201 dwSeconds = (secs % 3600) % 60;
202 wsprintf(pnmdi->item.pszText, _T("%d:%02d:%02d"), dwHours, dwMinutes, dwSeconds);
204 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
206 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeBytes(Index) / 1024);
207 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
208 _tcscat(pnmdi->item.pszText, _T(" K"));
210 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
212 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
213 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
214 _tcscat(pnmdi->item.pszText, _T(" K"));
216 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
218 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeDelta(Index) / 1024);
219 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
220 _tcscat(pnmdi->item.pszText, _T(" K"));
222 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
224 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCount(Index));
225 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
227 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
229 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCountDelta(Index));
230 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
232 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
234 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
235 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
236 _tcscat(pnmdi->item.pszText, _T(" K"));
238 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
240 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPagedPoolUsagePages(Index) / 1024);
241 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
242 _tcscat(pnmdi->item.pszText, _T(" K"));
244 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
246 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
247 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
248 _tcscat(pnmdi->item.pszText, _T(" K"));
250 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
251 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetBasePriority(Index));
252 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
254 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetHandleCount(Index));
255 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
257 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
259 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetThreadCount(Index));
260 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
262 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
264 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetUSERObjectCount(Index));
265 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
267 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
269 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetGDIObjectCount(Index));
270 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
272 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
274 PerfDataGetIOCounters(Index, &iocounters);
275 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadOperationCount); */
277 #define _ui64toa _ui64tow
280 _ui64toa(iocounters.ReadOperationCount, pnmdi->item.pszText, 10);
281 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
283 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
285 PerfDataGetIOCounters(Index, &iocounters);
286 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteOperationCount); */
287 _ui64toa(iocounters.WriteOperationCount, pnmdi->item.pszText, 10);
288 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
290 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
292 PerfDataGetIOCounters(Index, &iocounters);
293 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherOperationCount); */
294 _ui64toa(iocounters.OtherOperationCount, pnmdi->item.pszText, 10);
295 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
297 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
299 PerfDataGetIOCounters(Index, &iocounters);
300 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadTransferCount); */
301 _ui64toa(iocounters.ReadTransferCount, pnmdi->item.pszText, 10);
302 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
304 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
306 PerfDataGetIOCounters(Index, &iocounters);
307 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteTransferCount); */
308 _ui64toa(iocounters.WriteTransferCount, pnmdi->item.pszText, 10);
309 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
311 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
313 PerfDataGetIOCounters(Index, &iocounters);
314 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherTransferCount); */
315 _ui64toa(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
316 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
323 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
325 memset(&lvitem, 0, sizeof(LVITEM));
327 lvitem.mask = LVIF_STATE;
328 lvitem.stateMask = LVIS_SELECTED;
329 lvitem.iItem = Index;
331 SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem);
333 if (lvitem.state & LVIS_SELECTED)
337 if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) &&
338 (PerfDataGetProcessId(Index) != 0))
340 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
347 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
354 * FIXME: Fix the column sorting
356 *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
357 *bSortAscending = !bSortAscending;
362 case HDN_ITEMCHANGED:
364 UpdateColumnDataHints();
370 UpdateColumnDataHints();
379 void RefreshProcessPage(void)
381 /* Signal the event so that our refresh thread */
382 /* will wake up and refresh the process page */
383 SetEvent(hProcessPageEvent);
386 static DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
388 ULONG OldProcessorUsage = 0;
389 ULONG OldProcessCount = 0;
391 /* Create the event */
392 hProcessPageEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
394 /* If we couldn't create the event then exit the thread */
395 if (!hProcessPageEvent)
401 /* Wait on the event */
402 dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
404 /* If the wait failed then the event object must have been */
405 /* closed and the task manager is exiting so exit this thread */
406 if (dwWaitVal == WAIT_FAILED)
409 if (dwWaitVal == WAIT_OBJECT_0) {
412 /* Reset our event */
413 ResetEvent(hProcessPageEvent);
415 if ((ULONG)SendMessage(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
416 SendMessage(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
418 if (IsWindowVisible(hProcessPage))
419 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
421 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
422 OldProcessorUsage = PerfDataGetProcessorUsage();
423 wsprintf(text, _T("CPU Usage: %3d%%"), OldProcessorUsage);
424 SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
426 if (OldProcessCount != PerfDataGetProcessCount()) {
427 OldProcessCount = PerfDataGetProcessCount();
428 wsprintf(text, _T("Processes: %d"), OldProcessCount);
429 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
437 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
447 * Save the width and height
449 GetClientRect(hDlg, &rc);
450 nProcessPageWidth = rc.right;
451 nProcessPageHeight = rc.bottom;
453 /* Update window position */
454 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
457 * Get handles to the controls
459 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
460 hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
461 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
462 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
465 * Set the font, title, and extended window styles for the list control
467 SendMessage(hProcessPageListCtrl, WM_SETFONT, SendMessage(hProcessPage, WM_GETFONT, 0, 0), TRUE);
468 SetWindowText(hProcessPageListCtrl, _T("Processes"));
469 SendMessage(hProcessPageListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
474 * Subclass the process list control so we can intercept WM_ERASEBKGND
476 OldProcessListWndProc = (WNDPROC)SetWindowLongPtr(hProcessPageListCtrl, GWLP_WNDPROC, (LONG_PTR)ProcessListWndProc);
478 /* Start our refresh thread */
479 CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL);
484 /* Close the event handle, this will make the */
485 /* refresh thread exit when the wait fails */
486 CloseHandle(hProcessPageEvent);
488 SaveColumnSettings();
493 /* Handle the button clicks */
494 switch (LOWORD(wParam))
497 ProcessPage_OnEndProcess();
502 if (wParam == SIZE_MINIMIZED)
507 nXDifference = cx - nProcessPageWidth;
508 nYDifference = cy - nProcessPageHeight;
509 nProcessPageWidth = cx;
510 nProcessPageHeight = cy;
512 /* Reposition the application page's controls */
513 GetWindowRect(hProcessPageListCtrl, &rc);
514 cx = (rc.right - rc.left) + nXDifference;
515 cy = (rc.bottom - rc.top) + nYDifference;
516 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
517 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
519 GetClientRect(hProcessPageEndProcessButton, &rc);
520 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
521 cx = rc.left + nXDifference;
522 cy = rc.top + nYDifference;
523 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
524 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
526 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
527 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
529 cy = rc.top + nYDifference;
530 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
531 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
537 ProcessPageOnNotify(wParam, lParam);