Subversion Repositories Kolibri OS

Rev

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