kwin/effects/zoom/zoom.cpp

488 lines
17 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 Sebastian Sauer <sebsauer@kdab.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 "zoom.h"
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtGui/QX11Info>
#include <QtGui/QApplication>
#include <QtGui/QStyle>
#include <QtDBus/QDBusConnection>
#include <kaction.h>
#include <kactioncollection.h>
#include <kstandardaction.h>
#include <KDE/KConfigGroup>
#include <KDE/KLocale>
#include <kwinglutils.h>
#include <kwinxrenderutils.h>
#include <X11/extensions/Xfixes.h>
#include <X11/Xcursor/Xcursor.h>
namespace KWin
{
static int nominalCursorSize(int iconSize)
2011-01-30 14:34:42 +00:00
{
for (int i = 512; i > 8; i /= 2) {
if (i < iconSize)
return i;
2011-01-30 14:34:42 +00:00
if ((i * .75) < iconSize)
return int(i * .75);
}
2011-01-30 14:34:42 +00:00
return 8;
}
2011-01-30 14:34:42 +00:00
KWIN_EFFECT(zoom, ZoomEffect)
ZoomEffect::ZoomEffect()
: Effect()
2011-01-30 14:34:42 +00:00
, zoom(1)
, target_zoom(1)
, polling(false)
, zoomFactor(1.25)
, mouseTracking(MouseTrackingProportional)
, enableFocusTracking(false)
, followFocus(true)
, mousePointer(MousePointerScale)
, focusDelay(350) // in milliseconds
, texture(0)
, xrenderPicture(0)
, imageWidth(0)
, imageHeight(0)
, isMouseHidden(false)
, xMove(0)
, yMove(0)
, moveFactor(20.0)
{
KActionCollection* actionCollection = new KActionCollection(this);
KAction* a = 0;
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction(KStandardAction::ZoomIn, this, SLOT(zoomIn())));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Equal));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction(KStandardAction::ZoomOut, this, SLOT(zoomOut())));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Minus));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction(KStandardAction::ActualSize, this, SLOT(actualSize())));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_0));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction("MoveZoomLeft"));
a->setText(i18n("Move Left"));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Left));
2011-01-30 14:34:42 +00:00
connect(a, SIGNAL(triggered(bool)), this, SLOT(moveZoomLeft()));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction("MoveZoomRight"));
a->setText(i18n("Move Right"));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Right));
2011-01-30 14:34:42 +00:00
connect(a, SIGNAL(triggered(bool)), this, SLOT(moveZoomRight()));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction("MoveZoomUp"));
a->setText(i18n("Move Up"));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Up));
2011-01-30 14:34:42 +00:00
connect(a, SIGNAL(triggered(bool)), this, SLOT(moveZoomUp()));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction("MoveZoomDown"));
a->setText(i18n("Move Down"));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Down));
2011-01-30 14:34:42 +00:00
connect(a, SIGNAL(triggered(bool)), this, SLOT(moveZoomDown()));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction("MoveMouseToFocus"));
a->setText(i18n("Move Mouse to Focus"));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_F5));
2011-01-30 14:34:42 +00:00
connect(a, SIGNAL(triggered(bool)), this, SLOT(moveMouseToFocus()));
2011-01-30 14:34:42 +00:00
a = static_cast< KAction* >(actionCollection->addAction("MoveMouseToCenter"));
a->setText(i18n("Move Mouse to Center"));
a->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_F6));
2011-01-30 14:34:42 +00:00
connect(a, SIGNAL(triggered(bool)), this, SLOT(moveMouseToCenter()));
timeline.setDuration(350);
timeline.setFrameRange(0, 100);
connect(&timeline, SIGNAL(frameChanged(int)), this, SLOT(timelineFrameChanged(int)));
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);
}
ZoomEffect::~ZoomEffect()
2011-01-30 14:34:42 +00:00
{
// switch off and free resources
showCursor();
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::showCursor()
2011-01-30 14:34:42 +00:00
{
#if defined(KWIN_HAVE_OPENGL_COMPOSITING) || defined(KWIN_HAVE_XRENDER_COMPOSITING)
2011-01-30 14:34:42 +00:00
if (isMouseHidden) {
// show the previously hidden mouse-pointer again and free the loaded texture/picture.
Display* display = QX11Info::display();
XFixesShowCursor(display, DefaultRootWindow(display));
delete texture;
texture = 0;
delete xrenderPicture;
xrenderPicture = 0;
isMouseHidden = false;
}
2011-01-30 14:34:42 +00:00
#endif
}
void ZoomEffect::hideCursor()
2011-01-30 14:34:42 +00:00
{
#if defined(KWIN_HAVE_OPENGL_COMPOSITING) || defined(KWIN_HAVE_XRENDER_COMPOSITING)
2011-01-30 14:34:42 +00:00
if (!isMouseHidden) {
// try to load the cursor-theme into a OpenGL texture and if successful then hide the mouse-pointer
recreateTexture();
2011-01-30 14:34:42 +00:00
if (texture || xrenderPicture) {
Display* display = QX11Info::display();
XFixesHideCursor(display, DefaultRootWindow(display));
isMouseHidden = true;
}
}
2011-01-30 14:34:42 +00:00
#endif
}
void ZoomEffect::recreateTexture()
2011-01-30 14:34:42 +00:00
{
// read details about the mouse-cursor theme define per default
2011-01-30 14:34:42 +00:00
KConfigGroup mousecfg(KSharedConfig::openConfig("kcminputrc"), "Mouse");
QString theme = mousecfg.readEntry("cursorTheme", QString());
QString size = mousecfg.readEntry("cursorSize", QString());
// try to find the to the theme-name matching cursor-directory.
QByteArray themePath;
2011-01-30 14:34:42 +00:00
foreach (const QString & baseDir, QString(XcursorLibraryPath()).split(':', QString::SkipEmptyParts)) {
QDir dir(baseDir);
2011-01-30 14:34:42 +00:00
if (!dir.exists()) continue;
if (!theme.isEmpty() && dir.cd(theme)) {
themePath = QFile::encodeName(dir.absolutePath());
break; // theme found, job is done and we can abort the search now
}
2011-01-30 14:34:42 +00:00
if (dir.cd("default")) // default is better then nothing, so keep it as backup
themePath = QFile::encodeName(dir.absolutePath());
}
// fetch a reasonable size for the cursor-theme image
bool ok;
int iconSize = size.toInt(&ok);
2011-01-30 14:34:42 +00:00
if (!ok)
iconSize = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
// load the cursor-theme image from the Xcursor-library
XcursorImage *ximg = XcursorLibraryLoadImage("left_ptr", themePath, nominalCursorSize(iconSize));
2011-01-30 14:34:42 +00:00
if (ximg) {
// turn the XcursorImage into a QImage that will be used to create the GLTexture/XRenderPicture.
imageWidth = ximg->width;
imageHeight = ximg->height;
QImage img((uchar*)ximg->pixels, imageWidth, imageHeight, QImage::Format_ARGB32_Premultiplied);
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
2011-01-30 14:34:42 +00:00
if (effects->compositingType() == OpenGLCompositing)
texture = new GLTexture(img);
#endif
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
2011-01-30 14:34:42 +00:00
if (effects->compositingType() == XRenderCompositing)
xrenderPicture = new XRenderPicture(QPixmap::fromImage(img));
#endif
XcursorImageDestroy(ximg);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void ZoomEffect::reconfigure(ReconfigureFlags)
{
KConfigGroup conf = EffectsHandler::effectConfig("Zoom");
// On zoom-in and zoom-out change the zoom by the defined zoom-factor.
zoomFactor = qMax(0.1, conf.readEntry("ZoomFactor", zoomFactor));
// Visibility of the mouse-pointer.
mousePointer = MousePointerType(conf.readEntry("MousePointer", int(mousePointer)));
// Track moving of the mouse.
mouseTracking = MouseTrackingType(conf.readEntry("MouseTracking", int(mouseTracking)));
// Enable tracking of the focused location.
bool _enableFocusTracking = conf.readEntry("EnableFocusTracking", enableFocusTracking);
2011-01-30 14:34:42 +00:00
if (enableFocusTracking != _enableFocusTracking) {
enableFocusTracking = _enableFocusTracking;
2011-01-30 14:34:42 +00:00
if (QDBusConnection::sessionBus().isConnected()) {
if (enableFocusTracking)
QDBusConnection::sessionBus().connect("org.kde.kaccessibleapp", "/Adaptor", "org.kde.kaccessibleapp.Adaptor", "focusChanged", this, SLOT(focusChanged(int, int, int, int, int, int)));
else
2011-01-30 14:34:42 +00:00
QDBusConnection::sessionBus().disconnect("org.kde.kaccessibleapp", "/Adaptor", "org.kde.kaccessibleapp.Adaptor", "focusChanged", this, SLOT(focusChanged(int, int, int, int, int, int)));
}
2011-01-30 14:34:42 +00:00
}
// When the focus changes, move the zoom area to the focused location.
followFocus = conf.readEntry("EnableFollowFocus", followFocus);
// The time in milliseconds to wait before a focus-event takes away a mouse-move.
focusDelay = qMax(0, conf.readEntry("FocusDelay", focusDelay));
// The factor the zoom-area will be moved on touching an edge on push-mode or using the navigation KAction's.
moveFactor = qMax(0.1, conf.readEntry("MoveFactor", moveFactor));
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void ZoomEffect::prePaintScreen(ScreenPrePaintData& data, int time)
{
if (zoom != target_zoom) {
double diff = time / animationTime(500.0);
if (target_zoom > zoom)
zoom = qMin(zoom * qMax(1 + diff, 1.2), target_zoom);
else
2011-01-30 14:34:42 +00:00
zoom = qMax(zoom * qMin(1 - diff, 0.8), target_zoom);
}
2011-01-30 14:34:42 +00:00
if (zoom == 1.0) {
showCursor();
2011-01-30 14:34:42 +00:00
} else {
hideCursor();
data.mask |= PAINT_SCREEN_TRANSFORMED;
}
2011-01-30 14:34:42 +00:00
effects->prePaintScreen(data, time);
}
void ZoomEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{
if (zoom != 1.0) {
data.xScale *= zoom;
data.yScale *= zoom;
// mouse-tracking allows navigation of the zoom-area using the mouse.
2011-01-30 14:34:42 +00:00
switch(mouseTracking) {
case MouseTrackingProportional:
data.xTranslate = - int(cursorPoint.x() * (zoom - 1.0));
data.yTranslate = - int(cursorPoint.y() * (zoom - 1.0));
prevPoint = cursorPoint;
break;
case MouseTrackingCentred:
data.xTranslate = qMin(0, qMax(int(displayWidth() - displayWidth() * zoom), int(displayWidth() / 2 - cursorPoint.x() * zoom)));
data.yTranslate = qMin(0, qMax(int(displayHeight() - displayHeight() * zoom), int(displayHeight() / 2 - cursorPoint.y() * zoom)));
prevPoint = cursorPoint;
break;
case MouseTrackingPush:
if (timeline.state() != QTimeLine::Running) {
// touching an edge of the screen moves the zoom-area in that direction.
int x = cursorPoint.x() * zoom - prevPoint.x() * (zoom - 1.0);
int y = cursorPoint.y() * zoom - prevPoint.y() * (zoom - 1.0);
int threshold = 1; //qMax(1, int(10.0 / zoom));
xMove = yMove = 0;
if (x < threshold)
xMove = - qMax(1.0, displayWidth() / zoom / moveFactor);
else if (x + threshold > displayWidth())
xMove = qMax(1.0, displayWidth() / zoom / moveFactor);
if (y < threshold)
yMove = - qMax(1.0, displayHeight() / zoom / moveFactor);
else if (y + threshold > displayHeight())
yMove = qMax(1.0, displayHeight() / zoom / moveFactor);
if (xMove != 0 || yMove != 0) {
prevPoint.setX(qMax(0, qMin(displayWidth(), prevPoint.x() + xMove)));
prevPoint.setY(qMax(0, qMin(displayHeight(), prevPoint.y() + yMove)));
timeline.start();
}
}
2011-01-30 14:34:42 +00:00
// fall through
case MouseTrackingDisabled:
data.xTranslate = - int(prevPoint.x() * (zoom - 1.0));
data.yTranslate = - int(prevPoint.y() * (zoom - 1.0));
break;
}
// use the focusPoint if focus tracking is enabled
2011-01-30 14:34:42 +00:00
if (enableFocusTracking && followFocus) {
bool acceptFocus = true;
2011-01-30 14:34:42 +00:00
if (mouseTracking != MouseTrackingDisabled && focusDelay > 0) {
// Wait some time for the mouse before doing the switch. This serves as threshold
// to prevent the focus from jumping around to much while working with the mouse.
const int msecs = lastMouseEvent.msecsTo(lastFocusEvent);
acceptFocus = msecs > focusDelay;
2011-01-30 14:34:42 +00:00
}
if (acceptFocus) {
data.xTranslate = - int(focusPoint.x() * (zoom - 1.0));
data.yTranslate = - int(focusPoint.y() * (zoom - 1.0));
prevPoint = focusPoint;
}
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
effects->paintScreen(mask, region, data);
2011-01-30 14:34:42 +00:00
if (zoom != 1.0 && mousePointer != MousePointerHide) {
// Draw the mouse-texture at the position matching to zoomed-in image of the desktop. Hiding the
// previous mouse-cursor and drawing our own fake mouse-cursor is needed to be able to scale the
// mouse-cursor up and to re-position those mouse-cursor to match to the chosen zoom-level.
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
2011-01-30 14:34:42 +00:00
if (texture) {
2010-12-08 17:45:23 +00:00
#ifndef KWIN_HAVE_OPENGLES
2011-01-30 14:34:42 +00:00
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT);
2010-12-08 17:45:23 +00:00
#endif
texture->bind();
2011-01-30 14:34:42 +00:00
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
int w = imageWidth;
int h = imageHeight;
2011-01-30 14:34:42 +00:00
if (mousePointer == MousePointerScale) {
w *= zoom;
h *= zoom;
2011-01-30 14:34:42 +00:00
}
QPoint p = QCursor::pos();
QRect rect(p.x() * zoom + data.xTranslate, p.y() * zoom + data.yTranslate, w, h);
texture->render(region, rect);
texture->unbind();
2010-12-08 17:45:23 +00:00
glDisable(GL_BLEND);
#ifndef KWIN_HAVE_OPENGLES
glPopAttrib();
2010-12-08 17:45:23 +00:00
#endif
2011-01-30 14:34:42 +00:00
}
#endif
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
2011-01-30 14:34:42 +00:00
if (xrenderPicture) {
QPoint p = QCursor::pos();
QRect rect(p.x() * zoom + data.xTranslate, p.y() * zoom + data.yTranslate, imageWidth, imageHeight);
2011-01-30 14:34:42 +00:00
XRenderComposite(display(), PictOpOver, *xrenderPicture, None, effects->xrenderBufferPicture(), 0, 0, 0, 0, rect.x(), rect.y(), rect.width(), rect.height());
}
2011-01-30 14:34:42 +00:00
#endif
}
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::postPaintScreen()
2011-01-30 14:34:42 +00:00
{
if (zoom != target_zoom)
effects->addRepaintFull();
effects->postPaintScreen();
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::zoomIn()
2011-01-30 14:34:42 +00:00
{
target_zoom *= zoomFactor;
2011-01-30 14:34:42 +00:00
if (!polling) {
polling = true;
effects->startMousePolling();
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
void ZoomEffect::zoomOut()
2011-01-30 14:34:42 +00:00
{
target_zoom /= zoomFactor;
2011-01-30 14:34:42 +00:00
if (target_zoom < 1) {
target_zoom = 1;
2011-01-30 14:34:42 +00:00
if (polling) {
polling = false;
effects->stopMousePolling();
}
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
void ZoomEffect::actualSize()
2011-01-30 14:34:42 +00:00
{
target_zoom = 1;
2011-01-30 14:34:42 +00:00
if (polling) {
polling = false;
effects->stopMousePolling();
}
2011-01-30 14:34:42 +00:00
effects->addRepaintFull();
}
void ZoomEffect::timelineFrameChanged(int /* frame */)
2011-01-30 14:34:42 +00:00
{
prevPoint.setX(qMax(0, qMin(displayWidth(), prevPoint.x() + xMove)));
prevPoint.setY(qMax(0, qMin(displayHeight(), prevPoint.y() + yMove)));
cursorPoint = prevPoint;
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::moveZoom(int x, int y)
2011-01-30 14:34:42 +00:00
{
if (timeline.state() == QTimeLine::Running)
timeline.stop();
2011-01-30 14:34:42 +00:00
if (x < 0)
xMove = - qMax(1.0, displayWidth() / zoom / moveFactor);
2011-01-30 14:34:42 +00:00
else if (x > 0)
xMove = qMax(1.0, displayWidth() / zoom / moveFactor);
else
xMove = 0;
2011-01-30 14:34:42 +00:00
if (y < 0)
yMove = - qMax(1.0, displayHeight() / zoom / moveFactor);
2011-01-30 14:34:42 +00:00
else if (y > 0)
yMove = qMax(1.0, displayHeight() / zoom / moveFactor);
else
yMove = 0;
2011-01-30 14:34:42 +00:00
timeline.start();
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::moveZoomLeft()
2011-01-30 14:34:42 +00:00
{
moveZoom(-1, 0);
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::moveZoomRight()
2011-01-30 14:34:42 +00:00
{
moveZoom(1, 0);
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::moveZoomUp()
2011-01-30 14:34:42 +00:00
{
moveZoom(0, -1);
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::moveZoomDown()
2011-01-30 14:34:42 +00:00
{
moveZoom(0, 1);
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::moveMouseToFocus()
2011-01-30 14:34:42 +00:00
{
QCursor::setPos(focusPoint.x(), focusPoint.y());
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::moveMouseToCenter()
2011-01-30 14:34:42 +00:00
{
//QRect r = effects->clientArea(KWin::ScreenArea, effects->activeScreen(), effects->currentDesktop());
QRect r(0, 0, displayWidth(), displayHeight());
QCursor::setPos(r.x() + r.width() / 2, r.y() + r.height() / 2);
2011-01-30 14:34:42 +00:00
}
2011-03-12 13:37:30 +00:00
void ZoomEffect::slotMouseChanged(const QPoint& pos, const QPoint& old, Qt::MouseButtons,
2011-01-30 14:34:42 +00:00
Qt::MouseButtons, Qt::KeyboardModifiers, Qt::KeyboardModifiers)
{
if (zoom == 1.0)
return;
cursorPoint = pos;
2011-01-30 14:34:42 +00:00
if (pos != old) {
lastMouseEvent = QTime::currentTime();
effects->addRepaintFull();
}
2011-01-30 14:34:42 +00:00
}
void ZoomEffect::focusChanged(int px, int py, int rx, int ry, int rwidth, int rheight)
2011-01-30 14:34:42 +00:00
{
if (zoom == 1.0)
return;
2011-01-30 14:34:42 +00:00
focusPoint = (px >= 0 && py >= 0) ? QPoint(px, py) : QPoint(rx + qMax(0, (qMin(displayWidth(), rwidth) / 2) - 60), ry + qMax(0, (qMin(displayHeight(), rheight) / 2) - 60));
if (enableFocusTracking) {
lastFocusEvent = QTime::currentTime();
effects->addRepaintFull();
}
2011-01-30 14:34:42 +00:00
}
} // namespace
#include "zoom.moc"