home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0912.lha / Yak / Source / Source.lha / main.c < prev    next >
C/C++ Source or Header  |  1993-08-10  |  10KB  |  402 lines

  1. /*
  2.  * Yak version $Revision$
  3.  * ----------------
  4.  * [Yak == Yet Another K(?)ommodity
  5.  *
  6.  * There seems to be a profusion of commodities doing this or that.
  7.  * Heres mine, to do what I want it to:
  8.  *
  9.  *    AutoActivate windows (SunMouse)
  10.  *    ClickToFront, ClickToBack, ScreenCycle
  11.  *    Close/Zip/Shrink/Zoom/Turn a window via keyboard.
  12.  *    Bring up a palette on front screen.
  13.  *    Insert date into read-stream.
  14.  *    Produce key-click (like my keyclick program).
  15.  *    Some other things...
  16.  *
  17.  * Martin W. Scott & Gaδl Marziou, 8/93.
  18.  */
  19. #include <exec/types.h>
  20. #include <exec/libraries.h>
  21. #include <exec/memory.h>
  22. #include <devices/inputevent.h>
  23. #include <dos/dos.h>
  24. #include <dos/dostags.h>
  25. #include <graphics/displayinfo.h>
  26. #include <libraries/commodities.h>
  27. #include <libraries/reqtools.h>
  28. #include <libraries/locale.h>
  29. #include <intuition/intuitionbase.h>
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <proto/graphics.h>
  33. #include <proto/commodities.h>
  34. #include <proto/intuition.h>
  35. #include <proto/reqtools.h>
  36. #include <proto/locale.h>
  37. #include <string.h>
  38. #include <stdarg.h>
  39.  
  40. #include "yak.h"
  41. #include "hotkey_types.h"
  42. #include "beep.h"
  43. #include "icon.h"
  44. #include "version.h"
  45. #include "popup_locale_strings.h"
  46. extern UBYTE *getString (int);
  47.  
  48. #include "WB2CLI.h"    /* we'll be a shell process */
  49. #define DEF_CURRENTDIR    "SYS:"
  50.  
  51. /* local prototypes for main.c */
  52. static void CloseResources(void);
  53. static BOOL OpenResources(void);
  54. static void FreePatterns(void);
  55. static LONG ProcessMsg(void);
  56. void __main(void);
  57.  
  58. #ifdef __SASC_60    /* save some bytes */
  59. void _MemCleanup(void){}
  60. #else
  61. void MemCleanup(void){}
  62. #endif
  63.  
  64. /* global data - library bases and the like */
  65. extern struct WBStartup *_WBenchMsg;
  66. struct Library    *CxBase, *IconBase,
  67.         *GadToolsBase, *LayersBase,
  68.         *WorkbenchBase, *LocaleBase;
  69. struct IntuitionBase *IntuitionBase;
  70. struct GfxBase *GfxBase;
  71. struct DosLibrary *DOSBase;
  72. struct Locale *locale;
  73. struct Catalog *Catalog;
  74. struct MsgPort *broker_mp;
  75. CxObj *broker;
  76. char *PopKeyStr;
  77. #define POPKEY_EVENT    1L    /* cannot clash with YHK event... */
  78.  
  79. char *versionstr="\0$VER: "VERSION_NAME;
  80.  
  81. struct NewBroker newbroker = {
  82.     NB_VERSION,
  83.     "Yak",           /* string to identify this broker */
  84.     VERSION_STR,
  85.     "Multi-purpose commodity",
  86.     NBU_UNIQUE | NBU_NOTIFY,      /* Don't want any new commodities
  87.                                    * starting with this name.  If someone
  88.                                    * tries it, let me know */
  89.     COF_SHOW_HIDE
  90. };
  91.  
  92. ULONG        wndsigflag;        /* here for overlay purposes */
  93. ULONG        cxsigflag;
  94. ULONG        invariantsigflag;
  95. BYTE        palette_count;        /* how many palettes are open */
  96.  
  97. /* from handler.c */
  98. extern ULONG    clicksigflag, intuiopsigflag;
  99. extern void (*intui_routine)(APTR);    /* for intui_op's */
  100. extern APTR intui_parameter;
  101.  
  102. /* from icon.c */
  103. extern ULONG    appsigflag;
  104.  
  105. /* close what we opened */
  106. static void
  107. CloseResources()
  108. {
  109.     if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  110.     if (GfxBase) CloseLibrary((struct Library *)GfxBase);
  111.     if (CxBase) CloseLibrary(CxBase);
  112.     if (LayersBase) CloseLibrary(LayersBase);
  113.     if (IconBase) CloseLibrary(IconBase);
  114.     if (GadToolsBase) CloseLibrary(GadToolsBase);
  115.     if (WorkbenchBase) CloseLibrary(WorkbenchBase);
  116.     if (LocaleBase)
  117.     {
  118.         if (Catalog) CloseCatalog (Catalog);
  119.         CloseLocale(locale);
  120.         CloseLibrary(LocaleBase);
  121.     }
  122. }
  123.  
  124. /* open libraries, devices that we need */
  125. static BOOL
  126. OpenResources()
  127. {
  128.     if ((IntuitionBase = (void *)OpenLibrary("intuition.library", 37L)) &&
  129.         (GfxBase = (void *)OpenLibrary("graphics.library", 37L)) &&
  130.         (CxBase = OpenLibrary("commodities.library", 37L)) &&
  131.         (LayersBase = OpenLibrary("layers.library", 37L)) &&
  132.         (IconBase = OpenLibrary("icon.library", 37L)) &&
  133.         (GadToolsBase = OpenLibrary("gadtools.library", 37L)) &&
  134.         (WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
  135.         (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L)))
  136.         {
  137.         if (LocaleBase =(struct LocaleBase *)OpenLibrary("locale.library", 38L))
  138.             if (!(locale = OpenLocale(NULL)))
  139.             {
  140.                 PostError("No locale set!");
  141.                 CloseLibrary(LocaleBase);
  142.                 LocaleBase = NULL;
  143.             }
  144.             else
  145.                 if (!(Catalog = OpenCatalog( NULL, "yak.catalog", 
  146.                                           OC_BuiltInLanguage, "english", 
  147.                                           OC_Version, 15L, 
  148.                                           TAG_DONE )))
  149.                 {
  150.                     PostError("Couldn't open yak.catalog !");
  151.                 }
  152.         return TRUE;
  153.     }
  154.     CloseResources();
  155.     return FALSE;
  156. }
  157.  
  158. /* simple requester with args */
  159. void
  160. PostError(char *body, ... )
  161. {
  162.     struct EasyStruct es;
  163.     va_list args;
  164.  
  165.     if (!IntuitionBase)
  166.     {
  167.         Write(Output(), "Need AmigaDos 2.0+\n", -1);
  168.         return;
  169.     }
  170.  
  171.     /* setup the argument array */
  172.     va_start( args, body );
  173.  
  174.     /* initialise the structure */
  175.     es.es_StructSize = sizeof(struct EasyStruct);
  176.     es.es_Flags = 0L;
  177.     es.es_Title = "Yak Message";
  178.     es.es_TextFormat = body;
  179.     es.es_GadgetFormat = "OK";
  180.  
  181.     /* display the requester */
  182.     EasyRequestArgs(NULL, &es, NULL, args);
  183.  
  184.     /* free the arguments */
  185.     va_end( args );
  186. }
  187.  
  188. /* parse pattern, report errors */
  189. BOOL __regargs
  190. InitPattern(char *newpatstr, UWORD n)
  191. {
  192.     char *patstr = newpatstr ? newpatstr : patterns[n].patstr;
  193.     char *pat;
  194.     LONG len;
  195.  
  196.     if (pat = AllocVec(len = strlen(patstr)*3+10, MEMF_CLEAR))
  197.     {
  198.         if (ParsePattern(patstr, pat, len) != -1)
  199.         {
  200.             if (newpatstr) strncpy(patterns[n].patstr, newpatstr, PATLEN);
  201.             if (patterns[n].pat) FreeVec(patterns[n].pat);
  202.             patterns[n].pat = pat;
  203.             return TRUE;
  204.         }
  205.         PostError("Error parsing pattern:\n\"%s\"", patstr);
  206.         FreeVec(pat);
  207.     }
  208.     else PostError("Allocation error");
  209.     return FALSE;
  210. }
  211.  
  212. static void
  213. FreePatterns()
  214. {
  215.     UWORD i;
  216.  
  217.     for (i = 0; i < NUM_PATTERNS; i++)
  218.         if (patterns[i].pat) FreeVec(patterns[i].pat);
  219. }
  220.  
  221. void
  222. __main()        /* Yak: multi-function commodity */
  223. {
  224.     BPTR    newdir = NULL, olddir;
  225.  
  226.     if (OpenResources())
  227.     {
  228.         if (broker_mp = CreateMsgPort())
  229.             {
  230.                 newbroker.nb_Port = broker_mp;
  231.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  232.  
  233.         /* process tool-types */
  234.         GetOurIcon(_WBenchMsg);
  235.                 newbroker.nb_Pri = (BYTE)TTInt("CX_PRIORITY", 0);
  236.  
  237.         if (_WBenchMsg)
  238.         {
  239.             if (newdir = Lock(DEF_CURRENTDIR, ACCESS_READ))
  240.                 olddir = CurrentDir(newdir);
  241.             WB2CLI(_WBenchMsg,4000,DOSBase);        /* get it over with... */
  242.         }
  243.  
  244.         if (broker = CxBroker(&newbroker, NULL))
  245.                 {
  246.             /* HANDLER FIRST, SO IT SEES EVERYTHING!!! */
  247.             if (InitHandler())
  248.             {
  249.                 InitYakHotKeyList();
  250.                 LoadSettings();
  251.  
  252.                 if (PopKeyStr = DupStr(TTString("CX_POPKEY", "Rcommand help")))
  253.                 {
  254.                     CxObj *tmpobj;
  255.  
  256.                     if (tmpobj = HotKey(PopKeyStr, broker_mp, POPKEY_EVENT))
  257.                         AttachCxObj(broker, tmpobj);
  258.                     else
  259.                         PostError("CX_POPKEY definition invalid:\"%s\"", PopKeyStr);
  260.                 }
  261.                 /* else... if this failed, we lose */
  262.  
  263.                 MyPri(ACTIVE);
  264.                 ActivateCxObj(broker, 1L);
  265.  
  266.                 if (TTBool("CX_POPUP", FALSE))
  267.                     ShowWindow();
  268.  
  269.                 if (TTBool("APPICON", FALSE))
  270.                 {
  271.                     if (!MakeOurAppIcon(TTString("ICONNAME", "Yak!")))
  272.                         if (_WBenchMsg)
  273.                             PostError("Couldn't create AppIcon");
  274.                 }
  275.                 /* WB makes a copy of icon, so can free it */
  276.                 FreeOurIcon();
  277.  
  278.                 /* these are the signals waited for, + window sig */
  279.                 invariantsigflag = SIGBREAKF_CTRL_C | cxsigflag
  280.                            | clicksigflag | intuiopsigflag
  281.                            | appsigflag;
  282.  
  283.                 while (ProcessMsg())
  284.                     ;
  285.                 HideWindow();
  286.                 RemoveOurAppIcon();
  287.                 DeleteYakHotKeyList();
  288.                 FreeStr(PopKeyStr);
  289.                 MyPri(ORIGINAL);
  290.  
  291.                 EndHandler();
  292.                 FreePatterns();
  293.             }
  294.             else PostError("Allocation error");
  295.  
  296.                     DeleteCxObjAll(broker);
  297.                 }
  298.  
  299.         if (newdir) {
  300.             CurrentDir(olddir);
  301.             UnLock(newdir);
  302.         }
  303.  
  304.                 DeleteMsgPort(broker_mp);
  305.         FreeOurIcon();    /* may already be gone, but so what? */
  306.             }
  307.         else PostError("Allocation error");
  308.  
  309.         CloseResources();
  310.     }
  311.     else PostError("Resource error");
  312. }
  313.  
  314. /* monitor cx port, act on messages */
  315. static LONG
  316. ProcessMsg(void)
  317. {
  318.     CxMsg *msg;
  319.     ULONG sigrcvd, msgid, msgtype;
  320.     LONG returnvalue = 1L;
  321.  
  322.     sigrcvd = Wait(invariantsigflag | wndsigflag);
  323.  
  324.     if (sigrcvd & intuiopsigflag)        /* intuiop requested */
  325.     intui_routine(intui_parameter);
  326.  
  327.     if ((sigrcvd & clicksigflag) && click_volume)    /* keyclick please */
  328.     {
  329.     beep(click_volume);
  330.         Delay(1);   /* avoid ugly sound when key repeating */
  331.     }
  332.  
  333.     if (sigrcvd & appsigflag)        /* settings change */
  334.     {
  335.     RespondToAppIcon();
  336.     ShowWindow();
  337.     }
  338.  
  339.     if (sigrcvd & wndsigflag)        /* settings change */
  340.         if (HandleIDCMP() != HELP_OKAY)
  341.         returnvalue = 0;
  342.     else if (!wndsigflag)        /* window gone */
  343.         DummyOverlay();        /* remove code */
  344.  
  345.     while(msg = (CxMsg *)GetMsg(broker_mp))
  346.     {
  347.         msgid = CxMsgID(msg);
  348.         msgtype = CxMsgType(msg);
  349.         ReplyMsg((struct Message *)msg);
  350.  
  351.         switch(msgtype)
  352.         {
  353.             case CXM_IEVENT:
  354.  
  355.         if (msgid == POPKEY_EVENT)
  356.             ShowWindow();
  357.         else
  358.             /* a generic hotkey... */
  359.             PerformAction((YakHotKey *)msgid);
  360.         break;
  361.  
  362.             case CXM_COMMAND:
  363.                 switch(msgid)
  364.                 {
  365.                     case CXCMD_UNIQUE:
  366.             case CXCMD_APPEAR:
  367.             ShowWindow();    /* check error return? */
  368.             break;
  369.  
  370.             case CXCMD_DISAPPEAR:
  371.             HideWindow();
  372.             DummyOverlay();
  373.             break;
  374.  
  375.                     case CXCMD_DISABLE:
  376.                         ActivateCxObj(broker, 0L);
  377.                         break;
  378.  
  379.                     case CXCMD_ENABLE:
  380.                         ActivateCxObj(broker, 1L);
  381.                         break;
  382.  
  383.                     case CXCMD_KILL:
  384.                         returnvalue = 0L;
  385.                         break;
  386.                 }
  387.                 break;
  388.         }
  389.     }
  390.  
  391.     if (sigrcvd & SIGBREAKF_CTRL_C)
  392.         returnvalue = 0L;
  393.  
  394.     if (!returnvalue && !OkayToExit())
  395.     {
  396.     PostError("Cannot exit yet:\npalette(s) remain open");
  397.     returnvalue = 1;
  398.     }
  399.     
  400.     return(returnvalue);
  401. }
  402.