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>
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
const int STARS = 5;
|
|
|
|
const int DIST = 50;
|
|
|
|
|
|
|
|
TrackMouseEffect::TrackMouseEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
: active(false)
|
|
|
|
, angle(0)
|
|
|
|
, texture(NULL)
|
|
|
|
{
|
2010-01-21 14:56:24 +00:00
|
|
|
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);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
TrackMouseEffect::~TrackMouseEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (mousePolling)
|
2010-01-21 14:56:24 +00:00
|
|
|
effects->stopMousePolling();
|
2007-04-29 17:35:43 +00:00
|
|
|
delete texture;
|
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)
|
|
|
|
{
|
|
|
|
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);
|
2010-01-21 14:56:24 +00:00
|
|
|
modifier = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (meta)
|
2010-01-21 14:56:24 +00:00
|
|
|
modifier |= Qt::MetaModifier;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (control)
|
2010-01-21 14:56:24 +00:00
|
|
|
modifier |= Qt::ControlModifier;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (alt)
|
2010-01-21 14:56:24 +00:00
|
|
|
modifier |= Qt::AltModifier;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (shift)
|
2010-01-21 14:56:24 +00:00
|
|
|
modifier |= Qt::ShiftModifier;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (modifier != 0 && action != NULL) {
|
|
|
|
if (!mousePolling)
|
2010-01-22 05:01:05 +00:00
|
|
|
effects->startMousePolling();
|
2010-01-21 14:56:24 +00:00
|
|
|
mousePolling = true;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else if (action != NULL) {
|
|
|
|
if (mousePolling)
|
2010-01-21 14:56:24 +00:00
|
|
|
effects->stopMousePolling();
|
|
|
|
mousePolling = false;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
if (active) {
|
2008-04-15 12:54:17 +00:00
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
|
if (!active)
|
2007-04-29 17:35:43 +00:00
|
|
|
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
|
2010-12-11 09:28:37 +00:00
|
|
|
if (ShaderManager::instance()->isValid()) {
|
|
|
|
ShaderManager::instance()->pushShader(ShaderManager::SimpleShader);
|
2010-12-08 18:35:38 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +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);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
texture->unbind();
|
2010-12-08 18:35:38 +00:00
|
|
|
glDisable(GL_BLEND);
|
2010-12-11 09:28:37 +00:00
|
|
|
if (ShaderManager::instance()->isValid()) {
|
|
|
|
ShaderManager::instance()->popShader();
|
2010-12-08 18:35:38 +00:00
|
|
|
}
|
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2007-04-29 17:35:43 +00:00
|
|
|
glPopAttrib();
|
|
|
|
#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
|
|
|
{
|
|
|
|
if (active) {
|
|
|
|
for (int i = 0;
|
|
|
|
i < STARS;
|
|
|
|
++i)
|
|
|
|
effects->addRepaint(starRect(i));
|
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
|
|
|
|
2010-01-21 14:56:24 +00:00
|
|
|
void TrackMouseEffect::toggle()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (mousePolling)
|
2010-01-22 05:01:05 +00:00
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!active) {
|
|
|
|
if (texture == NULL)
|
2010-01-21 14:56:24 +00:00
|
|
|
loadTexture();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (texture == NULL)
|
2010-01-21 14:56:24 +00:00
|
|
|
return;
|
|
|
|
active = true;
|
|
|
|
angle = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else
|
2010-01-21 14:56:24 +00:00
|
|
|
active = false;
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < STARS; ++i)
|
|
|
|
effects->addRepaint(starRect(i));
|
|
|
|
}
|
2010-01-21 14:56:24 +00:00
|
|
|
|
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)
|
2007-04-29 17:35:43 +00:00
|
|
|
loadTexture();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (texture == NULL)
|
2007-04-29 17:35:43 +00:00
|
|
|
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));
|
2010-01-21 14:56:24 +00:00
|
|
|
active = false;
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
QRect TrackMouseEffect::starRect(int num) const
|
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
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);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void TrackMouseEffect::loadTexture()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
QString file = KGlobal::dirs()->findResource("appdata", "trackmouse.png");
|
|
|
|
if (file.isEmpty())
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
QImage im(file);
|
|
|
|
texture = new GLTexture(im);
|
2007-04-29 17:35:43 +00:00
|
|
|
textureSize = im.size();
|
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
|
|
|
|
{
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
} // namespace
|