2 ** $VER: XModule 3.9 (16.8.97) Copyright (C) 1993,94,95,96 Bernardo Innocenti
4 ** This source code is provided "AS-IS", without warranties of any kind and
5 ** it is subject to change without notice. All usage is at your own risk.
6 ** No liability or responsibility is assumed by the author.
8 ** Use 4 chars wide TABs to read this file
11 #include <exec/execbase.h>
12 #include <exec/lists.h>
13 #include <exec/memory.h>
15 #include <dos/dostags.h>
16 #include <libraries/gadtools.h>
17 #include <utility/utility.h>
18 #include <workbench/startup.h>
20 #include <proto/exec.h>
21 #include <proto/dos.h>
22 #include <proto/intuition.h>
23 #include <proto/icon.h>
24 #include <proto/utility.h>
25 #include <proto/xmodule.h>
28 #include "XModulePriv.h"
33 /* Local functions prototypes */
35 static LONG GetShellArgs (void);
36 static LONG GetWBArgs (void);
37 static void DisposeArgs (void);
38 static void HandleFrom (void);
39 static void SetupHooks (void);
40 static void Cleanup (LONG err);
41 static LONG Setup (void);
45 /* Memory pool for use by main program only */
46 XDEF void *Pool = NULL;
51 XDEF const UBYTE Version[] = "$VER: " VSTR " " BUILDMODE " " XMODULECOPY;
52 XDEF const UBYTE BaseName[] = BASENAME;
53 XDEF const UBYTE PrgName[] = PRGNAME;
58 XDEF struct IntuitionBase *IntuitionBase = NULL;
59 XDEF struct GfxBase *GfxBase = NULL;
60 XDEF struct Library *LayersBase = NULL;
61 XDEF struct UtilityBase *UtilityBase = NULL;
62 XDEF struct Library *GadToolsBase = NULL;
63 XDEF struct Library *DiskfontBase = NULL;
64 XDEF struct Library *AslBase = NULL;
65 XDEF struct Library *IFFParseBase = NULL;
66 XDEF struct Library *WorkbenchBase = NULL;
67 XDEF struct Library *IconBase = NULL;
68 XDEF struct ReqToolsBase *ReqToolsBase = NULL;
69 XDEF struct Library *CxBase = NULL;
70 XDEF struct Library *KeymapBase = NULL;
71 XDEF struct XModuleBase *XModuleBase = NULL;
75 /* This structure holds data to open required libraries automatically. */
79 struct Library **Base;
89 #endif /* !OS30_ONLY */
91 static const struct OpenLibs openlibs[] =
93 { (struct Library **)&IntuitionBase, "intuition.library", OSLIBVER },
94 { (struct Library **)&GfxBase, "graphics.library", OSLIBVER },
95 { (struct Library **)&LayersBase, "layers.library", OSLIBVER },
96 { (struct Library **)&UtilityBase, "utility.library", OSLIBVER },
97 { (struct Library **)&GadToolsBase, "gadtools.library", OSLIBVER },
98 { (struct Library **)&KeymapBase, "keymap.library", 37 },
99 { (struct Library **)&IFFParseBase, "iffparse.library", 37 },
100 { (struct Library **)&WorkbenchBase, "workbench.library", 37 },
101 { (struct Library **)&IconBase, "icon.library", 37 },
102 { (struct Library **)&DiskfontBase, "diskfont.library", 0 },
109 /* Used for argument parsing.
110 * NOTE: All fields must be 4 bytes long.
118 LONG CxPopup; /* Set to TRUE by default. */
124 } XMArgs; /* egcs doesn't like this: = { 0 }; */
126 static struct RDArgs *RDArgs = NULL;
128 #define ARGS_TEMPLATE "FROM/M,PUBSCREEN/K,PORTNAME/K,SETTINGS/K,CX_POPUP/T,CX_POPKEY/K,CX_PRIORITY/K/N,ICONXPOS/K/N,ICONYPOS/K/N,ICONNAME/K"
132 extern LONG STDARGS __main (void)
134 /* XModule main entry point. Get arguments from CLI/Workbench,
135 * setup environment, open required files and call main event
142 DB(kprintf ((STRPTR)(Version + 6)));
143 DB(kprintf ("\n*** Starting debug session -- Good luck!\n\n"));
146 if (!(err = Setup())) /* Setup environment */
156 static LONG GetShellArgs (void)
158 /* Parse command line arguments */
160 if (!(RDArgs = ReadArgs (ARGS_TEMPLATE, (LONG *)&XMArgs, NULL)))
165 } /* End GetShellArgs() */
169 static LONG GetWBArgs (void)
171 /* Parse Workbench arguments */
173 struct DiskObject *dobj;
178 /* Get Multiselect args.
179 * Create a NULL-terminated array of STRPTRs
180 * in the same way ReadArgs() would have done.
182 if (WBenchMsg->sm_NumArgs > 1)
183 if (!(XMArgs.From = AllocVec (WBenchMsg->sm_NumArgs * sizeof (STRPTR), MEMF_CLEAR)))
186 for (i = 1; i < WBenchMsg->sm_NumArgs; i++)
188 UBYTE buf[PATHNAME_MAX];
190 if (NameFromLock (WBenchMsg->sm_ArgList[i].wa_Lock, buf, PATHNAME_MAX))
191 if (AddPart (buf, WBenchMsg->sm_ArgList[i].wa_Name, PATHNAME_MAX))
193 if (XMArgs.From[i-1] = AllocVec (strlen (buf), MEMF_ANY))
194 strcpy (XMArgs.From[i-1], buf);
203 if (!(dobj = GetProgramIcon()))
206 if (val = FindToolType (dobj->do_ToolTypes, "PUBSCREEN"))
207 if (XMArgs.PubScreen = AllocVec (strlen (val), MEMF_ANY))
208 strcpy (XMArgs.PubScreen, val);
210 if (val = FindToolType (dobj->do_ToolTypes, "PORTNAME"))
211 if (XMArgs.PortName = AllocVec (strlen (val), MEMF_ANY))
212 strcpy (XMArgs.PortName, val);
214 if (val = FindToolType (dobj->do_ToolTypes, "SETTINGS"))
215 if (XMArgs.Settings = AllocVec (strlen (val), MEMF_ANY))
216 strcpy (XMArgs.Settings, val);
218 if (val = FindToolType (dobj->do_ToolTypes, "CX_POPUP"))
219 XMArgs.CxPopup = MatchToolValue (val, "YES");
221 if (val = FindToolType (dobj->do_ToolTypes, "CX_POPKEY"))
222 if (XMArgs.CxPopKey = AllocVec (strlen (val), MEMF_ANY))
223 strcpy (XMArgs.CxPopKey, val);
225 if (val = FindToolType (dobj->do_ToolTypes, "CX_PRIORITY"))
226 if (XMArgs.CxPriority = AllocVec (sizeof (LONG), MEMF_ANY))
227 StrToLong (val, XMArgs.CxPriority);
229 if (val = FindToolType (dobj->do_ToolTypes, "ICONXPOS"))
230 if (XMArgs.IconX = AllocVec (sizeof (LONG), MEMF_ANY))
231 StrToLong (val, XMArgs.IconX);
233 if (val = FindToolType (dobj->do_ToolTypes, "ICONYPOS"))
234 if (XMArgs.IconY = AllocVec (sizeof (LONG), MEMF_ANY))
235 StrToLong (val, XMArgs.IconY);
237 if (val = FindToolType (dobj->do_ToolTypes, "ICONNAME"))
238 if (XMArgs.IconName = AllocVec (strlen (val), MEMF_ANY))
239 strcpy (XMArgs.IconName, val);
241 FreeDiskObject (dobj);
244 } /* End GetWBArgs() */
248 static void DisposeArgs (void)
257 /* NULL is a valid parameter for FreeVec() */
258 FreeVec (XMArgs.IconName);
259 FreeVec (XMArgs.IconY);
260 FreeVec (XMArgs.IconX);
261 FreeVec (XMArgs.CxPriority);
262 FreeVec (XMArgs.CxPopKey);
263 FreeVec (XMArgs.Settings);
264 FreeVec (XMArgs.PortName);
265 FreeVec (XMArgs.PubScreen);
269 STRPTR *tmp = XMArgs.From;
277 FreeVec (XMArgs.From);
281 memset (&XMArgs, 0, sizeof (XMArgs));
286 static void HandleFrom (void)
290 STRPTR *name = XMArgs.From;
291 struct AnchorPath *ap;
294 if (ap = AllocMem (sizeof (struct AnchorPath) + PATHNAME_MAX, MEMF_CLEAR))
296 OpenProgressWindow ();
298 ap->ap_BreakBits = SIGBREAKF_CTRL_C;
299 ap->ap_Strlen = PATHNAME_MAX;
303 err = MatchFirst (*name, ap);
307 xmLoadModule (ap->ap_Buf,
308 XMSNG_AddToList, TRUE,
311 err = MatchNext (ap);
314 if (err != ERROR_NO_MORE_ENTRIES)
316 UBYTE buf[FAULT_MAX];
318 Fault (err, NULL, buf, FAULT_MAX);
319 ShowMessage (MSG_ERR_LOAD, *name, buf);
326 CloseProgressWindow();
328 FreeMem (ap, sizeof (struct AnchorPath) + PATHNAME_MAX);
330 else LastErr = ERROR_NO_FREE_STORE;
336 static void SetupHooks (void)
338 struct Library *XMHookBase;
339 struct FileInfoBlock *fib;
341 UBYTE libpath[PATHNAME_MAX];
348 if (fib = (struct FileInfoBlock *)AllocDosObject (DOS_FIB, NULL))
350 if (lock = Lock (DEF_HOOKSDIR, ACCESS_READ))
351 if (Examine (lock, fib))
352 while (ExNext (lock, fib))
354 strcpy (libpath, DEF_HOOKSDIR);
355 AddPart (libpath, fib->fib_FileName, PATHNAME_MAX);
357 if (XMHookBase = OpenLibrary (libpath, 0))
360 #pragma libcall XMHookBase SetupXMHook 24 801
361 void SetupXMHook (struct XModuleBase *);
362 #elif defined(__GNUC__)
363 #define SetupXMHook(xmbase) \
364 LP1NR(0x24, SetupXMHook, struct XModuleBase *, xmbase, a0, \
367 #error Define SetupXMHook() library call for your compiler
370 SetupXMHook (XModuleBase);
371 CloseLibrary (XMHookBase);
375 FreeDosObject (DOS_FIB, fib);
379 /* Set default saver */
381 if (!IsListEmpty ((struct List *)&XModuleBase->xm_Savers))
382 XModuleBase->xm_DefaultSaver = (struct XMHook *)XModuleBase->xm_Savers.mlh_Head;
387 static LONG Setup (void)
392 SetProgramName (PrgName);
394 /* Initialize view lists */
395 NEWLIST (&WindowList);
397 NEWLIST (&PatternsList);
398 NEWLIST (&SequenceList);
399 NEWLIST (&InstrList);
401 /* Install graphics function replacements */
402 /* InstallGfxFunctions(); */ /* These are currently never used */
404 /* Initialize ScrInfo structure */
405 strcpy (ScrInfo.PubScreenName, BaseName);
407 /* Initialize PubPort name */
408 strcpy (PubPortName, BaseName);
410 /* Initialize PubPort name */
411 strcpy (IconName, PrgName);
414 /* Open required libraries */
415 for (i = 0 ; openlibs[i].Base ; i++)
416 if (!(*(openlibs[i].Base) = MyOpenLibrary (openlibs[i].Name, openlibs[i].Version)))
417 if (openlibs[i].Version) return RETURN_FAIL;
420 if (UtilityBase->lib_Version >= 39)
421 #endif /* !OS30_ONLY */
422 UniqueID = GetUniqueID(); /* Get ID for HelpGroup and other jobs */
424 /* Get startup arguments */
425 XMArgs.CxPopup = TRUE;
430 err = GetShellArgs();
436 /* Create XModule library */
437 if (err = MakeXModuleLibrary ())
440 Pool = XModuleBase->xm_Pool;
443 /* Try to load XModule preferences */
446 if (LoadPrefs (XMArgs.Settings))
448 UBYTE buf[FAULT_MAX];
450 Fault (IoErr(), NULL, buf, FAULT_MAX);
451 ShowMessage (MSG_ERR_LOAD, XMArgs.Settings, buf);
456 if (LoadPrefs ("PROGDIR:" PRGNAME ".prefs"))
457 LoadPrefs ("ENV:" PRGNAME ".prefs");
461 /* Use startup Arguments */
463 if (XMArgs.PubScreen)
464 strncpy (ScrInfo.PubScreenName, XMArgs.PubScreen, 31);
467 strncpy (PubPortName, XMArgs.PortName, 15);
469 CxPopup = XMArgs.CxPopup;
472 strncpy (CxPopKey, XMArgs.CxPopKey, 31);
474 if (XMArgs.CxPriority)
475 CxPri = *XMArgs.CxPriority;
478 IconX = *XMArgs.IconX;
481 IconY = *XMArgs.IconY;
484 strncpy (IconName, XMArgs.IconName, 15);
486 /* Setup FileRequesters if LoadPrefs() hasn't already done it */
487 if (!AslBase && !ReqToolsBase)
488 if (err = SetupRequesters())
491 /* Setup App Message Port */
494 /* Setup Rexx Host */
497 /* Setup Commodity object */
500 /* Add internal hooks and load external ones */
503 /* Allocate a new SongInfo structure */
504 if (!XModuleBase->xm_CurrentSong)
508 if (si = xmCreateSong (
509 SNGA_ReadyToUse, TRUE,
513 ReleaseSemaphore (&si->Lock);
515 return ERROR_NO_FREE_STORE;
518 /* Open screen and ToolBox window */
520 if (err = SetupScreen())
524 /* Load modules requested with Shell/Workbench startup arguments */
535 static void Cleanup (LONG err)
537 /* Cleanup routine. Display error message, free all resources & exit */
542 PrintFault (err, PrgName);
544 /* Free all allocated resources */
548 DisposeArgs(); /* Just to be sure */
555 FreeVecPooled (Pool, ScreenAttr.ta_Name);
556 FreeVecPooled (Pool, WindowAttr.ta_Name);
557 FreeVecPooled (Pool, ListAttr.ta_Name);
558 FreeVecPooled (Pool, EditorAttr.ta_Name);
561 /* Dispose XModule library */
562 DisposeXModuleLibrary ();
564 /* Remove AppIcons/AppWindows Port */
567 /* Remove Commodity Broker */
570 /* Remove ARexx port */
575 /* Close all libraries */
576 for (i = 0 ; openlibs[i].Base ; i++)
577 /* starting with V36, NULL is a valid parameter for CloseLibrary(). */
578 CloseLibrary (*(openlibs[i].Base));
584 /* Emergency patch: */
586 LONG SampleWinTags[1] = { 0 };
587 void UpdateSampleMenu (void) {}