| 1 | // SPDX-FileCopyrightText: 2009 Ingo Ruhnke <[email protected]> |
| 2 | // SPDX-FileCopyrightText: 2025 Raphael Rosch <[email protected]> |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #ifndef HEADER_JSTEST_GTK_JOYSTICK_HPP |
| 6 | #define HEADER_JSTEST_GTK_JOYSTICK_HPP |
| 7 | |
| 8 | #include <sigc++/signal.h> |
| 9 | #include <sigc++/connection.h> |
| 10 | #include <glibmm/main.h> |
| 11 | #include <glibmm/ustring.h> |
| 12 | #include <linux/joystick.h> |
| 13 | #include <libudev.h> |
| 14 | #include "joystick_description.hpp" |
| 15 | #include "joystick_config_files.hpp" |
| 16 | |
| 17 | std::string get_js_dev_id_from_filename(const std::string& filename_); |
| 18 | |
| 19 | class Joystick |
| 20 | { |
| 21 | public: |
| 22 | struct CalibrationData { |
| 23 | bool calibrate; |
| 24 | bool invert; |
| 25 | int center_min; |
| 26 | int center_max; |
| 27 | int range_min; |
| 28 | int range_max; |
| 29 | }; |
| 30 | |
| 31 | private: |
| 32 | int fd = -1; |
| 33 | |
| 34 | std::string filename; |
| 35 | std::string orig_name; |
| 36 | Glib::ustring name; |
| 37 | std::string js_id; |
| 38 | std::string js_type; |
| 39 | std::string vendor_id; |
| 40 | std::string product_id; |
| 41 | std::string usb_id; |
| 42 | int axis_count = 0; |
| 43 | int button_count = 0; |
| 44 | |
| 45 | void connect_js(); |
| 46 | int get_new_joystick_fd(); |
| 47 | bool try_adopt_device(const std::string& path, const std::string& new_js_id); |
| 48 | std::pair<std::string, std::string> get_usb_id_pair_from_udev(const std::string& sysname); |
| 49 | |
| 50 | std::vector<int> axis_state; |
| 51 | std::vector<CalibrationData> orig_calibration_data; |
| 52 | |
| 53 | sigc::connection connection; |
| 54 | |
| 55 | public: |
| 56 | Joystick(const std::string& filename, const std::string& js_id); |
| 57 | ~Joystick(); |
| 58 | |
| 59 | JoystickConfig js_cfg; |
| 60 | int get_fd() const { return fd; } |
| 61 | |
| 62 | void update(); |
| 63 | bool on_in(Glib::IOCondition cond); |
| 64 | bool reconnected(); |
| 65 | |
| 66 | std::string get_filename() const { return filename; } |
| 67 | Glib::ustring get_name() const { return name; } |
| 68 | std::string get_js_id() const { return js_id; } |
| 69 | std::string get_js_type() const { return js_type; } |
| 70 | std::string get_vendor_id() const { return vendor_id; } |
| 71 | std::string get_product_id() const { return product_id; } |
| 72 | std::string get_usb_id() const { return usb_id; } |
| 73 | int get_axis_count() const { return axis_count; } |
| 74 | int get_button_count() const { return button_count; } |
| 75 | |
| 76 | std::string get_js_type_from_config(const JoystickConfig& js_cfg); |
| 77 | |
| 78 | sigc::signal<void, int, int> axis_move; |
| 79 | sigc::signal<void, int, bool> button_press; |
| 80 | |
| 81 | int get_axis_state(int id); |
| 82 | |
| 83 | static std::vector<JoystickDescription> get_joysticks(); |
| 84 | |
| 85 | std::vector<CalibrationData> get_calibration(); |
| 86 | void set_calibration(const std::vector<CalibrationData>& data); |
| 87 | void reset_calibration(); |
| 88 | |
| 89 | /** Clears all calibration data, note that this will mean raw USB |
| 90 | input values, not values scaled to -32767/32767 */ |
| 91 | void clear_calibration(); |
| 92 | |
| 93 | std::vector<int> get_button_mapping(); |
| 94 | std::vector<int> get_axis_mapping(); |
| 95 | |
| 96 | void set_button_mapping(const std::vector<int>& mapping); |
| 97 | void set_axis_mapping(const std::vector<int>& mapping); |
| 98 | |
| 99 | /** Corrects calibration data after remaping axes */ |
| 100 | void correct_calibration(const std::vector<int>& mapping_old, const std::vector<int>& mapping_new); |
| 101 | |
| 102 | /** Get the evdev that this joystick device is based on. This call |
| 103 | is just a guess, not guranteed to be the exact same device, but |
| 104 | for our uses that should be enough. */ |
| 105 | std::string get_evdev() const; |
| 106 | |
| 107 | private: |
| 108 | Joystick(const Joystick&); |
| 109 | Joystick& operator=(const Joystick&); |
| 110 | }; |
| 111 | |
| 112 | #endif |
| 113 | |
| 114 | /* EOF */ |