home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma76.lha / MAShare76 / proged.lha / proged22 / installproged / sources / ped_apidocks / PED_Docks.c next >
Encoding:
C/C++ Source or Header  |  1996-06-03  |  8.7 KB  |  414 lines

  1.  
  2. /******
  3.  * Various include
  4.  ******/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <libraries/toolmanager.h>
  10. #include <proto/toolmanager.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13. #include <proto/intuition.h>
  14. #include <ProgED:sources/include/Ped.h>
  15.  
  16. #define NUMDOCKS    20
  17.  
  18.  
  19.  
  20. /******
  21.  * Global structures & variables
  22.  ******/
  23.  
  24. struct tmTool
  25. {
  26.     APTR    exec,
  27.         icon,
  28.         sound;
  29. };
  30.  
  31. struct APIClient    MyClient=
  32. {
  33.     NULL,            /* This field will be filled later... */
  34.     NOTIFY_ON_SHOW_HIDE,    /* Let me know when you close or open your screen */
  35.     "DOCK Client",        /* Client's name (not used, till now) */
  36.     NULL            /* Set this to NULL! Thanks */
  37. };
  38.  
  39. struct MsgPort        *MyPort,
  40.             *PEDPort;
  41. struct APIMessage     msg;
  42. ULONG             NumDock=0;
  43. UWORD             PunDock[NUMDOCKS],
  44.              DockX[NUMDOCKS],
  45.              DockY[NUMDOCKS];
  46.  
  47.  
  48.  
  49. /*****
  50.  *
  51.  * FUNCTION:    void SendMessageToPED(struct APIMessage    *msg)
  52.  *
  53.  * AIM:        This function sends a message to ProgED.
  54.  *
  55.  * RESULT: -
  56.  *
  57.  ****/
  58.  
  59. void SendMessageToPED(struct APIMessage    *msg)
  60. {
  61.     msg->am_Message.mn_Node.ln_Succ=NULL;
  62.     msg->am_Message.mn_Node.ln_Pred=NULL;
  63.     msg->am_Message.mn_Node.ln_Type=NT_MESSAGE;
  64.     msg->am_Message.mn_Node.ln_Pri=0;
  65.     msg->am_Message.mn_Node.ln_Name=NULL;
  66.     msg->am_Message.mn_ReplyPort=MyPort;
  67.     msg->am_Message.mn_Length=sizeof(struct APIMessage);
  68.  
  69.     PutMsg(PEDPort,(struct Message *)msg);
  70.     WaitPort(MyPort);
  71.     while(GetMsg(MyPort));
  72. }
  73.  
  74.  
  75.  
  76. /*****
  77.  *
  78.  * FUNCTION:    struct TagItem *SetupDock(char *config,void *handle)
  79.  *
  80.  * AIM:        Create the dock using the name of the config-file
  81.  *        and the toolmanager's handle.
  82.  *
  83.  * RESULT:    TagItem[] used to create the dock.
  84.  *
  85.  ****/
  86.  
  87. struct TagItem *SetupDock(char *config,void *handle)
  88. {
  89.     BPTR         file;
  90.     ULONG         num=0,
  91.              oldnum=0,
  92.              totnum=0,
  93.              n=0,
  94.              orien=0,
  95.              col=1,
  96.              i;
  97.     struct TagItem    *dock;
  98.     struct RDArgs    *RDArgs;
  99.     char         buffer[300],
  100.              execName[10],
  101.              imageName[10];
  102.  
  103.  
  104.  
  105.     if (file=Open(config,MODE_OLDFILE))
  106.     {
  107.         while(FGets(file,buffer,sizeof(buffer)-1))
  108.         {
  109.             if (!strncmp(buffer,"ADD",3))        totnum++;
  110.             else if (!strncmp(buffer,"DOCK",4))    totnum+=10;
  111.         }
  112.  
  113.         Close(file);
  114.  
  115.         if (dock=(struct TagItem *)calloc(totnum*sizeof(struct TagItem),1))
  116.         {
  117.             if (file=Open(config,MODE_OLDFILE))
  118.             {
  119.                 if (RDArgs=AllocDosObject(DOS_RDARGS,NULL))
  120.                 {
  121.                     while(FGets(file,buffer,sizeof(buffer)-1))
  122.                     {
  123.                         ULONG    args[10]={0,0,0,0,0,0,0,0,0,0};
  124.  
  125.  
  126.  
  127.                         RDArgs->RDA_Source.CS_Buffer=buffer+4;
  128.                         RDArgs->RDA_Source.CS_Length=strlen(buffer+4);
  129.                         RDArgs->RDA_Source.CS_CurChr=0;
  130.                         RDArgs->RDA_DAList=NULL;
  131.                         RDArgs->RDA_Buffer=NULL;
  132.  
  133.                         if (!strncmp(buffer,"ADD",3))
  134.                         {
  135.                             memset(args,0,sizeof(args));
  136.                             if (ReadArgs("COMMAND/K/A,AREXX/S,SHELL/S,DIR/K,OUTPUT/K,IMAGE/K/A",args,RDArgs))
  137.                             {
  138.                                 struct TagItem imageTags[]={
  139.                                     TMOP_File,    NULL,
  140.                                     TMOP_Data,    NULL,
  141.                                 TAG_END};
  142.  
  143.                                 struct TagItem execTags[]={
  144.                                     TMOP_Command,        NULL,
  145.                                     TMOP_ExecType,        NULL,
  146.                                     TMOP_Stack,        10000,
  147.                                     TMOP_CurrentDir,    NULL,
  148.                                     TMOP_Output,        NULL,
  149.                                 TAG_END};
  150.  
  151.                                 imageTags[0].ti_Data=(ULONG)strdup((char *)args[5]);
  152.  
  153.                                 execTags[0].ti_Data=(ULONG)strdup((char *)args[0]);
  154.                                 execTags[1].ti_Data=args[1] ? TMET_ARexx : TMET_CLI;
  155.                                 execTags[3].ti_Data=args[3] ? (ULONG)strdup((char *)args[3]) : NULL;
  156.                                 execTags[4].ti_Data=args[4] ? (ULONG)strdup((char *)args[4]) : NULL;
  157.  
  158.                                 sprintf(execName,"PED_E%d",num);
  159.                                 sprintf(imageName,"PED_I%d",num++);
  160.  
  161.                                 CreateTMObjectTagList(handle,strdup(execName),TMOBJTYPE_EXEC,execTags);
  162.                                 CreateTMObjectTagList(handle,strdup(imageName),TMOBJTYPE_IMAGE,imageTags);
  163.  
  164.                                 FreeArgs(RDArgs);
  165.                             }
  166.                         }
  167.                         else if (!strncmp(buffer,"DOCK",4))
  168.                         {
  169.                             memset(args,0,sizeof(args));
  170.                             if (ReadArgs("X/N,Y/N,HORIZONTAL/S,COLUMNS/N",args,RDArgs))
  171.                             {
  172.                                 if (args[0])    DockX[NumDock]=*((ULONG *)args[0]);
  173.                                 if (args[1])    DockY[NumDock]=*((ULONG *)args[1]);
  174.                                 if (args[2])    orien=1;
  175.                                 if (args[3])    col=*((ULONG *)args[3]);
  176.  
  177.                                         if (num-oldnum>0)
  178.                                 {
  179.                                     PunDock[NumDock++]=n;
  180.  
  181.                                     dock[n].ti_Tag=TMOP_PubScreen;
  182.                                     dock[n++].ti_Data=NULL;
  183.  
  184.                                     dock[n].ti_Tag=TMOP_LeftEdge;
  185.                                     dock[n++].ti_Data=NULL;
  186.  
  187.                                     dock[n].ti_Tag=TMOP_TopEdge;
  188.                                     dock[n++].ti_Data=NULL;
  189.  
  190.                                     dock[n].ti_Tag=TMOP_Activated;
  191.                                     dock[n++].ti_Data=TRUE;
  192.  
  193.                                     dock[n].ti_Tag=TMOP_Centered;
  194.                                     dock[n++].ti_Data=FALSE;
  195.  
  196.                                     dock[n].ti_Tag=TMOP_Columns;
  197.                                     dock[n++].ti_Data=col;
  198.  
  199.                                     dock[n].ti_Tag=TMOP_Vertical;
  200.                                     dock[n++].ti_Data=orien;
  201.  
  202.                                     dock[n].ti_Tag=TMOP_Text;
  203.                                     dock[n++].ti_Data=FALSE;
  204.  
  205.                                     for(i=oldnum;i<num;i++)
  206.                                     {
  207.                                         struct tmTool *tool;
  208.  
  209.                                         if (tool=(struct tmTool *)calloc(sizeof(struct tmTool),1))
  210.                                         {
  211.                                             if ((tool->exec=calloc(12,1))&&(tool->icon=calloc(12,1)))
  212.                                             {
  213.                                                 sprintf((char *)tool->exec,"PED_E%d",i);
  214.                                                 sprintf((char *)tool->icon,"PED_I%d",i);
  215.  
  216.                                                 dock[n].ti_Tag=TMOP_Tool;
  217.                                                 dock[n++].ti_Data=(ULONG)tool;
  218.                                             }
  219.                                         }
  220.                                     }
  221.  
  222.                                     dock[n++].ti_Tag=TAG_DONE;
  223.  
  224.                                     oldnum=num;
  225.                                 }
  226.  
  227.                                 FreeArgs(RDArgs);
  228.                             }
  229.                         }
  230.                     }
  231.  
  232.                     FreeDosObject(DOS_RDARGS,RDArgs);
  233.                 }
  234.  
  235.                 Close(file);
  236.             }
  237.         }
  238.     }
  239.  
  240.     return(dock);
  241. }
  242.  
  243.  
  244.  
  245. /*****
  246.  *
  247.  * FUNCTION:    void HandleMsg(void *handle,struct TagItem *dock)
  248.  *
  249.  * AIM:        Messages' handler.
  250.  *
  251.  * RESULT:    -
  252.  *
  253.  ****/
  254.  
  255. void HandleMsg(void *handle,struct TagItem *dock)
  256. {
  257.     UBYTE             finito;
  258.     ULONG             i;
  259.     struct APIMessage    *mess;
  260.     struct Screen        *screen;
  261.     char            *pubscrname,
  262.                  tmp[25];
  263.  
  264.  
  265.  
  266.     /***** Get the ProgED screen address *****/
  267.     msg.am_MsgType=PED_API_GET_SCREEN_ADDRESS;
  268.     SendMessageToPED(&msg);
  269.     screen=(struct Screen *)msg.am_RC;
  270.  
  271.     /***** Get the ProgED public screen name *****/
  272.     msg.am_MsgType=PED_API_GET_PUBSCRNAME;
  273.     SendMessageToPED(&msg);
  274.     pubscrname=(char *)msg.am_RC;
  275.  
  276.     /***** If the screen is opened then open the dock, too *****/
  277.     if (screen)
  278.     {
  279.         for(i=0;i<NumDock;i++)
  280.         {
  281.             dock[PunDock[i]].ti_Data=(ULONG)pubscrname;
  282.             dock[PunDock[i]+1].ti_Data=(DockX[i] == ~0) ? screen->Width : DockX[i];
  283.             dock[PunDock[i]+2].ti_Data=(DockY[i] == ~0) ? screen->BarHeight+1 : DockY[i];
  284.  
  285.             sprintf(tmp,"PED_DOCK%d",i);
  286.             CreateTMObjectTagList(handle,strdup(tmp),TMOBJTYPE_DOCK,&dock[PunDock[i]]);
  287.         }
  288.     }
  289.  
  290.     /***** Main loop *****/
  291.     finito=FALSE;
  292.     while(!finito)
  293.     {
  294.         WaitPort(MyPort);
  295.         while(mess=(struct APIMessage *)GetMsg(MyPort))
  296.         {
  297.             switch(mess->am_MsgType)
  298.             {
  299.                 /***** ProgED is quitting!
  300.                     Let's quit, too :-( *****/
  301.                 case    PED_API_QUIT:
  302.                     finito=TRUE;
  303.                 break;
  304.  
  305.                 /***** ProgED is closing its screen.
  306.                     Let's close our window,too *****/
  307.                 case    PED_API_HIDE:
  308.                     for(i=0;i<NumDock;i++)
  309.                     {
  310.                         sprintf(tmp,"PED_DOCK%d",i);
  311.                         DeleteTMObject(handle,tmp);
  312.                     }
  313.                 break;
  314.  
  315.                 /***** ProgED is opening its screen
  316.                     Let's open our window,too *****/
  317.                 case    PED_API_SHOW:
  318.                     if (screen=(struct Screen *)mess->am_MsgArg[0])
  319.                     {
  320.                         for(i=0;i<NumDock;i++)
  321.                         {
  322.                             dock[PunDock[i]].ti_Data=(ULONG)mess->am_MsgArg[1];
  323.                             dock[PunDock[i]+1].ti_Data=(DockX[i] == ~0) ? screen->Width : DockX[i];
  324.                             dock[PunDock[i]+2].ti_Data=(DockY[i] == ~0) ? screen->BarHeight+1 : DockY[i];
  325.  
  326.                             sprintf(tmp,"PED_DOCK%d",i);
  327.                             CreateTMObjectTagList(handle,strdup(tmp),TMOBJTYPE_DOCK,&dock[PunDock[i]]);
  328.                         }
  329.                     }
  330.                 break;
  331.             }
  332.  
  333.             mess->am_RC=0;
  334.  
  335.             ReplyMsg((struct Message *)mess);
  336.         }
  337.     }
  338. }
  339.  
  340.  
  341.  
  342. /*****
  343.  *
  344.  * FUNCTION:    void main(int argc,char *argv[])
  345.  *
  346.  * AIM:        MAIN
  347.  *
  348.  * RESULT: -
  349.  *
  350.  ****/
  351.  
  352. void main(int argc,char *argv[])
  353. {
  354.     struct RDArgs        *RDArgs;
  355.     ULONG             args[2]={(ULONG)"progdir:docks.prefs",0,},
  356.                  try,
  357.                  i;
  358.     void            *handle;
  359.     struct TagItem        *dock;
  360.  
  361.  
  362.  
  363.     /***** Delete DockX and DockY arrays *****/
  364.     for(i=0;i<NUMDOCKS;i++)    DockX[i]=DockY[i]=(UWORD)~0;
  365.  
  366.     /***** Find the ProgED port *****/
  367.     if (!(PEDPort=FindPort("PED_API")))
  368.     {
  369.         printf("Can't find ProgED API Port!\n");
  370.         exit(0);
  371.     }
  372.  
  373.     /***** Create a message port. It will be used to get msgs from ProgED *****/
  374.     if (!(MyPort=CreateMsgPort()))
  375.     {
  376.         printf("Can't create a MsgPort!\n");
  377.         exit(0);
  378.     }
  379.  
  380.     /***** Fill in the ac_ClientPort field of the structure APIClient which
  381.         will be used for registering *****/
  382.     MyClient.ac_ClientPort=MyPort;
  383.  
  384.     /***** Let's register as client at ProgED port *****/
  385.     msg.am_MsgType=PED_API_REGISTER;
  386.     msg.am_MsgArg[0]=(ULONG)&MyClient;
  387.     SendMessageToPED(&msg);
  388.  
  389.     /***** Main loop *****/
  390.     if (RDArgs=ReadArgs("CONFIG/K",args,NULL))
  391.     {
  392.             /***** Wait for ToolManager *****/
  393.         for (try = 20; !(handle = AllocTMHandle()) && try; try--)
  394.             Delay(10);
  395.  
  396.         if (handle)
  397.         {
  398.             if (dock=SetupDock((char *)args[0],handle))
  399.                 HandleMsg(handle,dock);
  400.  
  401.             FreeTMHandle(handle);
  402.         }
  403.  
  404.         FreeArgs(RDArgs);
  405.     }
  406.  
  407.     /***** Exit! *****/
  408.     DeleteMsgPort(MyPort);
  409.     exit(0);
  410. }
  411.  
  412.  
  413.  
  414.