1
0
Fork 0
mirror of https://github.com/ctruLua/ctruLua.git synced 2025-10-27 16:39:29 +00:00

"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.
This commit is contained in:
Firew0lf 2015-08-24 20:24:15 +02:00
parent 2f27ea317c
commit d3ca4d0360
4 changed files with 165 additions and 24 deletions

View file

@ -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()

View file

@ -12,10 +12,12 @@ void load_hid_lib(lua_State *L);
void load_ir_lib(lua_State *L); void load_ir_lib(lua_State *L);
void load_fs_lib(lua_State *L); void load_fs_lib(lua_State *L);
void load_httpc_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_gfx_lib(lua_State *L);
void unload_hid_lib(lua_State *L); void unload_hid_lib(lua_State *L);
void unload_fs_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) { static int ctr_run(lua_State *L) {
lua_pushboolean(L, aptMainLoop()); lua_pushboolean(L, aptMainLoop());
@ -38,13 +40,14 @@ static const struct luaL_Reg ctr_lib[] = {
// Subtables // Subtables
struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L); } ctr_libs[] = { struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L); } ctr_libs[] = {
{ "gfx", load_gfx_lib, unload_gfx_lib }, { "gfx", load_gfx_lib, unload_gfx_lib },
{ "news", load_news_lib, NULL }, { "news", load_news_lib, NULL },
{ "ptm", load_ptm_lib, NULL }, { "ptm", load_ptm_lib, NULL },
{ "hid", load_hid_lib, unload_hid_lib }, { "hid", load_hid_lib, unload_hid_lib },
{ "ir", load_ir_lib, NULL }, { "ir", load_ir_lib, NULL },
{ "fs", load_fs_lib, unload_fs_lib }, { "fs", load_fs_lib, unload_fs_lib },
{ "httpc", load_httpc_lib, NULL }, { "httpc", load_httpc_lib, unload_httpc_lib },
{ "qtm", load_qtm_lib, NULL },
{ NULL, NULL } { NULL, NULL }
}; };
@ -67,4 +70,4 @@ void unload_ctr_lib(lua_State *L) {
for (int i = 0; ctr_libs[i].name; i++) { for (int i = 0; ctr_libs[i].name; i++) {
if (ctr_libs[i].unload) ctr_libs[i].unload(L); if (ctr_libs[i].unload) ctr_libs[i].unload(L);
} }
} }

View file

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <3ds.h> #include <3ds.h>
#include <3ds/types.h> #include <3ds/types.h>
@ -7,19 +8,31 @@
#include <lapi.h> #include <lapi.h>
#include <lauxlib.h> #include <lauxlib.h>
static int httpc_init(lua_State *L) { /*static int httpc_init(lua_State *L) {
httpcInit(); Result ret = httpcInit();
return 0; 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(); httpcExit();
return 0; return 0;
} }*/
static int httpc_context(lua_State *L) { static int httpc_context(lua_State *L) {
httpcContext *context; httpcContext context;
context = (httpcContext*)lua_newuserdata(L, sizeof(*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"); luaL_getmetatable(L, "LHTTPC");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
@ -32,8 +45,12 @@ static int httpc_open(lua_State *L) {
Result ret = 0; Result ret = 0;
ret = httpcOpenContext(context, url, 0); ret = httpcOpenContext(context, url, 0);
if (ret != 0) {
lua_pushinteger(L, ret); lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1; return 1;
} }
@ -42,8 +59,12 @@ static int httpc_beginRequest(lua_State *L) {
Result ret = 0; Result ret = 0;
ret = httpcBeginRequest(context); ret = httpcBeginRequest(context);
if (ret != 0) {
lua_pushinteger(L, ret); lua_pushnil(L);
lua_pushinteger(L, ret);
return 2;
}
lua_pushboolean(L, true);
return 1; return 1;
} }
@ -73,14 +94,31 @@ static int httpc_getDownloadSize(lua_State *L) {
static int httpc_downloadData(lua_State *L) { static int httpc_downloadData(lua_State *L) {
httpcContext *context = lua_touserdata(L, 1); 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; u32 size = 0;
httpcGetDownloadSizeState(context, NULL, &size); httpcGetDownloadSizeState(context, NULL, &size);
u8 *buff = (u8*)malloc(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); 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) { static int httpc_close(lua_State *L) {
@ -104,8 +142,8 @@ static const struct luaL_Reg httpc_methods[] = {
// module // module
static const struct luaL_Reg httpc_functions[] = { static const struct luaL_Reg httpc_functions[] = {
{"init", httpc_init }, // {"init", httpc_init },
{"shutdown", httpc_shutdown}, // {"shutdown", httpc_shutdown},
{"context", httpc_context }, {"context", httpc_context },
{NULL, NULL} {NULL, NULL}
}; };
@ -122,6 +160,12 @@ int luaopen_httpc_lib(lua_State *L) {
} }
void load_httpc_lib(lua_State *L) { void load_httpc_lib(lua_State *L) {
httpcInit();
luaL_requiref(L, "ctr.httpc", luaopen_httpc_lib, false); luaL_requiref(L, "ctr.httpc", luaopen_httpc_lib, false);
} }
void unload_httpc_lib(lua_State *L) {
httpcExit();
}

57
source/qtm.c Normal file
View file

@ -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);
}