~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kwin/effects/_test/videorecord.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
 KWin - the KDE window manager
 
3
 This file is part of the KDE project.
 
4
 
 
5
Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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
*********************************************************************/
 
20
 
 
21
/*
 
22
 
 
23
 This effect allows recording a video from the session.
 
24
 
 
25
 Requires libcaptury:
 
26
 
 
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])
 
32
 
 
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
 
37
 
 
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.
 
42
 
 
43
*/
 
44
 
 
45
#include "videorecord.h"
 
46
 
 
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>
 
56
 
 
57
#include <strings.h>
 
58
 
 
59
namespace KWin
 
60
{
 
61
 
 
62
KWIN_EFFECT(videorecord, VideoRecordEffect)
 
63
 
 
64
VideoRecordEffect::VideoRecordEffect()
 
65
    : client(NULL)
 
66
{
 
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());
 
73
}
 
74
 
 
75
VideoRecordEffect::~VideoRecordEffect()
 
76
{
 
77
    stopRecording();
 
78
}
 
79
 
 
80
void VideoRecordEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
 
81
{
 
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;
 
86
}
 
87
 
 
88
void VideoRecordEffect::postPaintScreen()
 
89
{
 
90
    effects->postPaintScreen();
 
91
    if (client != NULL) {
 
92
#if 1
 
93
        if (CapturyProcessRegionStart(client) == CAPTURY_SUCCESS) {
 
94
            capture_region &= QRect(0, 0, displayWidth(), displayHeight());  // limit to screen
 
95
            foreach (const QRect & r, capture_region.rects()) {
 
96
                int gly = displayHeight() - r.y() - r.height(); // opengl coords
 
97
                CapturyProcessRegion(client, r.x(), gly, r.width(), r.height());
 
98
            }
 
99
            CapturyProcessRegionCommit(client);
 
100
        }
 
101
#else
 
102
        CapturyProcessFrame(client);
 
103
#endif
 
104
    }
 
105
}
 
106
 
 
107
void VideoRecordEffect::startRecording()
 
108
{
 
109
    if (client != NULL)
 
110
        stopRecording();
 
111
    bzero(&config, sizeof(config));
 
112
    config.x = area.x();
 
113
    config.y = area.y();
 
114
    config.width = area.width();
 
115
    config.height = area.height();
 
116
    config.scale = 0;
 
117
    config.fps = 30; // TODO
 
118
    config.deviceType = CAPTURY_DEVICE_GLX; // TODO
 
119
    config.deviceHandle = display();
 
120
    config.windowHandle = rootWindow(); // TODO
 
121
    config.cursor = true;
 
122
    client = CapturyOpen(&config);
 
123
    if (client == NULL) {
 
124
        kDebug(1212) << "Video recording init failed";
 
125
        return;
 
126
    }
 
127
    KConfigGroup conf = EffectsHandler::effectConfig("VideoRecord");
 
128
    QString videoPath = conf.readEntry("videopath", KGlobalSettings::documentPath());
 
129
    QString videoName(videoPath + "/kwin_video1.cps");
 
130
    while (QFile::exists(videoName)) {
 
131
        autoincFilename(videoName);
 
132
    }
 
133
    if (CapturySetOutputFileName(client, QFile::encodeName(videoName).constData()) != CAPTURY_SUCCESS) {
 
134
        kDebug(1212) << "Video recording file open failed";
 
135
        return;
 
136
    }
 
137
    effects->addRepaintFull(); // trigger reading initial screen contents into buffer
 
138
    kDebug(1212) << "Video recording start";
 
139
}
 
140
 
 
141
void VideoRecordEffect::autoincFilename(QString & name)
 
142
{
 
143
    // If the name contains a number then increment it
 
144
    QRegExp numSearch("(^|[^\\d])(\\d+)");   // we want to match as far left as possible, and when the number is at the start of the name
 
145
    // Does it have a number?
 
146
    int start = numSearch.lastIndexIn(name);
 
147
    if (start != -1) {
 
148
        // It has a number, increment it
 
149
        start = numSearch.pos(2);   // we are only interested in the second group
 
150
        QString numAsStr = numSearch.capturedTexts()[ 2 ];
 
151
        QString number = QString::number(numAsStr.toInt() + 1);
 
152
        number = number.rightJustified(numAsStr.length(), '0');
 
153
        name.replace(start, number.length(), number);
 
154
    }
 
155
}
 
156
void VideoRecordEffect::stopRecording()
 
157
{
 
158
    if (client == NULL)
 
159
        return;
 
160
    kDebug(1212) << "Video recording stop";
 
161
    CapturyClose(client);
 
162
    client = NULL;
 
163
}
 
164
 
 
165
void VideoRecordEffect::toggleRecording()
 
166
{
 
167
    if (client == NULL)
 
168
        startRecording();
 
169
    else
 
170
        stopRecording();
 
171
}
 
172
 
 
173
} // namespace
 
174
 
 
175
#include "videorecord.moc"