| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2021 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 | #include <time.h> |
| 17 | |
| 18 | #include "cdrip.h" |
| 19 | |
| 20 | const String &BoCA::DeviceInfoCDRip::GetComponentSpecs() |
| 21 | { |
| 22 | static String componentSpecs; |
| 23 | |
| 24 | if (cdripdll != NIL && ex_CR_GetNumCDROM() >= 1) |
| 25 | { |
| 26 | componentSpecs = " \ |
| 27 | \ |
| 28 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 29 | <component> \ |
| 30 | <name>CDRip Ripper Device Info Component</name> \ |
| 31 | <version>1.0</version> \ |
| 32 | <id>cdrip-info</id> \ |
| 33 | <type>deviceinfo</type> \ |
| 34 | </component> \ |
| 35 | \ |
| 36 | "; |
| 37 | } |
| 38 | |
| 39 | return componentSpecs; |
| 40 | } |
| 41 | |
| 42 | static Bool initializedCDRip = False; |
| 43 | |
| 44 | Void smooth::AttachDLL(Void *instance) |
| 45 | { |
| 46 | LoadCDRipDLL(); |
| 47 | |
| 48 | if (cdripdll == NIL) return; |
| 49 | |
| 50 | if (!ex_CR_IsInitialized()) |
| 51 | { |
| 52 | BoCA::Config *config = BoCA::Config::Get(); |
| 53 | Long error = ex_CR_Init(True); |
| 54 | |
| 55 | if (error == CDEX_ACCESSDENIED) BoCA::Utilities::ErrorMessage("Access to CD-ROM drives was denied by Windows.\n\nPlease contact your system administrator in order\nto be granted the right to access the CD-ROM drive."); |
| 56 | else if (error != CDEX_OK && |
| 57 | error != CDEX_NOCDROMDEVICES) BoCA::Utilities::ErrorMessage("Unable to load NT SCSI drivers! CD ripping disabled!"); |
| 58 | |
| 59 | if (ex_CR_GetNumCDROM() <= config->GetIntValue("Ripper", "ActiveDrive", 0)) config->SetIntValue("Ripper", "ActiveDrive", 0); |
| 60 | |
| 61 | initializedCDRip = True; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Void smooth::DetachDLL() |
| 66 | { |
| 67 | if (cdripdll == NIL) return; |
| 68 | |
| 69 | if (initializedCDRip) ex_CR_DeInit(); |
| 70 | |
| 71 | FreeCDRipDLL(); |
| 72 | } |
| 73 | |
| 74 | namespace BoCA |
| 75 | { |
| 76 | static Bool OpenDriveTray(Int n) |
| 77 | { |
| 78 | Int nDrives = ex_CR_GetNumCDROM(); |
| 79 | |
| 80 | if (n >= nDrives) return False; |
| 81 | |
| 82 | CDROMDRIVE *cd = ex_CR_OpenCDROM(n); |
| 83 | |
| 84 | ex_CR_EjectCD(cd, True); |
| 85 | ex_CR_CloseCDROM(cd); |
| 86 | |
| 87 | return True; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | BoCA::DeviceInfoCDRip::DeviceInfoCDRip() |
| 92 | { |
| 93 | CollectDriveInfo(); |
| 94 | } |
| 95 | |
| 96 | BoCA::DeviceInfoCDRip::~DeviceInfoCDRip() |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | Bool BoCA::DeviceInfoCDRip::IsNthDeviceTrayOpen(Int n) |
| 101 | { |
| 102 | Int nDrives = ex_CR_GetNumCDROM(); |
| 103 | |
| 104 | if (n >= nDrives) return False; |
| 105 | |
| 106 | CDROMDRIVE *cd = ex_CR_OpenCDROM(n); |
| 107 | CDMEDIASTATUS status; |
| 108 | |
| 109 | ex_CR_IsMediaLoaded(cd, status); |
| 110 | ex_CR_CloseCDROM(cd); |
| 111 | |
| 112 | return (status == CDMEDIA_NOT_PRESENT_TRAY_OPEN); |
| 113 | } |
| 114 | |
| 115 | Bool BoCA::DeviceInfoCDRip::OpenNthDeviceTray(Int n) |
| 116 | { |
| 117 | Int nDrives = ex_CR_GetNumCDROM(); |
| 118 | |
| 119 | if (n >= nDrives) return False; |
| 120 | |
| 121 | NonBlocking1<Int>(&OpenDriveTray).Call(n); |
| 122 | |
| 123 | return True; |
| 124 | } |
| 125 | |
| 126 | Bool BoCA::DeviceInfoCDRip::CloseNthDeviceTray(Int n) |
| 127 | { |
| 128 | Int nDrives = ex_CR_GetNumCDROM(); |
| 129 | |
| 130 | if (n >= nDrives) return False; |
| 131 | |
| 132 | CDROMDRIVE *cd = ex_CR_OpenCDROM(n); |
| 133 | |
| 134 | ex_CR_EjectCD(cd, False); |
| 135 | ex_CR_CloseCDROM(cd); |
| 136 | |
| 137 | return True; |
| 138 | } |
| 139 | |
| 140 | const Array<String> &BoCA::DeviceInfoCDRip::GetNthDeviceTrackList(Int n) |
| 141 | { |
| 142 | static Array<String> trackList; |
| 143 | |
| 144 | trackList.RemoveAll(); |
| 145 | |
| 146 | const MCDI &mcdi = GetNthDeviceMCDI(n); |
| 147 | |
| 148 | for (Int i = 0; i < mcdi.GetNumberOfEntries(); i++) |
| 149 | { |
| 150 | /* Find all valid audio tracks (tracks with a positive length). |
| 151 | */ |
| 152 | if (mcdi.GetNthEntryType(i) == ENTRY_AUDIO && mcdi.GetNthEntryOffset(i + 1) - mcdi.GetNthEntryOffset(i) > 0) |
| 153 | { |
| 154 | /* Look for hidden track before the first track (HTOA). |
| 155 | */ |
| 156 | if (i == 0 && mcdi.GetNthEntryOffset(i) >= 450) |
| 157 | { |
| 158 | trackList.Add(String("device://cdda:") |
| 159 | .Append(String::FromInt(n)) |
| 160 | .Append("/") |
| 161 | .Append(String::FromInt(0))); |
| 162 | } |
| 163 | |
| 164 | /* Add CD track to joblist using a device:// URI |
| 165 | */ |
| 166 | trackList.Add(String("device://cdda:") |
| 167 | .Append(String::FromInt(n)) |
| 168 | .Append("/") |
| 169 | .Append(String::FromInt(mcdi.GetNthEntryTrackNumber(i)))); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return trackList; |
| 174 | } |
| 175 | |
| 176 | const BoCA::MCDI &BoCA::DeviceInfoCDRip::GetNthDeviceMCDI(Int n) |
| 177 | { |
| 178 | static MCDI mcdi = MCDI(Buffer<UnsignedByte>()); |
| 179 | |
| 180 | /* Do not read the TOC again if the last request |
| 181 | * was less than a quarter of a second ago. |
| 182 | */ |
| 183 | static Int lastDrive = -1; |
| 184 | static UnsignedInt64 lastAccess = 0; |
| 185 | |
| 186 | { |
| 187 | UnsignedInt64 clockValue = S::System::System::Clock(); |
| 188 | |
| 189 | if (lastDrive == n && clockValue - lastAccess < 250) |
| 190 | { |
| 191 | lastAccess = clockValue; |
| 192 | |
| 193 | return mcdi; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | mcdi.SetData(Buffer<UnsignedByte>()); |
| 198 | |
| 199 | Int nDrives = ex_CR_GetNumCDROM(); |
| 200 | |
| 201 | if (n < nDrives) |
| 202 | { |
| 203 | CDROMDRIVE *cd = ex_CR_OpenCDROM(n); |
| 204 | |
| 205 | if (ex_CR_ReadToc(cd) == CDEX_OK) |
| 206 | { |
| 207 | Int numTocEntries = ex_CR_GetNumTocEntries(cd); |
| 208 | Buffer<UnsignedByte> buffer(4 + 8 * numTocEntries + 8); |
| 209 | |
| 210 | ((UnsignedInt16 *) (UnsignedByte *) buffer)[0] = htons(buffer.Size() - 2); |
| 211 | |
| 212 | buffer[2] = ex_CR_GetTocEntry(cd, 0).btTrackNumber; |
| 213 | buffer[3] = ex_CR_GetTocEntry(cd, numTocEntries - 1).btTrackNumber; |
| 214 | |
| 215 | for (Int i = 0; i <= numTocEntries; i++) |
| 216 | { |
| 217 | TOCENTRY entry = ex_CR_GetTocEntry(cd, i); |
| 218 | |
| 219 | buffer[4 + 8 * i + 0] = 0; |
| 220 | buffer[4 + 8 * i + 1] = entry.btFlag; |
| 221 | buffer[4 + 8 * i + 2] = entry.btTrackNumber; |
| 222 | buffer[4 + 8 * i + 3] = 0; |
| 223 | |
| 224 | Int32 address = entry.dwStartSector; |
| 225 | |
| 226 | if (address < 0) address = ~((-address) - 1) & ((1 << 24) - 1); |
| 227 | |
| 228 | ((UnsignedInt32 *) (UnsignedByte *) buffer)[1 + 2 * i + 1] = htonl(address); |
| 229 | } |
| 230 | |
| 231 | mcdi.SetData(buffer); |
| 232 | } |
| 233 | |
| 234 | ex_CR_CloseCDROM(cd); |
| 235 | } |
| 236 | |
| 237 | lastDrive = n; |
| 238 | lastAccess = S::System::System::Clock(); |
| 239 | |
| 240 | return mcdi; |
| 241 | } |
| 242 | |
| 243 | Void BoCA::DeviceInfoCDRip::CollectDriveInfo() |
| 244 | { |
| 245 | static Bool initialized = False; |
| 246 | |
| 247 | if (initialized) return; |
| 248 | |
| 249 | Int nDrives = ex_CR_GetNumCDROM(); |
| 250 | |
| 251 | for (Int i = 0; i < nDrives; i++) |
| 252 | { |
| 253 | CDROMDRIVE *cd = ex_CR_OpenCDROM(i); |
| 254 | CDROMPARAMS params; |
| 255 | |
| 256 | if (cd == NIL) continue; |
| 257 | |
| 258 | ex_CR_GetCDROMParameters(cd, ¶ms); |
| 259 | |
| 260 | Device drive; |
| 261 | |
| 262 | drive.type = DEVICE_CDROM; |
| 263 | |
| 264 | drive.vendor = String(params.lpszCDROMID).SubString(0, 8).Trim(); |
| 265 | drive.model = String(params.lpszCDROMID).SubString(8, 16).Trim(); |
| 266 | |
| 267 | drive.path = String::FromInt(params.btAdapterID).Append(":").Append(String::FromInt(params.btTargetID)).Append(":").Append(String::FromInt(params.btLunID)); |
| 268 | |
| 269 | drive.canOpenTray = True; |
| 270 | |
| 271 | devices.Add(drive); |
| 272 | |
| 273 | ex_CR_CloseCDROM(cd); |
| 274 | } |
| 275 | |
| 276 | initialized = True; |
| 277 | } |