--- trunk/rosapps/sysutils/ctm/ctm.c 2005-01-15 15:09:22 UTC (rev 13056)
+++ trunk/rosapps/sysutils/ctm/ctm.c 2005-01-15 15:28:51 UTC (rev 13057)
@@ -1,3 +1,6 @@
+#define UNICODE
+#define _UNICODE
+
/* Console Task Manager
ctm.c - main program file
@@ -6,8 +9,11 @@
Most of the code dealing with getting system parameters is taken from
ReactOS Task Manager written by Brian Palmer (brianp@reactos.org)
+
+ Localization features added by Hervé Poussineau (hpoussineau@fr.st)
History:
+ 24 October 2004 - added localization features
09 April 2003 - v0.1, fixed bugs, added features, ported to mingw
20 March 2003 - v0.03, works good under ReactOS, and allows process
killing
@@ -42,18 +48,31 @@
#include <ntos/zwtypes.h>
#include "ctm.h"
+#include "resource.h"
#define MAX_PROC 17
#define TIMES
HANDLE hStdin;
HANDLE hStdout;
+HINSTANCE hInst;
DWORD inConMode;
DWORD outConMode;
-//PROCNTQSI NtQuerySystemInformation= NULL;
+DWORD columnRightPositions[5];
+TCHAR lpSeparator[80];
+TCHAR lpHeader[80];
+TCHAR lpMemUnit[3];
+TCHAR lpIdleProcess[80];;
+TCHAR lpTitle[80];
+TCHAR lpHeader[80];
+TCHAR lpMenu[80];
+TCHAR lpEmpty[80];
+TCHAR KEY_QUIT, KEY_KILL;
+TCHAR KEY_YES, KEY_NO;
+
const int ProcPerScreen = 17; // 17 processess are displayed on one page
ULONG ProcessCountOld = 0;
ULONG ProcessCount = 0;
@@ -70,9 +89,11 @@
int selection=0;
int scrolled=0; // offset from which process start showing
+int first = 0; // first time in DisplayScreen
#define NEW_CONSOLE
+// Functions that are needed by epsapi
void *PsaiMalloc(SIZE_T size) { return malloc(size); }
void *PsaiRealloc(void *ptr, SIZE_T size) { return realloc(ptr, size); }
void PsaiFree(void *ptr) { free(ptr); }
@@ -120,39 +141,33 @@
int posStr;
DWORD numChars;
int lines;
- int idx;
- static int first = 0;
+ int idx, i;
if (first == 0)
{
- // Header
- pos.X = 2; pos.Y = 2;
- _tcscpy(lpStr, _T("Console TaskManager v0.1 by Aleksey Bragin <aleksey@studiocerebral.com>"));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
+ // Header
+ pos.X = 2; pos.Y = 2;
+ WriteConsoleOutputCharacter(hStdout, lpTitle, _tcslen(lpTitle), pos, &numChars);
- pos.X = 2; pos.Y = 3;
- _tcscpy(lpStr, _T("+-------------------------------+-------+-----+-----------+-------------+"));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
+ pos.X = 2; pos.Y = 3;
+ WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
- pos.X = 2; pos.Y = 4;
- _tcscpy(lpStr, _T("| Image name | PID | CPU | Mem Usage | Page Faults |"));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
+ pos.X = 2; pos.Y = 4;
+ WriteConsoleOutputCharacter(hStdout, lpHeader, _tcslen(lpHeader), pos, &numChars);
- pos.X = 2; pos.Y = 5;
- _tcscpy(lpStr, _T("+-------------------------------+-------+-----+-----------+-------------+"));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
+ pos.X = 2; pos.Y = 5;
+ WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
- // Footer
- pos.X = 2; pos.Y = 23;
- _tcscpy(lpStr, _T("+-------------------------------+-------+-----+-----------+-------------+"));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
+ // Footer
+ pos.X = 2; pos.Y = ProcPerScreen+6;
+ WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
+
+ // Menu
+ pos.X = 2; pos.Y = ProcPerScreen+7;
+ WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
+ WriteConsoleOutputCharacter(hStdout, lpMenu, _tcslen(lpMenu), pos, &numChars);
- // Menu
- pos.X = 2; pos.Y = 24;
- _tcscpy(lpStr, _T("Press: q - quit, k - kill process "));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
-
- first = 1;
+ first = 1;
}
// Processess
@@ -168,11 +183,14 @@
TCHAR lpMemUsg[12];
TCHAR lpPageFaults[15];
WORD wColor;
+
+ for (i = 0; i < 80; i++)
+ lpStr[i] = _T(' ');
// data
- // image name
if (idx < lines && scrolled + idx < ProcessCount)
{
+ // image name
#ifdef _UNICODE
len = wcslen(pPerfData[scrolled+idx].ImageName);
#else
@@ -180,77 +198,40 @@
imgName, MAX_PATH, NULL, NULL);
len = strlen(imgName);
#endif
- if (len > 31)
- {
- len = 31;
- }
+ if (len > columnRightPositions[0])
+ {
+ len = columnRightPositions[0];
+ }
#ifdef _UNICODE
wcsncpy(&lpStr[2], pPerfData[scrolled+idx].ImageName, len);
#else
strncpy(&lpStr[2], imgName, len);
#endif
- }
- else
- {
- len = 0;
- }
- if (len < 31)
- {
- _tcsncpy(&lpStr[2 + len], _T(" "), 31 - len);
- }
- // PID
- if (idx < lines && scrolled + idx < ProcessCount)
- {
- _stprintf(lpPid, _T("%6ld "), pPerfData[scrolled+idx].ProcessId);
- _tcsncpy(&lpStr[34], lpPid, 7);
- }
- else
- {
- _tcsncpy(&lpStr[34], _T(" "), 7);
- }
+ // PID
+ _stprintf(lpPid, _T("%6ld"), pPerfData[scrolled+idx].ProcessId);
+ _tcsncpy(&lpStr[columnRightPositions[1] - 6], lpPid, 6);
- // CPU
- if (idx < lines && scrolled + idx < ProcessCount)
- {
- _stprintf(lpCpu, _T("%3d%% "), pPerfData[scrolled+idx].CPUUsage);
- _tcsncpy(&lpStr[42], lpCpu, 5);
- }
- else
- {
- _tcsncpy(&lpStr[42], _T(" "), 5);
- }
+#ifdef TIMES
+ // CPU
+ _stprintf(lpCpu, _T("%3d%%"), pPerfData[scrolled+idx].CPUUsage);
+ _tcsncpy(&lpStr[columnRightPositions[2] - 4], lpCpu, 4);
+#endif
- // Mem usage
- if (idx < lines && scrolled + idx < ProcessCount)
- {
- _stprintf(lpMemUsg, _T("%6ld "), pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024);
- _tcsncpy(&lpStr[48], lpMemUsg, 11);
- }
- else
- {
- _tcsncpy(&lpStr[48], _T(" "), 11);
- }
+ // Mem usage
+ _stprintf(lpMemUsg, _T("%6ld %s"), pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024, lpMemUnit);
+ _tcsncpy(&lpStr[columnRightPositions[3] - 9], lpMemUsg, 9);
- // Page Fault
- if (idx < lines && scrolled + idx < ProcessCount)
- {
- _stprintf(lpPageFaults, _T("%12ld "), pPerfData[scrolled+idx].PageFaultCount);
- _tcsncpy(&lpStr[60], lpPageFaults, 13);
+ // Page Fault
+ _stprintf(lpPageFaults, _T("%12ld"), pPerfData[scrolled+idx].PageFaultCount);
+ _tcsncpy(&lpStr[columnRightPositions[4] - 12], lpPageFaults, 12);
}
- else
- {
- _tcsncpy(&lpStr[60], _T(" "), 13);
- }
// columns
lpStr[0] = _T(' ');
lpStr[1] = _T('|');
- lpStr[33] = _T('|');
- lpStr[41] = _T('|');
- lpStr[47] = _T('|');
- lpStr[59] = _T('|');
- lpStr[73] = _T('|');
+ for (i = 0; i < 5; i++)
+ lpStr[columnRightPositions[i] + 1] = _T('|');
pos.X = 1; pos.Y = 6+idx;
WriteConsoleOutputCharacter(hStdout, lpStr, 74, pos, &numChars);
@@ -274,7 +255,7 @@
FillConsoleOutputAttribute(
hStdout, // screen buffer handle
wColor, // color to fill with
- 31, // number of cells to fill
+ columnRightPositions[0] - 1, // number of cells to fill
pos, // first cell to write to
&numChars); // actual number written
}
@@ -285,13 +266,14 @@
// returns TRUE if exiting
int ProcessKeys(int numEvents)
{
+ DWORD numChars;
if ((ProcessCount-scrolled < 17) && (ProcessCount > 17))
scrolled = ProcessCount-17;
TCHAR key = GetKeyPressed(numEvents);
- if (key == VK_Q)
+ if (key == KEY_QUIT)
return TRUE;
- else if (key == VK_K)
+ else if (key == KEY_KILL)
{
// user wants to kill some process, get his acknowledgement
DWORD pId;
@@ -299,15 +281,15 @@
TCHAR lpStr[100];
pos.X = 2; pos.Y = 24;
- _tcscpy(lpStr, _T("Are you sure you want to kill this process? (y/n)"));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &pId);
+ if (LoadString(hInst, IDS_KILL_PROCESS, lpStr, 100))
+ WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
do {
GetNumberOfConsoleInputEvents(hStdin, &pId);
key = GetKeyPressed(pId);
} while (key == 0);
-
- if (key == VK_Y)
+
+ if (key == KEY_YES)
{
HANDLE hProcess;
pId = pPerfData[selection+scrolled].ProcessId;
@@ -317,8 +299,11 @@
{
if (!TerminateProcess(hProcess, 0))
{
- _tcscpy(lpStr, _T("Unable to terminate this process... "));
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &pId);
+ if (LoadString(hInst, IDS_KILL_PROCESS_ERR1, lpStr, 80))
+ {
+ WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
+ WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
+ }
Sleep(1000);
}
@@ -326,11 +311,17 @@
}
else
{
- _stprintf(lpStr, _T("Unable to terminate process %3d (unable to OpenProcess) "), pId);
- WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &pId);
- Sleep(1000);
+ if (LoadString(hInst, IDS_KILL_PROCESS_ERR2, lpStr, 80))
+ {
+ WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
+ _stprintf(lpStr, lpStr, pId);
+ WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
+ }
+ Sleep(1000);
}
}
+
+ first = 0;
}
else if (key == VK_UP)
{
@@ -350,11 +341,6 @@
return FALSE;
}
-void PerfInit()
-{
-// NtQuerySystemInformation = //(PROCNTQSI)GetProcAddress(GetModuleHandle(_T("ntdll.dll")), //"NtQuerySystemInformation");
-}
-
void PerfDataRefresh()
{
LONG status;
@@ -435,11 +421,9 @@
// Now alloc a new PERFDATA array and fill in the data
if (pPerfDataOld) {
- //delete[] pPerfDataOld;
free(pPerfDataOld);
}
pPerfDataOld = pPerfData;
- //pPerfData = new PERFDATA[ProcessCount];
pPerfData = (PPERFDATA)malloc(sizeof(PERFDATA) * ProcessCount);
pSPI = PsaWalkFirstProcess((PSYSTEM_PROCESSES)pBuffer);
for (Idx=0; Idx<ProcessCount; Idx++) {
@@ -461,7 +445,13 @@
pPerfData[Idx].ImageName[pSPI->ProcessName.Length / sizeof(WCHAR)] = 0;
}
else
- wcscpy(pPerfData[Idx].ImageName, L"System Idle Process");
+ {
+#ifdef _UNICODE
+ wcscpy(pPerfData[Idx].ImageName, lpIdleProcess);
+#else
+ MultiByteToWideChar(CP_ACP, 0, lpIdleProcess, strlen(lpIdleProcess), pPerfData[Idx].ImageName, MAX_PATH);
+#endif
+ }
pPerfData[Idx].ProcessId = pSPI->ProcessId;
@@ -531,7 +521,6 @@
#endif
pSPI = PsaWalkNextProcess(pSPI);
}
- //delete[] pBuffer;
PsaFreeCapture(pBuffer);
free(SysProcessorTimeInfo);
@@ -565,31 +554,96 @@
int main(int *argc, char **argv)
{
+ int i;
+ TCHAR lpStr[80];
+
+ for (i = 0; i < 80; i++)
+ lpEmpty[i] = lpHeader[i] = _T(' ');
+ lpEmpty[79] = _T('\0');
+
+ /* Initialize global variables */
+ hInst = 0 /* FIXME: which value? [used with LoadString(hInst, ..., ..., ...)] */;
+ if (LoadString(hInst, IDS_COLUMN_IMAGENAME, lpStr, 80))
+ {
+ columnRightPositions[0] = _tcslen(lpStr);
+ _tcsncpy(&lpHeader[2], lpStr, _tcslen(lpStr));
+ }
+ if (LoadString(hInst, IDS_COLUMN_PID, lpStr, 80))
+ {
+ columnRightPositions[1] = columnRightPositions[0] + _tcslen(lpStr) + 3;
+ _tcsncpy(&lpHeader[columnRightPositions[0] + 2], lpStr, _tcslen(lpStr));
+ }
+ if (LoadString(hInst, IDS_COLUMN_CPU, lpStr, 80))
+ {
+ columnRightPositions[2] = columnRightPositions[1] + _tcslen(lpStr) + 3;
+ _tcsncpy(&lpHeader[columnRightPositions[1] + 2], lpStr, _tcslen(lpStr));
+ }
+ if (LoadString(hInst, IDS_COLUMN_MEM, lpStr, 80))
+ {
+ columnRightPositions[3] = columnRightPositions[2] + _tcslen(lpStr) + 3;
+ _tcsncpy(&lpHeader[columnRightPositions[2] + 2], lpStr, _tcslen(lpStr));
+ }
+ if (LoadString(hInst, IDS_COLUMN_PF, lpStr, 80))
+ {
+ columnRightPositions[4] = columnRightPositions[3] + _tcslen(lpStr) + 3;
+ _tcsncpy(&lpHeader[columnRightPositions[3] + 2], lpStr, _tcslen(lpStr));
+ }
+
+ for (i = 0; i < columnRightPositions[4]; i++)
+ lpSeparator[i] = _T('-');
+ lpHeader[0] = _T('|');
+ lpSeparator[0] = _T('+');
+ for (i = 0; i < 5; i++)
+ {
+ lpHeader[columnRightPositions[i]] = _T('|');
+ lpSeparator[columnRightPositions[i]] = _T('+');
+ }
+ lpSeparator[columnRightPositions[4] + 1] = _T('\0');
+
+ if (!LoadString(hInst, IDS_APP_TITLE, lpTitle, 80))
+ lpTitle[0] = _T('\0');
+ if (!LoadString(hInst, IDS_COLUMN_MEM_UNIT, lpMemUnit, 3))
+ lpMemUnit[0] = _T('\0');
+ if (!LoadString(hInst, IDS_MENU, lpMenu, 80))
+ lpMenu[0] = _T('\0');
+ if (!LoadString(hInst, IDS_IDLE_PROCESS, lpIdleProcess, 80))
+ lpIdleProcess[0] = _T('\0');
+
+ if (LoadString(hInst, IDS_MENU_QUIT, lpStr, 2))
+ KEY_QUIT = lpStr[0];
+ if (LoadString(hInst, IDS_MENU_KILL_PROCESS, lpStr, 2))
+ KEY_KILL = lpStr[0];
+ if (LoadString(hInst, IDS_YES, lpStr, 2))
+ KEY_YES = lpStr[0];
+ if (LoadString(hInst, IDS_NO, lpStr, 2))
+ KEY_NO = lpStr[0];
+
GetInputOutputHandles();
if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
{
- printf("ctm: can't use console.");
+ if (LoadString(hInst, IDS_CTM_GENERAL_ERR1, lpStr, 80))
+ _tprintf(lpStr);
return -1;
}
if (GetConsoleMode(hStdin, &inConMode) == 0)
{
- printf("ctm: can't GetConsoleMode() for input console.");
+ if (LoadString(hInst, IDS_CTM_GENERAL_ERR2, lpStr, 80))
+ _tprintf(lpStr);
return -1;
}
if (GetConsoleMode(hStdout, &outConMode) == 0)
{
- printf("ctm: can't GetConsoleMode() for output console.");
+ if (LoadString(hInst, IDS_CTM_GENERAL_ERR3, lpStr, 80))
+ _tprintf(lpStr);
return -1;
}
SetConsoleMode(hStdin, 0); //FIXME: Should check for error!
SetConsoleMode(hStdout, 0); //FIXME: Should check for error!
- PerfInit();
-
while (1)
{
DWORD numEvents;
@@ -598,7 +652,7 @@
DisplayScreen();
//WriteConsole(hStdin, " ", 1, &numEvents, NULL); // TODO: Make another way (this is ugly, I know)
-#if 1
+#if 0
/* WaitForSingleObject for console handles is not implemented in ROS */
WaitForSingleObject(hStdin, 1000);
#endif
@@ -610,7 +664,7 @@
if (ProcessKeys(numEvents) == TRUE)
break;
}
-#if 0
+#if 1
else
{
/* Should be removed, if WaitForSingleObject is implemented for console handles */