Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * System Monitor
  3.  * version 0.86
  4.  * Author: Leency
  5. */
  6.  
  7. #define MEMSIZE 4096*10
  8.  
  9. #include "../lib/io.h"
  10. #include "../lib/gui.h"
  11. #include "../lib/fs.h"
  12.  
  13. #include "../lib/obj/libio.h"
  14. #include "../lib/obj/libimg.h"
  15. #include "../lib/obj/libini.h"
  16.  
  17. //===================================================//
  18. //                                                   //
  19. //                      SENSOR                       //
  20. //                                                   //
  21. //===================================================//
  22.  
  23. #define MIN_PB_BLOCK_W 19
  24. #define LOAD_BG 0xFFFfff
  25. #define LOAD_ACTIVE 0x6C81DC
  26. #define LOAD_BG_TEXT 0x696969
  27.  
  28. struct sensor {
  29.         int x,y,w,h;
  30.         void set_size();
  31.         void draw_wrapper();
  32.         void draw_progress();
  33. };
  34.  
  35. void sensor::set_size(dword _x, _y, _w, _h)
  36. {
  37.         x=_x+2;
  38.         y=_y;
  39.         w=_w;
  40.         h=_h;
  41.         draw_wrapper();
  42. }
  43.  
  44. void sensor::draw_wrapper()
  45. {
  46.         DrawRectangle(x-1, y-1, w+1, h+1, system.color.work_graph);
  47.         DrawRectangle3D(x-2, y-2, w+3, h+3, system.color.work_dark, system.color.work_light);
  48. }
  49.  
  50. void sensor::draw_progress(dword progress_w, active_value, bg_value, mesure)
  51. {
  52.         if (progress_w < MIN_PB_BLOCK_W) progress_w = MIN_PB_BLOCK_W;
  53.         if (progress_w > w-MIN_PB_BLOCK_W) progress_w = w-MIN_PB_BLOCK_W;
  54.  
  55.         DrawBar(x, y, w-progress_w, h, LOAD_ACTIVE);
  56.         sprintf(#param, "%i%s", active_value, mesure);
  57.         WriteText(w-progress_w- calc(strlen(#param)*8) /2 + x, h/2-7+y, 0x90, LOAD_BG, #param);
  58.  
  59.         DrawBar(x+w-progress_w, y, progress_w, h, LOAD_BG);
  60.         sprintf(#param, "%i%s", bg_value, mesure);
  61.         WriteText(-progress_w - calc(strlen(#param)*8)/2 + w+x, h/2-7+y, 0x90, LOAD_BG_TEXT, #param);
  62. }
  63.  
  64. //===================================================//
  65. //                                                   //
  66. //                       DATA                        //
  67. //                                                   //
  68. //===================================================//
  69.  
  70. #define CPU_STACK 440
  71. dword cpu_stack[CPU_STACK];
  72.  
  73. sensor cpu;
  74. sensor ram;
  75. sensor rd;
  76. sensor tmp[10];
  77.  
  78. dword tmp_size[10];
  79.  
  80. //===================================================//
  81. //                                                   //
  82. //                       CODE                        //
  83. //                                                   //
  84. //===================================================//
  85.  
  86. void main()
  87. {
  88.         proc_info Form;
  89.         dword cpu_frequency = GetCpuFrequency()/1000;
  90.         int id;
  91.  
  92.         incn y;
  93.  
  94.         load_dll(libio, #libio_init,1);
  95.         load_dll(libimg, #libimg_init,1);
  96.         load_dll(libini, #lib_init,1);
  97.  
  98.         GetTmpDiskSizesFromIni();
  99.        
  100.         loop()
  101.         {
  102.                 WaitEventTimeout(25);
  103.                 switch(EAX & 0xFF)
  104.                 {
  105.                         case evButton:
  106.                                 if (GetButtonID()) ExitProcess();
  107.                                 break;
  108.                  
  109.                         case evKey:
  110.                                 GetKeys();
  111.                                 if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  112.                                 break;
  113.                          
  114.                         case evReDraw:
  115.                                 #define LEFT 25
  116.                                 #define ICONGAP 45
  117.                                 system.color.get();
  118.                                 DefineAndDrawWindow(150, 100, CPU_STACK+LEFT+LEFT+4+9, 480 + skin_height + 4, 0x34, system.color.work, "System Monitor",0);
  119.                                 GetProcessInfo(#Form, SelfInfo);
  120.  
  121.                                 y.n = 0;
  122.                                 if (cpu_frequency < 1000) sprintf(#param, "CPU frequency: %i Hz", cpu_frequency);
  123.                                 else sprintf(#param, "CPU frequency: %i MHz", cpu_frequency/1000);
  124.                                 DrawBlockHeader(LEFT, y.inc(20), 37, "CPU load", #param);
  125.                                 cpu.set_size(LEFT, y.inc(45), CPU_STACK, 100);
  126.  
  127.                                 sprintf(#param, "Total RAM: %i MiB", GetTotalRAM()/1024);
  128.                                 DrawBlockHeader(LEFT, y.inc(cpu.h + 25), 36, "RAM usage", #param);
  129.                                 ram.set_size(LEFT, y.inc(45), CPU_STACK, 23);
  130.  
  131.                                 DrawBlockHeader(LEFT, y.inc(ram.h + 25), 3, "System RAM Disk usage", "Fixed size: 1.44 MiB");
  132.                                 rd.set_size(LEFT, y.inc(45), CPU_STACK, 23);
  133.  
  134.                                 sprintf(#param, "TMP Disk 0 size: %i MiB", tmp_size[0]);
  135.                                 DrawBlockHeader(LEFT, y.inc(rd.h + 25), 50, "Virtual drive usage", #param);
  136.                                 tmp[0].set_size(LEFT, y.inc(45), CPU_STACK, 23);
  137.  
  138.                         default:
  139.                                 MonitorCpu();
  140.  
  141.                                 ram.draw_progress(
  142.                                         GetFreeRAM()*ram.w/GetTotalRAM(),
  143.                                         GetTotalRAM()-GetFreeRAM()/1024,
  144.                                         GetFreeRAM()/1024,
  145.                                         "M"
  146.                                         );
  147.  
  148.                                 dir_size.get("/rd/1");
  149.                                
  150.                                 dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
  151.                                 dir_size.bytes /= 1024; //convert to KiB
  152.                                 dir_size.bytes = 1440 - dir_size.bytes;
  153.                                 rd.draw_progress(
  154.                                         dir_size.bytes*rd.w/1440,
  155.                                         1440 - dir_size.bytes,
  156.                                         dir_size.bytes,
  157.                                         "K"
  158.                                         );
  159.  
  160.                                 if (tmp_size[0]) {
  161.                                         dir_size.get("/tmp0/1");
  162.                                         dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
  163.                                         dir_size.bytes /= 1024*1024; //convert to MiB
  164.                                         dir_size.bytes= tmp_size[0] - dir_size.bytes;
  165.                                         tmp[0].draw_progress(
  166.                                                 dir_size.bytes*tmp[0].w/tmp_size[0],
  167.                                                 tmp_size[0] - dir_size.bytes,
  168.                                                 dir_size.bytes,
  169.                                                 "M"
  170.                                                 );                                     
  171.                                 }
  172.                 }
  173.         }
  174. }
  175.  
  176. void DrawBlockHeader(dword _x, _y, _icon, _title, _subtitle)
  177. {
  178.         WriteTextB(_x+ICONGAP, _y, 0x90, system.color.work_text, _title);
  179.         DrawIcon32(_x, _y, system.color.work, _icon);
  180.         WriteText(_x+ICONGAP, _y+20, 0x90, system.color.work_text, _subtitle); 
  181. }
  182.  
  183. dword GetCpuLoad(dword max_h)
  184. {
  185.         dword idle;
  186.         dword CPU_SEC = GetCpuFrequency() >> 20 + 1;
  187.         dword IDLE_SEC = GetCpuIdleCount() >> 20 * max_h;
  188.  
  189.         EAX = IDLE_SEC;
  190.         EBX = CPU_SEC;
  191.         $cdq
  192.         $div ebx
  193.         idle = EAX;
  194.  
  195.         return max_h - idle;
  196. }
  197.  
  198. _ini ini = { "/sys/settings/system.ini", "DiskSizes" };
  199. void GetTmpDiskSizesFromIni()
  200. {
  201.         char i, key[2];
  202.         key[1]=0;
  203.         for (i=0; i<=9; i++)
  204.         {
  205.                 key[0]=i+'0';
  206.                 tmp_size[i] = ini.GetInt(#key, 0) / 1024 / 1024;
  207.         }
  208. }
  209.  
  210. //===================================================//
  211. //                                                   //
  212. //                     MONITORS                      //
  213. //                                                   //
  214. //===================================================//
  215.  
  216. int pos=0;
  217. void MonitorCpu()
  218. {
  219.         int i;
  220.         if (!cpu.w) return;
  221.  
  222.         cpu_stack[pos] = GetCpuLoad(cpu.h);
  223.         if (cpu_stack[pos]<=2) || (cpu_stack[pos]>cpu.h) cpu_stack[pos]=2;
  224.        
  225.         DrawBar(cpu.x+cpu.w-30, cpu.y-25, 30, 20, system.color.work);
  226.         sprintf(#param, "%i%%", cpu_stack[pos]);
  227.         WriteText(cpu.x+cpu.w-calc(strlen(#param)*8), cpu.y-25, 0x90, system.color.work_text, #param);
  228.  
  229.         for (i=0; i<CPU_STACK; i+=2) {
  230.                 DrawBar(i+cpu.x, cpu.y, 1, cpu.h-cpu_stack[i], LOAD_BG);
  231.                 DrawBar(i+cpu.x, cpu.h-cpu_stack[i]+cpu.y, 1, cpu_stack[i], LOAD_ACTIVE);
  232.  
  233.                 DrawBar(i+1+cpu.x, cpu.y, 1, cpu.h, LOAD_BG);
  234.         }
  235.  
  236.         pos++;
  237.         if (pos>=CPU_STACK) {
  238.                 pos = CPU_STACK-1;
  239.                 for (i=0; i<pos; i++) {
  240.                         cpu_stack[i] = cpu_stack[i+1];
  241.                 }
  242.         }
  243. }
  244.