home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / performance / memory / tinymeter / source / tinymeter_main / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-20  |  7.5 KB  |  396 lines

  1. #include <libraries/commodities.h>
  2. #include <proto/SysInfo.h>
  3. #include <libraries/SysInfo.h>
  4. #include <libraries/ScreenNotify.h>
  5. #include <exec/memory.h>
  6. #include <dos/dos.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <intuition/cghooks.h>
  11. #include <intuition/icclass.h>
  12. #include <intuition/classes.h>
  13. #include <utility/tagitem.h>
  14. #include "gaugeclass.h"
  15. #include "tinymeter.h"
  16.  
  17. /* default shit */
  18. #define Version "4.10"
  19.  
  20. #ifndef MAKE_ID
  21. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  22. #endif                                                                       -
  23.  
  24. int             __nocommandline =1;
  25.  
  26. struct Library          *RetinaBase;
  27. struct Library          *ShutdownBase;
  28. struct Library          *TimerBase;
  29. struct Library          *SysInfoBase;
  30. struct Library          *VMMBase;
  31. struct Library          *ScreenNotifyBase;
  32.  
  33. extern struct WBStartup *_WBBenchMsg;
  34. extern struct Library   *CxBase;
  35. extern struct Library   *DataTypesBase;
  36.  
  37. struct EasyStruct easymsg =
  38. {
  39.     sizeof(struct EasyStruct),
  40.     0,
  41.     "TinyMeter " Version " information",
  42.     0L,
  43.     "OK"
  44. };
  45.  
  46. show(char *Message)
  47. {
  48.     easymsg.es_TextFormat=Message;
  49.     EasyRequest(NULL, &easymsg, NULL);
  50. }
  51.  
  52. int getNum(struct tm_gau_set *list)
  53. {
  54.     int num=1;
  55.     while(list=list->next)num++;
  56.     return(num);
  57. }
  58. struct tm_sys_set default_set=
  59. {
  60.     "TM40",
  61.     1,
  62.  
  63.     10,
  64.     10,
  65.     450,
  66.     2,
  67.  
  68.     "Workbench",
  69.  
  70.     "topaz.font",
  71.     8,
  72.  
  73.     bg_none,
  74.     "",
  75.     FALSE,
  76.     -3,
  77.     FALSE,0x55555555,0x55555555,0x55555555,
  78.  
  79.     5,
  80.     1,
  81.  
  82.     0,
  83.     TRUE,
  84.  
  85.     win_normal,
  86.     0,
  87.     4,
  88.     4,
  89.     8,
  90.     8,
  91.  
  92.     bd_standard,
  93.     FALSE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  94.     FALSE,0x0,0x0,0x0,
  95.  
  96.     idle_own
  97. };
  98.  
  99. struct tm_gau_set g3=
  100. {
  101.     typ_idle,
  102.     typ_histmeter,
  103.  
  104.     200,
  105.  
  106.     "topaz.font",
  107.     8,
  108.  
  109.     "ChipMem",
  110.     "CPU %p%% free",
  111.     "",
  112.     ind_right,
  113.     TRUE,
  114.     TRUE,
  115.     TRUE,
  116.     TRUE,
  117.     FALSE,
  118.     FALSE,
  119.     TRUE,
  120.  
  121.     TRUE,2,0,0,
  122.     TRUE,2,0,0,
  123.     FALSE,0,0xFFFFFFFF,0,
  124.     FALSE,0,0,0xFFFFFFFF,
  125.     FALSE,0xFFFFFFFF,0,0,
  126.     TRUE,2,0,0,
  127.     TRUE,1,0,0,
  128.     TRUE,1,0,0,
  129.  
  130.     NULL
  131. };
  132.  
  133. struct tm_gau_set g2=
  134. {
  135.     typ_fast,
  136.     typ_gauge,
  137.  
  138.     20,
  139.  
  140.     "topaz.font",
  141.     8,
  142.  
  143.     "FastMem",
  144.     "%td Bytes free",
  145.     "",
  146.     ind_right,
  147.     TRUE,
  148.     TRUE,
  149.     TRUE,
  150.     TRUE,
  151.     FALSE,
  152.     FALSE,
  153.     FALSE,
  154.  
  155.     TRUE,2,0,0,
  156.     TRUE,2,0,0,
  157.     FALSE,0,0xFFFFFFFF,0,
  158.     FALSE,0,0,0xFFFFFFFF,
  159.     FALSE,0xFFFFFFFF,0,0,
  160.     TRUE,2,0,0,
  161.     TRUE,1,0,0,
  162.     TRUE,0,0,0,
  163.  
  164.     &g3,
  165. };
  166.  
  167.  
  168. struct tm_gau_set g1=
  169. {
  170.     typ_chip,
  171.     typ_gauge,
  172.  
  173.     20,
  174.  
  175.     "topaz.font",
  176.     8,
  177.  
  178.     "ChipMem",
  179.     "%td Bytes free",
  180.     "",
  181.     ind_right,
  182.     TRUE,
  183.     TRUE,
  184.     TRUE,
  185.     TRUE,
  186.     FALSE,
  187.     FALSE,
  188.     FALSE,
  189.  
  190.     TRUE,2,0,0,
  191.     TRUE,2,0,0,
  192.     FALSE,0,0xFFFFFFFF,0,
  193.     FALSE,0,0,0xFFFFFFFF,
  194.     FALSE,0xFFFFFFFF,0,0,
  195.     TRUE,2,0,0,
  196.     TRUE,1,0,0,
  197.     TRUE,0,0,0,
  198.  
  199.     &g2
  200. };
  201.  
  202. useDefault(struct tm_sys_set *set,struct tm_data *data)
  203. {
  204.     struct tm_gau_set   *many,
  205.             *act,
  206.             *old;
  207.  
  208.     CopyMem((char *)&default_set,set,sizeof(struct tm_sys_set));
  209.  
  210.     if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L))
  211.     {
  212.     many=&g1; data->list=act; CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  213.     while(many=many->next)
  214.     {
  215.         old=act;
  216.         if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L))
  217.         {
  218.         old->next=act;
  219.         CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  220.         }
  221.         else {act->next=0L;return;}
  222.     }
  223.     act->next=0L;
  224.     }
  225.     else data->list=0L;
  226. }
  227.  
  228. struct NewBroker newbroker =
  229. {
  230.     NB_VERSION,
  231.     "TinyMeter V" Version,
  232.     "TinyMeter V" Version " © by Tinic Urou",
  233.     "Shows system states",
  234.     NBU_UNIQUE,
  235.     0,
  236.     -3,
  237.     0,
  238.     0
  239. };
  240.  
  241. char *version="$VER: TinyMeter V" Version;
  242.  
  243. int main(void)
  244. {
  245.  
  246.     struct tm_sys_set   *set;
  247.     struct tm_data      *data;
  248.  
  249.     Class               *gclass;    
  250.     ULONG                cxsigflag;
  251.     UBYTE               *my_file;
  252.     CxMsg               *msg;
  253.     CxObj               *broker;
  254.     struct MsgPort      *broker_mp;
  255.  
  256.     if(set=(struct tm_sys_set *)AllocVec(sizeof(struct tm_sys_set),MEMF_CLEAR))
  257.     {
  258.     if(data=(struct tm_data *)AllocVec(sizeof(struct tm_data),MEMF_CLEAR))
  259.     {
  260.         if(gclass=(Class *)initGaugeGadgetClass())
  261.         {
  262.         char  *my_file;
  263.         struct tm_gau_set *many,*act;
  264.         if(my_file=(UBYTE *)Open("ENV:TinyMeter",MODE_OLDFILE))
  265.         {
  266.             if(!Read(my_file,set,(ULONG)sizeof(struct tm_sys_set)))
  267.             {
  268.             show("Loading of preferences failed! Using defaults.");
  269.             useDefault(set,data);
  270.             }
  271.             else
  272.             {
  273.             if((ULONG)*((ULONG *)&set->set_header[0])==0x544D3430)
  274.             {
  275.                 act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),NULL);
  276.                 data->list=act; many=act;
  277.                 while(Read((char *)my_file,(char *)act,(ULONG)sizeof(struct tm_gau_set)))
  278.                 {
  279.                 many=act;
  280.                 act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),NULL);
  281.                 many->next=act;
  282.                 }
  283.                 many->next=NULL; FreeVec(act); act=NULL;
  284.             }
  285.             else
  286.             {
  287.                 show("Not a TinyMeter4.0 preferences file! Using defaults.");
  288.                 useDefault(set,data);
  289.             }
  290.             }
  291.             Close(my_file);
  292.         }
  293.         else
  294.         {
  295.             show("No preferences file available! Using defaults.");
  296.             useDefault(set,data);
  297.         }
  298.         data->num_of_gaug=getNum(data->list);
  299.  
  300.         SetTaskPri(FindTask(0L),set->pri);
  301.  
  302.         Delay(set->start_wait*50L);
  303.  
  304.         if(broker_mp = (struct MsgPort *)CreatePort(0,0))
  305.         {
  306.             newbroker.nb_Port = (struct MsgPort *)broker_mp;
  307.  
  308.             if (broker = (CxObj *)CxBroker(&newbroker, NULL))
  309.             {
  310.             cxsigflag = 1L << broker_mp->mp_SigBit;
  311.             ActivateCxObj(broker, 1L);
  312.  
  313.             RetinaBase=(struct Library *)OpenLibrary("retina.library",0L);
  314.  
  315.             switch (set->Executive)
  316.             {
  317.                 case    idle_none:
  318.                     data->executive=idle_none;
  319.                     break;
  320.                 case    idle_executive:
  321.                     if(SysInfoBase = (struct Library *)OpenLibrary(SYSINFONAME, SYSINFOVERSION))
  322.                     {
  323.                         if(data->si =(struct SysInfo *)InitSysInfo())
  324.                         data->executive=idle_executive;
  325.                         else
  326.                         data->executive=idle_none;
  327.                     }
  328.                     else data->executive=idle_none;
  329.                     break;
  330.                 case    idle_own:
  331.                     init_idle();
  332.                     data->executive=idle_own;
  333.                     break;
  334.             }
  335.  
  336.             data->scrnot=FALSE; if(set->start_usescreennotify)
  337.                 if(ScreenNotifyBase=(struct Library *)OpenLibrary("screennotify.library",SCREENNOTIFY_VERSION))
  338.                 data->scrnot=TRUE;
  339.  
  340.             /* Go to main handler */
  341.  
  342.             if(openWindow(set,data))
  343.             {
  344.                 drawBackground(set,data);
  345.                 allocGadgets(set,data,gclass);
  346.                 set=(struct tm_sys_set *)handler(set,data,broker_mp,broker,cxsigflag,gclass);
  347.                 closeWindow(set,data);
  348.                 removeGadgets(set,data);
  349.  
  350.             }
  351.             else show("Could not open window!");
  352.  
  353.             switch (data->executive)
  354.             {
  355.                 case    idle_executive:
  356.                     FreeSysInfo(data->si);
  357.                     CloseLibrary(SysInfoBase);
  358.                     break;
  359.  
  360.                 case    idle_own:
  361.                     free_idle();
  362.                     break;
  363.  
  364.             }
  365.  
  366.             if(data->scrnot)CloseLibrary(ScreenNotifyBase);
  367.             if(RetinaBase)  CloseLibrary(RetinaBase);
  368.  
  369.             /* Clear any message before killing the cx */
  370.             while(msg = (CxMsg *)GetMsg(broker_mp)) ReplyMsg((struct Message *)msg);
  371.             DeleteCxObj(broker);
  372.             }
  373.             else show("CxBroker failed.");
  374.             DeletePort(broker_mp);
  375.  
  376.             many=data->list;
  377.             do
  378.             {
  379.             act=many->next;
  380.             FreeVec(many);
  381.             }
  382.             while(many=act);
  383.         }
  384.         else show("CreatePort failed!");
  385.         freeGaugeGadgetClass(gclass);
  386.         }
  387.         else show("BOOPSI class allocation failed!");
  388.         FreeVec(set);
  389.     }
  390.     else show("Not enough memory!");
  391.     FreeVec(data);
  392.     }
  393.     else show("No enough memory!");
  394.  
  395. }
  396.