File: mb_beos.cpp

package info (click to toggle)
mkcue 1-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 528 kB
  • ctags: 252
  • sloc: sh: 2,618; cpp: 1,747; makefile: 83
file content (224 lines) | stat: -rw-r--r-- 5,839 bytes parent folder | download | duplicates (10)
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
/* --------------------------------------------------------------------------

   MusicBrainz -- The Internet music metadatabase

   Copyright (C) 2000 Robert Kaye
   Copyright (C) 1999 Marc E E van Woerkom
   Copyright (C) 1999 Stephen van Egmond
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   
   This library 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
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id: mb_beos.cpp,v 1.3 2001/07/06 21:32:13 robert Exp $

----------------------------------------------------------------------------*/

#include "mb.h"
#include "diskid.h"
#include "config.h"

// BeOS layer includes
#include <Path.h>
#include <Directory.h>
#include <String.h>
#include <scsi.h>
#include <Roster.h>

// POSIX layer includes
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>


// forward declarations
static void FindCDPlayerDevice();
static uint32 msf_to_lba(uint8 m, uint8 s, uint8 f);

// global variables
BString gCDPlayerDeviceName;
int gCDPlayerDevice;

// initializer -- call before using above globals
status_t gConfigureGlobals() {
    gCDPlayerDevice = 0;
	
    FindCDPlayerDevice();
    if (gCDPlayerDevice == 0) {
        return B_DEV_BAD_DRIVE_NUM;
    }
	
    return B_NO_ERROR;
}

bool DiskId::ReadTOC(char *device, 
                     MUSICBRAINZ_CDINFO& cdinfo) {
    // how does it get the device?
    status_t rc = gConfigureGlobals();
    if (rc != B_NO_ERROR) {
        char err[255];
        sprintf(err, "Error while accessing the CD drive: %s.", 
                strerror(rc));
        ReportError(err);

        return false;
    }

    scsi_toc toc;

    rc = ioctl(gCDPlayerDevice, B_SCSI_GET_TOC, &toc);

    if (rc != B_NO_ERROR) {
        char err[255];
        sprintf(err, "Error while accessing %s: %s.", 
                gCDPlayerDeviceName.String(), strerror(rc));
        ReportError(err);

        return false;
    }	

    /*

      typedef struct {
          byte FirstTrack;         // The first track on CD : normally 1
          byte LastTrack;          // The last track on CD: max number 99
        
          dword FrameOffset[100];  // Track 2 is TrackFrameOffset[2] etc.

          // Leadout Track will be TrackFrameOffset[0]
	
      } MUSICBRAINZ_CDINFO, *PMUSICBRAINZ_CDINFO; 

    */
	

    /*

      SCSI toc format

        http://www.symbios.com/t10
        
        All multibyte values big-endian
        
        uint16 data_length;  // of following data, not including these 2 bytes
        uint8  first_track;
        uint8  last_track;

        struct {
            uint8 reserved;
            uint8 flag_bits; // ADR and CONTROL, whatever
            uint8 track_number;
            uint8 reserved2;
            uint32 logical_block_address;
        } [n];

        // where n fills out the data length
    */


    uint16 data_length = * ((uint16*)toc.toc_data);
    data_length = B_BENDIAN_TO_HOST_INT16(data_length);
    data_length += 2; // to include the length itself
	
    cdinfo.FirstTrack = toc.toc_data[2];
    cdinfo.LastTrack = toc.toc_data[3];
    assert(cdinfo.FirstTrack >= 1 && cdinfo.FirstTrack<=99);
    assert(cdinfo.LastTrack >= 1 && cdinfo.LastTrack<=99);

    int indexer = 4;
    while (indexer < data_length) {
        indexer+=2;
        int track_number = toc.toc_data[indexer];
        indexer+=2;
        uint32 lba = msf_to_lba(toc.toc_data[indexer+1],toc.toc_data[indexer+2],toc.toc_data[indexer+3]);
					
        indexer+=4;

        assert(track_number == 0xaa || (track_number>=1 && track_number <= cdinfo.LastTrack));
        if (track_number == 0xaa)
            track_number = 0;
		
        cdinfo.FrameOffset[track_number] = lba + 150;
    }

    return true;
}


static bool try_dir(const char *directory)
{ 
    BDirectory dir; 
    dir.SetTo(directory); 
    if(dir.InitCheck() != B_NO_ERROR) { 
        return false;
    } 
    dir.Rewind(); 
    BEntry entry; 
    while(dir.GetNextEntry(&entry) >= 0) { 
        BPath path; 
        const char *name; 
        entry_ref e; 
		
        if(entry.GetPath(&path) != B_NO_ERROR) 
            continue; 
        name = path.Path(); 
		
		
        if(entry.GetRef(&e) != B_NO_ERROR) 
            continue; 

        if(entry.IsDirectory()) { 
            if(strcmp(e.name, "floppy") == 0) 
                continue; // ignore floppy (it is not silent) 
            if (try_dir(name))
                return true;
        } 
        else { 
            int devfd; 
            device_geometry g; 

            if(strcmp(e.name, "raw") != 0) 
                continue; // ignore partitions 

            devfd = open(name, O_RDONLY); 
            if(devfd < 0) 
                continue; 

            if(ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0) {
                if(g.device_type == B_CD)
                    { 
                        gCDPlayerDevice = devfd;
                        gCDPlayerDeviceName = name;
                        return true;
                    }
            }
            close(devfd);
        } 
    }

    return false;
}


static void FindCDPlayerDevice()
{
    try_dir("/dev/disk");
}


static uint32 msf_to_lba(uint8 m, uint8 s, uint8 f) 
{ 
    return (((m * 60) + s) * 75 + f) - 150; 
}