MythTV  master
mythmediamonitor.cpp
Go to the documentation of this file.
1 
2 // Standard C headers
3 #include <cstdio>
4 
5 // C++ headers
6 #include <iostream>
7 #include <typeinfo>
8 using namespace std;
9 
10 // Qt headers
11 #include <QCoreApplication>
12 #include <QFile>
13 #include <QList>
14 #include <QDir>
15 
16 // MythTV headers
17 #include "mythmediamonitor.h"
18 #include "mythcdrom.h"
19 #include "mythcorecontext.h"
20 #include "mythconfig.h"
21 #include "mythdialogbox.h"
22 #include "mythdate.h"
23 #include "mythlogging.h"
24 #include "mythmainwindow.h"
25 #include "mythsystemlegacy.h"
26 #include "mythmiscutil.h"
27 
28 #ifdef USING_DARWIN_DA
29 #include "mediamonitor-darwin.h"
30 #elif CONFIG_CYGWIN || defined(_WIN32)
31 #include "mediamonitor-windows.h"
32 #else
33 #include "mediamonitor-unix.h"
34 #endif
35 
36 static const QString _Location = QObject::tr("Media Monitor");
37 
39 
40 // MonitorThread
41 MonitorThread::MonitorThread(MediaMonitor* pMon, unsigned long interval) :
42  MThread("Monitor")
43 {
44  m_Monitor = pMon;
45  m_Interval = interval;
46  m_lastCheckTime = QDateTime::currentDateTimeUtc();
47 }
48 
49 // Nice and simple, as long as our monitor is valid and active,
50 // loop and check it's devices.
52 {
53  RunProlog();
54  QMutex mtx;
55  mtx.lock();
56  while (m_Monitor && m_Monitor->IsActive())
57  {
58  m_Monitor->CheckDevices();
59  m_Monitor->m_wait.wait(&mtx, m_Interval);
60  QDateTime now(QDateTime::currentDateTimeUtc());
61  // if 10 seconds have elapsed instead of 5 seconds
62  // assume the system was suspended and reconnect
63  // sockets
64  if (m_lastCheckTime.secsTo(now) > 120)
65  {
67  if (HasMythMainWindow())
68  {
69  LOG(VB_GENERAL, LOG_INFO, "Restarting LIRC handler");
71  }
72  }
73  m_lastCheckTime = now;
74  }
75  mtx.unlock();
76  RunEpilog();
77 }
78 
80 // MediaMonitor
81 
82 #define MONITOR_INTERVAL 5000
83 
85 {
86  if (s_monitor)
87  return s_monitor;
88 
89 #ifdef USING_DARWIN_DA
90  s_monitor = new MediaMonitorDarwin(nullptr, MONITOR_INTERVAL, true);
91 #else
92  #if CONFIG_CYGWIN || defined(_WIN32)
93  s_monitor = new MediaMonitorWindows(nullptr, MONITOR_INTERVAL, true);
94  #else
95  s_monitor = new MediaMonitorUnix(nullptr, MONITOR_INTERVAL, true);
96  #endif
97 #endif
98 
99  return s_monitor;
100 }
101 
102 void MediaMonitor::SetCDSpeed(const char *device, int speed)
103 {
104  MediaMonitor *mon = GetMediaMonitor();
105  if (mon)
106  {
107  MythMediaDevice *pMedia = mon->GetMedia(device);
108  if (pMedia && mon->ValidateAndLock(pMedia))
109  {
110  pMedia->setSpeed(speed);
111  mon->Unlock(pMedia);
112  return;
113  }
114  }
115 
116  MythCDROM *cd = MythCDROM::get(nullptr, device, false, false);
117  if (cd)
118  {
119  cd->setDeviceSpeed(device, speed);
120  delete cd;
121  return;
122  }
123 
124  LOG(VB_MEDIA, LOG_INFO,
125  QString("MediaMonitor::setSpeed(%1) - Cannot find/create CDROM?")
126  .arg(device));
127 }
128 
129 // When ejecting one of multiple devices, present a nice name to the user
130 static QString DevName(MythMediaDevice *d)
131 {
132  QString str = d->getVolumeID(); // First choice, the name of the media
133 
134  if (str.isEmpty())
135  {
136  str = d->getDeviceModel(); // otherwise, the drive manufacturer/model
137 
138  if (!str.isEmpty()) // and/or the device node
139  str += " (" + d->getDevicePath() + ')';
140  else
141  str = d->getDevicePath();
142  }
143  // We could add even more information here, but volume names
144  // are usually descriptively unique (i.e. usually good enough)
145  //else
146  // str += " (" + d->getDeviceModel() + ", " + d->getDevicePath() + ')';
147 
148  return str;
149 }
150 
156 QList<MythMediaDevice*> MediaMonitor::GetRemovable(bool showMounted,
157  bool showUsable)
158 {
159  QList <MythMediaDevice *> drives;
160  QList <MythMediaDevice *>::iterator it;
161  QMutexLocker locker(&m_DevicesLock);
162 
163  for (it = m_Devices.begin(); it != m_Devices.end(); ++it)
164  {
165  // By default, we only list CD/DVD devices.
166  // Caller can also request mounted drives to be listed (e.g. USB flash)
167 
168  if (showUsable && !(*it)->isUsable())
169  continue;
170 
171  if (QString(typeid(**it).name()).contains("MythCDROM") ||
172  (showMounted && (*it)->isMounted(false)))
173  drives.append(*it);
174  }
175 
176  return drives;
177 }
178 
185  bool showMounted,
186  bool showUsable)
187 {
188  QList <MythMediaDevice *> drives = GetRemovable(showMounted,
189  showUsable);
190 
191  if (drives.count() == 0)
192  {
193  QString msg = "MediaMonitor::selectDrivePopup() - no removable devices";
194 
195  LOG(VB_MEDIA, LOG_INFO, msg);
196  return nullptr;
197  }
198 
199  if (drives.count() == 1)
200  {
201  LOG(VB_MEDIA, LOG_INFO,
202  "MediaMonitor::selectDrivePopup(" + label +
203  ") - One suitable device");
204  return drives.front();
205  }
206 
208  if (!win)
209  return nullptr;
210 
211  MythScreenStack *stack = win->GetMainStack();
212  if (!stack)
213  return nullptr;
214 
215  // Ignore MENU dialog actions
216  int btnIndex = -2;
217  while (btnIndex < -1)
218  {
219  auto dlg = new MythDialogBox(label, stack, "select drive");
220  if (!dlg->Create())
221  {
222  delete dlg;
223  return nullptr;
224  }
225 
226  // Add button for each drive
227  for (auto drive : drives)
228  dlg->AddButton(DevName(drive));
229 
230  dlg->AddButton(tr("Cancel"));
231 
232  stack->AddScreen(dlg);
233 
234  // Wait in local event loop so events are processed
235  QEventLoop block;
236  connect(dlg, &MythDialogBox::Closed,
237  &block, [&](const QString& /*resultId*/, int result) { block.exit(result); });
238 
239  // Block until dialog closes
240  btnIndex = block.exec();
241  }
242 
243  // If the user cancelled, return a special value
244  return btnIndex < 0 || btnIndex >= drives.size() ? (MythMediaDevice *)-1
245  : drives.at(btnIndex);
246 }
247 
248 
258 {
259  MythMediaDevice *selected;
260 
261 
262  selected = selectDrivePopup(tr("Select removable media"
263  " to eject or insert"), true);
264 
265  // If the user cancelled, no need to display or do anything more
266  if (selected == (MythMediaDevice *) -1)
267  return;
268 
269  if (!selected)
270  {
271  ShowNotification(tr("No devices to eject"), _Location);
272  return;
273  }
274 
275  AttemptEject(selected);
276 }
277 
278 
279 void MediaMonitor::EjectMedia(const QString &path)
280 {
281  MythMediaDevice *device = GetMedia(path);
282  if (device)
283  AttemptEject(device);
284 }
285 
286 
288 {
289  QString dev = DevName(device);
290 
291  if (device->getStatus() == MEDIASTAT_OPEN)
292  {
293  LOG(VB_MEDIA, LOG_INFO,
294  QString("Disk %1's tray is OPEN. Closing tray").arg(dev));
295 
296  if (device->eject(false) != MEDIAERR_OK)
297  {
298  QString msg =
299  QObject::tr("Unable to open or close the empty drive %1");
300  QString extra =
301  QObject::tr("You may have to use the eject button under its tray");
302  ShowNotificationError(msg.arg(dev), _Location, extra);
303  }
304  return;
305  }
306 
307  if (device->isMounted())
308  {
309  LOG(VB_MEDIA, LOG_INFO,
310  QString("Disk %1 is mounted? Unmounting").arg(dev));
311  device->unmount();
312 
313 #if !CONFIG_DARWIN
314  if (device->isMounted())
315  {
316  ShowNotificationError(tr("Failed to unmount %1").arg(dev),
317  _Location);
318  return;
319  }
320 #endif
321  }
322 
323  LOG(VB_MEDIA, LOG_INFO,
324  QString("Unlocking disk %1, then ejecting").arg(dev));
325  device->unlock();
326 
327  MythMediaError err = device->eject();
328 
329  if (err == MEDIAERR_UNSUPPORTED)
330  {
331  // Physical ejection isn't possible (there is no tray or slot),
332  // but logically the device is now ejected (ignored by the OS).
333  ShowNotification(tr("You may safely remove %1").arg(dev), _Location);
334  }
335  else if (err == MEDIAERR_FAILED)
336  {
337  ShowNotificationError(tr("Failed to eject %1").arg(dev), _Location);
338  }
339 }
340 
347 MediaMonitor::MediaMonitor(QObject* par, unsigned long interval,
348  bool allowEject)
349  : QObject(par), m_DevicesLock(QMutex::Recursive),
350  m_MonitorPollingInterval(interval), m_AllowEject(allowEject)
351 {
352  // User can specify that some devices are not monitored
353  QString ignore = gCoreContext->GetSetting("IgnoreDevices", "");
354 
355  if (ignore.length())
356  m_IgnoreList = ignore.split(',', QString::SkipEmptyParts);
357  else
358  m_IgnoreList = QStringList(); // Force empty list
359 
360  LOG(VB_MEDIA, LOG_NOTICE, "Creating MediaMonitor");
361  LOG(VB_MEDIA, LOG_INFO, "IgnoreDevices=" + ignore);
362 
363  // If any of IgnoreDevices are symlinks, also add the real device
364  QStringList::Iterator dev;
365  for (dev = m_IgnoreList.begin(); dev != m_IgnoreList.end(); ++dev)
366  {
367  QFileInfo *fi = new QFileInfo(*dev);
368 
369  if (fi && fi->isSymLink())
370  {
371  QString target = getSymlinkTarget(*dev);
372 
373  if (m_IgnoreList.filter(target).isEmpty())
374  {
375  LOG(VB_MEDIA, LOG_INFO,
376  "Also ignoring " + target + " (symlinked from " +
377  *dev + ").");
378  m_IgnoreList += target;
379  }
380  }
381  delete fi;
382  }
383 }
384 
386 {
387  if (m_Thread)
388  {
389  StopMonitoring();
390  delete m_Thread;
391  m_Thread = nullptr;
392  }
393  QObject::deleteLater();
394 }
395 
404 bool MediaMonitor::RemoveDevice(const QString &dev)
405 {
406  QMutexLocker locker(&m_DevicesLock);
407 
408  QList<MythMediaDevice*>::iterator it;
409  for (it = m_Devices.begin(); it != m_Devices.end(); ++it)
410  {
411  if ((*it)->getDevicePath() == dev)
412  {
413  // Ensure device gets an unmount
414  (*it)->checkMedia();
415 
416  if (m_UseCount[*it] == 0)
417  {
418  m_UseCount.remove(*it);
419  (*it)->deleteLater();
420  m_Devices.erase(it);
421  }
422  else
423  {
424  // Other threads are still using this device
425  // postpone actual delete until they finish.
426  disconnect(*it);
427  m_RemovedDevices.append(*it);
428  m_Devices.erase(it);
429  }
430 
431  return true;
432  }
433  }
434  return false;
435 }
436 
441 {
442  /* check if new devices have been plugged in */
444 
445  QMutexLocker locker(&m_DevicesLock);
446 
447  QList<MythMediaDevice*>::iterator itr = m_Devices.begin();
448  MythMediaDevice* pDev;
449  while (itr != m_Devices.end())
450  {
451  pDev = *itr;
452  if (pDev)
453  pDev->checkMedia();
454  ++itr;
455  }
456 }
457 
462 {
463  // Sanity check
464  if (m_Active)
465  return;
466  if (!gCoreContext->GetBoolSetting("MonitorDrives", false)) {
467  LOG(VB_MEDIA, LOG_NOTICE, "MediaMonitor diasabled by user setting.");
468  return;
469  }
470 
471  if (!m_Thread)
473 
474  qRegisterMetaType<MythMediaStatus>("MythMediaStatus");
475 
476  LOG(VB_MEDIA, LOG_NOTICE, "Starting MediaMonitor");
477  m_Active = true;
478  m_Thread->start();
479 }
480 
485 {
486  // Sanity check
487  if (!m_Active)
488  return;
489 
490  LOG(VB_MEDIA, LOG_NOTICE, "Stopping MediaMonitor");
491  m_Active = false;
492  m_wait.wakeAll();
493  m_Thread->wait();
494  LOG(VB_MEDIA, LOG_NOTICE, "Stopped MediaMonitor");
495 }
496 
508 {
509  QMutexLocker locker(&m_DevicesLock);
510 
511  if (!m_Devices.contains(pMedia))
512  return false;
513 
514  m_UseCount[pMedia]++;
515 
516  return true;
517 }
518 
525 {
526  QMutexLocker locker(&m_DevicesLock);
527 
528  if (!m_UseCount.contains(pMedia))
529  return;
530 
531  m_UseCount[pMedia]--;
532 
533  if (m_UseCount[pMedia] == 0 && m_RemovedDevices.contains(pMedia))
534  {
535  m_RemovedDevices.removeAll(pMedia);
536  m_UseCount.remove(pMedia);
537  pMedia->deleteLater();
538  }
539 }
540 
548 {
549  QMutexLocker locker(&m_DevicesLock);
550 
551  QList<MythMediaDevice*>::iterator it = m_Devices.begin();
552  for (;it != m_Devices.end(); ++it)
553  {
554  if ((*it)->isSameDevice(path) &&
555  (((*it)->getStatus() == MEDIASTAT_USEABLE) ||
556  ((*it)->getStatus() == MEDIASTAT_MOUNTED) ||
557  ((*it)->getStatus() == MEDIASTAT_NOTMOUNTED)))
558  {
559  return(*it);
560  }
561  }
562 
563  return nullptr;
564 }
565 
572 QString MediaMonitor::GetMountPath(const QString& devPath)
573 {
574  QString mountPath;
575 
576  if (s_monitor)
577  {
578  MythMediaDevice *pMedia = s_monitor->GetMedia(devPath);
579  if (pMedia && s_monitor->ValidateAndLock(pMedia))
580  {
581  mountPath = pMedia->getMountPath();
582  s_monitor->Unlock(pMedia);
583  }
584  // The media monitor could be inactive.
585  // Create a fake media device just to lookup mount map:
586  else
587  {
588  pMedia = MythCDROM::get(nullptr, devPath.toLatin1(), true, false);
589  if (pMedia && pMedia->findMountPath())
590  mountPath = pMedia->getMountPath();
591  else
592  LOG(VB_MEDIA, LOG_INFO,
593  "MediaMonitor::GetMountPath() - failed");
594  // need some way to delete the media device.
595  }
596  }
597 
598  return mountPath;
599 }
600 
619 QList<MythMediaDevice*> MediaMonitor::GetMedias(unsigned mediatypes)
620 {
621  QMutexLocker locker(&m_DevicesLock);
622 
623  QList<MythMediaDevice*> medias;
624 
625  QList<MythMediaDevice*>::iterator it = m_Devices.begin();
626  for (;it != m_Devices.end(); ++it)
627  {
628  if (((*it)->getMediaType() & mediatypes) &&
629  (((*it)->getStatus() == MEDIASTAT_USEABLE) ||
630  ((*it)->getStatus() == MEDIASTAT_MOUNTED) ||
631  ((*it)->getStatus() == MEDIASTAT_NOTMOUNTED)))
632  {
633  medias.push_back(*it);
634  }
635  }
636 
637  return medias;
638 }
639 
659 void MediaMonitor::RegisterMediaHandler(const QString &destination,
660  const QString &description,
661  void (*callback)
662  (MythMediaDevice*),
663  int mediaType,
664  const QString &extensions)
665 {
666  if (m_handlerMap.count(destination) == 0)
667  {
668  MHData mhd = { callback, mediaType, destination, description };
669  QString msg = MythMediaDevice::MediaTypeString((MythMediaType)mediaType);
670 
671  if (extensions.length())
672  msg += QString(", ext(%1)").arg(extensions, 0, 16);
673 
674  LOG(VB_MEDIA, LOG_INFO,
675  "Registering '" + destination + "' as a media handler for " +
676  msg);
677 
678  m_handlerMap[destination] = mhd;
679 
680  if (extensions.length())
681  MythMediaDevice::RegisterMediaExtensions(mediaType, extensions);
682  }
683  else
684  {
685  LOG(VB_GENERAL, LOG_INFO,
686  destination + " is already registered as a media handler.");
687  }
688 }
689 
698 {
699  QList<MHData> handlers;
700  QMap<QString, MHData>::Iterator itr = m_handlerMap.begin();
701 
702  while (itr != m_handlerMap.end())
703  {
704  if (((*itr).MythMediaType & (int)pMedia->getMediaType()))
705  {
706  LOG(VB_GENERAL, LOG_NOTICE,
707  QString("Found a handler for %1 - '%2'")
708  .arg(pMedia->MediaTypeString()) .arg(itr.key()));
709  handlers.append(*itr);
710  }
711  itr++;
712  }
713 
714  if (handlers.empty())
715  {
716  LOG(VB_MEDIA, LOG_INFO, "No media handler found for event type");
717  return;
718  }
719 
720 
721  // TODO - Generate a dialog, add buttons for each description,
722  // if user didn't cancel, selected = handlers.at(choice);
723  int selected = 0;
724 
725  handlers.at(selected).callback(pMedia);
726 }
727 
733  MythMediaDevice* pMedia)
734 {
735  // If we're not active then ignore signal.
736  if (!m_Active)
737  return;
738 
739  MythMediaStatus stat = pMedia->getStatus();
740  QString msg = QString(" (%1, %2 -> %3)")
741  .arg(pMedia->MediaTypeString())
742  .arg(MythMediaDevice::MediaStatusStrings[oldStatus])
744 
745  // This gets called from outside the main thread so we need
746  // to post an event back to the main thread.
747  // We now send events for all non-error statuses, so plugins get ejects
748  if (stat != MEDIASTAT_ERROR && stat != MEDIASTAT_UNKNOWN &&
749  // Don't send an event for a new device that's not mounted
750  !(oldStatus == MEDIASTAT_UNPLUGGED && stat == MEDIASTAT_NOTMOUNTED))
751  {
752  // Should we ValidateAndLock() first?
753  QEvent *e = new MythMediaEvent(stat, pMedia);
754 
755  LOG(VB_MEDIA, LOG_INFO, "Posting MediaEvent" + msg);
756 
757  // sendEvent() is needed here - it waits for the event to be used.
758  // postEvent() would result in pDevice's media type changing
759  // ... before the plugin's event chain would process it.
760  // Another way would be to send an exact copy of pDevice instead.
761  QCoreApplication::sendEvent((QObject*)GetMythMainWindow(), e);
762  delete e;
763  }
764  else
765  LOG(VB_MEDIA, LOG_INFO,
766  "Media status changed, but not sending event" + msg);
767 
768 
769  if (stat == MEDIASTAT_OPEN || stat == MEDIASTAT_NODISK
770  || stat == MEDIASTAT_UNPLUGGED)
771  {
772  pMedia->clearData();
773  }
774 }
775 
780 {
781  if (m_IgnoreList.contains(device->getMountPath()) ||
782  m_IgnoreList.contains(device->getRealDevice())||
783  m_IgnoreList.contains(device->getDevicePath()) )
784  {
785  LOG(VB_MEDIA, LOG_INFO,
786  "Ignoring device: " + device->getDevicePath());
787  return true;
788  }
789 #if 0
790  else
791  {
792  LOG(VB_MEDIA, LOG_DEBUG,
793  "Not ignoring: " + device->getDevicePath() + " / " +
794  device->getMountPath());
795  LOG(VB_MEDIA, LOG_DEBUG,
796  "Paths not in: " + m_IgnoreList.join(", "));
797  }
798 #endif
799 
800  return false;
801 }
802 
807 bool MediaMonitor::eventFilter(QObject *obj, QEvent *event)
808 {
809  if (event->type() == MythMediaEvent::kEventType)
810  {
811  MythMediaDevice *pDev = ((MythMediaEvent*)event)->getDevice();
812 
813  if (!pDev)
814  {
815  LOG(VB_GENERAL, LOG_ALERT,
816  "MediaMonitor::eventFilter() got a bad media event?");
817  return true;
818  }
819 
820  if (pDev->isUsable())
821  JumpToMediaHandler(pDev);
822  else
823  {
824  // We don't want to jump around in the menus, but should
825  // call each plugin's callback so it can track this change.
826 
827  QMap<QString, MHData>::Iterator itr = m_handlerMap.begin();
828  while (itr != m_handlerMap.end())
829  {
830  if ((*itr).MythMediaType & (int)pDev->getMediaType() ||
831  pDev->getStatus() == MEDIASTAT_OPEN)
832  (*itr).callback(pDev);
833  itr++;
834  }
835  }
836 
837  return false; // Don't eat the event
838  }
839 
840  // standard event processing
841  return QObject::eventFilter(obj, event);
842 }
843 
844 /*
845  * These methods return the user's preferred devices for playing and burning
846  * CDs and DVDs. Traditionally we had a database setting to remember this,
847  * but that is a bit wasteful when most users only have one drive.
848  *
849  * To make it a bit more beginner friendly, if no database default exists,
850  * or if it contains "default", the code tries to find a monitored drive.
851  * If, still, nothing is suitable, a caller hard-coded default is used.
852  *
853  * Ideally, these would return a MythMediaDevice * instead of a QString
854  */
855 
856 QString MediaMonitor::defaultDevice(const QString &dbSetting,
857  const QString &label,
858  const char *hardCodedDefault)
859 {
860  QString device = gCoreContext->GetSetting(dbSetting);
861 
862  LOG(VB_MEDIA, LOG_DEBUG,
863  QString("MediaMonitor::defaultDevice(%1,..,%2) dbSetting='%3'")
864  .arg(dbSetting).arg(hardCodedDefault).arg(device));
865 
866  // No settings database defaults? Try to choose one:
867  if (device.isEmpty() || device == "default")
868  {
869  device = hardCodedDefault;
870 
871  if (!s_monitor)
873 
874  if (s_monitor)
875  {
876  MythMediaDevice *d = s_monitor->selectDrivePopup(label, false, true);
877 
878  if (d == (MythMediaDevice *) -1) // User cancelled
879  {
880  device.clear(); // If user has explicitly cancelled return empty string
881  d = nullptr;
882  }
883 
884  if (d && s_monitor->ValidateAndLock(d))
885  {
886  device = d->getDevicePath();
887  s_monitor->Unlock(d);
888  }
889  }
890  }
891 
892  LOG(VB_MEDIA, LOG_DEBUG,
893  "MediaMonitor::defaultDevice() returning " + device);
894  return device;
895 }
896 
901 {
902  return defaultDevice("CDDevice", tr("Select a CD drive"), DEFAULT_CD);
903 }
904 
909 {
910  return defaultDevice("VCDDeviceLocation",
911  tr("Select a VCD drive"), DEFAULT_CD);
912 }
913 
918 {
919  return defaultDevice("DVDDeviceLocation",
920  tr("Select a DVD drive"), DEFAULT_DVD);
921 }
922 
927 {
928  return defaultDevice("CDWriterDeviceLocation",
929  tr("Select a CD writer"), DEFAULT_CD);
930 }
931 
939 {
940  QString device = defaultDevice("MythArchiveDVDLocation",
941  tr("Select a DVD writer"), DEFAULT_DVD);
942 
943  return device;
944 }
945 
946 
951 {
952  QList<MythMediaDevice*>::const_iterator dev;
953  QStringList list;
954 
955  for (dev = m_Devices.begin(); dev != m_Devices.end(); ++dev)
956  {
957  QString devStr;
958  QString model = (*dev)->getDeviceModel();
959  QString path = (*dev)->getDevicePath();
960  QString real = (*dev)->getRealDevice();
961 
962  if (path != real)
963  devStr += path + "->";
964  devStr += real;
965 
966  if (!model.length())
967  model = "unknown";
968  devStr += " (" + model + ")";
969 
970  list += devStr;
971  }
972 
973  return list.join(", ");
974 }
975 
983 {
985  if (mon)
986  mon->ChooseAndEjectMedia();
987  else
988  {
989  LOG(VB_MEDIA, LOG_INFO, "CD/DVD Monitor isn't enabled.");
990 #ifdef __linux__
991  LOG(VB_MEDIA, LOG_INFO, "Trying Linux 'eject -T' command");
992  myth_system("eject -T");
993 #elif CONFIG_DARWIN
994  QString def = DEFAULT_CD;
995  LOG(VB_MEDIA, LOG_INFO, "Trying 'diskutil eject " + def);
996  myth_system("diskutil eject " + def);
997 #endif
998  }
999 }
1000 
1001 /*
1002  * vim:ts=4:sw=4:ai:et:si:sts=4
1003  */
MythMediaType getMediaType() const
Definition: mythmedia.h:91
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
const QString & getDevicePath() const
Definition: mythmedia.h:61
CD/DVD tray open (meaningless for non-CDs?)
Definition: mythmedia.h:16
QMap< MythMediaDevice *, int > m_UseCount
QMap< QString, MHData > m_handlerMap
Registered Media Handlers.
MythMediaType
Definition: mythmedia.h:24
QPointer< MediaMonitor > m_Monitor
MonitorThread * m_Thread
static QString GetMountPath(const QString &devPath)
If the device is being monitored, return its mountpoint.
unsigned long m_MonitorPollingInterval
void EjectMedia(const QString &path)
bool shouldIgnore(const MythMediaDevice *device)
Check user preferences to see if this device should be monitored.
I am assuming, for now, that everything on Windows uses drive letters (e.g.
Unable to mount, but could be usable.
Definition: mythmedia.h:13
QList< MythMediaDevice * > m_RemovedDevices
static void ejectOpticalDisc(void)
Eject a disk, unmount a drive, open a tray.
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
Basic menu dialog, message and a list of options.
static QString defaultCDWriter()
CDWriterDeviceLocation, user-selected drive, or /dev/cdrom.
MythMediaDevice * GetMedia(const QString &path)
Get media device by pathname.
virtual void StartMonitoring(void)
Start the monitoring thread if needed.
static void RegisterMediaExtensions(uint mediatype, const QString &extensions)
Used to register media types with extensions.
Definition: mythmedia.cpp:297
QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries, unsigned maxLinks)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
static QString defaultDevice(const QString &setting, const QString &label, const char *hardCodedDefault)
Stores details of media handlers.
MythScreenStack * GetMainStack()
static QString defaultDVDdevice()
DVDDeviceLocation, user-selected drive, or /dev/dvd.
unsigned long m_Interval
friend class MonitorThread
virtual void deleteLater(void)
static const char * MediaStatusStrings[]
Definition: mythmedia.h:117
void StopMonitoring(void)
Stop the monitoring thread if needed.
#define MONITOR_INTERVAL
static void SetCDSpeed(const char *device, int speed)
QList< MythMediaDevice * > m_Devices
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
QWaitCondition m_wait
virtual MythMediaError unlock()
Definition: mythmedia.cpp:348
void ShowNotification(const QString &msg, const QString &from, const QString &detail, const VNMask visibility, const MythNotification::Priority priority)
void mediaStatusChanged(MythMediaStatus oldStatus, MythMediaDevice *pMedia)
Slot which is called when the device status changes and posts a media event to the mainwindow.
static const uint16_t * d
QString GetSetting(const QString &key, const QString &defaultval="")
void CheckDevices(void)
Poll the devices in our list.
MythMediaError
Definition: mythmedia.h:39
static MediaMonitor * GetMediaMonitor(void)
static QString defaultVCDdevice()
VCDDeviceLocation, user-selected drive, or /dev/cdrom.
void ShowNotificationError(const QString &msg, const QString &from, const QString &detail, const VNMask visibility, const MythNotification::Priority priority)
convenience utility to display error message as notification
MonitorThread(MediaMonitor *pMon, unsigned long interval)
static QString defaultCDdevice()
CDDevice, user-selected drive, or /dev/cdrom.
QList< MythMediaDevice * > GetMedias(unsigned mediatypes)
Ask for available media.
void AttemptEject(MythMediaDevice *device)
static QList< GameHandler * > * handlers
Definition: gamehandler.cpp:23
void Unlock(MythMediaDevice *pMedia)
decrements the MythMediaDevices reference count
uint myth_system(const QString &command, uint flags, uint timeout)
const QString & getRealDevice() const
Definition: mythmedia.h:63
bool eventFilter(QObject *obj, QEvent *event) override
Installed into the main window's event chain, so that the main thread can safely jump to plugin code.
void setDeviceSpeed(const char *devicePath, int speed) override
Definition: mythcdrom.cpp:132
virtual void setSpeed(int speed)
Definition: mythmedia.cpp:330
bool HasMythMainWindow(void)
QList< MythMediaDevice * > GetRemovable(bool showMounted=false, bool showUsable=false)
Generate a list of removable drives.
virtual MythMediaError eject(bool open_close=true)
Definition: mythmedia.cpp:305
MythMainWindow * GetMythMainWindow(void)
static MythCDROM * get(QObject *par, const char *devicePath, bool SuperMount, bool AllowEject)
Definition: mythcdrom.cpp:35
static Type kEventType
Definition: mythmedia.h:192
bool RemoveDevice(const QString &dev)
Remove a device from the media monitor.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
bool ValidateAndLock(MythMediaDevice *pMedia)
Validates the MythMediaDevice and increments its reference count.
bool findMountPath()
Try to find a mount of m_DevicePath in the mounts file.
Definition: mythmedia.cpp:364
bool GetBoolSetting(const QString &key, bool defaultval=false)
MediaMonitor(QObject *par, unsigned long interval, bool allowEject)
Lookup some settings, and do OS-specific stuff in sub-classes.
This currently depends on Apple's DiskArbitration framework.
const QString & getMountPath() const
Definition: mythmedia.h:58
void RegisterMediaHandler(const QString &destination, const QString &description, void(*callback)(MythMediaDevice *), int mediaType, const QString &extensions)
Register a handler for media related events.
static QString defaultDVDWriter()
MythArchiveDVDLocation, user-selected drive, or /dev/dvd.
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
static QString DevName(MythMediaDevice *d)
QDateTime m_lastCheckTime
QString listDevices(void)
A string summarising the current devices, for debugging.
#define DEFAULT_CD
bool isUsable() const
Is this device "ready", for a plugin to access?
Definition: mythmedia.h:84
virtual MythMediaStatus checkMedia()=0
QString MediaTypeString()
Definition: mythmedia.cpp:514
void JumpToMediaHandler(MythMediaDevice *pMedia)
Find a relevant jump point for this type of media.
bool isMounted(bool bVerify=true)
Tells us if m_DevicePath is a mounted device.
Definition: mythmedia.cpp:356
CD/DVD tray closed but empty, device unusable.
Definition: mythmedia.h:17
MythMediaStatus
Definition: mythmedia.h:12
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
bool volatile m_Active
Was MonitorThread started?
static MediaMonitor * s_monitor
static const QString _Location
void StartLIRC(void)
virtual void CheckDeviceNotifications(void)
void Closed(QString, int)
MythMediaDevice * selectDrivePopup(const QString &label, bool showMounted=false, bool showUsable=false)
List removable drives, let the user select one.
bool unmount()
Definition: mythmedia.h:108
MythMediaStatus getStatus() const
Definition: mythmedia.h:70
QStringList m_IgnoreList
void ChooseAndEjectMedia(void)
Unmounts and ejects removable media devices.
#define DEFAULT_DVD