$OpenBSD: patch-kioslave_audiocd_audiocd_cpp,v 1.3 2001/09/19 14:13:28 espie Exp $
--- kioslave/audiocd/audiocd.cpp.orig	Wed Sep  5 01:16:12 2001
+++ kioslave/audiocd/audiocd.cpp	Tue Sep 18 23:31:54 2001
@@ -41,8 +41,13 @@ extern "C"
 
 /* This is in support for the Mega Hack, if cdparanoia ever is fixed, or we
    use another ripping library we can remove this.  */
+#ifdef __linux__
 #include <linux/cdrom.h>
+#endif
 #include <sys/ioctl.h>
+#if defined(__NetBSD__) || defined(__OpenBSD__)
+#include <sys/cdio.h>
+#endif
 
 #ifdef HAVE_LAME
 #include <lame/lame.h>
@@ -104,14 +109,24 @@ int FixupTOC(cdrom_drive *d, int tracks)
   start_of_first_data_as_in_toc = -1;
   hack_track = -1;
   if (d->ioctl_fd != -1) {
+#ifdef __linux__
     struct cdrom_multisession ms_str;
     ms_str.addr_format = CDROM_LBA;
     if (ioctl(d->ioctl_fd, CDROMMULTISESSION, &ms_str) == -1)
       return -1;
-    if (ms_str.addr.lba > 100) {
+#define ms_addr (ms_str.addr.lba)
+#endif
+#if defined(__NetBSD__) || defined(__OpenBSD__)
+  int ms_addr;
+  ms_addr = 0;   /* last session */
+  if (ioctl(d->ioctl_fd, CDIOREADMSADDR, &ms_addr) == -1)
+        return -1;
+#endif
+
+    if (ms_addr > 100) {
       for (j = tracks-1; j >= 0; j--)
         if (j > 0 && !IS_AUDIO(d,j) && IS_AUDIO(d,j-1)) {
-          if (d->disc_toc[j].dwStartSector > ms_str.addr.lba - 11400) {
+          if (d->disc_toc[j].dwStartSector > ms_addr - 11400) {
             /* The next two code lines are the purpose of duplicating this
              * function, all others are an exact copy of paranoias FixupTOC().
              * The gory details: CD-Extra consist of N audio-tracks in the
@@ -132,7 +147,7 @@ int FixupTOC(cdrom_drive *d, int tracks)
              * length is only implicitely given.  Bloody sh*.  */
             start_of_first_data_as_in_toc = d->disc_toc[j].dwStartSector;
             hack_track = j + 1;
-            d->disc_toc[j].dwStartSector = ms_str.addr.lba - 11400;
+            d->disc_toc[j].dwStartSector = ms_addr - 11400;
           }
           break;
         }
@@ -273,11 +288,13 @@ AudioCDProtocol::initRequest(const KURL 
 {
 
 #ifdef HAVE_LAME
-  if (NULL == (d->gf = lame_init())) { // init the lame_global_flags structure with defaults
-    error(KIO::ERR_DOES_NOT_EXIST, url.path());
-    return 0;
-  }
-  id3tag_init (d->gf);
+  d->gf = lame_init();  // init the lame_global_flags structure with defaults
+  if (d->gf != NULL)
+    id3tag_init (d->gf);
+//  if (NULL == (d->gf = lame_init())) { 
+ //   error(KIO::ERR_DOES_NOT_EXIST, url.path());
+  //  return 0;
+//  }
 #endif
 
 #ifdef HAVE_VORBIS
@@ -429,7 +446,7 @@ AudioCDProtocol::get(const KURL & url)
  QString filetype = determineFiletype(d->fname);
 
 #ifdef HAVE_LAME
-  if (filetype == "mp3" && d->based_on_cddb && d->write_id3) {
+  if (filetype == "mp3" && d->gf != NULL && d->based_on_cddb && d->write_id3) {
     /* If CDDB is used to determine the filenames, tell lame to append ID3v1 TAG to MP3 Files */
     const char *tname =   d->titles[trackNumber-1].latin1();    // set trackname
     id3tag_set_album(d->gf, d->cd_title.latin1());
@@ -746,8 +763,11 @@ AudioCDProtocol::listDir(const KURL & ur
       listEntry(entry, false);
 
 #ifdef HAVE_LAME
-      app_dir(entry, d->s_mp3, d->tracks);
-      listEntry(entry, false);
+      if (d->gf != NULL)
+        {
+          app_dir(entry, d->s_mp3, d->tracks);
+          listEntry(entry, false);
+        }
 #endif
 
 #ifdef HAVE_VORBIS
@@ -1075,7 +1095,7 @@ static char mp3buffer[mp3buffer_size];
       ++currentSector;
 
 #ifdef HAVE_LAME
-      if ( filetype == "mp3" ) {
+      if ( filetype == "mp3" && d->gf != NULL) {
          int mp3bytes =
            lame_encode_buffer_interleaved(d->gf,buf,CD_FRAMESAMPLES,(unsigned char *)mp3buffer,(int)mp3buffer_size);
 
@@ -1158,7 +1178,7 @@ static char mp3buffer[mp3buffer_size];
     }
   }
 #ifdef HAVE_LAME
-  if (filetype == "mp3") {
+  if (filetype == "mp3" && d->gf != NULL) {
      int mp3bytes = lame_encode_finish(d->gf,(unsigned char *)mp3buffer,(int)mp3buffer_size);
 
      if (mp3bytes < 0 ) {
@@ -1224,6 +1244,8 @@ void AudioCDProtocol::getParameters() {
 
 #ifdef HAVE_LAME
 
+  if (d->gf != NULL)
+  {
   config->setGroup("MP3");
 
   int quality = config->readNumEntry("quality",2);
@@ -1311,6 +1333,7 @@ void AudioCDProtocol::getParameters() {
       lame_set_highpasswidth(d->gf, config->readNumEntry("highpassfilter_width",0));
     }
 
+  }
   }
 #endif // HAVE_LAME
 
