home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Utilities / ACDPlay / src / V1.5 / Application.c < prev    next >
C/C++ Source or Header  |  1997-12-23  |  15KB  |  355 lines

  1. /* Application.c */
  2.  
  3. #include "Constants.h"
  4. #include "Locale.h"
  5.  
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <exec/libraries.h>
  9. #include <intuition/intuition.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <intuition/gadgetclass.h>
  12. #include <libraries/gadtools.h>
  13. #include <libraries/commodities.h>
  14. #include <libraries/asl.h>
  15. #include <devices/scsidisk.h>
  16. #include <devices/cd.h>
  17. #include <graphics/rastport.h>
  18. #include <pragma/screennotify_lib.h>
  19. #include "AsmLib/CD-ROM.h"
  20. #include "Structures/Application.h"
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/dos_protos.h>
  24. #include <clib/commodities_protos.h>
  25. #include <clib/intuition_protos.h>
  26. #include <clib/gadtools_protos.h>
  27. #include <clib/icon_protos.h>
  28. #include <clib/wb_protos.h>
  29. #include <string.h>
  30. #include "ACDPlay_protos.h"
  31.  
  32. extern char        *ls[ANZ_MSG];
  33. extern char             PROG_NAME[];
  34. extern char        *PROG_FULLNAME;
  35.  
  36. struct Application *CreateApplication(char *progname)
  37. {
  38.         struct Application *app = NULL;
  39.  
  40.         /* Speicher für Applicationstruktur besorgen */
  41.         if (app = GetVecA(sizeof(struct Application), MEMF_PUBLIC | MEMF_CLEAR))
  42.         {
  43.                 app->progname = progname;
  44.                 app->orgprwin = ((struct Process *)FindTask(NULL))->pr_WindowPtr;
  45.  
  46.                 if (app->cdstr = GetVecA(sizeof(struct CDStruct), MEMF_PUBLIC | MEMF_CLEAR))
  47.                 {
  48.                         app->edit_pos = -1;
  49.  
  50.                         if (app->arexx_mp = OpenARexxPortA())
  51.                         {
  52.                                 ReadArguments(app);
  53.                                 ReadConfig(app, "ENV:acdplay.cfg");
  54.                                 app->arexxdir = GetDirContents("PROGDIR:ARexx");
  55.  
  56.                                 if (OpenCDStruct(app->cdstr))
  57.                                 {
  58.                                         if (app->scroller = GetVecA(sizeof(struct Scroll), MEMF_CLEAR | MEMF_PUBLIC))
  59.                                         {
  60.                                                 int i;
  61.  
  62.                                                 app->appwin[WINDOW_PUBSCREEN].minwidth = MINWIDTH_LISTVIEWWIN;
  63.                                                 app->magicwbmode = (app->flag_tool & FLAG_TOOL_MWB) ? TRUE : FALSE;
  64.  
  65.                                                 /* ASL-Requester-Werte eintragen */
  66.                                                 for (i = 0; i < ANZ_ASL; i++)
  67.                                                 {
  68.                                                         app->appasl[i].leftedge = 100;
  69.                                                         app->appasl[i].topedge  = 50;
  70.                                                         app->appasl[i].width    = 200;
  71.                                                         app->appasl[i].height   = 200;
  72.                                                         app->appasl[i].noicons  = TRUE;
  73.                                                 }
  74.                                                 /* spezifische ASL-Werte */
  75.                                                 app->appasl[ASL_AREXX].titletext                = ls[MSG_ASL_AREXX_HAIL];
  76.                                                 strcpy(app->appasl[ASL_AREXX].dirname, "ARexx/");
  77.  
  78.                                                 UpdateCDTitles(app);    /* CD-Titel setzen */
  79.                                                 if (CreateCommodity(app))
  80.                                                         return (app);
  81.                                                 else
  82.                                                         ShowMsgA(ls[MSG_ERROR_TITLE], ls[MSG_NO_CX], NULL);
  83.                                         }
  84.                                 }
  85.                         }
  86.                 }
  87.         }
  88.  
  89.         DeleteCxObjAll(app->broker);
  90.         DeleteMsgPort(app->broker_mp);
  91.         FreeVec(app->scroller);
  92.         FreeList(app->arexxdir);
  93.         CloseARexxPortA(app->arexx_mp);
  94.         FreeCDStruct(app->cdstr);
  95.         FreeVec(app);
  96.         return (NULL);
  97. }
  98.  
  99. void DeleteApplication(struct Application *app)
  100. {
  101.         CloseApplication(app);
  102.         FreeAppIcon(app);
  103.  
  104.         /* ScreenNotify-Port, falls vorhanden, entfernen */
  105.         if (app->scrnotify_mp)
  106.         {
  107.                 /* Solange versuchen den Screennotifyclient zu schließen, bis es klappt */
  108.                 if (app->snhandle_wbench)
  109.                         while (!RemWorkbenchClient(app->snhandle_wbench))
  110.                                 Delay(10);
  111.  
  112.                 Forbid();
  113.                 FlushMessagesA(app->scrnotify_mp);
  114.                 DeleteMsgPort(app->scrnotify_mp);
  115.                 Permit();
  116.         }
  117.  
  118.         /* Muß leider hier plaziert werden, da die Listen in CreateApplication() */
  119.         /* erstellt wurden: */
  120.         FreeList(app->cdstr->cdtitles);
  121.         FreeList(app->cdstr->progtitles);
  122.         FreeList(app->arexxdir);
  123.  
  124.         /* Commodities: Broker löschen, MsgPort leeren und freigeben */
  125.         DeleteCxObjAll(app->broker);
  126.         Forbid();                                                       /* Sicher ist sicher :-) */
  127.         FlushMessagesA(app->broker_mp);
  128.         DeleteMsgPort(app->broker_mp);
  129.         Permit();
  130.  
  131.         FreeVec(app->scroller);
  132.  
  133.         FreeCDStruct(app->cdstr);
  134.         CloseARexxPortA(app->arexx_mp);
  135.         FreeVec(app);
  136. }
  137.  
  138. BOOL OpenApplication(struct Application *app)
  139. {
  140.         BOOL success = TRUE;
  141.  
  142.         if (!app->screen)
  143.         {
  144.                 success = FALSE;
  145.  
  146.                 app->screen = LockPubScreen(app->pubscreenname);        /* Es wird zuerst versucht, */
  147.                 if (!app->screen)                                                                       /* den gewünschten PubScreen */
  148.                         app->screen = LockPubScreen(NULL);                              /* zu locken. */
  149.                 if (!app->screen)                                                                       /* Bei Scheitern wird es mit dem */
  150.                 {                                                                                                       /* DefaultPubScreen versucht, */
  151.                         ShowMsgA(ls[MSG_ERROR_TITLE], ls[MSG_NO_PUBSCREEN], NULL);      /* wenn das nicht klappt, */
  152.                         return (success);                                                               /* steigt ACDPlay aus */
  153.                 }
  154.  
  155.                 ScreenToFront(app->screen);
  156.  
  157.                 if (app->screen->Flags & WBENCHSCREEN)
  158.                         app->Flags |= APPF_WORKBENCH;
  159.                 else
  160.                         app->Flags &= ~APPF_WORKBENCH;
  161.  
  162.                 if (app->visualinfo = GetVisualInfo(app->screen, TAG_END))
  163.                 {
  164.                         WORD win;
  165.                         app->font = app->screen->Font;
  166.  
  167.                         success = TRUE;
  168.  
  169.                         if (!MakeMenus(app)) return (FALSE);
  170.  
  171.                         /* Message-Port erstellen, der als shared-IDCMP-Port benutzt wird */
  172.                         if (app->idcmp = CreateMsgPort())
  173.                         {
  174.                                 for (win = ANZ_WINDOWS - 1; win >= 0; win--)
  175.                                         if (app->appwin[win].winopen)
  176.                                                 if (!(OpenAppWin(app, win))) success = FALSE;
  177.                         }
  178.  
  179.                         /* Damit die Application nicht mehr durch OpenWorkBench() geöffnet werden kann */
  180.                         app->Flags &= ~APPF_SNCLOSED;
  181.                 }
  182.                 else ShowMsgA(ls[MSG_ERROR_TITLE], ls[MSG_NO_VISUALINFO], app->screen->Title);
  183.         }
  184.         else
  185.         {
  186.                 ScreenToFront(app->screen);             /* Screen wird *immer* nach vorne geholt */
  187.                 WindowToFront(app->appwin[WINDOW_MAIN].window);
  188.                 ActivateWindow(app->appwin[WINDOW_MAIN].window);
  189.         }
  190.  
  191.         return(success);
  192. }
  193.  
  194. void CloseApplication(Application *app)
  195. {
  196.         if (app->screen)
  197.         {
  198.                 WORD win;
  199.  
  200.                 for (win = ANZ_WINDOWS-1; win >= 0; win--)
  201.                         if (app->appwin[win].winopen) CloseAppWin(app, win, FALSE);
  202.  
  203.                 /* DeleteMsgPort(), FreeMenus() und FreeVisualInfo() testen
  204.                    selbstständig auf NULL-Pointer */
  205.  
  206.                 DeleteMsgPort(app->idcmp);
  207.                 app->idcmp = NULL;
  208.  
  209.                 FreeMenus(app->menustrip);
  210.                 app->menustrip = NULL;
  211.  
  212.                 FreeVisualInfo(app->visualinfo);
  213.                 app->visualinfo = NULL;
  214.  
  215.                 if (app->screen) {
  216.                         UnlockPubScreen(NULL, app->screen);
  217.                         app->screen = NULL;
  218.                 }
  219.         }
  220. }
  221.  
  222. BOOL OpenCDStruct(struct CDStruct *cd)
  223. {
  224.         BOOL success = FALSE;
  225.  
  226.         if (cd->cdx = CDOpenDeviceA(cd->device, cd->unit, cd->lun))
  227.         {
  228.                 /* Wenn geöffnetes Gerät kein CD-ROM-Laufwerk ist */
  229.                 if (cd->cdx->cdx_DeviceType != DEVTYPE_CDROM)
  230.                 {
  231.                         struct EasyStruct req={ sizeof(struct EasyStruct),      /* es_StructSize */
  232.                                                                         0,                                                      /* es_Flags */
  233.                                                                         ls[MSG_WARNING_TITLE],          /* es_Title */
  234.                                                                         ls[MSG_BAD_SCSIDEVICE],         /* es_TextFormat */
  235.                                                                         ls[MSG_YESNO_REQ] };            /* es_GadFormat */
  236.  
  237.                         if (!EasyRequestArgs(NULL, &req, NULL, &(ULONG)cd->unit))
  238.                                 return (success);
  239.                 }
  240.  
  241.                 cd->num_track   = cd->cdx->cdx_TOCData->TrackNum;
  242.                 cd->cur_track   = cd->cdx->cdx_CurrentTrack;
  243.                 cd->active              = cd->cdx->cdx_Active;
  244.                 cd->cdnameptr   = cd->cdname;
  245.                 cd->artistptr   = cd->artistname;
  246.  
  247.                 cd->fileformat  = FORMAT_CDR;
  248.                 cd->freq                = FREQ_44100;
  249.  
  250.                 success = TRUE;
  251.         }
  252.  
  253.         return (success);
  254. }
  255.  
  256. void FreeCDStruct(CDStruct *cd)
  257. {
  258.         if (cd->cdx)
  259.                 CDCloseDeviceA(cd->cdx);
  260.  
  261.         FreeVec(cd);
  262. }
  263.  
  264. void CreateAppIcon(struct Application *app)
  265. {
  266.         if (!app->diskobj)
  267.                 if (app->diskobj = GetDiskObject(app->progname))
  268.                 {
  269.                         /* Das muß wohl so sein */
  270.                         app->diskobj->do_Type = NULL;
  271.  
  272.                         if (app->appicon_mp = CreateMsgPort())
  273.                                 app->appicon = AddAppIcon(0L, 0L, PROG_NAME, app->appicon_mp, NULL, app->diskobj, TAG_DONE);
  274.                 }
  275. }
  276.  
  277. void FreeAppIcon(struct Application *app)
  278. {
  279.         BOOL success = FALSE;
  280.  
  281.         if (app->appicon)
  282.         {
  283.                 RemoveAppIcon(app->appicon);
  284.                 app->appicon = NULL;
  285.         }
  286.  
  287.         FlushMessagesA(app->appicon_mp);
  288.         DeleteMsgPort(app->appicon_mp);
  289.         app->appicon_mp = NULL;
  290.  
  291.         if (app->diskobj)
  292.         {
  293.                 FreeDiskObject(app->diskobj);
  294.                 app->diskobj = NULL;
  295.         }
  296. }
  297.  
  298. BOOL CreateCommodity(struct Application *app)
  299. {
  300.         /* Erstellen des Commodities */
  301.         if (app->broker_mp = CreateMsgPort())
  302.         {
  303.                 struct NewBroker newbroker = {
  304.                                 NB_VERSION,                                     /* nb_Version - Version of the NewBroker structure */
  305.                                 PROG_NAME,                                      /* nb_Name - Name of the commodity */
  306.                                 PROG_FULLNAME,                          /* nb_Title */
  307.                                 ls[PROG_DESCR],                         /* nb_Decr - Description */
  308.                                 NBU_UNIQUE,                                     /* nb_Unique */
  309.                                 COF_SHOW_HIDE,                          /* nb_Flags */
  310.                                 app->cx_priority,                       /* nb_Pri - Priority */
  311.                                 app->broker_mp,                         /* nb_Port - MsgPort CX talks to */
  312.                                 0 };                                            /* nb_ReservedChannel */
  313.  
  314.                 if (app->broker = CxBroker(&newbroker, NULL))
  315.                 {
  316.                         if (app->filter_popkey = CxFilter(app->cx_popkey))
  317.                         {
  318.                                 AttachCxObj(app->broker, app->filter_popkey);
  319.                                 if (app->sender_popkey = CxSender(app->broker_mp, EVENT_HOTKEYPRESSED))
  320.                                 {
  321.                                         AttachCxObj(app->filter_popkey, app->sender_popkey);
  322.                                         if (app->translate_popkey = CxTranslate(NULL))
  323.                                         {
  324.                                                 AttachCxObj(app->filter_popkey, app->translate_popkey);
  325.  
  326.                                                 if (app->filter_timer = CxFilter("timer -caps -alt -control -lcommand -rcommand -numericpad -rbutton -leftbutton -midbutton"))
  327.                                                 {
  328.                                                         AttachCxObj(app->broker, app->filter_timer);
  329.                                                         if (app->sender_timer = CxSender(app->broker_mp, EVENT_TIMER))
  330.                                                         {
  331.                                                                 AttachCxObj(app->filter_timer, app->sender_timer);
  332.                                                                 ActivateCxObj(app->broker, 1L);
  333.  
  334.                                                                 if (ScreenNotifyBase)
  335.                                                                 {
  336.                                                                         if (app->scrnotify_mp = CreateMsgPort())
  337.                                                                                 app->snhandle_wbench = AddWorkbenchClient(app->scrnotify_mp, 1);
  338.                                                                         else
  339.                                                                                 ShowMsgA(ls[MSG_ERROR_TITLE], ls[MSG_NO_MSGPORT], NULL);
  340.                                                                 }
  341.  
  342.                                                                 return (TRUE);
  343.                                                         }
  344.                                                 }
  345.                                         }
  346.                                 }
  347.                         }
  348.                 }
  349.                 DeleteMsgPort(app->broker_mp);
  350.         }
  351.         else ShowMsgA(ls[MSG_ERROR_TITLE], ls[MSG_NO_MSGPORT], NULL);
  352.  
  353.         return (FALSE);
  354. }
  355.