add more spacing
[personal-kdebase.git] / workspace / kwin / effects / videorecord.cpp
blobc3a7cbb185ea73b7936637ebcc823d15b0475870
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
23 This effect allows recording a video from the session.
25 Requires libcaptury:
27 - git clone git://gitorious.org/capseo/mainline.git capseo
28 - you may want to remove 1.10 from AUTOMAKE_OPTIONS in Makefile.am
29 - ./autogen.sh
30 - the usual configure && make && make install procedure
31 (you may want to pass --enable-theora --with-accel=x86 [or amd64])
33 - git clone git://gitorious.org/libcaptury/mainline.git libcaptury
34 - you may want to remove 1.10 from AUTOMAKE_OPTIONS in Makefile.am
35 - ./autogen.sh
36 - the usual configure && make && make install procedure
38 Video is saved to $HOME/kwin_video.cps, use
39 "cpsrecode -i kwin_video.cps -o - | mplayer -" to play,
40 to create a video use "cpsrecode -c theora -i kwin_video.cps -o screen.ogg"
41 or use mencoder in a similar way as mplayer.
45 #include "videorecord.h"
47 #include <kaction.h>
48 #include <kactioncollection.h>
49 #include <kdebug.h>
50 #include <klocale.h>
51 #include <qdir.h>
52 #include <qfile.h>
53 #include <kio/netaccess.h>
54 #include <KConfigGroup>
55 #include <KGlobalSettings>
57 #include <strings.h>
59 namespace KWin
62 KWIN_EFFECT( videorecord, VideoRecordEffect )
64 VideoRecordEffect::VideoRecordEffect()
65 : client( NULL )
67 KActionCollection* actionCollection = new KActionCollection( this );
68 KAction* a = static_cast< KAction* >( actionCollection->addAction( "VideoRecord" ));
69 a->setText( i18n("Toggle Video Recording" ));
70 a->setGlobalShortcut( KShortcut( Qt::CTRL + Qt::META + Qt::Key_V ));
71 connect( a, SIGNAL( triggered( bool )), this, SLOT( toggleRecording()));
72 area = QRect( 0, 0, displayWidth(), displayHeight());
75 VideoRecordEffect::~VideoRecordEffect()
77 stopRecording();
80 void VideoRecordEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data )
82 effects->paintScreen( mask, region, data );
83 if( client != NULL )
84 capture_region = ( mask & ( PAINT_WINDOW_TRANSFORMED | PAINT_SCREEN_TRANSFORMED ))
85 ? QRect( 0, 0, displayWidth(), displayHeight()) : region;
88 void VideoRecordEffect::postPaintScreen()
90 effects->postPaintScreen();
91 if( client != NULL )
93 #if 1
94 if( CapturyProcessRegionStart( client ) == CAPTURY_SUCCESS )
96 capture_region &= QRect( 0, 0, displayWidth(), displayHeight()); // limit to screen
97 foreach( const QRect &r, capture_region.rects())
99 int gly = displayHeight() - r.y() - r.height(); // opengl coords
100 CapturyProcessRegion( client, r.x(), gly, r.width(), r.height());
102 CapturyProcessRegionCommit( client );
104 #else
105 CapturyProcessFrame( client );
106 #endif
110 void VideoRecordEffect::startRecording()
112 if( client != NULL )
113 stopRecording();
114 bzero( &config, sizeof( config ));
115 config.x = area.x();
116 config.y = area.y();
117 config.width = area.width();
118 config.height = area.height();
119 config.scale = 0;
120 config.fps = 30; // TODO
121 config.deviceType = CAPTURY_DEVICE_GLX; // TODO
122 config.deviceHandle = display();
123 config.windowHandle = rootWindow(); // TODO
124 config.cursor = true;
125 client = CapturyOpen( &config );
126 if( client == NULL )
128 kDebug( 1212 ) << "Video recording init failed";
129 return;
131 KConfigGroup conf = EffectsHandler::effectConfig("VideoRecord");
132 QString videoPath =conf.readEntry( "videopath", KGlobalSettings::documentPath() );
133 QString videoName(videoPath +"/kwin_video1.cps" );
134 while(QFile::exists( videoName )) {
135 autoincFilename( videoName );
137 if( CapturySetOutputFileName( client, QFile::encodeName( videoName ).constData() ) != CAPTURY_SUCCESS )
139 kDebug( 1212 ) << "Video recording file open failed";
140 return;
142 effects->addRepaintFull(); // trigger reading initial screen contents into buffer
143 kDebug( 1212 ) << "Video recording start";
146 void VideoRecordEffect::autoincFilename(QString & name)
148 // If the name contains a number then increment it
149 QRegExp numSearch( "(^|[^\\d])(\\d+)" ); // we want to match as far left as possible, and when the number is at the start of the name
150 // Does it have a number?
151 int start = numSearch.lastIndexIn( name );
152 if (start != -1) {
153 // It has a number, increment it
154 start = numSearch.pos( 2 ); // we are only interested in the second group
155 QString numAsStr = numSearch.capturedTexts()[ 2 ];
156 QString number = QString::number( numAsStr.toInt() + 1 );
157 number = number.rightJustified( numAsStr.length(), '0' );
158 name.replace( start, number.length(), number );
161 void VideoRecordEffect::stopRecording()
163 if( client == NULL )
164 return;
165 kDebug( 1212 ) << "Video recording stop";
166 CapturyClose( client );
167 client = NULL;
170 void VideoRecordEffect::toggleRecording()
172 if( client == NULL )
173 startRecording();
174 else
175 stopRecording();
178 } // namespace
180 #include "videorecord.moc"