Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
"Fixed" the HTTPC lib with the crappiest code you've ever seen, Added…
… a (very) small QTM lib

Don't look at the HTTPC code !!! Also, the example only work a random number of times.
  • Loading branch information
firew0lf committed Aug 24, 2015
1 parent 2f27ea3 commit d3ca4d0
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 24 deletions.
37 changes: 37 additions & 0 deletions sdcard/3ds/ctruLua/tests/httpc.lua
@@ -0,0 +1,37 @@
local ctr = require("ctr")
local gfx = require("ctr.gfx")
local hid = require("ctr.hid")
local httpc = require("ctr.httpc")

local err = 0

--assert(httpc.init())

local context = assert(httpc.context())

assert(context:open("http://firew0lf.github.io/"))
assert(context:beginRequest())

local data = assert(context:downloadData())

while ctr.run() do
hid.read()
keys = hid.keys()
if keys.held.start then break end
if keys.down.b then
assert(context:open("http://firew0lf.github.io/"))
assert(context:beginRequest())
data = assert(context:downloadData())
data = (data.."!")
end

gfx.startFrame(gfx.GFX_TOP)
gfx.text(0, 0, data)
gfx.endFrame()

gfx.render()
end


context:close()
--httpc.shutdown()
19 changes: 11 additions & 8 deletions source/ctr.c
Expand Up @@ -12,10 +12,12 @@ void load_hid_lib(lua_State *L);
void load_ir_lib(lua_State *L);
void load_fs_lib(lua_State *L);
void load_httpc_lib(lua_State *L);
void load_qtm_lib(lua_State *L);

void unload_gfx_lib(lua_State *L);
void unload_hid_lib(lua_State *L);
void unload_fs_lib(lua_State *L);
void unload_httpc_lib(lua_State *L);

static int ctr_run(lua_State *L) {
lua_pushboolean(L, aptMainLoop());
Expand All @@ -38,13 +40,14 @@ static const struct luaL_Reg ctr_lib[] = {

// Subtables
struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L); } ctr_libs[] = {
{ "gfx", load_gfx_lib, unload_gfx_lib },
{ "news", load_news_lib, NULL },
{ "ptm", load_ptm_lib, NULL },
{ "hid", load_hid_lib, unload_hid_lib },
{ "ir", load_ir_lib, NULL },
{ "fs", load_fs_lib, unload_fs_lib },
{ "httpc", load_httpc_lib, NULL },
{ "gfx", load_gfx_lib, unload_gfx_lib },
{ "news", load_news_lib, NULL },
{ "ptm", load_ptm_lib, NULL },
{ "hid", load_hid_lib, unload_hid_lib },
{ "ir", load_ir_lib, NULL },
{ "fs", load_fs_lib, unload_fs_lib },
{ "httpc", load_httpc_lib, unload_httpc_lib },
{ "qtm", load_qtm_lib, NULL },
{ NULL, NULL }
};

Expand All @@ -67,4 +70,4 @@ void unload_ctr_lib(lua_State *L) {
for (int i = 0; ctr_libs[i].name; i++) {
if (ctr_libs[i].unload) ctr_libs[i].unload(L);
}
}
}
76 changes: 60 additions & 16 deletions source/httpc.c
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <string.h>

#include <3ds.h>
#include <3ds/types.h>
Expand All @@ -7,19 +8,31 @@
#include <lapi.h>
#include <lauxlib.h>

static int httpc_init(lua_State *L) {
httpcInit();
return 0;
}
/*static int httpc_init(lua_State *L) {
Result ret = httpcInit();
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1;
}*/

static int httpc_shutdown(lua_State *L) {
/*static int httpc_shutdown(lua_State *L) {
httpcExit();
return 0;
}
}*/

static int httpc_context(lua_State *L) {
httpcContext *context;
context = (httpcContext*)lua_newuserdata(L, sizeof(*context));
httpcContext context;
Result ret = httpcOpenContext(&context, "http://google.com/", 0); // Initialization only.
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_newuserdata(L, sizeof(&context));
luaL_getmetatable(L, "LHTTPC");
lua_setmetatable(L, -2);

Expand All @@ -32,8 +45,12 @@ static int httpc_open(lua_State *L) {
Result ret = 0;

ret = httpcOpenContext(context, url, 0);

lua_pushinteger(L, ret);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1;
}

Expand All @@ -42,8 +59,12 @@ static int httpc_beginRequest(lua_State *L) {
Result ret = 0;

ret = httpcBeginRequest(context);

lua_pushinteger(L, ret);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1;
}

Expand Down Expand Up @@ -73,14 +94,31 @@ static int httpc_getDownloadSize(lua_State *L) {

static int httpc_downloadData(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1);
u32 status = 0;
Result ret = httpcGetResponseStatusCode(context, &status, 0);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}

u32 size = 0;
httpcGetDownloadSizeState(context, NULL, &size);
u8 *buff = (u8*)malloc(size);
//memset(buff, 0, size);

httpcDownloadData(context, buff, size, NULL);
ret = httpcDownloadData(context, buff, size, NULL);
if (ret != 0) {
lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
//strcpy(buff, "loltest");

lua_pushstring(L, (char*)buff);
return 1;
free(buff);
lua_pushinteger(L, size); // only for test purposes.
return 2;
}

static int httpc_close(lua_State *L) {
Expand All @@ -104,8 +142,8 @@ static const struct luaL_Reg httpc_methods[] = {

// module
static const struct luaL_Reg httpc_functions[] = {
{"init", httpc_init },
{"shutdown", httpc_shutdown},
// {"init", httpc_init },
// {"shutdown", httpc_shutdown},
{"context", httpc_context },
{NULL, NULL}
};
Expand All @@ -122,6 +160,12 @@ int luaopen_httpc_lib(lua_State *L) {
}

void load_httpc_lib(lua_State *L) {
httpcInit();

luaL_requiref(L, "ctr.httpc", luaopen_httpc_lib, false);
}

void unload_httpc_lib(lua_State *L) {
httpcExit();
}

57 changes: 57 additions & 0 deletions source/qtm.c
@@ -0,0 +1,57 @@
#include <3ds.h>
#include <3ds/types.h>
#include <3ds/services/qtm.h>

#include <lapi.h>
#include <lauxlib.h>

static int qtm_init(lua_State *L) {
qtmInit();

return 0;
}

static int qtm_shutdown(lua_State *L) {
qtmExit();

return 0;
}

static int qtm_checkInitialized(lua_State *L) {
bool isInit = qtmCheckInitialized();

lua_pushboolean(L, isInit);
return 1;
}

static int qtm_getHeadtrackingInfo(lua_State *L) {
return 0;
}

static int qtm_checkHeadFullyDetected(lua_State *L) {
return 0;
}

static int qtm_convertCoordToScreen(lua_State *L) {
return 0;
}

// module
static const struct luaL_Reg qtm_functions[] = {
{"init", qtm_init },
{"shutdown", qtm_shutdown },
{"checkInitialized", qtm_checkInitialized },
{"getHeadtrackingInfo", qtm_getHeadtrackingInfo },
{"checkHeadFullyDetected", qtm_checkHeadFullyDetected},
{"convertCoordToScreen", qtm_convertCoordToScreen },
{NULL, NULL}
};

int luaopen_qtm_lib(lua_State *L) {
luaL_newlib(L, qtm_functions);
return 1;
}

void load_qtm_lib(lua_State *L) {
luaL_requiref(L, "ctr.qtm", luaopen_qtm_lib, false);
}

0 comments on commit d3ca4d0

Please sign in to comment.