libOpenWinControls
Multiplatform open-source library to interact with the dual-mode controller in GPD devices
Loading...
Searching...
No Matches
Controller.h
1/*
2 * This file is part of libOpenWinControls.
3 * Copyright (C) 2026 kylon
4 *
5 * libOpenWinControls is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libOpenWinControls is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#pragma once
19
20#include <functional>
21#include <source_location>
22#include <cstdint>
23
24#include "../extern/hidapi/hidapi/hidapi.h"
25#include "../include/export.h"
26#include "../include/Button.h"
27#include "../include/LedMode.h"
29
30namespace OWC {
35 class OWC_EXPORT Controller {
36 protected:
37 std::function<void(const std::wstring &)> logFn;
38 hid_device *gamepad = nullptr;
39 int features = 0;
40
41 [[nodiscard]] virtual int getVID() const = 0;
42 [[nodiscard]] virtual int getPID() const = 0;
43
44 void writeLog(const std::wstring &msg, std::source_location loc = std::source_location::current()) const;
45 [[nodiscard]] bool setButtonKey(uint16_t *btn, const std::string &key) const;
46 [[nodiscard]] bool setXinputKey(uint16_t *btn, const std::string &key) const;
47 void setBackButtonTime(uint16_t *field, int time) const;
48 void setAnalogDeadzone(int8_t *field, int value) const;
49
50 public:
51 virtual ~Controller();
52
57 [[nodiscard]] virtual int getControllerType() const = 0;
58
63 [[nodiscard]] virtual bool readConfig() = 0;
64
69 [[nodiscard]] virtual bool writeConfig() const = 0;
70
77 [[nodiscard]] virtual bool setButton(Button btn, const std::string &key) const = 0;
78
84 [[nodiscard]] virtual std::string getButton(Button btn) const = 0;
85
95 [[nodiscard]] virtual bool setBackButton(int num, int slot, const std::string &key) const = 0;
96
105 [[nodiscard]] virtual std::string getBackButton(int num, int slot) const = 0;
106
115 virtual void setBackButtonStartTime(int num, int slot, int timeMs) const = 0;
116
125 [[nodiscard]] virtual int getBackButtonStartTime(int num, int slot) const = 0;
126
131 virtual void setRumble(RumbleMode mode) const = 0;
132
137 [[nodiscard]] virtual RumbleMode getRumbleMode() const = 0;
138
143 virtual void setLedMode(LedMode mode) const = 0;
144
149 [[nodiscard]] virtual LedMode getLedMode() const = 0;
150
158 virtual void setLedColor(int r, int g, int b) const = 0;
159
165 [[nodiscard]] virtual std::tuple<int, int, int> getLedColor() const = 0;
166
172 virtual void setAnalogCenter(int center, bool left) const = 0;
173
179 [[nodiscard]] virtual int getAnalogCenter(bool left) const = 0;
180
186 virtual void setAnalogBoundary(int boundary, bool left) const = 0;
187
193 [[nodiscard]] virtual int getAnalogBoundary(bool left) const = 0;
194
199 void enableLogging(const std::function<void(const std::wstring &)> &logCB) { logFn = logCB; }
200
206 [[nodiscard]] bool init();
207
213 [[nodiscard]] bool hasFeature(int feature) const;
214 };
215}
Button
enum to refer to a physical button and its mode
Definition Button.h:28
LedMode
led mode
Definition LedMode.h:28
RumbleMode
vibration intensity
Definition RumbleMode.h:28
Controller abstract base class.
Definition Controller.h:35
virtual LedMode getLedMode() const =0
get current led mode
virtual void setAnalogBoundary(int boundary, bool left) const =0
set the analog stick boundary offset (circularity)
virtual std::string getButton(Button btn) const =0
get the current key of button btn
virtual bool readConfig()=0
read the whole configuration
virtual bool writeConfig() const =0
write the configuration
virtual std::string getBackButton(int num, int slot) const =0
get the current key in slot slot of back button num
virtual void setAnalogCenter(int center, bool left) const =0
set the analog stick offset from center (deadzone)
void enableLogging(const std::function< void(const std::wstring &)> &logCB)
Enable log messages.
Definition Controller.h:199
virtual void setBackButtonStartTime(int num, int slot, int timeMs) const =0
set start time for key in slot slot of back button num
virtual int getAnalogBoundary(bool left) const =0
get the current analog stick boundary offset (circularity)
virtual int getBackButtonStartTime(int num, int slot) const =0
get start time for key in slot slot of back button num
virtual bool setButton(Button btn, const std::string &key) const =0
set a new key for the button btn
virtual int getAnalogCenter(bool left) const =0
get the current analog stick offset from center (deadzone)
virtual bool setBackButton(int num, int slot, const std::string &key) const =0
set a new key in slot slot for back button num
virtual void setRumble(RumbleMode mode) const =0
set vibration intensity
virtual void setLedMode(LedMode mode) const =0
set led mode
virtual int getControllerType() const =0
int value of the controller class version
virtual RumbleMode getRumbleMode() const =0
get current vibration intesity
virtual void setLedColor(int r, int g, int b) const =0
set a custom color for the leds
virtual std::tuple< int, int, int > getLedColor() const =0
get current led custom color