Subversion Repositories Kolibri OS

Rev

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

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