/** * Copyright (c) 2005 Claudio Bustos * This code is hereby licensed for public consumption under the * GNU GPL v2. * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * Implementation of CdIo::Cd */ #include "CdIo.h" // Libera el puntero al objeto CdIo creado en Cd.new /** * Free resources for CdIo* in CdIo::Cd */ static void _cdio_cd_destroy(void *p) { /* * static int i; printf("\nDestruyendo cdio %d\n",i++); */ cdio_destroy(p); } /** Create a new Cd::CdIo object. The first argument is an String for a device or a cue/bin/toc/nrg file The seconf argument is the constant for a Driver (default CdIo::DRIVER_UNKNOWN) * CdIo::Cd.new() * CdIo::Cd.new("/dev/cdrom") * CdIo::Cd.new("/dev/cdrom", CdIo::DRIVER_UNKNOWN) */ VALUE rb_cdio_cd_new(int argc, VALUE * argv, VALUE class) { CdIo *p_cdio; char *device; driver_id_t driver = DRIVER_UNKNOWN; VALUE source, driver_fix; rb_scan_args(argc, argv, "02", &source, &driver_fix); // source=DEFAULT, driver=DEFAULT if (driver_fix != Qnil) { FIXNUM_P(driver_fix); driver = FIX2INT(driver_fix); } if (source != Qnil) { Check_Type(source, T_STRING); SafeStringValue(source); device = StringValuePtr(source); } else { device = cdio_get_default_device(NULL); if (NULL == device) { rb_raise(rb_eException, "I can't find a default device"); return Qnil; } } p_cdio = cdio_open(device, driver); if (p_cdio == NULL) { rb_raise(eCdIoError, "I can't create the object. Please, verify the media"); return Qnil; } else { VALUE c_argv[2]; VALUE tdata = Data_Wrap_Struct(class, 0, _cdio_cd_destroy, p_cdio); c_argv[0] = rb_str_new2(device); c_argv[1] = INT2FIX(driver); rb_obj_call_init(tdata, 2, c_argv); return tdata; } } /** CdIo::Cd.initialize Initialize the CdIo::Cd object The first argument is an String for a device or a cue/bin/toc/nrg file The seconf argument is the constant for a driver */ VALUE rb_cdio_cd_init(VALUE self, VALUE device, VALUE driver) { track_t nt; discmode_t discmode; Check_Type(device, T_STRING); FIXNUM_P(driver); GET_CDIO(self, p_cdio); rb_iv_set(self, "@device", device); rb_iv_set(self, "@driver_name", rb_str_new2(cdio_get_driver_name(p_cdio))); rb_iv_set(self, "@driver_id", INT2FIX(cdio_get_driver_id(p_cdio))); discmode = cdio_get_discmode(p_cdio); rb_iv_set(self, "@discmode", INT2FIX(discmode)); nt = cdio_get_first_track_num(p_cdio); rb_iv_set(self, "@first_track_num", (nt != CDIO_INVALID_TRACK) ? INT2FIX(nt) : Qnil); nt = cdio_get_num_tracks(p_cdio); rb_iv_set(self, "@num_tracks", (nt != CDIO_INVALID_TRACK) ? INT2FIX(nt) : Qnil); // mcn if (cdio_is_discmode_cdrom(discmode)) { char *mcn; mcn = cdio_get_mcn(p_cdio); if (NULL != mcn) { rb_iv_set(self, "@mcn", rb_str_new2(mcn)); free(mcn); } } // stat_size rb_iv_set(self, "@last_lsn", INT2NUM(cdio_get_disc_last_lsn(p_cdio))); rb_iv_set(self, "@joliet_level", INT2NUM(cdio_get_joliet_level(p_cdio))); // si es un disco de audio, lo extiendo con las funciones // del módulo Cdda if (CDIO_DISC_MODE_CD_DA == discmode) { rb_extend_object(self, mCdIoCdda); } return self; } /** * Returns an array with the tracks of the cd * cd.get_tracks #=> [,] */ VALUE rb_cdio_cd_get_tracks(VALUE self) { VALUE tracks = rb_iv_get(self, "@tracks"); if (tracks == Qnil) { VALUE argv[1]; argv[0] = self; tracks = rb_class_new_instance(1, argv, cCdIoTracks); rb_iv_set(self, "@tracks", tracks); } return tracks; } /** * CdIo::Cd.get_first_track */ VALUE rb_cdio_cd_get_first_track(VALUE self) { GET_CDIO(self, p_cdio); track_t first_track_num = cdio_get_first_track_num(p_cdio); return get_track(self, first_track_num); } /** * CdIo::Cd.get_arg */ VALUE rb_cdio_cd_get_arg(VALUE obj, VALUE arg) { rb_notimplement(); } /** * CdIo::Cd.is_cd? */ VALUE rb_cdio_cd_is_cd(VALUE obj) { GET_CDIO(obj, p_cdio); return (cdio_is_discmode_cdrom(cdio_get_discmode(p_cdio))) ? Qtrue : Qfalse; } /** * CdIo::Cd.is_dvd? */ VALUE rb_cdio_cd_is_dvd(VALUE obj) { GET_CDIO(obj, p_cdio); return (cdio_is_discmode_dvd(cdio_get_discmode(p_cdio))) ? Qtrue : Qfalse; } static VALUE _get_freedb(VALUE obj) { rb_require("freedb"); VALUE cFreeDb = rb_const_get(rb_mKernel, rb_intern("Freedb")); return rb_funcall(cFreeDb, rb_intern("new"), 1, rb_iv_get(obj, "@device")); } static VALUE _get_freedb_error(VALUE obj, VALUE error_info) { rb_raise(rb_eLoadError, "Please, install ruby-freedb to use this function"); return Qnil; } /** * CdIo::Cd.get_freedb */ VALUE rb_cdio_cd_get_freedb(VALUE obj) { VALUE retorno = rb_rescue2(_get_freedb, obj, _get_freedb_error, obj, rb_eLoadError, 0); return retorno; } VALUE rb_cdio_cd_get_drive_cap(VALUE obj) { GET_CDIO(obj, p_cdio); cdio_drive_read_cap_t read_cap; cdio_drive_write_cap_t write_cap; cdio_drive_misc_cap_t misc_cap; cdio_get_drive_cap(p_cdio, &read_cap, &write_cap, &misc_cap); // Error? return get_cap(&read_cap, &write_cap, &misc_cap); } VALUE rb_cdio_cd_get_hwinfo(VALUE obj) { GET_CDIO(obj, p_cdio); cdio_hwinfo_t hw_info; if (!cdio_get_hwinfo(p_cdio, &hw_info)) { rb_raise(rb_eException, "Error on retrieving hardware info"); return Qnil; } else { VALUE ret; ret = rb_hash_new(); rb_hash_aset(ret, rb_str_new2("vendor"), rb_str_new2(hw_info.psz_vendor)); rb_hash_aset(ret, rb_str_new2("model"), rb_str_new2(hw_info.psz_model)); rb_hash_aset(ret, rb_str_new2("revision"), rb_str_new2(hw_info.psz_revision)); return ret; } } VALUE rb_cdio_cd_close(VALUE obj) { rb_gc_start(); obj = Qnil; return Qnil; } VALUE rb_cdio_cd_get_cdtext(VALUE obj) { GET_CDIO(obj, p_cdio); const cdtext_t *cdtext = cdio_get_cdtext(p_cdio, 0); return get_cdtext(cdtext); } VALUE rb_cdio_cd_eject(VALUE obj) { GET_CDIO(obj, p_cdio); verify_return_code(cdio_eject_media(&p_cdio)); rb_gc_start(); obj = Qnil; return Qtrue; }