Skip to content

Commit

Permalink
fix 3ds lockup on apt suspend/resume and joystick:getGamepadAxis
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Mar 25, 2024
1 parent 4c78a66 commit 5705bdb
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 250 deletions.
6 changes: 4 additions & 2 deletions include/modules/love/love.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ namespace love

int NoGame(lua_State* L);

int LoadLogFile(lua_State* L);
int Print(lua_State* L);

int OpenNestlink(lua_State* L);
int OpenConsole(lua_State* L);

template<Console::Platform T>
bool MainLoop(lua_State* L, int numArgs);
Expand All @@ -38,6 +38,8 @@ namespace love
int IsVersionCompatible(lua_State* L);

int SetGammaCorrect(lua_State* L);

static constexpr int STDIO_PORT = 8000;
} // namespace love

extern "C"
Expand Down
5 changes: 3 additions & 2 deletions include/objects/joystick/joystick.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ namespace love

enum GamepadAxis
{
GAMEPAD_AXIS_LEFTY,
GAMEPAD_AXIS_INVALID,
GAMEPAD_AXIS_LEFTX,
GAMEPAD_AXIS_RIGHTY,
GAMEPAD_AXIS_LEFTY,
GAMEPAD_AXIS_RIGHTX,
GAMEPAD_AXIS_RIGHTY,
GAMEPAD_AXIS_TRIGGERLEFT,
GAMEPAD_AXIS_TRIGGERRIGHT,
GAMEPAD_AXIS_MAX_ENUM
Expand Down
4 changes: 2 additions & 2 deletions platform/ctr/source/objects/joystick_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ float Joystick<Console::CTR>::GetAxis(int index)
circlePosition leftStick {};
hidCircleRead(&leftStick);

float value = (index == 1) ? leftStick.dx : leftStick.dy;
float value = (index == 1) ? leftStick.dy : leftStick.dx;
return std::clamp<float>(value / Joystick::JoystickMax, -1.0f, 1.0f);
}
else if (index == 2 || index == 3)
{
circlePosition rightStick {};
irrstCstickRead(&rightStick);

float value = (index == 3) ? rightStick.dx : rightStick.dy;
float value = (index == 3) ? rightStick.dy : rightStick.dx;
return std::clamp<float>(value / Joystick::JoystickMax, -1.0f, 1.0f);
}
else if (index == 4)
Expand Down
10 changes: 5 additions & 5 deletions platform/ctr/source/utilities/driver/hid_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ static void aptEventHook(const APT_HookType type, void* parameter)
{
driver.SendFocus(true);

if (graphics)
graphics->SetActive(true);
// if (graphics)
// graphics->SetActive(true);

break;
}
Expand All @@ -32,8 +32,8 @@ static void aptEventHook(const APT_HookType type, void* parameter)
{
driver.SendFocus(false);

if (graphics)
graphics->SetActive(false);
// if (graphics)
// graphics->SetActive(false);

break;
}
Expand Down Expand Up @@ -119,7 +119,7 @@ void HID<Console::CTR>::_Poll()
this->SendGamepadPress(false, joystick->GetID(), input.button, input.buttonNumber);

/* handle trigger and stick inputs */
for (size_t index = 0; index < Joystick<>::GAMEPAD_AXIS_MAX_ENUM; index++)
for (size_t index = 1; index < Joystick<>::GAMEPAD_AXIS_MAX_ENUM; index++)
{
const auto axis = (Joystick<>::GamepadAxis)index;

Expand Down
116 changes: 81 additions & 35 deletions source/modules/love/love.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ static constexpr char nogame_lua[] = {
#include <scripts/nogame.lua>
};

static constexpr char logfile_lua[] = {
#include "scripts/logfile.lua"
};

static constexpr char nestlink_lua[] = {
#include "scripts/nestlink.lua"
};

#include <modules/graphics/graphics.tcc>

#include <modules/audio/wrap_audio.hpp>
Expand All @@ -52,7 +44,7 @@ static constexpr char nestlink_lua[] = {
#include <modules/touch/wrap_touch.hpp>
#include <modules/window/wrap_window.hpp>

#include <utilities/debug/logfile.hpp>
#include <arpa/inet.h>

// clang-format off
static constexpr luaL_Reg modules[] =
Expand All @@ -75,14 +67,10 @@ static constexpr luaL_Reg modules[] =
{ "love.timer", Wrap_Timer::Register },
{ "love.touch", Wrap_Touch::Register },
{ "love.window", Wrap_Window::Register },
#if defined(__DEBUG__)
{ "love.log", love::LoadLogFile },
#endif
{ "love.arg", love::LoadArgs },
{ "love.callbacks", love::LoadCallbacks },
{ "love.boot", love::Boot },
{ "love.nogame", love::NoGame },
{ "nestlink", love::OpenNestlink },
{ 0, 0 }
};
// clang-format on
Expand Down Expand Up @@ -147,6 +135,11 @@ int love::Initialize(lua_State* L)
lua_pushstring(L, CODENAME);
lua_setfield(L, -2, "_version_codename");

lua_pushcfunction(L, love::OpenConsole);
lua_setfield(L, -2, "_openConsole");

lua_register(L, "print", love::Print);

// love._potion_(major, minor, micro)
lua_pushnumber(L, LOVE_POTION.major);
lua_setfield(L, -2, "_potion_version_major");
Expand Down Expand Up @@ -190,19 +183,88 @@ int love::Initialize(lua_State* L)
luax::Preload(L, luaopen_luautf8, "utf8");
luax::Preload(L, luaopen_https, "https");

return 1;
}

int love::OpenConsole(lua_State* L)
{
struct sockaddr_in server;
int lsockfd = socket(AF_INET, SOCK_STREAM, 0);

if (lsockfd < 0)
{
lua_atpanic(L, [](lua_State* L) -> int {
auto location = std::source_location::current();
const auto* message = "PANIC: unprotected error in call to Lua API (%s)\n";
Log::Instance(true).Write(location, message, lua_tostring(L, -1));
luax::PushBoolean(L, false);
return 1;
}

/* make piepie happy :) */
std::fill_n(&server, 1, sockaddr_in {});

return 0;
});
server.sin_family = AF_INET;
server.sin_addr.s_addr = 0;
server.sin_port = htons(STDIO_PORT);

if (bind(lsockfd, (struct sockaddr*)&server, sizeof(server)) < 0)
{
luax::PushBoolean(L, false);
close(lsockfd);
return 1;
}

if (listen(lsockfd, 5) < 0)
{
luax::PushBoolean(L, false);
close(lsockfd);
return 1;
}

int sockfd = accept(lsockfd, NULL, NULL);
close(lsockfd);

if (sockfd < 0)
{
luax::PushBoolean(L, false);
return 1;
}

std::fflush(stdout);
dup2(sockfd, STDOUT_FILENO);

close(sockfd);

luax::PushBoolean(L, true);
return 1;
}

int love::Print(lua_State* L)
{
int argc = lua_gettop(L);
lua_getglobal(L, "tostring");

std::string result {};

for (int index = 1; index <= argc; index++)
{
lua_pushvalue(L, -1);
lua_pushvalue(L, index);
lua_call(L, 1, 1);

const char* string = lua_tostring(L, -1);
if (string == nullptr)
return luaL_error(L, "'tostring' must return a string to 'print'");

if (index > 1)
result += "\t";

result += string;

lua_pop(L, 1);
}

std::printf("[LOVE] %s\r\n", result.c_str());
return 0;
}

int love::GetVersion(lua_State* L)
{
lua_pushinteger(L, LOVE_FRAMEWORK.major);
Expand Down Expand Up @@ -249,14 +311,6 @@ int love::IsVersionCompatible(lua_State* L)
return 1;
}

int love::LoadLogFile(lua_State* L)
{
if (luaL_loadbuffer(L, logfile_lua, sizeof(logfile_lua), "=[love \"logfile.lua\"]") == 0)
lua_call(L, 0, 1);

return 1;
}

int love::LoadArgs(lua_State* L)
{
if (luaL_loadbuffer(L, arg_lua, sizeof(arg_lua), "=[love \"arg.lua\"]") == 0)
Expand Down Expand Up @@ -288,11 +342,3 @@ int love::NoGame(lua_State* L)

return 1;
}

int love::OpenNestlink(lua_State* L)
{
if (luaL_loadbuffer(L, nestlink_lua, sizeof(nestlink_lua), "=[love \"nestlink.lua\"]") == 0)
lua_call(L, 0, 1);

return 1;
}
37 changes: 12 additions & 25 deletions source/modules/love/scripts/boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,11 @@ freely, subject to the following restrictions:

-- Make sure love exists.
local love = require("love")
local nestlink = nil

-- Essential code boot/init.
require("love.arg")
require("love.callbacks")

local is_debug, log = pcall(require, "love.log")
local function TRACE(format, ...) end

if is_debug then
local file = log.new("boot.log")
TRACE = function(format, ...)
file:trace(format, ...)
end
end

local function uridecode(s)
return s:gsub("%%%x%x", function(str)
return string.char(tonumber(str:sub(2), 16))
Expand Down Expand Up @@ -227,6 +216,12 @@ function love.init()
excluderenderers = nil,
}

local openedconsole = false
if love.arg.options.console.set and love._openConsole then
love._openConsole()
openedconsole = true
end

-- If config file exists, load it and allow it to update config table.
local confok, conferr
if (not love.conf) and love.filesystem and love.filesystem.getInfo("conf.lua") then
Expand All @@ -241,14 +236,10 @@ function love.init()
-- the error message can be displayed in the window.
end

-- Open the nestlink client
-- Open the console
local console_ok, console_error
if c.console and type(c.console) == "table" then
console_ok, nestlink = pcall(require, "nestlink")

if console_ok then
console_ok, console_error = pcall(function() nestlink.connect(unpack(c.console)) end)
end
if c.console and love._openConsole and not openedconsole then
love._openConsole(c.console)
end

-- Hack for disabling accelerometer-as-joystick on Android / iOS.
Expand Down Expand Up @@ -432,18 +423,17 @@ function love.init()
local gamestr = gamepath == "" and "" or " at " .. '"' .. gamepath .. '"'

error(("No code to run %s\nYour game might be packaged incorrectly.\nMake sure %s is at the top level of the zip or folder.")
:format(gamestr, main_file))
:format(gamestr, main_file))
elseif invalid_game_path then
error(("Cannot load game at path '%s'.\nMake sure a folder exists at the specified path."):format(
invalid_game_path))
invalid_game_path))
end
end

local print, debug, tostring = print, debug, tostring

local function error_printer(msg, layer)
local trace = debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", "")
TRACE(trace)
print(debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", ""))
end

-----------------------------------------------------------
Expand Down Expand Up @@ -485,9 +475,6 @@ return function()
while func do
local _, retval, restartvalue = xpcall(func, deferErrhand)
if retval then
if nestlink then
nestlink.disconnect()
end
return retval, restartvalue
end
coroutine.yield()
Expand Down
16 changes: 2 additions & 14 deletions source/modules/love/scripts/callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
--]]

local love = require("love")

local is_debug, log = pcall(require, "love.log")
local function TRACE(format, ...) end

if is_debug then
local file = log.new("callbacks.log")
TRACE = function(format, ...)
file:trace(format, ...)
end
end
local love = require("love")

function love.createhandlers()

-- Standard callback handlers.
love.handlers = setmetatable({
--#region unused
Expand Down Expand Up @@ -306,8 +295,7 @@ end
local utf8 = require("utf8")

local function error_printer(msg, layer)
local trace = debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", "")
TRACE(trace)
print(debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", ""))
end

function love.errhand(msg)
Expand Down
Loading

0 comments on commit 5705bdb

Please sign in to comment.