kwin/effects/trackmouse/trackmouse.cpp

213 lines
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>
Copyright (C) 2010 Jorge Mata <matamax123@gmail.com>
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 "trackmouse.h"
#include <QTime>
#include <kwinconfig.h>
2010-12-08 18:35:38 +00:00
#include <kwinglutils.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kaction.h>
#include <kactioncollection.h>
#include <KDE/KConfigGroup>
#include <KDE/KLocale>
#include <math.h>
#include <kdebug.h>
namespace KWin
{
2011-01-30 14:34:42 +00:00
KWIN_EFFECT(trackmouse, TrackMouseEffect)
const int STARS = 5;
const int DIST = 50;
TrackMouseEffect::TrackMouseEffect()
2011-01-30 14:34:42 +00:00
: active(false)
, angle(0)
, texture(NULL)
{
mousePolling = false;
2011-01-30 14:34:42 +00:00
actionCollection = new KActionCollection(this);
action = static_cast< KAction* >(actionCollection->addAction("TrackMouse"));
action->setText(i18n("Track mouse"));
action->setGlobalShortcut(KShortcut());
2010-12-08 18:35:38 +00:00
2011-01-30 14:34:42 +00:00
connect(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)),
this, SLOT(slotMouseChanged(QPoint,QPoint,Qt::MouseButtons,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::KeyboardModifiers)));
2011-01-30 14:34:42 +00:00
reconfigure(ReconfigureAll);
}
TrackMouseEffect::~TrackMouseEffect()
2011-01-30 14:34:42 +00:00
{
if (mousePolling)
effects->stopMousePolling();
delete texture;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TrackMouseEffect::reconfigure(ReconfigureFlags)
{
KConfigGroup conf = effects->effectConfig("TrackMouse");
bool shift = conf.readEntry("Shift", false);
bool alt = conf.readEntry("Alt", false);
bool control = conf.readEntry("Control", true);
bool meta = conf.readEntry("Meta", true);
modifier = 0;
2011-01-30 14:34:42 +00:00
if (meta)
modifier |= Qt::MetaModifier;
2011-01-30 14:34:42 +00:00
if (control)
modifier |= Qt::ControlModifier;
2011-01-30 14:34:42 +00:00
if (alt)
modifier |= Qt::AltModifier;
2011-01-30 14:34:42 +00:00
if (shift)
modifier |= Qt::ShiftModifier;
2011-01-30 14:34:42 +00:00
if (modifier != 0 && action != NULL) {
if (!mousePolling)
effects->startMousePolling();
mousePolling = true;
2011-01-30 14:34:42 +00:00
} else if (action != NULL) {
if (mousePolling)
effects->stopMousePolling();
mousePolling = false;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void TrackMouseEffect::prePaintScreen(ScreenPrePaintData& data, int time)
{
if (active) {
QTime t = QTime::currentTime();
angle = ((t.second() % 4) * 90.0) + (t.msec() / 1000.0 * 90.0);
}
2011-01-30 14:34:42 +00:00
effects->prePaintScreen(data, time);
}
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
if (!active)
return;
2011-01-30 14:34:42 +00:00
if (texture) {
2010-12-08 18:35:38 +00:00
#ifndef KWIN_HAVE_OPENGLES
2011-01-30 14:34:42 +00:00
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT);
2010-12-08 18:35:38 +00:00
#endif
bool useShader = false;
if (ShaderManager::instance()->isValid()) {
useShader = true;
ShaderManager::instance()->pushShader(ShaderManager::SimpleShader);
2010-12-08 18:35:38 +00:00
}
texture->bind();
2011-01-30 14:34:42 +00:00
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for (int i = 0;
i < STARS;
++i) {
QRect r = starRect(i);
texture->render(region, r);
}
texture->unbind();
2010-12-08 18:35:38 +00:00
glDisable(GL_BLEND);
if (ShaderManager::instance()->isValid()) {
ShaderManager::instance()->popShader();
2010-12-08 18:35:38 +00:00
}
#ifndef KWIN_HAVE_OPENGLES
glPopAttrib();
#endif
}
2011-01-30 14:34:42 +00:00
}
void TrackMouseEffect::postPaintScreen()
2011-01-30 14:34:42 +00:00
{
if (active) {
for (int i = 0;
i < STARS;
++i)
effects->addRepaint(starRect(i));
}
2011-01-30 14:34:42 +00:00
effects->postPaintScreen();
}
void TrackMouseEffect::toggle()
2011-01-30 14:34:42 +00:00
{
if (mousePolling)
return;
2011-01-30 14:34:42 +00:00
if (!active) {
if (texture == NULL)
loadTexture();
2011-01-30 14:34:42 +00:00
if (texture == NULL)
return;
active = true;
angle = 0;
2011-01-30 14:34:42 +00:00
} else
active = false;
2011-01-30 14:34:42 +00:00
for (int i = 0; i < STARS; ++i)
effects->addRepaint(starRect(i));
}
2011-03-12 13:37:30 +00:00
void TrackMouseEffect::slotMouseChanged(const QPoint&, const QPoint&, Qt::MouseButtons,
2011-01-30 14:34:42 +00:00
Qt::MouseButtons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers)
{
if (modifier != 0 && modifiers == modifier) {
if (!active) {
if (texture == NULL)
loadTexture();
2011-01-30 14:34:42 +00:00
if (texture == NULL)
return;
active = true;
angle = 0;
}
2011-01-30 14:34:42 +00:00
for (int i = 0; i < STARS; ++i)
effects->addRepaint(starRect(i));
} else if (active) {
for (int i = 0; i < STARS; ++i)
effects->addRepaint(starRect(i));
active = false;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QRect TrackMouseEffect::starRect(int num) const
{
int a = angle + 360 / STARS * num;
2011-01-30 14:34:42 +00:00
int x = cursorPos().x() + int(DIST * cos(a * (2 * M_PI / 360)));
int y = cursorPos().y() + int(DIST * sin(a * (2 * M_PI / 360)));
return QRect(QPoint(x - textureSize.width() / 2,
y - textureSize.height() / 2), textureSize);
}
void TrackMouseEffect::loadTexture()
2011-01-30 14:34:42 +00:00
{
QString file = KGlobal::dirs()->findResource("appdata", "trackmouse.png");
if (file.isEmpty())
return;
2011-01-30 14:34:42 +00:00
QImage im(file);
texture = new GLTexture(im);
textureSize = im.size();
2011-01-30 14:34:42 +00:00
}
} // namespace