This commit is contained in:
mrbesen 2024-02-04 19:51:37 +01:00
commit 05f72bba15
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
10 changed files with 991 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.qmake.stash
CursorRotate
*.pro.user
Makefile
.vscode/settings.json
build/

41
CursorRotate.pro Normal file
View File

@ -0,0 +1,41 @@
QT += core gui svg
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
CONFIG += debug
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
MOC_DIR = build/generated/
UI_DIR = build/ui/
RCC_DIR = build/rcc/
OBJECTS_DIR = build/objects/
SOURCES += \
src/main.cpp \
src/mainwindow.cpp \
src/rotator.cpp
HEADERS += \
src/mainwindow.h \
src/rotator.h
FORMS += \
src/mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
LIBS += -lX11 -lXcursor

427
dmz3.svg Normal file
View File

@ -0,0 +1,427 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="32"
height="32"
id="svg2"
sodipodi:version="0.32"
inkscape:version="1.1.2 (0a00cf5339, 2022-02-04)"
version="1.0"
sodipodi:docname="dmz3.svg"
inkscape:output_extension="org.inkscape.output.svg.inkscape"
inkscape:export-filename="/home/jimmac/gfx/ximian/art/cursors/dmz/dmz.png"
inkscape:export-xdpi="90"
inkscape:export-ydpi="90"
sodipodi:modified="TRUE"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<defs
id="defs4">
<linearGradient
id="linearGradient1164">
<stop
style="stop-color:#ffffff;stop-opacity:1;"
offset="0"
id="stop1160" />
<stop
style="stop-color:#d9d9d9;stop-opacity:1;"
offset="1"
id="stop1162" />
</linearGradient>
<inkscape:perspective
sodipodi:type="inkscape:persp3d"
inkscape:vp_x="0 : 200 : 1"
inkscape:vp_y="0 : 1000 : 0"
inkscape:vp_z="400 : 200 : 1"
inkscape:persp3d-origin="200 : 133.33333 : 1"
id="perspective498" />
<linearGradient
id="linearGradient5724">
<stop
id="stop5726"
offset="0"
style="stop-color:#ffffff;stop-opacity:1;" />
<stop
style="stop-color:#ffffff;stop-opacity:1;"
offset="0.5"
id="stop5728" />
<stop
id="stop5730"
offset="1"
style="stop-color:#e3e3e3;stop-opacity:1;" />
</linearGradient>
<linearGradient
id="linearGradient5745">
<stop
style="stop-color:#ffffff;stop-opacity:1;"
offset="0"
id="stop5747" />
<stop
id="stop5753"
offset="0.5"
style="stop-color:#ffffff;stop-opacity:1;" />
<stop
style="stop-color:#d6d6d6;stop-opacity:1;"
offset="1"
id="stop5749" />
</linearGradient>
<linearGradient
id="linearGradient5965">
<stop
id="stop5967"
offset="0"
style="stop-color:#ffffff;stop-opacity:1;" />
<stop
id="stop5969"
offset="1"
style="stop-color:#b5b5b5;stop-opacity:1;" />
</linearGradient>
<linearGradient
id="linearGradient5716">
<stop
style="stop-color:#ffffff;stop-opacity:1;"
offset="0"
id="stop5718" />
<stop
style="stop-color:#d9d9d9;stop-opacity:1;"
offset="1"
id="stop5720" />
</linearGradient>
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient5839"
gradientUnits="userSpaceOnUse"
x1="112.12776"
y1="14.445171"
x2="112.12776"
y2="17.728685" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient5841"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(1,0,0,0.7178393,70.927237,5.0429461)"
x1="112.42966"
y1="15.217949"
x2="112.42966"
y2="21.001818" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient5853"
gradientUnits="userSpaceOnUse"
x1="30.415775"
y1="11.153909"
x2="26.104893"
y2="13.244899" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient5886"
gradientUnits="userSpaceOnUse"
x1="112.12776"
y1="14.445171"
x2="112.12776"
y2="17.632534" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient5918"
x1="238.16759"
y1="18.752937"
x2="239.99883"
y2="19.752937"
gradientUnits="userSpaceOnUse" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5965"
id="linearGradient5963"
gradientUnits="userSpaceOnUse"
x1="236.91759"
y1="18.190437"
x2="239.99883"
y2="19.752937" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient4708"
gradientUnits="userSpaceOnUse"
x1="30.415775"
y1="11.153909"
x2="26.104893"
y2="13.244899"
gradientTransform="matrix(1,0,0,-1,265.49301,591.02016)" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient5735"
gradientUnits="userSpaceOnUse"
x1="30.415775"
y1="11.153909"
x2="26.104893"
y2="13.244899" />
<radialGradient
inkscape:collect="always"
xlink:href="#linearGradient5724"
id="radialGradient5789"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(1.1119376,0,0,1.1119376,-34.134232,-2.1149802)"
cx="304.93979"
cy="18.894276"
fx="304.93979"
fy="18.894276"
r="10.045941" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6174"
gradientUnits="userSpaceOnUse"
x1="30.415775"
y1="11.153909"
x2="26.104893"
y2="13.244899" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6208"
gradientUnits="userSpaceOnUse"
x1="30.415775"
y1="11.153909"
x2="26.104893"
y2="13.244899" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6261"
gradientUnits="userSpaceOnUse"
x1="30.415775"
y1="11.153909"
x2="26.104893"
y2="13.244899" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6304"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(1,0,0,0.7178393,-14.056043,14.042946)"
x1="112.42966"
y1="15.217949"
x2="112.42966"
y2="21.001818" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient4908"
gradientUnits="userSpaceOnUse"
x1="30.415775"
y1="11.153909"
x2="26.104893"
y2="13.244899" />
<radialGradient
inkscape:collect="always"
xlink:href="#linearGradient5724"
id="radialGradient4930"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(1.2443906,0,0,1.2443906,-282.30215,101.25286)"
cx="304.93979"
cy="18.894276"
fx="304.93979"
fy="18.894276"
r="10.045941" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6027"
gradientUnits="userSpaceOnUse"
x1="133"
y1="127.75"
x2="133"
y2="135"
gradientTransform="translate(-2,1)" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6029"
gradientUnits="userSpaceOnUse"
x1="133.00002"
y1="128.25"
x2="133.00002"
y2="134.99998" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6055"
gradientUnits="userSpaceOnUse"
x1="133"
y1="127.75"
x2="133"
y2="135" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6057"
gradientUnits="userSpaceOnUse"
x1="133"
y1="127.75"
x2="133"
y2="135" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6059"
gradientUnits="userSpaceOnUse"
x1="133"
y1="127.75"
x2="133"
y2="135" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6061"
gradientUnits="userSpaceOnUse"
x1="133"
y1="127.75"
x2="133"
y2="135" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6124"
gradientUnits="userSpaceOnUse"
x1="112.12776"
y1="14.445171"
x2="112.12776"
y2="17.728685" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6141"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(0.667094,0,0,0.4717699,158.43011,116.8852)"
x1="112.42966"
y1="15.217949"
x2="112.42966"
y2="21.001818" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6227"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(0.667094,0,0,0.4717699,193.39491,116.8852)"
x1="112.42966"
y1="15.217949"
x2="112.42966"
y2="21.001818" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6251"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(0.667094,0,0,0.4717699,228.39491,116.8852)"
x1="112.42966"
y1="15.217949"
x2="112.42966"
y2="21.001818" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6276"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(0.667094,0,0,0.4717699,303.45741,116.8852)"
x1="112.42966"
y1="15.217949"
x2="112.42966"
y2="21.001818" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient6210"
gradientUnits="userSpaceOnUse"
gradientTransform="translate(174.03904,37.032934)"
x1="133"
y1="125.10307"
x2="133"
y2="131.0024" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient5716"
id="linearGradient5097"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(1,0,0,0.7178393,-14.056043,14.042946)"
x1="112.42966"
y1="15.217949"
x2="112.42966"
y2="21.001818" />
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#afafaf"
borderopacity="1"
gridtolerance="10000"
guidetolerance="10"
objecttolerance="10"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="28.963094"
inkscape:cx="22.407827"
inkscape:cy="16.331128"
inkscape:document-units="px"
inkscape:current-layer="layer1"
width="400px"
height="400px"
inkscape:showpageshadow="false"
borderlayer="true"
showgrid="false"
inkscape:window-width="2528"
inkscape:window-height="1384"
inkscape:window-x="1952"
inkscape:window-y="28"
inkscape:window-maximized="1"
inkscape:snap-nodes="false"
inkscape:snap-bbox="true"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:pagecheckerboard="1">
<inkscape:grid
type="xygrid"
id="grid11291"
originx="-26.104893"
originy="-379.50912" />
</sodipodi:namedview>
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
style="display:inline"
transform="translate(-26.104893,11.50912)">
<path
sodipodi:nodetypes="cccccccc"
id="path5565"
d="m 42.566583,0.6865965 -6.250128,15.1706965 4.850633,-1.839953 0.215285,4.817827 c 0.0086,1.281418 2.861238,1.496644 2.78258,-0.246162 l -0.128846,-4.594978 4.294841,1.504771 z"
style="color:#000000;display:block;overflow:visible;visibility:visible;fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;enable-background:accumulate"
inkscape:connector-curvature="0" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 13 KiB

2
downloadDmz.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
curl -so dmz.svg "https://salsa.debian.org/gnome-team/dmz-cursor-theme/-/raw/debian/master/DMZ-White/dmz.svg"

22
src/main.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include "rotator.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Rotator r;
QObject::connect(&r, &Rotator::angleUpdated, &w, &MainWindow::angleUpdated);
QObject::connect(&r, &Rotator::newImage, &w, &MainWindow::newImage);
QObject::connect(&r, &Rotator::force, &w, &MainWindow::setForce);
QObject::connect(&w, &MainWindow::reset, &r, &Rotator::reset);
w.show();
return a.exec();
}

19
src/mainwindow.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
QObject::connect(this, &MainWindow::angleUpdated, this->ui->dial, &QDial::setValue);
QObject::connect(this, &MainWindow::newImage, this->ui->label, &QLabel::setPixmap);
QObject::connect(this->ui->resetButton, &QPushButton::clicked, this, &MainWindow::reset);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::setForce(QPointF force) {
this->ui->forceX->setValue(force.x());
this->ui->forceY->setValue(-force.y());
}

31
src/mainwindow.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include <QPointF>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
signals:
void angleUpdated(double newAngle);
void newImage(QPixmap map);
void reset();
public slots:
void setForce(QPointF force);
private:
Ui::MainWindow* ui;
};
#endif // MAINWINDOW_H

142
src/mainwindow.ui Normal file
View File

@ -0,0 +1,142 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>234</width>
<height>439</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QDial" name="dial">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>150</x>
<y>10</y>
<width>50</width>
<height>64</height>
</rect>
</property>
<property name="maximum">
<number>360</number>
</property>
<property name="singleStep">
<number>1</number>
</property>
<property name="pageStep">
<number>1</number>
</property>
<property name="wrapping">
<bool>true</bool>
</property>
<property name="notchTarget">
<double>0.000000000000000</double>
</property>
<property name="notchesVisible">
<bool>false</bool>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<height>100</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">border-color:red;</string>
</property>
</widget>
<widget class="QPushButton" name="resetButton">
<property name="geometry">
<rect>
<x>130</x>
<y>70</y>
<width>100</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>&amp;Reset</string>
</property>
</widget>
<widget class="QSlider" name="forceX">
<property name="geometry">
<rect>
<x>10</x>
<y>130</y>
<width>160</width>
<height>16</height>
</rect>
</property>
<property name="minimum">
<number>-50</number>
</property>
<property name="maximum">
<number>50</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="tickPosition">
<enum>QSlider::TicksAbove</enum>
</property>
</widget>
<widget class="QSlider" name="forceY">
<property name="geometry">
<rect>
<x>10</x>
<y>150</y>
<width>16</width>
<height>160</height>
</rect>
</property>
<property name="minimum">
<number>-50</number>
</property>
<property name="maximum">
<number>50</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="tickPosition">
<enum>QSlider::TicksAbove</enum>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>234</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

240
src/rotator.cpp Normal file
View File

@ -0,0 +1,240 @@
#include "rotator.h"
#include <QDebug>
#include <QDateTime>
#include <QSvgRenderer>
#include <QTimer>
#include <QtMath>
#include <QColor>
#include <QPainter>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xcursor/Xcursor.h>
const QPointF Rotator::Gravity(0, 50);
static QPointF rotate(QPointF point, double angle) {
const double c = qCos(qDegreesToRadians(angle));
const double s = qSin(qDegreesToRadians(angle));
return {point.x() * c - point.y() * s, point.x() * s + point.y() * c};
}
static double lengthOfPoint(QPointF point) {
return std::sqrt((point.x() * point.x()) + (point.y() * point.y()));
}
static double clampDegrees(double deg) {
while(deg < 0) {
deg += 360.0;
}
while(deg > 360) {
deg -= 360.0;
}
return deg;
}
static double clampAngleDiff(double diff) {
diff = fmod(diff + 180.0, 360.0);
if (diff < 0) {
diff += 360.0;
}
return diff - 180.0;
}
static double pointToAngle(QPointF point) {
return clampDegrees(-qRadiansToDegrees(qAtan2(point.x(), point.y())));
}
Rotator::Rotator(QObject* parent) : QObject(parent), disp(XOpenDisplay(nullptr)), renderer(new QSvgRenderer(QString("dmz3.svg"), this)), loopTimer(new QTimer(nullptr)),
lastTimePoint(QDateTime::currentMSecsSinceEpoch()), lastPosition(-1, -1), angle(0), angleVelocity(0) {
QObject::connect(loopTimer, &QTimer::timeout, this, &Rotator::loop);
loopTimer->setInterval(std::chrono::milliseconds(10));
loopTimer->start();
// test
/*
for(int i = -360; i < 360; ++i) {
qDebug() << i << clampAngleDiff(i);
}
throw std::exception();
*/
}
Rotator::~Rotator() {
if(disp) {
XCloseDisplay(disp);
}
}
void Rotator::forAllWindows(std::function<void(Window)> func) {
forAllWindows(func, DefaultRootWindow(disp));
}
void Rotator::reset() {
angle = 0;
angleVelocity = 0;
}
void Rotator::loop() {
int x = 0, y = 0;
unsigned int mask = 0;
Window rootRet;
Window childRet;
int xChild = 0, yChild = 0;
if(XQueryPointer(disp, DefaultRootWindow(disp), &rootRet, &childRet, &x, &y, &xChild, &yChild, &mask)) {
recalculateCursor({(double) x, (double) y});
}
}
void Rotator::recalculateCursor(QPointF newPos) {
quint64 currentTimePoint = QDateTime::currentMSecsSinceEpoch();
if(lastPosition.x() == -1) {
lastPosition = newPos;
lastTimePoint = currentTimePoint;
return;
}
const double timeDiff = (currentTimePoint - lastTimePoint) / 1000.0;
QPointF mouseForceVector = lastPosition - newPos;
QPointF forceVector = ((mouseForceVector * mouseForceStrength) + (Gravity * gravityStrength)) * timeDiff;
emit this->force(forceVector);
// has position 0,0 (need soffset)
QPointF rotatedEndCap = rotate(QPointF(0, length), angle);
QPointF forcedCap = rotatedEndCap + forceVector;
// fix the distance to the origin (should be exactly length)
//double forcedCapLength = lengthOfPoint(forcedCap);
//forcedCap *= length / forcedCapLength;
//qDebug() << "normalized:" << forcedCap;
// calculate angle
double newAngle = clampDegrees(pointToAngle(forcedCap));
// qDebug() << "force:" << forceVector << "forcedCap:" << forcedCap << "angle:" << newAngle;
double angleDiff = clampAngleDiff(newAngle - angle);
if(qAbs(angleDiff) < 0.01 * timeDiff) {
lastPosition = newPos;
lastTimePoint = currentTimePoint;
return;
}
newAngle += angleVelocity * timeDiff;
angleVelocity += angleDiff / timeDiff;
qDebug() << "angleDiff:" << angleDiff << "angleVelocity:" << angleVelocity << "angle:" << newAngle;
angleVelocity *= 0.95;
if(qAbs(angleVelocity) < 0.001) {
angleVelocity = 0.0;
}
// calculate offset ( is this required? )
// QPointF realCap = forcedCap + newPos;
angle = clampDegrees(newAngle);
// just rotate
// angle += 300 * timeDiff;
emit this->angleUpdated(angle);
lastPosition = newPos;
lastTimePoint = currentTimePoint;
rerender();
}
void Rotator::rerender() {
int size = 100;
QPixmap px(size, size);
px.fill(QColorConstants::Transparent);
QPainter painter(&px);
QTransform transform;
qreal scaleF = 0.7;
qreal tX = 16 * (size/32.0);
qreal tY = 12 * (size/32.0);
qreal lateTranslate = size/2.0;
transform.translate(lateTranslate, lateTranslate).scale(scaleF, scaleF).rotate(angle).translate(-tX, -tY);
painter.setTransform(transform, false);
renderer->render(&painter);
painter.setPen({QColorConstants::Red});
painter.drawLine(tX-10, tY, tX+10, tY);
painter.drawLine(tX, tY-10, tX, tY+10);
painter.setTransform(QTransform(), false);
painter.drawRect(0, 0, size-1, size-1);
emit this->newImage(px);
// setCursor(px, {50, 50});
}
void Rotator::setCursor(const QPixmap& px, const QPointF& hotspot) {
// Get the raw XImage from the QPixmap
QImage image = px.toImage();
uint32_t imageSize = image.width() * image.depth() * image.height();
char* imageData = (char*) malloc(imageSize);
memcpy(imageData, image.bits(), imageSize);
XImage* xImage = XCreateImage(
disp,
DefaultVisual(disp, DefaultScreen(disp)),
DefaultDepth(disp, DefaultScreen(disp)),
ZPixmap, 0, imageData,
image.width(), image.height(),
image.depth(), 0
);
// Create the XcursorImages
XcursorImage* cursorImage = XcursorImageCreate(image.width(), image.height());
cursorImage->xhot = hotspot.x();
cursorImage->yhot = hotspot.y();
// Copy the image data to XcursorImage
cursorImage->pixels = (XcursorPixel*) xImage->data;
cursorImage->delay = 0;
// Create the cursor
Cursor cursor = XcursorImageLoadCursor(disp, cursorImage);
// Set the cursor
XDefineCursor(disp, DefaultRootWindow(disp), cursor);
// Clean up resources
// XFreeCursor(disp, cursor);
// XDestroyImage(xImage);
}
void Rotator::forAllWindows(std::function<void(Window)> func, Window root)
{
func(root);
Window* children = nullptr;
Window dummy;
unsigned int nchildren;
if (!XQueryTree(disp, root, &dummy, &dummy, &children, &nchildren)) {
// no children
return;
}
for (unsigned int i = 0; i < nchildren; i++) {
forAllWindows(func, children[i]);
}
if (children) {
XFree((char*) children);
}
}

61
src/rotator.h Normal file
View File

@ -0,0 +1,61 @@
#pragma once
#include <functional>
#include <QPointF>
#include <QObject>
#include <QPixmap>
class QSvgRenderer;
class QTimer;
struct _XDisplay;
using Display = _XDisplay;
using Window = unsigned long;
class Rotator : public QObject {
Q_OBJECT
public:
explicit Rotator(QObject* parent = nullptr);
virtual ~Rotator();
void forAllWindows(std::function<void(Window)> func);
signals:
void angleUpdated(double newAngle);
void newImage(QPixmap map);
void force(QPointF force);
public slots:
void reset();
private slots:
void loop();
private:
void recalculateCursor(QPointF newPos);
void rerender();
void setCursor(const QPixmap& px, const QPointF& hostspot);
void forAllWindows(std::function<void(Window)> func, Window root);
Display* disp;
QSvgRenderer* renderer;
QTimer* loopTimer;
quint64 lastTimePoint;
QPointF lastPosition;
double angle; // deg
double angleVelocity; // deg / s
// TODO: make them changeable
double gravityStrength = 1.0;
double mouseForceStrength = 1.0;
double impulseStrength = 1.0;
static constexpr double length = 10.0;
static const QPointF Gravity;
};