home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / memsrc.zip / MEMWATCH / main.c < prev    next >
C/C++ Source or Header  |  1999-07-02  |  31KB  |  938 lines

  1. //
  2. //Free memory including virtual memory is
  3. //[TotalAvailMem] - Free space on drive - SwapPathMinFree
  4. //
  5. //Total memory
  6. //[TotPhysMem] + SwapFileSize
  7. //
  8. //----------------------------------------------------
  9. // Version  Who   When        What
  10. // 1.0.00   JUL   09 jan 1998 Initial version
  11. // 1.1.00   JUL   24 apr 1998 Added file
  12. // 1.2.00   JUL   19 mar 1999 
  13. //----------------------------------------------------
  14.  
  15. #define INCL_WIN
  16. #define INCL_GPI
  17. //#define INCL_DOSMISC
  18. //#define INCL_DOSMEMMGR
  19. #define INCL_DOS
  20.  
  21. //#include "e:\watcom\h\os21x\bsedos.h"
  22. #include <os2.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include "main.h"
  27. #include "..\\common\\about.h"
  28.  
  29. #define FONT_SIZE 15
  30. #define INFO_HEIGHT 15*5
  31.  
  32. MRESULT EXPENTRY ClientWndProc (HWND, ULONG, MPARAM, MPARAM);
  33. ULONG GetFileSize (char *pszFile);
  34. ULONG getFreeMemory (BOOL);
  35. ULONG getUsedMemory (BOOL);
  36. ULONG getMaxMemory (void);
  37. ULONG queryDiskSpace (ULONG ulDriveNumber);
  38. void hideTitleBar (void);
  39. void displayTitleBar (void);
  40.  
  41.  
  42. void typeInfo (void);
  43. void insertData (void);
  44. void init ();
  45. void drawGraph (HWND hWnd, HPS hps);
  46. void clearBackGround (HPS hps, long x, long y, long cx, long cy, ULONG color);
  47. void drawInfo (HWND hWnd, HPS hps);
  48.  
  49. HAB   hab;
  50. HWND  hWndFrame,
  51.       hWndClient,
  52.       hWndMenu;
  53. char  szTitle[64];
  54.  
  55. #define MAX_MEM_SLOT 125
  56. ULONG gulBuffPointer;
  57. ULONG gulVirtualMemBuff[MAX_MEM_SLOT];
  58. ULONG gulPhysicMemBuff[MAX_MEM_SLOT];
  59. ULONG gulPhysMem;                //RAM installed in the PC
  60. ULONG gulSwapFileMin;            //Setting in Config.sys MinFree parameter
  61. ULONG gulSwapDrive;              //The drive number where the swapper.dat is located
  62. char gszSwapFileName[1024];      //Path to swap file
  63.  
  64. //--- Options ---
  65. void writeIniSettings (HWND);
  66. void readIniSettingsDlg (HWND hWnd);
  67. #define INI_FILE "memwatch.ini"
  68. #define INI_SECTION "options"
  69. BOOL  gbShowGrid;
  70. ULONG gulRefrashRate;
  71. BOOL  gbShowPhysicalMemory;
  72. BOOL  gbShowPhysicalMemoryLimit;
  73. BOOL  gbShowDetail;
  74. BOOL  gbShortText;
  75.  
  76. #define SLOW_RATE   2000UL
  77. #define NORMAL_RATE 1000UL
  78. #define FASTE_RATE   500UL
  79. ULONG gulTimer;
  80. ULONG gulFrameStyle = FCF_TITLEBAR | FCF_SYSMENU | FCF_ICON | FCF_SIZEBORDER |
  81.                       FCF_MINMAX | FCF_MENU | FCF_SHELLPOSITION | FCF_TASKLIST;
  82. CHAR    gszIniFileWithPath[2048];
  83. //*********************************************************************
  84. //*********************************************************************
  85. int main (int argc, char *argv[])
  86. {
  87.    HMQ   hmq;
  88.    QMSG  qmsg;
  89.    char szClientClass[] = "CLIENT";
  90.    LONG  l;
  91.    SWP   swp;
  92.    HINI  hini;
  93.  
  94.    if ((argc >= 2) && (argv[1][1] == 's'))
  95.    {
  96.       init ();
  97.       typeInfo ();
  98.       return (0);
  99.    }
  100.  
  101.    strcpy (gszIniFileWithPath, argv[0]);
  102.    l = strlen (gszIniFileWithPath) - 1;
  103.    while (gszIniFileWithPath[l] != '\\')
  104.       l--;
  105.  
  106.    gszIniFileWithPath[l+1] = 0;
  107.    strcat (gszIniFileWithPath, INI_FILE);
  108.    strlwr (gszIniFileWithPath);
  109.  
  110.    hab = WinInitialize (0);
  111.    hmq = WinCreateMsgQueue (hab, 0);
  112.  
  113.    WinRegisterClass (hab, szClientClass, (PFNWP)ClientWndProc, 0, 0);
  114.    WinLoadString (hab, 0, ID_APPNAME, sizeof(szTitle), szTitle);
  115.  
  116.    hWndFrame = WinCreateStdWindow (HWND_DESKTOP, 0, //WS_VISIBLE,
  117.                   &gulFrameStyle, szClientClass, szTitle, 0, 0,
  118.                   ID_APPNAME, &hWndClient);
  119.  
  120.    hini = PrfOpenProfile (hab, gszIniFileWithPath);
  121.    swp.cx = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"cx", 50);
  122.    swp.cy = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"cy", 50);
  123.    swp.x  = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"x" , 50);
  124.    swp.y  = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"y" , 50);
  125.    PrfCloseProfile (hini);
  126.    WinSetWindowPos (hWndFrame, HWND_TOP, swp.x, swp.y, swp.cx, swp.cy,
  127.                         SWP_SHOW | SWP_MOVE | SWP_SIZE | SWP_ZORDER);
  128.  
  129.    //Is the first param -s?
  130.    //Then user want staus out on stdio, close the app when done
  131.  
  132.    while (WinGetMsg (hab, &qmsg, 0, 0, 0))
  133.       WinDispatchMsg (hab, &qmsg);
  134.  
  135.    WinDestroyWindow (hWndFrame);
  136.    WinDestroyMsgQueue (hmq);
  137.    WinTerminate (hab);
  138.    return (0);
  139. }
  140.  
  141. //*********************************************************************
  142. //*********************************************************************
  143. MRESULT EXPENTRY ClientWndProc (HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  144. {
  145.    HPS      hps;
  146.    BOOL     bHandled = TRUE;
  147.    MRESULT  mReturn = 0;
  148.    RECTL    rclUpdateRegion;
  149.  
  150.    switch (msg)
  151.    {
  152.       case WM_CREATE:
  153.          hWndMenu = WinWindowFromID (WinQueryWindow(hWnd, QW_PARENT), FID_MENU);
  154.          gulTimer = WinStartTimer (hab, hWnd, 0, NORMAL_RATE);
  155.          init ();
  156.          readIniSettingsDlg (hWnd);
  157.       break;
  158.  
  159.       case WM_OPEN:
  160.          //gulRefrashRate = IDM_NORMAL;
  161.          //gbShowPhysicalMemory = TRUE;
  162.          //gbShowPhysicalMemoryLimit = TRUE;
  163.          //gbShowDetail = TRUE;
  164.       break;
  165.  
  166.       case WM_CLOSE:
  167.          WinStopTimer (hab, hWnd, gulTimer);
  168.          bHandled = FALSE;
  169.          writeIniSettings (hWnd);
  170.       break;
  171.       
  172.       case WM_TIMER:
  173.       {
  174.          hps = WinGetPS (hWnd);
  175.          insertData ();
  176.          //drawGraph (hWnd, hps);
  177.          drawGraph (hWndClient, hps);
  178.          WinReleasePS (hps);
  179.       }  
  180.       break;
  181.       
  182.       case WM_PAINT:
  183.          hps = WinBeginPaint (hWnd, 0, 0);
  184.          drawGraph (hWnd, hps);
  185.          WinEndPaint (hps);
  186.       break;
  187.       
  188.       case WM_ERASEBACKGROUND:
  189.       {
  190.          long x, y, cx, cy;
  191.          RECTL *pr;
  192.  
  193.          pr = (PRECTL)mp2;
  194.          x = pr->xLeft;
  195.          y = pr->yBottom;
  196.          cx= pr->xRight - x;
  197.          cy= pr->yTop - y;
  198.          clearBackGround ((HPS)mp1, x, y, cx, cy, CLR_BLACK);
  199.  
  200.          mReturn = MRFROMLONG(1L);
  201.       }
  202.       break;
  203.  
  204.       case WM_BUTTON1DBLCLK:
  205.          displayTitleBar ();
  206.       break;
  207.       
  208.       case WM_COMMAND:
  209.          switch (LOUSHORT(mp1))
  210.          {
  211.             case IDM_ABOUT:
  212.                DisplayAbout (hWnd, szTitle);
  213.             break;
  214.             
  215.             case IDM_EXIT:
  216.                WinPostMsg (hWnd, WM_CLOSE, MPVOID, MPVOID);
  217.             break;
  218.  
  219.             case IDM_GRID:
  220.  
  221.                if (gbShowGrid)   gbShowGrid = FALSE;
  222.                else              gbShowGrid = TRUE;
  223.                WinCheckMenuItem (hWndMenu, IDM_GRID, gbShowGrid);
  224.                WinQueryWindowRect (hWnd, &rclUpdateRegion);
  225.                WinInvalidateRect(hWnd, &rclUpdateRegion, FALSE);
  226.             break;
  227.             
  228.             case IDM_FAST:
  229.             case IDM_NORMAL:
  230.             case IDM_SLOW:
  231.             case IDM_PAUSE:
  232.                
  233.                WinStopTimer (hab, hWnd, gulTimer);
  234.  
  235.                WinCheckMenuItem (hWndMenu, gulRefrashRate, FALSE);
  236.                gulRefrashRate = LOUSHORT(mp1);
  237.                WinCheckMenuItem (hWndMenu, LOUSHORT(mp1), TRUE);
  238.                
  239.                if (LOUSHORT(mp1) == IDM_FAST)
  240.                   gulTimer = WinStartTimer (hab, hWnd, 0, FASTE_RATE);
  241.                else if (LOUSHORT(mp1) == IDM_NORMAL)
  242.                   gulTimer = WinStartTimer (hab, hWnd, 0, NORMAL_RATE);
  243.                else if (LOUSHORT(mp1) == IDM_SLOW)
  244.                   gulTimer = WinStartTimer (hab, hWnd, 0, SLOW_RATE);
  245.                else
  246.                   gulTimer = 0;
  247.                
  248.             break;
  249.  
  250.             case IDM_PHYSICAL_MEM:
  251.                if (gbShowPhysicalMemory)
  252.                   gbShowPhysicalMemory = FALSE;
  253.                else
  254.                   gbShowPhysicalMemory = TRUE;
  255.                   
  256.                WinCheckMenuItem (hWndMenu, IDM_PHYSICAL_MEM, gbShowPhysicalMemory);
  257.                WinQueryWindowRect (hWnd, &rclUpdateRegion);
  258.                WinInvalidateRect(hWnd, &rclUpdateRegion, FALSE);
  259.             break;
  260.             
  261.             case IDM_PHYSICAL_MEM_LIMIT:
  262.                if (gbShowPhysicalMemoryLimit)
  263.                   gbShowPhysicalMemoryLimit = FALSE;
  264.                else
  265.                   gbShowPhysicalMemoryLimit = TRUE;
  266.                   
  267.                WinCheckMenuItem (hWndMenu, IDM_PHYSICAL_MEM_LIMIT, gbShowPhysicalMemoryLimit);
  268.                WinQueryWindowRect (hWnd, &rclUpdateRegion);
  269.                WinInvalidateRect(hWnd, &rclUpdateRegion, FALSE);
  270.             break;
  271.             case IDM_DETAIL:
  272.                if (gbShowDetail)
  273.                   gbShowDetail = FALSE;
  274.                else
  275.                   gbShowDetail = TRUE;
  276.                WinCheckMenuItem (hWndMenu, IDM_DETAIL, gbShowDetail);
  277.                WinQueryWindowRect (hWnd, &rclUpdateRegion);
  278.                WinInvalidateRect(hWnd, &rclUpdateRegion, FALSE);
  279.             break;
  280.             case IDM_SHORTTEXT:
  281.                if (gbShortText)
  282.                   gbShortText = FALSE;
  283.                else
  284.                   gbShortText = TRUE;
  285.                WinCheckMenuItem (hWndMenu, IDM_SHORTTEXT, gbShortText);
  286.                WinQueryWindowRect (hWnd, &rclUpdateRegion);
  287.                WinInvalidateRect(hWnd, &rclUpdateRegion, FALSE);
  288.             break;
  289.             case IDMTITLEBAR:
  290.                hideTitleBar ();
  291.             break;
  292.          }
  293.          
  294.       default:
  295.          bHandled = FALSE;
  296.       break;
  297.    }
  298.  
  299.    if (!bHandled)
  300.       mReturn = WinDefWindowProc (hWnd, msg, mp1, mp2);
  301.  
  302.    return mReturn;      
  303. }
  304.  
  305. //*********************************************************************
  306. //   Fills the buffer with memory amount values
  307. //*********************************************************************
  308. void insertData (void)
  309. {
  310.    gulPhysicMemBuff [gulBuffPointer] = getUsedMemory (TRUE);
  311.    gulVirtualMemBuff[gulBuffPointer] = getUsedMemory (FALSE);
  312.    gulBuffPointer++;
  313.    if (gulBuffPointer == MAX_MEM_SLOT) gulBuffPointer = 0;
  314. }
  315.  
  316.  
  317. //*********************************************************************
  318. //*********************************************************************
  319. void clearBackGround (HPS hps, long x, long y, long cx, long cy, ULONG color)
  320. {
  321.    POINTL   ptlPoint;
  322.    
  323.    //Clear background
  324.    GpiSetColor (hps, color);
  325.    
  326.    ptlPoint.x = x; ptlPoint.y = y;
  327.    GpiMove (hps, &ptlPoint);
  328.    
  329.    ptlPoint.x = cx; ptlPoint.y = cy;
  330.    GpiBox (hps, DRO_OUTLINEFILL, &ptlPoint, 0, 0);
  331. }
  332.  
  333. //*********************************************************************
  334. //   Draws the background grid
  335. //*********************************************************************
  336. void drawGrid (HPS hps, long x, long y, long cx, long cy)
  337. {
  338.    #define  LINE_JUMP 10
  339.    long     lx, ly;
  340.    POINTL    p;
  341.  
  342.    GpiSetColor (hps, CLR_DARKGREEN);
  343.    for (lx=x; lx < x+cx; lx+=LINE_JUMP)
  344.    {
  345.       p.x = lx; p.y = y;
  346.       GpiMove (hps, &p);
  347.       
  348.       p.x = lx; p.y = y+cy;
  349.       GpiLine (hps, &p);
  350.    }
  351.    for (ly=y; ly<y+cy; ly+=LINE_JUMP)
  352.    {
  353.       p.x = x; p.y = ly;
  354.       GpiMove (hps, &p);
  355.       
  356.       p.x = x+cx; p.y = ly;
  357.       GpiLine (hps, &p);
  358.    }
  359. }
  360. //*********************************************************************
  361. //*********************************************************************
  362. long adjustCordinate (ULONG value, ULONG max, ULONG maxValue)
  363. {
  364.    float f;
  365.    
  366.    f  = (float)value / (float)max;
  367.    f *= (float)maxValue;
  368.  
  369.    return (long)f;
  370. }
  371. //*********************************************************************
  372. //   Draws the memory graph
  373. //*********************************************************************
  374. void drawGraph (HWND hWnd, HPS hps)
  375. {
  376.    ULONG    x, ulMaxMem, index;
  377.    POINTL   ptlPoint;
  378.    SWP      swp;
  379.  
  380.    drawInfo (hWnd, hps);
  381.  
  382.    //Get window width and height
  383.    WinQueryWindowPos (hWnd, &swp);
  384.  
  385.    //Make room for detail info
  386.    if (gbShowDetail)
  387.       swp.cy -= INFO_HEIGHT;
  388.       
  389.    if (swp.cy < 0) swp.cy = 0;
  390.  
  391.    clearBackGround (hps, 0, 0, swp.cx, swp.cy, CLR_BLACK);
  392.  
  393.    if (gbShowGrid) drawGrid (hps, 0, 0, swp.cx, swp.cy);
  394.       
  395.    //*** VIRTUAL GRAPH ***
  396.    //Prepare graph drawing
  397.    GpiSetColor (hps, CLR_GREEN);
  398.    ptlPoint.x = 0;
  399.    ptlPoint.y = 0;
  400.    GpiMove (hps, &ptlPoint);
  401.    ulMaxMem = getMaxMemory ();
  402.  
  403.    //Draw the graph
  404.    index = gulBuffPointer;
  405.    for (x = 0; x < MAX_MEM_SLOT; x++)
  406.    {
  407.       ptlPoint.x = adjustCordinate (x, MAX_MEM_SLOT, swp.cx);
  408.       ptlPoint.y = adjustCordinate (gulVirtualMemBuff[index], ulMaxMem, swp.cy);
  409.       if (ptlPoint.y > swp.cy-1) ptlPoint.y= swp.cy -1;
  410.       
  411.       GpiLine (hps, &ptlPoint);
  412.  
  413.       index++;
  414.       if (index == MAX_MEM_SLOT) index = 0;
  415.    }
  416.  
  417.    //*** PHYSICAL GRAPH ***
  418.    if (gbShowPhysicalMemory)
  419.    {
  420.       //Prepare graph drawing
  421.       GpiSetColor (hps, CLR_YELLOW);
  422.       ptlPoint.x = 0;
  423.       ptlPoint.y = 0;
  424.       GpiMove (hps, &ptlPoint);
  425.       ulMaxMem = getMaxMemory ();
  426.  
  427.       //Draw the graph
  428.       index = gulBuffPointer;
  429.       for (x = 0; x < MAX_MEM_SLOT; x++)
  430.       {
  431.          ptlPoint.x = adjustCordinate (x, MAX_MEM_SLOT, swp.cx);
  432.          ptlPoint.y = adjustCordinate (gulPhysicMemBuff[index], ulMaxMem, swp.cy);
  433.          if (ptlPoint.y > swp.cy-1) ptlPoint.y= swp.cy -1;
  434.       
  435.          GpiLine (hps, &ptlPoint);
  436.  
  437.          index++;
  438.          if (index == MAX_MEM_SLOT) index = 0;
  439.       }
  440.    }
  441.  
  442.    //*** Draw physical memory limit ***
  443.    if (gbShowPhysicalMemoryLimit)
  444.    {
  445.       GpiSetColor (hps, CLR_DARKGRAY);
  446.       ptlPoint.x = 0;
  447.       ptlPoint.y = adjustCordinate (gulPhysMem, ulMaxMem, swp.cy);
  448.       GpiMove (hps, &ptlPoint);
  449.       ptlPoint.x = swp.cx;
  450.       GpiLine (hps, &ptlPoint);
  451.    }
  452. }
  453. //*********************************************************************
  454. //*********************************************************************
  455. void typeInfo (void)
  456. {
  457.    printf ("Information produced by Memory Watcher\n");
  458.    printf ("Copyright (C) Jostein Ullestad\n");
  459.    printf ("http:\\www.powerutilities.no\n");
  460.    printf ("\n");
  461.    printf ("Available virtual memory %u KB\n"  , getFreeMemory (FALSE) / 1024);
  462.    printf ("Available physical memory %u KB\n" , getFreeMemory (TRUE) / 1024);
  463.    printf ("Page file size %lu KB\n"           , GetFileSize (gszSwapFileName) / 1024);
  464.    printf ("Available Page file space %lu KB\n", queryDiskSpace (gulSwapDrive) / 1024);
  465.    printf ("Installed memory %lu KB\n"         , gulPhysMem / 1024);
  466. }
  467. //*********************************************************************
  468. //*********************************************************************
  469. void drawInfo (HWND hWnd, HPS hps)
  470. {
  471.    SWP      swp;
  472.    char    szTiles[33];
  473.    RECTL    rectl;
  474.  
  475.    if (gbShowDetail)
  476.    {
  477.       //Get window width and height
  478.       WinQueryWindowPos (hWnd, &swp);
  479.    
  480.       clearBackGround (hps, 0, swp.cy - INFO_HEIGHT, swp.cx, swp.cy, CLR_PALEGRAY);
  481.  
  482.       strcpy (szTiles, "8.Helv"); //""System Proportional");
  483.       WinSetPresParam (hWnd, PP_FONTNAMESIZE, (ULONG)strlen (szTiles)+1, (PVOID)szTiles);
  484.  
  485.       rectl.xLeft  = 0;
  486.       rectl.xRight = swp.cx;
  487.       rectl.yTop   = swp.cy;
  488.       rectl.yBottom= swp.cy - FONT_SIZE;
  489.  
  490.       if (gbShortText)
  491.          sprintf (szTiles, "VM %u KB", getFreeMemory (FALSE) / 1024);
  492.       else
  493.          sprintf (szTiles, "Available virtual memory %u KB", getFreeMemory (FALSE) / 1024);
  494.       WinDrawText (hps, -1, szTiles, &rectl, CLR_BLACK, CLR_DARKGRAY, 0L);
  495.  
  496.       rectl.yTop   = swp.cy - FONT_SIZE;
  497.       rectl.yBottom= swp.cy - FONT_SIZE*2;
  498.       if (gbShortText)
  499.          sprintf (szTiles, "PM %lu KB", getFreeMemory (TRUE) / 1024);
  500.       else
  501.          sprintf (szTiles, "Available physical memory %u KB", getFreeMemory (TRUE) / 1024);
  502.       WinDrawText (hps, -1, szTiles, &rectl, CLR_BLACK, CLR_DARKGRAY, 0L);
  503.  
  504.       rectl.yTop   = swp.cy - FONT_SIZE*2;
  505.       rectl.yBottom= swp.cy - FONT_SIZE*3;
  506.       if (gbShortText)
  507.          sprintf (szTiles, "PF %lu KB", GetFileSize (gszSwapFileName) / 1024);
  508.       else
  509.          sprintf (szTiles, "Page file size %lu KB", GetFileSize (gszSwapFileName) / 1024);
  510.       WinDrawText (hps, -1, szTiles, &rectl, CLR_BLACK, CLR_DARKGRAY, 0L);
  511.  
  512.       rectl.yTop   = swp.cy - FONT_SIZE*3;
  513.       rectl.yBottom= swp.cy - FONT_SIZE*4;
  514.       if (gbShortText)
  515.          sprintf (szTiles, "FS %lu KB", queryDiskSpace (gulSwapDrive) / 1024);
  516.       else
  517.          sprintf (szTiles, "Available Page file space %lu KB", queryDiskSpace (gulSwapDrive) / 1024);
  518.       WinDrawText (hps, -1, szTiles, &rectl, CLR_BLACK, CLR_DARKGRAY, 0L);
  519.  
  520.       rectl.yTop   = swp.cy - FONT_SIZE*4;
  521.       rectl.yBottom= swp.cy - FONT_SIZE*5;
  522.       if (gbShortText)
  523.          sprintf (szTiles, "IM %lu KB", gulPhysMem / 1024);
  524.       else
  525.          sprintf (szTiles, "Installed memory %lu KB", gulPhysMem / 1024);
  526.       WinDrawText (hps, -1, szTiles, &rectl, CLR_BLACK, CLR_DARKGRAY, 0L);
  527.    }  //if (gbShowDetail)
  528. }
  529.  
  530. //*********************************************************************
  531. //Function which returns file size in bytes
  532. //
  533. //   Returns
  534. //   0 : No file, or error
  535. //   x : file size in bytes
  536. //*********************************************************************
  537. ULONG GetFileSize (char *pszFile)
  538. {
  539.    HDIR    fhandle;
  540.    ULONG   count;
  541.    ULONG   fsize;
  542.    USHORT  frc;
  543.    FILEFINDBUF buffer;   ///* file information struct 
  544.  
  545.    count = 1;
  546.    fhandle = 0xFFFF;
  547.    frc = DosFindFirst (pszFile, &fhandle, 0, &buffer, sizeof(buffer), &count, 1L);
  548.    DosFindClose (fhandle);
  549.    if (frc != 0)
  550.       return(0L);
  551.  
  552.    fsize = buffer.cbFileAlloc;
  553.    return ((ULONG)fsize);
  554. }
  555.  
  556. //*********************************************************************
  557. //   Reads a given file and place its contents in the given
  558. //   pre-allocated buffer.
  559. //
  560. //   Returns
  561. //   -1 : Error
  562. //    0 : Ok
  563. //*********************************************************************
  564. long readFile (char *pszFile, char *buffer, ULONG ulSize)
  565. {
  566.    FILE  *fp;
  567.    long  rtc;
  568.  
  569.    rtc = -1;
  570.    fp = fopen (pszFile, "rt");
  571.    if (fp != NULL)
  572.    {
  573.       fread (buffer, ulSize, 1, fp);
  574.       fclose (fp);
  575.       rtc = 0;
  576.    }
  577.       
  578.    return rtc;
  579. }
  580.  
  581. //*********************************************************************
  582. //   Reads the corresponding value in the config.sys file
  583. //   eg.
  584. //   SwapPath=e:\os2\system\swapper.dat
  585. //
  586. //   In
  587. //   file     must point to valid OS/2 config.sys file
  588. //   variable the setting you want to read, eg. swappath
  589. //
  590. //   Out
  591. //   value    the resulting value, eg. E:\OS2\SYSTEM\SWAPPER.DAT
  592. //            in upper case.
  593. //
  594. //   Returns
  595. //   -1 : Error or not found
  596. //    0 : Found, "value" contains a valid value.
  597. //*********************************************************************
  598. long getConfigValue (char *file, char *variable, char *value)
  599. {
  600.    char *buffer;
  601.    ULONG ulSize;
  602.    long  lRtc;
  603.  
  604.    lRtc = -1;
  605.    ulSize = GetFileSize (file);
  606.    if (ulSize > 0)
  607.    {
  608.       buffer = (char *)malloc (ulSize +2);
  609.       if (buffer != NULL)
  610.       {
  611.          if (readFile (file, buffer, ulSize) == 0)
  612.          {
  613.             char *delims = {"\n\r"};
  614.             char *p, *f, *s;
  615.             
  616.             strcat (buffer, "\r");
  617.             strupr (buffer);
  618.             strupr (variable);
  619.             
  620.             p = strtok (buffer, delims);
  621.             while (p != NULL)
  622.             {
  623.                f = strstr (p, variable);
  624.                if (f != NULL)
  625.                {
  626.                   //Trim the string
  627.                   while (*f == ' ' || *f == '\t') f++;
  628.                   //Make sure that the "variable" is the first part of the string
  629.                   s = strstr (f, variable);
  630.                   if (s == f)
  631.                   {
  632.                      p = NULL;
  633.                      s = strstr (f, "=");
  634.                      if (s != NULL)
  635.                      {
  636.                         //Don't include the =
  637.                         strcpy (value, &s[1]);
  638.                         lRtc = 0;
  639.                      }
  640.                   }
  641.                   else
  642.                      p = strtok (NULL, delims);
  643.                }
  644.                else
  645.                   p = strtok (NULL, delims);
  646.             }
  647.          }
  648.          free (buffer);
  649.       }
  650.    }
  651.    return lRtc;
  652. }
  653.  
  654. //*********************************************************************
  655. //Fills the global varible "gszSwapFileName" with the path and name
  656. //of the systems page-file.
  657. //
  658. //Also fills the global variable "gulSwapFileMin" with the value of the
  659. //minimum free space that the drive must have.
  660. //
  661. //gulSwapDrive gets the swapper drive number, A=1, B=2, C=3, ...
  662. //*********************************************************************
  663. void getSwapSettings (ULONG ulBootDrive)
  664. {
  665.    char szFile[2048];
  666.    char variable[30];
  667.    char value[300];
  668.       
  669.    szFile[0] = ulBootDrive + 'A' - 1; //A = 1, B = 2, C = 3, ...
  670.    szFile[1] = 0;
  671.    strcat (szFile, ":\\CONFIG.SYS");
  672.  
  673.    strcpy (variable, "MEMMAN");     //can have one or more of the following settings
  674.                                     //(SWAP, NOSWAP, MOVE, NOMOVE, COMMIT, PROTECT)
  675.    if (getConfigValue (szFile, &variable, &value) == 0)
  676.    {
  677.       //Did the user specify all but NOSWAP ?
  678.       if (strstr (value, "NOSWAP") == NULL)
  679.       {
  680.          //Get the path to the swapper file...
  681.          strcpy (variable, "SWAPPATH");
  682.          if (getConfigValue (szFile, &variable, &value) == 0)
  683.          {
  684.             char *p;
  685.             char *delims = {" "};
  686.             long l;
  687.  
  688.             //Extract the path from the value
  689.             p = strtok (value, delims);
  690.             strcpy (gszSwapFileName, value);
  691.             l = strlen (gszSwapFileName);
  692.             if (gszSwapFileName[l-1] != '\\')
  693.                strcat (gszSwapFileName, "\\");
  694.             strcat (gszSwapFileName, "SWAPPER.DAT");
  695.             gulSwapDrive = gszSwapFileName[0] - 'A' + 1;
  696.  
  697.             //Extract the minimum free value
  698.             p = strtok (NULL, delims);
  699.             gulSwapFileMin = strtoul (p, NULL, 10);
  700.          }
  701.       }
  702.    }
  703. }      
  704.  
  705. //*********************************************************************
  706. //*********************************************************************
  707. void init (void)
  708. {
  709.    ULONG ulValues[1];
  710.    ULONG ulBootDrive;
  711.    ULONG ulCounter;
  712.  
  713.    //Get the RAM amount in the PC
  714.    DosQuerySysInfo (QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &ulValues, sizeof(ulValues));
  715.    gulPhysMem = ulValues[0];
  716.  
  717.    //No swap name means we have no swap file. or no swaping enabled in the config.sys
  718.    strcpy (gszSwapFileName, "");
  719.    gulSwapFileMin = 0;
  720.    //Get the BOOT drive
  721.    DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulValues, sizeof(ulValues));
  722.    ulBootDrive = ulValues[0];
  723.    //It must be an harddisk
  724.    if (ulBootDrive >=3 ) getSwapSettings (ulBootDrive);
  725.    //
  726.    gulBuffPointer = 0;
  727.  
  728.    for (ulCounter = 0; ulCounter<MAX_MEM_SLOT; ulCounter++)
  729.       gulVirtualMemBuff[ulCounter] = 0;
  730.  
  731.    for (ulCounter = 0; ulCounter<MAX_MEM_SLOT; ulCounter++)
  732.       gulPhysicMemBuff[ulCounter] = 0;
  733.  
  734. }
  735.  
  736.  
  737. //*********************************************************************
  738. //   Returns the amount of diskspace on given drive
  739. //*********************************************************************
  740. ULONG queryDiskSpace (ULONG ulDriveNumber)
  741. {
  742.    FSALLOCATE fsAllocate;
  743.    ULONG      ulRtc;
  744.  
  745.    DosError (FERR_DISABLEHARDERR);
  746.    ulRtc = 0; 
  747.    if (!DosQueryFSInfo (ulDriveNumber, FSIL_ALLOC, &fsAllocate, sizeof(FSALLOCATE)))
  748.       ulRtc = fsAllocate.cSectorUnit * fsAllocate.cUnitAvail * fsAllocate.cbSector;
  749.    DosError (FERR_ENABLEHARDERR);
  750.  
  751.    return ulRtc;
  752. }
  753. ///*********************************************************************
  754. //   Returns the amount of free memory in the system
  755. //
  756. //   Arg:
  757. //      TRUE returns the amount of free physic memory (RAM)
  758. //      FALSE returns the amount of free virtual memory
  759. //      
  760. //Free memory including virtual memory is
  761. //[TotalAvailMem] - Free space on drive - SwapPathMinFree
  762. //   
  763. //*********************************************************************
  764. ULONG getFreeMemory (BOOL bPhysicMem)
  765. {
  766.    ULONG ulValues[1];
  767.    ULONG ulDiskSpace;
  768.    ULONG ulRtc;
  769.    ULONG ulA, ulB;
  770.  
  771.    if (bPhysicMem)
  772.    {
  773.       APIRET16 APIENTRY16 Dos16MemAvail ( PULONG pulAvailMem ) ;
  774.       ULONG PhysMemFree ;
  775.       Dos16MemAvail ( &PhysMemFree ) ;
  776.       ulRtc = PhysMemFree;
  777.    }
  778.    else
  779.    {
  780.       if (strcmp (gszSwapFileName, "") == 0)
  781.          ulDiskSpace = 0;
  782.       else
  783.          ulDiskSpace = queryDiskSpace (gulSwapDrive);
  784.  
  785.       //Get the maximum number of bytes an process can allocate
  786.       DosQuerySysInfo (QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &ulValues, sizeof(ulValues));
  787.    
  788.       ulA  = ulDiskSpace;
  789.       ulA -= gulSwapFileMin*1000;
  790.       ulB  = ulValues[0];
  791.  
  792.  
  793.       if (ulB < ulA)
  794.          ulRtc = 0;
  795.       else
  796.          ulRtc = ulB - ulA;
  797.    }
  798.  
  799.    return ulRtc;
  800. }
  801. ///*********************************************************************
  802. //   Returns the amount of free memory in the system
  803. //   
  804. //   Arg:
  805. //      TRUE returns the amount of free physic memory (RAM)
  806. //      FALSE returns the amount of free virtual memory
  807. //
  808. //Free memory including virtual memory is
  809. //[TotalAvailMem] - Free space on drive - SwapPathMinFree
  810. //*********************************************************************
  811. ULONG getUsedMemory (BOOL bPhysicMem)
  812. {
  813.    ULONG ulRtc;
  814.    if (bPhysicMem)
  815.       ulRtc = gulPhysMem - getFreeMemory (TRUE);
  816.    else
  817.       ulRtc = getMaxMemory () - getFreeMemory (FALSE);
  818.    return ulRtc;
  819. }
  820.  
  821.  
  822. //*********************************************************************
  823. //   Returns the amount of total memory in the system
  824. //
  825. //Total memory
  826. //[TotPhysMem] + SwapFileSize
  827. //*********************************************************************
  828. ULONG getMaxMemory (void)
  829. {
  830.    ULONG ulRtc;
  831.    
  832.    if (strcmp (gszSwapFileName, "") == 0)
  833.       ulRtc = gulPhysMem;
  834.    else
  835.       ulRtc = gulPhysMem + GetFileSize (gszSwapFileName);
  836.  
  837.    return ulRtc;
  838. }
  839.  
  840. //****************************************************************
  841. // Writes the options into the ini file
  842. //****************************************************************
  843. void writeIniSettings (HWND hWnd)
  844. {
  845.    HINI  hini;
  846.    char  cBuffer[255];
  847.    SWP   swp;
  848.  
  849.    WinQueryWindowPos (hWndFrame, (PSWP)&swp);
  850.  
  851.    hini = PrfOpenProfile (hab, gszIniFileWithPath);
  852.    sprintf (cBuffer, "%d", swp.cx); PrfWriteProfileString (hini, INI_SECTION, (PSZ)"cx",   (PSZ)cBuffer);
  853.    sprintf (cBuffer, "%d", swp.cy); PrfWriteProfileString (hini, INI_SECTION, (PSZ)"cy",   (PSZ)cBuffer);
  854.    sprintf (cBuffer, "%d", swp.x) ; PrfWriteProfileString (hini, INI_SECTION, (PSZ)"x" ,   (PSZ)cBuffer);
  855.    sprintf (cBuffer, "%d", swp.y) ; PrfWriteProfileString (hini, INI_SECTION, (PSZ)"y" ,   (PSZ)cBuffer);
  856.  
  857.    sprintf (cBuffer, "%d", gbShowGrid);                PrfWriteProfileString (hini, INI_SECTION, (PSZ)"grid",   (PSZ)cBuffer);
  858.    sprintf (cBuffer, "%d", gulRefrashRate);            PrfWriteProfileString (hini, INI_SECTION, (PSZ)"refrate",(PSZ)cBuffer);
  859.    sprintf (cBuffer, "%d", gbShowPhysicalMemory);      PrfWriteProfileString (hini, INI_SECTION, (PSZ)"phymem", (PSZ)cBuffer);
  860.    sprintf (cBuffer, "%d", gbShowPhysicalMemoryLimit); PrfWriteProfileString (hini, INI_SECTION, (PSZ)"phymeml",(PSZ)cBuffer);
  861.    sprintf (cBuffer, "%d", gbShowDetail);              PrfWriteProfileString (hini, INI_SECTION, (PSZ)"detail", (PSZ)cBuffer);
  862.    sprintf (cBuffer, "%d", gbShortText);               PrfWriteProfileString (hini, INI_SECTION, (PSZ)"shortxt",(PSZ)cBuffer);
  863.  
  864.    PrfCloseProfile (hini);
  865. }
  866.  
  867. //****************************************************************
  868. // Read the ini file settings and apply them
  869. //****************************************************************
  870. void readIniSettingsDlg (HWND hWnd)
  871. {
  872.    HINI  hini;
  873.    RECTL rclUpdateRegion;
  874.    LONG  lRate;
  875.  
  876.    hini = PrfOpenProfile (hab, gszIniFileWithPath);
  877.    gbShowGrid                = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"grid"   , 1);
  878.    lRate                     = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"refrate", IDM_NORMAL);
  879.    gbShowPhysicalMemory      = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"phymem" , 1);
  880.    gbShowPhysicalMemoryLimit = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"phymeml", 1);
  881.    gbShowDetail              = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"detail" , 1);
  882.    gbShortText               = PrfQueryProfileInt (hini, INI_SECTION, (PSZ)"shortxt", 0);
  883.  
  884.    //--- Update menu items ---
  885.    // Show grid
  886.    WinCheckMenuItem (hWndMenu, IDM_GRID, gbShowGrid);
  887.    // Show Physical Memory
  888.    WinCheckMenuItem (hWndMenu, IDM_PHYSICAL_MEM, gbShowPhysicalMemory);
  889.    // Show Physical Memory Limit
  890.    WinCheckMenuItem (hWndMenu, IDM_PHYSICAL_MEM_LIMIT, gbShowPhysicalMemoryLimit);
  891.    // Show Detail
  892.    WinCheckMenuItem (hWndMenu, IDM_DETAIL, gbShowDetail);
  893.    // Show Short Description
  894.    WinCheckMenuItem (hWndMenu, IDM_SHORTTEXT, gbShortText);
  895.  
  896.    WinQueryWindowRect (hWnd, &rclUpdateRegion);
  897.    WinInvalidateRect(hWnd, &rclUpdateRegion, FALSE);
  898.    // Refresh rate
  899.    WinCheckMenuItem (hWndMenu, IDM_FAST  , FALSE);
  900.    WinCheckMenuItem (hWndMenu, IDM_NORMAL, FALSE);
  901.    WinCheckMenuItem (hWndMenu, IDM_SLOW  , FALSE);
  902.    WinCheckMenuItem (hWndMenu, IDM_PAUSE , FALSE);
  903.    WinSendMsg (hWnd, WM_COMMAND, (MPARAM)lRate, (MPARAM)0);
  904. }
  905.  
  906. void hideTitleBar (void)
  907. {
  908.    SWP   swp;
  909.  
  910.    WinDestroyWindow (WinWindowFromID (hWndFrame, FID_TITLEBAR));
  911.    WinDestroyWindow (WinWindowFromID (hWndFrame, FID_SYSMENU));
  912.    WinDestroyWindow (WinWindowFromID (hWndFrame, FID_MINMAX));
  913.    WinDestroyWindow (hWndMenu); hWndMenu = NULL;
  914.  
  915.    WinQueryWindowPos (hWndFrame, (PSWP)&swp);
  916.    WinSetWindowPos (hWndFrame, HWND_TOP, swp.x, swp.y, swp.cx, swp.cy -1, SWP_SIZE);
  917.    WinSetWindowPos (hWndFrame, HWND_TOP, swp.x, swp.y, swp.cx, swp.cy   , SWP_SIZE);
  918. }
  919.  
  920. void displayTitleBar (void)
  921. {
  922.    FRAMECDATA pFcdata;
  923.    SWP   swp;
  924.  
  925.    pFcdata.cb = sizeof(FRAMECDATA);
  926.    pFcdata.flCreateFlags = gulFrameStyle | FCF_MENU;
  927.    pFcdata.hmodResources = 0L;
  928.    pFcdata.idResources = ID_APPNAME;
  929.  
  930.    WinCreateFrameControls (hWndFrame, &pFcdata, szTitle);
  931.    hWndMenu = WinWindowFromID (hWndFrame, FID_MENU);
  932.  
  933.    WinQueryWindowPos (hWndFrame, (PSWP)&swp);
  934.    WinSetWindowPos (hWndFrame, HWND_TOP, swp.x, swp.y, swp.cx, swp.cy -1, SWP_SIZE);
  935.    WinSetWindowPos (hWndFrame, HWND_TOP, swp.x, swp.y, swp.cx, swp.cy   , SWP_SIZE);
  936. }
  937.  
  938.