| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2020 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <smooth.h> |
| 14 | #include <smooth/dll.h> |
| 15 | |
| 16 | extern "C" { |
| 17 | # include <cdda_interface.h> |
| 18 | } |
| 19 | |
| 20 | #include <glob.h> |
| 21 | #include <limits.h> |
| 22 | #include <arpa/inet.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | |
| 25 | #ifdef __linux__ |
| 26 | # include <linux/cdrom.h> |
| 27 | #else |
| 28 | # include <sys/cdio.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "cdparanoia.h" |
| 32 | |
| 33 | static Int numDrives = 0; |
| 34 | |
| 35 | const String &BoCA::DeviceInfoCDParanoia::GetComponentSpecs() |
| 36 | { |
| 37 | static String componentSpecs; |
| 38 | |
| 39 | if (numDrives >= 1) |
| 40 | { |
| 41 | componentSpecs = " \ |
| 42 | \ |
| 43 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 44 | <component> \ |
| 45 | <name>cdparanoia Device Info Component</name> \ |
| 46 | <version>1.0</version> \ |
| 47 | <id>cdparanoia-info</id> \ |
| 48 | <type>deviceinfo</type> \ |
| 49 | </component> \ |
| 50 | \ |
| 51 | "; |
| 52 | } |
| 53 | |
| 54 | return componentSpecs; |
| 55 | } |
| 56 | |
| 57 | const Array<String> &BoCA::DeviceInfoCDParanoia::FindDrives() |
| 58 | { |
| 59 | #if defined __linux__ |
| 60 | static const char *deviceNames[] = { "/dev/hd?", "/dev/sr?", "/dev/scd?", NIL }; |
| 61 | #elif defined __FreeBSD__ |
| 62 | static const char *deviceNames[] = { "/dev/acd?", "/dev/cd?", NIL }; |
| 63 | #elif defined __OpenBSD__ |
| 64 | static const char *deviceNames[] = { "/dev/cd?c", NIL }; |
| 65 | #elif defined __NetBSD__ |
| 66 | static const char *deviceNames[] = { "/dev/cd?d", NIL }; |
| 67 | #else |
| 68 | static const char *deviceNames[] = { "/dev/cdrom?", NIL }; |
| 69 | #endif |
| 70 | |
| 71 | static Array<String> driveNames; |
| 72 | static Bool initialized = False; |
| 73 | |
| 74 | if (initialized) return driveNames; |
| 75 | |
| 76 | for (Int i = 0; deviceNames[i] != NIL; i++) |
| 77 | { |
| 78 | glob_t fileData = { 0 }; |
| 79 | |
| 80 | if (glob(deviceNames[i], 0, NIL, &fileData) == 0) |
| 81 | { |
| 82 | for (UnsignedInt n = 0; n < fileData.gl_pathc; n++) |
| 83 | { |
| 84 | cdrom_drive *cd = cdda_identify(fileData.gl_pathv[n], CDDA_MESSAGE_FORGETIT, NIL); |
| 85 | |
| 86 | if (cd != NIL) |
| 87 | { |
| 88 | driveNames.Add(fileData.gl_pathv[n]); |
| 89 | |
| 90 | cdda_close(cd); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | globfree(&fileData); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | initialized = True; |
| 99 | |
| 100 | return driveNames; |
| 101 | } |
| 102 | |
| 103 | Void smooth::AttachDLL(Void *instance) |
| 104 | { |
| 105 | BoCA::Config *config = BoCA::Config::Get(); |
| 106 | const Array<String> &driveNames = BoCA::DeviceInfoCDParanoia::FindDrives(); |
| 107 | |
| 108 | numDrives = driveNames.Length(); |
| 109 | |
| 110 | if (numDrives <= config->GetIntValue("Ripper", "ActiveDrive", 0)) config->SetIntValue("Ripper", "ActiveDrive", 0); |
| 111 | } |
| 112 | |
| 113 | Void smooth::DetachDLL() |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | namespace BoCA |
| 118 | { |
| 119 | static Bool OpenDriveTray(Int n) |
| 120 | { |
| 121 | const Array<String> &driveNames = DeviceInfoCDParanoia::FindDrives(); |
| 122 | cdrom_drive *cd = cdda_identify(driveNames.GetNth(n), CDDA_MESSAGE_FORGETIT, NIL); |
| 123 | |
| 124 | if (cd == NIL) return False; |
| 125 | |
| 126 | cdda_open(cd); |
| 127 | |
| 128 | /* Unlock tray, then eject. |
| 129 | */ |
| 130 | #if defined __linux__ |
| 131 | ioctl(cd->ioctl_fd, CDROM_LOCKDOOR, 0); |
| 132 | ioctl(cd->ioctl_fd, CDROMEJECT); |
| 133 | #else |
| 134 | ioctl(cd->ioctl_fd, CDIOCALLOW); |
| 135 | ioctl(cd->ioctl_fd, CDIOCEJECT); |
| 136 | #endif |
| 137 | |
| 138 | cdda_close(cd); |
| 139 | |
| 140 | return True; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | BoCA::DeviceInfoCDParanoia::DeviceInfoCDParanoia() |
| 145 | { |
| 146 | CollectDriveInfo(); |
| 147 | } |
| 148 | |
| 149 | BoCA::DeviceInfoCDParanoia::~DeviceInfoCDParanoia() |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | Bool BoCA::DeviceInfoCDParanoia::IsNthDeviceTrayOpen(Int n) |
| 154 | { |
| 155 | const Array<String> &driveNames = FindDrives(); |
| 156 | cdrom_drive *cd = cdda_identify(driveNames.GetNth(n), CDDA_MESSAGE_FORGETIT, NIL); |
| 157 | |
| 158 | if (cd == NIL) return False; |
| 159 | |
| 160 | cdda_open(cd); |
| 161 | |
| 162 | /* Get tray status. |
| 163 | */ |
| 164 | Bool status = False; |
| 165 | |
| 166 | #if defined __linux__ |
| 167 | status = (ioctl(cd->ioctl_fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_TRAY_OPEN); |
| 168 | #endif |
| 169 | |
| 170 | cdda_close(cd); |
| 171 | |
| 172 | return status; |
| 173 | } |
| 174 | |
| 175 | Bool BoCA::DeviceInfoCDParanoia::OpenNthDeviceTray(Int n) |
| 176 | { |
| 177 | Int nDrives = GetNumberOfDevices(); |
| 178 | |
| 179 | if (n >= nDrives) return False; |
| 180 | |
| 181 | NonBlocking1<Int>(&OpenDriveTray).Call(n); |
| 182 | |
| 183 | return True; |
| 184 | } |
| 185 | |
| 186 | Bool BoCA::DeviceInfoCDParanoia::CloseNthDeviceTray(Int n) |
| 187 | { |
| 188 | const Array<String> &driveNames = FindDrives(); |
| 189 | cdrom_drive *cd = cdda_identify(driveNames.GetNth(n), CDDA_MESSAGE_FORGETIT, NIL); |
| 190 | |
| 191 | if (cd == NIL) return False; |
| 192 | |
| 193 | cdda_open(cd); |
| 194 | |
| 195 | /* Close tray. |
| 196 | */ |
| 197 | #if defined __linux__ |
| 198 | ioctl(cd->ioctl_fd, CDROMCLOSETRAY); |
| 199 | #else |
| 200 | ioctl(cd->ioctl_fd, CDIOCCLOSE); |
| 201 | #endif |
| 202 | |
| 203 | cdda_close(cd); |
| 204 | |
| 205 | return True; |
| 206 | } |
| 207 | |
| 208 | const Array<String> &BoCA::DeviceInfoCDParanoia::GetNthDeviceTrackList(Int n) |
| 209 | { |
| 210 | static Array<String> trackList; |
| 211 | |
| 212 | trackList.RemoveAll(); |
| 213 | |
| 214 | const MCDI &mcdi = GetNthDeviceMCDI(n); |
| 215 | |
| 216 | for (Int i = 0; i < mcdi.GetNumberOfEntries(); i++) |
| 217 | { |
| 218 | /* Find all valid audio tracks (tracks with a positive length). |
| 219 | */ |
| 220 | if (mcdi.GetNthEntryType(i) == ENTRY_AUDIO && mcdi.GetNthEntryOffset(i + 1) - mcdi.GetNthEntryOffset(i) > 0) |
| 221 | { |
| 222 | /* Look for hidden track before the first track (HTOA). |
| 223 | */ |
| 224 | if (i == 0 && mcdi.GetNthEntryOffset(i) >= 450) |
| 225 | { |
| 226 | trackList.Add(String("device://cdda:") |
| 227 | .Append(String::FromInt(n)) |
| 228 | .Append("/") |
| 229 | .Append(String::FromInt(0))); |
| 230 | } |
| 231 | |
| 232 | /* Add CD track to joblist using a device:// URI |
| 233 | */ |
| 234 | trackList.Add(String("device://cdda:") |
| 235 | .Append(String::FromInt(n)) |
| 236 | .Append("/") |
| 237 | .Append(String::FromInt(mcdi.GetNthEntryTrackNumber(i)))); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return trackList; |
| 242 | } |
| 243 | |
| 244 | const BoCA::MCDI &BoCA::DeviceInfoCDParanoia::GetNthDeviceMCDI(Int n) |
| 245 | { |
| 246 | static MCDI mcdi = MCDI(Buffer<UnsignedByte>()); |
| 247 | |
| 248 | /* Do not read the TOC again if the last request |
| 249 | * was less than a quarter of a second ago. |
| 250 | */ |
| 251 | static Int lastDrive = -1; |
| 252 | static UnsignedInt64 lastAccess = 0; |
| 253 | |
| 254 | { |
| 255 | UnsignedInt64 clockValue = S::System::System::Clock(); |
| 256 | |
| 257 | if (lastDrive == n && clockValue - lastAccess < 250) |
| 258 | { |
| 259 | lastAccess = clockValue; |
| 260 | |
| 261 | return mcdi; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | mcdi.SetData(Buffer<UnsignedByte>()); |
| 266 | |
| 267 | const Array<String> &driveNames = FindDrives(); |
| 268 | cdrom_drive *cd = cdda_identify(driveNames.GetNth(n), CDDA_MESSAGE_FORGETIT, NIL); |
| 269 | |
| 270 | if (cd != NIL) |
| 271 | { |
| 272 | typedef struct |
| 273 | { |
| 274 | uint8_t rsvd; |
| 275 | uint8_t ADR; |
| 276 | uint8_t trackNumber; |
| 277 | uint8_t rsvd2; |
| 278 | uint32_t addr; |
| 279 | } __attribute__((__packed__)) TOCTRACK; |
| 280 | |
| 281 | typedef struct |
| 282 | { |
| 283 | uint16_t tocLen; |
| 284 | uint8_t firstTrack; |
| 285 | uint8_t lastTrack; |
| 286 | TOCTRACK tracks[100]; |
| 287 | } __attribute__((__packed__)) TOC; |
| 288 | |
| 289 | if (cdda_open(cd) == 0) |
| 290 | { |
| 291 | TOC toc; |
| 292 | |
| 293 | toc.tocLen = htons(2 + cd->tracks * 8 + 8); |
| 294 | toc.firstTrack = 1; |
| 295 | toc.lastTrack = cd->tracks; |
| 296 | |
| 297 | for (Int i = 0; i <= cd->tracks; i++) |
| 298 | { |
| 299 | toc.tracks[i].rsvd = 0; |
| 300 | toc.tracks[i].ADR = cd->disc_toc[i].bFlags; |
| 301 | toc.tracks[i].trackNumber = cd->disc_toc[i].bTrack; |
| 302 | toc.tracks[i].rsvd2 = 0; |
| 303 | toc.tracks[i].addr = htonl(cd->disc_toc[i].dwStartSector); |
| 304 | |
| 305 | #ifndef __FreeBSD__ |
| 306 | /* cdparanoia accounts for the session gap on mixed mode discs, so we need to undo that to build the |
| 307 | * correct MCDI. The FreeBSD port has the corresponding code removed, so this is not necessary there. |
| 308 | */ |
| 309 | if ((i > 1 && (cd->disc_toc[i - 1].bFlags & 4) != (cd->disc_toc[i].bFlags & 4) && cd->disc_toc[i].bTrack != 0xAA) || |
| 310 | (i < cd->tracks && cd->disc_toc[i + 1].dwStartSector - cd->disc_toc[i].dwStartSector <= 0)) |
| 311 | { |
| 312 | toc.tracks[i].addr = htonl(cd->disc_toc[i].dwStartSector + 11400); |
| 313 | } |
| 314 | #endif |
| 315 | } |
| 316 | |
| 317 | Buffer<UnsignedByte> buffer(ntohs(toc.tocLen) + 2); |
| 318 | |
| 319 | memcpy(buffer, &toc, ntohs(toc.tocLen) + 2); |
| 320 | |
| 321 | mcdi.SetData(buffer); |
| 322 | } |
| 323 | |
| 324 | cdda_close(cd); |
| 325 | } |
| 326 | |
| 327 | lastDrive = n; |
| 328 | lastAccess = S::System::System::Clock(); |
| 329 | |
| 330 | return mcdi; |
| 331 | } |
| 332 | |
| 333 | Void BoCA::DeviceInfoCDParanoia::CollectDriveInfo() |
| 334 | { |
| 335 | static Bool initialized = False; |
| 336 | |
| 337 | if (initialized) return; |
| 338 | |
| 339 | const Array<String> &driveNames = FindDrives(); |
| 340 | |
| 341 | foreach (const String &driveName, driveNames) |
| 342 | { |
| 343 | cdrom_drive *cd = cdda_identify(driveName, CDDA_MESSAGE_FORGETIT, NIL); |
| 344 | |
| 345 | if (cd == NIL) continue; |
| 346 | |
| 347 | Device drive; |
| 348 | String model = String(cd->drive_model).Trim(); |
| 349 | |
| 350 | drive.type = DEVICE_CDROM; |
| 351 | |
| 352 | drive.vendor = model.Head( model.Find(" ") ).Trim(); |
| 353 | drive.model = model.SubString(model.Find(" ") + 1, model.FindLast(" ") - model.Find(" ") - 1).Trim(); |
| 354 | drive.revision = model.Tail( model.Length() - model.FindLast(" ") - 1).Trim(); |
| 355 | |
| 356 | drive.path = driveName; |
| 357 | |
| 358 | drive.canOpenTray = True; |
| 359 | |
| 360 | devices.Add(drive); |
| 361 | |
| 362 | cdda_close(cd); |
| 363 | } |
| 364 | |
| 365 | initialized = True; |
| 366 | } |