home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / maestro / source / cdedit / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-15  |  14.9 KB  |  448 lines

  1. /*
  2.  * Copyright (c) 1990, 1991 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and 
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name
  8.  * Stanford may not be used in any advertising or publicity relating to
  9.  * the software without the specific, prior written permission of
  10.  * Stanford.
  11.  * 
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  13.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  14.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  15.  *
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
  19.  * ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
  20.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21.  * SOFTWARE.
  22.  */
  23.  
  24. /* $Header: /Source/Media/collab/cdEdit/RCS/misc.c,v 2.10 92/07/17 16:33:48 drapeau Exp $ */
  25. /* $Log:    misc.c,v $
  26.  * Revision 2.10  92/07/17  16:33:48  drapeau
  27.  * Minor change to ConvertRelativeToAbsolute(), made overflow code
  28.  * more robust (previous code assumed overflow would be less than
  29.  * one second; new code can handle overflow of any number of
  30.  * seconds).
  31.  * 
  32.  * Revision 2.0  91/10/06  21:01:45  chua
  33.  * Update to version 2.0
  34.  * 
  35.  * Revision 1.23  91/09/24  16:32:58  chua
  36.  * The duration array stores time in milliseconds instead of seconds.  So, in line
  37.  * 90, 97, do the appropriate conversions for the duration.
  38.  * 
  39.  * Revision 1.22  91/09/18  17:27:00  chua
  40.  * Do proper casting of assignment statements to be ANSI-compliant.
  41.  * 
  42.  * Revision 1.21  91/09/03  15:19:10  chua
  43.  * Added the copyright header.
  44.  * 
  45.  * In the SetDuration function, take a parameter, whichEdit, so that the duration for that
  46.  * edit will be properly stored in the duration array.
  47.  * 
  48.  * All constants, which were previously all capitalized, are now only capitalized in the
  49.  * first letter.
  50.  * 
  51.  * Deleted the DiscNotEjected function, as there is a variable, DiscInPlayer, which keeps
  52.  * track of whether there is a disc currently in the player.
  53.  * 
  54.  * Replaced the calls to notice_prompt by a call to AlertMessage (to make things shorter
  55.  * and tidier).
  56.  * 
  57.  * Revision 1.2  91/07/10  11:04:09  chua
  58.  * Removed a debugging printf statement.
  59.  * 
  60.  * Revision 1.1  91/07/09  16:47:51  chua
  61.  * 
  62.  * 
  63.  * Revision 1.0  91/07/08  13:46:16  chua
  64.  * Initial revision
  65.  *  */
  66.  
  67. static char miscrcsid[] = "$Header: /Source/Media/collab/cdEdit/RCS/misc.c,v 2.10 92/07/17 16:33:48 drapeau Exp $";
  68.  
  69. #include "main.h"
  70.  
  71. int fd;                                    /* file descriptor to the port */
  72. Toc toc;                                /* table of contents */
  73. static int discData;
  74.  
  75. /*
  76.  * This function displays the approximate duration of the current selection 
  77.  */
  78. int SetDuration(whichEdit)
  79.      int whichEdit;
  80. {
  81.   int calculatedDuration;
  82.   Msf absolutestart, absoluteend;
  83.   Msf difference;
  84.   char buf[80];
  85.  
  86.   absolutestart = (Msf) GetCurrentStart();                /* Convert to absolute values first */
  87.   if (absolutestart == NULL) 
  88.   {
  89.     return Error;
  90.   }
  91.   absoluteend = (Msf) GetCurrentEnd();
  92.   if (absoluteend == NULL) 
  93.   {
  94.     return Error;
  95.   }
  96.   if (CheckSelection(absolutestart, absoluteend) == Error)        /* Check if the end time is greater than the start time */
  97.   {
  98.     return Error;
  99.   }
  100.   else                                    /* Calculate the approximate duration */
  101.   {
  102.     difference = diff_msf(absoluteend, absolutestart);
  103.     calculatedDuration = (difference->min * 60 * 1000) + 
  104.         (difference->sec * 1000) + (difference->frame * 1000 /75);
  105.     if (whichEdit != -1) 
  106.     {
  107.       duration[whichEdit] = calculatedDuration;
  108.     }
  109.   }
  110.   sprintf(buf, "%d", (calculatedDuration + 500) / 1000);
  111.   xv_set(cdEdit_EditPopup->EditPopupDurationText, PANEL_VALUE, buf, NULL); /* Display the duration in the duration textfield */
  112.   return OK;
  113. }
  114.  
  115. /*
  116.  * This function will read the table of contents off the CD if possible.  The table of contents is read into the appropriate data structure.
  117.  * This code is obtained from the cdrom driver provided by Sun Microsystems.
  118.  */
  119. Toc ReadDiscToc()
  120. {
  121.   struct  cdrom_tochdr    hdr;
  122.   struct  cdrom_tocentry  entry;
  123.   int     firstTrack;
  124.   int     lastTrack;
  125.   int     i;
  126.   Toc     toc;
  127.   Msf     msf, tempMsf;
  128.   int     control;
  129.   
  130.   if (cdrom_read_tochdr(fd, &hdr) == 0)
  131.   {
  132.     return (Toc)NULL;
  133.   }
  134.   firstTrack = hdr.cdth_trk0;
  135.   lastTrack = hdr.cdth_trk1;
  136.   
  137.   entry.cdte_format = CDROM_MSF;
  138.   entry.cdte_track = CDROM_LEADOUT;
  139.   cdrom_read_tocentry(fd, &entry);
  140.   msf = init_msf();
  141.   msf->min = entry.cdte_addr.msf.minute;
  142.   msf->sec = entry.cdte_addr.msf.second;
  143.   msf->frame = entry.cdte_addr.msf.frame;
  144.   
  145.   toc = init_toc(firstTrack, lastTrack, msf);
  146.   
  147.   tempMsf = init_msf();
  148.   entry.cdte_format = CDROM_MSF;
  149.   discData = 0;        
  150.   for (i=firstTrack; i<=lastTrack; i++) 
  151.   {
  152.     entry.cdte_track = i;
  153.     cdrom_read_tocentry(fd, &entry);
  154.     msf = init_msf();
  155.     msf->min = entry.cdte_addr.msf.minute;
  156.     msf->sec = entry.cdte_addr.msf.second;
  157.     msf->frame = entry.cdte_addr.msf.frame;
  158.     control = entry.cdte_ctrl;
  159.     if (control & CDROM_DATA_TRACK) 
  160.     {
  161.       discData = 1;
  162.     }
  163.     entry.cdte_track = i + 1;
  164.     if (entry.cdte_track > (unsigned char) lastTrack) 
  165.     {
  166.       entry.cdte_track = CDROM_LEADOUT;
  167.     }
  168.     entry.cdte_format = CDROM_MSF;
  169.     cdrom_read_tocentry(fd, &entry);
  170.     tempMsf->min = entry.cdte_addr.msf.minute;
  171.     tempMsf->sec = entry.cdte_addr.msf.second;
  172.     tempMsf->frame = entry.cdte_addr.msf.frame;
  173.     add_track_entry(toc, i, control,
  174.             msf, tempMsf);
  175.   }
  176.   return (toc);
  177. }
  178.  
  179. /*
  180.  * Get the current track that is currently playing on the disc.
  181.  */
  182. int GetCurrentTrack()
  183. {
  184.   int currentTrack, dummy1, dummy2, dummy3;
  185.   
  186.   if (playerState == StopMode) 
  187.   {
  188.     currentTrack = 0;
  189.   }
  190.   else 
  191.   {
  192.     cdrom_get_relinfo(fd, ¤tTrack, &dummy1, &dummy2, &dummy3);
  193.   }
  194.   return (currentTrack);
  195. }
  196.  
  197. /*
  198.  * Compare two msf values.  Return Equal if they are the same, Greater if the first is 
  199.  * greater than the second, and Less if the second is greater than the first 
  200.  */
  201. int CompareMsf(first, second)
  202.      Msf first, second;
  203. {
  204.   if (first->min == second->min && first->sec == second->sec &&
  205.       first->frame == second->frame) 
  206.   {
  207.     return Equal;
  208.   }
  209.   if ((first->min > second->min) || 
  210.       (first->min == second->min && first->sec > second->sec) ||
  211.       (first->min == second->min && first->sec == second->sec 
  212.        && first->frame > second->frame)) 
  213.   {
  214.     return Greater;
  215.   }
  216.   return Less;
  217. }
  218.  
  219. /* 
  220.  * This function checks if a selection (given by start and end times) is valid.  It first checks that each time is valid (falls within the total time
  221.  * of the disc.  It then checks to see that the end time is greater than the start time.
  222.  */
  223. int CheckSelection(start, end)
  224.      Msf start, end;
  225. {
  226.   Msf zeroMsf;
  227.  
  228.   if (start == NULL || end == NULL) 
  229.   {
  230.     return Error;
  231.   }
  232.   zeroMsf = (Msf) malloc(sizeof(struct msf));
  233.   zeroMsf->min = 0;
  234.   zeroMsf->sec = 0;
  235.   zeroMsf->frame = 0;
  236.   
  237.   if (CompareMsf(start, zeroMsf) == Equal ||                /* Check that the times are valid (falls within the disc times */
  238.       CompareMsf(end, zeroMsf) == Equal || 
  239.       CompareMsf(start, toc->total_msf) == Greater || 
  240.       CompareMsf(end, toc->total_msf) == Greater) 
  241.   { 
  242.     AlertMessage("Invalid time specifed in current selection.", NULL);
  243.     free(zeroMsf);
  244.     return Error;
  245.   }
  246.   free(zeroMsf);
  247.   if (CompareMsf(start, end) == Greater)                /* Check that the end time is greater than the start time */
  248.   {
  249.     AlertMessage("Invalid current selection:",
  250.          "Start time is greater than end time.");
  251.     return Error;
  252.   }
  253.   return OK;
  254. }
  255.  
  256. /*
  257.  * This function converts a relative msf to absolute msf. 
  258.  */
  259. Msf ConvertRelativeToAbsolute(track, msf)
  260.      int track;
  261.      Msf msf;
  262. {
  263.   Msf startMsf, resultMsf;
  264.   
  265.   if ((msf->min == 0 && msf->sec == 0 && msf->frame == 0) ||        /* Check that the time is valid */
  266.       (track < startTrack || track > endTrack)) 
  267.   {
  268.     AlertMessage("Illegal time specified in current selection.", NULL);
  269.     return NULL;
  270.   }
  271.   resultMsf = (Msf) malloc(sizeof(struct msf));                /* Initialize the absolute msf */
  272.   resultMsf->min = 0;
  273.   resultMsf->sec = 0;
  274.   resultMsf->frame = 0;
  275.   startMsf = get_track_msf(toc, track);
  276.   
  277.   resultMsf->frame = msf->frame + startMsf->frame;            /* Perform the conversion */
  278.   while (resultMsf->frame >= 75) 
  279.   {
  280.     resultMsf->frame -= 75;
  281.     resultMsf->sec ++;
  282.   }
  283.   resultMsf->sec += msf->sec + startMsf->sec;
  284.   while (resultMsf->sec >= 60) 
  285.   {
  286.     resultMsf->sec -= 60;
  287.     resultMsf->min ++;
  288.   }
  289.   resultMsf->min += msf->min + startMsf->min;
  290.   if (CompareMsf(resultMsf, toc->total_msf) == Greater)            /* Check that the absolute msf does not exceed the total time on the disc */
  291.   {
  292.     AlertMessage("Illegal time specified in current selection.", NULL);
  293.     return NULL;
  294.   }
  295.   return (resultMsf);
  296. }
  297.  
  298. /*
  299.  * Get the current start selection from the current selection textfields and return it as a msf structure.
  300.  */
  301. Msf GetCurrentStart()
  302. {
  303.   int track;
  304.   Msf relative, result;
  305.   
  306.   track = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupStartTrackText, PANEL_VALUE));
  307.   relative = (Msf) malloc(sizeof(struct msf));
  308.   relative->min = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupStartMinText, PANEL_VALUE));
  309.   relative->sec = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupStartSecText, PANEL_VALUE));
  310.   relative->frame = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupStartFrameText, PANEL_VALUE));
  311.   result = ConvertRelativeToAbsolute(track, relative);
  312.   free(relative);
  313.   return(result);
  314. }
  315.  
  316. /*
  317.  * Get the current end selection from the current selection textfields and return it as a msf structure.
  318.  */
  319. Msf GetCurrentEnd()
  320. {
  321.   int track;
  322.   Msf relative, result;
  323.   
  324.   track = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupEndTrackText, PANEL_VALUE));
  325.   relative = (Msf) malloc(sizeof(struct msf));
  326.   relative->min = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupEndMinText, PANEL_VALUE));
  327.   relative->sec = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupEndSecText, PANEL_VALUE));
  328.   relative->frame = (int) atoi(xv_get(cdEdit_EditPopup->EditPopupEndFrameText, PANEL_VALUE));
  329.   result = ConvertRelativeToAbsolute(track, relative);
  330.   free(relative);
  331.   return (result);
  332. }
  333.  
  334. /*
  335.  * Get the start and end times of the current selection and place them in the edit list entry given by the parameter num.
  336.  */
  337. void GetStartEnd(num)
  338.      int num;
  339. {
  340.   starttrack[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupStartTrackText, PANEL_VALUE)); /* Get the start time */
  341.   startmin[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupStartMinText, PANEL_VALUE));
  342.   startsec[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupStartSecText, PANEL_VALUE));
  343.   startframe[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupStartFrameText, PANEL_VALUE));
  344.  
  345.   endtrack[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupEndTrackText, PANEL_VALUE)); /* Get the end time */
  346.   endmin[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupEndMinText, PANEL_VALUE));
  347.   endsec[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupEndSecText, PANEL_VALUE));
  348.   endframe[num] = (int) atoi(xv_get (cdEdit_EditPopup->EditPopupEndFrameText, PANEL_VALUE));
  349.  
  350.   strcpy (label[num], (char *) xv_get (cdEdit_EditPopup->EditPopupLabelText, /* Get the label */
  351.                        PANEL_VALUE));
  352.  
  353.   volume[num] = xv_get(cdEdit_window1->VolumeSlider, PANEL_VALUE);
  354.   balance[num] = xv_get(cdEdit_window1->BalanceSlider, PANEL_VALUE);
  355. }
  356.  
  357. /*
  358.  * Load the start and end times of the currently selected entry in the edit list onto the current selection textfields.
  359.  */
  360. void LoadStartEnd()
  361. {
  362.   char e[10], t[2], m[2], s[2], f[2];
  363.   
  364.   sprintf(e, "Current Selection : %d", editnum+1);            /* Update the edit status information */
  365.   xv_set(cdEdit_EditPopup->CurrentSelectionMsg, PANEL_LABEL_STRING, e, NULL);
  366.   
  367.   sprintf(t, "%d", starttrack[editnum]);                /* Load the start and end times onto the current selection textfields */
  368.   sprintf(m, "%d", startmin[editnum]);
  369.   sprintf(s, "%d", startsec[editnum]);
  370.   sprintf(f, "%d", startframe[editnum]);
  371.   xv_set(cdEdit_EditPopup->EditPopupStartTrackText, PANEL_VALUE,
  372.      t, NULL);
  373.   xv_set(cdEdit_EditPopup->EditPopupStartMinText, PANEL_VALUE,
  374.      m, NULL);
  375.   xv_set(cdEdit_EditPopup->EditPopupStartSecText, PANEL_VALUE,
  376.      s, NULL);
  377.   xv_set(cdEdit_EditPopup->EditPopupStartFrameText, PANEL_VALUE,
  378.      f, NULL);
  379.   sprintf(t, "%d", endtrack[editnum]);
  380.   sprintf(m, "%d", endmin[editnum]);
  381.   sprintf(s, "%d", endsec[editnum]);
  382.   sprintf(f, "%d", endframe[editnum]);
  383.   xv_set(cdEdit_EditPopup->EditPopupEndTrackText, PANEL_VALUE,
  384.      t, NULL);
  385.   xv_set(cdEdit_EditPopup->EditPopupEndMinText, PANEL_VALUE,
  386.      m, NULL);
  387.   xv_set(cdEdit_EditPopup->EditPopupEndSecText, PANEL_VALUE,
  388.      s, NULL);
  389.   xv_set(cdEdit_EditPopup->EditPopupEndFrameText, PANEL_VALUE,
  390.      f, NULL);
  391.   xv_set(cdEdit_EditPopup->EditPopupLabelText, PANEL_VALUE, label[editnum], /* Load the label */
  392.      NULL);
  393.  
  394.   if (discInPlayer == 1) 
  395.   {
  396.     xv_set(cdEdit_EditPopup->ModifyButton, PANEL_INACTIVE, FALSE, NULL); /* Make the modify and delete buttons active */
  397.     xv_set(cdEdit_EditPopup->DeleteButton, PANEL_INACTIVE, FALSE, NULL); /* only if there is a disc in the player */
  398.   }
  399.   xv_set(cdEdit_window1->VolumeSlider, PANEL_VALUE, volume[editnum], NULL);
  400.   xv_set(cdEdit_window1->BalanceSlider, PANEL_VALUE, balance[editnum], NULL);
  401.   SetVolume(volume[editnum], balance[editnum]);
  402.   
  403. }
  404.  
  405. /*
  406.  * Notify callback function for `GetStartButton'.
  407.  * Loads the current playing time on the disc to the start time textfield in the current selection area.
  408.  */
  409. void GetStart(item, event)
  410.      Panel_item    item;
  411.      Event        *event;
  412. {
  413.   int track, min, sec, frame;
  414.   char t[2], m[2], s[2], f[2];
  415.   
  416.   cdrom_get_relinfo(fd, &track, &min, &sec, &frame);            /* Get the current playing time */
  417.   sprintf (t, "%d", track);                        /* Set the values in the start time textfield */
  418.   sprintf (m, "%d", min);
  419.   sprintf (s, "%d", sec);
  420.   sprintf (f, "%d", frame);
  421.   xv_set(cdEdit_EditPopup->EditPopupStartTrackText, PANEL_VALUE, t,  NULL);
  422.   xv_set(cdEdit_EditPopup->EditPopupStartMinText, PANEL_VALUE, m,  NULL);
  423.   xv_set(cdEdit_EditPopup->EditPopupStartSecText, PANEL_VALUE, s,  NULL);
  424.   xv_set(cdEdit_EditPopup->EditPopupStartFrameText, PANEL_VALUE, f,  NULL);
  425. }
  426.  
  427. /*
  428.  * Notify callback function for `GetEndButton'.
  429.  * Loads the current playing time on the disc to the end time textfield in the current selection area.
  430.  */
  431. void GetEnd(item, event)
  432.      Panel_item    item;
  433.      Event        *event;
  434. {
  435.   int track, min, sec, frame;
  436.   char t[2], m[2], s[2], f[2];
  437.   
  438.   cdrom_get_relinfo(fd, &track, &min, &sec, &frame);        /* Get the current playing time */
  439.   sprintf (t, "%d", track);                    /* Set the values in the end time textfield */
  440.   sprintf (m, "%d", min);
  441.   sprintf (s, "%d", sec);
  442.   sprintf (f, "%d", frame);
  443.   xv_set(cdEdit_EditPopup->EditPopupEndTrackText, PANEL_VALUE, t,  NULL);
  444.   xv_set(cdEdit_EditPopup->EditPopupEndMinText, PANEL_VALUE, m,  NULL);
  445.   xv_set(cdEdit_EditPopup->EditPopupEndSecText, PANEL_VALUE, s,  NULL);
  446.   xv_set(cdEdit_EditPopup->EditPopupEndFrameText, PANEL_VALUE, f,  NULL);
  447. }
  448.