Skip to content

Commit

Permalink
Split drm update input (#3397)
Browse files Browse the repository at this point in the history
* Update `PLATFORM_DRM` implementation of `GetGamepadAxisCount`

* Update

* Update `PLATFORM_DRM` implementation of `GetGamepadName`

* Add example to test gamepad info functions
Fix typo

* Update new gamepad info example

* Move axis count update out of GamepadThread - race condition

* Remove pointless if statement
  • Loading branch information
michaelfiber authored Oct 11, 2023
1 parent 101a9b0 commit daba1a2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ CORE = \
core/core_input_mouse \
core/core_input_mouse_wheel \
core/core_input_gamepad \
core/core_input_gamepad_info \
core/core_input_multitouch \
core/core_input_gestures \
core/core_input_gestures_web \
Expand Down
60 changes: 60 additions & 0 deletions examples/core/core_input_gamepad_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************************
*
* raylib [core] example - Gamepad information
*
* NOTE: This example requires a Gamepad connected to the system
* Check raylib.h for buttons configuration
*
* Example originally created with raylib 4.6, last time updated with raylib 4.6
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2013-2023 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;

SetConfigFlags(FLAG_MSAA_4X_HINT); // Set MSAA 4X hint before windows creation

InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad information");

SetTargetFPS(60); // Set our game to run at 60 frames-per-second

// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
int y = 10;

BeginDrawing();

ClearBackground(RAYWHITE);

for (int i = 0; i < 4; i++) // by default rcore.h has a MAX_GAMEPADS of 4 so mimmic that here.
{
if (IsGamepadAvailable(i))
{
DrawText(TextFormat("Gamepad:\n\tName: %s\n\tAxes: %d", GetGamepadName(i), GetGamepadAxisCount(i)), 10, y, 20, BLACK);
y += 40;
}
}

EndDrawing();
}

// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
18 changes: 4 additions & 14 deletions src/rcore_drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,25 +742,12 @@ void OpenURL(const char *url)
// Get gamepad internal name id
const char *GetGamepadName(int gamepad)
{
const char *name = NULL;

if (CORE.Input.Gamepad.ready[gamepad])
{
ioctl(platform.gamepadStreamFd[gamepad], JSIOCGNAME(64), &CORE.Input.Gamepad.name[gamepad]);
name = CORE.Input.Gamepad.name[gamepad];
}

return name;
return CORE.Input.Gamepad.name[gamepad];
}

// Get gamepad axis count
int GetGamepadAxisCount(int gamepad)
{
int axisCount = 0;

if (CORE.Input.Gamepad.ready[gamepad]) ioctl(platform.gamepadStreamFd[gamepad], JSIOCGAXES, &axisCount);
CORE.Input.Gamepad.axisCount = axisCount;

return CORE.Input.Gamepad.axisCount;
}

Expand Down Expand Up @@ -1959,6 +1946,9 @@ static void InitGamepad(void)
if (error != 0) TRACELOG(LOG_WARNING, "RPI: Failed to create gamepad input event thread");
else TRACELOG(LOG_INFO, "RPI: Gamepad device initialized successfully");
}

ioctl(platform.gamepadStreamFd[i], JSIOCGNAME(64), &CORE.Input.Gamepad.name[i]);
ioctl(platform.gamepadStreamFd[i], JSIOCGAXES, &CORE.Input.Gamepad.axisCount);
}
}
}
Expand Down

0 comments on commit daba1a2

Please sign in to comment.