]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
Fix problem when changing capture setting while connected to camcorder:
[kdenlive] / src / recmonitor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "recmonitor.h"
22 #include "gentime.h"
23 #include "kdenlivesettings.h"
24 #include "managecapturesdialog.h"
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <QPainter>
29 #include <KStandardDirs>
30 #include <KComboBox>
31 #include <KIO/NetAccess>
32 #include <KFileItem>
33 #include <KMessageBox>
34
35 #if KDE_IS_VERSION(4,2,0)
36 #include <KDiskFreeSpaceInfo>
37 #endif
38
39 #include <QMouseEvent>
40 #include <QMenu>
41 #include <QToolButton>
42 #include <QFile>
43 #include <QDir>
44
45
46 RecMonitor::RecMonitor(QString name, QWidget *parent) :
47         QWidget(parent),
48         m_name(name),
49         m_isActive(false),
50         m_isCapturing(false),
51         m_didCapture(false),
52         m_isPlaying(false)
53 {
54     setupUi(this);
55
56     video_frame->setAttribute(Qt::WA_PaintOnScreen);
57     device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
58     connect(device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
59
60
61
62     QToolBar *toolbar = new QToolBar(name, this);
63     QHBoxLayout *layout = new QHBoxLayout;
64     layout->setContentsMargins(0, 0, 0, 0);
65     m_playIcon = KIcon("media-playback-start");
66     m_pauseIcon = KIcon("media-playback-pause");
67
68     m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
69     connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
70
71     m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
72     connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
73
74     m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
75     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartCapture()));
76
77     m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
78     connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
79     m_stopAction->setEnabled(false);
80     m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
81     connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
82
83     m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
84     connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
85     m_recAction->setCheckable(true);
86
87     toolbar->addSeparator();
88
89     QAction *configAction = toolbar->addAction(KIcon("configure"), i18n("Configure"));
90     connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigure()));
91     configAction->setCheckable(false);
92
93     layout->addWidget(toolbar);
94
95     layout->addWidget(&m_dvinfo);
96
97 #if KDE_IS_VERSION(4,2,0)
98     m_freeSpace = new KCapacityBar(KCapacityBar::DrawTextInline, this);
99     m_freeSpace->setMaximumWidth(150);
100     QFontMetricsF fontMetrics(font());
101     m_freeSpace->setMaximumHeight(fontMetrics.height() * 1.2);
102     slotUpdateFreeSpace();
103     layout->addWidget(m_freeSpace);
104     connect(&m_spaceTimer, SIGNAL(timeout()), this, SLOT(slotUpdateFreeSpace()));
105     m_spaceTimer.setInterval(30000);
106     m_spaceTimer.setSingleShot(false);
107 #endif
108
109     control_frame_firewire->setLayout(layout);
110
111     slotVideoDeviceChanged(device_selector->currentIndex());
112     m_displayProcess = new QProcess;
113     m_captureProcess = new QProcess;
114
115     connect(m_captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
116     connect(m_captureProcess, SIGNAL(readyReadStandardError()), this, SLOT(slotReadDvgrabInfo()));
117
118     QStringList env = QProcess::systemEnvironment();
119     env << "SDL_WINDOWID=" + QString::number(video_frame->winId());
120
121     QString videoDriver = KdenliveSettings::videodrivername();
122     if (!videoDriver.isEmpty()) {
123         if (videoDriver == "x11_noaccel") {
124             env << "SDL_VIDEO_YUV_HWACCEL=0";
125             env << "SDL_VIDEODRIVER=x11";
126         } else env << "SDL_VIDEODRIVER=" + videoDriver;
127     }
128     setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
129
130     m_displayProcess->setEnvironment(env);
131
132     if (KdenliveSettings::video4capture().isEmpty()) {
133         QString captureCommand;
134         if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
135
136         captureCommand +=  " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice();
137         KdenliveSettings::setVideo4capture(captureCommand);
138     }
139
140     kDebug() << "/////// BUILDING MONITOR, ID: " << video_frame->winId();
141 }
142
143 RecMonitor::~RecMonitor()
144 {
145     m_spaceTimer.stop();
146     delete m_captureProcess;
147     delete m_displayProcess;
148 }
149
150 QString RecMonitor::name() const
151 {
152     return m_name;
153 }
154
155 void RecMonitor::slotConfigure()
156 {
157     emit showConfigDialog(4, device_selector->currentIndex());
158 }
159
160 void RecMonitor::slotUpdateCaptureFolder()
161 {
162     if (m_captureProcess) m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
163     if (m_captureProcess->state() != QProcess::NotRunning) {
164         if (device_selector->currentIndex() == FIREWIRE)
165             KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
166         else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
167     }
168     else slotVideoDeviceChanged(device_selector->currentIndex());
169     kDebug() << "// UPDATE CAPT FOLD: " << KdenliveSettings::capturefolder();
170
171 #if KDE_IS_VERSION(4,2,0)
172     // update free space info
173     slotUpdateFreeSpace();
174 #endif
175 }
176
177 void RecMonitor::slotVideoDeviceChanged(int ix)
178 {
179     switch (ix) {
180     case SCREENGRAB:
181         m_discAction->setEnabled(false);
182         m_rewAction->setEnabled(false);
183         m_fwdAction->setEnabled(false);
184         m_recAction->setEnabled(true);
185         m_stopAction->setEnabled(false);
186         m_playAction->setEnabled(false);
187         if (KdenliveSettings::rmd_path().isEmpty()) {
188             QString rmdpath = KStandardDirs::findExe("recordmydesktop");
189             if (rmdpath.isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("dialog-warning").pixmap(QSize(50, 50)), i18n("Recordmydesktop utility not found,\n please install it for screen grabs")));
190             else KdenliveSettings::setRmd_path(rmdpath);
191         }
192         if (!KdenliveSettings::rmd_path().isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("video-display").pixmap(QSize(50, 50)), i18n("Press record button\nto start screen capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
193         //video_frame->setText(i18n("Press record button\nto start screen capture"));
194         break;
195     case VIDEO4LINUX:
196         m_discAction->setEnabled(false);
197         m_rewAction->setEnabled(false);
198         m_fwdAction->setEnabled(false);
199         m_recAction->setEnabled(true);
200         m_stopAction->setEnabled(false);
201         m_playAction->setEnabled(true);
202         checkDeviceAvailability();
203         break;
204     default: // FIREWIRE
205         m_discAction->setEnabled(true);
206         m_recAction->setEnabled(false);
207         m_stopAction->setEnabled(false);
208         m_playAction->setEnabled(false);
209         m_rewAction->setEnabled(false);
210         m_fwdAction->setEnabled(false);
211
212         // Check that dvgab is available
213         if (KdenliveSettings::dvgrab_path().isEmpty()) {
214             QString dvgrabpath = KStandardDirs::findExe("dvgrab");
215             if (dvgrabpath.isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("dialog-warning").pixmap(QSize(50, 50)), i18n("dvgrab utility not found,\n please install it for firewire capture")));
216             else KdenliveSettings::setDvgrab_path(dvgrabpath);
217         } else {
218             // Show capture info
219             QString capturefile = KdenliveSettings::capturefolder();
220             if (!capturefile.endsWith("/")) capturefile.append("/");
221             QString capturename = KdenliveSettings::dvgrabfilename();
222             if (capturename.isEmpty()) capturename = "capture";
223             QString extension;
224             switch (KdenliveSettings::firewireformat()) {
225             case 0:
226                 extension = ".dv";
227                 break;
228             case 1:
229             case 2:
230                 extension = ".avi";
231                 break;
232             case 3:
233                 extension = ".m2t";
234                 break;
235             }
236             capturename.append("xxx" + extension);
237             capturefile.append(capturename);
238             video_frame->setPixmap(mergeSideBySide(KIcon("network-connect").pixmap(QSize(50, 50)), i18n("Plug your camcorder and\npress connect button\nto initialize connection\nFiles will be saved in:\n%1", capturefile)));
239         }
240         break;
241     }
242 }
243
244 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt)
245 {
246     QPainter p;
247     QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
248     int strWidth = r.width();
249     int strHeight = r.height();
250     int pixWidth = pix.width();
251     int pixHeight = pix.height();
252     QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
253     res.fill(Qt::transparent);
254     p.begin(&res);
255     p.drawPixmap(0, 0, pix);
256     p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
257     p.end();
258     return res;
259 }
260
261
262 void RecMonitor::checkDeviceAvailability()
263 {
264     if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
265         m_playAction->setEnabled(false);
266         m_recAction->setEnabled(false);
267         video_frame->setPixmap(mergeSideBySide(KIcon("camera-web").pixmap(QSize(50, 50)), i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice())));
268         //video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
269     } else //video_frame->setText(i18n("Press play or record button\nto start video capture"));
270         video_frame->setPixmap(mergeSideBySide(KIcon("camera-web").pixmap(QSize(50, 50)), i18n("Press play or record button\nto start video capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
271 }
272
273 void RecMonitor::slotDisconnect()
274 {
275     if (m_captureProcess->state() == QProcess::NotRunning) {
276         m_captureTime = KDateTime::currentLocalDateTime();
277         kDebug() << "CURRENT TIME: " << m_captureTime.toString();
278         m_didCapture = false;
279         slotStartCapture(false);
280         m_discAction->setIcon(KIcon("network-disconnect"));
281         m_discAction->setText(i18n("Disonnect"));
282         m_recAction->setEnabled(true);
283         m_stopAction->setEnabled(true);
284         m_playAction->setEnabled(true);
285         m_rewAction->setEnabled(true);
286         m_fwdAction->setEnabled(true);
287     } else {
288         m_captureProcess->write("q", 1);
289         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
290         if (m_didCapture) manageCapturedFiles();
291         m_didCapture = false;
292     }
293 }
294
295 void RecMonitor::slotRewind()
296 {
297     m_captureProcess->write("a", 1);
298 }
299
300 void RecMonitor::slotForward()
301 {
302     m_captureProcess->write("z", 1);
303 }
304
305 void RecMonitor::slotStopCapture()
306 {
307     // stop capture
308     switch (device_selector->currentIndex()) {
309     case FIREWIRE:
310         m_captureProcess->write("\e", 2);
311         m_playAction->setIcon(m_playIcon);
312         m_isPlaying = false;
313         break;
314     case VIDEO4LINUX:
315         m_captureProcess->write("q\n", 3);
316         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
317
318         break;
319     case SCREENGRAB:
320         m_captureProcess->write("q\n", 3);
321         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
322         break;
323     default:
324         break;
325     }
326 }
327
328 void RecMonitor::slotStartCapture(bool play)
329 {
330     if (m_captureProcess->state() != QProcess::NotRunning) {
331         if (device_selector->currentIndex() == FIREWIRE) {
332             if (m_isPlaying) {
333                 m_captureProcess->write("k", 1);
334                 //captureProcess->write("\e", 2);
335                 m_playAction->setIcon(m_playIcon);
336                 m_isPlaying = false;
337             } else {
338                 m_captureProcess->write("p", 1);
339                 m_playAction->setIcon(m_pauseIcon);
340                 m_isPlaying = true;
341             }
342         }
343         return;
344     }
345     m_captureArgs.clear();
346     m_displayArgs.clear();
347     m_isPlaying = false;
348     QString capturename = KdenliveSettings::dvgrabfilename();
349     QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(" ", QString::SkipEmptyParts);
350
351     switch (device_selector->currentIndex()) {
352     case FIREWIRE:
353         switch (KdenliveSettings::firewireformat()) {
354         case 0:
355             // RAW DV CAPTURE
356             m_captureArgs << "--format" << "raw";
357             m_displayArgs << "-f" << "dv";
358             break;
359         case 1:
360             // DV type 1
361             m_captureArgs << "--format" << "dv1";
362             m_displayArgs << "-f" << "dv";
363             break;
364         case 2:
365             // DV type 2
366             m_captureArgs << "--format" << "dv2";
367             m_displayArgs << "-f" << "dv";
368             break;
369         case 3:
370             // HDV CAPTURE
371             m_captureArgs << "--format" << "hdv";
372             m_displayArgs << "-f" << "mpegts";
373             break;
374         }
375         if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
376         if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
377         if (!dvargs.isEmpty()) {
378             m_captureArgs << dvargs;
379         }
380         m_captureArgs << "-i";
381         if (capturename.isEmpty()) capturename = "capture";
382         m_captureArgs << capturename << "-";
383
384         m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
385
386         m_captureProcess->setStandardOutputProcess(m_displayProcess);
387         m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
388         kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
389
390         m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
391         if (play) m_captureProcess->write(" ", 1);
392         m_discAction->setEnabled(true);
393         break;
394     case VIDEO4LINUX:
395         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
396         m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
397         m_captureProcess->setStandardOutputProcess(m_displayProcess);
398         kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
399         m_captureProcess->start("ffmpeg", m_captureArgs);
400         break;
401     default:
402         break;
403     }
404
405     if (device_selector->currentIndex() != SCREENGRAB) {
406         kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
407         m_displayProcess->start("ffplay", m_displayArgs);
408         video_frame->setText(i18n("Initialising..."));
409     } else {
410         // do something when starting screen grab
411     }
412 }
413
414 void RecMonitor::slotRecord()
415 {
416     if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
417         slotStartCapture();
418     }
419     if (m_isCapturing) {
420         switch (device_selector->currentIndex()) {
421         case FIREWIRE:
422             m_captureProcess->write("\e", 2);
423             m_playAction->setIcon(m_playIcon);
424             m_isCapturing = false;
425             m_isPlaying = false;
426             m_recAction->setChecked(false);
427             break;
428         case VIDEO4LINUX:
429             m_captureProcess->terminate();
430             slotStopCapture();
431             //m_isCapturing = false;
432             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
433             break;
434         case SCREENGRAB:
435             //captureProcess->write("q\n", 3);
436             m_captureProcess->terminate();
437             video_frame->setText(i18n("Encoding captured video..."));
438             // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
439             //QTimer::singleShot(1000, captureProcess, SLOT(kill()));
440             break;
441         }
442         return;
443     } else if (device_selector->currentIndex() == FIREWIRE) {
444         m_isCapturing = true;
445         m_didCapture = true;
446         m_captureProcess->write("c\n", 3);
447         m_spaceTimer.start();
448         return;
449     }
450     if (m_captureProcess->state() == QProcess::NotRunning) {
451         m_recAction->setChecked(true);
452         QString extension = "mp4";
453         if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
454         QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
455         int i = 1;
456         while (QFile::exists(path)) {
457             QString num = QString::number(i).rightJustified(4, '0', false);
458             path = KdenliveSettings::capturefolder() + "/capture" + num + '.' + extension;
459             i++;
460         }
461         m_captureFile = KUrl(path);
462
463         m_captureArgs.clear();
464         m_displayArgs.clear();
465         QString args;
466         QString capturename = KdenliveSettings::dvgrabfilename();
467         if (capturename.isEmpty()) capturename = "capture";
468
469         switch (device_selector->currentIndex()) {
470         case VIDEO4LINUX:
471             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-y" << m_captureFile.path() << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
472             m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
473             m_captureProcess->setStandardOutputProcess(m_displayProcess);
474             kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
475             m_captureProcess->start("ffmpeg", m_captureArgs);
476             break;
477         case SCREENGRAB:
478             switch (KdenliveSettings::rmd_capture_type()) {
479             case 0:
480                 // Full screen capture, nothing special to do
481                 break;
482             default:
483                 // Region capture
484                 m_captureArgs << "-width" << QString::number(KdenliveSettings::rmd_width()) << "-height" << QString::number(KdenliveSettings::rmd_height());
485                 if (!KdenliveSettings::rmd_follow_mouse()) {
486                     m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
487                 } else {
488                     m_captureArgs << "--follow-mouse";
489                     if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
490                 }
491                 break;
492             }
493             m_isCapturing = true;
494             if (KdenliveSettings::rmd_capture_audio()) {
495                 m_captureArgs << "-freq" << KdenliveSettings::rmd_freq();
496                 m_captureArgs << "-channels" << QString::number(KdenliveSettings::rmd_audio_channels());
497                 if (KdenliveSettings::rmd_use_jack()) {
498                     m_captureArgs << "-use-jack" << KdenliveSettings::rmd_jackports();
499                     if (KdenliveSettings::rmd_jack_buffer() > 0.0)
500                         m_captureArgs << "-ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
501                 } else {
502                     if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
503                         m_captureArgs << "-device" << KdenliveSettings::rmd_alsadevicename();
504                     if (KdenliveSettings::rmd_alsa_buffer() > 0)
505                         m_captureArgs << "-buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
506                 }
507             } else m_captureArgs << "--no-sound";
508
509             if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
510             m_captureArgs << "-workdir" << KdenliveSettings::currenttmpfolder();
511             m_captureArgs << "-fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
512             m_captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
513             kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
514             break;
515         default:
516             break;
517         }
518
519
520         if (device_selector->currentIndex() != SCREENGRAB) {
521             m_isCapturing = true;
522             kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
523             m_displayProcess->start("ffplay", m_displayArgs);
524             video_frame->setText(i18n("Initialising..."));
525         }
526     } else {
527         // stop capture
528         m_displayProcess->kill();
529         //captureProcess->kill();
530         QTimer::singleShot(1000, this, SLOT(slotRecord()));
531     }
532 }
533
534 /*
535 void RecMonitor::slotStartGrab(const QRect &rect) {
536     rgnGrab->deleteLater();
537     QApplication::restoreOverrideCursor();
538     show();
539     if (rect.isNull()) return;
540     int width = rect.width();
541     int height = rect.height();
542     if (width % 2 != 0) width--;
543     if (height % 2 != 0) height--;
544     QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
545     if (KdenliveSettings::screengrabenableaudio()) {
546         // also capture audio
547         if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
548         else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
549     }
550     m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
551     m_isCapturing = true;
552     video_frame->setText(i18n("Capturing..."));
553     if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
554         QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
555         alsaProcess->setStandardOutputProcess(captureProcess);
556         kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
557         alsaProcess->start("arecord", alsaArgs);
558     }
559     kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
560     captureProcess->start("ffmpeg", m_captureArgs);
561 }*/
562
563 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
564 {
565     if (status == QProcess::NotRunning) {
566         m_displayProcess->kill();
567         if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
568             if (autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
569         if (device_selector->currentIndex() == FIREWIRE) {
570             m_discAction->setIcon(KIcon("network-connect"));
571             m_discAction->setText(i18n("Connect"));
572             m_playAction->setEnabled(false);
573             m_rewAction->setEnabled(false);
574             m_fwdAction->setEnabled(false);
575             m_recAction->setEnabled(false);
576         }
577         m_isPlaying = false;
578         m_playAction->setIcon(m_playIcon);
579         m_recAction->setChecked(false);
580         m_stopAction->setEnabled(false);
581         device_selector->setEnabled(true);
582         if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
583             video_frame->setText(i18n("Capture crashed, please check your parameters"));
584         } else {
585             if (device_selector->currentIndex() != SCREENGRAB) video_frame->setText(i18n("Not connected"));
586             else video_frame->setPixmap(mergeSideBySide(KIcon("video-display").pixmap(QSize(50, 50)), i18n("Press record button\nto start screen capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
587         }
588         m_isCapturing = false;
589         m_spaceTimer.stop();
590
591 #if KDE_IS_VERSION(4,2,0)
592         // update free space info
593         slotUpdateFreeSpace();
594 #endif
595
596     } else {
597         if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
598         device_selector->setEnabled(false);
599     }
600 }
601
602 void RecMonitor::manageCapturedFiles()
603 {
604     QString extension;
605     switch (KdenliveSettings::firewireformat()) {
606     case 0:
607         extension = ".dv";
608         break;
609     case 1:
610     case 2:
611         extension = ".avi";
612         break;
613     case 3:
614         extension = ".m2t";
615         break;
616     }
617     QDir dir(KdenliveSettings::capturefolder());
618     QStringList filters;
619     QString capturename = KdenliveSettings::dvgrabfilename();
620     if (capturename.isEmpty()) capturename = "capture";
621     filters << capturename + "*" + extension;
622     const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
623     KUrl::List capturedFiles;
624     foreach(const QString &name, result) {
625         KUrl url = KUrl(dir.filePath(name));
626         if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
627             KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
628             if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
629         }
630     }
631     kDebug() << "Found : " << capturedFiles.count() << " new capture files";
632     kDebug() << capturedFiles;
633
634     if (capturedFiles.count() > 0) {
635         ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
636         if (d->exec() == QDialog::Accepted) {
637             capturedFiles = d->importFiles();
638             foreach(const KUrl &url, capturedFiles) {
639                 emit addProjectClip(url);
640             }
641         }
642         delete d;
643     }
644 }
645
646 // virtual
647 void RecMonitor::mousePressEvent(QMouseEvent * /*event*/)
648 {
649 #if KDE_IS_VERSION(4,2,0)
650     if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
651 #endif
652 }
653
654 void RecMonitor::slotUpdateFreeSpace()
655 {
656 #if KDE_IS_VERSION(4,2,0)
657     KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(KdenliveSettings::capturefolder());
658     if (info.isValid()) {
659         m_freeSpace->setValue(100 * info.used() / info.size());
660         m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
661         m_freeSpace->update();
662     }
663 #endif
664 }
665
666 void RecMonitor::activateRecMonitor()
667 {
668     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
669 }
670
671 void RecMonitor::stop()
672 {
673     m_isActive = false;
674
675 }
676
677 void RecMonitor::start()
678 {
679     m_isActive = true;
680
681 }
682
683 void RecMonitor::refreshRecMonitor(bool visible)
684 {
685     if (visible) {
686         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
687
688     }
689 }
690
691 void RecMonitor::slotPlay()
692 {
693
694     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
695
696 }
697
698 void RecMonitor::slotReadDvgrabInfo()
699 {
700     QString data = m_captureProcess->readAllStandardError().simplified();
701     data = data.section('"', 2, 2).simplified();
702     m_dvinfo.setText(data.left(11));
703     m_dvinfo.updateGeometry();
704 }
705
706 #include "recmonitor.moc"