File: commands.c

package info (click to toggle)
cdtool 2.1.8-release-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: ansic: 6,525; sh: 2,823; makefile: 143
file content (396 lines) | stat: -rw-r--r-- 11,056 bytes parent folder | download | duplicates (3)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* commands.c 
 *
 * Copyright 1994 thomas insel
 * Copyright 1995 sven oliver moll
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 dated June, 1991.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

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

#include "config.h"
#include "cdtool.h"
#include "commands.h"
#include "hardware.h"
#include "shuffle.h"
#include "util.h"
#include "info.h"

/************************************************************************
 * Procedure:  cdtool_open
 * Purpose:    finds device, opens it, allocates and reads *hw.
 * Returns:
 *  -1     failure
 *   0     success
 ************************************************************************/

int cdtool_open(cdtool_t *cdtool, char *device)
{
    int fd;
    char *cddev;
    cdhw_t *hw;

    /* tries detection if device is NULL */
    if ((cddev = device_detect(device)) == NULL) {
        if (!device)
            errormsg("%P: no cdrom found: Try option -d <device>");
        return(-1);
    }

    if ((fd = open(cddev, O_RDONLY | O_NONBLOCK)) < 0) {
        errormsg("%P: can't open cdrom (%s): %s", cddev, strerror(errno));
        if (errno == EACCES)
            show_permissions(cddev);
        return(-1);
    }

    hw = read_hw(fd);
    if (!hw)
        return(-1);

    cdtool->fd = fd;
    cdtool->device = cddev;
    cdtool->hw = hw;
    return(0);
}

/************************************************************************/
/* Procedure:  do_play
 * Purpose:    to start playing
 * Inputs:     CD file descriptor, initial track, last track
 * Returns:    0< on error; 0 on success
 * 
 ************************************************************************/
int do_play(cdtool_t *cd, int trk0, int trk1)
{
    int newlast;
    struct cdrom_ti ti;
    cdhw_t *hw = cd->hw;

    debugmsg("do_play:  called, trk0=%d, trk1=%d", trk0, trk1);

    if (!check_disc(hw)) {
        errormsg("%P: can't play (%s)", cd_status_text(hw->iStatus));
        return(-1);
    }

    /* special cases for track "any" */
    if ((trk0 == 0) && (trk1 == 0)) {
        debugmsg("do_play:  trk[0.1]==0, status=%#x", 
          hw->subchnl.cdsc_audiostatus);

        /* subchannel status returns PLAY, PAUSED, COMPLETED, or none */
        switch (hw->subchnl.cdsc_audiostatus) {
            case CDROM_AUDIO_PAUSED:
                if (ioctl(cd->fd, CDROMRESUME) == -1) {
                    errormsg("%P: ioctl cdromresume: %s", strerror(errno));
                    return(-1);
                }
                return(0);

            case CDROM_AUDIO_PLAY:
                trk0 = hw->subchnl.cdsc_trk;
                break;

            case CDROM_AUDIO_COMPLETED:  /* WWH added */
                trk0 = 1;
                break;

            case CDROM_AUDIO_NO_STATUS:  /* ignore */
                debugmsg("%P: ioctl subchnl: NO_STATUS");
                break;

            case CDROM_AUDIO_INVALID:	 /* ignore */
                debugmsg("%P: ioctl subchnl: INVALID");
                break;
		
            case CDROM_AUDIO_ERROR:
                errormsg("%P: ioctl subchnl: ERROR");
                return(-1);

            default:
                errormsg("%P: ioctl subchnl: unknown status: subchnl=%#x",
                  hw->subchnl.cdsc_audiostatus);
                return(-1);
        }
    }

    /* default to TOC start/end tracks */
    ti.cdti_trk0 = trk0 > 0 ? trk0 : hw->tochdr.cdth_trk0;
    ti.cdti_trk1 = trk1 > 0 ? trk1 : hw->tochdr.cdth_trk1;

    /* set track indices (XX what for?) */
    ti.cdti_ind0 = 1;
    ti.cdti_ind1 = 99;
    
    /* XXXX */
    if (ti.cdti_trk0 < first_track(hw))
        ti.cdti_trk0 = first_track(hw);

    /* XXX */
    if (ti.cdti_trk1 > last_track(hw))
        ti.cdti_trk1 = last_track(hw);
    
    /* check range for non-audio tracks */
    newlast = check_audio_range(hw, ti.cdti_trk0, ti.cdti_trk1);
    if (newlast < 0) {
        errormsg("%P: no audio tracks within requested range");
        return(-1);
    }

    /* range may include data tracks; play up to last audio track */
    if (newlast < ti.cdti_trk1)
        ti.cdti_trk1 = newlast;
    
    verbosemsg("Playing from track %d to %d", ti.cdti_trk0, ti.cdti_trk1);
    return do_play_ti(cd->fd, cd->hw, &ti);
}

/************************************************************************/
/* Procedure:  do_skip
 * Purpose:    skip forwards or backwards and (re-)start playing
 * Returns:    0< on error; 0 on success
 *
 *   if direction is -1, skip back
 *   if direction is -2, skip forward
 * 
 ************************************************************************/
int do_skip(cdtool_t *cd, int direction, int skip)
{
    cdhw_t *hw = cd->hw;
    int track = 0;

    if (!check_disc(hw)) {
        errormsg("%P: can't skip (%s)", cd_status_text(hw->iStatus));
        return(-1);
    }

    if (skip < 1)
        skip = 1;

    /* skip back/forward from current track */
    switch (direction) {
        case SKIPBACK:
            track = hw->subchnl.cdsc_trk - skip;
            break;
        case SKIPFORWARD:
            track = hw->subchnl.cdsc_trk + skip;
    }

    /* check against TOC range */
    if ((track < hw->tochdr.cdth_trk0) || (track > hw->tochdr.cdth_trk1))
        track = 1;

    /* play from 'track' to last track */
    return do_play(cd, track, hw->tochdr.cdth_trk1);
}

/************************************************************************/
/* Procedure:  do_pause
 * Purpose:    to pause CD-ROM
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    0< on error; 0 on success
 * Notes:  
 *   1.
 */
/************************************************************************/
int do_pause(cdtool_t *cd) 
{
    cdhw_t *hw = cd->hw;
    verbosemsg("Pausing");
    
    /* pause only if disc inserted */
    if (!check_disc(hw)) {
        errormsg("%P: can't pause (%s)", cd_status_text(hw->iStatus));
        return(-1);
    }

    debugmsg("do_pause: iStatus=%d subchnl_status=%d",
        hw->iStatus,  hw->subchnl.cdsc_audiostatus);

    /* pause only if currently playing */
    if (hw->subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY) {
        debugmsg("do_pause: not pausing: status != play");
        return(0);
    }

    if (ioctl(cd->fd, CDROMPAUSE) == -1) {
        errormsg("%P: ioctl cdrompause: %s", strerror(errno));
        return(-1);
    }

    return(0);
}

/************************************************************************/
/* Procedure:  do_stop
 * Purpose:    to stop playing a CD-ROM
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    0< on error; 0 on success
 * Notes:  
 *   1.
 */
/************************************************************************/
int do_stop(cdtool_t *cd, int iCDNum) 
{
    debugmsg("do_stop: called, cdfd=%d", cd->fd);
    verbosemsg("Stopping");

    /* if shuffle is running, stop it... */
    shuffle_unlock(iCDNum, 1);

    /* send stop to the CDROM device */
    if (ioctl(cd->fd, CDROMSTOP) == -1) {
	errormsg("%P: ioctl cdromstop: %s", strerror(errno));
	return(-1);
    }

    return(0);
}

/************************************************************************/
/* Procedure:  do_eject
 * Purpose:    to eject the CD-ROM 
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    0< on error; 0 on success
 * Notes:  
 *   1.
 */
/************************************************************************/
int do_eject(cdtool_t *cd)
{
    debugmsg("do_eject:  called, cdfd=%d", cd->fd);
    verbosemsg("Ejecting CD-ROM");

    if (ioctl(cd->fd, CDROMEJECT) == -1) {
        errormsg("%P: ioctl cdromeject: %s", strerror(errno));
        return(-1);
    }

    return(0);
}

/************************************************************************/
/* Procedure:  do_close
 * Purpose:    to close the CD-ROM 
 * 
 * Inputs:     program name, CD file des.
 * Outputs:    to CD-ROM, debug output
 * Returns:    0< on error; 0 on success
 * Notes:  
 *   1.
 */
/************************************************************************/
int do_close(cdtool_t *cd)
{
    debugmsg("do_close: called, cdfd=%d", cd->fd);
    verbosemsg("Closing CD-ROM tray\n");

#ifdef HAVE_IOCTL_CDROMCLOSETRAY
    if (ioctl(cd->fd, CDROMCLOSETRAY) == -1) {
        errormsg("%P: ioctl cdromclose: %s", strerror(errno));
        return(-1);
    }
#endif

    return(0);
}

/************************************************************************/
/* 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.
 */
/************************************************************************/
int do_reset(cdtool_t *cd)
{
    debugmsg("do_reset:  called, cdfd=%d", cd->fd);
    verbosemsg("Resetting CDROM\n");

#ifdef HAVE_IOCTL_CDROMRESET
    if (ioctl(cd->fd, CDROMRESET) == -1) {
        errormsg("%P: ioctl cdromreset: %s", strerror(errno));
        return(-1);
    }
#else
    /* Solaris doesn't know CDROMRESET */
    if (ioctl(cd->fd, CDROMSTOP) == -1) {
        errormsg("%P: ioctl cdromstop: %s", strerror(errno));
        return(-1);
    }
#endif

    if (ioctl(cd->fd, CDROMSTART) == -1) {
        errormsg("%P: ioctl cdromstart: %s", strerror(errno));
        return(-1);
    }

    return(0);
}

/************************************************************************/
/* Procedure:  do_volume
 * Purpose:    to adjust CD-ROM volume level
 * 
 * Inputs:     program name, CD file des., volume level (uchar)
 * Outputs:    to CD-ROM, to stdout
 * Returns:    0 on success, -1 on failure
 * Notes:  
 *   1.
 */
/************************************************************************/
int do_volume(cdtool_t *cd, int level)
{
    struct cdrom_volctrl volctrl;
    debugmsg("do_volume:  called, volume=%d, cdfd=%d", level, cd->fd);

    if ((level < 0) || (level > 255)) {
        errormsg("%P: illegal volume (allowed values 0-255)");
        return -1;
    }

    verbosemsg("Setting volume to level %d", level);

    volctrl.channel0=level;
    volctrl.channel1=level;
    volctrl.channel2=level;
    volctrl.channel3=level;

    if (ioctl(cd->fd, CDROMVOLCTRL, &volctrl) == -1) {
        errormsg("%P: CDROMVOLCTRL ioctl failed: %s\n", strerror(errno));
        return -1;
    }

    return 0;
}