Skip to content

Commit 7ed506f

Browse files
committed
Merge pull request #108007 from adamscott/the-spirit-of-105601
[Web] Poll controllers only if at least one is detected
2 parents f936033 + c56d131 commit 7ed506f

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

platform/web/display_server_web.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,13 @@ void DisplayServerWeb::gamepad_callback(int p_index, int p_connected, const char
829829
}
830830

831831
void DisplayServerWeb::_gamepad_callback(int p_index, int p_connected, const String &p_id, const String &p_guid) {
832-
Input *input = Input::get_singleton();
833-
DisplayServerWeb *ds = get_singleton();
834-
ds->active_gamepad_sample_count = -1; // Invalidate cache
832+
if (p_connected) {
833+
DisplayServerWeb::get_singleton()->gamepad_count += 1;
834+
} else {
835+
DisplayServerWeb::get_singleton()->gamepad_count -= 1;
836+
}
835837

838+
Input *input = Input::get_singleton();
836839
if (p_connected) {
837840
input->joy_connection_changed(p_index, true, p_id, p_guid);
838841
} else {
@@ -1435,11 +1438,11 @@ DisplayServer::VSyncMode DisplayServerWeb::window_get_vsync_mode(WindowID p_vsyn
14351438
void DisplayServerWeb::process_events() {
14361439
process_keys();
14371440
Input::get_singleton()->flush_buffered_events();
1438-
if (active_gamepad_sample_count == -1) {
1439-
active_gamepad_sample_count = godot_js_input_gamepad_sample();
1440-
}
1441-
if (active_gamepad_sample_count > 0) {
1442-
process_joypads();
1441+
1442+
if (gamepad_count > 0) {
1443+
if (godot_js_input_gamepad_sample() == OK) {
1444+
process_joypads();
1445+
}
14431446
}
14441447
}
14451448

platform/web/display_server_web.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class DisplayServerWeb : public DisplayServer {
104104
bool swap_cancel_ok = false;
105105
NativeMenu *native_menu = nullptr;
106106

107-
int active_gamepad_sample_count = -1;
107+
int gamepad_count = 0;
108108

109109
MouseMode mouse_mode_base = MOUSE_MODE_VISIBLE;
110110
MouseMode mouse_mode_override = MOUSE_MODE_VISIBLE;

platform/web/js/libs/library_godot_input.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ const GodotInputGamepads = {
205205
sample: function () {
206206
const pads = GodotInputGamepads.get_pads();
207207
const samples = [];
208-
let active = 0;
209208
for (let i = 0; i < pads.length; i++) {
210209
const pad = pads[i];
211210
if (!pad) {
@@ -225,10 +224,8 @@ const GodotInputGamepads = {
225224
s.axes.push(pad.axes[a]);
226225
}
227226
samples.push(s);
228-
active++;
229227
}
230228
GodotInputGamepads.samples = samples;
231-
return active;
232229
},
233230

234231
init: function (onchange) {
@@ -662,7 +659,8 @@ const GodotInput = {
662659
godot_js_input_gamepad_sample__proxy: 'sync',
663660
godot_js_input_gamepad_sample__sig: 'i',
664661
godot_js_input_gamepad_sample: function () {
665-
return GodotInputGamepads.sample();
662+
GodotInputGamepads.sample();
663+
return 0;
666664
},
667665

668666
godot_js_input_gamepad_sample_get__proxy: 'sync',

0 commit comments

Comments
 (0)