Subversion Repositories Kolibri OS

Rev

Rev 9602 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * System Monitor
  3.  * version 1.41
  4.  * Author: Leency
  5. */
  6.  
  7. #define MEMSIZE 1024*60
  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. #include "../lib/obj/libini.h"
  22.  
  23. #include "../lib/patterns/select_list.h"
  24. #include "../lib/patterns/restart_process.h"
  25.  
  26. //===================================================//
  27. //                                                   //
  28. //                      CONST                        //
  29. //                                                   //
  30. //===================================================//
  31.  
  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
  37. #define ICONGAP 26
  38. #define BOTPANEL_H 36
  39.  
  40. #ifdef LANG_RUS
  41.         #define T_APP_TITLE      "‘¨á⥬­ë© ¬®­¨â®à"
  42.         #define T_SHOW_SYSTEM    "‘¨á⥬­ë¥"
  43.         #define T_DETAILS        "®¤à®¡­¥¥"
  44.         #define T_PROC_KILL      "‘­ïâì § ¤ çã"
  45.         #define T_PROC_INFO      "ˆ­ä®"
  46.         #define T_PROC_HEADER    "à®æ¥áá        Ž‡“ Š¡    – %"
  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 Œ¡"
  51. #else
  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"
  57.         #define T_PROC_HEADER    "Process        RAM KB   CPU %"
  58.         #define T_CPU_LOAD       "CPU load %i%%   "
  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"
  62. #endif
  63.  
  64. enum {
  65.         BTN_SHOW_SYSTEM_PROCESSES=200,
  66.         BTN_PROC_KILL,
  67.         BTN_PROC_INFO,
  68.         BTN_MENU,
  69.         BTN_SHOWHIDE_SENSORS
  70. };
  71.  
  72. //===================================================//
  73. //                                                   //
  74. //                       VARS                        //
  75. //                                                   //
  76. //===================================================//
  77.  
  78. int current_process_id = 0;
  79. int proc_list[MAX_PROCESS_COUNT];
  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.  
  92. bool show_sensors;
  93.  
  94. _ini ini = { "/sys/settings/app.ini", "Sysmon" };
  95.  
  96. //===================================================//
  97. //                                                   //
  98. //                       CODE                        //
  99. //                                                   //
  100. //===================================================//
  101.  
  102. void main()
  103. {
  104.         int btn;
  105.         load_dll(boxlib, #box_lib_init,0);
  106.         load_dll(libini, #lib_init,1);
  107.         ReadIni();
  108.         @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  109.         loop() switch(@WaitEventTimeout(50))
  110.         {
  111.                 case evMouse:
  112.                         SelectList_ProcessMouse();
  113.                         if (mouse.up) && (mouse.pkm) || (mouse.mkm) EventShowTinfo();
  114.                         break;
  115.                 case evKey:
  116.                         GetKeys();
  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.                         }
  138.                         break;
  139.                 case evButton:
  140.                         btn = @GetButtonID();
  141.                         if (1==btn) EventExit();
  142.  
  143.                         if (show_system.click(btn)) {
  144.                                 SelectList_LineChanged();
  145.                         }
  146.                         if (BTN_PROC_KILL == btn) {
  147.                                 EventKillCurrentProcess();
  148.                         }
  149.                         if (BTN_PROC_INFO == btn) {
  150.                                 EventShowTinfo();
  151.                         }
  152.                         if (BTN_SHOWHIDE_SENSORS == btn) {
  153.                                 show_sensors ^= 1;
  154.                                 GOTO _DRAW_WINDOW;
  155.                         }
  156.                         break;
  157.                 case evReDraw:
  158.                         sc.get();
  159.                         DefineAndDrawWindow(Form.left, Form.top, Form.width, Form.height, 0x33, sc.work, T_APP_TITLE,0);
  160.                         _DRAW_WINDOW:
  161.                         GetProcessInfo(#Form, SelfInfo);
  162.                         if (Form.status_window&ROLLED_UP) break;
  163.                         if (Form.height < 420) { MoveSize(OLD,OLD,OLD,420); break; }
  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.                         }
  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.  
  173.                         DefineButton(RIGHT_X-38,WIN_CONTENT_Y-25,18,18,BTN_SHOWHIDE_SENSORS,sc.button);
  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);
  176.                         EDX = "<\0>";
  177.                         EDX += show_sensors * 2;
  178.                         WriteText(RIGHT_X-38+5,WIN_CONTENT_Y-25+2,0x90,sc.button_text, EDX);
  179.                         //EBX += 5 << 16;
  180.                         //$int 64
  181.  
  182.                         //bool burger_active = false;
  183.                         //if (menu_id == OPEN_FILE) burger_active = true;
  184.                         //DrawTopPanelButton(BTN_MENU, Form.cwidth-GAP-3, GAP, -1, burger_active);
  185.  
  186.                         SelectList_Init(GAP, WIN_CONTENT_Y, PROCESS_LIST_W,
  187.                                 Form.cheight-BOTPANEL_H-WIN_CONTENT_Y);
  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,
  193.                                 110,23,BTN_PROC_KILL,0xF38181, 0xFFFfff, T_PROC_KILL);
  194.                         DrawCaptButton(PROCESS_LIST_W+GAP-165+18, select_list.y+select_list.h+5,
  195.                                 46,23,BTN_PROC_INFO,sc.button, sc.button_text, T_PROC_INFO);
  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");
  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.                         }
  204.                 default:
  205.                         SelectList_LineChanged();
  206.                         if (show_sensors) {
  207.                                 MonitorCpu();
  208.                                 MonitorRam();
  209.                                 MonitorRd();
  210.                                 MonitorTmp();                          
  211.                         }
  212.         }
  213. }
  214.  
  215. void EventKillCurrentProcess()
  216. {
  217.         KillProcess(current_process_id);
  218.         pause(10);
  219.         SelectList_LineChanged();
  220. }
  221.  
  222. void Processes__GetProcessList()
  223. {
  224.         int i, j;
  225.         proc_info Process;
  226.  
  227.         select_list.count=0;
  228.         for (i=0; i<MAX_PROCESS_COUNT; 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)
  249. {
  250.         int posy, j, len;
  251.         char cpu_use[16], mem_use[16], mem_use_pretty[16];
  252.         dword bg_color;
  253.         proc_info Process;
  254.         static unsigned maxcpu;
  255.         if (!maxcpu) maxcpu = GetCpuFrequency();
  256.  
  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);
  266.  
  267.         WriteText(GAP+5, posy+select_list.text_y, 0x90, 0, #Process.name);
  268.  
  269.         if (Process.use_memory < 3670016000)
  270.         {
  271.                 sprintf(#mem_use, "%i", Process.use_memory/1024);
  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);
  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);
  299.         rdempty = get_file_size("/rd/1/rdempty") / 1024;
  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.sizelo += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
  314.         dir_size.sizelo /= 1024*1024; //convert to MiB
  315.         return dir_size.sizelo;
  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.         {
  326.                 get_file_size( sprintf(#param, "/tmp%i/1", i) );
  327.                 if (EAX) {
  328.                         tmp_size[i] =  EAX / 1024 / 1024;
  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. {
  341.         if (draw_icon_16w(_x, _y, _icon)) _x += ICONGAP;
  342.         WriteTextWithBg(_x, _y + 2, 0xD0, sc.work_text, _title, sc.work);
  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.        
  373.         #define LINEW 8
  374.         for (i=0; i<right_w; i+=LINEW) {
  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);
  378.         }
  379.  
  380.         pos++;
  381.         if (pos>=right_w) {
  382.                 pos = right_w-1;
  383.                 for (i=0; i<pos; 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.  
  396. void EventShowTinfo()
  397. {
  398.         RunProgram("/sys/tinfo", itoa(GetProcessSlot(current_process_id)));
  399. }
  400.  
  401. void ReadIni()
  402. {
  403.         Form.left    = ini.GetInt("x", screen.w/2 - 350);
  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. }
  421.