Skip to content

Commit 3e7e09f

Browse files
committed
Web: Avoid unnecessary gamepad polling when no gamepads are connected
1 parent 2d3bdca commit 3e7e09f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

platform/web/display_server_web.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,9 @@ void DisplayServerWeb::gamepad_callback(int p_index, int p_connected, const char
829829

830830
void DisplayServerWeb::_gamepad_callback(int p_index, int p_connected, const String &p_id, const String &p_guid) {
831831
Input *input = Input::get_singleton();
832+
DisplayServerWeb *ds = get_singleton();
833+
ds->active_gamepad_sample_count = -1; // Invalidate cache
834+
832835
if (p_connected) {
833836
input->joy_connection_changed(p_index, true, p_id, p_guid);
834837
} else {
@@ -1406,7 +1409,10 @@ DisplayServer::VSyncMode DisplayServerWeb::window_get_vsync_mode(WindowID p_vsyn
14061409
void DisplayServerWeb::process_events() {
14071410
process_keys();
14081411
Input::get_singleton()->flush_buffered_events();
1409-
if (godot_js_input_gamepad_sample() == OK) {
1412+
if (active_gamepad_sample_count == -1) {
1413+
active_gamepad_sample_count = godot_js_input_gamepad_sample();
1414+
}
1415+
if (active_gamepad_sample_count > 0) {
14101416
process_joypads();
14111417
}
14121418
}

platform/web/display_server_web.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class DisplayServerWeb : public DisplayServer {
104104
bool swap_cancel_ok = false;
105105
NativeMenu *native_menu = nullptr;
106106

107+
int active_gamepad_sample_count = -1;
108+
107109
MouseMode mouse_mode_base = MOUSE_MODE_VISIBLE;
108110
MouseMode mouse_mode_override = MOUSE_MODE_VISIBLE;
109111
bool mouse_mode_override_enabled = false;

platform/web/js/libs/library_godot_input.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ const GodotInputGamepads = {
205205
sample: function () {
206206
const pads = GodotInputGamepads.get_pads();
207207
const samples = [];
208+
let active = 0;
208209
for (let i = 0; i < pads.length; i++) {
209210
const pad = pads[i];
210211
if (!pad) {
@@ -224,8 +225,10 @@ const GodotInputGamepads = {
224225
s.axes.push(pad.axes[a]);
225226
}
226227
samples.push(s);
228+
active++;
227229
}
228230
GodotInputGamepads.samples = samples;
231+
return active;
229232
},
230233

231234
init: function (onchange) {
@@ -651,8 +654,7 @@ const GodotInput = {
651654
godot_js_input_gamepad_sample__proxy: 'sync',
652655
godot_js_input_gamepad_sample__sig: 'i',
653656
godot_js_input_gamepad_sample: function () {
654-
GodotInputGamepads.sample();
655-
return 0;
657+
return GodotInputGamepads.sample();
656658
},
657659

658660
godot_js_input_gamepad_sample_get__proxy: 'sync',

0 commit comments

Comments
 (0)