Skip to content

Commit

Permalink
Fixed some documentation, added some things to sockets, "Fixed" the R…
Browse files Browse the repository at this point in the history
…OMFS

The romfs is still not working, but it works better.
  • Loading branch information
firew0lf committed Apr 13, 2016
1 parent e7ff54d commit 6b65df0
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 70 deletions.
4 changes: 3 additions & 1 deletion Makefile
Expand Up @@ -51,6 +51,9 @@ CFLAGS := -g -Wall -O2 -mword-relocations -std=gnu11 \
$(ARCH)

CFLAGS += $(INCLUDE) -DARM11 -D_3DS -DCTR_VERSION=\"$(APP_VERSION)\" -DCTR_BUILD=\"$(LASTCOMMIT)\"
ifneq ($(ROMFS),)
CFLAGS += -DROMFS
endif

CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11

Expand Down Expand Up @@ -132,7 +135,6 @@ endif

ifneq ($(ROMFS),)
export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS)
CFLAGS += -DROMFS
endif

.PHONY: $(BUILD) clean all
Expand Down
4 changes: 2 additions & 2 deletions sdcard/3ds/ctruLua/examples/qtm/qtm.lua
Expand Up @@ -25,12 +25,12 @@ while ctr.run() do
local keys = hid.keys()
if keys.down.start then break end

local infos = qtm.getHeadTrackingInfo()
local infos = qtm.getHeadtrackingInfo()

gfx.start(gfx.TOP)
if infos:checkHeadFullyDetected() then
for i=1, 4 do
gfx.point(infos:convertCoordToScreen(1, 400, 240))
gfx.point(infos:convertCoordToScreen(i, 400, 240))
end
end
gfx.stop()
Expand Down
2 changes: 1 addition & 1 deletion sdcard/3ds/ctruLua/main.lua
Expand Up @@ -51,4 +51,4 @@ while ctr.run() do
end
end

error("Main process has exited.\nPlease reboot.\nPressing Start does not work yet.")
error("Main process has exited.\nPlease reboot.\nPressing Start does not work yet.")
8 changes: 6 additions & 2 deletions source/cfgu.c
Expand Up @@ -109,9 +109,13 @@ static int cfgu_getUsername(lua_State *L) {

CFGU_GetConfigInfoBlk2(0x1C, 0xA0000, (u8*)block);
u8 *name = malloc(0x14);
utf16_to_utf8(name, block, 0x14);
ssize_t len = utf16_to_utf8(name, block, 0x14);
if (len < 0) {
lua_pushstring(L, "");
return 1;
}

lua_pushlstring(L, (const char *)name, 0x14); // The username is only 0x14 characters long.
lua_pushlstring(L, (const char *)name, len); // The username is only 0x14 characters long.
return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion source/ctr.c
Expand Up @@ -215,7 +215,8 @@ int luaopen_ctr_lib(lua_State *L) {
@field root
*/
#ifdef ROMFS
char* buff = "romfs:";
char* buff = "romfs:/";
chdir(buff);
#else
char* buff = malloc(1024);
getcwd(buff, 1024);
Expand Down
1 change: 1 addition & 0 deletions source/fs.c
Expand Up @@ -239,3 +239,4 @@ void unload_fs_lib(lua_State *L) {

fsExit();
}

7 changes: 6 additions & 1 deletion source/gfx.c
Expand Up @@ -525,12 +525,17 @@ static int gfx_target___index(lua_State *L) {
return 1;
}

/***
Console
@section console
*/

/***
Initialize the console. You can print on it using print(), or any function that normally outputs to stdout.
Warning: you can't use a screen for both a console and drawing, you have to disable the console first.
@function console
@tparam[opt=gfx.TOP] number screen screen to draw the console on.
@tparam[opt=true] boolean debug enable stderr output on the console
@tparam[opt=false] boolean debug enable stderr output on the console
*/
u8 consoleScreen = GFX_TOP;
static int gfx_console(lua_State *L) {
Expand Down
28 changes: 16 additions & 12 deletions source/main.c
Expand Up @@ -36,7 +36,23 @@ void error(const char *error) {
// Main loop
int main(int argc, char** argv) {
// Default arguments
#ifdef ROMFS
char* mainFile = "romfs:/main.lua";
#else
char* mainFile = "main.lua";
#endif

// Init Lua
lua_State *L = luaL_newstate();
if (L == NULL) {
error("Memory allocation error while creating a new Lua state");
return 0;
}

// Load libs
luaL_openlibs(L);
load_ctr_lib(L);
isGfxInitialized = true;

// Parse arguments
for (int i=0;i<argc;i++) {
Expand All @@ -60,18 +76,6 @@ int main(int argc, char** argv) {
}
}

// Init Lua
lua_State *L = luaL_newstate();
if (L == NULL) {
error("Memory allocation error while creating a new Lua state");
return 0;
}

// Load libs
luaL_openlibs(L);
load_ctr_lib(L);
isGfxInitialized = true;

// Do the actual thing
if (luaL_dofile(L, mainFile)) error(luaL_checkstring(L, -1));

Expand Down
2 changes: 1 addition & 1 deletion source/qtm.c
Expand Up @@ -59,7 +59,7 @@ static int qtm_checkInitialized(lua_State *L) {

/***
Return informations about the headtracking
@function getHeadTrackingInfo
@function getHeadtrackingInfo
@treturn qtmInfos QTM informations
*/
static int qtm_getHeadtrackingInfo(lua_State *L) {
Expand Down
126 changes: 77 additions & 49 deletions source/socket.c
@@ -1,6 +1,7 @@
/***
The `socket` module. Almost like luasocket, but for the TCP part only.
The UDP part is only without connection.
All sockets are not blocking by default.
@module ctr.socket
@usage local socket = require("ctr.socket")
*/
Expand Down Expand Up @@ -41,6 +42,9 @@ u32 rootCertChain = 0;
Initialize the socket module
@function init
@tparam[opt=0x100000] number buffer size (in bytes), must be a multiple of 0x1000
@treturn[1] boolean `true` if everything went fine
@treturn[2] boolean `false` in case of error
@treturn[2] number/string error code/message
*/
static int socket_init(lua_State *L) {
if (!initStateSocket) {
Expand All @@ -60,7 +64,7 @@ static int socket_init(lua_State *L) {

Result ret = socInit(mem, size);

if (ret) {
if (R_FAILED(ret)) {
lua_pushboolean(L, false);
lua_pushinteger(L, ret);
return 2;
Expand Down Expand Up @@ -92,11 +96,13 @@ Disable the socket module. Must be called before exiting ctrµLua.
@function shutdown
*/
static int socket_shutdown(lua_State *L) {
sslcDestroyRootCertChain(rootCertChain);
sslcExit();
socExit();
if (initStateSocket) {
sslcDestroyRootCertChain(rootCertChain);
sslcExit();
socExit();
initStateSocket = false;
}

initStateSocket = false;
return 0;
}

Expand All @@ -120,6 +126,7 @@ static int socket_tcp(lua_State *L) {
userdata->addr.sin_family = AF_INET;

userdata->isSSL = false;
fcntl(userdata->socket, F_SETFL, fcntl(userdata->socket, F_GETFL, 0)|O_NONBLOCK);

return 1;
}
Expand All @@ -140,9 +147,9 @@ static int socket_udp(lua_State *L) {
lua_pushstring(L, strerror(errno));
return 2;
}
fcntl(userdata->socket, F_SETFL, O_NONBLOCK);

userdata->addr.sin_family = AF_INET;
fcntl(userdata->socket, F_SETFL, fcntl(userdata->socket, F_GETFL, 0)|O_NONBLOCK);

return 1;
}
Expand All @@ -151,6 +158,9 @@ static int socket_udp(lua_State *L) {
Add a trusted root CA to the certChain.
@function addTrustedRootCA
@tparam string cert DER cert
@treturn[1] boolean `true` if everything went fine
@treturn[2] nil in case of error
@treturn[2] number error code
*/
static int socket_addTrustedRootCA(lua_State *L) {
size_t size = 0;
Expand Down Expand Up @@ -181,7 +191,7 @@ static int socket_bind(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");
int port = luaL_checkinteger(L, 2);

userdata->addr.sin_addr.s_addr = htonl(INADDR_ANY);
userdata->addr.sin_addr.s_addr = gethostid();
userdata->addr.sin_port = htons(port);

bind(userdata->socket, (struct sockaddr*)&userdata->addr, sizeof(userdata->addr));
Expand All @@ -205,6 +215,64 @@ static int socket_close(lua_State *L) {
return 0;
}

/***
Get some informations from a socket.
@function :getpeername
@treturn string IP
@treturn number port
*/
static int socket_getpeername(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");

struct sockaddr_in addr;
socklen_t addrSize = sizeof(addr);

getpeername(userdata->socket, (struct sockaddr*)&addr, &addrSize);

lua_pushstring(L, inet_ntoa(addr.sin_addr));
lua_pushinteger(L, ntohs(addr.sin_port));

return 2;
}

/***
Get some local informations from a socket.
@function :getsockname
@treturn string IP
@treturn number port
*/
static int socket_getsockname(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");

struct sockaddr_in addr;
socklen_t addrSize = sizeof(addr);

getsockname(userdata->socket, (struct sockaddr*)&addr, &addrSize);

lua_pushstring(L, inet_ntoa(addr.sin_addr));
lua_pushinteger(L, ntohs(addr.sin_port));

return 2;
}

/***
Set if the socket should be blocking.
@function :setBlocking
@tparam[opt=true] boolean block if `false`, the socket won't block
*/
static int socket_setBlocking(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");
bool block = true;
if (lua_isboolean(L, 2))
block = lua_toboolean(L, 2);

int flags = fcntl(userdata->socket, F_GETFL, 0);
flags = block?(flags&~O_NONBLOCK):(flags|O_NONBLOCK);
fcntl(userdata->socket, F_SETFL, flags);

return 0;
}

/***
TCP Sockets
@section TCP
Expand All @@ -221,14 +289,14 @@ static int socket_accept(lua_State *L) {
socket_userdata *client = lua_newuserdata(L, sizeof(*client));
luaL_getmetatable(L, "LSocket");
lua_setmetatable(L, -2);
client->isSSL = false;

socklen_t addrSize = sizeof(client->addr);
client->socket = accept(userdata->socket, (struct sockaddr*)&client->addr, &addrSize);
if (client->socket < 0) {
lua_pushnil(L);
return 1;
}
fcntl(client->socket, F_SETFL, O_NONBLOCK);

return 1;
}
Expand Down Expand Up @@ -276,7 +344,6 @@ static int socket_connect(lua_State *L) {
}
userdata->isSSL = true;
}
fcntl(userdata->socket, F_SETFL, O_NONBLOCK);

lua_pushboolean(L, 1);
return 1;
Expand Down Expand Up @@ -402,46 +469,6 @@ static int socket_send(lua_State *L) {
return 1;
}

/***
Get some informations from a socket.
@function :getpeername
@treturn string IP
@treturn number port
*/
static int socket_getpeername(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");

struct sockaddr_in addr;
socklen_t addrSize = sizeof(addr);

getpeername(userdata->socket, (struct sockaddr*)&addr, &addrSize);

lua_pushstring(L, inet_ntoa(addr.sin_addr));
lua_pushinteger(L, ntohs(addr.sin_port));

return 2;
}

/***
Get some local informations from a socket.
@function :getsockname
@treturn string IP
@treturn number port
*/
static int socket_getsockname(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");

struct sockaddr_in addr;
socklen_t addrSize = sizeof(addr);

getsockname(userdata->socket, (struct sockaddr*)&addr, &addrSize);

lua_pushstring(L, inet_ntoa(addr.sin_addr));
lua_pushinteger(L, ntohs(addr.sin_port));

return 2;
}

/***
UDP sockets
@section UDP
Expand Down Expand Up @@ -533,6 +560,7 @@ static const struct luaL_Reg socket_methods[] = {
{"accept", socket_accept },
{"bind", socket_bind },
{"close", socket_close },
{"setBlocking", socket_setBlocking},
{"__gc", socket_close },
{"connect", socket_connect },
{"listen", socket_listen },
Expand Down

0 comments on commit 6b65df0

Please sign in to comment.