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
35 HWND hProcessPage; /* Process List Property Page */
37 HWND hProcessPageListCtrl; /* Process ListCtrl Window */
38 HWND hProcessPageHeaderCtrl; /* Process Header Control */
39 HWND hProcessPageEndProcessButton; /* Process End Process button */
40 HWND hProcessPageShowAllProcessesButton;/* Process Show All Processes checkbox */
42 static int nProcessPageWidth;
43 static int nProcessPageHeight;
45 static HANDLE hProcessPageEvent = NULL; /* When this event becomes signaled then we refresh the process list */
48 static void CommaSeparateNumberString(LPWSTR strNumber, int nMaxCount)
52 int len = lstrlenW(strNumber);
54 for (i=0; i < len % 3; i++)
55 temp[i] = strNumber[i];
56 for (k=0,j=i; i < len; i++,j++,k++) {
57 if ((k % 3 == 0) && (j > 0))
59 temp[j] = strNumber[i];
62 memcpy(strNumber, temp, min(nMaxCount, j) * sizeof(WCHAR));
65 static void ProcessPageShowContextMenu(DWORD dwProcessId)
73 DWORD dwProcessPriorityClass;
74 WCHAR strDebugger[260];
78 static const WCHAR wszAeDebugRegPath[] = {
79 'S','o','f','t','w','a','r','e','\\',
80 'M','i','c','r','o','s','o','f','t','\\',
81 'W','i','n','d','o','w','s',' ','N','T','\\',
82 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
83 'A','e','D','e','b','u','g',0};
85 memset(&si, 0, sizeof(SYSTEM_INFO));
90 hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_PROCESS_PAGE_CONTEXT));
91 hSubMenu = GetSubMenu(hMenu, 0);
92 hPriorityMenu = GetSubMenu(hSubMenu, 4);
94 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
95 dwProcessPriorityClass = GetPriorityClass(hProcess);
96 CloseHandle(hProcess);
98 if (si.dwNumberOfProcessors < 2)
99 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
101 if (!AreDebugChannelsSupported())
102 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
104 switch (dwProcessPriorityClass) {
105 case REALTIME_PRIORITY_CLASS:
106 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
108 case HIGH_PRIORITY_CLASS:
109 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
111 case ABOVE_NORMAL_PRIORITY_CLASS:
112 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
114 case NORMAL_PRIORITY_CLASS:
115 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
117 case BELOW_NORMAL_PRIORITY_CLASS:
118 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
120 case IDLE_PRIORITY_CLASS:
121 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
125 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszAeDebugRegPath, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
127 static const WCHAR wszDebugger[] = {'D','e','b','u','g','g','e','r',0};
128 dwDebuggerSize = 260;
129 if (RegQueryValueExW(hKey, wszDebugger, NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
131 static const WCHAR wszDRWTSN32[] = {'D','R','W','T','S','N','3','2',0};
132 for (Idx=0; Idx < lstrlenW(strDebugger); Idx++)
133 strDebugger[Idx] = toupper(strDebugger[Idx]);
135 if (wcsstr(strDebugger, wszDRWTSN32))
136 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
139 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
143 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
145 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
149 static void ProcessPageOnNotify(LPARAM lParam)
152 NMLVDISPINFOW* pnmdi;
156 IO_COUNTERS iocounters;
158 static const WCHAR wszFmtD[] = {'%','d',0};
159 static const WCHAR wszFmt02D[] = {'%','0','2','d',0};
160 static const WCHAR wszUnitK[] = {' ','K',0};
162 pnmh = (LPNMHDR) lParam;
163 pnmdi = (NMLVDISPINFOW*) lParam;
165 if (pnmh->hwndFrom == hProcessPageListCtrl)
170 case LVN_ITEMCHANGED:
175 case LVN_GETDISPINFOW:
177 if (!(pnmdi->item.mask & LVIF_TEXT))
180 ColumnIndex = pnmdi->item.iSubItem;
181 Index = pnmdi->item.iItem;
183 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
184 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
185 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
186 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetProcessId(Index));
187 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
188 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
189 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
190 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetSessionId(Index));
191 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
192 wsprintfW(pnmdi->item.pszText, wszFmt02D, PerfDataGetCPUUsage(Index));
193 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
199 static const WCHAR timefmt[] = {'%','d',':','%','0','2','d',':','%','0','2','d',0};
201 time = PerfDataGetCPUTime(Index);
202 secs = time.QuadPart / 10000000;
203 dwHours = secs / 3600;
204 dwMinutes = (secs % 3600) / 60;
205 dwSeconds = (secs % 3600) % 60;
206 wsprintfW(pnmdi->item.pszText, timefmt, dwHours, dwMinutes, dwSeconds);
208 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
210 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetWorkingSetSizeBytes(Index) / 1024);
211 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
212 wcscat(pnmdi->item.pszText, wszUnitK);
214 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
216 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
217 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
218 wcscat(pnmdi->item.pszText, wszUnitK);
220 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
222 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetWorkingSetSizeDelta(Index) / 1024);
223 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
224 wcscat(pnmdi->item.pszText, wszUnitK);
226 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
228 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPageFaultCount(Index));
229 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
231 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
233 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPageFaultCountDelta(Index));
234 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
236 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
238 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
239 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
240 wcscat(pnmdi->item.pszText, wszUnitK);
242 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
244 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPagedPoolUsagePages(Index) / 1024);
245 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
246 wcscat(pnmdi->item.pszText, wszUnitK);
248 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
250 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
251 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
252 wcscat(pnmdi->item.pszText, wszUnitK);
254 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
255 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetBasePriority(Index));
256 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
258 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetHandleCount(Index));
259 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
261 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
263 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetThreadCount(Index));
264 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
266 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
268 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetUSERObjectCount(Index));
269 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
271 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
273 wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetGDIObjectCount(Index));
274 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
276 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
278 PerfDataGetIOCounters(Index, &iocounters);
279 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.ReadOperationCount); */
280 _ui64tow(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 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.WriteOperationCount); */
287 _ui64tow(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 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.OtherOperationCount); */
294 _ui64tow(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 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.ReadTransferCount); */
301 _ui64tow(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 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.WriteTransferCount); */
308 _ui64tow(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 /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.OtherTransferCount); */
315 _ui64tow(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
316 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
322 Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
323 for (Index=0; Index<Count; Index++)
325 lvitem.mask = LVIF_STATE;
326 lvitem.stateMask = LVIS_SELECTED;
327 lvitem.iItem = Index;
330 SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
332 if (lvitem.state & LVIS_SELECTED)
336 if ((SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0) == 1) &&
337 (PerfDataGetProcessId(Index) != 0))
339 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
346 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
353 * FIXME: Fix the column sorting
355 *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
356 *bSortAscending = !bSortAscending;
361 case HDN_ITEMCHANGEDW:
363 UpdateColumnDataHints();
369 UpdateColumnDataHints();
378 void RefreshProcessPage(void)
380 /* Signal the event so that our refresh thread */
381 /* will wake up and refresh the process page */
382 SetEvent(hProcessPageEvent);
385 static DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
387 ULONG OldProcessorUsage = 0;
388 ULONG OldProcessCount = 0;
390 WCHAR wszCPU_Usage[255];
391 WCHAR wszProcesses[255];
393 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
394 LoadStringW(hInst, IDS_STATUS_BAR_PROCESSES, wszProcesses, sizeof(wszProcesses)/sizeof(WCHAR));
396 /* Create the event */
397 hProcessPageEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
399 /* If we couldn't create the event then exit the thread */
400 if (!hProcessPageEvent)
406 /* Wait on the event */
407 dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
409 /* If the wait failed then the event object must have been */
410 /* closed and the task manager is exiting so exit this thread */
411 if (dwWaitVal == WAIT_FAILED)
414 if (dwWaitVal == WAIT_OBJECT_0) {
417 /* Reset our event */
418 ResetEvent(hProcessPageEvent);
420 if (SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
421 SendMessageW(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
423 if (IsWindowVisible(hProcessPage))
424 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
426 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
427 OldProcessorUsage = PerfDataGetProcessorUsage();
428 wsprintfW(text, wszCPU_Usage, OldProcessorUsage);
429 SendMessageW(hStatusWnd, SB_SETTEXTW, 1, (LPARAM)text);
431 if (OldProcessCount != PerfDataGetProcessCount()) {
432 OldProcessCount = PerfDataGetProcessCount();
433 wsprintfW(text, wszProcesses, OldProcessCount);
434 SendMessageW(hStatusWnd, SB_SETTEXTW, 0, (LPARAM)text);
442 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
448 DWORD extended_styles;
453 * Save the width and height
455 GetClientRect(hDlg, &rc);
456 nProcessPageWidth = rc.right;
457 nProcessPageHeight = rc.bottom;
459 /* Update window position */
460 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
463 * Get handles to the controls
465 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
466 hProcessPageHeaderCtrl = (HWND)SendMessageW(hProcessPageListCtrl, LVM_GETHEADER, 0, 0);
467 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
468 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
471 * Set the extended window styles for the list control
473 extended_styles = SendMessageW(hProcessPageListCtrl, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
474 SendMessageW(hProcessPageListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, extended_styles | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
479 * Subclass the process list control so we can intercept WM_ERASEBKGND
481 OldProcessListWndProc = (WNDPROC)SetWindowLongPtrW(hProcessPageListCtrl, GWLP_WNDPROC, (LONG_PTR)ProcessListWndProc);
483 /* Start our refresh thread */
484 CloseHandle( CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL));
489 /* Close the event handle, this will make the */
490 /* refresh thread exit when the wait fails */
491 CloseHandle(hProcessPageEvent);
493 SaveColumnSettings();
498 /* Handle the button clicks */
499 switch (LOWORD(wParam))
502 ProcessPage_OnEndProcess();
507 if (wParam == SIZE_MINIMIZED)
512 nXDifference = cx - nProcessPageWidth;
513 nYDifference = cy - nProcessPageHeight;
514 nProcessPageWidth = cx;
515 nProcessPageHeight = cy;
517 /* Reposition the application page's controls */
518 GetWindowRect(hProcessPageListCtrl, &rc);
519 cx = (rc.right - rc.left) + nXDifference;
520 cy = (rc.bottom - rc.top) + nYDifference;
521 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
522 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
524 GetClientRect(hProcessPageEndProcessButton, &rc);
525 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
526 cx = rc.left + nXDifference;
527 cy = rc.top + nYDifference;
528 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
529 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
531 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
532 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
534 cy = rc.top + nYDifference;
535 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
536 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
541 ProcessPageOnNotify(lParam);