summaryrefslogtreecommitdiff
path: root/lib/cdabstract.cpp
blob: d7cf18ec83ad62df68e1891e2b225b9f5142412a (plain)
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
#include "cdabstract.h"
#include "Input.h"
#include "cdreader.h"

#ifdef __linux__
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#endif

#if defined (_MSC_VER) || defined (__MINGW32__)
#include <windowsx.h>
#endif

Handle * cdabstract::open_cd(const String & nom) {
    if (nom.extract(0, 2).toupper() == "CD:") {
	return new cdreader(nom.extract(3));
    } else {
	return new Input(nom);
    }
}

bool cdabstract::canprobe() {
#ifdef __linux__
    return true;
#endif
#ifdef _WIN32
    OSVERSIONINFO ov;
    memset(&ov, 0, sizeof(OSVERSIONINFO));
    ov.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    GetVersionEx(&ov);
    
    return ((ov.dwPlatformId == VER_PLATFORM_WIN32_NT) && (ov.dwMajorVersion > 4));
#endif
    return false;
}

std::vector<String> cdabstract::probe() throw (GeneralException) {
    int i;
    std::vector<String> r;
    String probed;

    if (!canprobe())
	throw GeneralException("Can't probe CD devices on this platform.");

#ifdef __linux__
    if (subprobe(probed = "/dev/cdrom"))
	r.push_back(probed);

    for (i = 0; i < 63; i++) {
	probed.set("/dev/cdroms/cdrom%i", i);
	if (subprobe(probed))
	    r.push_back(probed);
    }
    for (i = 'a'; i <= 'z'; i++) {
	probed.set("/dev/hd%c", i);
	if (subprobe(probed))
	    r.push_back(probed);
    }
#endif

#ifdef _WIN32
    for (i = 'A'; i <= 'Z'; i++) {
	probed.set("%c:\\", i);
	if (subprobe(probed))
	    r.push_back(probed);
    }
#endif

    return r;
}

#ifdef _WIN32
HANDLE cdabstract::OpenIOCTLFile(char cLetter) {
    HANDLE hF;
    char szFName[16];
    OSVERSIONINFO ov;
    DWORD dwFlags;
    DWORD dwIOCTLAttr = 0;

    memset(&ov, 0, sizeof(OSVERSIONINFO));
    ov.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&ov);

    if((ov.dwPlatformId == VER_PLATFORM_WIN32_NT) && (ov.dwMajorVersion > 4))
	dwFlags = GENERIC_READ | GENERIC_WRITE;               // add gen write on W2k/XP
    else dwFlags = GENERIC_READ;

    sprintf(szFName, "\\\\.\\%c:", cLetter);

    hF = CreateFile(szFName, dwFlags, FILE_SHARE_READ,        // open drive
               NULL, OPEN_EXISTING, dwIOCTLAttr, NULL);

    if (hF == INVALID_HANDLE_VALUE) {                         // mmm... no success?
	dwFlags ^= GENERIC_WRITE;                             // -> try write toggle
	hF = CreateFile(szFName, dwFlags, FILE_SHARE_READ,    // -> open drive again
                 NULL, OPEN_EXISTING, dwIOCTLAttr, NULL);
	if (hF == INVALID_HANDLE_VALUE)
	    return NULL;
    }
    return hF;                                          
}

typedef struct {
  ULONG Length;
  UCHAR PortNumber;
  UCHAR PathId;
  UCHAR TargetId;
  UCHAR Lun;
} SCSI_ADDRESS, *PSCSI_ADDRESS;

void cdabstract::GetIOCTLAdapter(HANDLE hF, int * iDA, int * iDT, int * iDL) {
    char szBuf[1024];
    PSCSI_ADDRESS pSA;
    DWORD dwRet;

    *iDA = *iDT = *iDL = -1;

    if (hF == NULL)
	return;

    memset(szBuf,0,1024);

    pSA = (PSCSI_ADDRESS)szBuf;
    pSA->Length = sizeof(SCSI_ADDRESS);
                                                   
    if (!DeviceIoControl(hF, IOCTL_SCSI_GET_ADDRESS, NULL, 0, pSA,
	                 sizeof(SCSI_ADDRESS), &dwRet, NULL))
	return;

    *iDA = pSA->PortNumber;
    *iDT = pSA->TargetId;
    *iDL = pSA->Lun;
}
#endif

bool cdabstract::subprobe(String & probed) {
#ifdef __linux__
    int h, caps;

    h = open(probed.to_charp(), O_RDONLY | O_NONBLOCK);

    if (ioctl(h, CDROM_GET_CAPABILITY, &caps) < 0) {
	close(h);
	return false;
    } else {
	close(h);
	return true;
    }
#endif

#ifdef _WIN32
    int iDA, iDT, iDL;
    char letter[4];
    HANDLE h;

    if (GetDriveType(probed.to_charp()) == DRIVE_CDROM) {
	h = OpenIOCTLFile(probed[0]);
	GetIOCTLAdapter(h, &iDA, &iDT, &iDL);
	CloseHandle(h);
	if ((iDA != -1) && (iDT != -1) && (iDL != -1)) {
	    probed += String().set(" [%i:%i:%i]", iDA, iDT, iDL);
	    return true;
	}
    }
#endif

    return false;
}