KWin
Loading...
Searching...
No Matches
mousemark.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2006 Lubos Lunak <l.lunak@kde.org>
6 SPDX-FileCopyrightText: 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
7 SPDX-FileCopyrightText: 2023 Andrew Shark <ashark at linuxcomp.ru>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11
12#include "mousemark.h"
13#include "mousemarklogging.h"
14
15// KConfigSkeleton
16#include "mousemarkconfig.h"
17
18#include "core/rendertarget.h"
19#include "core/renderviewport.h"
21#include "opengl/glplatform.h"
22#include <KGlobalAccel>
23#include <KLocalizedString>
24#include <QAction>
25
26#include <QPainter>
27
28#include <cmath>
29
30namespace KWin
31{
32
33static consteval QPoint nullPoint()
34{
35 return QPoint(-1, -1);
36}
37
39{
40 MouseMarkConfig::instance(effects->config());
41 QAction *a = new QAction(this);
42 a->setObjectName(QStringLiteral("ClearMouseMarks"));
43 a->setText(i18n("Clear All Mouse Marks"));
44 KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << (Qt::SHIFT | Qt::META | Qt::Key_F11));
45 KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << (Qt::SHIFT | Qt::META | Qt::Key_F11));
46 connect(a, &QAction::triggered, this, &MouseMarkEffect::clear);
47 a = new QAction(this);
48 a->setObjectName(QStringLiteral("ClearLastMouseMark"));
49 a->setText(i18n("Clear Last Mouse Mark"));
50 KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << (Qt::SHIFT | Qt::META | Qt::Key_F12));
51 KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << (Qt::SHIFT | Qt::META | Qt::Key_F12));
52 connect(a, &QAction::triggered, this, &MouseMarkEffect::clearLast);
53
54 connect(effects, &EffectsHandler::mouseChanged, this, &MouseMarkEffect::slotMouseChanged);
55 connect(effects, &EffectsHandler::screenLockingChanged, this, &MouseMarkEffect::screenLockingChanged);
57 arrow_tail = nullPoint();
58 effects->startMousePolling(); // We require it to detect activation as well
59}
60
65
66static int width_2 = 1;
67void MouseMarkEffect::reconfigure(ReconfigureFlags)
68{
69 m_freedraw_modifiers = Qt::KeyboardModifiers();
70 m_arrowdraw_modifiers = Qt::KeyboardModifiers();
71 MouseMarkConfig::self()->read();
72 width = MouseMarkConfig::lineWidth();
73 width_2 = width / 2;
74 color = MouseMarkConfig::color();
75 color.setAlphaF(1.0);
76 if (MouseMarkConfig::freedrawshift()) {
77 m_freedraw_modifiers |= Qt::ShiftModifier;
78 }
79 if (MouseMarkConfig::freedrawalt()) {
80 m_freedraw_modifiers |= Qt::AltModifier;
81 }
82 if (MouseMarkConfig::freedrawcontrol()) {
83 m_freedraw_modifiers |= Qt::ControlModifier;
84 }
85 if (MouseMarkConfig::freedrawmeta()) {
86 m_freedraw_modifiers |= Qt::MetaModifier;
87 }
88
89 if (MouseMarkConfig::arrowdrawshift()) {
90 m_arrowdraw_modifiers |= Qt::ShiftModifier;
91 }
92 if (MouseMarkConfig::arrowdrawalt()) {
93 m_arrowdraw_modifiers |= Qt::AltModifier;
94 }
95 if (MouseMarkConfig::arrowdrawcontrol()) {
96 m_arrowdraw_modifiers |= Qt::ControlModifier;
97 }
98 if (MouseMarkConfig::arrowdrawmeta()) {
99 m_arrowdraw_modifiers |= Qt::MetaModifier;
100 }
101}
102
103void MouseMarkEffect::paintScreen(const RenderTarget &renderTarget, const RenderViewport &viewport, int mask, const QRegion &region, Output *screen)
104{
105 effects->paintScreen(renderTarget, viewport, mask, region, screen); // paint normal screen
106 if (marks.isEmpty() && drawing.isEmpty()) {
107 return;
108 }
110 if (!GLPlatform::instance()->isGLES()) {
111 glEnable(GL_BLEND);
112 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
113
114 glEnable(GL_LINE_SMOOTH);
115 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
116 }
117 glLineWidth(width);
119 vbo->reset();
120 const auto scale = viewport.scale();
123 binder.shader()->setColorspaceUniformsFromSRGB(renderTarget.colorDescription());
125 QList<QVector2D> verts;
126 for (const Mark &mark : std::as_const(marks)) {
127 verts.clear();
128 verts.reserve(mark.size() * 2);
129 for (const QPointF &p : std::as_const(mark)) {
130 verts.push_back(QVector2D(p.x() * scale, p.y() * scale));
131 }
132 vbo->setVertices(verts);
133 vbo->render(GL_LINE_STRIP);
134 }
135 if (!drawing.isEmpty()) {
136 verts.clear();
137 verts.reserve(drawing.size() * 2);
138 for (const QPointF &p : std::as_const(drawing)) {
139 verts.push_back(QVector2D(p.x() * scale, p.y() * scale));
140 }
141 vbo->setVertices(verts);
142 vbo->render(GL_LINE_STRIP);
143 }
144 glLineWidth(1.0);
145 if (!GLPlatform::instance()->isGLES()) {
146 glDisable(GL_LINE_SMOOTH);
147 glDisable(GL_BLEND);
148 }
150 QPainter *painter = effects->scenePainter();
151 painter->save();
152 QPen pen(color);
153 pen.setWidth(width);
154 painter->setPen(pen);
155 for (const Mark &mark : std::as_const(marks)) {
156 drawMark(painter, mark);
157 }
158 drawMark(painter, drawing);
159 painter->restore();
160 }
161}
162
163void MouseMarkEffect::drawMark(QPainter *painter, const Mark &mark)
164{
165 if (mark.count() <= 1) {
166 return;
167 }
168 for (int i = 0; i < mark.count() - 1; ++i) {
169 painter->drawLine(mark[i], mark[i + 1]);
170 }
171}
172
173void MouseMarkEffect::slotMouseChanged(const QPointF &pos, const QPointF &,
174 Qt::MouseButtons, Qt::MouseButtons,
175 Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers)
176{
177 qCDebug(KWIN_MOUSEMARK) << "MouseChanged" << pos;
178 if (modifiers == m_arrowdraw_modifiers && m_arrowdraw_modifiers != Qt::NoModifier) { // start/finish arrow
179 if (arrow_tail != nullPoint()) {
180 if (drawing.length() != 0) {
181 clearLast(); // clear our arrow with tail at previous position
182 }
183 drawing = createArrow(pos, arrow_tail);
185 return;
186 } else {
187 if (drawing.length() > 0) { // has unfinished freedraw right before arrowdraw
188 marks.append(drawing);
189 drawing.clear();
190 }
191 arrow_tail = pos;
192 }
193 } else if (modifiers == m_freedraw_modifiers && m_freedraw_modifiers != Qt::NoModifier ) { // activated
194 if (arrow_tail != nullPoint()) {
195 arrow_tail = nullPoint(); // for the case when user started freedraw right after arrowdraw
196 marks.append(drawing);
197 drawing.clear();
198 }
199 if (drawing.isEmpty()) {
200 drawing.append(pos);
201 }
202 if (drawing.last() == pos) {
203 return;
204 }
205 QPointF pos2 = drawing.last();
206 drawing.append(pos);
207 QRect repaint = QRect(std::min(pos.x(), pos2.x()), std::min(pos.y(), pos2.y()),
208 std::max(pos.x(), pos2.x()), std::max(pos.y(), pos2.y()));
209 repaint.adjust(-width, -width, width, width);
210 effects->addRepaint(repaint);
211 } else { // neither freedraw, nor arrowdraw modifiers pressed, but mouse moved
212 if (drawing.length() > 1) {
213 marks.append(drawing);
214 drawing.clear();
215 }
216 arrow_tail = nullPoint();
217 }
218}
219
220void MouseMarkEffect::clear()
221{
222 arrow_tail = nullPoint();
223 drawing.clear();
224 marks.clear();
226}
227
228void MouseMarkEffect::clearLast()
229{
230 if (drawing.length() > 1) { // just pressing a modifiers already create a drawing with 1 point (so not visible), treat it as non-existent
231 drawing.clear();
233 } else if (!marks.isEmpty()) {
234 marks.pop_back();
236 }
237}
238
239MouseMarkEffect::Mark MouseMarkEffect::createArrow(QPointF arrow_head, QPointF arrow_tail)
240{
241 Mark ret;
242 double angle = atan2((double)(arrow_tail.y() - arrow_head.y()), (double)(arrow_tail.x() - arrow_head.x()));
243 // Arrow is made of connected lines. We make it's last point at tail, so freedraw can begin from the tail
244 ret += arrow_head;
245 ret += arrow_head + QPoint(50 * cos(angle + M_PI / 6),
246 50 * sin(angle + M_PI / 6)); // right one
247 ret += arrow_head;
248 ret += arrow_head + QPoint(50 * cos(angle - M_PI / 6),
249 50 * sin(angle - M_PI / 6)); // left one
250 ret += arrow_head;
251 ret += arrow_tail;
252 return ret;
253}
254
255void MouseMarkEffect::screenLockingChanged(bool locked)
256{
257 if (!marks.isEmpty() || !drawing.isEmpty()) {
259 }
260 // disable mouse polling while screen is locked.
261 if (locked) {
263 } else {
265 }
266}
267
269{
270 return (!marks.isEmpty() || !drawing.isEmpty()) && !effects->isScreenLocked();
271}
272
274{
275 return 10;
276}
277
278} // namespace
279
280#include "moc_mousemark.cpp"
void screenLockingChanged(bool locked)
void paintScreen(const RenderTarget &renderTarget, const RenderViewport &viewport, int mask, const QRegion &region, Output *screen)
Q_SCRIPTABLE void addRepaint(const QRectF &r)
CompositingType compositingType
bool isOpenGLCompositing() const
Whether the Compositor is OpenGL based (either GL 1 or 2).
void mouseChanged(const QPointF &pos, const QPointF &oldpos, Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers)
KSharedConfigPtr config() const
Q_SCRIPTABLE void addRepaintFull()
QPainter * scenePainter()
Provides access to the QPainter which is rendering to the back buffer.
static GLPlatform * instance()
Definition glplatform.h:394
bool setColorspaceUniformsFromSRGB(const ColorDescription &dst)
Definition glshader.cpp:457
bool setUniform(const char *name, float value)
Definition glshader.cpp:301
Vertex Buffer Object.
static GLVertexBuffer * streamingBuffer()
void render(GLenum primitiveMode)
void setVertices(const T &range)
void reconfigure(ReconfigureFlags) override
Definition mousemark.cpp:67
~MouseMarkEffect() override
Definition mousemark.cpp:61
void paintScreen(const RenderTarget &renderTarget, const RenderViewport &viewport, int mask, const QRegion &region, Output *screen) override
int requestedEffectChainPosition() const override
bool isActive() const override
Qt::KeyboardModifiers modifiers
Definition mousemark.h:25
const ColorDescription & colorDescription() const
QMatrix4x4 projectionMatrix() const
@ ReconfigureAll
Definition effect.h:601
@ QPainterCompositing
Definition globals.h:39
EffectsHandler * effects