Skip to content

Commit

Permalink
working rec?
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmagne committed Jun 17, 2024
1 parent 8931a6c commit b0ed9b2
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 132 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ endif()
set(SOURCES
src/main.cpp
src/input_source.cpp
src/utils.cpp
)
target_sources(${CMAKE_PROJECT_NAME} PRIVATE ${SOURCES})

Expand Down
4 changes: 4 additions & 0 deletions src/globals.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include "utils.hpp"

extern rec_timer REC_TIMER;
145 changes: 45 additions & 100 deletions src/input_source.cpp
Original file line number Diff line number Diff line change
@@ -1,100 +1,37 @@
#include <input_source.hpp>
#include <obs-module.h>
#include <plugin-support.h>
#include <obs-frontend-api.h>
#include <vector>

#include <thread>
#include <iostream>
#include <SDL3/SDL.h>
#include <chrono>
#include <atomic>
typedef std::chrono::high_resolution_clock Clock;
#include <fstream>
#include <filesystem>

constexpr int16_t AXIS_DEADZONE = 0;
#include <obs-module.h>
#include <obs-frontend-api.h>

std::string axis_to_string(SDL_GamepadAxis axis) {
switch (axis)
{
case SDL_GAMEPAD_AXIS_LEFTX:
return "LEFTX";
case SDL_GAMEPAD_AXIS_LEFTY:
return "LEFTY";
case SDL_GAMEPAD_AXIS_RIGHTX:
return "RIGHTX";
case SDL_GAMEPAD_AXIS_RIGHTY:
return "RIGHTY";
case SDL_GAMEPAD_AXIS_LEFT_TRIGGER:
return "LEFT_TRIGGER";
case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER:
return "RIGHT_TRIGGER";
default:
return "UNKNOWN";
}
}
#include "input_source.hpp"
#include "plugin-support.h"
#include "utils.hpp"
#include "globals.hpp"

std::string button_to_string(SDL_GamepadButton button) {
switch (button)
{
case SDL_GAMEPAD_BUTTON_SOUTH:
return "SOUTH";
case SDL_GAMEPAD_BUTTON_EAST:
return "EAST";
case SDL_GAMEPAD_BUTTON_WEST:
return "WEST";
case SDL_GAMEPAD_BUTTON_NORTH:
return "NORTH";
case SDL_GAMEPAD_BUTTON_BACK:
return "BACK";
case SDL_GAMEPAD_BUTTON_GUIDE:
return "GUIDE";
case SDL_GAMEPAD_BUTTON_START:
return "START";
case SDL_GAMEPAD_BUTTON_LEFT_STICK:
return "LEFT_STICK";
case SDL_GAMEPAD_BUTTON_RIGHT_STICK:
return "RIGHT_STICK";
case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER:
return "LEFT_SHOULDER";
case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER:
return "RIGHT_SHOULDER";
case SDL_GAMEPAD_BUTTON_DPAD_UP:
return "DPAD_UP";
case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
return "DPAD_DOWN";
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
return "DPAD_LEFT";
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
return "DPAD_RIGHT";
case SDL_GAMEPAD_BUTTON_MISC1:
return "MISC1";
case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1:
return "RIGHT_PADDLE1";
case SDL_GAMEPAD_BUTTON_LEFT_PADDLE1:
return "LEFT_PADDLE1";
case SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2:
return "RIGHT_PADDLE2";
case SDL_GAMEPAD_BUTTON_LEFT_PADDLE2:
return "LEFT_PADDLE2";
case SDL_GAMEPAD_BUTTON_TOUCHPAD:
return "TOUCHPAD";
case SDL_GAMEPAD_BUTTON_MISC2:
return "MISC2";
case SDL_GAMEPAD_BUTTON_MISC3:
return "MISC3";
case SDL_GAMEPAD_BUTTON_MISC4:
return "MISC4";
case SDL_GAMEPAD_BUTTON_MISC5:
return "MISC5";
case SDL_GAMEPAD_BUTTON_MISC6:
return "MISC6";
case SDL_GAMEPAD_BUTTON_MAX:
return "MAX";
default:
return "UNKNOWN";
}
constexpr int16_t AXIS_DEADZONE = 0;

std::filesystem::path home_path() {
const std::filesystem::path default_path {"/tmp"};
char* path;
#ifdef _WIN32
path = getenv("USERPROFILE");
if (path == nullptr) path = getenv("HOMEPATH"); // Alternative on Windows
#else
path = getenv("HOME");
#endif
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!! HOME PATH: " << path << std::endl;
return (path != nullptr) ? std::filesystem::path(path) : default_path;
}

gamepad_manager::gamepad_manager(/* args */) {
gamepad_manager::gamepad_manager():
m_file(home_path()/"input_recording.csv", std::ios::out)
{
/* Init SDL, see SDL/test/testcontroller.c */
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1");
Expand All @@ -118,8 +55,8 @@ gamepad_manager::gamepad_manager(/* args */) {
SDL_Log("\n");
SDL_free(mappings);
}

init_gamepads();
setup_file();
}

void gamepad_manager::init_gamepads() {
Expand Down Expand Up @@ -161,41 +98,49 @@ SDL_Gamepad* gamepad_manager::active_gamepad() {
return nullptr;
}

void gamepad_manager::setup_file() {
m_file << "time,";
for (int i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
m_file << button_to_string((SDL_GamepadButton)i) << ", ";
}
for (int i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
m_file << axis_to_string((SDL_GamepadAxis)i) << ", ";
}
m_file << std::endl;
}

void gamepad_manager::save_gamepad_state() {
SDL_Gamepad* gamepad = active_gamepad();
// Print number of gamepads
if (gamepad) {
auto now = Clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto epoch = now_ms.time_since_epoch();
auto t = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
std::cout << SDL_GAMEPAD_BUTTON_MAX << " " << SDL_GAMEPAD_BUTTON_TOUCHPAD << std::endl;
std::cout << t.count() << " ";
auto dt = REC_TIMER.elapsed();
if (!dt) return;
std::cout << *dt << std::endl;

int i;
m_file << *dt << ",";
for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
const SDL_GamepadButton button = (SDL_GamepadButton)i;
const bool pressed = SDL_GetGamepadButton(gamepad, button) == SDL_PRESSED;
std::cout << "(" << button_to_string(button) << "," << pressed << ") ";

m_file << pressed << ",";
}

for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
const SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
int16_t value = SDL_GetGamepadAxis(gamepad, axis);
value = (value < AXIS_DEADZONE && value > -AXIS_DEADZONE) ? 0 : value;
std::cout << "(" << axis_to_string(axis) << "," << value << ") ";
m_file << value << ",";
}

std::cout << std::endl;
m_file << std::endl;
}

}

gamepad_manager::~gamepad_manager() {
for (auto gamepad : m_gamepads) {
SDL_CloseGamepad(gamepad);
}
m_file.close();
SDL_Quit();
}

Expand Down
15 changes: 9 additions & 6 deletions src/input_source.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#pragma once
#include <vector>
#include <SDL3/SDL.h>


bool initialize_rec_source();
#include <fstream>

class gamepad_manager
{
private:
std::vector<SDL_Gamepad*> m_gamepads;
std::ofstream m_file;
public:
gamepad_manager(/* args */);
gamepad_manager();
~gamepad_manager();
void init_gamepads();
void add_gamepad(SDL_JoystickID joystickid);
void loop();
int get_gamepad_idx(SDL_JoystickID joystickid);
void remove_gamepad(SDL_JoystickID joystickid);
int get_gamepad_idx(SDL_JoystickID joystickid);
void loop();
SDL_Gamepad* active_gamepad();
void setup_file();
void save_gamepad_state();
};

bool initialize_rec_source();
37 changes: 11 additions & 26 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/

#include <obs-module.h>
#include <plugin-support.h>
#include <input_source.hpp>
#include <obs-frontend-api.h>
#include <atomic>
#include <iostream>

#include <obs-module.h>
#include <obs-frontend-api.h>
#include <SDL3/SDL.h>

#include "plugin-support.h"
#include "input_source.hpp"
#include "utils.hpp"
#include "globals.hpp"

OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE(PLUGIN_NAME, "en-US")

long double t {0.};
bool first_frame {true};
std::atomic<bool> is_recording {false};
rec_timer REC_TIMER;

bool obs_module_load(void) {
if (!initialize_rec_source()) {
Expand All @@ -42,35 +44,18 @@ bool obs_module_load(void) {
switch (event) {
case OBS_FRONTEND_EVENT_RECORDING_STARTING:
obs_log(LOG_INFO, "OBS_FRONTEND_EVENT_RECORDING_STARTING received");
is_recording = true;
REC_TIMER.start();
break;
case OBS_FRONTEND_EVENT_RECORDING_STOPPING:
obs_log(LOG_INFO, "OBS_FRONTEND_EVENT_RECORDING_STOPPING received");
is_recording = false;
first_frame = true;
REC_TIMER.stop();
break;
default:
break;
}
}, nullptr);

obs_add_tick_callback([](void *param, float seconds) {
UNUSED_PARAMETER(param);
if (is_recording) {
if (first_frame) {
t = 0.;
first_frame = false;
} else {
t += seconds;
}
obs_log(LOG_INFO, "time: %Lf", t);
}
}, nullptr);
obs_log(LOG_INFO, "input-rec (version %s) loaded successfully", PLUGIN_VERSION);

obs_log(LOG_INFO, "SDL_Init succeeded!");

SDL_Quit();
return true;
}

Expand Down
Loading

0 comments on commit b0ed9b2

Please sign in to comment.