glfw: add Joystick.getGamepadName

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-21 21:30:42 -07:00 committed by Stephen Gutekanst
parent c390f6a1f3
commit b5f2f64bd9

View file

@ -410,39 +410,36 @@ const GamepadState = extern struct {
// /// // ///
// /// @thread_safety This function must only be called from the main thread. // /// @thread_safety This function must only be called from the main thread.
// /// // ///
// /// see also: gamepad, glfwJoystickIsGamepad, glfwGetGamepadName // /// see also: gamepad, glfw.Joystick.isGamepad, glfwGetGamepadName
// /// // ///
// /// // ///
// /// @ingroup input // /// @ingroup input
// GLFWAPI int glfwUpdateGamepadMappings(const char* string); // GLFWAPI int glfwUpdateGamepadMappings(const char* string);
// TODO(joystick) /// Returns the human-readable gamepad name for the specified joystick.
// /// Returns the human-readable gamepad name for the specified joystick. ///
// /// /// This function returns the human-readable name of the gamepad from the gamepad mapping assigned
// /// This function returns the human-readable name of the gamepad from the /// to the specified joystick.
// /// gamepad mapping assigned to the specified joystick. ///
// /// /// If the specified joystick is not present or does not have a gamepad mapping this function will
// /// If the specified joystick is not present or does not have a gamepad mapping /// return null, not an error. Call glfw.Joystick.present to check whether it is
// /// this function will return null but will not generate an error. Call /// present regardless of whether it has a mapping.
// /// @ref glfwJoystickPresent to check whether it is present regardless of ///
// /// whether it has a mapping. /// @return The UTF-8 encoded name of the gamepad, or null if the joystick is not present or does
// /// /// not have a mapping.
// /// @param[in] jid The [joystick](@ref joysticks) to query. ///
// /// @return The UTF-8 encoded name of the gamepad, or null if the /// @pointer_lifetime The returned string is allocated and freed by GLFW. You should not free it
// /// joystick is not present, does not have a mapping or an /// yourself. It is valid until the specified joystick is disconnected, the gamepad mappings are
// /// error occurred. /// updated or the library is terminated.
// /// ///
// /// @pointer_lifetime The returned string is allocated and freed by GLFW. You /// @thread_safety This function must only be called from the main thread.
// /// should not free it yourself. It is valid until the specified joystick is ///
// /// disconnected, the gamepad mappings are updated or the library is terminated. /// see also: gamepad, glfw.Joystick.isGamepad
// /// pub inline fn getGamepadName(self: Joystick) Error!?[*c]const u8 {
// /// @thread_safety This function must only be called from the main thread. const name = c.glfwGetGamepadName(self.jid);
// /// try getError();
// /// see also: gamepad, glfwJoystickIsGamepad return name;
// /// }
// ///
// /// @ingroup input
// GLFWAPI const char* glfwGetGamepadName(int jid);
/// Retrieves the state of the joystick remapped as a gamepad. /// Retrieves the state of the joystick remapped as a gamepad.
/// ///
@ -467,7 +464,7 @@ const GamepadState = extern struct {
/// ///
/// @thread_safety This function must only be called from the main thread. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: gamepad, glfwUpdateGamepadMappings, glfwJoystickIsGamepad /// see also: gamepad, glfw.UpdateGamepadMappings, glfw.Joystick.isGamepad
pub inline fn getGamepadState(self: Joystick) Error!?GamepadState { pub inline fn getGamepadState(self: Joystick) Error!?GamepadState {
var state: GamepadState = undefined; var state: GamepadState = undefined;
const success = c.glfwGetGamepadState(self.jid, @ptrCast(*c.GLFWgamepadstate, &state)); const success = c.glfwGetGamepadState(self.jid, @ptrCast(*c.GLFWgamepadstate, &state));
@ -475,6 +472,15 @@ pub inline fn getGamepadState(self: Joystick) Error!?GamepadState {
return if (success == c.GLFW_TRUE) state else null; return if (success == c.GLFW_TRUE) state else null;
} }
test "getGamepadName" {
const glfw = @import("main.zig");
try glfw.init();
defer glfw.terminate();
const joystick = glfw.Joystick{ .jid = glfw.Joystick.one };
_ = joystick.getGamepadName() catch |err| std.debug.print("failed to get gamepad name, joysticks not supported? error={}\n", .{err});
}
test "getGamepadState" { test "getGamepadState" {
const glfw = @import("main.zig"); const glfw = @import("main.zig");
try glfw.init(); try glfw.init();