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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/* This file is part of the KDE Project
Copyright (c) 2003 Gav Wood <gav kde org>
Copyright (c) 2004 Kévin Ottens <ervin ipsquad net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/* Some code of this file comes from kdeautorun */
#include "linuxcdpolling.h"
#include <tqthread.h>
#include <tqmutex.h>
#include <tqfile.h>
#include <kdebug.h>
#include "fstabbackend.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
// Never ever include directly a kernel header!
// #include <linux/cdrom.h>
// Instead we redefine the necessary (copied from the header)
/* This struct is used by the CDROMREADTOCHDR ioctl */
struct cdrom_tochdr
{
unsigned char cdth_trk0; /* start track */
unsigned char cdth_trk1; /* end track */
};
#define CDROMREADTOCHDR 0x5305 /* Read TOC header
(struct cdrom_tochdr) */
#define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
#define CDROM_DISC_STATUS 0x5327 /* Get disc type, etc. */
/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
#define CDS_NO_INFO 0 /* if not implemented */
#define CDS_NO_DISC 1
#define CDS_TRAY_OPEN 2
#define CDS_DRIVE_NOT_READY 3
#define CDS_DISC_OK 4
/* return values for the CDROM_DISC_STATUS ioctl */
/* can also return CDS_NO_[INFO|DISC], from above */
#define CDS_AUDIO 100
#define CDS_DATA_1 101
#define CDS_DATA_2 102
#define CDS_XA_2_1 103
#define CDS_XA_2_2 104
#define CDS_MIXED 105
#define CDSL_CURRENT ((int) (~0U>>1))
// -------
DiscType::DiscType(Type type)
: m_type(type)
{
}
bool DiscType::isKnownDisc() const
{
return m_type != None
&& m_type != Unknown
&& m_type != UnknownType
&& m_type != Broken;
}
bool DiscType::isDisc() const
{
return m_type != None
&& m_type != Unknown
&& m_type != Broken;
}
bool DiscType::isNotDisc() const
{
return m_type == None;
}
bool DiscType::isData() const
{
return m_type == Data;
}
DiscType::operator int() const
{
return (int)m_type;
}
class PollingThread : public QThread
{
public:
PollingThread(const TQCString &devNode) : m_dev(devNode)
{
kdDebug(1219) << "PollingThread::PollingThread("
<< devNode << ")" << endl;
m_stop = false;
m_currentType = DiscType::None;
m_lastPollType = DiscType::None;
}
void stop()
{
TQMutexLocker locker(&m_mutex);
m_stop = true;
}
bool hasChanged()
{
TQMutexLocker locker(&m_mutex);
return m_currentType!=m_lastPollType;
}
DiscType type()
{
TQMutexLocker locker(&m_mutex);
m_currentType = m_lastPollType;
return m_currentType;
}
protected:
virtual void run()
{
kdDebug(1219) << "PollingThread(" << m_dev << ") start" << endl;
while (!m_stop && m_lastPollType!=DiscType::Broken)
{
m_mutex.lock();
DiscType type = m_lastPollType;
m_mutex.unlock();
type = LinuxCDPolling::identifyDiscType(m_dev, type);
m_mutex.lock();
m_lastPollType = type;
m_mutex.unlock();
msleep(500);
}
kdDebug(1219) << "PollingThread(" << m_dev << ") stop" << endl;
}
private:
TQMutex m_mutex;
bool m_stop;
const TQCString m_dev;
DiscType m_currentType;
DiscType m_lastPollType;
};
LinuxCDPolling::LinuxCDPolling(MediaList &list)
: TQObject(), BackendBase(list)
{
connect(&m_mediaList, TQT_SIGNAL(mediumAdded(const TQString &,
const TQString &, bool)),
this, TQT_SLOT(slotMediumAdded(const TQString &)) );
connect(&m_mediaList, TQT_SIGNAL(mediumRemoved(const TQString &,
const TQString &, bool)),
this, TQT_SLOT(slotMediumRemoved(const TQString &)) );
connect(&m_mediaList, TQT_SIGNAL(mediumStateChanged(const TQString &,
const TQString &, bool, bool)),
this, TQT_SLOT(slotMediumStateChanged(const TQString &)) );
connect(&m_timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotTimeout()));
}
LinuxCDPolling::~LinuxCDPolling()
{
TQMap<TQString, PollingThread*>::iterator it = m_threads.begin();
TQMap<TQString, PollingThread*>::iterator end = m_threads.end();
for(; it!=end; ++it)
{
PollingThread *thread = it.data();
thread->stop();
thread->wait();
delete thread;
}
}
void LinuxCDPolling::slotMediumAdded(const TQString &id)
{
kdDebug(1219) << "LinuxCDPolling::slotMediumAdded(" << id << ")" << endl;
if (m_threads.contains(id)) return;
const Medium *medium = m_mediaList.findById(id);
TQString mime = medium->mimeType();
kdDebug(1219) << "mime == " << mime << endl;
if (mime.find("dvd")==-1 && mime.find("cd")==-1) return;
if (!medium->isMounted())
{
m_excludeNotification.append( id );
TQCString dev = TQFile::encodeName( medium->deviceNode() ).data();
PollingThread *thread = new PollingThread(dev);
m_threads[id] = thread;
thread->start();
m_timer.start(500);
}
}
void LinuxCDPolling::slotMediumRemoved(const TQString &id)
{
kdDebug(1219) << "LinuxCDPolling::slotMediumRemoved(" << id << ")" << endl;
if (!m_threads.contains(id)) return;
PollingThread *thread = m_threads[id];
m_threads.remove(id);
thread->stop();
thread->wait();
delete thread;
m_excludeNotification.remove(id);
}
void LinuxCDPolling::slotMediumStateChanged(const TQString &id)
{
kdDebug(1219) << "LinuxCDPolling::slotMediumStateChanged("
<< id << ")" << endl;
const Medium *medium = m_mediaList.findById(id);
TQString mime = medium->mimeType();
kdDebug(1219) << "mime == " << mime << endl;
if (mime.find("dvd")==-1 && mime.find("cd")==-1) return;
if (!m_threads.contains(id) && !medium->isMounted())
{
// It is just a mount state change, no need to notify
m_excludeNotification.append( id );
TQCString dev = TQFile::encodeName( medium->deviceNode() ).data();
PollingThread *thread = new PollingThread(dev);
m_threads[id] = thread;
thread->start();
m_timer.start(500);
}
else if (m_threads.contains(id) && medium->isMounted())
{
PollingThread *thread = m_threads[id];
m_threads.remove(id);
thread->stop();
thread->wait();
delete thread;
}
}
void LinuxCDPolling::slotTimeout()
{
//kdDebug(1219) << "LinuxCDPolling::slotTimeout()" << endl;
if (m_threads.isEmpty())
{
m_timer.stop();
return;
}
TQMap<TQString, PollingThread*>::iterator it = m_threads.begin();
TQMap<TQString, PollingThread*>::iterator end = m_threads.end();
for(; it!=end; ++it)
{
TQString id = it.key();
PollingThread *thread = it.data();
if (thread->hasChanged())
{
DiscType type = thread->type();
const Medium *medium = m_mediaList.findById(id);
applyType(type, medium);
}
}
}
static TQString baseType(const Medium *medium)
{
kdDebug(1219) << "baseType(" << medium->id() << ")" << endl;
TQString devNode = medium->deviceNode();
TQString mountPoint = medium->mountPoint();
TQString fsType = medium->fsType();
bool mounted = medium->isMounted();
TQString mimeType, iconName, label;
FstabBackend::guess(devNode, mountPoint, fsType, mounted,
mimeType, iconName, label);
if (devNode.find("dvd")!=-1)
{
kdDebug(1219) << "=> dvd" << endl;
return "dvd";
}
else
{
kdDebug(1219) << "=> cd" << endl;
return "cd";
}
}
static void restoreEmptyState(MediaList &list, const Medium *medium,
bool allowNotification)
{
kdDebug(1219) << "restoreEmptyState(" << medium->id() << ")" << endl;
TQString id = medium->id();
TQString devNode = medium->deviceNode();
TQString mountPoint = medium->mountPoint();
TQString fsType = medium->fsType();
bool mounted = medium->isMounted();
TQString mimeType, iconName, label;
FstabBackend::guess(devNode, mountPoint, fsType, mounted,
mimeType, iconName, label);
list.changeMediumState(id, devNode, mountPoint, fsType, mounted,
allowNotification, mimeType, iconName, label);
}
void LinuxCDPolling::applyType(DiscType type, const Medium *medium)
{
kdDebug(1219) << "LinuxCDPolling::applyType(" << type << ", "
<< medium->id() << ")" << endl;
TQString id = medium->id();
TQString dev = medium->deviceNode();
bool notify = !m_excludeNotification.contains(id);
m_excludeNotification.remove(id);
switch (type)
{
case DiscType::Data:
restoreEmptyState(m_mediaList, medium, notify);
break;
case DiscType::Audio:
case DiscType::Mixed:
m_mediaList.changeMediumState(id, "audiocd:/?device="+dev,
notify, "media/audiocd");
break;
case DiscType::VCD:
m_mediaList.changeMediumState(id, false, notify, "media/vcd");
break;
case DiscType::SVCD:
m_mediaList.changeMediumState(id, false, notify, "media/svcd");
break;
case DiscType::DVD:
m_mediaList.changeMediumState(id, false, notify, "media/dvdvideo");
break;
case DiscType::Blank:
if (baseType(medium)=="dvd")
{
m_mediaList.changeMediumState(id, false,
notify, "media/blankdvd");
}
else
{
m_mediaList.changeMediumState(id, false,
notify, "media/blankcd");
}
break;
case DiscType::None:
case DiscType::Unknown:
case DiscType::UnknownType:
restoreEmptyState(m_mediaList, medium, false);
break;
}
}
DiscType LinuxCDPolling::identifyDiscType(const TQCString &devNode,
const DiscType ¤t)
{
//kdDebug(1219) << "LinuxCDPolling::identifyDiscType("
// << devNode << ")" << endl;
int fd;
struct cdrom_tochdr th;
// open the device
fd = open(devNode, O_RDONLY | O_NONBLOCK);
if (fd < 0) return DiscType::Broken;
switch (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT))
{
case CDS_DISC_OK:
{
if (current.isDisc())
{
close(fd);
return current;
}
// see if we can read the disc's table of contents (TOC).
if (ioctl(fd, CDROMREADTOCHDR, &th))
{
close(fd);
return DiscType::Blank;
}
// read disc status info
int status = ioctl(fd, CDROM_DISC_STATUS, CDSL_CURRENT);
// release the device
close(fd);
switch (status)
{
case CDS_AUDIO:
return DiscType::Audio;
case CDS_DATA_1:
case CDS_DATA_2:
if (hasDirectory(devNode, "video_ts"))
{
return DiscType::DVD;
}
else if (hasDirectory(devNode, "vcd"))
{
return DiscType::VCD;
}
else if (hasDirectory(devNode, "svcd"))
{
return DiscType::SVCD;
}
else
{
return DiscType::Data;
}
case CDS_MIXED:
return DiscType::Mixed;
default:
return DiscType::UnknownType;
}
}
case CDS_NO_INFO:
close(fd);
return DiscType::Unknown;
default:
close(fd);
return DiscType::None;
}
}
bool LinuxCDPolling::hasDirectory(const TQCString &devNode, const TQCString &dir)
{
bool ret = false; // return value
int fd = 0; // file descriptor for drive
unsigned short bs; // the discs block size
unsigned short ts; // the path table size
unsigned int tl; // the path table location (in blocks)
unsigned char len_di = 0; // length of the directory name in current path table entry
unsigned int parent = 0; // the number of the parent directory's path table entry
char dirname[256]; // filename for the current path table entry
int pos = 0; // our position into the path table
int curr_record = 1; // the path table record we're on
TQCString fixed_directory = dir.upper(); // the uppercase version of the "directory" parameter
// open the drive
fd = open(devNode, O_RDONLY | O_NONBLOCK);
if (fd == -1) return false;
// read the block size
lseek(fd, 0x8080, SEEK_CUR);
if (read(fd, &bs, 2) != 2)
{
close(fd);
return false;
}
if (Q_BYTE_ORDER != Q_LITTLE_ENDIAN)
bs = ((bs << 8) & 0xFF00) | ((bs >> 8) & 0xFF);
// read in size of path table
lseek(fd, 2, SEEK_CUR);
if (read(fd, &ts, 2) != 2)
{
close(fd);
return false;
}
if (Q_BYTE_ORDER != Q_LITTLE_ENDIAN)
ts = ((ts << 8) & 0xFF00) | ((ts >> 8) & 0xFF);
// read in which block path table is in
lseek(fd, 6, SEEK_CUR);
if (read(fd, &tl, 4) != 4)
{
close(fd);
return false;
}
if (Q_BYTE_ORDER != Q_LITTLE_ENDIAN)
tl = ((tl << 24) & 0xFF000000) | ((tl << 8) & 0xFF0000) |
((tl >> 8) & 0xFF00) | ((tl >> 24) & 0xFF);
// seek to the path table
lseek(fd, bs * tl, SEEK_SET);
// loop through the path table entries
while (pos < ts)
{
// get the length of the filename of the current entry
if (read(fd, &len_di, 1) != 1)
{
ret = false;
break;
}
// get the record number of this entry's parent
// i'm pretty sure that the 1st entry is always the top directory
lseek(fd, 5, SEEK_CUR);
if (read(fd, &parent, 2) != 2)
{
ret = false;
break;
}
if (Q_BYTE_ORDER != Q_LITTLE_ENDIAN)
parent = ((parent << 8) & 0xFF00) | ((parent >> 8) & 0xFF);
// read the name
if (read(fd, dirname, len_di) != len_di)
{
ret = false;
break;
}
dirname[len_di] = 0;
qstrcpy(dirname, TQCString(dirname).upper());
// if we found a folder that has the root as a parent, and the directory name matches
// then return success
if ((parent == 1) && (dirname == fixed_directory))
{
ret = true;
break;
}
// all path table entries are padded to be even, so if this is an odd-length table, seek a byte to fix it
if (len_di%2 == 1)
{
lseek(fd, 1, SEEK_CUR);
pos++;
}
// update our position
pos += 8 + len_di;
curr_record++;
}
close(fd);
return ret;
}
#include "linuxcdpolling.moc"
|