Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * System Monitor
  3.  * version 1.36
  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/gui.h"
  20. #include "../lib/fs.h"
  21. #include "../lib/list_box.h"
  22.  
  23. #include "../lib/obj/libimg.h"
  24. #include "../lib/obj/box_lib.h"
  25.  
  26. #include "../lib/patterns/select_list.h"
  27. #include "../lib/patterns/restart_process.h"
  28.  
  29. //===================================================//
  30. //                                                   //
  31. //                      CONST                        //
  32. //                                                   //
  33. //===================================================//
  34.  
  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
  40. #define ICONGAP 26
  41. #define BOTPANEL_H 36
  42.  
  43. #ifdef LANG_RUS
  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 Œ¡"
  54. #else
  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"
  65. #endif
  66.  
  67. enum {
  68.         BTN_ID_SHOW_SYSTEM_PROCESSES=200,
  69.         BTN_ID_PROC_KILL,
  70.         BTN_ID_PROC_INFO,
  71.         BTN_ID_MENU
  72. };
  73.  
  74. //===================================================//
  75. //                                                   //
  76. //                       VARS                        //
  77. //                                                   //
  78. //===================================================//
  79.  
  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()
  101. {
  102.         load_dll(libimg, #libimg_init,1);
  103.         load_dll(boxlib, #box_lib_init,0);
  104. }
  105.  
  106. void main()
  107. {
  108.         int btn;
  109.         //dword cpu_frequency = GetCpuFrequency()/1000;
  110.         load_lib();
  111.         @SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP);
  112.         @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  113.         loop() switch(@WaitEventTimeout(50))
  114.         {
  115.                 case evMouse:
  116.                         SelectList_ProcessMouse();
  117.                         break;
  118.                 case evKey:
  119.                         GetKeys();
  120.                         if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  121.                         if (key_scancode == SCAN_CODE_DEL) EventKillCurrentProcess();
  122.                         if (select_list.ProcessKey(key_scancode)) SelectList_LineChanged();
  123.                         break;
  124.                 case evButton:
  125.                         btn = @GetButtonID();
  126.                         if (1==btn) ExitProcess();
  127.  
  128.                         if (show_system.click(btn)) {
  129.                                 SelectList_LineChanged();
  130.                         }
  131.                         if (BTN_ID_PROC_KILL == btn) {
  132.                                 EventKillCurrentProcess();
  133.                         }
  134.                         if (BTN_ID_PROC_INFO == btn) {
  135.                                 RunProgram("/sys/tinfo", itoa(GetProcessSlot(current_process_id)));
  136.                         }
  137.                         break;
  138.                 case evReDraw:
  139.                         sc.get();
  140.                         DefineAndDrawWindow(screen.width/2 - 350, 100, 700, 490, 0x33, sc.work, T_APP_TITLE,0);
  141.                         GetProcessInfo(#Form, SelfInfo);
  142.                         if (Form.status_window>2) break;
  143.                         if (Form.width  < RIGHT_X+370) { MoveSize(OLD,OLD,RIGHT_X+370,OLD); break; }
  144.                         if (Form.height < 420) { MoveSize(OLD,OLD,OLD,420); break; }
  145.                         right_w = Form.cwidth - RIGHT_X - GAP;
  146.                         right_w &= ~1; // make sure the number is even
  147.                         WriteText(GAP+5, WIN_CONTENT_Y-20, 0x90, sc.work_text, T_PROC_HEADER);
  148.  
  149.                         //bool burger_active = false;
  150.                         //if (menu_id == OPEN_FILE) burger_active = true;
  151.                         //DrawTopPanelButton(BTN_ID_MENU, Form.cwidth-GAP-3, GAP, -1, burger_active);
  152.  
  153.                         SelectList_Init(GAP, WIN_CONTENT_Y, PROCESS_LIST_W,
  154.                                 Form.cheight-BOTPANEL_H-WIN_CONTENT_Y, false);
  155.                         SelectList_DrawBorder();
  156.  
  157.                         DrawBar(select_list.x-2, select_list.y+select_list.h+2,
  158.                                 select_list.w+scroll1.size_x+4, BOTPANEL_H, sc.work);
  159.                         DrawCaptButton(PROCESS_LIST_W+GAP-110+18, select_list.y+select_list.h+5,
  160.                                 110,23,BTN_ID_PROC_KILL,0xF38181, 0xFFFfff, T_PROC_KILL);
  161.                         DrawCaptButton(PROCESS_LIST_W+GAP-165+18, select_list.y+select_list.h+5,
  162.                                 46,23,BTN_ID_PROC_INFO,sc.button, sc.button_text, T_PROC_INFO);
  163.                         show_system.draw(GAP-1, select_list.y+select_list.h+10);
  164.  
  165.                         //WriteText(RIGHT_X, WIN_CONTENT_Y+25, 0x90, sc.work, "Update period: 5 seconds");
  166.                         cpu.set_size(RIGHT_X, WIN_CONTENT_Y+25, right_w, 100);
  167.                         ram.set_size(RIGHT_X, WIN_CONTENT_Y+170, right_w, 23);
  168.                         rd.set_size(RIGHT_X, WIN_CONTENT_Y+240, right_w, 23);
  169.                 default:
  170.                         MonitorCpu();
  171.                         MonitorRam();
  172.                         SelectList_LineChanged();
  173.                         MonitorRd();
  174.                         MonitorTmp();
  175.         }
  176. }
  177.  
  178. void EventKillCurrentProcess()
  179. {
  180.         KillProcess(current_process_id);
  181.         pause(10);
  182.         SelectList_LineChanged();
  183. }
  184.  
  185. void Processes__GetProcessList()
  186. {
  187.         int i, j;
  188.         proc_info Process;
  189.  
  190.         select_list.count=0;
  191.         for (i=0; i<MAX_PROCESS_COUNT; i++)
  192.         {
  193.                 GetProcessInfo(#Process, i);
  194.                 if (Process.name)
  195.                 {
  196.                         for (j=0; j<11; j++) if (Process.name[j]!=' ') {
  197.                                 if (show_system.checked==false) {
  198.                                         //do not show system process
  199.                                         if (Process.name[0]=='@') break;
  200.                                         if (!strcmp(#Process.name, "IDLE")) break;
  201.                                         if (!strcmp(#Process.name, "OS")) break;
  202.                                 }
  203.                                 proc_list[select_list.count] = i;
  204.                                 select_list.count++;
  205.                                 break;
  206.                         }
  207.                 }
  208.         }
  209. }
  210.  
  211. void SelectList_DrawLine(dword i)
  212. {
  213.         int posy, j, len;
  214.         char cpu_use[16], mem_use[16], mem_use_pretty[16];
  215.         dword bg_color;
  216.         proc_info Process;
  217.         static unsigned maxcpu;
  218.         if (!maxcpu) maxcpu = GetCpuFrequency();
  219.  
  220.         GetProcessInfo(#Process, proc_list[i+select_list.first]);
  221.        
  222.         posy = i *select_list.item_h + select_list.y;
  223.         if (i % 2) bg_color = 0xFFFfff; else bg_color = 0xF0F0F0;
  224.         if (i+select_list.first == select_list.cur_y) {
  225.                 current_process_id = Process.ID;
  226.                 bg_color = 0x67CCEB;
  227.         }
  228.         DrawBar(select_list.x, posy, select_list.w, select_list.item_h, bg_color);
  229.  
  230.         WriteText(GAP+5, posy+select_list.text_y, 0x90, 0, #Process.name);
  231.  
  232.         if (Process.use_memory < 3670016000)
  233.         {
  234.                 sprintf(#mem_use, "%i", Process.use_memory/1024);
  235.                 len = strlen(#mem_use);
  236.                 strcpy(#mem_use_pretty, "               ");
  237.  
  238.                 for (j=1; j<=len; j++) {
  239.                         EDI = sizeof(mem_use_pretty)-1-j - calc(j/4);
  240.                         mem_use_pretty[EDI] = mem_use[len-j];
  241.                 }
  242.  
  243.                 WriteText(GAP+109, posy+select_list.text_y, 0x90, 0x444444, #mem_use_pretty+16-9);
  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.         rdempty = get_file_size("/rd/1/rdempty") / 1024;
  263.         DeleteFile("/rd/1/rdempty");
  264.  
  265.         sprintf(#param, T_RD_USAGE, rdempty);
  266.         DrawIconWithText(RIGHT_X, rd.y - 25, 5, #param);
  267.  
  268.         rd.draw_progress(rdempty * rd.w / 1440);       
  269. }
  270.  
  271. dword GetTmpDiskFreeSpace(int _id)
  272. {
  273.         DIR_SIZE dir_size;
  274.         sprintf(#param, "/tmp%i/1", _id);
  275.         dir_size.get(#param);
  276.         dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
  277.         dir_size.bytes /= 1024*1024; //convert to MiB
  278.         return dir_size.bytes; 
  279. }
  280.  
  281. void MonitorTmp()
  282. {
  283.         char text_status[64];
  284.         int i, yy=WIN_CONTENT_Y+300;
  285.         dword tmp_size[10];
  286.         dword free_space;
  287.         for (i=0; i<=9; i++)
  288.         {
  289.                 get_file_size( sprintf(#param, "/tmp%i/1", i) );
  290.                 if (EAX) {
  291.                         tmp_size[i] =  EAX / 1024 / 1024;
  292.                         free_space = tmp_size[i] - GetTmpDiskFreeSpace(i);
  293.                         sprintf(#text_status, T_TMP_USAGE, i, free_space, tmp_size[i]);
  294.                         tmp.set_size(RIGHT_X, yy, right_w, 23);
  295.                         tmp.draw_progress(free_space * right_w / tmp_size[i]);
  296.                         DrawIconWithText(RIGHT_X, tmp.y - 25, 50, #text_status);
  297.                         yy += 65;
  298.                 }
  299.         }
  300. }
  301.  
  302. void DrawIconWithText(dword _x, _y, _icon, _title)
  303. {
  304.         DrawIcon16(_x, _y, sc.work, _icon);
  305.         WriteTextWithBg(_x+ICONGAP, _y, 0xD0, sc.work_text, _title, sc.work);
  306. }
  307.  
  308. dword GetCpuLoad(dword max_h)
  309. {
  310.         dword idle;
  311.         dword CPU_SEC = GetCpuFrequency() >> 20 + 1;
  312.         dword IDLE_SEC = GetCpuIdleCount() >> 20 * max_h;
  313.  
  314.         EAX = IDLE_SEC;
  315.         EBX = CPU_SEC;
  316.         $cdq
  317.         $div ebx
  318.         idle = EAX;
  319.  
  320.         return max_h - idle;
  321. }
  322.  
  323. int pos=0;
  324. void MonitorCpu()
  325. {
  326.         static dword cpu_stack[1980*3];
  327.         int i;
  328.         if (!cpu.w) return;
  329.  
  330.         cpu_stack[pos] = GetCpuLoad(cpu.h);
  331.         if (cpu_stack[pos]<=2) || (cpu_stack[pos]>cpu.h) cpu_stack[pos]=2;
  332.        
  333.         sprintf(#param, T_CPU_LOAD, cpu_stack[pos]);
  334.         DrawIconWithText(RIGHT_X, cpu.y - 25, 48, #param);
  335.        
  336.         #define LINEW 8
  337.         for (i=0; i<right_w; i+=LINEW) {
  338.                 DrawBar(i+cpu.x, cpu.y, LINEW, cpu.h-cpu_stack[i], PROGRESS_BG);
  339.                 DrawBar(i+cpu.x, cpu.h-cpu_stack[i]+cpu.y, LINEW, cpu_stack[i], 0xDFA13B);
  340.                 //DrawBar(i+LINEW+cpu.x, cpu.y, 1, cpu.h, PROGRESS_BG);
  341.         }
  342.  
  343.         pos++;
  344.         if (pos>=right_w) {
  345.                 pos = right_w-1;
  346.                 for (i=0; i<pos; i++) {
  347.                         cpu_stack[i] = cpu_stack[i+1];
  348.                 }
  349.         }
  350. }
  351.  
  352. void MonitorRam()
  353. {
  354.         ram.draw_progress(GetFreeRAM()*ram.w/GetTotalRAM());
  355.         sprintf(#param, T_RAM_USAGE, GetFreeRAM()/1024, GetTotalRAM()/1024);
  356.         DrawIconWithText(RIGHT_X, ram.y - 25, 51, #param);
  357. }
  358.  
  359.  
  360.