Skip to content

Commit

Permalink
Finished the QTM library, Added some text in ctr.c
Browse files Browse the repository at this point in the history
The QTM (Headtracking) is not tested on real hardware, and doesn't work in citra.
But should work, maybe. Please update your boot.3dsx to use it without errors.
  • Loading branch information
firew0lf committed Sep 11, 2015
1 parent 3f48e6e commit 848fe42
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
2 changes: 2 additions & 0 deletions source/ctr.c
Expand Up @@ -13,6 +13,7 @@ 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 load_cam_lib(lua_State *L);

void unload_gfx_lib(lua_State *L);
void unload_hid_lib(lua_State *L);
Expand Down Expand Up @@ -48,6 +49,7 @@ struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L); }
{ "fs", load_fs_lib, unload_fs_lib },
{ "httpc", load_httpc_lib, unload_httpc_lib },
{ "qtm", load_qtm_lib, NULL },
// { "cam", load_cam_lib, NULL },
{ NULL, NULL }
};

Expand Down
80 changes: 73 additions & 7 deletions source/qtm.c
Expand Up @@ -5,10 +5,20 @@
#include <lapi.h>
#include <lauxlib.h>

static int qtm_init(lua_State *L) {
qtmInit();
typedef struct {
qtmHeadtrackingInfo *info;
} qtm_userdata;

return 0;
static int qtm_init(lua_State *L) {
Result ret = qtmInit();
if (ret) {
lua_pushboolean(L, false);
lua_pushinteger(L, ret);
return 2;
}

lua_pushboolean(L, true);
return 1;
}

static int qtm_shutdown(lua_State *L) {
Expand All @@ -25,18 +35,69 @@ static int qtm_checkInitialized(lua_State *L) {
}

static int qtm_getHeadtrackingInfo(lua_State *L) {
return 0;
qtm_userdata *data = lua_newuserdata(L, sizeof(*data));
luaL_getmetatable(L, "LQTM");
lua_setmetatable(L, -2);
Result ret = qtmGetHeadtrackingInfo(0, data->info);
if (ret) {
lua_pushnil(L);
return 1;
}
return 1;
}

static int qtm_checkHeadFullyDetected(lua_State *L) {
return 0;
qtm_userdata *info = luaL_checkudata(L, 1, "LQTM");
lua_pushboolean(L, qtmCheckHeadFullyDetected(info->info));
return 1;
}

static int qtm___index(lua_State *L) {
qtm_userdata *info = luaL_checkudata(L, 1, "LQTM");
lua_Integer index = luaL_checkinteger(L, 2);
index = index - 1; // Lua index begins at 1
if (index > 3 || index < 0) {
lua_pushnil(L);
lua_pushnil(L);
return 2;
}

lua_pushnumber(L, info->info->coords0[index].x);
lua_pushnumber(L, info->info->coords0[index].y);

return 2;
}

static int qtm_convertCoordToScreen(lua_State *L) {
return 0;
qtm_userdata *info = luaL_checkudata(L, 1, "LQTM");
lua_Integer index = luaL_checkinteger(L, 2);
index = index - 1; // Lua index begins at 1
if (index > 3 || index < 0) {
lua_pushnil(L);
lua_pushnil(L);
return 2;
}
float screenWidth = luaL_optnumber(L, 3, 400.0f);
float screenHeight = luaL_optnumber(L, 4, 320.0f);

u32 x, y = 0;
qtmConvertCoordToScreen(&info->info->coords0[index], &screenWidth, &screenHeight, &x, &y);

lua_pushinteger(L, x);
lua_pushinteger(L, y);

return 2;
}

// module
// object
static const struct luaL_Reg qtm_methods[] = {
{"checkHeadFullyDetected", qtm_checkHeadFullyDetected},
{"convertCoordToScreen", qtm_convertCoordToScreen },
{"__index", qtm___index },
{NULL, NULL}
};

// module functions
static const struct luaL_Reg qtm_functions[] = {
{"init", qtm_init },
{"shutdown", qtm_shutdown },
Expand All @@ -48,6 +109,11 @@ static const struct luaL_Reg qtm_functions[] = {
};

int luaopen_qtm_lib(lua_State *L) {
luaL_newmetatable(L, "LQTM");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, qtm_methods, 0);

luaL_newlib(L, qtm_functions);
return 1;
}
Expand Down

0 comments on commit 848fe42

Please sign in to comment.