home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Commodities / ClipTool / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  10.6 KB  |  410 lines

  1. /*
  2.  *  ClipTool (Udklipsværktøj) - A Commodities Exchange Application
  3.  *  Copyright (C) 1994 Torsten Poulin
  4.  *
  5.  *  main.c - reads arguments and sets up system resources
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *             The author can be contacted by mail at
  22.  *               Torsten Poulin
  23.  *               Banebrinken 99, 2, 77
  24.  *               DK-2400 Copenhagen NV
  25.  *               Denmark
  26.  *             or via email: torsten@diku.dk
  27.  *
  28.  * Created 15-Jan-94
  29.  * $Log:    main.c,v $
  30.  * Revision 1.1  94/02/20  21:28:43  Torsten
  31.  * Initial revision
  32.  * 
  33.  */
  34.  
  35. #include "cliptool.h"
  36. #define STRINGARRAY
  37. #include "ctstrings.h"
  38. #define NSTRINGS (sizeof AppStrings / sizeof(struct AppString))
  39.  
  40. #ifdef __AMIGADATE__
  41. #define COMPDATE __AMIGADATE__
  42. /* 
  43.  * __AMIGADATE__ is defined by SAS/C 6.51; it resolves to a datestamp
  44.  * string in the format required for the AmigaDOS VERSION command.
  45.  * The format is "(dd.mm.yy)", where dd is the day (1-31),
  46.  * mm is the month (1-12), and yy is the year.
  47.  * If it is unknown, we use the value below:
  48.  */
  49. #else
  50. #define COMPDATE "(22.1.94)"
  51. #endif
  52.  
  53. /* Embedded strings for Version, Copyright, and ident */
  54. static char const version[] = "$VER: ClipTool 38.1 " COMPDATE;
  55. static char const copyright[] = "$COPYRIGHT: ©1994 Torsten Poulin$";
  56. static char const RCSid[] = "$Id: main.c,v 1.1 94/02/20 21:28:43 Torsten Exp $";
  57.  
  58. UBYTE *verstring = (UBYTE *) &version[6];
  59.  
  60. struct Library *IntuitionBase;
  61. struct Library *CxBase;
  62. struct Library *IconBase;
  63. struct Library *WorkbenchBase;
  64. struct Library *GadToolsBase;
  65. struct Library *GfxBase;
  66. struct Library *IFFParseBase;
  67. struct Library *LayersBase;
  68. struct Library *AslBase;
  69. struct Library *RexxSysBase;
  70. struct Library *LocaleBase;
  71.  
  72. struct Window *guiwin;
  73. struct AppWindow *appwin;
  74. struct AppIcon *appicon;
  75. long xoffset, yoffset;
  76. struct MsgPort *broker_mp;
  77. struct MsgPort *appwin_mp;
  78. struct MsgPort *appicon_mp;
  79. struct MsgPort *arexx_mp;
  80. CxObj *broker;
  81. ULONG cxsigflag, awsigflag, aisigflag, rxsigflag;
  82. struct FileRequester *filereq;
  83.  
  84. struct Catalog *catalog;
  85.  
  86. BOOL nothidden;
  87. BOOL guiminimized;
  88. BOOL simplebuttons;
  89. BOOL createicons;
  90. BOOL usedropicon;
  91. long unitnumber;
  92.  
  93. UBYTE *portname, *hotkey;
  94.  
  95. /* Private variables */
  96. static UBYTE defportname[] = "CLIPTOOL";
  97. static UBYTE defhotkey[] = "control lalt c";
  98. static BYTE cxpriority;
  99.  
  100. /* Private functions */
  101. static void setupbroker(void);
  102. static void getwbarguments(int, char **);
  103. static void getshellarguments(void);
  104. static void openres(void);
  105. static void closeres(int result);
  106. static void openrexx(void);
  107. static void closerexx(void);
  108. static void loadlocalestrings(void);
  109.  
  110.  
  111. void main(int argc, char **argv)
  112. {
  113.   openres();
  114.   if (argc == 0) getwbarguments(argc, argv);
  115.   else getshellarguments();
  116.   loadlocalestrings();
  117.   initnewmenustruct();
  118.   if (usedropicon) {
  119.     opendropicon();
  120.     if (!appicon) usedropicon = FALSE;
  121.   }
  122.   setupbroker();
  123.   closeres(0);
  124. }
  125.  
  126.  
  127. /*
  128.  * ClipTool knows the following arguments/tooltypes:
  129.  *   CX_PRIORITY/K/N    (=0)
  130.  *   CX_POPKEY/K        (=f1)
  131.  *   CX_POPUP/K         (=yes|no)
  132.  *   EMBOSSED/K         (=yes|no)
  133.  *   CREATEICONS/K      (=yes|no)
  134.  *   UNIT/K/N           (=0-255)
  135.  *   APPICON/K          (=yes|no)
  136.  *   PORTNAME/K         (=CLIPTOOL)
  137.  *
  138.  * The function below is also capable of handling shell arguments,
  139.  * but the current trend is to use ReadArgs().
  140.  */
  141.  
  142. static void getwbarguments(int argc, char **argv)
  143. {
  144.   UBYTE **tooltypes;
  145.   UBYTE *s;
  146.  
  147.   tooltypes = ArgArrayInit(argc, argv);
  148.  
  149.   cxpriority = (BYTE) ArgInt(tooltypes, "CX_PRIORITY", 0);
  150.   if (cxpriority < -128) cxpriority = -128;
  151.   else if (cxpriority > 127) cxpriority = 127;
  152.  
  153.   s = ArgString(tooltypes, "CX_POPKEY", defhotkey);
  154.   if (hotkey = malloc(strlen(s) + 1)) strcpy(hotkey, s);
  155.   else hotkey = defhotkey;
  156.  
  157.   s = ArgString(tooltypes, "CX_POPUP", "yes");
  158.   if (stricmp(s, "yes") == 0) nothidden = TRUE;
  159.  
  160.   s = ArgString(tooltypes, "EMBOSSED", "yes");
  161.   if (stricmp(s, "yes") == 0) simplebuttons = FALSE;
  162.   else simplebuttons = TRUE;
  163.  
  164.   s = ArgString(tooltypes, "CREATEICONS", "yes");
  165.   if (stricmp(s, "yes") == 0) createicons = TRUE;
  166.  
  167.   s = ArgString(tooltypes, "UNIT", "0");
  168.   unitnumber = atol(s);
  169.   if (unitnumber < 0) unitnumber = 0;
  170.   else if (unitnumber > 255) unitnumber = 255;
  171.  
  172.   s = ArgString(tooltypes, "APPICON", "yes");
  173.   if (stricmp(s, "yes") == 0) usedropicon = TRUE;
  174.  
  175.   s = ArgString(tooltypes, "PORTNAME", defportname);
  176.   if (portname = malloc(strlen(s) + 1)) strcpy(portname, s);
  177.   else portname = defportname;
  178.   
  179.   ArgArrayDone();
  180. }
  181.  
  182.  
  183. static long shellargs[8];
  184.  
  185. static void getshellarguments(void)
  186. {
  187.   struct RDArgs *rda;
  188.  
  189.   if (rda = ReadArgs("CX_PRIORITY/K/N,CX_POPKEY/K,CX_POPUP/K,"
  190.              "EMBOSSED/K,CREATEICONS/K,UNIT/K/N,"
  191.              "APPICON/K,PORTNAME/K",
  192.              shellargs, NULL)) {
  193.  
  194.     if (shellargs[0]) {
  195.       cxpriority = *(long *) shellargs[0];
  196.       if (cxpriority < -128) cxpriority = -128;
  197.       else if (cxpriority > 127) cxpriority = 127;
  198.     }
  199.  
  200.     hotkey = defhotkey;
  201.     if (shellargs[1])
  202.       if (hotkey = malloc(strlen((UBYTE *) shellargs[1]) + 1))
  203.     strcpy(hotkey, (char *) shellargs[1]);
  204.       else hotkey = defhotkey;
  205.  
  206.     nothidden = TRUE;
  207.     if (shellargs[2])
  208.       if (stricmp((UBYTE *) shellargs[2], "no") == 0) nothidden = FALSE;
  209.  
  210.     if (shellargs[3])
  211.       if (stricmp((UBYTE *) shellargs[3], "no") == 0) simplebuttons = TRUE;
  212.     
  213.     createicons = TRUE;
  214.     if (shellargs[4])
  215.       if (stricmp((UBYTE *) shellargs[4], "no") == 0) createicons = FALSE;
  216.  
  217.     if (shellargs[5]) {
  218.       unitnumber = *(long *) shellargs[5];
  219.       if (unitnumber < 0) unitnumber = 0;
  220.       else if (unitnumber > 255) unitnumber = 255;
  221.     }
  222.  
  223.     usedropicon = TRUE;
  224.     if (shellargs[6])
  225.       if (stricmp((UBYTE *) shellargs[6], "no") == 0) usedropicon = FALSE;
  226.  
  227.     portname = defportname;
  228.     if (shellargs[7])
  229.       if (portname = malloc(strlen((UBYTE *) shellargs[7]) + 1))
  230.     strcpy(portname, (char *) shellargs[7]);
  231.       else portname = defportname;
  232.     
  233.     FreeArgs(rda);
  234.   }
  235.   else {
  236.     PrintFault(IoErr(), ls(MSG_CLIPTOOL));
  237.     closeres(20);
  238.   }
  239. }
  240.  
  241.  
  242. /*
  243.  * Open the necessary resources
  244.  */
  245.  
  246. static void openres(void)
  247. {
  248.   IntuitionBase = OpenLibrary("intuition.library", 37L);
  249.   CxBase = OpenLibrary("commodities.library", 37L);
  250.   IconBase = OpenLibrary("icon.library", 36L);
  251.   WorkbenchBase = OpenLibrary("workbench.library", 37L);
  252.   GadToolsBase = OpenLibrary("gadtools.library", 37L);
  253.   GfxBase = OpenLibrary("graphics.library", 37L);
  254.   IFFParseBase = OpenLibrary("iffparse.library", 37L);
  255.   LayersBase = OpenLibrary("layers.library", 37L);
  256.   AslBase = OpenLibrary("asl.library", 37L);
  257.   RexxSysBase = OpenLibrary("rexxsyslib.library", 0L);
  258.   LocaleBase = OpenLibrary("locale.library", 38L);
  259.  
  260.   if (LocaleBase)
  261.     catalog = OpenCatalog(NULL, "cliptool.catalog",
  262.               OC_BuiltInLanguage, (ULONG) "english",
  263.               TAG_END);
  264.  
  265.   if (AslBase)
  266.     filereq = (struct FileRequester *)
  267.       AllocAslRequestTags(ASL_FileRequest,
  268.               ASL_LeftEdge, 20,
  269.               ASL_TopEdge, 30,
  270.               TAG_END);
  271.  
  272.   if (!(IntuitionBase && CxBase && IconBase && WorkbenchBase
  273.     && GadToolsBase    && GfxBase && IFFParseBase
  274.     && LayersBase && AslBase && filereq))
  275.     closeres(20);
  276. }
  277.  
  278.  
  279. /*
  280.  * Close them again
  281.  */
  282.  
  283. static void closeres(int result)
  284. {
  285.   closedropicon();
  286.   if (guiwin) closegui();
  287.   if (filereq) FreeAslRequest(filereq);
  288.   if (catalog) CloseCatalog(catalog);
  289.   if (LocaleBase) CloseLibrary(LocaleBase);
  290.   if (RexxSysBase) CloseLibrary(RexxSysBase);
  291.   if (AslBase) CloseLibrary(AslBase);
  292.   if (LayersBase) CloseLibrary(LayersBase);
  293.   if (IFFParseBase) CloseLibrary(IFFParseBase);
  294.   if (GfxBase) CloseLibrary(GfxBase);
  295.   if (GadToolsBase) CloseLibrary(GadToolsBase);
  296.   if (WorkbenchBase) CloseLibrary(WorkbenchBase);
  297.   if (IconBase) CloseLibrary(IconBase);
  298.   if (CxBase) CloseLibrary(CxBase);
  299.   if (IntuitionBase) CloseLibrary(IntuitionBase);
  300.   exit(result);
  301. }
  302.  
  303.  
  304. /*
  305.  * Set up the Commodities Exchange broker and call handleevents().
  306.  */
  307.  
  308. static void setupbroker(void)
  309. {
  310.   struct NewBroker ctbroker;
  311.   CxObj *filter, *sender, *translate;
  312.   CxMsg *msg;
  313.  
  314.   ctbroker.nb_Version = NB_VERSION;
  315.   ctbroker.nb_Name = "ClipTool"; /* not localized */
  316.   ctbroker.nb_Title = verstring;
  317.   ctbroker.nb_Descr = ls(MSG_DESCRIPTION);
  318.   ctbroker.nb_Unique = NBU_UNIQUE | NBU_NOTIFY;
  319.   ctbroker.nb_Flags = COF_SHOW_HIDE;
  320.   ctbroker.nb_Pri = cxpriority;
  321.   ctbroker.nb_ReservedChannel = 0;
  322.  
  323.  
  324.   if (broker_mp = CreateMsgPort()) {
  325.     ctbroker.nb_Port = broker_mp;
  326.     cxsigflag = 1L << broker_mp->mp_SigBit;
  327.     if (broker = CxBroker(&ctbroker, NULL)) {
  328.       if (filter = CxFilter(hotkey)) {
  329.     AttachCxObj(broker, filter);
  330.     if (sender = CxSender(broker_mp, EVENT_HOTKEY)) {
  331.       AttachCxObj(filter, sender);
  332.       if (translate = CxTranslate(NULL)) {
  333.         AttachCxObj(filter, translate);
  334.         if (!CxObjError(filter)) {
  335.           ActivateCxObj(broker, 1L);
  336.           openrexx();
  337.           handleevents();
  338.           closerexx();
  339.         }
  340.       }
  341.     }
  342.       }
  343.       DeleteCxObjAll(broker);
  344.       /* get rid of remaining CxMsgs */
  345.       while (msg = (CxMsg *) GetMsg(broker_mp))
  346.     ReplyMsg((struct Message *) msg);
  347.     }
  348.     DeleteMsgPort(broker_mp);
  349.   }
  350. }
  351.  
  352.  
  353. static void openrexx(void)
  354. {
  355.   if (RexxSysBase) {
  356.     Forbid();
  357.     if (!FindPort(portname)) {
  358.       if (arexx_mp = CreateMsgPort()) {
  359.     arexx_mp->mp_Node.ln_Pri = 1L;
  360.     arexx_mp->mp_Node.ln_Name = portname;
  361.     AddPort(arexx_mp);
  362.       }
  363.     }
  364.     Permit();
  365.     rxsigflag = 1 << arexx_mp->mp_SigBit;
  366.   }
  367. }
  368.  
  369.  
  370. static void closerexx(void)
  371. {
  372.   struct RexxMsg *rxmsg;
  373.   UBYTE *s;
  374.  
  375.   if (arexx_mp) {
  376.     RemPort(arexx_mp);
  377.     while ((rxmsg = (struct RexxMsg *) GetMsg(arexx_mp))) {
  378.       if (CheckRexxMsg((struct Message *) rxmsg)) {
  379.     rxmsg->rm_Result1 = 20;
  380.     rxmsg->rm_Result2 = (long) NULL;
  381.     if (rxmsg->rm_Action & RXFF_RESULT)
  382.       if (s = CreateArgstring("0", 1L)) rxmsg->rm_Result2 = (long) s;
  383.       }
  384.       ReplyMsg((struct Message *) rxmsg);
  385.     }
  386.     DeleteMsgPort(arexx_mp);
  387.   }
  388. }
  389.  
  390.  
  391. /*
  392.  * Make the look-up table point at the local strings, if possible.
  393.  */
  394.  
  395. static void loadlocalestrings(void)
  396. {
  397.   int i;
  398.  
  399.   if (catalog)
  400.     for (i = 0; i < NSTRINGS; i++)
  401.       AppStrings[i].as_Str =
  402.     GetCatalogStr(catalog, i, AppStrings[i].as_Str);
  403. }
  404.  
  405.  
  406. STRPTR ls(LONG id)
  407. {
  408.   return AppStrings[id].as_Str;
  409. }
  410.