Added a few more Unicode digits from Unicode version 4.1.
[wine] / programs / taskmgr / procpage.c
1 /*
2  *  ReactOS Task Manager
3  *
4  *  procpage.c
5  *
6  *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
7  *
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.
12  *
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.
17  *
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
21  */
22     
23 #define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28 #include <memory.h>
29 #include <tchar.h>
30 #include <stdio.h>
31 #include <winnt.h>
32     
33 #include "taskmgr.h"
34 #include "perfdata.h"
35 #include "column.h"
36 #include <ctype.h>
37
38 HWND hProcessPage;                        /* Process List Property Page */
39
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 */
44
45 static int    nProcessPageWidth;
46 static int    nProcessPageHeight;
47
48 static HANDLE    hProcessPageEvent = NULL;    /* When this event becomes signaled then we refresh the process list */
49
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);
54
55 INT_PTR CALLBACK
56 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
57 {
58     RECT    rc;
59     int        nXDifference;
60     int        nYDifference;
61     int        cx, cy;
62
63     switch (message) {
64     case WM_INITDIALOG:
65         /*
66          * Save the width and height
67          */
68         GetClientRect(hDlg, &rc);
69         nProcessPageWidth = rc.right;
70         nProcessPageHeight = rc.bottom;
71
72         /* Update window position */
73         SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
74
75         /*
76          * Get handles to the controls
77          */
78         hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
79         hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
80         hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
81         hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
82
83         /*
84          * Set the font, title, and extended window styles for the list control
85          */
86         SendMessage(hProcessPageListCtrl, WM_SETFONT, SendMessage(hProcessPage, WM_GETFONT, 0, 0), TRUE);
87         SetWindowText(hProcessPageListCtrl, _T("Processes"));
88         ListView_SetExtendedListViewStyle(hProcessPageListCtrl, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
89
90         AddColumns();
91
92         /*
93          * Subclass the process list control so we can intercept WM_ERASEBKGND
94          */
95         OldProcessListWndProc = (WNDPROC)SetWindowLongPtr(hProcessPageListCtrl, GWLP_WNDPROC, (LONG_PTR)ProcessListWndProc);
96
97         /* Start our refresh thread */
98          CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL);
99
100         return TRUE;
101
102     case WM_DESTROY:
103         /* Close the event handle, this will make the */
104         /* refresh thread exit when the wait fails */
105         CloseHandle(hProcessPageEvent);
106
107         SaveColumnSettings();
108
109         break;
110
111     case WM_COMMAND:
112         /* Handle the button clicks */
113         switch (LOWORD(wParam))
114         {
115                 case IDC_ENDPROCESS:
116                         ProcessPage_OnEndProcess();
117         }
118         break;
119
120     case WM_SIZE:
121         if (wParam == SIZE_MINIMIZED)
122             return 0;
123
124         cx = LOWORD(lParam);
125         cy = HIWORD(lParam);
126         nXDifference = cx - nProcessPageWidth;
127         nYDifference = cy - nProcessPageHeight;
128         nProcessPageWidth = cx;
129         nProcessPageHeight = cy;
130
131         /* Reposition the application page's controls */
132         GetWindowRect(hProcessPageListCtrl, &rc);
133         cx = (rc.right - rc.left) + nXDifference;
134         cy = (rc.bottom - rc.top) + nYDifference;
135         SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
136         InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
137         
138         GetClientRect(hProcessPageEndProcessButton, &rc);
139         MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
140            cx = rc.left + nXDifference;
141         cy = rc.top + nYDifference;
142         SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
143         InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
144         
145         GetClientRect(hProcessPageShowAllProcessesButton, &rc);
146         MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
147            cx = rc.left;
148         cy = rc.top + nYDifference;
149         SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
150         InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
151
152         break;
153
154     case WM_NOTIFY:
155
156         ProcessPageOnNotify(wParam, lParam);
157         break;
158     }
159
160     return 0;
161 }
162
163 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
164 {
165     int                idctrl;
166     LPNMHDR            pnmh;
167     LPNMLISTVIEW    pnmv;
168     NMLVDISPINFO*    pnmdi;
169     LPNMHEADER        pnmhdr;
170     LVITEM            lvitem;
171     ULONG            Index;
172     ULONG            ColumnIndex;
173     IO_COUNTERS        iocounters;
174     TIME            time;
175
176     idctrl = (int) wParam;
177     pnmh = (LPNMHDR) lParam;
178     pnmv = (LPNMLISTVIEW) lParam;
179     pnmdi = (NMLVDISPINFO*) lParam;
180     pnmhdr = (LPNMHEADER) lParam;
181
182     if (pnmh->hwndFrom == hProcessPageListCtrl)
183     {
184         switch (pnmh->code)
185         {
186 #if 0
187         case LVN_ITEMCHANGED:
188             ProcessPageUpdate();
189             break;
190 #endif
191             
192         case LVN_GETDISPINFO:
193
194             if (!(pnmdi->item.mask & LVIF_TEXT))
195                 break;
196             
197             ColumnIndex = pnmdi->item.iSubItem;
198             Index = pnmdi->item.iItem;
199
200             if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
201                 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
202             if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
203                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetProcessId(Index));
204             if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
205                 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
206             if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
207                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetSessionId(Index));
208             if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
209                 wsprintf(pnmdi->item.pszText, _T("%02d"), PerfDataGetCPUUsage(Index));
210             if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
211             {
212                 DWORD dwHours;
213                 DWORD dwMinutes;
214                 DWORD dwSeconds;
215                 ULONGLONG secs;
216
217                 time = PerfDataGetCPUTime(Index);
218                 secs = time.QuadPart / 10000000;
219                 dwHours = secs / 3600;
220                 dwMinutes = (secs % 3600) / 60;
221                 dwSeconds = (secs % 3600) % 60;
222                 wsprintf(pnmdi->item.pszText, _T("%d:%02d:%02d"), dwHours, dwMinutes, dwSeconds);
223             }
224             if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
225             {
226                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeBytes(Index) / 1024);
227                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
228                 _tcscat(pnmdi->item.pszText, _T(" K"));
229             }
230             if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
231             {
232                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
233                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
234                 _tcscat(pnmdi->item.pszText, _T(" K"));
235             }
236             if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
237             {
238                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeDelta(Index) / 1024);
239                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
240                 _tcscat(pnmdi->item.pszText, _T(" K"));
241             }
242             if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
243             {
244                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCount(Index));
245                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
246             }
247             if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
248             {
249                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCountDelta(Index));
250                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
251             }
252             if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
253             {
254                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
255                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
256                 _tcscat(pnmdi->item.pszText, _T(" K"));
257             }
258             if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
259             {
260                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPagedPoolUsagePages(Index) / 1024);
261                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
262                 _tcscat(pnmdi->item.pszText, _T(" K"));
263             }
264             if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
265             {
266                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
267                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
268                 _tcscat(pnmdi->item.pszText, _T(" K"));
269             }
270             if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
271                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetBasePriority(Index));
272             if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
273             {
274                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetHandleCount(Index));
275                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
276             }
277             if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
278             {
279                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetThreadCount(Index));
280                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
281             }
282             if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
283             {
284                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetUSERObjectCount(Index));
285                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
286             }
287             if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
288             {
289                 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetGDIObjectCount(Index));
290                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
291             }
292             if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
293             {
294                 PerfDataGetIOCounters(Index, &iocounters);
295                 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadOperationCount); */
296 #ifdef UNICODE
297 #define _ui64toa _ui64tow
298 #else
299 #endif
300                 _ui64toa(iocounters.ReadOperationCount, pnmdi->item.pszText, 10);
301                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
302             }
303             if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
304             {
305                 PerfDataGetIOCounters(Index, &iocounters);
306                 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteOperationCount); */
307                 _ui64toa(iocounters.WriteOperationCount, pnmdi->item.pszText, 10);
308                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
309             }
310             if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
311             {
312                 PerfDataGetIOCounters(Index, &iocounters);
313                 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherOperationCount); */
314                 _ui64toa(iocounters.OtherOperationCount, pnmdi->item.pszText, 10);
315                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
316             }
317             if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
318             {
319                 PerfDataGetIOCounters(Index, &iocounters);
320                 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadTransferCount); */
321                 _ui64toa(iocounters.ReadTransferCount, pnmdi->item.pszText, 10);
322                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
323             }
324             if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
325             {
326                 PerfDataGetIOCounters(Index, &iocounters);
327                 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteTransferCount); */
328                 _ui64toa(iocounters.WriteTransferCount, pnmdi->item.pszText, 10);
329                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
330             }
331             if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
332             {
333                 PerfDataGetIOCounters(Index, &iocounters);
334                 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherTransferCount); */
335                 _ui64toa(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
336                 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
337             }
338
339             break;
340
341         case NM_RCLICK:
342
343             for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
344             {
345                 memset(&lvitem, 0, sizeof(LVITEM));
346
347                 lvitem.mask = LVIF_STATE;
348                 lvitem.stateMask = LVIS_SELECTED;
349                 lvitem.iItem = Index;
350
351                 ListView_GetItem(hProcessPageListCtrl, &lvitem);
352
353                 if (lvitem.state & LVIS_SELECTED)
354                     break;
355             }
356
357             if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) &&
358                 (PerfDataGetProcessId(Index) != 0))
359             {
360                 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
361             }
362
363             break;
364
365         }
366     }
367     else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
368     {
369         switch (pnmh->code)
370         {
371         case HDN_ITEMCLICK:
372
373             /*
374              * FIXME: Fix the column sorting
375              *
376              *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
377              *bSortAscending = !bSortAscending;
378              */
379
380             break;
381
382         case HDN_ITEMCHANGED:
383
384             UpdateColumnDataHints();
385
386             break;
387
388         case HDN_ENDDRAG:
389
390             UpdateColumnDataHints();
391
392             break;
393
394         }
395     }
396
397 }
398
399 void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount)
400 {
401     TCHAR    temp[260];
402     UINT    i, j, k;
403
404     for (i=0,j=0; i<(_tcslen(strNumber) % 3); i++, j++)
405         temp[j] = strNumber[i];
406     for (k=0; i<_tcslen(strNumber); i++,j++,k++) {
407         if ((k % 3 == 0) && (j > 0))
408             temp[j++] = _T(',');
409         temp[j] = strNumber[i];
410     }
411     temp[j] = _T('\0');
412     _tcsncpy(strNumber, temp, nMaxCount);
413 }
414
415 void ProcessPageShowContextMenu(DWORD dwProcessId)
416 {
417     HMENU        hMenu;
418     HMENU        hSubMenu;
419     HMENU        hPriorityMenu;
420     POINT        pt;
421     SYSTEM_INFO    si;
422     HANDLE        hProcess;
423     DWORD        dwProcessPriorityClass;
424     TCHAR        strDebugger[260];
425     DWORD        dwDebuggerSize;
426     HKEY        hKey;
427     UINT        Idx;
428
429     memset(&si, 0, sizeof(SYSTEM_INFO));
430
431     GetCursorPos(&pt);
432     GetSystemInfo(&si);
433
434     hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_PROCESS_PAGE_CONTEXT));
435     hSubMenu = GetSubMenu(hMenu, 0);
436     hPriorityMenu = GetSubMenu(hSubMenu, 4);
437
438     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
439     dwProcessPriorityClass = GetPriorityClass(hProcess);
440     CloseHandle(hProcess);
441
442     if (si.dwNumberOfProcessors < 2)
443         RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
444     
445     if (!AreDebugChannelsSupported())
446         RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
447
448     switch (dwProcessPriorityClass)    {
449     case REALTIME_PRIORITY_CLASS:
450         CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
451         break;
452     case HIGH_PRIORITY_CLASS:
453         CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
454         break;
455     case ABOVE_NORMAL_PRIORITY_CLASS:
456         CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
457         break;
458     case NORMAL_PRIORITY_CLASS:
459         CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
460         break;
461     case BELOW_NORMAL_PRIORITY_CLASS:
462         CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
463         break;
464     case IDLE_PRIORITY_CLASS:
465         CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
466         break;
467     }
468
469     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
470     {
471         dwDebuggerSize = 260;
472         if (RegQueryValueEx(hKey, _T("Debugger"), NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
473         {
474             for (Idx=0; Idx<_tcslen(strDebugger); Idx++)
475                 strDebugger[Idx] = toupper(strDebugger[Idx]);
476
477             if (_tcsstr(strDebugger, _T("DRWTSN32")))
478                 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
479         }
480         else
481             EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
482
483         RegCloseKey(hKey);
484     } else {
485         EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
486     }
487     TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
488     DestroyMenu(hMenu);
489 }
490
491 void RefreshProcessPage(void)
492 {
493     /* Signal the event so that our refresh thread */
494     /* will wake up and refresh the process page */
495     SetEvent(hProcessPageEvent);
496 }
497
498 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
499 {
500     ULONG    OldProcessorUsage = 0;
501     ULONG    OldProcessCount = 0;
502
503     /* Create the event */
504     hProcessPageEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
505
506     /* If we couldn't create the event then exit the thread */
507     if (!hProcessPageEvent)
508         return 0;
509
510     while (1) {
511         DWORD    dwWaitVal;
512
513         /* Wait on the event */
514         dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
515
516         /* If the wait failed then the event object must have been */
517         /* closed and the task manager is exiting so exit this thread */
518         if (dwWaitVal == WAIT_FAILED)
519             return 0;
520
521         if (dwWaitVal == WAIT_OBJECT_0) {
522             TCHAR    text[260];
523
524             /* Reset our event */
525             ResetEvent(hProcessPageEvent);
526
527             if ((ULONG)SendMessage(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
528                 SendMessage(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
529
530             if (IsWindowVisible(hProcessPage))
531                 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
532
533             if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
534                 OldProcessorUsage = PerfDataGetProcessorUsage();
535                 wsprintf(text, _T("CPU Usage: %3d%%"), OldProcessorUsage);
536                 SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
537             }
538             if (OldProcessCount != PerfDataGetProcessCount()) {
539                 OldProcessCount = PerfDataGetProcessCount();
540                 wsprintf(text, _T("Processes: %d"), OldProcessCount);
541                 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
542             }
543         }
544     }
545         return 0;
546 }