1// tContGamepad.h 
2// 
3// This file implements a gamepad controller. Controllers represent physical input devices. 
4// 
5// Copyright (c) 2025 Tristan Grimmer. 
6// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby 
7// granted, provided that the above copyright notice and this permission notice appear in all copies. 
8// 
9// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 
10// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 
11// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
12// AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 
13// PERFORMANCE OF THIS SOFTWARE. 
14 
15#pragma once 
16#include <condition_variable> 
17#include "Input/tCont.h" 
18#include "Input/tCompJoystick.h" // A gamepad has 2 joysticks. Joysticks contain the push button. 
19#include "Input/tCompDirPad.h" // A gamepad has 1 DPad. 
20#include "Input/tCompTrigger.h" // A gamepad has 2 Triggers. 
21#include "Input/tCompButton.h" // A gamepad has 8 Buttons (not including the joystick buttons). 
22namespace tInput 
23
24 
25 
26enum class tGamepadID 
27
28 Invalid = -1
29 GP0, GP1, GP2, GP3
30 NumGamepads 
31}; 
32 
33 
34class tContGamepad : public tController 
35
36public
37 // Move constructor. Must be present for std::vector reserve. 
38 tContGamepad(tContGamepad&& src) : 
39 tController(src.Name), 
40 InitCompMove(LStick), 
41 InitCompMove(RStick), 
42 InitCompMove(DPad), 
43 InitCompMove(LTrigger), 
44 InitCompMove(RTrigger), 
45 InitCompMove(LViewButton), 
46 InitCompMove(RMenuButton), 
47 InitCompMove(LBumperButton), 
48 InitCompMove(RBumperButton), 
49 InitCompMove(XButton), 
50 InitCompMove(YButton), 
51 InitCompMove(AButton), 
52 InitCompMove(BButton), 
53 GamepadID(src.GamepadID) { } 
54 
55 // Constructs an initially disconnected (non-polling) controller. All gamepads must be contructed with an ID and a 
56 // unique tName. 
57 tContGamepad(const tName& name, tGamepadID id) : 
58 tController(name), 
59 InitCompCopy(LStick), 
60 InitCompCopy(RStick), 
61 InitCompCopy(DPad), 
62 InitCompCopy(LTrigger), 
63 InitCompCopy(RTrigger), 
64 InitCompCopy(LViewButton), 
65 InitCompCopy(RMenuButton), 
66 InitCompCopy(LBumperButton), 
67 InitCompCopy(RBumperButton), 
68 InitCompCopy(XButton), 
69 InitCompCopy(YButton), 
70 InitCompCopy(AButton), 
71 InitCompCopy(BButton), 
72 GamepadID(id) { } 
73 virtual ~tContGamepad() { StopPolling(); } 
74 
75 tCompJoystick LStick; // Contains the Button and 2 axes. 
76 tCompJoystick RStick; // Contains the Button and 2 axes. 
77 tCompDirPad DPad
78 tCompTrigger LTrigger
79 tCompTrigger RTrigger
80 tCompButton LViewButton
81 tCompButton RMenuButton
82 tCompButton LBumperButton
83 tCompButton RBumperButton
84 tCompButton XButton
85 tCompButton YButton
86 tCompButton AButton
87 tCompButton BButton
88 
89 void StartPolling(int pollingPeriod_ns); 
90 void StopPolling(); 
91 bool IsPolling() const { return PollingThread.joinable(); } 
92 bool IsConnected() const { return IsPolling(); } 
93 void Update(); 
94 
95private
96 void SetDefinition(); 
97 void ClearDefinition(); 
98 
99 // This function runs on the polling thread for this controller. 
100 void Poll(); 
101 
102 tGamepadID GamepadID = tGamepadID::Invalid
103 
104 // The PollExitRequested predicate is required to avoid 'spurious wakeups'. Mutex protected. 
105 bool PollingExitRequested = false
106 std::condition_variable PollingExitCondition
107 
108 // We consider the controller connected if the PollingThread is joinable. 
109 std::thread PollingThread
110 
111 ulong PollingPacketNumber = ulong(-1); 
112 int PollingPeriod_ns = 0
113}; 
114 
115 
116
117