*.c: return status of operations. Exit code is set on operation failure.

cdda-player.c: debug and verbose change libcdio loglevel verbosity.
This commit is contained in:
rocky
2005-03-15 12:22:37 +00:00
parent af71247d66
commit b6798f337e
2 changed files with 88 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
/* /*
$Id: audio.c,v 1.1 2005/03/15 04:18:05 rocky Exp $ $Id: audio.c,v 1.2 2005/03/15 12:22:37 rocky Exp $
Copyright (C) 2005 Rocky Bernstein <rocky@panix.com> Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
@@ -117,61 +117,76 @@ oops(const char *psz_msg, int rc)
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
/*! Stop playing audio CD */ /*! Stop playing audio CD */
static void static bool
cd_stop(CdIo_t *p_cdio) cd_stop(CdIo_t *p_cdio)
{ {
bool b_ok = true;
if (b_cd && p_cdio) { if (b_cd && p_cdio) {
i_last_audio_track = CDIO_INVALID_TRACK; i_last_audio_track = CDIO_INVALID_TRACK;
if (DRIVER_OP_SUCCESS != cdio_audio_stop(p_cdio)) b_ok = DRIVER_OP_SUCCESS == cdio_audio_stop(p_cdio);
if ( !b_ok )
xperror("stop"); xperror("stop");
} }
return b_ok;
} }
/*! Eject CD */ /*! Eject CD */
static void static bool
cd_eject(void) cd_eject(void)
{ {
bool b_ok = true;
if (p_cdio) { if (p_cdio) {
cd_stop(p_cdio); cd_stop(p_cdio);
if (DRIVER_OP_SUCCESS != cdio_eject_media(&p_cdio)) b_ok = DRIVER_OP_SUCCESS == cdio_eject_media(&p_cdio);
if (!b_ok)
xperror("eject"); xperror("eject");
b_cd = 0; b_cd = 0;
p_cdio = NULL; p_cdio = NULL;
} }
return b_ok;
} }
/*! Close CD tray */ /*! Close CD tray */
static void static bool
cd_close(const char *psz_device) cd_close(const char *psz_device)
{ {
bool b_ok = true;
if (!b_cd) { if (!b_cd) {
if (DRIVER_OP_SUCCESS != cdio_close_tray(psz_device, &driver_id)) b_ok = DRIVER_OP_SUCCESS == cdio_close_tray(psz_device, &driver_id);
if (!b_ok)
xperror("close"); xperror("close");
} }
return b_ok;
} }
/*! Pause playing audio CD */ /*! Pause playing audio CD */
static void static bool
cd_pause(CdIo_t *p_cdio) cd_pause(CdIo_t *p_cdio)
{ {
if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) bool b_ok = true;
if (DRIVER_OP_SUCCESS != cdio_audio_pause(p_cdio)) if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) {
b_ok = DRIVER_OP_SUCCESS == cdio_audio_pause(p_cdio);
if (!b_ok)
xperror("pause"); xperror("pause");
}
return b_ok;
} }
/*! Get status/track/position info of an audio CD */ /*! Get status/track/position info of an audio CD */
static void static bool
read_subchannel(CdIo_t *p_cdio) read_subchannel(CdIo_t *p_cdio)
{ {
if (!b_cd) return; bool b_ok = true;
if (!b_cd) return false;
sub.format = CDIO_CDROM_MSF; b_ok = DRIVER_OP_SUCCESS == cdio_audio_read_subchannel(p_cdio, &sub);
if (DRIVER_OP_SUCCESS != cdio_audio_read_subchannel(p_cdio, &sub)) { if (!b_ok) {
xperror("read subchannel"); xperror("read subchannel");
b_cd = 0; b_cd = 0;
} }
if (auto_mode && sub.audio_status == CDIO_MMC_READ_SUB_ST_COMPLETED) if (auto_mode && sub.audio_status == CDIO_MMC_READ_SUB_ST_COMPLETED)
cd_eject(); cd_eject();
return b_ok;
} }
/*! Read CD TOC and set CD information. */ /*! Read CD TOC and set CD information. */
@@ -286,8 +301,9 @@ usage(char *prog)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c, nostop=0; int c, nostop=0;
char *h; char *h;
int i_rc = 0;
psz_program = strrchr(argv[0],'/'); psz_program = strrchr(argv[0],'/');
psz_program = psz_program ? psz_program+1 : argv[0]; psz_program = psz_program ? psz_program+1 : argv[0];
@@ -386,7 +402,7 @@ main(int argc, char *argv[])
{ {
nostop=1; nostop=1;
if (EJECT_CD == todo) { if (EJECT_CD == todo) {
cd_eject(); i_rc = cd_eject() ? 0 : 1;
} else { } else {
read_toc(p_cdio); read_toc(p_cdio);
if (!b_cd) { if (!b_cd) {
@@ -396,7 +412,7 @@ main(int argc, char *argv[])
if (b_cd) if (b_cd)
switch (todo) { switch (todo) {
case STOP_PLAYING: case STOP_PLAYING:
cd_stop(p_cdio); i_rc = cd_stop(p_cdio) ? 0 : 1;
break; break;
case EJECT_CD: case EJECT_CD:
/* Should have been handled above. */ /* Should have been handled above. */
@@ -410,7 +426,7 @@ main(int argc, char *argv[])
play_track(1,CDIO_CDROM_LEADOUT_TRACK); play_track(1,CDIO_CDROM_LEADOUT_TRACK);
break; break;
case CLOSE_CD: case CLOSE_CD:
cdio_close_tray(psz_device, NULL); i_rc = cdio_close_tray(psz_device, NULL) ? 0 : 1;
break; break;
} }
else { else {
@@ -420,7 +436,7 @@ main(int argc, char *argv[])
} }
if (!nostop) cd_stop(p_cdio); if (!nostop) cd_stop(p_cdio);
oops("bye", 0); oops("bye", i_rc);
return 0; /* keep compiler happy */ return 0; /* keep compiler happy */
} }

View File

@@ -1,5 +1,5 @@
/* /*
$Id: cdda-player.c,v 1.12 2005/03/15 04:17:05 rocky Exp $ $Id: cdda-player.c,v 1.13 2005/03/15 12:22:38 rocky Exp $
Copyright (C) 2005 Rocky Bernstein <rocky@panix.com> Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
@@ -63,6 +63,7 @@
#include <cdio/mmc.h> #include <cdio/mmc.h>
#include <cdio/util.h> #include <cdio/util.h>
#include <cdio/cd_types.h> #include <cdio/cd_types.h>
#include <cdio/logging.h>
#include "cddb.h" #include "cddb.h"
@@ -299,63 +300,79 @@ oops(const char *psz_msg, int rc)
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
/*! Stop playing audio CD */ /*! Stop playing audio CD */
static void static bool
cd_stop(CdIo_t *p_cdio) cd_stop(CdIo_t *p_cdio)
{ {
bool b_ok = true;
if (b_cd && p_cdio) { if (b_cd && p_cdio) {
action("stop..."); action("stop...");
i_last_audio_track = CDIO_INVALID_TRACK; i_last_audio_track = CDIO_INVALID_TRACK;
if (DRIVER_OP_SUCCESS != cdio_audio_stop(p_cdio)) b_ok = DRIVER_OP_SUCCESS == cdio_audio_stop(p_cdio);
if ( !b_ok )
xperror("stop"); xperror("stop");
} }
return b_ok;
} }
/*! Eject CD */ /*! Eject CD */
static void static bool
cd_eject(void) cd_eject(void)
{ {
bool b_ok = true;
if (p_cdio) { if (p_cdio) {
cd_stop(p_cdio); cd_stop(p_cdio);
action("eject..."); action("eject...");
if (DRIVER_OP_SUCCESS != cdio_eject_media(&p_cdio)) b_ok = DRIVER_OP_SUCCESS == cdio_eject_media(&p_cdio);
if (!b_ok)
xperror("eject"); xperror("eject");
b_cd = 0; b_cd = 0;
p_cdio = NULL; p_cdio = NULL;
} }
return b_ok;
} }
/*! Close CD tray */ /*! Close CD tray */
static void static bool
cd_close(const char *psz_device) cd_close(const char *psz_device)
{ {
bool b_ok = true;
if (!b_cd) { if (!b_cd) {
action("close..."); action("close...");
if (DRIVER_OP_SUCCESS != cdio_close_tray(psz_device, &driver_id)) b_ok = DRIVER_OP_SUCCESS == cdio_close_tray(psz_device, &driver_id);
if (!b_ok)
xperror("close"); xperror("close");
} }
return b_ok;
} }
/*! Pause playing audio CD */ /*! Pause playing audio CD */
static void static bool
cd_pause(CdIo_t *p_cdio) cd_pause(CdIo_t *p_cdio)
{ {
if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) bool b_ok = true;
if (DRIVER_OP_SUCCESS != cdio_audio_pause(p_cdio)) if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) {
b_ok = DRIVER_OP_SUCCESS == cdio_audio_pause(p_cdio);
if (!b_ok)
xperror("pause"); xperror("pause");
}
return b_ok;
} }
/*! Get status/track/position info of an audio CD */ /*! Get status/track/position info of an audio CD */
static void static bool
read_subchannel(CdIo_t *p_cdio) read_subchannel(CdIo_t *p_cdio)
{ {
if (!b_cd) return; bool b_ok = true;
if (!b_cd) return false;
if (DRIVER_OP_SUCCESS != cdio_audio_read_subchannel(p_cdio, &sub)) { b_ok = DRIVER_OP_SUCCESS == cdio_audio_read_subchannel(p_cdio, &sub);
if (!b_ok) {
xperror("read subchannel"); xperror("read subchannel");
b_cd = 0; b_cd = 0;
} }
if (auto_mode && sub.audio_status == CDIO_MMC_READ_SUB_ST_COMPLETED) if (auto_mode && sub.audio_status == CDIO_MMC_READ_SUB_ST_COMPLETED)
cd_eject(); cd_eject();
return b_ok;
} }
#define add_cddb_disc_info(format_str, field) \ #define add_cddb_disc_info(format_str, field) \
@@ -548,18 +565,22 @@ skip(int diff)
xperror("play"); xperror("play");
} }
static void static bool
toggle_pause() toggle_pause()
{ {
if (!b_cd) return; bool b_ok = true;
if (!b_cd) return false;
if (CDIO_MMC_READ_SUB_ST_PAUSED == sub.audio_status) { if (CDIO_MMC_READ_SUB_ST_PAUSED == sub.audio_status) {
if (DRIVER_OP_SUCCESS != cdio_audio_resume(p_cdio)) b_ok = DRIVER_OP_SUCCESS != cdio_audio_resume(p_cdio);
if (!b_ok)
xperror("resume"); xperror("resume");
} else { } else {
if (DRIVER_OP_SUCCESS != cdio_audio_pause(p_cdio)) b_ok = DRIVER_OP_SUCCESS != cdio_audio_pause(p_cdio);
if (!b_ok)
xperror("pause"); xperror("pause");
} }
return b_ok;
} }
/*! Update windows with status and possibly track info. This used in /*! Update windows with status and possibly track info. This used in
@@ -1182,12 +1203,14 @@ main(int argc, char *argv[])
{ {
int c, nostop=0; int c, nostop=0;
char *h; char *h;
int i_rc = 0;
psz_program = strrchr(argv[0],'/'); psz_program = strrchr(argv[0],'/');
psz_program = psz_program ? psz_program+1 : argv[0]; psz_program = psz_program ? psz_program+1 : argv[0];
memset(&cddb_opts, 0, sizeof(cddb_opts)); memset(&cddb_opts, 0, sizeof(cddb_opts));
cdio_loglevel_default = CDIO_LOG_WARN;
/* parse options */ /* parse options */
while ( 1 ) { while ( 1 ) {
if (-1 == (c = getopt(argc, argv, "xdhkvcCpset:la"))) if (-1 == (c = getopt(argc, argv, "xdhkvcCpset:la")))
@@ -1195,9 +1218,13 @@ main(int argc, char *argv[])
switch (c) { switch (c) {
case 'v': case 'v':
b_verbose = true; b_verbose = true;
if (cdio_loglevel_default > CDIO_LOG_INFO)
cdio_loglevel_default = CDIO_LOG_INFO;
break; break;
case 'd': case 'd':
debug = 1; debug = 1;
if (cdio_loglevel_default > CDIO_LOG_DEBUG)
cdio_loglevel_default = CDIO_LOG_DEBUG;
break; break;
case 'a': case 'a':
auto_mode = 1; auto_mode = 1;
@@ -1288,14 +1315,7 @@ main(int argc, char *argv[])
p_cdio = cdio_open (psz_device, driver_id); p_cdio = cdio_open (psz_device, driver_id);
if (!p_cdio) { if (p_cdio && b_verbose)
if (b_verbose)
fprintf(stderr, "error: %s\n", strerror(errno));
else
fprintf(stderr, "open %s: %s\n", psz_device, strerror(errno));
exit(1);
} else
if (b_verbose)
fprintf(stderr,"ok\n"); fprintf(stderr,"ok\n");
tty_raw(); tty_raw();
@@ -1308,7 +1328,7 @@ main(int argc, char *argv[])
b_sig = true; b_sig = true;
nostop=1; nostop=1;
if (EJECT_CD == todo) { if (EJECT_CD == todo) {
cd_eject(); i_rc = cd_eject() ? 0 : 1;
} else { } else {
read_toc(p_cdio); read_toc(p_cdio);
if (!b_cd) { if (!b_cd) {
@@ -1318,7 +1338,7 @@ main(int argc, char *argv[])
if (b_cd) if (b_cd)
switch (todo) { switch (todo) {
case STOP_PLAYING: case STOP_PLAYING:
cd_stop(p_cdio); i_rc = cd_stop(p_cdio) ? 0 : 1;
break; break;
case EJECT_CD: case EJECT_CD:
/* Should have been handled above. */ /* Should have been handled above. */
@@ -1342,7 +1362,7 @@ main(int argc, char *argv[])
play_track(1,CDIO_CDROM_LEADOUT_TRACK); play_track(1,CDIO_CDROM_LEADOUT_TRACK);
break; break;
case CLOSE_CD: case CLOSE_CD:
cdio_close_tray(psz_device, NULL); i_rc = cdio_close_tray(psz_device, NULL) ? 0 : 1;
break; break;
case LIST_KEYS: case LIST_KEYS:
case LIST_TRACKS: case LIST_TRACKS:
@@ -1433,10 +1453,10 @@ main(int argc, char *argv[])
case '7': case '7':
case '8': case '8':
case '9': case '9':
play_track(key - '0',CDIO_CDROM_LEADOUT_TRACK); play_track(key - '0', CDIO_CDROM_LEADOUT_TRACK);
break; break;
case '0': case '0':
play_track(10,CDIO_CDROM_LEADOUT_TRACK); play_track(10, CDIO_CDROM_LEADOUT_TRACK);
break; break;
case KEY_F(1): case KEY_F(1):
case KEY_F(2): case KEY_F(2):
@@ -1458,14 +1478,14 @@ main(int argc, char *argv[])
case KEY_F(18): case KEY_F(18):
case KEY_F(19): case KEY_F(19):
case KEY_F(20): case KEY_F(20):
play_track(key - KEY_F(1) + 11,CDIO_CDROM_LEADOUT_TRACK); play_track(key - KEY_F(1) + 11, CDIO_CDROM_LEADOUT_TRACK);
break; break;
} }
} }
} }
if (!nostop) cd_stop(p_cdio); if (!nostop) cd_stop(p_cdio);
tty_restore(); tty_restore();
oops("bye", 0); oops("bye", i_rc);
return 0; /* keep compiler happy */ return 0; /* keep compiler happy */
} }