home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / audio / multiplay / window.c < prev    next >
C/C++ Source or Header  |  1992-09-14  |  12KB  |  589 lines

  1. /*
  2.  * MultiPlayer
  3.  * Copyright (C) 1992 Bryan Ford
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * I (the author of MultiPlayer) can be contacted on the Internet at
  20.  * "bryan.ford@m.cc.utah.edu".  See "Player.doc" for other addresses.
  21.  *
  22.  * $Id: window.c,v 4.4 92/07/19 18:09:48 BAF Exp $
  23.  *
  24.  * $Log:    window.c,v $
  25.  * Revision 4.4  92/07/19  18:09:48  BAF
  26.  * Adjustment for putting localdata back into RemindNodes
  27.  * 
  28.  * Revision 4.3  92/07/12  08:26:40  BAF
  29.  * end() waits with an EasyRequester as necessary until font requester comes down
  30.  * Enforcer bug with DrawInfo fixed
  31.  *
  32.  * Revision 4.2  92/06/21  11:13:00  BAF
  33.  * Migrated regargs to stdargs
  34.  *
  35.  * Revision 4.1  92/06/06  19:56:05  BAF
  36.  * Major_code_cleanup
  37.  *
  38.  * Revision 3.1  92/05/25  07:53:28  BAF
  39.  * GNU-ized.
  40.  *
  41.  *
  42.  */
  43.  
  44.  
  45. #include <exec/types.h>
  46. #include <exec/execbase.h>
  47. #include <intuition/intuition.h>
  48. #include <proto/exec.h>
  49. #include <proto/intuition.h>
  50. #include <proto/wb.h>
  51.  
  52. #include "bry/macros.h"
  53. #include "bry/clist.h"
  54. #include "bry/remind.h"
  55. #include "bry/misc.h"
  56. #include "bry/guido.h"
  57.  
  58. #include "player.h"
  59.  
  60. #define THEIGHT (10)
  61. #define ZHEIGHT (10+4+16*6+18*2+2)
  62.  
  63. #define ERRORLIFE 4 // How long an error stays in the title bar
  64.  
  65. struct Screen *playerscreen;
  66.  
  67. extern struct ExecBase *SysBase;
  68. extern char *modname, *modtypename, *authorname, **songcatalog;
  69.  
  70. extern GuidoSpec windowspec, windowfrspec;
  71. struct Gadget *windowmodgad, *windowtypegad, *windowauthorgad, *windowsonggad,
  72.   *windowvolumegad, *windowbalancegad, *windowbalanceresetgad,
  73.   *windowspeedgad, *windowjumpbackgad, *windowjumpforwardgad;
  74.  
  75. extern long songpos, songlen; // Displayed in the window titlebar
  76.  
  77. static struct DrawInfo *dri;
  78. static struct Screen *driscreen;
  79.  
  80. static struct AppWindow *awin;
  81.  
  82. static char * errmsg;
  83. static short errlife;
  84.  
  85. static char frclear;
  86. static void *ifr;
  87.  
  88. static long timer(void);
  89. static long end(void);
  90. static long check(long sigmask);
  91. static long update(void);
  92. static long close(int remake);
  93. static void snapshotwin(void);
  94. static void doremake(void);
  95. static void initfunc(void);
  96.  
  97. void windowsettingsupdate(void);
  98.  
  99. static struct RemindNode timernode = {{0},timer};
  100. static struct RemindNode updatenode = {{0},update};
  101. static struct RemindNode endnode = {{0},end};
  102. static struct RemindNode remakenode = {{0},doremake};
  103.  
  104. static struct MPWin mpwin = {windowspec,0L,initfunc,
  105.   {{0},snapshotwin,(long)&mpwin},
  106.   {{0},check,(long)&mpwin},
  107.   {{0},close,(long)&mpwin}};
  108.  
  109. void
  110. windowupdatetimer(void)
  111. {
  112.   extern struct TextAttr topaz8;
  113.   extern char *modname;
  114.   static struct IntuiText itext = {1,0,JAM2,0,1,&topaz8};
  115.   char buf[32];
  116.   struct Window *win;
  117.  
  118.   if(!(win = mpwin.win))
  119.     return;
  120.  
  121.   sysflags &= ~SF_TIMERUP;
  122.   if((showmode == SM_NOTHING) || (!modname) || (!win))
  123.     return;
  124.   if((songlen >= 0) && (showmode != SM_CLOCK))
  125.     {
  126.       sprintf(buf," %ld/%ld",songpos + (showmode - SM_SEQUENCE0),songlen);
  127.     }
  128.   else
  129.     {
  130.     extern long songtime;
  131.     long time = songtime/60;
  132.  
  133.       sprintf(buf," %ld:%02ld",time/60,time%60);
  134.     }
  135.   itext.IText = buf;
  136.   if(dri)
  137.     {
  138.       if(win->Flags & WFLG_WINDOWACTIVE)
  139.         {
  140.           itext.FrontPen = dri->dri_Pens[FILLTEXTPEN];
  141.           itext.BackPen = dri->dri_Pens[FILLPEN];
  142.         }
  143.       else
  144.         {
  145.           itext.FrontPen = dri->dri_Pens[TEXTPEN];
  146.           itext.BackPen = dri->dri_Pens[BACKGROUNDPEN];
  147.         }
  148.     }
  149.   PrintIText(win->RPort,&itext,win->Width-50-strlen(buf)*8,0);
  150. }
  151.  
  152.  
  153. static void updatetitle(void)
  154. {
  155.   extern char copyright[];
  156.  
  157.   if(!mpwin.win)
  158.     return;
  159.   SetWindowTitles(mpwin.win,errlife ? errmsg :
  160.                             modname ? modname : "MultiPlayer",copyright);
  161.   windowupdatetimer();
  162. }
  163.  
  164.  
  165. static long
  166. timer(void)
  167. {
  168.   if((errlife) && (--errlife == 0))
  169.     updatetitle();
  170.   else if((songlen < 0) || (showmode == SM_CLOCK))
  171.     windowupdatetimer();
  172.   return(0);
  173. }
  174.  
  175.  
  176. static long
  177. update(void)
  178. {
  179.   static char nosongstext[] = "";
  180.   static char *nosongslab[2] = {nosongstext,0};
  181.   extern long cursong;
  182.   struct Window *win;
  183.  
  184.   if(win = mpwin.win)
  185.     {
  186.       updatetitle();
  187.       GSetText(win,windowmodgad,modname);
  188.       GSetText(win,windowtypegad,modtypename);
  189.       GSetText(win,windowauthorgad,authorname);
  190.       GSetCycleOptions(win,windowsonggad,songcatalog ? songcatalog : nosongslab,cursong);
  191.       GEnDisGadgets(win,modtype & MTF_JUMP,
  192.         windowjumpbackgad,
  193.         windowjumpforwardgad,
  194.         0L);
  195.     }
  196.   return(0);
  197. }
  198.  
  199.  
  200. static void
  201. snapshotwin(void)
  202. {
  203.   extern short zoomwinleft, zoomwintop;
  204.   extern short tinywinleft, tinywintop;
  205.   struct Window *win = mpwin.win;
  206.  
  207.   if(win->Flags & WFLG_ZOOMED)
  208.     {
  209.       zoomwinleft = win->LeftEdge;
  210.       zoomwintop = win->TopEdge;
  211.     }
  212.   else
  213.     {
  214.       tinywinleft = win->LeftEdge;
  215.       tinywintop = win->TopEdge;
  216.     }
  217. }
  218.  
  219. static long
  220. close(int remake)
  221. {
  222.   char *windowopen(void);
  223.  
  224.   remind_rem(&timernode);
  225.   remind_rem(&updatenode);
  226.   remind_rem(&endnode);
  227.   if(dri)
  228.     {
  229.       FreeScreenDrawInfo(driscreen,dri);
  230.       dri = 0L;
  231.     }
  232.   if(awin)
  233.     {
  234.       RemoveAppWindow(awin);
  235.       awin = 0L;
  236.     }
  237.   closempwin(&mpwin);
  238.   if(remake)
  239.     showerr(openmpwin(&mpwin));
  240.   else
  241.     adddo(&endnode);
  242.   return(0);
  243. }
  244.  
  245. static long end(void)
  246. {
  247.   struct Screen *pubscreen;
  248.  
  249.   pubscreen = playerscreen, playerscreen = 0;
  250.   remind_callrem(&closelist,0);
  251.   if(pubscreen)
  252.     UnlockPubScreen(0L,pubscreen);
  253.   remind_rem(&remakenode);
  254.   return(0);
  255. }
  256.  
  257. static void
  258. doremake(void)
  259. {
  260.   struct RemindList templist;
  261.   struct RemindNode *node;
  262.  
  263.   remind_initlist(&templist);
  264.   while(node = clist_remhead(&closelist))
  265.     clist_addtail(&templist,node);
  266.   remind_callrem(&templist,1);
  267. }
  268.  
  269. void
  270. windowremake(void)
  271. {
  272.   remind_add(&dolist,&remakenode);
  273. }
  274.  
  275. static long
  276. frcheck(void)
  277. {
  278.   struct WBArg *args;
  279.   long count;
  280.  
  281.   if(ifr && ((count = GCheckRequester(ifr,&args)) >= 0))
  282.     {
  283.     extern short modfrleft;
  284.  
  285.       GGetRequesterLocation(ifr,&modfrleft);
  286.       if(count)
  287.         {
  288.           if(frclear)
  289.             progclear();
  290.           if(!showerr(progaddargs(count,args)))
  291.             if(frclear)
  292.               showerr(progstart());
  293.         }
  294.     }
  295.   return(0);
  296. }
  297.  
  298. static struct RemindNode frchecknode = {{0},frcheck};
  299.  
  300. static long
  301. frend(void)
  302. {
  303.   if(ifr)
  304.     {
  305.       while(GDeleteRequester(ifr))
  306.         {
  307.           static struct EasyStruct easy = {
  308.             sizeof(easy),0,"MultiPlayer","Please close all file and font requesters","OK"};
  309.  
  310.           EasyRequestArgs(0,&easy,0,0);
  311.         }
  312.       ifr = 0L;
  313.     }
  314.   remind_rem(&frchecknode);
  315.   return(0);
  316. }
  317.  
  318. static struct RemindNode frendnode = {{0},frend,-100};
  319.  
  320. static void
  321. addfrcalls(void)
  322. {
  323.   addcalls(&frchecknode,0,0,&frendnode);
  324. }
  325.  
  326. char *
  327. reqmod(int clear)
  328. {
  329.   struct {
  330.     struct Window *win;
  331.     char *oktext;
  332.     } params;
  333.   int rc;
  334.  
  335.   if(!mpwin.win)
  336.     return("Window not open");
  337.  
  338.   params.win = mpwin.win;
  339.   params.oktext = (frclear = clear) ? "Play" : "Add";
  340.   if(!ifr && !(ifr = GCreateRequester(windowfrspec)))
  341.     showerr("File requester unavailable");
  342.   else
  343.     {
  344.       addfrcalls();
  345.       if((rc = GOpenRequester(ifr,windowfrspec,¶ms)) == 0)
  346.         return("File requester in use");
  347.       else if(rc < 0)
  348.         return("Not enough memory");
  349.     }
  350.   return(0);
  351. }
  352.  
  353. void
  354. windowsettingsupdate(void)
  355. {
  356.   extern short mspeed, mvolume, mbalance;
  357.   int endis;
  358.   struct Window * win = mpwin.win;
  359.  
  360.   if(!win)
  361.     return;
  362.   endis = 0;
  363.   if((!curmod) || (curmod->volume >= 0))
  364.     {
  365.       GSetSliderLevel(win,windowvolumegad,mvolume);
  366.       GSetSliderLevel(win,windowbalancegad,mbalance);
  367.       endis = 1;
  368.     }
  369.   GEnDisGadgets(win,endis,
  370.     windowvolumegad,
  371.     windowbalancegad,
  372.     windowbalanceresetgad,
  373.     0L);
  374.  
  375.   endis = 0;
  376.   if((!curmod) || (curmod->speed))
  377.     GSetSliderLevel(win,windowspeedgad,mspeed),
  378.     endis = 1;
  379.   GEnDisGadget(win,endis,windowspeedgad);
  380. }
  381.  
  382. void
  383. gui_windowsong(struct GuidoMessage *im)
  384. {
  385.   setsong(im->Code);
  386. }
  387.  
  388. void
  389. gui_windowvolume(struct GuidoMessage *gm)
  390. {
  391.   extern void playervolume(void);
  392.   extern short mvolume;
  393.  
  394.   mvolume = gm->Code;
  395.   playervolume();
  396. }
  397.  
  398. void
  399. gui_windowbalance(struct GuidoMessage *gm)
  400. {
  401.   extern void playervolume(void);
  402.   extern short mbalance;
  403.  
  404.   mbalance = gm->Code;
  405.   playervolume();
  406. }
  407.  
  408. void
  409. gui_windowbalancereset(void)
  410. {
  411.   extern void playervolume(void);
  412.   extern short mbalance;
  413.  
  414.   mbalance = 0;
  415.   GSetSliderLevel(mpwin.win,windowbalancegad,0);
  416.   playervolume();
  417. }
  418.  
  419. void
  420. gui_windowspeed(struct GuidoMessage *gm)
  421. {
  422.   extern void playerspeed(void);
  423.  
  424.   mspeed = gm->Code;
  425.   playerspeed();
  426. }
  427.  
  428. void
  429. gui_windoweject(void)
  430. {
  431.   endmod();
  432.   GlobSetLong(&curmod,0L,0);
  433. }
  434.  
  435. void
  436. gui_windownext(void)
  437. {
  438.   showerr(prognext());
  439. }
  440.  
  441. void
  442. gui_windowprev(void)
  443. {
  444.   showerr(progprev());
  445. }
  446.  
  447. void
  448. gui_windownew(void)
  449. {
  450.   showerr(reqmod(1));
  451. }
  452.  
  453. void
  454. windowerror(char *mes)
  455. {
  456.   if(mpwin.win)
  457.     {
  458.       errmsg = mes;
  459.       errlife = ERRORLIFE;
  460.       updatetitle();
  461.     }
  462. }
  463.  
  464.  
  465. /* Zoom/unzoom the main window */
  466. static void
  467. windowzoom(void)
  468. {
  469.   if(mpwin.win)
  470.     ZipWindow(mpwin.win);
  471. }
  472.  
  473. void
  474. gui_windowzoom(void)
  475. {
  476.   windowzoom();
  477. }
  478.  
  479.  
  480. /* Handle button-down events from all MultiPlayer windows */
  481. void
  482. windowclick(int code)
  483. {
  484.   if(code == MENUDOWN)
  485.     showerr(progwinopenclose());
  486.   if(code == MIDDLEDOWN)
  487.     windowzoom();
  488. }
  489.  
  490.  
  491. /* Guido default callback function, called from within check() */
  492. static void
  493. callback(struct GuidoMessage *im)
  494. {
  495.   switch(im->Class)
  496.     {
  497.       case IDCMP_ACTIVEWINDOW:
  498.       case IDCMP_INACTIVEWINDOW:
  499.       case IDCMP_REFRESHWINDOW:
  500.         windowupdatetimer();
  501.         break;
  502.       case IDCMP_CHANGEWINDOW:
  503.         snapshotwin();
  504.         break;
  505.     }
  506.   standardcallback(im);
  507. }
  508.  
  509.  
  510. /* Window-check called from CheckList */
  511. static long
  512. check(long sigmask)
  513. {
  514.   if((sigmask & mpwin.sigmask) && GCheckPanel(mpwin.win,callback,0L))
  515.     {
  516.     extern struct Library *CxBase;
  517.     extern char hotkey[];
  518.  
  519.       if((hotkey[0]) && (CxBase))
  520.         windowclose();
  521.       else
  522.         sysflags |= SF_KILL;
  523.     }
  524.   return(0);
  525. }
  526.  
  527.  
  528. /* Called from openmpwin() for initialization */
  529. static void
  530. initfunc(void)
  531. {
  532.   remind_rem(&remakenode);
  533.   if(mpwin.win)
  534.     {
  535.       dri = GetScreenDrawInfo(driscreen = mpwin.win->WScreen);
  536.       remind_add(&timerlist,&timernode);
  537.       remind_add(&updatelist,&updatenode);
  538.       remind_add(&endlist,&endnode);
  539.       if(wbappport)
  540.         awin = AddAppWindowA(1,0,mpwin.win,wbappport,0);
  541.       update();
  542.       windowsettingsupdate();
  543.     }
  544. }
  545.  
  546.  
  547. /* Close the main window and all other Workbench-style windows */
  548. void
  549. windowclose(void)
  550. {
  551.   setfinmpwin(&mpwin);
  552. }
  553.  
  554.  
  555. /* Open the main MultiPlayer window (and any flashy window currently enabled),
  556.    or simply activates it if it's already open.  */
  557. char *
  558. windowopen(void)
  559. {
  560.   extern short tinywinleft;
  561.   extern char flashflags;
  562.   extern char *flashywinopenclose(void);
  563.   char *err = 0;
  564.  
  565.   if(mpwin.win)
  566.     {
  567.       WindowToFront(mpwin.win);
  568.       ActivateWindow(mpwin.win);
  569.       return(0L);
  570.     }
  571.  
  572.   /* User interface only works with 2.04+ */
  573.   if(SysBase->LibNode.lib_Version < 37)
  574.     return("Kickstart 2.04 or later required");
  575.  
  576.   if(!playerscreen
  577.      && !(playerscreen = LockPubScreen(argarray.screen)))
  578.     return("Public screen unavailable");
  579.   if(tinywinleft < 0)
  580.     tinywinleft = playerscreen->Width-23-320;
  581.  
  582.   /* First open the flashy windows */
  583.   if(flashflags)
  584.     err = flashywinopenclose();
  585.  
  586.   /* Then open the main control panel (on top) */
  587.   return(err ? err : openmpwin(&mpwin));
  588. }
  589.