Subversion Repositories Kolibri OS

Rev

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