Replaygain pre-amp can be set in 0.5 dB steps.
[kugel-rb.git] / rbutil / rbutilqt / base / autodetection.cpp
blobf233f0c3fd8f30b9aa713d71c72c482f016a4dcf
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <QtCore>
21 #include "autodetection.h"
22 #include "rbsettings.h"
23 #include "systeminfo.h"
25 #include "../ipodpatcher/ipodpatcher.h"
26 #include "../sansapatcher/sansapatcher.h"
28 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
29 #include <stdio.h>
30 #endif
31 #if defined(Q_OS_LINUX)
32 #include <mntent.h>
33 #endif
34 #if defined(Q_OS_MACX)
35 #include <sys/param.h>
36 #include <sys/ucred.h>
37 #include <sys/mount.h>
38 #endif
39 #if defined(Q_OS_WIN32)
40 #if defined(UNICODE)
41 #define _UNICODE
42 #endif
43 #include <stdio.h>
44 #include <tchar.h>
45 #include <windows.h>
46 #include <setupapi.h>
47 #include <winioctl.h>
48 #endif
50 #if defined(Q_OS_OPENBSD)
51 #include <sys/param.h>
52 #include <sys/mount.h>
53 #endif
55 #include "system.h"
56 #include "utils.h"
57 #include "rockboxinfo.h"
59 Autodetection::Autodetection(QObject* parent): QObject(parent)
63 bool Autodetection::detect()
65 m_device = "";
66 m_mountpoint = "";
67 m_errdev = "";
69 detectUsb();
71 // Try detection via rockbox.info / rbutil.log
72 QStringList mounts = mountpoints();
74 for(int i=0; i< mounts.size();i++)
76 // do the file checking
77 QDir dir(mounts.at(i));
78 qDebug() << "[Autodetect] paths to check:" << mounts;
79 if(dir.exists())
81 // check logfile first.
82 if(QFile(mounts.at(i) + "/.rockbox/rbutil.log").exists()) {
83 QSettings log(mounts.at(i) + "/.rockbox/rbutil.log",
84 QSettings::IniFormat, this);
85 if(!log.value("platform").toString().isEmpty()) {
86 if(m_device.isEmpty())
87 m_device = log.value("platform").toString();
88 m_mountpoint = mounts.at(i);
89 qDebug() << "[Autodetect] rbutil.log detected:" << m_device << m_mountpoint;
90 return true;
94 // check rockbox-info.txt afterwards.
95 RockboxInfo info(mounts.at(i));
96 if(info.success())
98 if(m_device.isEmpty())
100 m_device = info.target();
101 // special case for video64mb. This is a workaround, and
102 // should get replaced when autodetection is reworked.
103 if(m_device == "ipodvideo" && info.ram() == 64)
105 m_device = "ipodvideo64mb";
108 m_mountpoint = mounts.at(i);
109 qDebug() << "[Autodetect] rockbox-info.txt detected:"
110 << m_device << m_mountpoint;
111 return true;
114 // check for some specific files in root folder
115 QDir root(mounts.at(i));
116 QStringList rootentries = root.entryList(QDir::Files);
117 if(rootentries.contains("archos.mod", Qt::CaseInsensitive))
119 // archos.mod in root folder -> Archos Player
120 m_device = "player";
121 m_mountpoint = mounts.at(i);
122 return true;
124 if(rootentries.contains("ONDIOST.BIN", Qt::CaseInsensitive))
126 // ONDIOST.BIN in root -> Ondio FM
127 m_device = "ondiofm";
128 m_mountpoint = mounts.at(i);
129 return true;
131 if(rootentries.contains("ONDIOSP.BIN", Qt::CaseInsensitive))
133 // ONDIOSP.BIN in root -> Ondio SP
134 m_device = "ondiosp";
135 m_mountpoint = mounts.at(i);
136 return true;
138 if(rootentries.contains("ajbrec.ajz", Qt::CaseInsensitive))
140 qDebug() << "[Autodetect] ajbrec.ajz found. Trying detectAjbrec()";
141 if(detectAjbrec(mounts.at(i))) {
142 m_mountpoint = mounts.at(i);
143 qDebug() << "[Autodetect]" << m_device;
144 return true;
147 // detection based on player specific folders
148 QStringList rootfolders = root.entryList(QDir::Dirs
149 | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
150 if(rootfolders.contains("GBSYSTEM", Qt::CaseInsensitive))
152 // GBSYSTEM folder -> Gigabeat
153 m_device = "gigabeatf";
154 m_mountpoint = mounts.at(i);
155 return true;
157 #if defined(Q_OS_WIN32)
158 // on windows, try to detect the drive letter of an Ipod
159 if(rootfolders.contains("iPod_Control", Qt::CaseInsensitive))
161 // iPod_Control folder -> Ipod found
162 // detecting of the Ipod type is done below using ipodpatcher
163 m_mountpoint = mounts.at(i);
165 #endif
170 int n;
171 // try ipodpatcher
172 // initialize sector buffer. Needed.
173 ipod_sectorbuf = NULL;
174 ipod_alloc_buffer(&ipod_sectorbuf, BUFFER_SIZE);
175 struct ipod_t ipod;
176 n = ipod_scan(&ipod);
177 if(n == 1) {
178 qDebug() << "[Autodetect] Ipod found:" << ipod.modelstr << "at" << ipod.diskname;
179 // if the found ipod is a macpod also notice it as device with problem.
180 if(ipod.macpod)
181 m_errdev = ipod.targetname;
182 m_device = ipod.targetname;
183 // since resolveMountPoint is doing exact matches we need to select
184 // the correct partition.
185 QString mp(ipod.diskname);
186 #ifdef Q_OS_LINUX
187 mp.append("2");
188 #endif
189 #ifdef Q_OS_MACX
190 mp.append("s2");
191 #endif
192 m_mountpoint = resolveMountPoint(mp);
193 return true;
195 else {
196 qDebug() << "[Autodetect] ipodpatcher: no Ipod found." << n;
198 free(ipod_sectorbuf);
199 ipod_sectorbuf = NULL;
201 // try sansapatcher
202 // initialize sector buffer. Needed.
203 sansa_sectorbuf = NULL;
204 sansa_alloc_buffer(&sansa_sectorbuf, BUFFER_SIZE);
205 struct sansa_t sansa;
206 n = sansa_scan(&sansa);
207 if(n == 1) {
208 qDebug() << "[Autodetect] Sansa found:" << sansa.targetname << "at" << sansa.diskname;
209 m_device = QString("sansa%1").arg(sansa.targetname);
210 QString mp(sansa.diskname);
211 #ifdef Q_OS_LINUX
212 mp.append("1");
213 #endif
214 #ifdef Q_OS_MACX
215 mp.append("s1");
216 #endif
217 m_mountpoint = resolveMountPoint(mp);
218 return true;
220 else {
221 qDebug() << "[Autodetect] sansapatcher: no Sansa found." << n;
223 free(sansa_sectorbuf);
224 sansa_sectorbuf = NULL;
226 if(m_mountpoint.isEmpty() && m_device.isEmpty()
227 && m_errdev.isEmpty() && m_incompat.isEmpty())
228 return false;
229 return true;
233 QStringList Autodetection::mountpoints()
235 QStringList tempList;
236 #if defined(Q_OS_WIN32)
237 QFileInfoList list = QDir::drives();
238 for(int i=0; i<list.size();i++)
240 tempList << list.at(i).absolutePath();
243 #elif defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
244 int num;
245 struct statfs *mntinf;
247 num = getmntinfo(&mntinf, MNT_WAIT);
248 while(num--) {
249 tempList << QString(mntinf->f_mntonname);
250 mntinf++;
252 #elif defined(Q_OS_LINUX)
254 FILE *mn = setmntent("/etc/mtab", "r");
255 if(!mn)
256 return QStringList("");
258 struct mntent *ent;
259 while((ent = getmntent(mn)))
260 tempList << QString(ent->mnt_dir);
261 endmntent(mn);
263 #else
264 #error Unknown Plattform
265 #endif
266 return tempList;
270 /** resolve device name to mount point / drive letter
271 * @param device device name / disk number
272 * @return mount point / drive letter
274 QString Autodetection::resolveMountPoint(QString device)
276 qDebug() << "[Autodetect] resolving mountpoint:" << device;
278 #if defined(Q_OS_LINUX)
279 FILE *mn = setmntent("/etc/mtab", "r");
280 if(!mn)
281 return QString("");
283 struct mntent *ent;
284 while((ent = getmntent(mn))) {
285 // Check for valid filesystem. Allow hfs too, as an Ipod might be a
286 // MacPod.
287 if(QString(ent->mnt_fsname) == device
288 && (QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)
289 || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive))) {
290 endmntent(mn);
291 qDebug() << "[Autodetect] resolved mountpoint is:" << ent->mnt_dir;
292 return QString(ent->mnt_dir);
295 endmntent(mn);
297 #endif
299 #if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
300 int num;
301 struct statfs *mntinf;
303 num = getmntinfo(&mntinf, MNT_WAIT);
304 while(num--) {
305 // Check for valid filesystem. Allow hfs too, as an Ipod might be a
306 // MacPod.
307 if(QString(mntinf->f_mntfromname) == device
308 && (QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive)
309 || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive))) {
310 qDebug() << "[Autodetect] resolved mountpoint is:" << mntinf->f_mntonname;
311 return QString(mntinf->f_mntonname);
313 mntinf++;
315 #endif
317 #if defined(Q_OS_WIN32)
318 QString result;
319 unsigned int driveno = device.replace(QRegExp("^.*([0-9]+)"), "\\1").toInt();
321 int letter;
322 for(letter = 'A'; letter <= 'Z'; letter++) {
323 if(resolveDevicename(QString(letter)).toUInt() == driveno) {
324 result = letter;
325 break;
328 qDebug() << "[Autodetect] resolved mountpoint is:" << result;
329 if(!result.isEmpty())
330 return result + ":/";
331 #endif
332 return QString("");
336 /** Resolve mountpoint to devicename / disk number
337 * @param path mountpoint path / drive letter
338 * @return devicename / disk number
340 QString Autodetection::resolveDevicename(QString path)
342 qDebug() << "[Autodetect] resolving device name" << path;
343 #if defined(Q_OS_LINUX)
344 FILE *mn = setmntent("/etc/mtab", "r");
345 if(!mn)
346 return QString("");
348 struct mntent *ent;
349 while((ent = getmntent(mn))) {
350 // check for valid filesystem type.
351 // Linux can handle hfs (and hfsplus), so consider it a valid file
352 // system. Otherwise resolving the device name would fail, which in
353 // turn would make it impossible to warn about a MacPod.
354 if(QString(ent->mnt_dir) == path
355 && (QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)
356 || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive))) {
357 endmntent(mn);
358 qDebug() << "[Autodetect] device name is" << ent->mnt_fsname;
359 return QString(ent->mnt_fsname);
362 endmntent(mn);
364 #endif
366 #if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
367 int num;
368 struct statfs *mntinf;
370 num = getmntinfo(&mntinf, MNT_WAIT);
371 while(num--) {
372 // check for valid filesystem type. OS X can handle hfs (hfs+ is
373 // treated as hfs), BSD should be the same.
374 if(QString(mntinf->f_mntonname) == path
375 && (QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive)
376 || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive))) {
377 qDebug() << "[Autodetect] device name is" << mntinf->f_mntfromname;
378 return QString(mntinf->f_mntfromname);
380 mntinf++;
382 #endif
384 #if defined(Q_OS_WIN32)
385 DWORD written;
386 HANDLE h;
387 TCHAR uncpath[MAX_PATH];
388 UCHAR buffer[0x400];
389 PVOLUME_DISK_EXTENTS extents = (PVOLUME_DISK_EXTENTS)buffer;
391 _stprintf(uncpath, _TEXT("\\\\.\\%c:"), path.toAscii().at(0));
392 h = CreateFile(uncpath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
393 NULL, OPEN_EXISTING, 0, NULL);
394 if(h == INVALID_HANDLE_VALUE) {
395 //qDebug() << "error getting extents for" << uncpath;
396 return "";
398 // get the extents
399 if(DeviceIoControl(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
400 NULL, 0, extents, sizeof(buffer), &written, NULL)) {
401 if(extents->NumberOfDiskExtents > 1) {
402 qDebug() << "[Autodetect] resolving device name: volume spans multiple disks!";
403 return "";
405 qDebug() << "[Autodetect] device name is" << extents->Extents[0].DiskNumber;
406 return QString("%1").arg(extents->Extents[0].DiskNumber);
408 #endif
409 return QString("");
414 /** @brief detect devices based on usb pid / vid.
415 * @return true upon success, false otherwise.
417 bool Autodetection::detectUsb()
419 // usbids holds the mapping in the form
420 // ((VID<<16)|(PID)), targetname
421 // the ini file needs to hold the IDs as hex values.
422 QMap<int, QString> usbids = SystemInfo::usbIdMap(SystemInfo::MapDevice);
423 QMap<int, QString> usberror = SystemInfo::usbIdMap(SystemInfo::MapError);
424 QMap<int, QString> usbincompat = SystemInfo::usbIdMap(SystemInfo::MapIncompatible);
426 // usb pid detection
427 QList<uint32_t> attached;
428 attached = System::listUsbIds();
430 int i = attached.size();
431 while(i--) {
432 if(usbids.contains(attached.at(i))) {
433 m_device = usbids.value(attached.at(i));
434 qDebug() << "[USB] detected supported player" << m_device;
435 return true;
437 if(usberror.contains(attached.at(i))) {
438 m_errdev = usberror.value(attached.at(i));
439 qDebug() << "[USB] detected problem with player" << m_errdev;
440 return true;
442 QString idstring = QString("%1").arg(attached.at(i), 8, 16, QChar('0'));
443 if(!SystemInfo::platformValue(idstring, SystemInfo::CurName).toString().isEmpty()) {
444 m_incompat = idstring;
445 qDebug() << "[USB] detected incompatible player" << m_incompat;
446 return true;
449 return false;
453 bool Autodetection::detectAjbrec(QString root)
455 QFile f(root + "/ajbrec.ajz");
456 char header[24];
457 f.open(QIODevice::ReadOnly);
458 if(!f.read(header, 24)) return false;
460 // check the header of the file.
461 // recorder v1 had a 6 bytes sized header
462 // recorder v2, FM, Ondio SP and FM have a 24 bytes header.
464 // recorder v1 has the binary length in the first 4 bytes, so check
465 // for them first.
466 int len = (header[0]<<24) | (header[1]<<16) | (header[2]<<8) | header[3];
467 qDebug() << "[Autodetect] ABJREC possible bin length:" << len
468 << "file len:" << f.size();
469 if((f.size() - 6) == len)
470 m_device = "recorder";
472 // size didn't match, now we need to assume we have a headerlength of 24.
473 switch(header[11]) {
474 case 2:
475 m_device = "recorderv2";
476 break;
478 case 4:
479 m_device = "fmrecorder";
480 break;
482 case 8:
483 m_device = "ondiofm";
484 break;
486 case 16:
487 m_device = "ondiosp";
488 break;
490 default:
491 break;
493 f.close();
495 if(m_device.isEmpty()) return false;
496 return true;