Subversion Repositories Kolibri OS

Rev

Rev 7909 | Rev 8281 | 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/libini.h"
  27. #include "../lib/obj/box_lib.h"
  28.  
  29. #include "../lib/patterns/select_list.h"
  30. #include "../lib/patterns/restart_process.h"
  31.  
  32. //===================================================//
  33. //                                                   //
  34. //                      CONST                        //
  35. //                                                   //
  36. //===================================================//
  37.  
  38. #define GAP 16   //Window padding
  39. #define WIN_CONTENT_X GAP
  40. #define WIN_CONTENT_Y GAP+15
  41. #define PROCESS_LIST_W 260
  42. #define RIGHT_X PROCESS_LIST_W + GAP + GAP + 22
  43. #define ICONGAP 26
  44. #define BOTPANEL_H 36
  45.  
  46. #ifdef LANG_RUS
  47.         #define T_CPU_AND_RAM    "à®æ¥áá®à ¨ Ž‡“"
  48.         #define T_DRIVES         "„¨áª¨"
  49.         #define T_PROCESSES      "à®æ¥ááë"
  50.         #define T_APP_TITLE      "‘¨á⥬­ë© ¬®­¨â®à"
  51.         #define T_SHOW_SYSTEM    "‘¨á⥬­ë¥"
  52.         #define T_DETAILS        "®¤à®¡­¥¥"
  53.         #define T_PROC_KILL      "‘­ïâì § ¤ çã"
  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 Œ¡"
  60. #else
  61.         #define T_CPU_AND_RAM    "CPU & RAM"
  62.         #define T_DRIVES         "Drives"
  63.         #define T_PROCESSES      "Processes"
  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"
  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. //===================================================//
  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.  
  101. int right_w;
  102.  
  103. //===================================================//
  104. //                                                   //
  105. //                       CODE                        //
  106. //                                                   //
  107. //===================================================//
  108.  
  109. void load_lib()
  110. {
  111.         load_dll(libio, #libio_init,1);
  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);
  123.         loop() switch(@WaitEventTimeout(120))
  124.         {
  125.                 case evMouse:
  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;
  148.                 case evReDraw:
  149.                         sc.get();
  150.                         DefineAndDrawWindow(screen.width/2 - 350, 100, 700, 490, 0x33, sc.work, T_APP_TITLE,0);
  151.                         GetProcessInfo(#Form, SelfInfo);
  152.                         if (Form.status_window>2) 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();
  181.                         MonitorCpu();
  182.                         MonitorRam();
  183.                         MonitorRd();
  184.                         MonitorTmp();
  185.         }
  186. }
  187.  
  188. void EventKillCurrentProcess()
  189. {
  190.         KillProcess(current_process_id);
  191.         pause(10);
  192.         SelectList_LineChanged();
  193. }
  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<MAX_PROCESS_COUNT; i++)
  202.         {
  203.                 GetProcessInfo(#Process, i);
  204.                 if (Process.name)
  205.                 {
  206.                         for (j=0; j<11; j++) if (Process.name[j]!=' ') {
  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;
  216.                         }
  217.                 }
  218.         }
  219. }
  220.  
  221. void SelectList_DrawLine(dword i)
  222. {
  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.  
  248.         sprintf(#cpu_use, "%i", Process.use_cpu*100/maxcpu);
  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();
  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);
  269.         DrawIconWithText(RIGHT_X, rd.y - 25, 5, #param);
  270.  
  271.         rd.draw_progress(rdempty * rd.w / 1440);       
  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.                 }
  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;
  328. void MonitorCpu()
  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<right_w; i+=2) {
  341.                 DrawBar(i+cpu.x, cpu.y, 1, cpu.h-cpu_stack[i], PROGRESS_BG);
  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.  
  347.         pos++;
  348.         if (pos>=right_w) {
  349.                 pos = right_w-1;
  350.                 for (i=0; i<pos; i++) {
  351.                         cpu_stack[i] = cpu_stack[i+1];
  352.                 }
  353.         }
  354. }
  355.  
  356. void MonitorRam()
  357. {
  358.         ram.draw_progress(GetFreeRAM()*ram.w/GetTotalRAM());
  359.         sprintf(#param, T_RAM_USAGE, GetFreeRAM()/1024, GetTotalRAM()/1024);
  360.         DrawIconWithText(RIGHT_X, ram.y - 25, 51, #param);
  361. }
  362.  
  363.  
  364.