Subversion Repositories Kolibri OS

Rev

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