2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2006 Lubos Lunak <l.lunak@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#include "mousemark.h"
|
|
|
|
|
2012-09-12 17:14:30 +00:00
|
|
|
// KConfigSkeleton
|
|
|
|
#include "mousemarkconfig.h"
|
|
|
|
|
2013-08-14 19:13:12 +00:00
|
|
|
#include <QAction>
|
2007-12-17 14:14:53 +00:00
|
|
|
#include <kwinconfig.h>
|
2015-10-30 09:14:55 +00:00
|
|
|
#include <kwinglplatform.h>
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KGlobalAccel>
|
|
|
|
#include <KLocalizedString>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2013-06-24 06:08:55 +00:00
|
|
|
#include <QPainter>
|
|
|
|
|
2019-07-09 19:19:26 +00:00
|
|
|
#include <cmath>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2013-01-29 07:20:53 +00:00
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
#include <xcb/render.h>
|
|
|
|
#endif
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2008-01-08 16:21:04 +00:00
|
|
|
#define NULL_POINT (QPoint( -1, -1 )) // null point is (0,0), which is valid :-/
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
MouseMarkEffect::MouseMarkEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2016-12-02 19:27:43 +00:00
|
|
|
initConfig<MouseMarkConfig>();
|
2013-12-10 10:45:33 +00:00
|
|
|
QAction* a = new QAction(this);
|
|
|
|
a->setObjectName(QStringLiteral("ClearMouseMarks"));
|
2011-01-30 14:34:42 +00:00
|
|
|
a->setText(i18n("Clear All Mouse Marks"));
|
2013-08-14 19:13:12 +00:00
|
|
|
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::SHIFT + Qt::META + Qt::Key_F11);
|
|
|
|
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::SHIFT + Qt::META + Qt::Key_F11);
|
2013-07-10 10:26:50 +00:00
|
|
|
effects->registerGlobalShortcut(Qt::SHIFT + Qt::META + Qt::Key_F11, a);
|
2019-01-01 20:48:53 +00:00
|
|
|
connect(a, &QAction::triggered, this, &MouseMarkEffect::clear);
|
2013-12-10 10:45:33 +00:00
|
|
|
a = new QAction(this);
|
|
|
|
a->setObjectName(QStringLiteral("ClearLastMouseMark"));
|
2011-01-30 14:34:42 +00:00
|
|
|
a->setText(i18n("Clear Last Mouse Mark"));
|
2013-08-14 19:13:12 +00:00
|
|
|
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::SHIFT + Qt::META + Qt::Key_F12);
|
|
|
|
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::SHIFT + Qt::META + Qt::Key_F12);
|
2013-07-10 10:26:50 +00:00
|
|
|
effects->registerGlobalShortcut(Qt::SHIFT + Qt::META + Qt::Key_F12, a);
|
2019-01-01 20:48:53 +00:00
|
|
|
connect(a, &QAction::triggered, this, &MouseMarkEffect::clearLast);
|
2013-08-14 19:13:12 +00:00
|
|
|
|
2019-01-01 20:48:53 +00:00
|
|
|
connect(effects, &EffectsHandler::mouseChanged, this, &MouseMarkEffect::slotMouseChanged);
|
|
|
|
connect(effects, &EffectsHandler::screenLockingChanged, this, &MouseMarkEffect::screenLockingChanged);
|
2011-01-30 14:34:42 +00:00
|
|
|
reconfigure(ReconfigureAll);
|
2008-10-02 09:27:32 +00:00
|
|
|
arrow_start = NULL_POINT;
|
2009-02-01 15:16:52 +00:00
|
|
|
effects->startMousePolling(); // We require it to detect activation as well
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-02-01 15:16:52 +00:00
|
|
|
|
|
|
|
MouseMarkEffect::~MouseMarkEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2009-02-01 15:16:52 +00:00
|
|
|
effects->stopMousePolling();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-11-24 14:23:04 +00:00
|
|
|
|
2012-04-19 18:25:27 +00:00
|
|
|
static int width_2 = 1;
|
2011-01-30 14:34:42 +00:00
|
|
|
void MouseMarkEffect::reconfigure(ReconfigureFlags)
|
|
|
|
{
|
2014-03-25 15:29:03 +00:00
|
|
|
MouseMarkConfig::self()->read();
|
2012-09-12 17:14:30 +00:00
|
|
|
width = MouseMarkConfig::lineWidth();
|
2012-04-19 18:25:27 +00:00
|
|
|
width_2 = width / 2;
|
2012-09-12 17:14:30 +00:00
|
|
|
color = MouseMarkConfig::color();
|
2010-12-12 13:51:56 +00:00
|
|
|
color.setAlphaF(1.0);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2012-04-19 18:25:27 +00:00
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
2013-01-29 07:20:53 +00:00
|
|
|
void MouseMarkEffect::addRect(const QPoint &p1, const QPoint &p2, xcb_rectangle_t *r, xcb_render_color_t *c)
|
2012-04-19 18:25:27 +00:00
|
|
|
{
|
|
|
|
r->x = qMin(p1.x(), p2.x()) - width_2;
|
|
|
|
r->y = qMin(p1.y(), p2.y()) - width_2;
|
|
|
|
r->width = qAbs(p1.x()-p2.x()) + 1 + width_2;
|
|
|
|
r->height = qAbs(p1.y()-p2.y()) + 1 + width_2;
|
|
|
|
// fast move -> large rect, <strike>tess...</strike> interpolate a line
|
|
|
|
if (r->width > 3*width/2 && r->height > 3*width/2) {
|
|
|
|
const int n = sqrt(r->width*r->width + r->height*r->height) / width;
|
2013-01-29 07:20:53 +00:00
|
|
|
xcb_rectangle_t *rects = new xcb_rectangle_t[n-1];
|
2012-04-19 18:25:27 +00:00
|
|
|
const int w = p1.x() < p2.x() ? r->width : -r->width;
|
|
|
|
const int h = p1.y() < p2.y() ? r->height : -r->height;
|
|
|
|
for (int i = 1; i < n; ++i) {
|
|
|
|
rects[i-1].x = p1.x() + i*w/n;
|
|
|
|
rects[i-1].y = p1.y() + i*h/n;
|
|
|
|
rects[i-1].width = rects[i-1].height = width;
|
|
|
|
}
|
2014-04-16 13:58:06 +00:00
|
|
|
xcb_render_fill_rectangles(xcbConnection(), XCB_RENDER_PICT_OP_SRC, effects->xrenderBufferPicture(), *c, n - 1, rects);
|
2012-04-19 18:25:27 +00:00
|
|
|
delete [] rects;
|
|
|
|
r->x = p1.x();
|
|
|
|
r->y = p1.y();
|
|
|
|
r->width = r->height = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-10-29 22:04:15 +00:00
|
|
|
void MouseMarkEffect::paintScreen(int mask, const QRegion ®ion, ScreenPaintData& data)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
effects->paintScreen(mask, region, data); // paint normal screen
|
|
|
|
if (marks.isEmpty() && drawing.isEmpty())
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
2012-09-20 09:33:32 +00:00
|
|
|
if ( effects->isOpenGLCompositing()) {
|
2015-10-30 09:14:55 +00:00
|
|
|
if (!GLPlatform::instance()->isGLES()) {
|
2018-08-09 18:29:30 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
2015-10-30 09:14:55 +00:00
|
|
|
glEnable(GL_LINE_SMOOTH);
|
2018-08-09 18:29:30 +00:00
|
|
|
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
2015-10-30 09:14:55 +00:00
|
|
|
}
|
2012-04-19 18:25:27 +00:00
|
|
|
glLineWidth(width);
|
|
|
|
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
|
|
|
|
vbo->reset();
|
|
|
|
vbo->setUseColor(true);
|
|
|
|
vbo->setColor(color);
|
2015-11-27 09:04:56 +00:00
|
|
|
ShaderBinder binder(ShaderTrait::UniformColor);
|
|
|
|
binder.shader()->setUniform(GLShader::ModelViewProjectionMatrix, data.projectionMatrix());
|
2012-04-19 18:25:27 +00:00
|
|
|
QVector<float> verts;
|
|
|
|
foreach (const Mark & mark, marks) {
|
|
|
|
verts.clear();
|
|
|
|
verts.reserve(mark.size() * 2);
|
|
|
|
foreach (const QPoint & p, mark) {
|
|
|
|
verts << p.x() << p.y();
|
|
|
|
}
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
vbo->setData(verts.size() / 2, 2, verts.data(), nullptr);
|
2012-04-19 18:25:27 +00:00
|
|
|
vbo->render(GL_LINE_STRIP);
|
|
|
|
}
|
|
|
|
if (!drawing.isEmpty()) {
|
|
|
|
verts.clear();
|
|
|
|
verts.reserve(drawing.size() * 2);
|
|
|
|
foreach (const QPoint & p, drawing) {
|
|
|
|
verts << p.x() << p.y();
|
|
|
|
}
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
vbo->setData(verts.size() / 2, 2, verts.data(), nullptr);
|
2012-04-19 18:25:27 +00:00
|
|
|
vbo->render(GL_LINE_STRIP);
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2012-04-19 18:25:27 +00:00
|
|
|
glLineWidth(1.0);
|
2015-10-30 09:14:55 +00:00
|
|
|
if (!GLPlatform::instance()->isGLES()) {
|
|
|
|
glDisable(GL_LINE_SMOOTH);
|
2018-08-09 18:29:30 +00:00
|
|
|
glDisable(GL_BLEND);
|
2015-10-30 09:14:55 +00:00
|
|
|
}
|
2010-12-12 13:51:56 +00:00
|
|
|
}
|
2012-04-19 18:25:27 +00:00
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
if ( effects->compositingType() == XRenderCompositing) {
|
2013-02-12 09:57:27 +00:00
|
|
|
xcb_render_color_t c = preMultiply(color);
|
2012-04-19 18:25:27 +00:00
|
|
|
for (int i = 0; i < marks.count(); ++i) {
|
|
|
|
const int n = marks[i].count() - 1;
|
|
|
|
if (n > 0) {
|
2013-01-29 07:20:53 +00:00
|
|
|
xcb_rectangle_t *rects = new xcb_rectangle_t[n];
|
2012-04-19 18:25:27 +00:00
|
|
|
for (int j = 0; j < marks[i].count()-1; ++j) {
|
|
|
|
addRect(marks[i][j], marks[i][j+1], &rects[j], &c);
|
|
|
|
}
|
2014-04-16 13:58:06 +00:00
|
|
|
xcb_render_fill_rectangles(xcbConnection(), XCB_RENDER_PICT_OP_SRC, effects->xrenderBufferPicture(), c, n, rects);
|
2012-04-19 18:25:27 +00:00
|
|
|
delete [] rects;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const int n = drawing.count() - 1;
|
|
|
|
if (n > 0) {
|
2013-01-29 07:20:53 +00:00
|
|
|
xcb_rectangle_t *rects = new xcb_rectangle_t[n];
|
2012-04-19 18:25:27 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
addRect(drawing[i], drawing[i+1], &rects[i], &c);
|
2014-04-16 13:58:06 +00:00
|
|
|
xcb_render_fill_rectangles(xcbConnection(), XCB_RENDER_PICT_OP_SRC, effects->xrenderBufferPicture(), c, n, rects);
|
2012-04-19 18:25:27 +00:00
|
|
|
delete [] rects;
|
|
|
|
}
|
2010-12-12 13:51:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-06-24 06:08:55 +00:00
|
|
|
if (effects->compositingType() == QPainterCompositing) {
|
|
|
|
QPainter *painter = effects->scenePainter();
|
|
|
|
painter->save();
|
|
|
|
QPen pen(color);
|
|
|
|
pen.setWidth(width);
|
|
|
|
painter->setPen(pen);
|
|
|
|
foreach (const Mark &mark, marks) {
|
|
|
|
drawMark(painter, mark);
|
|
|
|
}
|
|
|
|
drawMark(painter, drawing);
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseMarkEffect::drawMark(QPainter *painter, const Mark &mark)
|
|
|
|
{
|
|
|
|
if (mark.count() <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < mark.count() - 1; ++i) {
|
|
|
|
painter->drawLine(mark[i], mark[i+1]);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-03-12 13:37:30 +00:00
|
|
|
void MouseMarkEffect::slotMouseChanged(const QPoint& pos, const QPoint&,
|
2011-01-30 14:34:42 +00:00
|
|
|
Qt::MouseButtons, Qt::MouseButtons,
|
|
|
|
Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers)
|
|
|
|
{
|
|
|
|
if (modifiers == (Qt::META | Qt::SHIFT | Qt::CTRL)) { // start/finish arrow
|
|
|
|
if (arrow_start != NULL_POINT) {
|
|
|
|
marks.append(createArrow(arrow_start, pos));
|
2008-01-08 16:21:04 +00:00
|
|
|
arrow_start = NULL_POINT;
|
|
|
|
effects->addRepaintFull();
|
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else
|
2008-01-08 16:21:04 +00:00
|
|
|
arrow_start = pos;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
if (arrow_start != NULL_POINT)
|
2008-01-08 16:21:04 +00:00
|
|
|
return;
|
2008-01-08 16:23:56 +00:00
|
|
|
// TODO the shortcuts now trigger this right before they're activated
|
2011-01-30 14:34:42 +00:00
|
|
|
if (modifiers == (Qt::META | Qt::SHIFT)) { // activated
|
|
|
|
if (drawing.isEmpty())
|
|
|
|
drawing.append(pos);
|
|
|
|
if (drawing.last() == pos)
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
|
|
|
QPoint pos2 = drawing.last();
|
2011-01-30 14:34:42 +00:00
|
|
|
drawing.append(pos);
|
|
|
|
QRect repaint = QRect(qMin(pos.x(), pos2.x()), qMin(pos.y(), pos2.y()),
|
|
|
|
qMax(pos.x(), pos2.x()), qMax(pos.y(), pos2.y()));
|
|
|
|
repaint.adjust(-width, -width, width, width);
|
|
|
|
effects->addRepaint(repaint);
|
|
|
|
} else if (!drawing.isEmpty()) {
|
|
|
|
marks.append(drawing);
|
2007-04-29 17:35:43 +00:00
|
|
|
drawing.clear();
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void MouseMarkEffect::clear()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
drawing.clear();
|
|
|
|
marks.clear();
|
|
|
|
effects->addRepaintFull();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2008-01-08 16:23:56 +00:00
|
|
|
void MouseMarkEffect::clearLast()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (arrow_start != NULL_POINT) {
|
2008-01-08 16:23:56 +00:00
|
|
|
arrow_start = NULL_POINT;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else if (!drawing.isEmpty()) {
|
2008-01-08 16:23:56 +00:00
|
|
|
drawing.clear();
|
|
|
|
effects->addRepaintFull();
|
2011-01-30 14:34:42 +00:00
|
|
|
} else if (!marks.isEmpty()) {
|
2008-01-08 16:23:56 +00:00
|
|
|
marks.pop_back();
|
|
|
|
effects->addRepaintFull();
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-01-08 16:23:56 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
MouseMarkEffect::Mark MouseMarkEffect::createArrow(QPoint arrow_start, QPoint arrow_end)
|
|
|
|
{
|
2008-01-08 16:21:04 +00:00
|
|
|
Mark ret;
|
2011-01-30 14:34:42 +00:00
|
|
|
double angle = atan2((double)(arrow_end.y() - arrow_start.y()), (double)(arrow_end.x() - arrow_start.x()));
|
|
|
|
ret += arrow_start + QPoint(50 * cos(angle + M_PI / 6),
|
|
|
|
50 * sin(angle + M_PI / 6)); // right one
|
2008-01-08 16:21:04 +00:00
|
|
|
ret += arrow_start;
|
|
|
|
ret += arrow_end;
|
|
|
|
ret += arrow_start; // it's connected lines, so go back with the middle one
|
2011-01-30 14:34:42 +00:00
|
|
|
ret += arrow_start + QPoint(50 * cos(angle - M_PI / 6),
|
|
|
|
50 * sin(angle - M_PI / 6)); // left one
|
2008-01-08 16:21:04 +00:00
|
|
|
return ret;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 12:07:59 +00:00
|
|
|
void MouseMarkEffect::screenLockingChanged(bool locked)
|
|
|
|
{
|
|
|
|
if (!marks.isEmpty() || !drawing.isEmpty()) {
|
|
|
|
effects->addRepaintFull();
|
|
|
|
}
|
|
|
|
// disable mouse polling while screen is locked.
|
|
|
|
if (locked) {
|
|
|
|
effects->stopMousePolling();
|
|
|
|
} else {
|
|
|
|
effects->startMousePolling();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-27 09:21:31 +00:00
|
|
|
bool MouseMarkEffect::isActive() const
|
|
|
|
{
|
2013-01-30 12:07:59 +00:00
|
|
|
return (!marks.isEmpty() || !drawing.isEmpty()) && !effects->isScreenLocked();
|
2011-08-27 09:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
} // namespace
|
|
|
|
|