Subversion Repositories Kolibri OS

Rev

Rev 9597 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7361 leency 1
/*
2
 * System Monitor
9584 leency 3
 * version 1.41
7361 leency 4
 * Author: Leency
5
*/
6
 
9581 leency 7
#define MEMSIZE 1024*60
9584 leency 8
//#define NO_DLL_INIT
7361 leency 9
 
7614 leency 10
//===================================================//
11
//                                                   //
12
//                       LIB                         //
13
//                                                   //
14
//===================================================//
15
 
7361 leency 16
#include "../lib/gui.h"
7369 leency 17
#include "../lib/fs.h"
7614 leency 18
#include "../lib/list_box.h"
7361 leency 19
 
7614 leency 20
#include "../lib/obj/box_lib.h"
9584 leency 21
#include "../lib/obj/libini.h"
7361 leency 22
 
7614 leency 23
#include "../lib/patterns/select_list.h"
24
#include "../lib/patterns/restart_process.h"
25
 
7361 leency 26
//===================================================//
27
//                                                   //
8277 leency 28
//                      CONST                        //
7361 leency 29
//                                                   //
30
//===================================================//
31
 
8277 leency 32
#define GAP 16   //Window padding
33
#define WIN_CONTENT_X GAP
34
#define WIN_CONTENT_Y GAP+15
35
#define PROCESS_LIST_W 260
36
#define RIGHT_X PROCESS_LIST_W + GAP + GAP + 22
7906 leency 37
#define ICONGAP 26
8277 leency 38
#define BOTPANEL_H 36
7361 leency 39
 
7906 leency 40
#ifdef LANG_RUS
8277 leency 41
	#define T_APP_TITLE      "Системный монитор"
42
	#define T_SHOW_SYSTEM    "Системные"
43
	#define T_DETAILS        "Подробнее"
44
	#define T_PROC_KILL      "Снять задачу"
45
	#define T_PROC_INFO      "Инфо"
9581 leency 46
	#define T_PROC_HEADER    "Процесс        ОЗУ Кб    ЦП %"
8277 leency 47
	#define T_CPU_LOAD       "Загрузка процессора %i%%   "
48
	#define T_RAM_USAGE      "Память ОЗУ: %i Мб свободно из %i Мб"
49
	#define T_RD_USAGE       "Системный диск: %i Кб свободно из 1.4 Мб"
50
	#define T_TMP_USAGE      "TMP%i диск: %i Мб свободно из %i Мб"
7906 leency 51
#else
8277 leency 52
	#define T_APP_TITLE      "System Monitor"
53
	#define T_SHOW_SYSTEM    "System"
54
	#define T_DETAILS        "Details"
55
	#define T_PROC_KILL      "Terminate"
56
	#define T_PROC_INFO      "Info"
9581 leency 57
	#define T_PROC_HEADER    "Process        RAM KB   CPU %"
8277 leency 58
	#define T_CPU_LOAD       "CPU load %i%%   "
9301 leency 59
	#define T_RAM_USAGE      "RAM usage: %i MB free of %i MB"
60
	#define T_RD_USAGE       "System disk usage: %i MB free of 1.4 MB"
61
	#define T_TMP_USAGE      "TMP%i usage: %i MB free of %i MB"
7906 leency 62
#endif
7369 leency 63
 
8277 leency 64
enum {
9581 leency 65
	BTN_SHOW_SYSTEM_PROCESSES=200,
66
	BTN_PROC_KILL,
67
	BTN_PROC_INFO,
68
	BTN_MENU,
69
	BTN_SHOWHIDE_SENSORS
8277 leency 70
};
7361 leency 71
 
7906 leency 72
//===================================================//
73
//                                                   //
8277 leency 74
//                       VARS                        //
7906 leency 75
//                                                   //
76
//===================================================//
77
 
8277 leency 78
int current_process_id = 0;
9584 leency 79
int proc_list[MAX_PROCESS_COUNT];
8277 leency 80
 
81
checkbox show_system = { T_SHOW_SYSTEM, false };
82
 
83
sensor cpu;
84
sensor ram;
85
sensor rd;
86
sensor tmp;
87
 
88
proc_info Form;
89
 
90
int right_w;
91
 
9584 leency 92
bool show_sensors;
9581 leency 93
 
9584 leency 94
_ini ini = { "/sys/settings/app.ini", "Sysmon" };
95
 
8277 leency 96
//===================================================//
97
//                                                   //
98
//                       CODE                        //
99
//                                                   //
100
//===================================================//
101
 
102
void main()
7361 leency 103
{
8277 leency 104
	int btn;
9581 leency 105
	load_dll(boxlib, #box_lib_init,0);
9584 leency 106
	load_dll(libini, #lib_init,1);
107
	ReadIni();
8407 leency 108
	@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
8381 leency 109
	loop() switch(@WaitEventTimeout(50))
8277 leency 110
	{
9584 leency 111
		case evMouse:
112
			SelectList_ProcessMouse();
113
			if (mouse.up) && (mouse.pkm) || (mouse.mkm) EventShowTinfo();
8277 leency 114
			break;
115
		case evKey:
116
			GetKeys();
9584 leency 117
			switch(key_scancode) {
118
				case SCAN_CODE_ESC:
119
						EventExit();
120
				case SCAN_CODE_DEL:
121
						EventKillCurrentProcess();
122
						break;
123
				case SCAN_CODE_SPACE:
124
						show_sensors ^= 1;
125
						goto _DRAW_WINDOW;
126
				case SCAN_CODE_ENTER:
127
						EventShowTinfo();
128
						break;
129
				case SCAN_CODE_TAB:
130
						show_system.checked ^= 1;
131
						SelectList_LineChanged();
132
						break;
133
				default:
134
						if (select_list.ProcessKey(key_scancode)) {
135
							SelectList_LineChanged();
136
						}
137
			}
8277 leency 138
			break;
139
		case evButton:
8407 leency 140
			btn = @GetButtonID();
9584 leency 141
			if (1==btn) EventExit();
8277 leency 142
 
143
			if (show_system.click(btn)) {
144
				SelectList_LineChanged();
145
			}
9581 leency 146
			if (BTN_PROC_KILL == btn) {
8277 leency 147
				EventKillCurrentProcess();
148
			}
9581 leency 149
			if (BTN_PROC_INFO == btn) {
9584 leency 150
				EventShowTinfo();
8277 leency 151
			}
9581 leency 152
			if (BTN_SHOWHIDE_SENSORS == btn) {
153
				show_sensors ^= 1;
154
				GOTO _DRAW_WINDOW;
155
			}
8277 leency 156
			break;
157
		case evReDraw:
158
			sc.get();
9584 leency 159
			DefineAndDrawWindow(Form.left, Form.top, Form.width, Form.height, 0x33, sc.work, T_APP_TITLE,0);
9581 leency 160
			_DRAW_WINDOW:
8277 leency 161
			GetProcessInfo(#Form, SelfInfo);
8946 leency 162
			if (Form.status_window&ROLLED_UP) break;
8277 leency 163
			if (Form.height < 420) { MoveSize(OLD,OLD,OLD,420); break; }
9581 leency 164
			if (show_sensors) {
165
				if (Form.width < RIGHT_X+370) { MoveSize(OLD,OLD,RIGHT_X+370,OLD); break; }
166
			} else {
167
				if (Form.width != RIGHT_X+5) { MoveSize(OLD,OLD,RIGHT_X+5,OLD); break; }
168
			}
8277 leency 169
			right_w = Form.cwidth - RIGHT_X - GAP;
170
			right_w &= ~1; // make sure the number is even
171
			WriteText(GAP+5, WIN_CONTENT_Y-20, 0x90, sc.work_text, T_PROC_HEADER);
172
 
9584 leency 173
			DefineButton(RIGHT_X-38,WIN_CONTENT_Y-25,18,18,BTN_SHOWHIDE_SENSORS,sc.button);
9602 leency 174
			DrawRectangle3D(RIGHT_X-38,WIN_CONTENT_Y-25,19,18,sc.line,sc.light);
175
			PutPixel(RIGHT_X-38+19,WIN_CONTENT_Y-25,sc.light);
9581 leency 176
			EDX = "<\0>";
177
			EDX += show_sensors * 2;
9584 leency 178
			WriteText(RIGHT_X-38+5,WIN_CONTENT_Y-25+2,0x90,sc.button_text, EDX);
9581 leency 179
			//EBX += 5 << 16;
180
			//$int 64
181
 
8277 leency 182
			//bool burger_active = false;
183
			//if (menu_id == OPEN_FILE) burger_active = true;
9581 leency 184
			//DrawTopPanelButton(BTN_MENU, Form.cwidth-GAP-3, GAP, -1, burger_active);
8277 leency 185
 
186
			SelectList_Init(GAP, WIN_CONTENT_Y, PROCESS_LIST_W,
8933 leency 187
				Form.cheight-BOTPANEL_H-WIN_CONTENT_Y);
8277 leency 188
			SelectList_DrawBorder();
189
 
190
			DrawBar(select_list.x-2, select_list.y+select_list.h+2,
191
				select_list.w+scroll1.size_x+4, BOTPANEL_H, sc.work);
192
			DrawCaptButton(PROCESS_LIST_W+GAP-110+18, select_list.y+select_list.h+5,
9581 leency 193
				110,23,BTN_PROC_KILL,0xF38181, 0xFFFfff, T_PROC_KILL);
8277 leency 194
			DrawCaptButton(PROCESS_LIST_W+GAP-165+18, select_list.y+select_list.h+5,
9581 leency 195
				46,23,BTN_PROC_INFO,sc.button, sc.button_text, T_PROC_INFO);
8277 leency 196
			show_system.draw(GAP-1, select_list.y+select_list.h+10);
197
 
198
			//WriteText(RIGHT_X, WIN_CONTENT_Y+25, 0x90, sc.work, "Update period: 5 seconds");
9581 leency 199
			if (show_sensors) {
200
				cpu.set_size(RIGHT_X, WIN_CONTENT_Y+25, right_w, 100);
201
				ram.set_size(RIGHT_X, WIN_CONTENT_Y+170, right_w, 23);
202
				rd.set_size(RIGHT_X, WIN_CONTENT_Y+240, right_w, 23);
203
			}
8277 leency 204
		default:
8381 leency 205
			SelectList_LineChanged();
9581 leency 206
			if (show_sensors) {
207
				MonitorCpu();
208
				MonitorRam();
209
				MonitorRd();
210
				MonitorTmp();
211
			}
8277 leency 212
	}
7361 leency 213
}
7614 leency 214
 
8277 leency 215
void EventKillCurrentProcess()
7906 leency 216
{
8277 leency 217
	KillProcess(current_process_id);
218
	pause(10);
219
	SelectList_LineChanged();
7906 leency 220
}
7614 leency 221
 
8277 leency 222
void Processes__GetProcessList()
223
{
224
	int i, j;
225
	proc_info Process;
7906 leency 226
 
8277 leency 227
	select_list.count=0;
228
	for (i=0; i
229
	{
230
		GetProcessInfo(#Process, i);
231
		if (Process.name)
232
		{
233
			for (j=0; j<11; j++) if (Process.name[j]!=' ') {
234
				if (show_system.checked==false) {
235
					//do not show system process
236
					if (Process.name[0]=='@') break;
237
					if (!strcmp(#Process.name, "IDLE")) break;
238
					if (!strcmp(#Process.name, "OS")) break;
239
				}
240
				proc_list[select_list.count] = i;
241
				select_list.count++;
242
				break;
243
			}
244
		}
245
	}
246
}
247
 
248
void SelectList_DrawLine(dword i)
7906 leency 249
{
8353 leency 250
	int posy, j, len;
251
	char cpu_use[16], mem_use[16], mem_use_pretty[16];
8277 leency 252
	dword bg_color;
253
	proc_info Process;
254
	static unsigned maxcpu;
255
	if (!maxcpu) maxcpu = GetCpuFrequency();
7909 leency 256
 
8277 leency 257
	GetProcessInfo(#Process, proc_list[i+select_list.first]);
258
 
259
	posy = i *select_list.item_h + select_list.y;
260
	if (i % 2) bg_color = 0xFFFfff; else bg_color = 0xF0F0F0;
261
	if (i+select_list.first == select_list.cur_y) {
262
		current_process_id = Process.ID;
263
		bg_color = 0x67CCEB;
264
	}
265
	DrawBar(select_list.x, posy, select_list.w, select_list.item_h, bg_color);
7909 leency 266
 
8277 leency 267
	WriteText(GAP+5, posy+select_list.text_y, 0x90, 0, #Process.name);
268
 
8353 leency 269
	if (Process.use_memory < 3670016000)
270
	{
8277 leency 271
		sprintf(#mem_use, "%i", Process.use_memory/1024);
8353 leency 272
		len = strlen(#mem_use);
273
		strcpy(#mem_use_pretty, "               ");
274
 
275
		for (j=1; j<=len; j++) {
276
			EDI = sizeof(mem_use_pretty)-1-j - calc(j/4);
277
			mem_use_pretty[EDI] = mem_use[len-j];
278
		}
279
 
280
		WriteText(GAP+109, posy+select_list.text_y, 0x90, 0x444444, #mem_use_pretty+16-9);
8277 leency 281
	}
282
 
283
	sprintf(#cpu_use, "%i", Process.use_cpu*100/maxcpu);
284
	if (maxcpu) WriteText(GAP+203 - calc(strlen(#cpu_use)-4*8),
285
		posy+select_list.text_y, 0x90, 0x444444, #cpu_use);
286
}
287
 
288
void SelectList_LineChanged()
289
{
290
	Processes__GetProcessList();
291
	SelectList_Draw();
292
}
293
 
294
void MonitorRd()
295
{
296
	dword rdempty = malloc(1440*1024);
297
	CreateFile(0, 1440*1024, rdempty, "/rd/1/rdempty");
298
	free(rdempty);
8392 leency 299
	rdempty = get_file_size("/rd/1/rdempty") / 1024;
8277 leency 300
	DeleteFile("/rd/1/rdempty");
301
 
302
	sprintf(#param, T_RD_USAGE, rdempty);
303
	DrawIconWithText(RIGHT_X, rd.y - 25, 5, #param);
304
 
305
	rd.draw_progress(rdempty * rd.w / 1440);
306
}
307
 
308
dword GetTmpDiskFreeSpace(int _id)
309
{
310
	DIR_SIZE dir_size;
311
	sprintf(#param, "/tmp%i/1", _id);
312
	dir_size.get(#param);
313
	dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
314
	dir_size.bytes /= 1024*1024; //convert to MiB
315
	return dir_size.bytes;
316
}
317
 
318
void MonitorTmp()
319
{
320
	char text_status[64];
321
	int i, yy=WIN_CONTENT_Y+300;
322
	dword tmp_size[10];
323
	dword free_space;
324
	for (i=0; i<=9; i++)
325
	{
8392 leency 326
		get_file_size( sprintf(#param, "/tmp%i/1", i) );
327
		if (EAX) {
328
			tmp_size[i] =  EAX / 1024 / 1024;
8277 leency 329
			free_space = tmp_size[i] - GetTmpDiskFreeSpace(i);
330
			sprintf(#text_status, T_TMP_USAGE, i, free_space, tmp_size[i]);
331
			tmp.set_size(RIGHT_X, yy, right_w, 23);
332
			tmp.draw_progress(free_space * right_w / tmp_size[i]);
333
			DrawIconWithText(RIGHT_X, tmp.y - 25, 50, #text_status);
334
			yy += 65;
335
		}
336
	}
337
}
338
 
339
void DrawIconWithText(dword _x, _y, _icon, _title)
340
{
9439 leency 341
	if (draw_icon_16w(_x, _y, _icon)) _x += ICONGAP;
342
	WriteTextWithBg(_x, _y + 2, 0xD0, sc.work_text, _title, sc.work);
8277 leency 343
}
344
 
345
dword GetCpuLoad(dword max_h)
346
{
347
	dword idle;
348
	dword CPU_SEC = GetCpuFrequency() >> 20 + 1;
349
	dword IDLE_SEC = GetCpuIdleCount() >> 20 * max_h;
350
 
351
	EAX = IDLE_SEC;
352
	EBX = CPU_SEC;
353
	$cdq
354
	$div ebx
355
	idle = EAX;
356
 
357
	return max_h - idle;
358
}
359
 
360
int pos=0;
361
void MonitorCpu()
362
{
363
	static dword cpu_stack[1980*3];
364
	int i;
365
	if (!cpu.w) return;
366
 
367
	cpu_stack[pos] = GetCpuLoad(cpu.h);
368
	if (cpu_stack[pos]<=2) || (cpu_stack[pos]>cpu.h) cpu_stack[pos]=2;
369
 
370
	sprintf(#param, T_CPU_LOAD, cpu_stack[pos]);
371
	DrawIconWithText(RIGHT_X, cpu.y - 25, 48, #param);
372
 
8381 leency 373
	#define LINEW 8
374
	for (i=0; i
375
		DrawBar(i+cpu.x, cpu.y, LINEW, cpu.h-cpu_stack[i], PROGRESS_BG);
376
		DrawBar(i+cpu.x, cpu.h-cpu_stack[i]+cpu.y, LINEW, cpu_stack[i], 0xDFA13B);
377
		//DrawBar(i+LINEW+cpu.x, cpu.y, 1, cpu.h, PROGRESS_BG);
8277 leency 378
	}
379
 
380
	pos++;
381
	if (pos>=right_w) {
382
		pos = right_w-1;
383
		for (i=0; i
384
			cpu_stack[i] = cpu_stack[i+1];
385
		}
386
	}
387
}
388
 
389
void MonitorRam()
390
{
391
	ram.draw_progress(GetFreeRAM()*ram.w/GetTotalRAM());
392
	sprintf(#param, T_RAM_USAGE, GetFreeRAM()/1024, GetTotalRAM()/1024);
393
	DrawIconWithText(RIGHT_X, ram.y - 25, 51, #param);
394
}
395
 
9584 leency 396
void EventShowTinfo()
397
{
398
	RunProgram("/sys/tinfo", itoa(GetProcessSlot(current_process_id)));
399
}
8277 leency 400
 
9584 leency 401
void ReadIni()
402
{
9597 leency 403
	Form.left    = ini.GetInt("x", screen.w/2 - 350);
9584 leency 404
	Form.top     = ini.GetInt("y", 100);
405
	Form.width   = ini.GetInt("w", 700);
406
	Form.height  = ini.GetInt("h", 490);
407
	show_sensors = ini.GetInt("show_sensors", true);
408
	show_system.checked = ini.GetInt("show_system", false);
409
}
410
 
411
void EventExit()
412
{
413
	ini.SetInt("x", Form.left);
414
	ini.SetInt("y", Form.top);
415
	ini.SetInt("w", Form.width);
416
	ini.SetInt("h", Form.height);
417
	ini.SetInt("show_sensors", show_sensors);
418
	ini.SetInt("show_system", show_system.checked);
419
	ExitProcess();
420
}