home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / ProgEd / sources / ped_apidocks / PED_Docks.c next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  9.0 KB  |  416 lines

  1.  
  2. /******
  3.  * Include vari
  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.  * Strutture e variabili globali
  22.  ******/
  23.  
  24. struct tmTool
  25. {
  26.     APTR    exec,
  27.         icon,
  28.         sound;
  29. };
  30.  
  31. struct APIClient    MyClient=
  32. {
  33.     NULL,            /* Questo campo sara' riempito dopo... */
  34.     NOTIFY_ON_SHOW_HIDE,    /* Dimmi quando chiudi e riapri il tuo schermo */
  35.     "Cliente DOCK",        /* Nome del cliente (non utilizzato ancora) */
  36.     NULL            /* Porre a NULL e non manipolare! Grazie. */
  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.  * FUNZIONE:    void SendMessageToPED(struct APIMessage    *msg)
  52.  *
  53.  * SCOPO:    Questa funzione spedisce un messaggio alla porta APi del
  54.  *        ProgED.
  55.  *
  56.  * RESTITUISCE: -
  57.  *
  58.  ****/
  59.  
  60. void SendMessageToPED(struct APIMessage    *msg)
  61. {
  62.     msg->am_Message.mn_Node.ln_Succ=NULL;
  63.     msg->am_Message.mn_Node.ln_Pred=NULL;
  64.     msg->am_Message.mn_Node.ln_Type=NT_MESSAGE;
  65.     msg->am_Message.mn_Node.ln_Pri=0;
  66.     msg->am_Message.mn_Node.ln_Name=NULL;
  67.     msg->am_Message.mn_ReplyPort=MyPort;
  68.     msg->am_Message.mn_Length=sizeof(struct APIMessage);
  69.  
  70.     PutMsg(PEDPort,(struct Message *)msg);
  71.     WaitPort(MyPort);
  72.     while(GetMsg(MyPort));
  73. }
  74.  
  75.  
  76.  
  77. /*****
  78.  *
  79.  * FUNZIONE:    struct TagItem *SetupDock(char *config,void *handle)
  80.  *
  81.  * SCOPO:    Crea il dock dato il nome del file di configurazione, l'handle
  82.  *        per il toolmanager e due puntatori che restituiranno la
  83.  *        posizione x ed y del dock.
  84.  *
  85.  * RESTITUISCE: Puntatore al vettore di definizione per creare il dock.
  86.  *
  87.  ****/
  88.  
  89. struct TagItem *SetupDock(char *config,void *handle)
  90. {
  91.     BPTR         file;
  92.     ULONG         num=0,
  93.              oldnum=0,
  94.              totnum=0,
  95.              n=0,
  96.              orien=0,
  97.              col=1,
  98.              i;
  99.     struct TagItem    *dock;
  100.     struct RDArgs    *RDArgs;
  101.     char         buffer[300],
  102.              execName[10],
  103.              imageName[10];
  104.  
  105.  
  106.  
  107.     if (file=Open(config,MODE_OLDFILE))
  108.     {
  109.         while(FGets(file,buffer,sizeof(buffer)-1))
  110.         {
  111.             if (!strncmp(buffer,"ADD",3))        totnum++;
  112.             else if (!strncmp(buffer,"DOCK",4))    totnum+=10;
  113.         }
  114.  
  115.         Close(file);
  116.  
  117.         if (dock=(struct TagItem *)calloc(totnum*sizeof(struct TagItem),1))
  118.         {
  119.             if (file=Open(config,MODE_OLDFILE))
  120.             {
  121.                 if (RDArgs=AllocDosObject(DOS_RDARGS,NULL))
  122.                 {
  123.                     while(FGets(file,buffer,sizeof(buffer)-1))
  124.                     {
  125.                         ULONG    args[10]={0,0,0,0,0,0,0,0,0,0};
  126.  
  127.  
  128.  
  129.                         RDArgs->RDA_Source.CS_Buffer=buffer+4;
  130.                         RDArgs->RDA_Source.CS_Length=strlen(buffer+4);
  131.                         RDArgs->RDA_Source.CS_CurChr=0;
  132.                         RDArgs->RDA_DAList=NULL;
  133.                         RDArgs->RDA_Buffer=NULL;
  134.  
  135.                         if (!strncmp(buffer,"ADD",3))
  136.                         {
  137.                             memset(args,0,sizeof(args));
  138.                             if (ReadArgs("COMMAND/K/A,AREXX/S,SHELL/S,DIR/K,OUTPUT/K,IMAGE/K/A",args,RDArgs))
  139.                             {
  140.                                 struct TagItem imageTags[]={
  141.                                     TMOP_File,    NULL,
  142.                                     TMOP_Data,    NULL,
  143.                                 TAG_END};
  144.  
  145.                                 struct TagItem execTags[]={
  146.                                     TMOP_Command,        NULL,
  147.                                     TMOP_ExecType,        NULL,
  148.                                     TMOP_Stack,        10000,
  149.                                     TMOP_CurrentDir,    NULL,
  150.                                     TMOP_Output,        NULL,
  151.                                 TAG_END};
  152.  
  153.                                 imageTags[0].ti_Data=(ULONG)strdup((char *)args[5]);
  154.  
  155.                                 execTags[0].ti_Data=(ULONG)strdup((char *)args[0]);
  156.                                 execTags[1].ti_Data=args[1] ? TMET_ARexx : TMET_CLI;
  157.                                 execTags[3].ti_Data=args[3] ? (ULONG)strdup((char *)args[3]) : NULL;
  158.                                 execTags[4].ti_Data=args[4] ? (ULONG)strdup((char *)args[4]) : NULL;
  159.  
  160.                                 sprintf(execName,"PED_E%d",num);
  161.                                 sprintf(imageName,"PED_I%d",num++);
  162.  
  163.                                 CreateTMObjectTagList(handle,strdup(execName),TMOBJTYPE_EXEC,execTags);
  164.                                 CreateTMObjectTagList(handle,strdup(imageName),TMOBJTYPE_IMAGE,imageTags);
  165.  
  166.                                 FreeArgs(RDArgs);
  167.                             }
  168.                         }
  169.                         else if (!strncmp(buffer,"DOCK",4))
  170.                         {
  171.                             memset(args,0,sizeof(args));
  172.                             if (ReadArgs("X/N,Y/N,HORIZONTAL/S,COLUMNS/N",args,RDArgs))
  173.                             {
  174.                                 if (args[0])    DockX[NumDock]=*((ULONG *)args[0]);
  175.                                 if (args[1])    DockY[NumDock]=*((ULONG *)args[1]);
  176.                                 if (args[2])    orien=1;
  177.                                 if (args[3])    col=*((ULONG *)args[3]);
  178.  
  179.                                         if (num-oldnum>0)
  180.                                 {
  181.                                     PunDock[NumDock++]=n;
  182.  
  183.                                     dock[n].ti_Tag=TMOP_PubScreen;
  184.                                     dock[n++].ti_Data=NULL;
  185.  
  186.                                     dock[n].ti_Tag=TMOP_LeftEdge;
  187.                                     dock[n++].ti_Data=NULL;
  188.  
  189.                                     dock[n].ti_Tag=TMOP_TopEdge;
  190.                                     dock[n++].ti_Data=NULL;
  191.  
  192.                                     dock[n].ti_Tag=TMOP_Activated;
  193.                                     dock[n++].ti_Data=TRUE;
  194.  
  195.                                     dock[n].ti_Tag=TMOP_Centered;
  196.                                     dock[n++].ti_Data=FALSE;
  197.  
  198.                                     dock[n].ti_Tag=TMOP_Columns;
  199.                                     dock[n++].ti_Data=col;
  200.  
  201.                                     dock[n].ti_Tag=TMOP_Vertical;
  202.                                     dock[n++].ti_Data=orien;
  203.  
  204.                                     dock[n].ti_Tag=TMOP_Text;
  205.                                     dock[n++].ti_Data=FALSE;
  206.  
  207.                                     for(i=oldnum;i<num;i++)
  208.                                     {
  209.                                         struct tmTool *tool;
  210.  
  211.                                         if (tool=(struct tmTool *)calloc(sizeof(struct tmTool),1))
  212.                                         {
  213.                                             if ((tool->exec=calloc(12,1))&&(tool->icon=calloc(12,1)))
  214.                                             {
  215.                                                 sprintf((char *)tool->exec,"PED_E%d",i);
  216.                                                 sprintf((char *)tool->icon,"PED_I%d",i);
  217.  
  218.                                                 dock[n].ti_Tag=TMOP_Tool;
  219.                                                 dock[n++].ti_Data=(ULONG)tool;
  220.                                             }
  221.                                         }
  222.                                     }
  223.  
  224.                                     dock[n++].ti_Tag=TAG_DONE;
  225.  
  226.                                     oldnum=num;
  227.                                 }
  228.  
  229.                                 FreeArgs(RDArgs);
  230.                             }
  231.                         }
  232.                     }
  233.  
  234.                     FreeDosObject(DOS_RDARGS,RDArgs);
  235.                 }
  236.  
  237.                 Close(file);
  238.             }
  239.         }
  240.     }
  241.  
  242.     return(dock);
  243. }
  244.  
  245.  
  246.  
  247. /*****
  248.  *
  249.  * FUNZIONE:    void HandleMsg(void *handle,struct TagItem *dock)
  250.  *
  251.  * SCOPO:    Gestione dei messaggi.
  252.  *
  253.  * RESTITUISCE: -
  254.  *
  255.  ****/
  256.  
  257. void HandleMsg(void *handle,struct TagItem *dock)
  258. {
  259.     UBYTE             finito;
  260.     ULONG             i;
  261.     struct APIMessage    *mess;
  262.     struct Screen        *screen;
  263.     char            *pubscrname,
  264.                  tmp[25];
  265.  
  266.  
  267.  
  268.     /***** Ottieni l'indirizzo dello schermo del ProgED *****/
  269.     msg.am_MsgType=PED_API_GET_SCREEN_ADDRESS;
  270.     SendMessageToPED(&msg);
  271.     screen=(struct Screen *)msg.am_RC;
  272.  
  273.     /***** Ottieni il nome dello schermo pubblico *****/
  274.     msg.am_MsgType=PED_API_GET_PUBSCRNAME;
  275.     SendMessageToPED(&msg);
  276.     pubscrname=(char *)msg.am_RC;
  277.  
  278.     /***** Se lo schermo e' aperto apri anche il dock *****/
  279.     if (screen)
  280.     {
  281.         for(i=0;i<NumDock;i++)
  282.         {
  283.             dock[PunDock[i]].ti_Data=(ULONG)pubscrname;
  284.             dock[PunDock[i]+1].ti_Data=(DockX[i] == ~0) ? screen->Width : DockX[i];
  285.             dock[PunDock[i]+2].ti_Data=(DockY[i] == ~0) ? screen->BarHeight+1 : DockY[i];
  286.  
  287.             sprintf(tmp,"PED_DOCK%d",i);
  288.             CreateTMObjectTagList(handle,strdup(tmp),TMOBJTYPE_DOCK,&dock[PunDock[i]]);
  289.         }
  290.     }
  291.  
  292.     /***** Loop principale *****/
  293.     finito=FALSE;
  294.     while(!finito)
  295.     {
  296.         WaitPort(MyPort);
  297.         while(mess=(struct APIMessage *)GetMsg(MyPort))
  298.         {
  299.             switch(mess->am_MsgType)
  300.             {
  301.                 /***** ProgED sta' per terminare! Terminiamo
  302.                     anche noi :-( *****/
  303.                 case    PED_API_QUIT:
  304.                     finito=TRUE;
  305.                 break;
  306.  
  307.                 /***** ProgED sta' per chiudere il suo schermo.
  308.                     Chiudiamo la nostra finestra. *****/
  309.                 case    PED_API_HIDE:
  310.                     for(i=0;i<NumDock;i++)
  311.                     {
  312.                         sprintf(tmp,"PED_DOCK%d",i);
  313.                         DeleteTMObject(handle,tmp);
  314.                     }
  315.                 break;
  316.  
  317.                 /***** ProgED ha riaperto il suo schermo.
  318.                     Riapriamo la nostra finestra. *****/
  319.                 case    PED_API_SHOW:
  320.                     if (screen=(struct Screen *)mess->am_MsgArg[0])
  321.                     {
  322.                         for(i=0;i<NumDock;i++)
  323.                         {
  324.                             dock[PunDock[i]].ti_Data=(ULONG)mess->am_MsgArg[1];
  325.                             dock[PunDock[i]+1].ti_Data=(DockX[i] == ~0) ? screen->Width : DockX[i];
  326.                             dock[PunDock[i]+2].ti_Data=(DockY[i] == ~0) ? screen->BarHeight+1 : DockY[i];
  327.  
  328.                             sprintf(tmp,"PED_DOCK%d",i);
  329.                             CreateTMObjectTagList(handle,strdup(tmp),TMOBJTYPE_DOCK,&dock[PunDock[i]]);
  330.                         }
  331.                     }
  332.                 break;
  333.             }
  334.  
  335.             mess->am_RC=0;
  336.  
  337.             ReplyMsg((struct Message *)mess);
  338.         }
  339.     }
  340. }
  341.  
  342.  
  343.  
  344. /*****
  345.  *
  346.  * FUNZIONE:    void main(int argc,char *argv[])
  347.  *
  348.  * SCOPO:    MAIN.
  349.  *
  350.  * RESTITUISCE: -
  351.  *
  352.  ****/
  353.  
  354. void main(int argc,char *argv[])
  355. {
  356.     struct RDArgs        *RDArgs;
  357.     ULONG             args[2]={(ULONG)"progdir:docks.prefs",0,},
  358.                  try,
  359.                  i;
  360.     void            *handle;
  361.     struct TagItem        *dock;
  362.  
  363.  
  364.  
  365.     /***** Cancella i vettori DockX e DockY *****/
  366.     for(i=0;i<NUMDOCKS;i++)    DockX[i]=DockY[i]=(UWORD)~0;
  367.  
  368.     /***** Cerca la porta del ProgED *****/
  369.     if (!(PEDPort=FindPort("PED_API")))
  370.     {
  371.         printf("Can't find ProgED API Port!\n");
  372.         exit(0);
  373.     }
  374.  
  375.     /***** Crea una porta che utilizzeremo per ricevere i msg dal ProgED *****/
  376.     if (!(MyPort=CreateMsgPort()))
  377.     {
  378.         printf("Can't create a MsgPort!\n");
  379.         exit(0);
  380.     }
  381.  
  382.     /***** Riempi il campo ac_ClientPort della struttura APIClient che
  383.         utilizzeremo per la registrazione *****/
  384.     MyClient.ac_ClientPort=MyPort;
  385.  
  386.     /***** Registriamoci come cliente presso il ProgED *****/
  387.     msg.am_MsgType=PED_API_REGISTER;
  388.     msg.am_MsgArg[0]=(ULONG)&MyClient;
  389.     SendMessageToPED(&msg);
  390.  
  391.     /***** Ciclo principale *****/
  392.     if (RDArgs=ReadArgs("CONFIG/K",args,NULL))
  393.     {
  394.             /***** Aspetta il ToolManager *****/
  395.         for (try = 20; !(handle = AllocTMHandle()) && try; try--)
  396.             Delay(10);
  397.  
  398.         if (handle)
  399.         {
  400.             if (dock=SetupDock((char *)args[0],handle))
  401.                 HandleMsg(handle,dock);
  402.  
  403.             FreeTMHandle(handle);
  404.         }
  405.  
  406.         FreeArgs(RDArgs);
  407.     }
  408.  
  409.     /***** Usciamo *****/
  410.     DeleteMsgPort(MyPort);
  411.     exit(0);
  412. }
  413.  
  414.  
  415.  
  416.