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 uint8_t *sendBuf = nullptr;
40 uint8_t *respBuf = nullptr;
41 int features = 0;
42 int sendPacketLen;
43 int respPacketLen;
44
45 [[nodiscard]] virtual int getVID() const = 0;
46 [[nodiscard]] virtual int getPID() const = 0;
47
48 void prepareRespBuffer() const;
49 void writeLog(const std::wstring &msg, std::source_location loc = std::source_location::current()) const;
50 [[nodiscard]] bool sendReadRequest() const;
51 [[nodiscard]] bool sendWriteRequest() const;
52 [[nodiscard]] bool setButtonKey(uint16_t *btn, const std::string &key) const;
53 [[nodiscard]] bool setXinputKey(uint16_t *btn, const std::string &key) const;
54 void setBackButtonTime(uint16_t *field, int time) const;
55 void setAnalogDeadzone(int8_t *field, int value) const;
56
57 public:
58 Controller(int controllerFeatures, int sendBufSz, int respBufSz);
59 virtual ~Controller();
60
65 [[nodiscard]] virtual int getControllerType() const = 0;
66
71 [[nodiscard]] virtual bool readVersion() = 0;
72
77 [[nodiscard]] virtual bool readConfig() = 0;
78
83 [[nodiscard]] virtual bool writeConfig() const = 0;
84
90 [[nodiscard]] virtual bool resetConfig() const = 0;
91
98 [[nodiscard]] virtual bool setButton(Button btn, const std::string &key) const = 0;
99
105 [[nodiscard]] virtual std::string getButton(Button btn) const = 0;
106
116 [[nodiscard]] virtual bool setBackButton(int num, int slot, const std::string &key) const = 0;
117
126 [[nodiscard]] virtual std::string getBackButton(int num, int slot) const = 0;
127
136 virtual void setBackButtonStartTime(int num, int slot, int timeMs) const = 0;
137
146 [[nodiscard]] virtual int getBackButtonStartTime(int num, int slot) const = 0;
147
152 virtual void setRumble(RumbleMode mode) const = 0;
153
158 [[nodiscard]] virtual RumbleMode getRumbleMode() const = 0;
159
164 virtual void setLedMode(LedMode mode) const = 0;
165
170 [[nodiscard]] virtual LedMode getLedMode() const = 0;
171
179 virtual void setLedColor(int r, int g, int b) const = 0;
180
186 [[nodiscard]] virtual std::tuple<int, int, int> getLedColor() const = 0;
187
193 virtual void setAnalogCenter(int center, bool left) const = 0;
194
200 [[nodiscard]] virtual int getAnalogCenter(bool left) const = 0;
201
207 virtual void setAnalogBoundary(int boundary, bool left) const = 0;
208
214 [[nodiscard]] virtual int getAnalogBoundary(bool left) const = 0;
215
220 void enableLogging(const std::function<void(const std::wstring &)> &logCB) { logFn = logCB; }
221
227 [[nodiscard]] bool init();
228
234 [[nodiscard]] bool hasFeature(int feature) const;
235 };
236}
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
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 resetConfig() const =0
reset the whole controller configuration memory
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:220
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 bool readVersion()=0
read the controller firmware version
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