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