fix build of ctm
[reactos.git] / rosapps / sysutils / ctm / ctm.c
1 /* Console Task Manager
2
3 ctm.c - main program file
4
5 Written by: Aleksey Bragin (aleksey@studiocerebral.com)
6
7 Most of the code dealing with getting system parameters is taken from
8 ReactOS Task Manager written by Brian Palmer (brianp@reactos.org)
9
10 Localization features added by Hervé Poussineau (hpoussineau@fr.st)
11
12 History:
13 24 October 2004 - added localization features
14 09 April 2003 - v0.1, fixed bugs, added features, ported to mingw
15 20 March 2003 - v0.03, works good under ReactOS, and allows process
16 killing
17 18 March 2003 - Initial version 0.01, doesn't work under RectOS
18
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
32
33
34 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows //headers
35 #include <windows.h>
36
37 #include <stdlib.h>
38 #include <malloc.h>
39 #include <memory.h>
40 #include <tchar.h>
41 #include <process.h>
42 #include <stdio.h>
43
44 #define NTOS_MODE_USER
45 #include <ndk/ntndk.h>
46
47 #include <epsapi/epsapi.h>
48
49 #include "ctm.h"
50 #include "resource.h"
51
52 #define MAX_PROC 17
53 #define TIMES
54
55 HANDLE hStdin;
56 HANDLE hStdout;
57 HINSTANCE hInst;
58
59 DWORD inConMode;
60 DWORD outConMode;
61
62 DWORD columnRightPositions[5];
63 TCHAR lpSeparator[80];
64 TCHAR lpHeader[80];
65 TCHAR lpMemUnit[3];
66 TCHAR lpIdleProcess[80];
67 TCHAR lpTitle[80];
68 TCHAR lpHeader[80];
69 TCHAR lpMenu[80];
70 TCHAR lpEmpty[80];
71
72 TCHAR KEY_QUIT, KEY_KILL;
73 TCHAR KEY_YES, KEY_NO;
74
75 const int ProcPerScreen = 17; // 17 processess are displayed on one page
76 ULONG ProcessCountOld = 0;
77 ULONG ProcessCount = 0;
78
79 double dbIdleTime;
80 double dbKernelTime;
81 double dbSystemTime;
82 LARGE_INTEGER liOldIdleTime = {{0,0}};
83 LARGE_INTEGER liOldKernelTime = {{0,0}};
84 LARGE_INTEGER liOldSystemTime = {{0,0}};
85
86 PPERFDATA pPerfDataOld = NULL; // Older perf data (saved to establish delta values)
87 PPERFDATA pPerfData = NULL; // Most recent copy of perf data
88
89 int selection=0;
90 int scrolled=0; // offset from which process start showing
91 int first = 0; // first time in DisplayScreen
92 SYSTEM_BASIC_INFORMATION SystemBasicInfo;
93
94 #define NEW_CONSOLE
95
96 // Functions that are needed by epsapi
97 void *PsaiMalloc(SIZE_T size) { return malloc(size); }
98 void *PsaiRealloc(void *ptr, SIZE_T size) { return realloc(ptr, size); }
99 void PsaiFree(void *ptr) { free(ptr); }
100
101 // Prototypes
102 unsigned int GetKeyPressed();
103
104 void GetInputOutputHandles()
105 {
106 #ifdef NEW_CONSOLE
107 HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
108 FILE_SHARE_READ | FILE_SHARE_WRITE,
109 0, CONSOLE_TEXTMODE_BUFFER, 0);
110
111 if (SetConsoleActiveScreenBuffer(console) == FALSE)
112 {
113 hStdin = GetStdHandle(STD_INPUT_HANDLE);
114 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
115 }
116 else
117 {
118 hStdin = GetStdHandle(STD_INPUT_HANDLE);//console;
119 hStdout = console;
120 }
121 #else
122 hStdin = GetStdHandle(STD_INPUT_HANDLE);
123 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
124 #endif
125 }
126
127 void RestoreConsole()
128 {
129 SetConsoleMode(hStdin, inConMode);
130 SetConsoleMode(hStdout, outConMode);
131
132 #ifdef NEW_CONSOLE
133 SetConsoleActiveScreenBuffer(GetStdHandle(STD_OUTPUT_HANDLE));
134 #endif
135 }
136
137 void DisplayScreen()
138 {
139 COORD pos;
140 TCHAR lpStr[80];
141 int posStr;
142 DWORD numChars;
143 int lines;
144 int idx, i;
145
146 if (first == 0)
147 {
148 // Header
149 pos.X = 2; pos.Y = 2;
150 WriteConsoleOutputCharacter(hStdout, lpTitle, _tcslen(lpTitle), pos, &numChars);
151
152 pos.X = 2; pos.Y = 3;
153 WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
154
155 pos.X = 2; pos.Y = 4;
156 WriteConsoleOutputCharacter(hStdout, lpHeader, _tcslen(lpHeader), pos, &numChars);
157
158 pos.X = 2; pos.Y = 5;
159 WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
160
161 // Footer
162 pos.X = 2; pos.Y = ProcPerScreen+6;
163 WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
164
165 // Menu
166 pos.X = 2; pos.Y = ProcPerScreen+7;
167 WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
168 WriteConsoleOutputCharacter(hStdout, lpMenu, _tcslen(lpMenu), pos, &numChars);
169
170 first = 1;
171 }
172
173 // Processess
174 lines = ProcessCount;
175 if (lines > MAX_PROC)
176 lines = MAX_PROC;
177 for (idx=0; idx<MAX_PROC; idx++)
178 {
179 int len, i;
180 TCHAR imgName[MAX_PATH];
181 TCHAR lpPid[8];
182 TCHAR lpCpu[6];
183 TCHAR lpMemUsg[12];
184 TCHAR lpPageFaults[15];
185 WORD wColor;
186
187 for (i = 0; i < 80; i++)
188 lpStr[i] = _T(' ');
189
190 // data
191 if (idx < lines && scrolled + idx < ProcessCount)
192 {
193 // image name
194 #ifdef _UNICODE
195 len = wcslen(pPerfData[scrolled+idx].ImageName);
196 #else
197 WideCharToMultiByte(CP_ACP, 0, pPerfData[scrolled+idx].ImageName, -1,
198 imgName, MAX_PATH, NULL, NULL);
199 len = strlen(imgName);
200 #endif
201 if (len > columnRightPositions[0])
202 {
203 len = columnRightPositions[0];
204 }
205 #ifdef _UNICODE
206 wcsncpy(&lpStr[2], pPerfData[scrolled+idx].ImageName, len);
207 #else
208 strncpy(&lpStr[2], imgName, len);
209 #endif
210
211 // PID
212 _stprintf(lpPid, _T("%6ld"), pPerfData[scrolled+idx].ProcessId);
213 _tcsncpy(&lpStr[columnRightPositions[1] - 6], lpPid, 6);
214
215 #ifdef TIMES
216 // CPU
217 _stprintf(lpCpu, _T("%3d%%"), pPerfData[scrolled+idx].CPUUsage);
218 _tcsncpy(&lpStr[columnRightPositions[2] - 4], lpCpu, 4);
219 #endif
220
221 // Mem usage
222 _stprintf(lpMemUsg, _T("%6ld %s"), pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024, lpMemUnit);
223 _tcsncpy(&lpStr[columnRightPositions[3] - 9], lpMemUsg, 9);
224
225 // Page Fault
226 _stprintf(lpPageFaults, _T("%12ld"), pPerfData[scrolled+idx].PageFaultCount);
227 _tcsncpy(&lpStr[columnRightPositions[4] - 12], lpPageFaults, 12);
228 }
229
230 // columns
231 lpStr[0] = _T(' ');
232 lpStr[1] = _T('|');
233 for (i = 0; i < 5; i++)
234 lpStr[columnRightPositions[i] + 1] = _T('|');
235 pos.X = 1; pos.Y = 6+idx;
236 WriteConsoleOutputCharacter(hStdout, lpStr, 74, pos, &numChars);
237
238 // Attributes now...
239 pos.X = 3; pos.Y = 6+idx;
240 if (selection == idx)
241 {
242 wColor = BACKGROUND_GREEN |
243 FOREGROUND_RED |
244 FOREGROUND_GREEN |
245 FOREGROUND_BLUE;
246 }
247 else
248 {
249 wColor = BACKGROUND_BLUE |
250 FOREGROUND_RED |
251 FOREGROUND_GREEN |
252 FOREGROUND_BLUE;
253 }
254
255 FillConsoleOutputAttribute(
256 hStdout, // screen buffer handle
257 wColor, // color to fill with
258 columnRightPositions[0] - 1, // number of cells to fill
259 pos, // first cell to write to
260 &numChars); // actual number written
261 }
262
263 return;
264 }
265
266 // returns TRUE if exiting
267 int ProcessKeys(int numEvents)
268 {
269 DWORD numChars;
270 if ((ProcessCount-scrolled < 17) && (ProcessCount > 17))
271 scrolled = ProcessCount-17;
272
273 TCHAR key = GetKeyPressed(numEvents);
274 if (key == KEY_QUIT)
275 return TRUE;
276 else if (key == KEY_KILL)
277 {
278 // user wants to kill some process, get his acknowledgement
279 DWORD pId;
280 COORD pos;
281 TCHAR lpStr[100];
282
283 pos.X = 2; pos.Y = 24;
284 if (LoadString(hInst, IDS_KILL_PROCESS, lpStr, 100))
285 WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
286
287 do {
288 GetNumberOfConsoleInputEvents(hStdin, &pId);
289 key = GetKeyPressed(pId);
290 } while (key != KEY_YES && key != KEY_NO);
291
292 if (key == KEY_YES)
293 {
294 HANDLE hProcess;
295 pId = pPerfData[selection+scrolled].ProcessId;
296 hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pId);
297
298 if (hProcess)
299 {
300 if (!TerminateProcess(hProcess, 0))
301 {
302 if (LoadString(hInst, IDS_KILL_PROCESS_ERR1, lpStr, 80))
303 {
304 WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
305 WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
306 }
307 Sleep(1000);
308 }
309
310 CloseHandle(hProcess);
311 }
312 else
313 {
314 if (LoadString(hInst, IDS_KILL_PROCESS_ERR2, lpStr, 80))
315 {
316 WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
317 _stprintf(lpStr, lpStr, pId);
318 WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
319 }
320 Sleep(1000);
321 }
322 }
323
324 first = 0;
325 }
326 else if (key == VK_UP)
327 {
328 if (selection > 0)
329 selection--;
330 else if ((selection == 0) && (scrolled > 0))
331 scrolled--;
332 }
333 else if (key == VK_DOWN)
334 {
335 if ((selection < MAX_PROC-1) && (selection < ProcessCount-1))
336 selection++;
337 else if ((selection == MAX_PROC-1) && (selection+scrolled < ProcessCount-1))
338 scrolled++;
339 }
340
341 return FALSE;
342 }
343
344 void PerfInit()
345 {
346 NtQuerySystemInformation(SystemBasicInformation, &SystemBasicInfo, sizeof(SystemBasicInfo), 0);
347 }
348
349 void PerfDataRefresh()
350 {
351 LONG status;
352 ULONG ulSize;
353 LPBYTE pBuffer;
354 ULONG BufferSize;
355 ULONG Idx, Idx2;
356 HANDLE hProcess;
357 HANDLE hProcessToken;
358 PSYSTEM_PROCESS_INFORMATION pSPI;
359 PPERFDATA pPDOld;
360 TCHAR szTemp[MAX_PATH];
361 DWORD dwSize;
362 #ifdef TIMES
363 LARGE_INTEGER liCurrentKernelTime;
364 LARGE_INTEGER liCurrentIdleTime;
365 LARGE_INTEGER liCurrentTime;
366 #endif
367 PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SysProcessorTimeInfo;
368 SYSTEM_TIMEOFDAY_INFORMATION SysTimeInfo;
369
370 #ifdef TIMES
371 // Get new system time
372 status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
373 if (status != NO_ERROR)
374 return;
375 #endif
376 // Get processor information
377 SysProcessorTimeInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors);
378 status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors, &ulSize);
379
380
381 // Get process information
382 PsaCaptureProcessesAndThreads((PSYSTEM_PROCESS_INFORMATION *)&pBuffer);
383
384 #ifdef TIMES
385 liCurrentKernelTime.QuadPart = 0;
386 liCurrentIdleTime.QuadPart = 0;
387 for (Idx=0; Idx<SystemBasicInfo.NumberOfProcessors; Idx++) {
388 liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].KernelTime.QuadPart;
389 liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].DpcTime.QuadPart;
390 liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].InterruptTime.QuadPart;
391 liCurrentIdleTime.QuadPart += SysProcessorTimeInfo[Idx].IdleTime.QuadPart;
392 }
393
394 // If it's a first call - skip idle time calcs
395 if (liOldIdleTime.QuadPart != 0) {
396 // CurrentValue = NewValue - OldValue
397 liCurrentTime.QuadPart = liCurrentIdleTime.QuadPart - liOldIdleTime.QuadPart;
398 dbIdleTime = Li2Double(liCurrentTime);
399 liCurrentTime.QuadPart = liCurrentKernelTime.QuadPart - liOldKernelTime.QuadPart;
400 dbKernelTime = Li2Double(liCurrentTime);
401 liCurrentTime.QuadPart = SysTimeInfo.CurrentTime.QuadPart - liOldSystemTime.QuadPart;
402 dbSystemTime = Li2Double(liCurrentTime);
403
404 // CurrentCpuIdle = IdleTime / SystemTime
405 dbIdleTime = dbIdleTime / dbSystemTime;
406 dbKernelTime = dbKernelTime / dbSystemTime;
407
408 // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
409 dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
410 dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
411 }
412
413 // Store new CPU's idle and system time
414 liOldIdleTime = liCurrentIdleTime;
415 liOldSystemTime = SysTimeInfo.CurrentTime;
416 liOldKernelTime = liCurrentKernelTime;
417 #endif
418
419 // Determine the process count
420 // We loop through the data we got from PsaCaptureProcessesAndThreads
421 // and count how many structures there are (until PsaWalkNextProcess
422 // returns NULL)
423 ProcessCountOld = ProcessCount;
424 ProcessCount = 0;
425 pSPI = PsaWalkFirstProcess((PSYSTEM_PROCESS_INFORMATION)pBuffer);
426 while (pSPI) {
427 ProcessCount++;
428 pSPI = PsaWalkNextProcess(pSPI);
429 }
430
431 // Now alloc a new PERFDATA array and fill in the data
432 if (pPerfDataOld) {
433 free(pPerfDataOld);
434 }
435 pPerfDataOld = pPerfData;
436 pPerfData = (PPERFDATA)malloc(sizeof(PERFDATA) * ProcessCount);
437 pSPI = PsaWalkFirstProcess((PSYSTEM_PROCESS_INFORMATION)pBuffer);
438 for (Idx=0; Idx<ProcessCount; Idx++) {
439 // Get the old perf data for this process (if any)
440 // so that we can establish delta values
441 pPDOld = NULL;
442 for (Idx2=0; Idx2<ProcessCountOld; Idx2++) {
443 if (pPerfDataOld[Idx2].ProcessId == (ULONG)(pSPI->UniqueProcessId) &&
444 /* check also for the creation time, a new process may have an id of an old one */
445 pPerfDataOld[Idx2].CreateTime.QuadPart == pSPI->CreateTime.QuadPart) {
446 pPDOld = &pPerfDataOld[Idx2];
447 break;
448 }
449 }
450
451 // Clear out process perf data structure
452 memset(&pPerfData[Idx], 0, sizeof(PERFDATA));
453
454 if (pSPI->ImageName.Buffer) {
455 wcsncpy(pPerfData[Idx].ImageName, pSPI->ImageName.Buffer, pSPI->ImageName.Length / sizeof(WCHAR));
456 pPerfData[Idx].ImageName[pSPI->ImageName.Length / sizeof(WCHAR)] = 0;
457 }
458 else
459 {
460 #ifdef _UNICODE
461 wcscpy(pPerfData[Idx].ImageName, lpIdleProcess);
462 #else
463 MultiByteToWideChar(CP_ACP, 0, lpIdleProcess, strlen(lpIdleProcess), pPerfData[Idx].ImageName, MAX_PATH);
464 #endif
465 }
466
467 pPerfData[Idx].ProcessId = (ULONG)(pSPI->UniqueProcessId);
468 pPerfData[Idx].CreateTime = pSPI->CreateTime;
469
470 if (pPDOld) {
471 #ifdef TIMES
472 double CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
473 double OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
474 double CpuTime = (CurTime - OldTime) / dbSystemTime;
475 CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; // + 0.5;
476
477 pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
478 #else
479 pPerfData[Idx].CPUUsage = 0;
480 #endif
481 }
482
483 pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
484 pPerfData[Idx].WorkingSetSizeBytes = pSPI->WorkingSetSize;
485 pPerfData[Idx].PeakWorkingSetSizeBytes = pSPI->PeakWorkingSetSize;
486 if (pPDOld)
487 pPerfData[Idx].WorkingSetSizeDelta = labs((LONG)pSPI->WorkingSetSize - (LONG)pPDOld->WorkingSetSizeBytes);
488 else
489 pPerfData[Idx].WorkingSetSizeDelta = 0;
490 pPerfData[Idx].PageFaultCount = pSPI->PageFaultCount;
491 if (pPDOld)
492 pPerfData[Idx].PageFaultCountDelta = labs((LONG)pSPI->PageFaultCount - (LONG)pPDOld->PageFaultCount);
493 else
494 pPerfData[Idx].PageFaultCountDelta = 0;
495 pPerfData[Idx].VirtualMemorySizeBytes = pSPI->VirtualSize;
496 pPerfData[Idx].PagedPoolUsagePages = pSPI->QuotaPagedPoolUsage;
497 pPerfData[Idx].NonPagedPoolUsagePages = pSPI->QuotaNonPagedPoolUsage;
498 pPerfData[Idx].BasePriority = pSPI->BasePriority;
499 pPerfData[Idx].HandleCount = pSPI->HandleCount;
500 pPerfData[Idx].ThreadCount = pSPI->NumberOfThreads;
501 //pPerfData[Idx].SessionId = pSPI->SessionId;
502
503 #ifdef EXTRA_INFO
504 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pSPI->UniqueProcessId);
505 if (hProcess) {
506 if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_IMPERSONATE, &hProcessToken)) {
507 ImpersonateLoggedOnUser(hProcessToken);
508 memset(szTemp, 0, sizeof(TCHAR[MAX_PATH]));
509 dwSize = MAX_PATH;
510 GetUserName(szTemp, &dwSize);
511 #ifndef UNICODE
512 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTemp, -1, pPerfData[Idx].UserName, MAX_PATH);
513 /*
514 int MultiByteToWideChar(
515 UINT CodePage, // code page
516 DWORD dwFlags, // character-type options
517 LPCSTR lpMultiByteStr, // string to map
518 int cbMultiByte, // number of bytes in string
519 LPWSTR lpWideCharStr, // wide-character buffer
520 int cchWideChar // size of buffer
521 );
522 */
523 #endif
524 RevertToSelf();
525 CloseHandle(hProcessToken);
526 }
527 CloseHandle(hProcess);
528 }
529 #endif
530 #ifdef TIMES
531 pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
532 pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
533 #endif
534 pSPI = PsaWalkNextProcess(pSPI);
535 }
536 PsaFreeCapture(pBuffer);
537
538 free(SysProcessorTimeInfo);
539 }
540
541 // Code partly taken from slw32tty.c from mc/slang
542 unsigned int GetKeyPressed(int events)
543 {
544 long key;
545 DWORD bytesRead;
546 INPUT_RECORD record;
547 int i;
548
549
550 for (i=0; i<events; i++)
551 {
552 if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
553 return 0;
554 }
555
556 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
557 return record.Event.KeyEvent.wVirtualKeyCode;//.uChar.AsciiChar;
558 }
559
560 return 0;
561 }
562
563
564 int main(int argc, char **argv)
565 {
566 int i;
567 TCHAR lpStr[80];
568
569 for (i = 0; i < 80; i++)
570 lpEmpty[i] = lpHeader[i] = _T(' ');
571 lpEmpty[79] = _T('\0');
572
573 /* Initialize global variables */
574 hInst = 0 /* FIXME: which value? [used with LoadString(hInst, ..., ..., ...)] */;
575 if (LoadString(hInst, IDS_COLUMN_IMAGENAME, lpStr, 80))
576 {
577 columnRightPositions[0] = _tcslen(lpStr);
578 _tcsncpy(&lpHeader[2], lpStr, _tcslen(lpStr));
579 }
580 if (LoadString(hInst, IDS_COLUMN_PID, lpStr, 80))
581 {
582 columnRightPositions[1] = columnRightPositions[0] + _tcslen(lpStr) + 3;
583 _tcsncpy(&lpHeader[columnRightPositions[0] + 2], lpStr, _tcslen(lpStr));
584 }
585 if (LoadString(hInst, IDS_COLUMN_CPU, lpStr, 80))
586 {
587 columnRightPositions[2] = columnRightPositions[1] + _tcslen(lpStr) + 3;
588 _tcsncpy(&lpHeader[columnRightPositions[1] + 2], lpStr, _tcslen(lpStr));
589 }
590 if (LoadString(hInst, IDS_COLUMN_MEM, lpStr, 80))
591 {
592 columnRightPositions[3] = columnRightPositions[2] + _tcslen(lpStr) + 3;
593 _tcsncpy(&lpHeader[columnRightPositions[2] + 2], lpStr, _tcslen(lpStr));
594 }
595 if (LoadString(hInst, IDS_COLUMN_PF, lpStr, 80))
596 {
597 columnRightPositions[4] = columnRightPositions[3] + _tcslen(lpStr) + 3;
598 _tcsncpy(&lpHeader[columnRightPositions[3] + 2], lpStr, _tcslen(lpStr));
599 }
600
601 for (i = 0; i < columnRightPositions[4]; i++)
602 lpSeparator[i] = _T('-');
603 lpHeader[0] = _T('|');
604 lpSeparator[0] = _T('+');
605 for (i = 0; i < 5; i++)
606 {
607 lpHeader[columnRightPositions[i]] = _T('|');
608 lpSeparator[columnRightPositions[i]] = _T('+');
609 }
610 lpSeparator[columnRightPositions[4] + 1] = _T('\0');
611 lpHeader[columnRightPositions[4] + 1] = _T('\0');
612
613
614 if (!LoadString(hInst, IDS_APP_TITLE, lpTitle, 80))
615 lpTitle[0] = _T('\0');
616 if (!LoadString(hInst, IDS_COLUMN_MEM_UNIT, lpMemUnit, 3))
617 lpMemUnit[0] = _T('\0');
618 if (!LoadString(hInst, IDS_MENU, lpMenu, 80))
619 lpMenu[0] = _T('\0');
620 if (!LoadString(hInst, IDS_IDLE_PROCESS, lpIdleProcess, 80))
621 lpIdleProcess[0] = _T('\0');
622
623 if (LoadString(hInst, IDS_MENU_QUIT, lpStr, 2))
624 KEY_QUIT = lpStr[0];
625 if (LoadString(hInst, IDS_MENU_KILL_PROCESS, lpStr, 2))
626 KEY_KILL = lpStr[0];
627 if (LoadString(hInst, IDS_YES, lpStr, 2))
628 KEY_YES = lpStr[0];
629 if (LoadString(hInst, IDS_NO, lpStr, 2))
630 KEY_NO = lpStr[0];
631
632 GetInputOutputHandles();
633
634 if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
635 {
636 if (LoadString(hInst, IDS_CTM_GENERAL_ERR1, lpStr, 80))
637 _tprintf(lpStr);
638 return -1;
639 }
640
641 if (GetConsoleMode(hStdin, &inConMode) == 0)
642 {
643 if (LoadString(hInst, IDS_CTM_GENERAL_ERR2, lpStr, 80))
644 _tprintf(lpStr);
645 return -1;
646 }
647
648 if (GetConsoleMode(hStdout, &outConMode) == 0)
649 {
650 if (LoadString(hInst, IDS_CTM_GENERAL_ERR3, lpStr, 80))
651 _tprintf(lpStr);
652 return -1;
653 }
654
655 SetConsoleMode(hStdin, 0); //FIXME: Should check for error!
656 SetConsoleMode(hStdout, 0); //FIXME: Should check for error!
657
658 PerfInit();
659
660 while (1)
661 {
662 DWORD numEvents;
663
664 PerfDataRefresh();
665 DisplayScreen();
666
667 /* WaitForSingleObject for console handles is not implemented in ROS */
668 WaitForSingleObject(hStdin, 1000);
669 GetNumberOfConsoleInputEvents(hStdin, &numEvents);
670
671 if (numEvents > 0)
672 {
673 if (ProcessKeys(numEvents) == TRUE)
674 break;
675 }
676 }
677
678 RestoreConsole();
679 return 0;
680 }