File indexing completed on 2024-12-22 05:17:17

0001 /*
0002  * This file is part of the KDE wacomtablet project. For copyright
0003  * information and license terms see the AUTHORS and COPYING files
0004  * in the top-level directory of this distribution.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "x11wacom.h"
0021 
0022 #include "logging.h"
0023 #include "x11input.h"
0024 #include "x11inputdevice.h"
0025 
0026 using namespace Wacom;
0027 
0028 const TabletArea X11Wacom::getMaximumTabletArea(const QString &deviceName)
0029 {
0030     TabletArea maximumAreaRect;
0031 
0032     if (deviceName.isEmpty()) {
0033         qCWarning(COMMON) << QString::fromLatin1("Internal Error: Missing device name parameter!");
0034         return maximumAreaRect;
0035     }
0036 
0037     // find the xinput device
0038     X11InputDevice x11Device;
0039 
0040     if (!X11Input::findDevice(deviceName, x11Device)) {
0041         qCWarning(COMMON) << QString::fromLatin1("Failed to lookup X11 input device '%1'!").arg(deviceName);
0042         return maximumAreaRect;
0043     }
0044 
0045     // get the current property value as backup
0046     static const QString areaProperty = X11Input::PROPERTY_WACOM_TABLET_AREA;
0047     QList<long> previousArea;
0048 
0049     if (!x11Device.getLongProperty(areaProperty, previousArea, 4)) {
0050         qCWarning(COMMON) << QString::fromLatin1("Failed to get tablet area property from X11 input device '%1'!").arg(deviceName);
0051         return maximumAreaRect;
0052     }
0053 
0054     // reset the area so it turns back to the maximum.. does not seem to be working
0055     QList<long> resetArea;
0056     resetArea.append(-1);
0057     resetArea.append(-1);
0058     resetArea.append(-1);
0059     resetArea.append(-1);
0060 
0061     if (!x11Device.setLongProperty(areaProperty, resetArea)) {
0062         qCWarning(COMMON) << QString::fromLatin1("Failed to reset tablet area property on X11 input device '%1'!").arg(deviceName);
0063         return maximumAreaRect;
0064     }
0065 
0066     // get the maximum area
0067     QList<long> maximumArea;
0068 
0069     if (x11Device.getLongProperty(areaProperty, maximumArea, 4) && maximumArea.size() == 4) {
0070         maximumAreaRect.setX(maximumArea.at(0));
0071         maximumAreaRect.setY(maximumArea.at(1));
0072         maximumAreaRect.setWidth(maximumArea.at(2) - maximumArea.at(0));
0073         maximumAreaRect.setHeight(maximumArea.at(3) - maximumArea.at(1));
0074     }
0075 
0076     // reset the area back to the previous value
0077     if (!x11Device.setLongProperty(areaProperty, previousArea)) {
0078         qCWarning(COMMON) << QString::fromLatin1("Failed to set tablet area property on X11 input device '%1'!").arg(deviceName);
0079     }
0080 
0081     qCDebug(COMMON) << "getMaximumTabletArea result" << maximumAreaRect.toString();
0082 
0083     return maximumAreaRect;
0084 }
0085 
0086 bool X11Wacom::isScrollDirectionInverted(const QString &deviceName)
0087 {
0088     X11InputDevice device;
0089 
0090     if (!X11Input::findDevice(deviceName, device)) {
0091         return false;
0092     }
0093 
0094     const auto buttonMap = device.getDeviceButtonMapping();
0095 
0096     if (buttonMap.count() == 0 || buttonMap.count() < 5) {
0097         return false;
0098     }
0099 
0100     return (buttonMap.at(3) == 5 && buttonMap.at(4) == 4);
0101 }
0102 
0103 bool X11Wacom::setCoordinateTransformationMatrix(const QString &deviceName, qreal offsetX, qreal offsetY, qreal width, qreal height)
0104 {
0105     X11InputDevice device;
0106 
0107     if (!X11Input::findDevice(deviceName, device)) {
0108         return false;
0109     }
0110 
0111     /*
0112      *  | width  0       offsetX |
0113      *  | 0      height  offsetY |
0114      *  | 0      0          1    |
0115      */
0116     QList<float> matrix;
0117     matrix.append(width);
0118     matrix.append(0);
0119     matrix.append(offsetX);
0120     matrix.append(0);
0121     matrix.append(height);
0122     matrix.append(offsetY);
0123     matrix.append(0);
0124     matrix.append(0);
0125     matrix.append(1);
0126 
0127     return device.setFloatProperty(X11Input::PROPERTY_TRANSFORM_MATRIX, matrix);
0128 }
0129 
0130 bool X11Wacom::setScrollDirection(const QString &deviceName, bool inverted)
0131 {
0132     X11InputDevice device;
0133 
0134     if (!X11Input::findDevice(deviceName, device)) {
0135         return false;
0136     }
0137 
0138     auto buttonMap = device.getDeviceButtonMapping();
0139 
0140     if (buttonMap.count() == 0 || buttonMap.count() < 5) {
0141         return false;
0142     }
0143 
0144     if (inverted) {
0145         buttonMap[3] = 5;
0146         buttonMap[4] = 4;
0147     } else {
0148         buttonMap[3] = 4;
0149         buttonMap[4] = 5;
0150     }
0151 
0152     return device.setDeviceButtonMapping(buttonMap);
0153 }