File indexing completed on 2024-05-12 17:07:11

0001 /*
0002     This file is part of the KDE Control Center Module for Joysticks
0003 
0004     SPDX-FileCopyrightText: 2003 Martin Koller <kollix@aon.at>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <QString>
0011 
0012 #include <sys/types.h>
0013 
0014 #ifdef Q_OS_LINUX
0015 #undef __STRICT_ANSI__
0016 #include <linux/joystick.h>
0017 #define __STRICT_ANSI__
0018 #endif
0019 
0020 #ifdef Q_OS_FREEBSD
0021 #include <sys/joystick.h>
0022 #endif
0023 
0024 // helper class which holds all current values, file descriptor, etc. for
0025 // one device
0026 class JoyDevice
0027 {
0028 public:
0029     enum ErrorCode {
0030         SUCCESS,
0031         OPEN_FAILED,
0032         NO_JOYSTICK,
0033         WRONG_VERSION,
0034         ERR_GET_VERSION,
0035         ERR_GET_BUTTONS,
0036         ERR_GET_AXES,
0037         ERR_GET_CORR,
0038         ERR_RESTORE_CORR,
0039         ERR_INIT_CAL,
0040         ERR_APPLY_CAL,
0041     };
0042 
0043     enum EventType {
0044         BUTTON,
0045         AXIS,
0046     };
0047 
0048     // devicefile to use, e.g. "/dev/js0"
0049     JoyDevice(const QString &devicefile);
0050     ~JoyDevice();
0051 
0052     // returns one of the error-codes from above
0053     ErrorCode open();
0054 
0055     // return descriptive error text for given error code
0056     QString errText(ErrorCode code) const;
0057 
0058     int fd() const
0059     {
0060         return joyFd;
0061     }
0062     void close();
0063     ErrorCode restoreCorr();
0064 
0065     // return devicefilename from constructor
0066     const QString &device() const
0067     {
0068         return devName;
0069     }
0070 
0071     // descriptive text for this device read from the driver
0072     QString text() const
0073     {
0074         return descr;
0075     }
0076 
0077     int numButtons() const
0078     {
0079         return buttons;
0080     }
0081     int numAxes() const
0082     {
0083         return axes;
0084     }
0085     int axisMin(int axis) const;
0086     int axisMax(int axis) const;
0087 
0088     // read next event from device; returns true if there was an event during the short timeout
0089     bool getEvent(JoyDevice::EventType &type, int &number, int &value, bool wait);
0090 
0091     // methods for calibration
0092     ErrorCode initCalibration(); // must be called first
0093     void calcPrecision();
0094 
0095     void resetMinMax(int axis, int value = 0);
0096 
0097     // calculate correction values
0098     // min[2], center[2], max[2], index 0 == minimum, index 1 == maximum
0099     void calcCorrection(int axis, int *min, int *center, int *max);
0100     ErrorCode applyCalibration();
0101 
0102 private:
0103     QString devName; // device filename
0104     QString descr; // descriptive text
0105     int joyFd;
0106 
0107     int buttons;
0108     int axes;
0109     int *amin; // axes min values
0110     int *amax; // axes max values
0111 
0112     struct js_corr *corr; // calibration values during the calib. steps
0113     struct js_corr *origCorr; // original calibration correction values
0114 };