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