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 std::pair<int, int> xVersion;
45 std::pair<int, int> kVersion;
46
47 [[nodiscard]] virtual int getVID() const = 0;
48 [[nodiscard]] virtual int getPID() const = 0;
49
50 void prepareRespBuffer() const;
51 void writeLog(const std::wstring &msg, std::source_location loc = std::source_location::current()) const;
52 [[nodiscard]] bool sendReadRequest() const;
53 [[nodiscard]] bool sendWriteRequest() const;
54 [[nodiscard]] bool setButtonKey(uint16_t *btn, const std::string &key) const;
55 [[nodiscard]] bool setXinputKey(uint16_t *btn, const std::string &key) const;
56 void setBackButtonTime(uint16_t *field, int time) const;
57 void setAnalogDeadzone(int8_t *field, int value) const;
58
59 public:
60 Controller(int controllerFeatures, int sendBufSz, int respBufSz);
61 virtual ~Controller();
62
67 [[nodiscard]] virtual int getControllerType() const = 0;
68
73 [[nodiscard]] virtual bool readVersion() = 0;
74
79 [[nodiscard]] virtual bool readConfig() = 0;
80
85 [[nodiscard]] virtual bool writeConfig() const = 0;
86
93 [[nodiscard]] virtual bool writeConfigMem() const;
94
100 [[nodiscard]] virtual bool resetConfig() const = 0;
101
108 [[nodiscard]] virtual bool setButton(Button btn, const std::string &key) const = 0;
109
115 [[nodiscard]] virtual std::string getButton(Button btn) const = 0;
116
126 [[nodiscard]] virtual bool setBackButton(int num, int slot, const std::string &key) const = 0;
127
136 [[nodiscard]] virtual std::string getBackButton(int num, int slot) const = 0;
137
146 virtual void setBackButtonStartTime(int num, int slot, int timeMs) const = 0;
147
156 [[nodiscard]] virtual int getBackButtonStartTime(int num, int slot) const = 0;
157
162 virtual void setRumble(RumbleMode mode) const = 0;
163
168 [[nodiscard]] virtual RumbleMode getRumbleMode() const = 0;
169
174 virtual void setLedMode(LedMode mode) const = 0;
175
180 [[nodiscard]] virtual LedMode getLedMode() const = 0;
181
189 virtual void setLedColor(int r, int g, int b) const = 0;
190
196 [[nodiscard]] virtual std::tuple<int, int, int> getLedColor() const = 0;
197
203 virtual void setAnalogCenter(int center, bool left) const = 0;
204
210 [[nodiscard]] virtual int getAnalogCenter(bool left) const = 0;
211
217 virtual void setAnalogBoundary(int boundary, bool left) const = 0;
218
224 [[nodiscard]] virtual int getAnalogBoundary(bool left) const = 0;
225
230 void enableLogging(const std::function<void(const std::wstring &)> &logCB) { logFn = logCB; }
231
237 [[nodiscard]] bool init();
238
244 [[nodiscard]] bool hasFeature(int feature) const;
245
246
251 [[nodiscard]] virtual std::pair<int, int> getXVersion() const;
252
257 [[nodiscard]] virtual std::pair<int, int> getKVersion() const;
258 };
259}
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 bool writeConfigMem() const
write the configuration to controller temporary memory (no flash)
Definition Controller.cpp:105
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:230
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