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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
37 HWND hProcessPage; /* Process List Property Page */
39 HWND hProcessPageListCtrl; /* Process ListCtrl Window */
40 HWND hProcessPageHeaderCtrl; /* Process Header Control */
41 HWND hProcessPageEndProcessButton; /* Process End Process button */
42 HWND hProcessPageShowAllProcessesButton;/* Process Show All Processes checkbox */
44 static int nProcessPageWidth;
45 static int nProcessPageHeight;
47 static HANDLE hProcessPageEvent = NULL; /* When this event becomes signaled then we refresh the process list */
50 static void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount)
55 for (i=0,j=0; i<(_tcslen(strNumber) % 3); i++, j++)
56 temp[j] = strNumber[i];
57 for (k=0; i<_tcslen(strNumber); i++,j++,k++) {
58 if ((k % 3 == 0) && (j > 0))
60 temp[j] = strNumber[i];
63 _tcsncpy(strNumber, temp, nMaxCount);
66 static void ProcessPageShowContextMenu(DWORD dwProcessId)
74 DWORD dwProcessPriorityClass;
75 TCHAR strDebugger[260];
80 memset(&si, 0, sizeof(SYSTEM_INFO));
85 hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_PROCESS_PAGE_CONTEXT));
86 hSubMenu = GetSubMenu(hMenu, 0);
87 hPriorityMenu = GetSubMenu(hSubMenu, 4);
89 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
90 dwProcessPriorityClass = GetPriorityClass(hProcess);
91 CloseHandle(hProcess);
93 if (si.dwNumberOfProcessors < 2)
94 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
96 if (!AreDebugChannelsSupported())
97 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
99 switch (dwProcessPriorityClass) {
100 case REALTIME_PRIORITY_CLASS:
101 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
103 case HIGH_PRIORITY_CLASS:
104 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
106 case ABOVE_NORMAL_PRIORITY_CLASS:
107 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
109 case NORMAL_PRIORITY_CLASS:
110 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
112 case BELOW_NORMAL_PRIORITY_CLASS:
113 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
115 case IDLE_PRIORITY_CLASS:
116 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
120 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
122 dwDebuggerSize = 260;
123 if (RegQueryValueEx(hKey, _T("Debugger"), NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
125 for (Idx=0; Idx<_tcslen(strDebugger); Idx++)
126 strDebugger[Idx] = toupper(strDebugger[Idx]);
128 if (_tcsstr(strDebugger, _T("DRWTSN32")))
129 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
132 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
136 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
138 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
142 static void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
152 IO_COUNTERS iocounters;
155 idctrl = (int) wParam;
156 pnmh = (LPNMHDR) lParam;
157 pnmv = (LPNMLISTVIEW) lParam;
158 pnmdi = (NMLVDISPINFO*) lParam;
159 pnmhdr = (LPNMHEADER) lParam;
161 if (pnmh->hwndFrom == hProcessPageListCtrl)
166 case LVN_ITEMCHANGED:
171 case LVN_GETDISPINFO:
173 if (!(pnmdi->item.mask & LVIF_TEXT))
176 ColumnIndex = pnmdi->item.iSubItem;
177 Index = pnmdi->item.iItem;
179 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
180 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
181 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
182 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetProcessId(Index));
183 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
184 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
185 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
186 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetSessionId(Index));
187 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
188 wsprintf(pnmdi->item.pszText, _T("%02d"), PerfDataGetCPUUsage(Index));
189 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
196 time = PerfDataGetCPUTime(Index);
197 secs = time.QuadPart / 10000000;
198 dwHours = secs / 3600;
199 dwMinutes = (secs % 3600) / 60;
200 dwSeconds = (secs % 3600) % 60;
201 wsprintf(pnmdi->item.pszText, _T("%d:%02d:%02d"), dwHours, dwMinutes, dwSeconds);
203 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
205 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeBytes(Index) / 1024);
206 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
207 _tcscat(pnmdi->item.pszText, _T(" K"));
209 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
211 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
212 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
213 _tcscat(pnmdi->item.pszText, _T(" K"));
215 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
217 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeDelta(Index) / 1024);
218 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
219 _tcscat(pnmdi->item.pszText, _T(" K"));
221 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
223 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCount(Index));
224 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
226 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
228 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCountDelta(Index));
229 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
231 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
233 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
234 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
235 _tcscat(pnmdi->item.pszText, _T(" K"));
237 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
239 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPagedPoolUsagePages(Index) / 1024);
240 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
241 _tcscat(pnmdi->item.pszText, _T(" K"));
243 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
245 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
246 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
247 _tcscat(pnmdi->item.pszText, _T(" K"));
249 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
250 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetBasePriority(Index));
251 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
253 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetHandleCount(Index));
254 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
256 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
258 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetThreadCount(Index));
259 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
261 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
263 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetUSERObjectCount(Index));
264 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
266 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
268 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetGDIObjectCount(Index));
269 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
271 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
273 PerfDataGetIOCounters(Index, &iocounters);
274 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadOperationCount); */
276 #define _ui64toa _ui64tow
279 _ui64toa(iocounters.ReadOperationCount, pnmdi->item.pszText, 10);
280 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
282 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
284 PerfDataGetIOCounters(Index, &iocounters);
285 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteOperationCount); */
286 _ui64toa(iocounters.WriteOperationCount, pnmdi->item.pszText, 10);
287 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
289 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
291 PerfDataGetIOCounters(Index, &iocounters);
292 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherOperationCount); */
293 _ui64toa(iocounters.OtherOperationCount, pnmdi->item.pszText, 10);
294 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
296 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
298 PerfDataGetIOCounters(Index, &iocounters);
299 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadTransferCount); */
300 _ui64toa(iocounters.ReadTransferCount, pnmdi->item.pszText, 10);
301 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
303 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
305 PerfDataGetIOCounters(Index, &iocounters);
306 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteTransferCount); */
307 _ui64toa(iocounters.WriteTransferCount, pnmdi->item.pszText, 10);
308 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
310 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
312 PerfDataGetIOCounters(Index, &iocounters);
313 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherTransferCount); */
314 _ui64toa(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
315 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
322 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
324 lvitem.mask = LVIF_STATE;
325 lvitem.stateMask = LVIS_SELECTED;
326 lvitem.iItem = Index;
329 SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem);
331 if (lvitem.state & LVIS_SELECTED)
335 if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) &&
336 (PerfDataGetProcessId(Index) != 0))
338 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
345 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
352 * FIXME: Fix the column sorting
354 *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
355 *bSortAscending = !bSortAscending;
360 case HDN_ITEMCHANGED:
362 UpdateColumnDataHints();
368 UpdateColumnDataHints();
377 void RefreshProcessPage(void)
379 /* Signal the event so that our refresh thread */
380 /* will wake up and refresh the process page */
381 SetEvent(hProcessPageEvent);
384 static DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
386 ULONG OldProcessorUsage = 0;
387 ULONG OldProcessCount = 0;
389 WCHAR wszCPU_Usage[255];
390 WCHAR wszProcesses[255];
392 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
393 LoadStringW(hInst, IDS_STATUS_BAR_PROCESSES, wszProcesses, sizeof(wszProcesses)/sizeof(WCHAR));
395 /* Create the event */
396 hProcessPageEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
398 /* If we couldn't create the event then exit the thread */
399 if (!hProcessPageEvent)
405 /* Wait on the event */
406 dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
408 /* If the wait failed then the event object must have been */
409 /* closed and the task manager is exiting so exit this thread */
410 if (dwWaitVal == WAIT_FAILED)
413 if (dwWaitVal == WAIT_OBJECT_0) {
416 /* Reset our event */
417 ResetEvent(hProcessPageEvent);
419 if ((ULONG)SendMessage(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
420 SendMessage(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
422 if (IsWindowVisible(hProcessPage))
423 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
425 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
426 OldProcessorUsage = PerfDataGetProcessorUsage();
427 wsprintfW(text, wszCPU_Usage, OldProcessorUsage);
428 SendMessageW(hStatusWnd, SB_SETTEXTW, 1, (LPARAM)text);
430 if (OldProcessCount != PerfDataGetProcessCount()) {
431 OldProcessCount = PerfDataGetProcessCount();
432 wsprintfW(text, wszProcesses, OldProcessCount);
433 SendMessageW(hStatusWnd, SB_SETTEXTW, 0, (LPARAM)text);
441 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
451 * Save the width and height
453 GetClientRect(hDlg, &rc);
454 nProcessPageWidth = rc.right;
455 nProcessPageHeight = rc.bottom;
457 /* Update window position */
458 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
461 * Get handles to the controls
463 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
464 hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
465 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
466 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
469 * Set the extended window styles for the list control
471 SendMessage(hProcessPageListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
476 * Subclass the process list control so we can intercept WM_ERASEBKGND
478 OldProcessListWndProc = (WNDPROC)SetWindowLongPtr(hProcessPageListCtrl, GWLP_WNDPROC, (LONG_PTR)ProcessListWndProc);
480 /* Start our refresh thread */
481 CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL);
486 /* Close the event handle, this will make the */
487 /* refresh thread exit when the wait fails */
488 CloseHandle(hProcessPageEvent);
490 SaveColumnSettings();
495 /* Handle the button clicks */
496 switch (LOWORD(wParam))
499 ProcessPage_OnEndProcess();
504 if (wParam == SIZE_MINIMIZED)
509 nXDifference = cx - nProcessPageWidth;
510 nYDifference = cy - nProcessPageHeight;
511 nProcessPageWidth = cx;
512 nProcessPageHeight = cy;
514 /* Reposition the application page's controls */
515 GetWindowRect(hProcessPageListCtrl, &rc);
516 cx = (rc.right - rc.left) + nXDifference;
517 cy = (rc.bottom - rc.top) + nYDifference;
518 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
519 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
521 GetClientRect(hProcessPageEndProcessButton, &rc);
522 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
523 cx = rc.left + nXDifference;
524 cy = rc.top + nYDifference;
525 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
526 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
528 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
529 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
531 cy = rc.top + nYDifference;
532 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
533 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
539 ProcessPageOnNotify(wParam, lParam);