MPEGPlayer: Skip to next file when there is a problem with a video file in all-play...
[kugel-rb.git] / rbutil / rbutilqt / base / autodetection.cpp
blob2bd61ff5bac88d8cbddf02fc1f33484551e3de29
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();
102 m_mountpoint = mounts.at(i);
103 qDebug() << "[Autodetect] rockbox-info.txt detected:"
104 << m_device << m_mountpoint;
105 return true;
108 // check for some specific files in root folder
109 QDir root(mounts.at(i));
110 QStringList rootentries = root.entryList(QDir::Files);
111 if(rootentries.contains("archos.mod", Qt::CaseInsensitive))
113 // archos.mod in root folder -> Archos Player
114 m_device = "player";
115 m_mountpoint = mounts.at(i);
116 return true;
118 if(rootentries.contains("ONDIOST.BIN", Qt::CaseInsensitive))
120 // ONDIOST.BIN in root -> Ondio FM
121 m_device = "ondiofm";
122 m_mountpoint = mounts.at(i);
123 return true;
125 if(rootentries.contains("ONDIOSP.BIN", Qt::CaseInsensitive))
127 // ONDIOSP.BIN in root -> Ondio SP
128 m_device = "ondiosp";
129 m_mountpoint = mounts.at(i);
130 return true;
132 if(rootentries.contains("ajbrec.ajz", Qt::CaseInsensitive))
134 qDebug() << "[Autodetect] ajbrec.ajz found. Trying detectAjbrec()";
135 if(detectAjbrec(mounts.at(i))) {
136 m_mountpoint = mounts.at(i);
137 qDebug() << "[Autodetect]" << m_device;
138 return true;
141 // detection based on player specific folders
142 QStringList rootfolders = root.entryList(QDir::Dirs
143 | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
144 if(rootfolders.contains("GBSYSTEM", Qt::CaseInsensitive))
146 // GBSYSTEM folder -> Gigabeat
147 m_device = "gigabeatf";
148 m_mountpoint = mounts.at(i);
149 return true;
151 #if defined(Q_OS_WIN32)
152 // on windows, try to detect the drive letter of an Ipod
153 if(rootfolders.contains("iPod_Control", Qt::CaseInsensitive))
155 // iPod_Control folder -> Ipod found
156 // detecting of the Ipod type is done below using ipodpatcher
157 m_mountpoint = mounts.at(i);
159 #endif
164 int n;
165 // try ipodpatcher
166 // initialize sector buffer. Needed.
167 ipod_sectorbuf = NULL;
168 ipod_alloc_buffer(&ipod_sectorbuf, BUFFER_SIZE);
169 struct ipod_t ipod;
170 n = ipod_scan(&ipod);
171 if(n == 1) {
172 qDebug() << "[Autodetect] Ipod found:" << ipod.modelstr << "at" << ipod.diskname;
173 // if the found ipod is a macpod also notice it as device with problem.
174 if(ipod.macpod)
175 m_errdev = ipod.targetname;
176 m_device = ipod.targetname;
177 // since resolveMountPoint is doing exact matches we need to select
178 // the correct partition.
179 QString mp(ipod.diskname);
180 #ifdef Q_OS_LINUX
181 mp.append("2");
182 #endif
183 #ifdef Q_OS_MACX
184 mp.append("s2");
185 #endif
186 m_mountpoint = resolveMountPoint(mp);
187 return true;
189 else {
190 qDebug() << "[Autodetect] ipodpatcher: no Ipod found." << n;
192 free(ipod_sectorbuf);
193 ipod_sectorbuf = NULL;
195 // try sansapatcher
196 // initialize sector buffer. Needed.
197 sansa_sectorbuf = NULL;
198 sansa_alloc_buffer(&sansa_sectorbuf, BUFFER_SIZE);
199 struct sansa_t sansa;
200 n = sansa_scan(&sansa);
201 if(n == 1) {
202 qDebug() << "[Autodetect] Sansa found:" << sansa.targetname << "at" << sansa.diskname;
203 m_device = QString("sansa%1").arg(sansa.targetname);
204 QString mp(sansa.diskname);
205 #ifdef Q_OS_LINUX
206 mp.append("1");
207 #endif
208 #ifdef Q_OS_MACX
209 mp.append("s1");
210 #endif
211 m_mountpoint = resolveMountPoint(mp);
212 return true;
214 else {
215 qDebug() << "[Autodetect] sansapatcher: no Sansa found." << n;
217 free(sansa_sectorbuf);
218 sansa_sectorbuf = NULL;
220 if(m_mountpoint.isEmpty() && m_device.isEmpty()
221 && m_errdev.isEmpty() && m_incompat.isEmpty())
222 return false;
223 return true;
227 QStringList Autodetection::mountpoints()
229 QStringList tempList;
230 #if defined(Q_OS_WIN32)
231 QFileInfoList list = QDir::drives();
232 for(int i=0; i<list.size();i++)
234 tempList << list.at(i).absolutePath();
235 qDebug() << "[Autodetection] Mounted on" << list.at(i).absolutePath();
238 #elif defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
239 int num;
240 struct statfs *mntinf;
242 num = getmntinfo(&mntinf, MNT_WAIT);
243 while(num--) {
244 tempList << QString(mntinf->f_mntonname);
245 qDebug() << "[Autodetection] Mounted on" << mntinf->f_mntonname
246 << "is" << mntinf->f_mntfromname << "type" << mntinf->f_fstypename;
247 mntinf++;
249 #elif defined(Q_OS_LINUX)
251 FILE *mn = setmntent("/etc/mtab", "r");
252 if(!mn)
253 return QStringList("");
255 struct mntent *ent;
256 while((ent = getmntent(mn))) {
257 tempList << QString(ent->mnt_dir);
258 qDebug() << "[Autodetection] Mounted on" << ent->mnt_dir
259 << "is" << ent->mnt_fsname << "type" << ent->mnt_type;
261 endmntent(mn);
263 #else
264 #error Unknown Platform
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 result;
289 if(QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)
290 || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive)) {
291 qDebug() << "[Autodetect] resolved mountpoint is:" << ent->mnt_dir;
292 result = QString(ent->mnt_dir);
294 else {
295 qDebug() << "[Autodetect] mountpoint is wrong filesystem!";
297 endmntent(mn);
298 return result;
301 endmntent(mn);
303 #endif
305 #if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
306 int num;
307 struct statfs *mntinf;
309 num = getmntinfo(&mntinf, MNT_WAIT);
310 while(num--) {
311 // Check for valid filesystem. Allow hfs too, as an Ipod might be a
312 // MacPod.
313 if(QString(mntinf->f_mntfromname) == device) {
314 if(QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive)
315 || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive)) {
316 qDebug() << "[Autodetect] resolved mountpoint is:" << mntinf->f_mntonname;
317 return QString(mntinf->f_mntonname);
319 else {
320 qDebug() << "[Autodetect] mountpoint is wrong filesystem!";
321 return QString();
324 mntinf++;
326 #endif
328 #if defined(Q_OS_WIN32)
329 QString result;
330 unsigned int driveno = device.replace(QRegExp("^.*([0-9]+)"), "\\1").toInt();
332 int letter;
333 for(letter = 'A'; letter <= 'Z'; letter++) {
334 if(resolveDevicename(QString(letter)).toUInt() == driveno) {
335 result = letter;
336 qDebug() << "[Autodetect] resolved mountpoint is:" << result;
337 break;
340 if(!result.isEmpty())
341 return result + ":/";
342 #endif
343 qDebug() << "[Autodetect] resolving mountpoint failed!";
344 return QString("");
348 /** Resolve mountpoint to devicename / disk number
349 * @param path mountpoint path / drive letter
350 * @return devicename / disk number
352 QString Autodetection::resolveDevicename(QString path)
354 qDebug() << "[Autodetect] resolving device name" << path;
355 #if defined(Q_OS_LINUX)
356 FILE *mn = setmntent("/etc/mtab", "r");
357 if(!mn)
358 return QString("");
360 struct mntent *ent;
361 while((ent = getmntent(mn))) {
362 // check for valid filesystem type.
363 // Linux can handle hfs (and hfsplus), so consider it a valid file
364 // system. Otherwise resolving the device name would fail, which in
365 // turn would make it impossible to warn about a MacPod.
366 if(QString(ent->mnt_dir) == path
367 && (QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)
368 || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive))) {
369 endmntent(mn);
370 qDebug() << "[Autodetect] device name is" << ent->mnt_fsname;
371 return QString(ent->mnt_fsname);
374 endmntent(mn);
376 #endif
378 #if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
379 int num;
380 struct statfs *mntinf;
382 num = getmntinfo(&mntinf, MNT_WAIT);
383 while(num--) {
384 // check for valid filesystem type. OS X can handle hfs (hfs+ is
385 // treated as hfs), BSD should be the same.
386 if(QString(mntinf->f_mntonname) == path
387 && (QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive)
388 || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive))) {
389 qDebug() << "[Autodetect] device name is" << mntinf->f_mntfromname;
390 return QString(mntinf->f_mntfromname);
392 mntinf++;
394 #endif
396 #if defined(Q_OS_WIN32)
397 DWORD written;
398 HANDLE h;
399 TCHAR uncpath[MAX_PATH];
400 UCHAR buffer[0x400];
401 PVOLUME_DISK_EXTENTS extents = (PVOLUME_DISK_EXTENTS)buffer;
403 _stprintf(uncpath, _TEXT("\\\\.\\%c:"), path.toAscii().at(0));
404 h = CreateFile(uncpath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
405 NULL, OPEN_EXISTING, 0, NULL);
406 if(h == INVALID_HANDLE_VALUE) {
407 //qDebug() << "error getting extents for" << uncpath;
408 return "";
410 // get the extents
411 if(DeviceIoControl(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
412 NULL, 0, extents, sizeof(buffer), &written, NULL)) {
413 if(extents->NumberOfDiskExtents > 1) {
414 qDebug() << "[Autodetect] resolving device name: volume spans multiple disks!";
415 return "";
417 qDebug() << "[Autodetect] device name is" << extents->Extents[0].DiskNumber;
418 return QString("%1").arg(extents->Extents[0].DiskNumber);
420 #endif
421 return QString("");
426 /** @brief detect devices based on usb pid / vid.
427 * @return true upon success, false otherwise.
429 bool Autodetection::detectUsb()
431 // usbids holds the mapping in the form
432 // ((VID<<16)|(PID)), targetname
433 // the ini file needs to hold the IDs as hex values.
434 QMap<int, QString> usbids = SystemInfo::usbIdMap(SystemInfo::MapDevice);
435 QMap<int, QString> usberror = SystemInfo::usbIdMap(SystemInfo::MapError);
436 QMap<int, QString> usbincompat = SystemInfo::usbIdMap(SystemInfo::MapIncompatible);
438 // usb pid detection
439 QList<uint32_t> attached;
440 attached = System::listUsbIds();
442 int i = attached.size();
443 while(i--) {
444 if(usbids.contains(attached.at(i))) {
445 m_device = usbids.value(attached.at(i));
446 qDebug() << "[USB] detected supported player" << m_device;
447 return true;
449 if(usberror.contains(attached.at(i))) {
450 m_errdev = usberror.value(attached.at(i));
451 qDebug() << "[USB] detected problem with player" << m_errdev;
452 return true;
454 QString idstring = QString("%1").arg(attached.at(i), 8, 16, QChar('0'));
455 if(!SystemInfo::platformValue(idstring, SystemInfo::CurName).toString().isEmpty()) {
456 m_incompat = idstring;
457 qDebug() << "[USB] detected incompatible player" << m_incompat;
458 return true;
461 return false;
465 bool Autodetection::detectAjbrec(QString root)
467 QFile f(root + "/ajbrec.ajz");
468 char header[24];
469 f.open(QIODevice::ReadOnly);
470 if(!f.read(header, 24)) return false;
472 // check the header of the file.
473 // recorder v1 had a 6 bytes sized header
474 // recorder v2, FM, Ondio SP and FM have a 24 bytes header.
476 // recorder v1 has the binary length in the first 4 bytes, so check
477 // for them first.
478 int len = (header[0]<<24) | (header[1]<<16) | (header[2]<<8) | header[3];
479 qDebug() << "[Autodetect] ABJREC possible bin length:" << len
480 << "file len:" << f.size();
481 if((f.size() - 6) == len)
482 m_device = "recorder";
484 // size didn't match, now we need to assume we have a headerlength of 24.
485 switch(header[11]) {
486 case 2:
487 m_device = "recorderv2";
488 break;
490 case 4:
491 m_device = "fmrecorder";
492 break;
494 case 8:
495 m_device = "ondiofm";
496 break;
498 case 16:
499 m_device = "ondiosp";
500 break;
502 default:
503 break;
505 f.close();
507 if(m_device.isEmpty()) return false;
508 return true;