File: commands.c.orig

package info (click to toggle)
cdtool 2.1.5-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 284 kB
  • ctags: 176
  • sloc: ansic: 2,216; makefile: 167; sh: 121
file content (342 lines) | stat: -rw-r--r-- 8,403 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* "commands.c" copyright 1994 thomas insel
 *              copyright 1995 sven oliver moll */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>

#ifdef sun
#  include <sundev/srreg.h>
#elif defined linux
#  include <linux/cdrom.h>
#else
#  error Please fix includes for your system in commands.c
#endif

#include "config.h"
#include "commands.h"
#include "hardware.h"
#include "shuffle.h"
#include "main.h"

char    *cd_device;

/************************************************************************/
/* Procedure:  do_play
 * Purpose:    to start playing
 * 
 * Inputs:     program name, CD file des., initial track, last track
 * Outputs:    to CD-ROM, to stdout
 * Returns:    n/a
 * Notes:  
 *   1.
 */
/************************************************************************/
/* if trk0 is -1, skip back
 * if trk0 is -2, skip forward
 */
void do_play(char *progname, int cdfile, int trk0, int trk1, int iVerbose)
{
  struct cdrom_tochdr tochdr;
  struct cdrom_ti ti;
  struct cdrom_subchnl subchnl;

#ifdef DEBUG
fprintf (stderr,"do_play:  called, name=%s, cdfile=%d, trk0=%d, trk1=%d\n",
	 progname, cdfile, trk0, trk1);
#endif

  if (iVerbose == TRUE)
      printf ("Playing from track %d to %d\n", trk0, trk1);

  subchnl.cdsc_format = CDROM_MSF;
  if ( (( trk0 == 0 ) && ( trk1 == 0 )) || ( trk0 < 0 ) )
    {
#ifdef DEBUG
	fprintf (stderr, "do_play:  trk0=%d\n", trk0);
#endif

      if ( ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1 )
	{
	  fprintf(stderr, "%s: ioctl cdromsubchnl\n", progname);
	  EXIT(1);
        }
    }

  /* if starting play.... */
  if ( ( trk0 == 0 ) && ( trk1 == 0 ) )
    {
#ifdef DEBUG
	fprintf (stderr, "do_play:  trk[0.1]==0, status=%s\n",
		 (subchnl.cdsc_audiostatus == CDROM_AUDIO_PAUSED)?
		 "PAUSED":"PLAY");
#endif

	/* subchannel status returns PLAY, PAUSED, COMPLETED, or none */
	switch ( subchnl.cdsc_audiostatus ) {
	case CDROM_AUDIO_PAUSED:
	    if ( ioctl(cdfile, CDROMRESUME) == -1 ) {
		fprintf(stderr, "%s: ioctl cdromresume\n", progname);
		EXIT(1);
	    }
	    return;
	case CDROM_AUDIO_PLAY:
	    trk0=subchnl.cdsc_trk;
	    break;
	case CDROM_AUDIO_COMPLETED:  /* WWH added */
	    trk0=1;
	    break;
#ifdef USE_THIS
	default:
	    fprintf(stderr, "%s: error subchnl=%d\n", 
		    progname, subchnl.cdsc_audiostatus);
	    EXIT(1);
#endif	    
	}
    }
  

  if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 ) 
    {
      fprintf(stderr, "%s: ioctl cdromreadtochdr\n", progname);
      EXIT(1);
    }

  /* handle previous and next track */
  if ( trk0 < 0 )
    {
      if ( trk1 == 0 )
      	trk1=1;
      switch (trk0)
	{
	 case SKIPBACK:
	  trk1=subchnl.cdsc_trk-trk1;
	  break;
	 case SKIPFORWARD:
	  trk1=subchnl.cdsc_trk+trk1;
	}
      if ((trk1 < tochdr.cdth_trk0) || (trk1 > tochdr.cdth_trk1))
      	trk1=1;
      ti.cdti_trk0 = trk1;
      ti.cdti_trk1 = tochdr.cdth_trk1;
    }
  else
    {
      if ( trk0 ) 
      	ti.cdti_trk0 = trk0;
      else
      	ti.cdti_trk0 = tochdr.cdth_trk0;
      
      if ( trk1 )
      	ti.cdti_trk1 = trk1;
      else
      	ti.cdti_trk1 = tochdr.cdth_trk1;
    }
  
  ti.cdti_ind0 = 1;
  ti.cdti_ind1 = 99;
  
  /* check range */
#ifdef DEBUG
  fprintf (stderr, "do_play:  ti.cdti_trk0=%d, ti.cdti_trk1=%d\n",
      	ti.cdti_trk0, ti.cdti_trk1);
#endif

#ifndef SCSI
  if ( ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1 ) 
    {
      /* patch by <Ingo.Wilken@Informatik.Uni-Oldenburg.DE> for Plextor */
      /* PX-32TSI drive */
      /* retry with all indices set to 1 */
      ti.cdti_ind1 = 1;
      if ( ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1 )
      {
      	fprintf(stderr, "%s: ioctl cdromplaytrkind\n", progname);
      	EXIT(1);
      }
    }
#else
  /* SCSI - convert track index into MSF format */
  {
      cdhw_t *hw;
      struct cdrom_msf msf;
      int i;
      
      hw = read_hw(progname, cdfile, cd_device);
      if (hw == NULL)
	  EXIT(1);
      
      if ( ioctl(cdfile, CDROMSTART) == -1 ) {
	  fprintf(stderr, "%s: ioctl cdromstart\n", progname);
	  free_hw_buf(hw);
	  EXIT(1);
      }

      /* convert track offset to MSF format */
      i=ti.cdti_trk0;
      if (i >hw->tochdr.cdth_trk1) {
	  fprintf(stderr, "%s: invalid track %d\n", progname, i);
	  free_hw_buf(hw);
	  EXIT(1);
      }
	  
      msf.cdmsf_min0=hw->tocentries[i-1].cdte_addr.msf.minute;
      msf.cdmsf_sec0=hw->tocentries[i-1].cdte_addr.msf.second;
      msf.cdmsf_frame0=hw->tocentries[i-1].cdte_addr.msf.frame;

      i=ti.cdti_trk1;
      if (i == 0)
	  i = hw->tochdr.cdth_trk1+1;
      else
	  i++;
      msf.cdmsf_min1=hw->tocentries[i-1].cdte_addr.msf.minute;
	  msf.cdmsf_sec1=hw->tocentries[i-1].cdte_addr.msf.second;
      msf.cdmsf_frame1=hw->tocentries[i-1].cdte_addr.msf.frame;
      
      free_hw_buf(hw);
      if ( ioctl(cdfile, CDROMPLAYMSF, &msf) == -1 ) {
	  fprintf(stderr, "%s: ioctl cdrommsf\n", progname);
	  EXIT(1);
      }
  }
#endif
}

/************************************************************************/
/* Procedure:  do_pause
 * Purpose:    to pause CD-ROM
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    n/a
 * Notes:  
 *   1.
 */
/************************************************************************/
void do_pause(char *progname, int cdfile, int iVerbose) 
{
#ifdef DEBUG
fprintf (stderr,"do_pause:  called, name=%s, cdfile=%d,\n",
	 progname, cdfile);
#endif
  if ( ioctl(cdfile, CDROMPAUSE) == -1 ) 
      {
	  fprintf(stderr, "%s: ioctl cdrompause\n", progname);
	  EXIT(1);
      }
  if (iVerbose == TRUE)
      printf ("Pausing\n");
}

/************************************************************************/
/* Procedure:  do_stop
 * Purpose:    to stop playing a CD-ROM
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    n/a
 * Notes:  
 *   1.
 */
/************************************************************************/
void do_stop(char *progname, int cdfile, int iCDNum, int iVerbose) 
{
    char  caBuf[40];
    FILE  *fpPID;
    pid_t shuffle_pid;

#ifdef DEBUG
    fprintf (stderr,"do_stop:  called, name=%s, cdfile=%d,\n",
	     progname, cdfile);
#endif

    /* if shuffle is running, stop it... */
    sprintf (caBuf, "%s%d", PID_FILE, iCDNum);
    fpPID = fopen (caBuf, "r");
    if (fpPID == NULL) {
	if (iVerbose == TRUE) {
	    fprintf(stderr,"%s:  Error opening pid file %s\n", 
		    progname, caBuf);
	}
    } else {
	if (fgets (caBuf, sizeof(caBuf), fpPID) != NULL) {
	    shuffle_pid = atoi (caBuf);
	    kill (shuffle_pid, SIGINT);
	}
	fclose (fpPID);
    }
    
    /* send stop to the CDROM device */
    if ( ioctl(cdfile, CDROMSTOP) == -1 ) {
	fprintf(stderr, "%s: ioctl cdromstop\n", progname);
	EXIT(1);
    }

    if (iVerbose == TRUE)
	printf ("Stopping\n");
}

/************************************************************************/
/* Procedure:  do_eject
 * Purpose:    to eject the CD-ROM 
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    n/a
 * Notes:  
 *   1.
 */
/************************************************************************/
void do_eject(char *progname, int cdfile, int iVerbose)
{
#ifdef DEBUG
fprintf (stderr,"do_eject:  called, name=%s, cdfile=%d,\n",
	 progname, cdfile);
#endif
  if ( ioctl(cdfile, CDROMEJECT) == -1 ) 
    {
      fprintf(stderr, "%s: ioctl cdromeject\n", progname);
      EXIT(1);
    }

    if (iVerbose == TRUE)
	printf ("Ejecting CD-ROM\n");
}

/************************************************************************/
/* Procedure:  do_reset
 * Purpose:    to reset the CD-ROM
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    n/a
 * Notes:  
 *   1.
 */
/************************************************************************/
void do_reset(char *progname, int cdfile, int iVerbose)
{
#ifdef DEBUG
fprintf (stderr,"do_reset:  called, name=%s, cdfile=%d,\n",
	 progname, cdfile);
#endif
  if ( ioctl(cdfile, CDROMRESET) == -1 ) 
    {
      fprintf(stderr, "%s: ioctl cdromreset\n", progname);
      EXIT(1);
    }
  if ( ioctl(cdfile, CDROMSTART) == -1 ) 
    {
      fprintf(stderr, "%s: ioctl cdromstart\n", progname);
      EXIT(1);
    }

  if (iVerbose == TRUE)
      printf ("Reseting CDROM\n");
}