kwin/effects/startupfeedback/startupfeedback.cpp

404 lines
14 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2010 Martin Gräßlin <mgraesslin@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/>.
*********************************************************************/
#include "startupfeedback.h"
// Qt
#include <QApplication>
#include <QSize>
#include <QStyle>
#include <QtCore/QStandardPaths>
#include <QtGui/QPainter>
// KDE
#include <KConfigGroup>
#include <KSharedConfig>
#include <KIconLoader>
#include <KStartupInfo>
#include <KSelectionOwner>
// KWin
#include <kwinglutils.h>
// based on StartupId in KRunner by Lubos Lunak
// Copyright (C) 2001 Lubos Lunak <l.lunak@kde.org>
namespace KWin
{
// number of key frames for bouncing animation
static const int BOUNCE_FRAMES = 20;
// duration between two key frames in msec
static const int BOUNCE_FRAME_DURATION = 30;
// duration of one bounce animation
static const int BOUNCE_DURATION = BOUNCE_FRAME_DURATION * BOUNCE_FRAMES;
// number of key frames for blinking animation
static const int BLINKING_FRAMES = 5;
// duration between two key frames in msec
static const int BLINKING_FRAME_DURATION = 100;
// duration of one blinking animation
static const int BLINKING_DURATION = BLINKING_FRAME_DURATION * BLINKING_FRAMES;
//const int color_to_pixmap[] = { 0, 1, 2, 3, 2, 1 };
2011-01-30 14:34:42 +00:00
static const int FRAME_TO_BOUNCE_YOFFSET[] = {
-5, -1, 2, 5, 8, 10, 12, 13, 15, 15, 15, 15, 14, 12, 10, 8, 5, 2, -1, -5
2011-01-30 14:34:42 +00:00
};
static const QSize BOUNCE_SIZES[] = {
QSize(16, 16), QSize(14, 18), QSize(12, 20), QSize(18, 14), QSize(20, 12)
2011-01-30 14:34:42 +00:00
};
static const int FRAME_TO_BOUNCE_TEXTURE[] = {
0, 0, 0, 1, 2, 2, 1, 0, 3, 4, 4, 3, 0, 1, 2, 2, 1, 0, 0, 0
2011-01-30 14:34:42 +00:00
};
static const int FRAME_TO_BLINKING_COLOR[] = {
0, 1, 2, 3, 2, 1
2011-01-30 14:34:42 +00:00
};
static const QColor BLINKING_COLORS[] = {
Qt::black, Qt::darkGray, Qt::lightGray, Qt::white, Qt::white
2011-01-30 14:34:42 +00:00
};
static const int s_startupDefaultTimeout = 5;
StartupFeedbackEffect::StartupFeedbackEffect()
: m_bounceSizesRatio(1.0)
, m_startupInfo(new KStartupInfo(KStartupInfo::CleanOnCantDetect, this))
2011-01-30 14:34:42 +00:00
, m_selection(new KSelectionOwner("_KDE_STARTUP_FEEDBACK", -1, this))
, m_active(false)
, m_frame(0)
, m_progress(0)
, m_texture(0)
, m_type(BouncingFeedback)
, m_blinkingShader(0)
, m_cursorSize(0)
2011-01-30 14:34:42 +00:00
{
for (int i = 0; i < 5; ++i) {
m_bouncingTextures[i] = 0;
}
2011-01-30 14:34:42 +00:00
m_selection->claim(true);
2011-08-17 21:51:55 +00:00
connect(m_startupInfo, SIGNAL(gotNewStartup(KStartupInfoId,KStartupInfoData)), SLOT(gotNewStartup(KStartupInfoId,KStartupInfoData)));
connect(m_startupInfo, SIGNAL(gotRemoveStartup(KStartupInfoId,KStartupInfoData)), SLOT(gotRemoveStartup(KStartupInfoId,KStartupInfoData)));
connect(m_startupInfo, SIGNAL(gotStartupChange(KStartupInfoId,KStartupInfoData)), SLOT(gotStartupChange(KStartupInfoId,KStartupInfoData)));
2011-03-12 13:37:30 +00:00
connect(effects, SIGNAL(mouseChanged(QPoint,QPoint,Qt::MouseButtons,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::KeyboardModifiers)),
this, SLOT(slotMouseChanged(QPoint,QPoint,Qt::MouseButtons,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::KeyboardModifiers)));
2011-01-30 14:34:42 +00:00
reconfigure(ReconfigureAll);
}
StartupFeedbackEffect::~StartupFeedbackEffect()
2011-01-30 14:34:42 +00:00
{
if (m_active) {
effects->stopMousePolling();
2011-01-30 14:34:42 +00:00
}
for (int i = 0; i < 5; ++i) {
delete m_bouncingTextures[i];
2011-01-30 14:34:42 +00:00
}
delete m_texture;
2011-01-06 12:47:42 +00:00
delete m_blinkingShader;
2011-01-30 14:34:42 +00:00
}
bool StartupFeedbackEffect::supported()
2011-01-30 14:34:42 +00:00
{
return effects->isOpenGLCompositing();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::reconfigure(Effect::ReconfigureFlags flags)
{
Q_UNUSED(flags)
KConfig conf(QStringLiteral("klaunchrc"), KConfig::NoGlobals);
2011-01-30 14:34:42 +00:00
KConfigGroup c = conf.group("FeedbackStyle");
const bool busyCursor = c.readEntry("BusyCursor", true);
2011-01-30 14:34:42 +00:00
c = conf.group("BusyCursorSettings");
m_startupInfo->setTimeout(c.readEntry("Timeout", s_startupDefaultTimeout));
2011-01-30 14:34:42 +00:00
const bool busyBlinking = c.readEntry("Blinking", false);
const bool busyBouncing = c.readEntry("Bouncing", true);
if (!busyCursor)
m_type = NoFeedback;
2011-01-30 14:34:42 +00:00
else if (busyBouncing)
m_type = BouncingFeedback;
2011-01-06 12:47:42 +00:00
else if (busyBlinking) {
m_type = BlinkingFeedback;
if (effects->compositingType() == OpenGL2Compositing) {
2011-01-06 12:47:42 +00:00
delete m_blinkingShader;
m_blinkingShader = 0;
const QString shader = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kwin/blinking-startup-fragment.glsl"));
2011-01-06 12:47:42 +00:00
m_blinkingShader = ShaderManager::instance()->loadFragmentShader(ShaderManager::SimpleShader, shader);
if (m_blinkingShader->isValid()) {
qCDebug(KWINEFFECTS) << "Blinking Shader is valid";
2011-01-06 12:47:42 +00:00
} else {
qCDebug(KWINEFFECTS) << "Blinking Shader is not valid";
2011-01-06 12:47:42 +00:00
}
}
2011-01-30 14:34:42 +00:00
} else
m_type = PassiveFeedback;
2011-01-30 14:34:42 +00:00
if (m_active) {
stop();
2011-01-30 14:34:42 +00:00
start(m_startups[ m_currentStartup ]);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::prePaintScreen(ScreenPrePaintData& data, int time)
{
if (m_active) {
// need the unclipped version
2011-01-30 14:34:42 +00:00
switch(m_type) {
case BouncingFeedback:
m_progress = (m_progress + time) % BOUNCE_DURATION;
m_frame = qRound((qreal)m_progress / (qreal)BOUNCE_FRAME_DURATION) % BOUNCE_FRAMES;
m_currentGeometry = feedbackRect(); // bounce alters geometry with m_frame
data.paint.unite(m_currentGeometry);
break;
case BlinkingFeedback:
m_progress = (m_progress + time) % BLINKING_DURATION;
m_frame = qRound((qreal)m_progress / (qreal)BLINKING_FRAME_DURATION) % BLINKING_FRAMES;
break;
default:
break; // nothing
}
}
2011-01-30 14:34:42 +00:00
effects->prePaintScreen(data, time);
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{
effects->paintScreen(mask, region, data);
if (m_active) {
GLTexture* texture;
2011-01-30 14:34:42 +00:00
switch(m_type) {
case BouncingFeedback:
texture = m_bouncingTextures[ FRAME_TO_BOUNCE_TEXTURE[ m_frame ]];
break;
case BlinkingFeedback: // fall through
case PassiveFeedback:
texture = m_texture;
break;
default:
return; // safety
2011-01-30 14:34:42 +00:00
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
texture->bind();
2011-01-06 12:47:42 +00:00
bool useShader = false;
if (m_type == BlinkingFeedback) {
const QColor& blinkingColor = BLINKING_COLORS[ FRAME_TO_BLINKING_COLOR[ m_frame ]];
2011-01-06 12:47:42 +00:00
if (m_blinkingShader && m_blinkingShader->isValid()) {
useShader = true;
ShaderManager::instance()->pushShader(m_blinkingShader);
m_blinkingShader->setUniform("u_color", blinkingColor);
}
} else {
2011-01-06 12:47:42 +00:00
useShader = true;
ShaderManager::instance()->pushShader(ShaderManager::SimpleShader);
}
2011-01-30 14:34:42 +00:00
texture->render(m_currentGeometry, m_currentGeometry);
2011-01-06 12:47:42 +00:00
if (useShader) {
ShaderManager::instance()->popShader();
}
texture->unbind();
2011-01-30 14:34:42 +00:00
glDisable(GL_BLEND);
}
2011-01-30 14:34:42 +00:00
}
void StartupFeedbackEffect::postPaintScreen()
2011-01-30 14:34:42 +00:00
{
if (m_active) {
m_dirtyRect = m_currentGeometry; // ensure the now dirty region is cleaned on the next pass
if (m_type == BlinkingFeedback || m_type == BouncingFeedback)
effects->addRepaint(m_dirtyRect); // we also have to trigger a repaint
}
2011-01-30 14:34:42 +00:00
effects->postPaintScreen();
}
2011-03-12 13:37:30 +00:00
void StartupFeedbackEffect::slotMouseChanged(const QPoint& pos, const QPoint& oldpos, Qt::MouseButtons buttons,
2011-01-30 14:34:42 +00:00
Qt::MouseButtons oldbuttons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers)
{
Q_UNUSED(pos)
Q_UNUSED(oldpos)
Q_UNUSED(buttons)
Q_UNUSED(oldbuttons)
Q_UNUSED(modifiers)
Q_UNUSED(oldmodifiers)
if (m_active) {
m_dirtyRect |= m_currentGeometry;
m_currentGeometry = feedbackRect();
m_dirtyRect |= m_currentGeometry;
effects->addRepaint(m_dirtyRect);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::gotNewStartup(const KStartupInfoId& id, const KStartupInfoData& data)
{
const QString& icon = data.findIcon();
m_currentStartup = id;
m_startups[ id ] = icon;
2011-01-30 14:34:42 +00:00
start(icon);
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::gotRemoveStartup(const KStartupInfoId& id, const KStartupInfoData& data)
{
2011-04-30 11:17:26 +00:00
Q_UNUSED( data )
2011-01-30 14:34:42 +00:00
m_startups.remove(id);
if (m_startups.count() == 0) {
m_currentStartup = KStartupInfoId(); // null
stop();
return;
}
2011-01-30 14:34:42 +00:00
m_currentStartup = m_startups.begin().key();
start(m_startups[ m_currentStartup ]);
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::gotStartupChange(const KStartupInfoId& id, const KStartupInfoData& data)
{
if (m_currentStartup == id) {
const QString& icon = data.findIcon();
2011-01-30 14:34:42 +00:00
if (!icon.isEmpty() && icon != m_startups[ m_currentStartup ]) {
m_startups[ id ] = icon;
2011-01-30 14:34:42 +00:00
start(icon);
}
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::start(const QString& icon)
{
if (m_type == NoFeedback)
return;
2011-01-30 14:34:42 +00:00
if (!m_active)
effects->startMousePolling();
m_active = true;
// get ratio for bouncing cursor so we don't need to manually calculate the sizes for each icon size
if (m_type == BouncingFeedback)
m_bounceSizesRatio = IconSize(KIconLoader::Small) / 16.0;
2011-01-30 14:34:42 +00:00
QPixmap iconPixmap = KIconLoader::global()->loadIcon(icon, KIconLoader::Small, 0,
KIconLoader::DefaultState, QStringList(), 0, true); // return null pixmap if not found
if (iconPixmap.isNull())
iconPixmap = SmallIcon(QStringLiteral("system-run"));
2011-01-30 14:34:42 +00:00
prepareTextures(iconPixmap);
auto readCursorSize = []() -> int {
// read details about the mouse-cursor theme define per default
KConfigGroup mousecfg(KSharedConfig::openConfig(QStringLiteral("kcminputrc")), "Mouse");
QString size = mousecfg.readEntry("cursorSize", QString());
// fetch a reasonable size for the cursor-theme image
bool ok;
int cursorSize = size.toInt(&ok);
if (!ok)
cursorSize = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
return cursorSize;
};
m_cursorSize = readCursorSize();
m_dirtyRect = m_currentGeometry = feedbackRect();
effects->addRepaint(m_dirtyRect);
2011-01-30 14:34:42 +00:00
}
void StartupFeedbackEffect::stop()
2011-01-30 14:34:42 +00:00
{
if (m_active)
effects->stopMousePolling();
m_active = false;
Better handling for making the compositing OpenGL context current With QtQuick2 it's possible that the scene graph rendering context either lives in an own thread or uses the main GUI thread. In the latter case it's the same thread as our compositing OpenGL context lives in. This means our basic assumption that between two rendering passes the context stays current does not hold. The code already ensured that before we start a rendering pass the context is made current, but there are many more possible cases. If we use OpenGL in areas not triggered by the rendering loop but in response to other events the context needs to be made current. This includes the loading and unloading of effects (some effects use OpenGL in the static effect check, in the ctor and dtor), background loading of texture data, lazy loading after first usage invoked by shortcut, etc. etc. To properly handle these cases new methods are added to EffectsHandler to make the compositing OpenGL context current. These calls delegate down into the scene. On non-OpenGL scenes they are noop, but on OpenGL they go into the backend and make the context current. In addition they ensure that Qt doesn't think that it's QOpenGLContext is current by calling doneCurrent() on the QOpenGLContext::currentContext(). This unfortunately causes an additional call to makeCurrent with a null context, but there is no other way to tell Qt - it doesn't notice when a different context is made current with low level API calls. In the multi-threaded architecture this doesn't matter as ::currentContext() returns null. A short evaluation showed that a transition to QOpenGLContext doesn't seem feasible. Qt only supports either GLX or EGL while KWin supports both and when entering the transition phase for Wayland, it would become extremely tricky if our native platform is X11, but we want a Wayland EGL context. A future solution might be to have a "KWin-QPA plugin" which uses either xcb or Wayland and hides everything from Qt. The API documentation is extended to describe when the effects-framework ensures that an OpenGL context is current. The effects are changed to make the context current in cases where it's not guaranteed. This has been done by looking for creation or deletion of GLTextures and Shaders. If there are other OpenGL usages outside the rendering loop, ctor/dtor this needs to be changed, too.
2013-11-22 14:05:36 +00:00
effects->makeOpenGLContextCurrent();
2011-01-30 14:34:42 +00:00
switch(m_type) {
case BouncingFeedback:
2011-01-30 14:34:42 +00:00
for (int i = 0; i < 5; ++i) {
delete m_bouncingTextures[i];
m_bouncingTextures[i] = 0;
2011-01-30 14:34:42 +00:00
}
break;
case BlinkingFeedback:
case PassiveFeedback:
delete m_texture;
m_texture = 0;
break;
case NoFeedback:
return; // don't want the full repaint
default:
break; // impossible
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
2011-01-30 14:34:42 +00:00
void StartupFeedbackEffect::prepareTextures(const QPixmap& pix)
{
Better handling for making the compositing OpenGL context current With QtQuick2 it's possible that the scene graph rendering context either lives in an own thread or uses the main GUI thread. In the latter case it's the same thread as our compositing OpenGL context lives in. This means our basic assumption that between two rendering passes the context stays current does not hold. The code already ensured that before we start a rendering pass the context is made current, but there are many more possible cases. If we use OpenGL in areas not triggered by the rendering loop but in response to other events the context needs to be made current. This includes the loading and unloading of effects (some effects use OpenGL in the static effect check, in the ctor and dtor), background loading of texture data, lazy loading after first usage invoked by shortcut, etc. etc. To properly handle these cases new methods are added to EffectsHandler to make the compositing OpenGL context current. These calls delegate down into the scene. On non-OpenGL scenes they are noop, but on OpenGL they go into the backend and make the context current. In addition they ensure that Qt doesn't think that it's QOpenGLContext is current by calling doneCurrent() on the QOpenGLContext::currentContext(). This unfortunately causes an additional call to makeCurrent with a null context, but there is no other way to tell Qt - it doesn't notice when a different context is made current with low level API calls. In the multi-threaded architecture this doesn't matter as ::currentContext() returns null. A short evaluation showed that a transition to QOpenGLContext doesn't seem feasible. Qt only supports either GLX or EGL while KWin supports both and when entering the transition phase for Wayland, it would become extremely tricky if our native platform is X11, but we want a Wayland EGL context. A future solution might be to have a "KWin-QPA plugin" which uses either xcb or Wayland and hides everything from Qt. The API documentation is extended to describe when the effects-framework ensures that an OpenGL context is current. The effects are changed to make the context current in cases where it's not guaranteed. This has been done by looking for creation or deletion of GLTextures and Shaders. If there are other OpenGL usages outside the rendering loop, ctor/dtor this needs to be changed, too.
2013-11-22 14:05:36 +00:00
effects->makeOpenGLContextCurrent();
2011-01-30 14:34:42 +00:00
switch(m_type) {
case BouncingFeedback:
2011-01-30 14:34:42 +00:00
for (int i = 0; i < 5; ++i) {
delete m_bouncingTextures[i];
2011-01-30 14:34:42 +00:00
m_bouncingTextures[i] = new GLTexture(scalePixmap(pix, BOUNCE_SIZES[i]));
}
break;
case BlinkingFeedback:
case PassiveFeedback:
2011-01-30 14:34:42 +00:00
m_texture = new GLTexture(pix);
break;
default:
// for safety
m_active = false;
break;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QImage StartupFeedbackEffect::scalePixmap(const QPixmap& pm, const QSize& size) const
{
const QSize& adjustedSize = size * m_bounceSizesRatio;
QImage scaled = pm.toImage().scaled(adjustedSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
2011-01-30 14:34:42 +00:00
if (scaled.format() != QImage::Format_ARGB32_Premultiplied && scaled.format() != QImage::Format_ARGB32)
scaled = scaled.convertToFormat(QImage::Format_ARGB32);
QImage result(20 * m_bounceSizesRatio, 20 * m_bounceSizesRatio, QImage::Format_ARGB32);
2011-01-30 14:34:42 +00:00
QPainter p(&result);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(result.rect(), Qt::transparent);
p.drawImage((20 * m_bounceSizesRatio - adjustedSize.width()) / 2, (20*m_bounceSizesRatio - adjustedSize.height()) / 2, scaled, 0, 0, adjustedSize.width(), adjustedSize.height() * m_bounceSizesRatio);
return result;
2011-01-30 14:34:42 +00:00
}
QRect StartupFeedbackEffect::feedbackRect() const
2011-01-30 14:34:42 +00:00
{
int xDiff;
if (m_cursorSize <= 16)
xDiff = 8 + 7;
else if (m_cursorSize <= 32)
xDiff = 16 + 7;
else if (m_cursorSize <= 48)
xDiff = 24 + 7;
else
xDiff = 32 + 7;
int yDiff = xDiff;
2011-04-30 11:17:26 +00:00
GLTexture* texture = 0;
int yOffset = 0;
2011-01-30 14:34:42 +00:00
switch(m_type) {
case BouncingFeedback:
texture = m_bouncingTextures[ FRAME_TO_BOUNCE_TEXTURE[ m_frame ]];
yOffset = FRAME_TO_BOUNCE_YOFFSET[ m_frame ] * m_bounceSizesRatio;
2011-01-30 14:34:42 +00:00
break;
case BlinkingFeedback: // fall through
case PassiveFeedback:
texture = m_texture;
break;
default:
// nothing
break;
}
2011-01-30 14:34:42 +00:00
const QPoint cursorPos = effects->cursorPos() + QPoint(xDiff, yDiff + yOffset);
2011-04-30 11:17:26 +00:00
QRect rect;
if( texture )
rect = QRect(cursorPos, texture->size());
2011-01-30 14:34:42 +00:00
return rect;
}
bool StartupFeedbackEffect::isActive() const
{
return m_active;
}
} // namespace