Subversion Repositories Kolibri OS

Rev

Rev 7909 | Rev 8281 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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