<commit_before>/*
 * Copyright (C) 2005 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <config.h>

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

#ifdef HAVE_CDDB
# include <cddb/cddb.h>
#endif

#include <fusionsound.h>

#include <media/ifusionsoundmusicprovider.h>

#include <direct/types.h>
#include <direct/mem.h>
#include <direct/thread.h>
#include <direct/util.h>

static DFBResult
Probe( IFusionSoundMusicProvider_ProbeContext *ctx );

static DFBResult
Construct( IFusionSoundMusicProvider *thiz,
           const char                *filename );

#include <direct/interface_implementation.h>

DIRECT_INTERFACE_IMPLEMENTATION( IFusionSoundMusicProvider, CDDA )


struct cdda_track {
     unsigned int  start;  /* first frame   */
     unsigned int  length; /* total frames  */
     unsigned int  frame;  /* current frame */
     
     char         *artist;
     char         *title;
     char         *genre;
     char         *album;
     short         year;
};

/*
 * private data struct of IFusionSoundMusicProvider
 */
typedef struct {
     int                      ref;        /* reference counter */
     
     int                      fd;

     unsigned int             current_track;
     unsigned int             total_tracks;
     struct cdda_track       *tracks;
     
     DirectThread            *thread;
     pthread_mutex_t          lock;
     bool                     playing;
     bool                     finished;
     
     int                      buffered_frames;
     __s16                   *src_buffer;
     __u8                    *dst_buffer;
     
     struct {
          IFusionSoundStream *stream;
          IFusionSoundBuffer *buffer;
          int                 format;
          int                 channels;
          int                 length;
     } dest;

     FMBufferCallback         callback;
     void                    *ctx;
} IFusionSoundMusicProvider_CDDA_data;



#define CD_FRAMES_PER_SECOND  75
#define CD_BYTES_PER_FRAME    2352


#if defined(__linux__)
 
#include <linux/cdrom.h>

static DFBResult
cdda_probe( int fd )
{
     struct cdrom_tochdr tochdr;
     
     if (ioctl( fd, CDROMREADTOCHDR, &tochdr ) < 0)
          return DFB_UNSUPPORTED;
     
     return DFB_OK;
}

static DFBResult
cdda_build_tracklits( int fd, struct cdda_track **ret_tracks,
                              unsigned int       *ret_num )
{
     int                        total_tracks;
     struct cdda_track         *tracks, *track;
     struct cdrom_tochdr        tochdr;
     struct cdrom_tocentry      tocentry;
     struct cdrom_multisession  ms;
     struct cdrom_msf0          msf;
     int                        i;
     
     /* read TOC header */
     if (ioctl( fd, CDROMREADTOCHDR, &tochdr ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDROMREADTOCHDR ) failed!\n" );
          return DFB_IO;
     }
     
     ms.addr_format = CDROM_LBA;
     if (ioctl( fd, CDROMMULTISESSION, &ms ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDROMMULTISESSION ) failed!\n" );
          return DFB_IO;
     }
     
     total_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
          
     tracks = D_CALLOC( total_tracks+1, sizeof(struct cdda_track) );
     if (!tracks)
          return D_OOM();
     
     /* iterate through tracks */
     for (i = tochdr.cdth_trk0, track = tracks; i <= tochdr.cdth_trk1; i++) {          
          memset( &tocentry, 0, sizeof(tocentry) );
          
          tocentry.cdte_track  = i;
          tocentry.cdte_format = CDROM_MSF;
          if (ioctl( fd, CDROMREADTOCENTRY, &tocentry ) == -1) {
               D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                         "ioctl( CDROMREADTOCENTRY ) failed!\n" );
               D_FREE( tracks );
               return DFB_IO;
          }
          
          /* skip data tracks */
          if (tocentry.cdte_ctrl & 0x04) {
               total_tracks--;
               continue;
          }
          
          msf = tocentry.cdte_addr.msf;
          track->start = (msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                         (msf.second      * CD_FRAMES_PER_SECOND) +
                          msf.frame;
          track++;
     }
     
     if (total_tracks < 1) {
          D_FREE( tracks );
          return DFB_FAILURE;
     }
     
     memset( &tocentry, 0, sizeof(tocentry) );
     
     /* get leadout track */
     tocentry.cdte_track  = 0xaa;
     tocentry.cdte_format = CDROM_MSF;
     if (ioctl( fd, CDROMREADTOCENTRY, &tocentry ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDROMREADTOCENTRY ) failed!\n" );
          D_FREE( tracks );
          return DFB_IO;
     }          
     
     msf = tocentry.cdte_addr.msf;
     tracks[total_tracks].start = (msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                                  (msf.second      * CD_FRAMES_PER_SECOND) +
                                   msf.frame;
     
     /* compute tracks' length */
     for (i = 0; i < total_tracks; i++)
          tracks[i].length = tracks[i+1].start - tracks[i].start;
          
     *ret_tracks = tracks;
     *ret_num    = total_tracks;
     
     return DFB_OK;
}

static int
cdda_read_audio( int fd, __u8 *buf, int pos, int len )
{
     struct cdrom_read_audio ra;
     
     ra.addr.msf.minute =  pos / (60 * CD_FRAMES_PER_SECOND);
     ra.addr.msf.second = (pos / CD_FRAMES_PER_SECOND) % 60;
     ra.addr.msf.frame  =  pos % CD_FRAMES_PER_SECOND;
     ra.addr_format     = CDROM_MSF;
     ra.nframes         = len;
     ra.buf             = buf;
          
     if (ioctl( fd, CDROMREADAUDIO, &ra ) < 0) {
          D_PERROR( "IFusionSoundMusicProvider_CDDA: "
                    "ioctl( CDROMREADAUDIO ) failed!\n" );
          return -1;
     }
     
     return ra.nframes;
}

#elif defined(__FreeBSD__)

#include <sys/cdio.h>

static DFBResult
cdda_probe( int fd )
{
     struct ioc_toc_header tochdr;
     
     if (ioctl( fd, CDROM_DRIVE_STATUS, CDSL_CURRENT ) != CDS_DISC_OK)
          return DFB_UNSUPPORTED;
     
     if (ioctl( fd, CDIOREADTOCHEADER, &tochdr ) < 0)
          return DFB_UNSUPPORTED;
          
     return DFB_OK;
}

static DFBResult
cdda_build_tracklits( int fd, struct cdda_track **ret_tracks,
                              unsigned int       *ret_num )
{
     int                               total_tracks;
     struct cdda_track                *tracks, *track;
     struct ioc_toc_header             tochdr;
     struct ioc_read_toc_single_entry  tocentry;
     union msf_lba                     msf_lba;
     int                               i;
     
     /* read TOC header */
     if (ioctl( fd, CDIOREADTOCHEADER, &tochdr ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDIOREADTOCHEADER ) failed!\n" );
          return DFB_IO;
     }
     
     total_tracks = tochdr.ending_track - tochdr.starting_track + 1;
          
     tracks = D_CALLOC( total_tracks+1, sizeof(struct cdda_track) );
     if (!tracks)
          return D_OOM();
     
     /* iterate through tracks */
     for (i = tochdr.starting_track, track = tracks; i <= tochdr.ending_track; i++) {          
          memset( &tocentry, 0, sizeof(tocentry) );
          
          tocentry.track          = i;
          tocentry.address_format = CD_MSF_FORMAT;
          if (ioctl( fd, CDIOREADTOCENTRY, &tocentry ) == -1) {
               D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                         "ioctl( CDIOREADTOCENTRY ) failed!\n" );
               D_FREE( tracks );
               return DFB_IO;
          }
          
          /* skip data tracks */
          if (tocentry.entry.control & 0x04) {
               total_tracks--;
               continue;
          }
          
          msf_lba = tocentry.entry.addr;
          track->start = (msf_lba.msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                         (msf_lba.msf.second      * CD_FRAMES_PER_SECOND) +
                          msf_lba.msf.frame;
          track++;
     }
     
     if (total_tracks < 1) {
          D_FREE( tracks );
          return DFB_FAILURE;
     }
     
     memset( &tocentry, 0, sizeof(tocentry) );
     
     /* get leadout track */
     tocentry.track          = 0xaa;
     tocentry.address_format = CD_MSF_FORMAT;
     if (ioctl( fd, CDIOREADTOCENTRY, &tocentry ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDIOREADTOCENTRY ) failed!\n" );
          D_FREE( tracks );
          return DFB_IO;
     }          
     
     msf_lba = tocentry.entry.addr;
     tracks[total_tracks].start = (msf_lba.msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                                  (msf_lba.msf.second      * CD_FRAMES_PER_SECOND) +
                                   msf_lba.msf.frame;
     
     /* compute tracks' length */
     for (i = 0; i < total_tracks; i++)
          tracks[i].length = tracks[i+1].start - tracks[i].start;
          
     *ret_tracks = tracks;
     *ret_num    = total_tracks;
     
     return DFB_OK;
}

static int
cdda_read_audio( int fd, __u8 *buf, int pos, int len )
{
     struct ioc_read_audio ra;
     
     ra.address.msf.minute =  pos / (60 * CD_FRAMES_PER_SECOND);
     ra.address.msf.second = (pos / CD_FRAMES_PER_SECOND) % 60;
     ra.address.msf.frame  =  pos % CD_FRAMES_PER_SECOND;
     ra.address_format     = CD_MSF_FORMAT;
     ra.nframes            = len;
     ra.buffer             = buf;
          
     if (ioctl( fd, CDIOCREADAUDIO, &ra ) < 0) {
          D_PERROR( "IFusionSoundMusicProvider_CDDA: "
                    "ioctl( CDIOCREADAUDIO ) failed!\n" );
          return -1;
     }
     
     return ra.nframes;
}

#else

static DFBResult
cdda_probe( int fd )
{
     D_WARN( "unsupported platform" );
     return DFB_UNSUPPORTED;
}

struct DFBResult
cdda_build_tracklits( int fd, struct cdda_track **ret_tracks,
                              unsigned int       *ret_num )
{
     return DFB_UNSUPPORTED;
}

static int
cdda_read_audio( int fd, __u8 *buf, int pos, int frames )
{
     return -1;
}
<commit_msg>Moved ioctl CDROM_DRIVE_STATUS to the right place.<commit_after>/*
 * Copyright (C) 2005 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <config.h>

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

#ifdef HAVE_CDDB
# include <cddb/cddb.h>
#endif

#include <fusionsound.h>

#include <media/ifusionsoundmusicprovider.h>

#include <direct/types.h>
#include <direct/mem.h>
#include <direct/thread.h>
#include <direct/util.h>

static DFBResult
Probe( IFusionSoundMusicProvider_ProbeContext *ctx );

static DFBResult
Construct( IFusionSoundMusicProvider *thiz,
           const char                *filename );

#include <direct/interface_implementation.h>

DIRECT_INTERFACE_IMPLEMENTATION( IFusionSoundMusicProvider, CDDA )


struct cdda_track {
     unsigned int  start;  /* first frame   */
     unsigned int  length; /* total frames  */
     unsigned int  frame;  /* current frame */
     
     char         *artist;
     char         *title;
     char         *genre;
     char         *album;
     short         year;
};

/*
 * private data struct of IFusionSoundMusicProvider
 */
typedef struct {
     int                      ref;        /* reference counter */
     
     int                      fd;

     unsigned int             current_track;
     unsigned int             total_tracks;
     struct cdda_track       *tracks;
     
     DirectThread            *thread;
     pthread_mutex_t          lock;
     bool                     playing;
     bool                     finished;
     
     int                      buffered_frames;
     __s16                   *src_buffer;
     __u8                    *dst_buffer;
     
     struct {
          IFusionSoundStream *stream;
          IFusionSoundBuffer *buffer;
          int                 format;
          int                 channels;
          int                 length;
     } dest;

     FMBufferCallback         callback;
     void                    *ctx;
} IFusionSoundMusicProvider_CDDA_data;



#define CD_FRAMES_PER_SECOND  75
#define CD_BYTES_PER_FRAME    2352


#if defined(__linux__)
 
#include <linux/cdrom.h>

static DFBResult
cdda_probe( int fd )
{
     struct cdrom_tochdr tochdr;

     if (ioctl( fd, CDROM_DRIVE_STATUS, CDSL_CURRENT ) != CDS_DISC_OK)
          return DFB_UNSUPPORTED;
     
     if (ioctl( fd, CDROMREADTOCHDR, &tochdr ) < 0)
          return DFB_UNSUPPORTED;
     
     return DFB_OK;
}

static DFBResult
cdda_build_tracklits( int fd, struct cdda_track **ret_tracks,
                              unsigned int       *ret_num )
{
     int                        total_tracks;
     struct cdda_track         *tracks, *track;
     struct cdrom_tochdr        tochdr;
     struct cdrom_tocentry      tocentry;
     struct cdrom_multisession  ms;
     struct cdrom_msf0          msf;
     int                        i;
     
     /* read TOC header */
     if (ioctl( fd, CDROMREADTOCHDR, &tochdr ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDROMREADTOCHDR ) failed!\n" );
          return DFB_IO;
     }
     
     ms.addr_format = CDROM_LBA;
     if (ioctl( fd, CDROMMULTISESSION, &ms ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDROMMULTISESSION ) failed!\n" );
          return DFB_IO;
     }
     
     total_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
          
     tracks = D_CALLOC( total_tracks+1, sizeof(struct cdda_track) );
     if (!tracks)
          return D_OOM();
     
     /* iterate through tracks */
     for (i = tochdr.cdth_trk0, track = tracks; i <= tochdr.cdth_trk1; i++) {          
          memset( &tocentry, 0, sizeof(tocentry) );
          
          tocentry.cdte_track  = i;
          tocentry.cdte_format = CDROM_MSF;
          if (ioctl( fd, CDROMREADTOCENTRY, &tocentry ) == -1) {
               D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                         "ioctl( CDROMREADTOCENTRY ) failed!\n" );
               D_FREE( tracks );
               return DFB_IO;
          }
          
          /* skip data tracks */
          if (tocentry.cdte_ctrl & 0x04) {
               total_tracks--;
               continue;
          }
          
          msf = tocentry.cdte_addr.msf;
          track->start = (msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                         (msf.second      * CD_FRAMES_PER_SECOND) +
                          msf.frame;
          track++;
     }
     
     if (total_tracks < 1) {
          D_FREE( tracks );
          return DFB_FAILURE;
     }
     
     memset( &tocentry, 0, sizeof(tocentry) );
     
     /* get leadout track */
     tocentry.cdte_track  = 0xaa;
     tocentry.cdte_format = CDROM_MSF;
     if (ioctl( fd, CDROMREADTOCENTRY, &tocentry ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDROMREADTOCENTRY ) failed!\n" );
          D_FREE( tracks );
          return DFB_IO;
     }          
     
     msf = tocentry.cdte_addr.msf;
     tracks[total_tracks].start = (msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                                  (msf.second      * CD_FRAMES_PER_SECOND) +
                                   msf.frame;
     
     /* compute tracks' length */
     for (i = 0; i < total_tracks; i++)
          tracks[i].length = tracks[i+1].start - tracks[i].start;
          
     *ret_tracks = tracks;
     *ret_num    = total_tracks;
     
     return DFB_OK;
}

static int
cdda_read_audio( int fd, __u8 *buf, int pos, int len )
{
     struct cdrom_read_audio ra;
     
     ra.addr.msf.minute =  pos / (60 * CD_FRAMES_PER_SECOND);
     ra.addr.msf.second = (pos / CD_FRAMES_PER_SECOND) % 60;
     ra.addr.msf.frame  =  pos % CD_FRAMES_PER_SECOND;
     ra.addr_format     = CDROM_MSF;
     ra.nframes         = len;
     ra.buf             = buf;
          
     if (ioctl( fd, CDROMREADAUDIO, &ra ) < 0) {
          D_PERROR( "IFusionSoundMusicProvider_CDDA: "
                    "ioctl( CDROMREADAUDIO ) failed!\n" );
          return -1;
     }
     
     return ra.nframes;
}

#elif defined(__FreeBSD__)

#include <sys/cdio.h>

static DFBResult
cdda_probe( int fd )
{
     struct ioc_toc_header tochdr;
     
     if (ioctl( fd, CDIOREADTOCHEADER, &tochdr ) < 0)
          return DFB_UNSUPPORTED;
          
     return DFB_OK;
}

static DFBResult
cdda_build_tracklits( int fd, struct cdda_track **ret_tracks,
                              unsigned int       *ret_num )
{
     int                               total_tracks;
     struct cdda_track                *tracks, *track;
     struct ioc_toc_header             tochdr;
     struct ioc_read_toc_single_entry  tocentry;
     union msf_lba                     msf_lba;
     int                               i;
     
     /* read TOC header */
     if (ioctl( fd, CDIOREADTOCHEADER, &tochdr ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDIOREADTOCHEADER ) failed!\n" );
          return DFB_IO;
     }
     
     total_tracks = tochdr.ending_track - tochdr.starting_track + 1;
          
     tracks = D_CALLOC( total_tracks+1, sizeof(struct cdda_track) );
     if (!tracks)
          return D_OOM();
     
     /* iterate through tracks */
     for (i = tochdr.starting_track, track = tracks; i <= tochdr.ending_track; i++) {          
          memset( &tocentry, 0, sizeof(tocentry) );
          
          tocentry.track          = i;
          tocentry.address_format = CD_MSF_FORMAT;
          if (ioctl( fd, CDIOREADTOCENTRY, &tocentry ) == -1) {
               D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                         "ioctl( CDIOREADTOCENTRY ) failed!\n" );
               D_FREE( tracks );
               return DFB_IO;
          }
          
          /* skip data tracks */
          if (tocentry.entry.control & 0x04) {
               total_tracks--;
               continue;
          }
          
          msf_lba = tocentry.entry.addr;
          track->start = (msf_lba.msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                         (msf_lba.msf.second      * CD_FRAMES_PER_SECOND) +
                          msf_lba.msf.frame;
          track++;
     }
     
     if (total_tracks < 1) {
          D_FREE( tracks );
          return DFB_FAILURE;
     }
     
     memset( &tocentry, 0, sizeof(tocentry) );
     
     /* get leadout track */
     tocentry.track          = 0xaa;
     tocentry.address_format = CD_MSF_FORMAT;
     if (ioctl( fd, CDIOREADTOCENTRY, &tocentry ) == -1) {
          D_PERROR( "IFusionSoundMusicProvide_CDDA: "
                    "ioctl( CDIOREADTOCENTRY ) failed!\n" );
          D_FREE( tracks );
          return DFB_IO;
     }          
     
     msf_lba = tocentry.entry.addr;
     tracks[total_tracks].start = (msf_lba.msf.minute * 60 * CD_FRAMES_PER_SECOND) +
                                  (msf_lba.msf.second      * CD_FRAMES_PER_SECOND) +
                                   msf_lba.msf.frame;
     
     /* compute tracks' length */
     for (i = 0; i < total_tracks; i++)
          tracks[i].length = tracks[i+1].start - tracks[i].start;
          
     *ret_tracks = tracks;
     *ret_num    = total_tracks;
     
     return DFB_OK;
}

static int
cdda_read_audio( int fd, __u8 *buf, int pos, int len )
{
     struct ioc_read_audio ra;
     
     ra.address.msf.minute =  pos / (60 * CD_FRAMES_PER_SECOND);
     ra.address.msf.second = (pos / CD_FRAMES_PER_SECOND) % 60;
     ra.address.msf.frame  =  pos % CD_FRAMES_PER_SECOND;
     ra.address_format     = CD_MSF_FORMAT;
     ra.nframes            = len;
     ra.buffer             = buf;
          
     if (ioctl( fd, CDIOCREADAUDIO, &ra ) < 0) {
          D_PERROR( "IFusionSoundMusicProvider_CDDA: "
                    "ioctl( CDIOCREADAUDIO ) failed!\n" );
          return -1;
     }
     
     return ra.nframes;
}

#else

static DFBResult
cdda_probe( int fd )
{
     D_WARN( "unsupported system" );
     return DFB_UNSUPPORTED;
}

struct DFBResult
cdda_build_tracklits( int fd, struct cdda_track **ret_tracks,
                              unsigned int       *ret_num )
{
     return DFB_UNSUPPORTED;
}

static int
cdda_read_audio( int fd, __u8 *buf, int pos, int frames )
{
     return -1;
}

{'fraction_non_alphanumeric': 0.09257901969766376, 'fraction_numerical': 0.008062299587723316, 'mean_word_length': 1.649071714597743, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1919340', 'n_tokens_mistral': 7330, 'n_tokens_neox': 7074, 'n_words': 1570}