home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 592b.lha / TermII / English / XCMD Examples / panel.c next >
Encoding:
C/C++ Source or Header  |  1991-12-22  |  7.7 KB  |  386 lines

  1. #include <exec/types.h>
  2. #include <exec/libraries.h>
  3. #include <exec/ports.h>
  4. #include <intuition/intuition.h>
  5. #include <libraries/gadtools.h>
  6. #include <clib/exec_protos.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/gadtools_protos.h>
  9. #include <clib/dos_protos.h>
  10. #include <clib/alib_protos.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include "XCMD.h"
  15. #include "XCMDTools.h"
  16.  
  17. #define forever for(;;)
  18.  
  19. /*
  20. *     VARIABLES
  21. */
  22. struct GadgetList *glist         = NULL;
  23. struct Window     *window        = NULL;
  24. struct Screen     *screen_lock   = NULL;
  25. void              *vi            = NULL;
  26. struct MsgPort    *port          = NULL;
  27. BPTR              dir_lock;
  28. struct Library    *GadToolsBase  = NULL;
  29. struct TextAttr   Topaz80        = { "topaz.font",8,0,0 };
  30. char              line[256];
  31. struct Panel
  32.   {
  33.   BOOL pa_Set;
  34.   char pa_Text[10];
  35.   char pa_Command[256];
  36.   };
  37. struct Panel      Panel[25];
  38. int               PanelIX = 0;
  39.  
  40.  
  41. /*
  42. *
  43. *       PROTOS
  44. *
  45. */
  46.  
  47. void Load(void);
  48. void ParseLine(void);
  49. BOOL InitPanel(void);
  50.  
  51.  
  52. /*
  53. *       void ParseLine(void)
  54. *
  55. *       Parse the line variable, and associate button name
  56. *       and command to one of the gadget panel
  57. *
  58. */
  59. void ParseLine()
  60.   {
  61.   int i = 0;
  62.   while((line[i] == ' ') && (line[i]))
  63.     i++;
  64.   if(line[i])
  65.     {
  66.     int j = 0;
  67.     while((line[i] != ';') && (line[i]))
  68.       {
  69.       if(j<9)
  70.         Panel[PanelIX].pa_Text[j] = line[i];
  71.       else if(j==9)
  72.         Panel[PanelIX].pa_Text[j] = '\0';
  73.       i++;
  74.       j++;
  75.       }
  76.     if((line[i] == ';') && (j<9))
  77.       Panel[PanelIX].pa_Text[j] = '\0';
  78.     if(line[i])
  79.       {
  80.       i++;
  81.       while((line[i] == ' ') && (line[i]))
  82.         i++;
  83.       if(line[i])
  84.         {
  85.         int k = 0;
  86.         while(line[i])
  87.           {
  88.           Panel[PanelIX].pa_Command[k] = line[i];
  89.           i++; k++;
  90.           }
  91.         Panel[PanelIX].pa_Command[k] = line[i];
  92.         Panel[PanelIX].pa_Set = TRUE;
  93.         PanelIX++;
  94.         }
  95.       }
  96.     }
  97.   }
  98.  
  99.  
  100.  
  101.  
  102. /*
  103. *
  104. *       void Load(void)
  105. *
  106. *       Load the file "panel.config" to define all gadgets
  107. *       of the panel. Each read line is put in the variable
  108. *       line and parse with ParseLine()
  109. *
  110. */
  111. void Load()
  112.   {
  113.   FILE *fp;
  114.   int i,c;
  115.   for(i = 0; i<25; i++)
  116.     Panel[i].pa_Set = FALSE;
  117.   fp = fopen("panel.config","r");
  118.   if(fp)
  119.     {
  120.     forever         /* Read until EOF */
  121.       {
  122.       i = 0;
  123.       forever
  124.         {
  125.         c = fgetc(fp);
  126.         if((c == EOF) || (c == '\n') || (i == 255))
  127.           {
  128.           line[i] = '\0';
  129.           break;
  130.           }
  131.         else
  132.           {
  133.           line[i] = c;
  134.           i++;
  135.           }
  136.         }
  137.       if(strlen(line) && (line[0] != '#'))
  138.         ParseLine();
  139.       if ((c == EOF) || (PanelIX == 25)) break;
  140.       }
  141.     fclose(fp);
  142.     }
  143.   }
  144.  
  145.  
  146.  
  147. /*
  148. *       InitPanel()
  149. *
  150. *       The panel is initialized, informations
  151. *       read from "panel.config", parsed, open
  152. *       window and create gadgets. Retourne TRUE
  153. *       if everything is OK, FALSE otherwise.
  154. *
  155. */
  156. BOOL InitPanel()
  157.   {
  158.   vi = GetVisualInfo(screen_lock, TAG_DONE);
  159.   if(!vi)
  160.     {
  161.     puts("Unable to get visual info");
  162.     return(FALSE);
  163.     }
  164.   window = OpenWindowTags(NULL,
  165.     WA_DragBar, TRUE,
  166.     WA_DepthGadget, TRUE,
  167.     WA_PubScreen, screen_lock,
  168.     WA_Width, 417,
  169.     WA_Height, 90,
  170.     WA_CloseGadget, TRUE,
  171.     WA_IDCMP, CLOSEWINDOW|REFRESHWINDOW|BUTTONIDCMP,
  172.     WA_Title, "Command panel",
  173.     WA_Activate, TRUE,
  174.     TAG_DONE);
  175.   if(!window)
  176.     {
  177.     puts("Unable to open window");
  178.     return(FALSE);
  179.     }
  180.  
  181.   struct Gadget *gad = CreateContext(&glist);
  182.   struct NewGadget ng;
  183.  
  184.   ng.ng_TextAttr = &Topaz80;
  185.   ng.ng_VisualInfo = vi;
  186.   ng.ng_LeftEdge = 5;
  187.   ng.ng_TopEdge = 12;
  188.   ng.ng_Width = 80;
  189.   ng.ng_Height = 14;
  190.   ng.ng_Flags = 0;
  191.   ng.ng_GadgetText = NULL;
  192.  
  193.   for(int j=0; j<5; j++)
  194.     {
  195.     for(int i = 0; i<5; i++)
  196.       {
  197.       int k = j*5 + i;
  198.       ng.ng_GadgetID = k;
  199.       if(Panel[k].pa_Set)
  200.         ng.ng_GadgetText = Panel[k].pa_Text;
  201.       else
  202.         ng.ng_GadgetText = NULL;
  203.       gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_DONE);
  204.       ng.ng_LeftEdge += 82;
  205.       }
  206.     ng.ng_TopEdge += 15;
  207.     ng.ng_LeftEdge = 5;
  208.     }
  209.  
  210.   if(!gad)
  211.     {
  212.     puts("Unable to create gadgets");
  213.     return(FALSE);
  214.     }
  215.  
  216.   AddGList(window,glist,-1,-1,NULL);
  217.   RefreshGList(glist,window,NULL,-1);
  218.   GT_RefreshWindow(window,NULL);
  219.   return(TRUE);
  220.   }
  221.  
  222.  
  223.  
  224.  
  225. /*
  226. *
  227. *           main()
  228. *
  229. *
  230. */
  231.  
  232. int main()
  233.  
  234.   {
  235.   /*
  236.   *     Check we are not already running
  237.   */
  238.   if(FindPort("PANEL_PORT"))
  239.     {
  240.     puts("I am already here");
  241.     exit(10);
  242.     }
  243.   /*
  244.   *     Open gadget toolkit
  245.   */
  246.   GadToolsBase = OpenLibrary("gadtools.library",36);
  247.   if(!GadToolsBase)
  248.     {
  249.     puts("Unable to open gadget toolkit");
  250.     exit(10);
  251.     }
  252.   /*
  253.   *     Create message port
  254.   */
  255.   port = CreatePort("PANEL_PORT",0);
  256.   if(!port)
  257.     {
  258.     puts("Unable to open PANEL_PORT");
  259.     CloseLibrary(GadToolsBase);
  260.     exit(10);
  261.     }
  262.   /*
  263.   *     Wait for a START command from Term II
  264.   */
  265.   forever
  266.     {
  267.     WaitPort(port);
  268.     struct XCMD *xcmd = GetMsg(port);
  269.     if(xcmd && (xcmd->xcmd_TermCmd == XCMD_START))
  270.       {
  271.       screen_lock = xcmd->xcmd_TermScreen;
  272.       dir_lock    = xcmd->xcmd_TermDir;
  273.       ReplyMsg(xcmd);
  274.       break;
  275.       }
  276.     else
  277.       ReplyMsg(xcmd);
  278.     }
  279.   /*
  280.   *     Go in the same directory than Term II
  281.   *     to read config file.
  282.   */
  283.   BPTR old_lock = CurrentDir(dir_lock);
  284.   /*
  285.   *     Read configuration file
  286.   */
  287.   Load();
  288.   /*
  289.   *     Create panel
  290.   */
  291.   struct XCMD *xcmd = CreateXCMD(port);
  292.   if(InitPanel() && xcmd)
  293.     {
  294.     BOOL not_the_end = TRUE;
  295.     struct IntuiMessage *msg;
  296.     ULONG IntuiSignal = 1 << window->UserPort->mp_SigBit;
  297.     ULONG XCMDSignal  = 1 << port->mp_SigBit;
  298.     while(not_the_end)
  299.       {
  300.       ULONG signals = Wait(IntuiSignal|XCMDSignal);
  301.       if(signals & IntuiSignal)
  302.         {
  303.         /*
  304.         *       We have been signaled by Intuition
  305.         */
  306.         while(msg = GT_GetIMsg(window->UserPort))
  307.           {
  308.           ULONG class = msg->Class;
  309.           SHORT code = msg->Code;
  310.           struct Gadget *gadget = msg->IAddress;
  311.           GT_ReplyIMsg(msg);
  312.           switch(class)
  313.             {
  314.             case CLOSEWINDOW:
  315.               not_the_end = FALSE;
  316.               break;
  317.             case REFRESHWINDOW:
  318.               GT_BeginRefresh(window);
  319.               GT_EndRefresh(window,TRUE);
  320.               break;
  321.             case GADGETUP:
  322.               /*
  323.               *     A gadget has been clicked
  324.               */
  325.               xcmd->xcmd_Command = Panel[gadget->GadgetID].pa_Command;
  326.               if(SendXCMD(xcmd))
  327.                 {
  328.                 /*
  329.                 *   We _must_ wait for Term answer
  330.                 */
  331.                 WaitPort(port);
  332.                 GetMsg(port);
  333.                 }
  334.               /*
  335.               *     Command is done
  336.               */
  337.               break;
  338.             }
  339.           }
  340.         }
  341.       if(signals & XCMDSignal)
  342.         {
  343.         /*
  344.         *       We receive a message from Term II.
  345.         *       It can only be a START or a STOP
  346.         *       command.
  347.         */
  348.         struct XCMD *x = GetMsg(port);
  349.         if(x->xcmd_TermCmd == XCMD_STOP)
  350.           not_the_end = FALSE;
  351.         else
  352.           {
  353.           /*
  354.           *     It is a START sent by mistake.
  355.           *     We free the locks and return
  356.           *     the message
  357.           */
  358.           UnLock(x->xcmd_TermDir);
  359.           UnlockPubScreen(NULL,x->xcmd_TermScreen);
  360.           }
  361.         ReplyMsg(x);
  362.         }
  363.       }
  364.     }
  365.   if(xcmd)
  366.     DeleteXCMD(xcmd);
  367.   if(port)
  368.     DeletePort(port);
  369.   if(window)
  370.     {
  371.     struct IntuiMessage *msg;
  372.     while(msg = GT_GetIMsg(window->UserPort)) GT_ReplyIMsg(msg);
  373.     CloseWindow(window);
  374.     };
  375.   if(vi)
  376.     FreeVisualInfo(vi);
  377.   if(GadToolsBase)
  378.     {
  379.     FreeGadgets(glist);
  380.     UnlockPubScreen(NULL,screen_lock);
  381.     CloseLibrary(GadToolsBase);
  382.     }
  383.   CurrentDir(old_lock);
  384.   UnLock(dir_lock);
  385.   }
  386.