Subversion Repositories Kolibri OS

Rev

Rev 9436 | Rev 9581 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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