kwin/effects/_test/videorecord.cpp

176 lines
5.6 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
/*
This effect allows recording a video from the session.
Requires libcaptury:
- git clone git://gitorious.org/capseo/mainline.git capseo
- you may want to remove 1.10 from AUTOMAKE_OPTIONS in Makefile.am
- ./autogen.sh
- the usual configure && make && make install procedure
(you may want to pass --enable-theora --with-accel=x86 [or amd64])
- git clone git://gitorious.org/libcaptury/mainline.git libcaptury
- you may want to remove 1.10 from AUTOMAKE_OPTIONS in Makefile.am
- ./autogen.sh
- the usual configure && make && make install procedure
Video is saved to $HOME/kwin_video.cps, use
"cpsrecode -i kwin_video.cps -o - | mplayer -" to play,
to create a video use "cpsrecode -c theora -i kwin_video.cps -o screen.ogg"
or use mencoder in a similar way as mplayer.
*/
#include "videorecord.h"
#include <kaction.h>
#include <kactioncollection.h>
#include <kdebug.h>
#include <klocale.h>
#include <qdir.h>
#include <qfile.h>
#include <kio/netaccess.h>
#include <KConfigGroup>
#include <KGlobalSettings>
#include <strings.h>
namespace KWin
{
2011-01-30 14:34:42 +00:00
KWIN_EFFECT(videorecord, VideoRecordEffect)
VideoRecordEffect::VideoRecordEffect()
2011-01-30 14:34:42 +00:00
: client(NULL)
{
KActionCollection* actionCollection = new KActionCollection(this);
KAction* a = static_cast< KAction* >(actionCollection->addAction("VideoRecord"));
a->setText(i18n("Toggle Video Recording"));
a->setGlobalShortcut(KShortcut(Qt::CTRL + Qt::META + Qt::Key_V));
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleRecording()));
area = QRect(0, 0, displayWidth(), displayHeight());
}
VideoRecordEffect::~VideoRecordEffect()
2011-01-30 14:34:42 +00:00
{
stopRecording();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void VideoRecordEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{
effects->paintScreen(mask, region, data);
if (client != NULL)
capture_region = (mask & (PAINT_WINDOW_TRANSFORMED | PAINT_SCREEN_TRANSFORMED))
? QRect(0, 0, displayWidth(), displayHeight()) : region;
}
void VideoRecordEffect::postPaintScreen()
2011-01-30 14:34:42 +00:00
{
effects->postPaintScreen();
2011-01-30 14:34:42 +00:00
if (client != NULL) {
#if 1
2011-01-30 14:34:42 +00:00
if (CapturyProcessRegionStart(client) == CAPTURY_SUCCESS) {
capture_region &= QRect(0, 0, displayWidth(), displayHeight()); // limit to screen
foreach (const QRect & r, capture_region.rects()) {
int gly = displayHeight() - r.y() - r.height(); // opengl coords
2011-01-30 14:34:42 +00:00
CapturyProcessRegion(client, r.x(), gly, r.width(), r.height());
}
2011-01-30 14:34:42 +00:00
CapturyProcessRegionCommit(client);
}
#else
2011-01-30 14:34:42 +00:00
CapturyProcessFrame(client);
#endif
}
2011-01-30 14:34:42 +00:00
}
void VideoRecordEffect::startRecording()
2011-01-30 14:34:42 +00:00
{
if (client != NULL)
stopRecording();
2011-01-30 14:34:42 +00:00
bzero(&config, sizeof(config));
config.x = area.x();
config.y = area.y();
config.width = area.width();
config.height = area.height();
config.scale = 0;
config.fps = 30; // TODO
config.deviceType = CAPTURY_DEVICE_GLX; // TODO
config.deviceHandle = display();
config.windowHandle = rootWindow(); // TODO
config.cursor = true;
2011-01-30 14:34:42 +00:00
client = CapturyOpen(&config);
if (client == NULL) {
kDebug(1212) << "Video recording init failed";
return;
2011-01-30 14:34:42 +00:00
}
KConfigGroup conf = EffectsHandler::effectConfig("VideoRecord");
2011-01-30 14:34:42 +00:00
QString videoPath = conf.readEntry("videopath", KGlobalSettings::documentPath());
QString videoName(videoPath + "/kwin_video1.cps");
while (QFile::exists(videoName)) {
autoincFilename(videoName);
}
2011-01-30 14:34:42 +00:00
if (CapturySetOutputFileName(client, QFile::encodeName(videoName).constData()) != CAPTURY_SUCCESS) {
kDebug(1212) << "Video recording file open failed";
return;
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull(); // trigger reading initial screen contents into buffer
kDebug(1212) << "Video recording start";
}
void VideoRecordEffect::autoincFilename(QString & name)
2011-01-30 14:34:42 +00:00
{
// If the name contains a number then increment it
QRegExp numSearch("(^|[^\\d])(\\d+)"); // we want to match as far left as possible, and when the number is at the start of the name
// Does it have a number?
int start = numSearch.lastIndexIn(name);
if (start != -1) {
// It has a number, increment it
start = numSearch.pos(2); // we are only interested in the second group
QString numAsStr = numSearch.capturedTexts()[ 2 ];
QString number = QString::number(numAsStr.toInt() + 1);
number = number.rightJustified(numAsStr.length(), '0');
name.replace(start, number.length(), number);
}
2011-01-30 14:34:42 +00:00
}
void VideoRecordEffect::stopRecording()
2011-01-30 14:34:42 +00:00
{
if (client == NULL)
return;
2011-01-30 14:34:42 +00:00
kDebug(1212) << "Video recording stop";
CapturyClose(client);
client = NULL;
2011-01-30 14:34:42 +00:00
}
void VideoRecordEffect::toggleRecording()
2011-01-30 14:34:42 +00:00
{
if (client == NULL)
startRecording();
else
stopRecording();
2011-01-30 14:34:42 +00:00
}
} // namespace
#include "videorecord.moc"