2007-11-27 19:40:25 +00:00
|
|
|
/********************************************************************
|
2007-04-29 17:35:43 +00:00
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
|
2010-01-21 14:56:24 +00:00
|
|
|
Copyright (C) 2010 Jorge Mata <matamax123@gmail.com>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2007-11-27 19:40:25 +00:00
|
|
|
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/>.
|
|
|
|
*********************************************************************/
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#include "trackmouse.h"
|
|
|
|
|
2008-04-15 12:54:17 +00:00
|
|
|
#include <QTime>
|
2012-04-19 19:06:11 +00:00
|
|
|
#include <QMatrix4x4>
|
2008-04-15 12:54:17 +00:00
|
|
|
|
2007-12-17 14:14:53 +00:00
|
|
|
#include <kwinconfig.h>
|
2010-12-08 18:35:38 +00:00
|
|
|
#include <kwinglutils.h>
|
2007-05-13 17:47:20 +00:00
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
#include <kglobal.h>
|
|
|
|
#include <kstandarddirs.h>
|
|
|
|
|
2010-01-21 14:56:24 +00:00
|
|
|
#include <kaction.h>
|
|
|
|
#include <kactioncollection.h>
|
2011-02-17 18:35:08 +00:00
|
|
|
#include <KDE/KConfigGroup>
|
|
|
|
#include <KDE/KLocale>
|
2010-01-21 14:56:24 +00:00
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
#include <math.h>
|
2012-04-19 19:06:11 +00:00
|
|
|
#include <X11/extensions/Xrender.h>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#include <kdebug.h>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
KWIN_EFFECT(trackmouse, TrackMouseEffect)
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
TrackMouseEffect::TrackMouseEffect()
|
2012-04-19 19:06:11 +00:00
|
|
|
: m_active(false)
|
|
|
|
, m_angle(0)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
m_texture[0] = m_texture[1] = 0;
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
m_pixmap[0] = m_pixmap[1] = 0;
|
|
|
|
if ( effects->compositingType() == XRenderCompositing)
|
|
|
|
m_angleBase = 1.57079632679489661923; // Pi/2
|
|
|
|
#endif
|
|
|
|
if ( effects->compositingType() == OpenGLCompositing)
|
|
|
|
m_angleBase = 90.0;
|
|
|
|
m_mousePolling = false;
|
|
|
|
KActionCollection *actionCollection = new KActionCollection(this);
|
|
|
|
m_action = static_cast< KAction* >(actionCollection->addAction("TrackMouse"));
|
|
|
|
m_action->setText(i18n("Track mouse"));
|
|
|
|
m_action->setGlobalShortcut(KShortcut());
|
2010-12-08 18:35:38 +00:00
|
|
|
|
2012-04-19 19:06:11 +00:00
|
|
|
connect(m_action, SIGNAL(triggered(bool)), this, SLOT(toggle()));
|
2011-03-12 13:37:30 +00:00
|
|
|
connect(effects, SIGNAL(mouseChanged(QPoint,QPoint,Qt::MouseButtons,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::KeyboardModifiers)),
|
2012-04-19 19:06:11 +00:00
|
|
|
SLOT(slotMouseChanged(QPoint,QPoint,Qt::MouseButtons,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::KeyboardModifiers)));
|
2011-01-30 14:34:42 +00:00
|
|
|
reconfigure(ReconfigureAll);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
TrackMouseEffect::~TrackMouseEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
if (m_mousePolling)
|
2010-01-21 14:56:24 +00:00
|
|
|
effects->stopMousePolling();
|
2012-04-19 19:06:11 +00:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
delete m_texture[i]; m_texture[i] = 0;
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
delete m_pixmap[i]; m_pixmap[i] = 0;
|
|
|
|
#endif
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void TrackMouseEffect::reconfigure(ReconfigureFlags)
|
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
m_modifiers = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
KConfigGroup conf = effects->effectConfig("TrackMouse");
|
2012-04-19 19:06:11 +00:00
|
|
|
if (conf.readEntry("Shift", false))
|
|
|
|
m_modifiers |= Qt::ShiftModifier;
|
|
|
|
if (conf.readEntry("Alt", false))
|
|
|
|
m_modifiers |= Qt::AltModifier;
|
|
|
|
if (conf.readEntry("Control", true))
|
|
|
|
m_modifiers |= Qt::ControlModifier;
|
|
|
|
if (conf.readEntry("Meta", true))
|
|
|
|
m_modifiers |= Qt::MetaModifier;
|
|
|
|
|
|
|
|
if (m_modifiers) {
|
|
|
|
if (!m_mousePolling)
|
2010-01-22 05:01:05 +00:00
|
|
|
effects->startMousePolling();
|
2012-04-19 19:06:11 +00:00
|
|
|
m_mousePolling = true;
|
|
|
|
} else if (m_mousePolling) {
|
2010-01-21 14:56:24 +00:00
|
|
|
effects->stopMousePolling();
|
2012-04-19 19:06:11 +00:00
|
|
|
m_mousePolling = false;
|
2010-01-21 14:56:24 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-01-21 14:56:24 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void TrackMouseEffect::prePaintScreen(ScreenPrePaintData& data, int time)
|
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
if (m_active) {
|
2008-04-15 12:54:17 +00:00
|
|
|
QTime t = QTime::currentTime();
|
2012-04-19 19:06:11 +00:00
|
|
|
m_angle = ((t.second() % 4) * m_angleBase) + (t.msec() / 1000.0 * m_angleBase);
|
|
|
|
m_lastRect[0].moveCenter(cursorPos());
|
|
|
|
m_lastRect[1].moveCenter(cursorPos());
|
|
|
|
data.paint |= m_lastRect[0].adjusted(-1,-1,1,1);
|
2008-04-15 12:54:17 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->prePaintScreen(data, time);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void TrackMouseEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
|
|
|
|
{
|
|
|
|
effects->paintScreen(mask, region, data); // paint normal screen
|
2012-04-19 19:06:11 +00:00
|
|
|
if (!m_active)
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
2012-04-19 19:06:11 +00:00
|
|
|
|
|
|
|
if ( effects->compositingType() == OpenGLCompositing && m_texture[0] && m_texture[1]) {
|
|
|
|
GLShader *shader(0);
|
|
|
|
QMatrix4x4 modelview;
|
2010-12-11 09:28:37 +00:00
|
|
|
if (ShaderManager::instance()->isValid()) {
|
2012-04-19 19:06:11 +00:00
|
|
|
ShaderManager::instance()->pushShader(ShaderManager::GenericShader);
|
|
|
|
shader = ShaderManager::instance()->getBoundShader();
|
|
|
|
modelview = shader->getUniformMatrix4x4("modelview");
|
2010-12-08 18:35:38 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
glEnable(GL_BLEND);
|
2012-04-19 19:06:11 +00:00
|
|
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
QMatrix4x4 matrix(modelview);
|
|
|
|
const QPointF p = m_lastRect[0].topLeft() + QPoint(m_lastRect[0].width()/2.0, m_lastRect[0].height()/2.0);
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
matrix.translate(p.x(), p.y(), 0.0);
|
|
|
|
matrix.rotate(i ? -2*m_angle : m_angle, 0, 0, 1.0);
|
|
|
|
matrix.translate(-p.x(), -p.y(), 0.0);
|
|
|
|
if (shader)
|
|
|
|
shader->setUniform(GLShader::ModelViewMatrix, matrix);
|
|
|
|
else
|
|
|
|
pushMatrix(matrix);
|
|
|
|
m_texture[i]->bind();
|
|
|
|
m_texture[i]->render(region, m_lastRect[i]);
|
|
|
|
m_texture[i]->unbind();
|
|
|
|
if (!shader)
|
|
|
|
popMatrix();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-12-08 18:35:38 +00:00
|
|
|
glDisable(GL_BLEND);
|
2010-12-11 09:28:37 +00:00
|
|
|
if (ShaderManager::instance()->isValid()) {
|
2012-04-19 19:06:11 +00:00
|
|
|
shader->setUniform(GLShader::ModelViewMatrix, modelview);
|
2010-12-11 09:28:37 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
2010-12-08 18:35:38 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2012-04-19 19:06:11 +00:00
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
if ( effects->compositingType() == XRenderCompositing && m_pixmap[0] && m_pixmap[1]) {
|
|
|
|
float sine = sin(m_angle);
|
|
|
|
const float cosine = cos(m_angle);
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
if (i) sine = -sine;
|
|
|
|
const float dx = m_pixmap[i]->width()/2.0;
|
|
|
|
const float dy = m_pixmap[i]->height()/2.0;
|
|
|
|
XTransform xform = {{
|
|
|
|
{ XDoubleToFixed( cosine ), XDoubleToFixed( -sine ), XDoubleToFixed( dx - cosine*dx + sine*dy ) },
|
|
|
|
{ XDoubleToFixed( sine ), XDoubleToFixed( cosine ), XDoubleToFixed( dy - sine*dx - cosine*dy ) },
|
|
|
|
{ XDoubleToFixed( 0.0 ), XDoubleToFixed( 0.0 ), XDoubleToFixed( 1.0 ) }
|
|
|
|
}};
|
|
|
|
XRenderSetPictureTransform(display(), m_pixmap[i]->x11PictureHandle(), &xform );
|
|
|
|
XRenderSetPictureFilter( display(), m_pixmap[i]->x11PictureHandle(), FilterBilinear, 0, 0 );
|
|
|
|
XRenderComposite(display(), PictOpOver, m_pixmap[i]->x11PictureHandle(), 0,
|
|
|
|
effects->xrenderBufferPicture(), 0, 0, 0, 0,
|
|
|
|
m_lastRect[i].x(), m_lastRect[i].y(), m_lastRect[i].width(), m_lastRect[i].height());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void TrackMouseEffect::postPaintScreen()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
if (m_active) {
|
|
|
|
effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1));
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->postPaintScreen();
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2012-04-19 19:06:11 +00:00
|
|
|
bool TrackMouseEffect::init()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
if (!(m_texture[0] || m_pixmap[0])) {
|
|
|
|
loadTexture();
|
|
|
|
if (!(m_texture[0] || m_pixmap[0]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (!m_texture[0]) {
|
|
|
|
loadTexture();
|
|
|
|
if (!m_texture[0])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
m_lastRect[0].moveCenter(cursorPos());
|
|
|
|
m_lastRect[1].moveCenter(cursorPos());
|
|
|
|
m_active = true;
|
|
|
|
m_angle = 0;
|
|
|
|
return true;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-01-21 14:56:24 +00:00
|
|
|
|
2012-04-19 19:06:11 +00:00
|
|
|
void TrackMouseEffect::toggle()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
if (m_mousePolling)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_active) {
|
|
|
|
m_active = false;
|
|
|
|
} else if (!init()) {
|
|
|
|
return;
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2012-04-19 19:06:11 +00:00
|
|
|
effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2012-04-19 19:06:11 +00:00
|
|
|
void TrackMouseEffect::slotMouseChanged(const QPoint&, const QPoint&,
|
|
|
|
Qt::MouseButtons, Qt::MouseButtons,
|
|
|
|
Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
if (!m_mousePolling) // we didn't ask for it but maybe someone else did...
|
|
|
|
return;
|
|
|
|
if (m_modifiers && modifiers == m_modifiers) {
|
|
|
|
if (!m_active && !init()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1));
|
|
|
|
} else if (m_active) {
|
|
|
|
m_active = false;
|
|
|
|
effects->addRepaint(m_lastRect[0].adjusted(-1,-1,1,1));
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void TrackMouseEffect::loadTexture()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
QString f[2] = {KGlobal::dirs()->findResource("appdata", "tm_outer.png"),
|
|
|
|
KGlobal::dirs()->findResource("appdata", "tm_inner.png")};
|
|
|
|
if (f[0].isEmpty() || f[1].isEmpty())
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
2012-04-19 19:06:11 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
if ( effects->compositingType() == OpenGLCompositing) {
|
|
|
|
QImage img(f[i]);
|
|
|
|
m_texture[i] = new GLTexture(img);
|
|
|
|
m_lastRect[i].setSize(img.size());
|
|
|
|
}
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
if ( effects->compositingType() == XRenderCompositing) {
|
|
|
|
m_pixmap[i] = new QPixmap(f[i]);
|
|
|
|
m_lastRect[i].setSize(m_pixmap[i]->size());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-08-27 09:21:31 +00:00
|
|
|
bool TrackMouseEffect::isActive() const
|
|
|
|
{
|
2012-04-19 19:06:11 +00:00
|
|
|
return m_active;
|
2011-08-27 09:21:31 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
} // namespace
|