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(LPWSTR strNumber, int nMaxCount)
54 int len = lstrlenW(strNumber);
56 for (i=0; i < len % 3; i++)
57 temp[i] = strNumber[i];
58 for (k=0,j=i; i < len; i++,j++,k++) {
59 if ((k % 3 == 0) && (j > 0))
61 temp[j] = strNumber[i];
64 memcpy(strNumber, temp, min(nMaxCount, j) * sizeof(WCHAR));
67 static void ProcessPageShowContextMenu(DWORD dwProcessId)
75 DWORD dwProcessPriorityClass;
76 WCHAR strDebugger[260];
80 static const WCHAR wszAeDebugRegPath[] = {
81 'S','o','f','t','w','a','r','e','\\',
82 'M','i','c','r','o','s','o','f','t','\\',
83 'W','i','n','d','o','w','s',' ','N','T','\\',
84 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
85 'A','e','D','e','b','u','g',0};
87 memset(&si, 0, sizeof(SYSTEM_INFO));
92 hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_PROCESS_PAGE_CONTEXT));
93 hSubMenu = GetSubMenu(hMenu, 0);
94 hPriorityMenu = GetSubMenu(hSubMenu, 4);
96 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
97 dwProcessPriorityClass = GetPriorityClass(hProcess);
98 CloseHandle(hProcess);
100 if (si.dwNumberOfProcessors < 2)
101 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
103 if (!AreDebugChannelsSupported())
104 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
106 switch (dwProcessPriorityClass) {
107 case REALTIME_PRIORITY_CLASS:
108 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
110 case HIGH_PRIORITY_CLASS:
111 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
113 case ABOVE_NORMAL_PRIORITY_CLASS:
114 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
116 case NORMAL_PRIORITY_CLASS:
117 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
119 case BELOW_NORMAL_PRIORITY_CLASS:
120 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
122 case IDLE_PRIORITY_CLASS:
123 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
127 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszAeDebugRegPath, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
129 static const WCHAR wszDebugger[] = {'D','e','b','u','g','g','e','r',0};
130 dwDebuggerSize = 260;
131 if (RegQueryValueExW(hKey, wszDebugger, NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
133 static const WCHAR wszDRWTSN32[] = {'D','R','W','T','S','N','3','2',0};
134 for (Idx=0; Idx < lstrlenW(strDebugger); Idx++)
135 strDebugger[Idx] = toupper(strDebugger[Idx]);
137 if (wcsstr(strDebugger, wszDRWTSN32))
138 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
141 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
145 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
147 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
151 static void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
156 NMLVDISPINFOW* pnmdi;
161 IO_COUNTERS iocounters;
163 static const WCHAR wszFmtD[] = {'%','d',0};
164 static const WCHAR wszFmt02D[] = {'%','0','2','d',0};
165 static const WCHAR wszUnitK[] = {' ','K',0};
167 idctrl = (int) wParam;
168 pnmh = (LPNMHDR) lParam;
169 pnmv = (LPNMLISTVIEW) lParam;
170 pnmdi = (NMLVDISPINFOW*) lParam;
171 pnmhdr = (LPNMHEADERW) lParam;
173 if (pnmh->hwndFrom == hProcessPageListCtrl)
178 case LVN_ITEMCHANGED:
183 case LVN_GETDISPINFOW:
185 if (!(pnmdi->item.mask & LVIF_TEXT))
188 ColumnIndex = pnmdi->item.iSubItem;
189 Index = pnmdi->item.iItem;
191 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
192 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
193 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
194 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetProcessId(Index));
195 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
196 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
197 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
198 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetSessionId(Index));
199 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
200 wsprintfW(pnmdi->item.pszText, wszFmt02D, PerfDataGetCPUUsage(Index));
201 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
207 static const WCHAR timefmt[] = {'%','d',':','%','0','2','d',':','%','0','2','d',0};
209 time = PerfDataGetCPUTime(Index);
210 secs = time.QuadPart / 10000000;
211 dwHours = secs / 3600;
212 dwMinutes = (secs % 3600) / 60;
213 dwSeconds = (secs % 3600) % 60;
214 wsprintfW(pnmdi->item.pszText, timefmt, dwHours, dwMinutes, dwSeconds);
216 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
218 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetWorkingSetSizeBytes(Index) / 1024);
219 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
220 wcscat(pnmdi->item.pszText, wszUnitK);
222 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
224 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
225 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
226 wcscat(pnmdi->item.pszText, wszUnitK);
228 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
230 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetWorkingSetSizeDelta(Index) / 1024);
231 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
232 wcscat(pnmdi->item.pszText, wszUnitK);
234 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
236 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPageFaultCount(Index));
237 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
239 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
241 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPageFaultCountDelta(Index));
242 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
244 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
246 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
247 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
248 wcscat(pnmdi->item.pszText, wszUnitK);
250 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
252 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPagedPoolUsagePages(Index) / 1024);
253 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
254 wcscat(pnmdi->item.pszText, wszUnitK);
256 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
258 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
259 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
260 wcscat(pnmdi->item.pszText, wszUnitK);
262 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
263 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetBasePriority(Index));
264 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
266 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetHandleCount(Index));
267 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
269 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
271 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetThreadCount(Index));
272 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
274 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
276 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetUSERObjectCount(Index));
277 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
279 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
281 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetGDIObjectCount(Index));
282 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
284 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
286 PerfDataGetIOCounters(Index, &iocounters);
287 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.ReadOperationCount); */
288 _ui64tow(iocounters.ReadOperationCount, pnmdi->item.pszText, 10);
289 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
291 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
293 PerfDataGetIOCounters(Index, &iocounters);
294 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.WriteOperationCount); */
295 _ui64tow(iocounters.WriteOperationCount, pnmdi->item.pszText, 10);
296 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
298 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
300 PerfDataGetIOCounters(Index, &iocounters);
301 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.OtherOperationCount); */
302 _ui64tow(iocounters.OtherOperationCount, pnmdi->item.pszText, 10);
303 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
305 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
307 PerfDataGetIOCounters(Index, &iocounters);
308 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.ReadTransferCount); */
309 _ui64tow(iocounters.ReadTransferCount, pnmdi->item.pszText, 10);
310 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
312 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
314 PerfDataGetIOCounters(Index, &iocounters);
315 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.WriteTransferCount); */
316 _ui64tow(iocounters.WriteTransferCount, pnmdi->item.pszText, 10);
317 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
319 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
321 PerfDataGetIOCounters(Index, &iocounters);
322 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.OtherTransferCount); */
323 _ui64tow(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
324 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
331 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
333 lvitem.mask = LVIF_STATE;
334 lvitem.stateMask = LVIS_SELECTED;
335 lvitem.iItem = Index;
338 SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem);
340 if (lvitem.state & LVIS_SELECTED)
344 if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) &&
345 (PerfDataGetProcessId(Index) != 0))
347 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
354 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
361 * FIXME: Fix the column sorting
363 *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
364 *bSortAscending = !bSortAscending;
369 case HDN_ITEMCHANGEDW:
371 UpdateColumnDataHints();
377 UpdateColumnDataHints();
386 void RefreshProcessPage(void)
388 /* Signal the event so that our refresh thread */
389 /* will wake up and refresh the process page */
390 SetEvent(hProcessPageEvent);
393 static DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
395 ULONG OldProcessorUsage = 0;
396 ULONG OldProcessCount = 0;
398 WCHAR wszCPU_Usage[255];
399 WCHAR wszProcesses[255];
401 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
402 LoadStringW(hInst, IDS_STATUS_BAR_PROCESSES, wszProcesses, sizeof(wszProcesses)/sizeof(WCHAR));
404 /* Create the event */
405 hProcessPageEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
407 /* If we couldn't create the event then exit the thread */
408 if (!hProcessPageEvent)
414 /* Wait on the event */
415 dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
417 /* If the wait failed then the event object must have been */
418 /* closed and the task manager is exiting so exit this thread */
419 if (dwWaitVal == WAIT_FAILED)
422 if (dwWaitVal == WAIT_OBJECT_0) {
425 /* Reset our event */
426 ResetEvent(hProcessPageEvent);
428 if ((ULONG)SendMessage(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
429 SendMessage(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
431 if (IsWindowVisible(hProcessPage))
432 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
434 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
435 OldProcessorUsage = PerfDataGetProcessorUsage();
436 wsprintfW(text, wszCPU_Usage, OldProcessorUsage);
437 SendMessageW(hStatusWnd, SB_SETTEXTW, 1, (LPARAM)text);
439 if (OldProcessCount != PerfDataGetProcessCount()) {
440 OldProcessCount = PerfDataGetProcessCount();
441 wsprintfW(text, wszProcesses, OldProcessCount);
442 SendMessageW(hStatusWnd, SB_SETTEXTW, 0, (LPARAM)text);
450 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
460 * Save the width and height
462 GetClientRect(hDlg, &rc);
463 nProcessPageWidth = rc.right;
464 nProcessPageHeight = rc.bottom;
466 /* Update window position */
467 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
470 * Get handles to the controls
472 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
473 hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
474 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
475 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
478 * Set the extended window styles for the list control
480 SendMessage(hProcessPageListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
485 * Subclass the process list control so we can intercept WM_ERASEBKGND
487 OldProcessListWndProc = (WNDPROC)SetWindowLongPtr(hProcessPageListCtrl, GWLP_WNDPROC, (LONG_PTR)ProcessListWndProc);
489 /* Start our refresh thread */
490 CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL);
495 /* Close the event handle, this will make the */
496 /* refresh thread exit when the wait fails */
497 CloseHandle(hProcessPageEvent);
499 SaveColumnSettings();
504 /* Handle the button clicks */
505 switch (LOWORD(wParam))
508 ProcessPage_OnEndProcess();
513 if (wParam == SIZE_MINIMIZED)
518 nXDifference = cx - nProcessPageWidth;
519 nYDifference = cy - nProcessPageHeight;
520 nProcessPageWidth = cx;
521 nProcessPageHeight = cy;
523 /* Reposition the application page's controls */
524 GetWindowRect(hProcessPageListCtrl, &rc);
525 cx = (rc.right - rc.left) + nXDifference;
526 cy = (rc.bottom - rc.top) + nYDifference;
527 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
528 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
530 GetClientRect(hProcessPageEndProcessButton, &rc);
531 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
532 cx = rc.left + nXDifference;
533 cy = rc.top + nYDifference;
534 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
535 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
537 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
538 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
540 cy = rc.top + nYDifference;
541 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
542 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
548 ProcessPageOnNotify(wParam, lParam);