diff --git a/sdcard/3ds/ctruLua/editor/main.lua b/sdcard/3ds/ctruLua/editor/main.lua index 5685fb5..e247583 100644 --- a/sdcard/3ds/ctruLua/editor/main.lua +++ b/sdcard/3ds/ctruLua/editor/main.lua @@ -44,9 +44,36 @@ local fileModified = false local function displayedText(text) return text:gsub("\t", " "), nil end +local function drawTop(eye) + -- Depth multiplicator. Multiply by a positive and add to x to add depth; multiply by a negative and add to x to go out of the screen. + local function d(mul) return math.floor(mul * hid.pos3d() * eye) end + + -- Lines + local sI = math.floor(scrollY / lineHeight) + if sI < 1 then sI = 1 end + + local eI = math.ceil((scrollY + gfx.TOP_HEIGHT) / lineHeight) + if eI > #lines then eI = #lines end + + for i = sI, eI, 1 do + local x = -scrollX + local y = -scrollY+ (i-1)*lineHeight + + for _,colored in ipairs(coloredLines[i]) do + local str = displayedText(colored[1]) + gfx.text(x + d(#(lines[i]:match("^%s+") or "")*3), y, str, fontSize, colored[2]) + x = x + font:width(str) + end + end + + -- Cursor + local curline = lines[cursorY] + gfx.rectangle(-scrollX+ font:width(displayedText(curline:sub(1, (utf8.offset(curline, cursorX) or 0)-1))) + d(#(curline:match("^%s+") or "")*3), + -scrollY+ (cursorY-1)*lineHeight, 1, lineHeight, 0, color.cursor) +end -- Set defaults -gfx.set3D(false) +gfx.set3D(true) gfx.color.setDefault(color.default) gfx.color.setBackground(color.background) gfx.font.setDefault(font) @@ -178,32 +205,17 @@ while ctr.run() do end -- Draw - gfx.start(gfx.TOP) - - -- Lines - local sI = math.floor(scrollY / lineHeight) - if sI < 1 then sI = 1 end - - local eI = math.ceil((scrollY + gfx.TOP_HEIGHT) / lineHeight) - if eI > #lines then eI = #lines end - - for i = sI, eI, 1 do - local x = -scrollX - local y = -scrollY+ (i-1)*lineHeight - - for _,colored in ipairs(coloredLines[i]) do - local str = displayedText(colored[1]) - gfx.text(x, y, str, fontSize, colored[2]) - x = x + font:width(str) - end - end - - -- Cursor - local curline = lines[cursorY] - gfx.rectangle(-scrollX+ font:width(displayedText(curline:sub(1, (utf8.offset(curline, cursorX) or 0)-1))), - -scrollY+ (cursorY-1)*lineHeight, 1, lineHeight, 0, color.cursor) + local is3D = hid.pos3d() > 0 + gfx.start(gfx.TOP, gfx.LEFT) + drawTop(is3D and 0 or -1) gfx.stop() + + if is3D then + gfx.start(gfx.TOP, gfx.RIGHT) + drawTop(1) + gfx.stop() + end gfx.start(gfx.BOTTOM) diff --git a/sdcard/3ds/ctruLua/libs/filepicker.lua b/sdcard/3ds/ctruLua/libs/filepicker.lua index 44f5f77..da4760d 100644 --- a/sdcard/3ds/ctruLua/libs/filepicker.lua +++ b/sdcard/3ds/ctruLua/libs/filepicker.lua @@ -115,7 +115,7 @@ local function drawBottom(externalConfig, workingDirectoryScroll, selected) gfx.text(1, 15, selectedFile.name, 12) if not selectedFile.isDirectory then - gfx.text(1, 45, tostring(selectedFile.fileSize) .. "B", 12, 0xFF727272) + gfx.text(1, 45, tostring(selectedFile.size) .. "B", 12, 0xFF727272) end local binding, pattern = getBinding(selectedFile, bindings) diff --git a/source/cam.c b/source/cam.c index 961d26f..4d756c5 100644 --- a/source/cam.c +++ b/source/cam.c @@ -21,6 +21,9 @@ The `cam` module. /*** Initialize the camera module. @function init +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int cam_init(lua_State *L) { Result ret = camInit(); diff --git a/source/font.c b/source/font.c index a29f356..bac5c44 100644 --- a/source/font.c +++ b/source/font.c @@ -1,5 +1,5 @@ /*** -The `font` module +The `gfx.font` module @module ctr.gfx.font @usage local font = require("ctr.gfx.font") */ @@ -21,7 +21,9 @@ u32 textSize = 9; Load a TTF font. @function load @tparam string path path to the file -@treturn font the loaded font. +@treturn[1] font the loaded font. +@treturn[2] nil if an error occurred +@treturn[2] string error message */ static int font_load(lua_State *L) { const char *path = luaL_checkstring(L, 1); diff --git a/source/fs.c b/source/fs.c index 54d67d6..bc08fdd 100644 --- a/source/fs.c +++ b/source/fs.c @@ -52,14 +52,16 @@ const char* prefix_path(const char* path) { Lists a directory contents (unsorted). @function list @tparam string path the directory we wants to list the content -@treturn table the item list. Each item is a table like: +@treturn[1] table the item list. Each item is a table like: ` { name = "Item name.txt", isDirectory = false, - fileSize = 321 -- (integer) in bytes + size = 321 -- (integer) item size, in bytes } ` +@treturn[2] nil if an error occurred +@treturn[2] string error message */ static int fs_list(lua_State *L) { const char* basepath = prefix_path(luaL_checkstring(L, 1)); @@ -80,8 +82,8 @@ static int fs_list(lua_State *L) { DIR* dir = opendir(path); if (dir == NULL) { if (shouldFreePath) free(path); - lua_pushboolean(L, false); - lua_pushstring(L, strerror(errno)); + lua_pushnil(L); + lua_pushfstring(L, "Can't open directory: %s (%s)", strerror(errno), errno); return 2; } errno = 0; @@ -96,21 +98,17 @@ static int fs_list(lua_State *L) { if (entry->d_type==DT_REG) { // Regular files: check size char* filepath = malloc(strlen(path)+strlen(entry->d_name)+1); - if (filepath == NULL) { + if (filepath == NULL) luaL_error(L, "Memory allocation error"); - } - memset(filepath, 0, strlen(path)+strlen(entry->d_name)+1); - strcpy(filepath, path); strcat(filepath, entry->d_name); + struct stat stats; if (stat(filepath, &stats)) { free(filepath); if (shouldFreePath) free(path); luaL_error(L, "Stat error: %s (%d)", strerror(errno), errno); - lua_pushboolean(L, false); - lua_pushstring(L, strerror(errno)); - return 2; + return 0; } else { lua_pushinteger(L, stats.st_size); } @@ -118,7 +116,7 @@ static int fs_list(lua_State *L) { } else { // Everything else: 0 bytes lua_pushinteger(L, 0); } - lua_setfield(L, -2, "fileSize"); + lua_setfield(L, -2, "size"); lua_seti(L, -2, i); i++; diff --git a/source/httpc.c b/source/httpc.c index ef70afc..97ad4a0 100644 --- a/source/httpc.c +++ b/source/httpc.c @@ -40,6 +40,9 @@ Open an url in the context. @function :open @tparam string url the url to open @tparam[opt="GET"] string method method to use; can be `"GET"`, `"POST"`, `"HEAD"`, `"PUT"` or `"DELETE"` +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int httpc_open(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); @@ -59,7 +62,7 @@ static int httpc_open(lua_State *L) { ret = httpcOpenContext(context, method, url, 0); if (ret != 0) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushinteger(L, ret); return 2; } @@ -72,6 +75,9 @@ Add a field in the request header. @function :addRequestHeaderField @tparam string name Name of the field @tparam string value Value of the field +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int httpc_addRequestHeaderField(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); @@ -80,7 +86,7 @@ static int httpc_addRequestHeaderField(lua_State *L) { Result ret = httpcAddRequestHeaderField(context, name ,value); if (ret != 0) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushinteger(L, ret); return 2; } @@ -91,6 +97,9 @@ static int httpc_addRequestHeaderField(lua_State *L) { /*** Begin a request to get the content at the URL. @function :beginRequest +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int httpc_beginRequest(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); @@ -98,7 +107,7 @@ static int httpc_beginRequest(lua_State *L) { ret = httpcBeginRequest(context); if (ret != 0) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushinteger(L, ret); return 2; } @@ -109,7 +118,9 @@ static int httpc_beginRequest(lua_State *L) { /*** Return the status code returned by the request. @function :getStatusCode -@treturn number the status code +@treturn[1] integer the status code +@treturn[2] nil in case of error +@treturn[2] integer error code */ static int httpc_getStatusCode(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); @@ -143,7 +154,9 @@ static int httpc_getDownloadSize(lua_State *L) { /*** Download and return the data of the context. @function :downloadData -@treturn string data +@treturn[1] string data +@treturn[2] nil in case of error +@treturn[2] integer error code */ static int httpc_downloadData(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); @@ -167,9 +180,9 @@ static int httpc_downloadData(lua_State *L) { } lua_pushstring(L, (char*)buff); - //free(buff); - lua_pushinteger(L, size); // only for test purposes. - return 2; + //free(buff); FIXME we need to free this buffer at some point ? + //lua_pushinteger(L, size); // only for test purposes. + return 1; } /*** @@ -223,6 +236,9 @@ static int httpc_getResponseHeader(lua_State *L) { Add a trusted RootCA cert to a context. @function :addTrustedRootCA @tparam string DER certificate +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int httpc_addTrustedRootCA(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); @@ -231,7 +247,7 @@ static int httpc_addTrustedRootCA(lua_State *L) { Result ret = httpcAddTrustedRootCA(context, cert, certsize); if (ret != 0) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushinteger(L, ret); return 2; } diff --git a/source/ir.c b/source/ir.c index 86d85fe..0144bfb 100644 --- a/source/ir.c +++ b/source/ir.c @@ -37,6 +37,9 @@ Bitrate codes list (this is not a part of the module, just a reference) Initialize the IR module. @function init @tparam[opt=6] number bitrate bitrate of the IR module (more informations below) +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int ir_init(lua_State *L) { u8 bitrate = luaL_optinteger(L, 1, 6); @@ -56,6 +59,9 @@ static int ir_init(lua_State *L) { /*** Disable the IR module. @function shutdown +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int ir_shutdown(lua_State *L) { Result ret = IRU_Shutdown(); @@ -74,6 +80,9 @@ Send some data over the IR module. @function send @tparam string data just some data @tparam[opt=false] boolean wait set to `true` to wait until the data is sent. +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int ir_send(lua_State *L) { u8 *data = (u8*)luaL_checkstring(L, 1); @@ -98,7 +107,9 @@ Receive some data from the IR module. @function receive @tparam number size bytes to receive @tparam[opt=false] boolean wait wait until the data is received -@return string data +@treturn[1] string data +@treturn[2] nil in case of error +@treturn[2] integer error code */ static int ir_receive(lua_State *L) { u32 size = luaL_checkinteger(L, 1); @@ -108,7 +119,7 @@ static int ir_receive(lua_State *L) { Result ret = iruRecvData(data, size, 0x00, &transfercount, wait); if (ret) { - lua_pushboolean(L, false); + lua_pushnil(L); lua_pushinteger(L, ret); return 2; } @@ -122,6 +133,9 @@ static int ir_receive(lua_State *L) { Set the bitrate of the communication. @function setBitRate @tparam number bitrate new bitrate for the communication +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int ir_setBitRate(lua_State *L) { u8 bitrate = luaL_checkinteger(L, 1); @@ -133,20 +147,24 @@ static int ir_setBitRate(lua_State *L) { return 2; } - return 0; + lua_pushboolean(L, true); + + return 1; } /*** Return the actual bitrate of the communication. @function getBitRate -@treturn number actual bitrate +@treturn[1] number actual bitrate +@treturn[2] nil in case of error +@treturn[2] integer error code */ static int ir_getBitRate(lua_State *L) { u8 bitrate = 0; Result ret = IRU_GetBitRate(&bitrate); if (ret) { - lua_pushboolean(L, false); + lua_pushnil(L); lua_pushinteger(L, ret); return 2; } diff --git a/source/map.c b/source/map.c index 6509d0c..c92242d 100644 --- a/source/map.c +++ b/source/map.c @@ -1,5 +1,5 @@ /*** -The `map` module. +The `gfx.map` module. Tile coordinates start at x=0,y=0. @module ctr.gfx.map @usage local map = require("ctr.gfx.map") @@ -47,7 +47,9 @@ Load a map from a file. @tparam texture tileset containing the tileset @tparam number tileWidth tile width @tparam number tileHeight tile height -@treturn map loaded map object +@treturn[1] map loaded map object +@treturn[2] nil in case of error +@treturn[2] string error message */ static int map_load(lua_State *L) { texture_userdata *texture = luaL_checkudata(L, 2, "LTexture"); diff --git a/source/mic.c b/source/mic.c index ed9d308..0e3f19a 100644 --- a/source/mic.c +++ b/source/mic.c @@ -20,20 +20,23 @@ u32 bufferSize = 0; Initialize the mic module. @function init @tparam[opt=0x50000] number bufferSize size of the buffer (must be a multiple of 0x1000) +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer/string error code/message */ static int mic_init(lua_State *L) { bufferSize = luaL_optinteger(L, 1, 0x50000); buff = memalign(0x1000, bufferSize); if (buff == NULL) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushstring(L, "Couldn't allocate buffer"); return 2; } Result ret = micInit(buff, bufferSize); if (ret) { free(buff); - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushinteger(L, ret); return 2; } diff --git a/source/ptm.c b/source/ptm.c index 438acde..5401fe3 100644 --- a/source/ptm.c +++ b/source/ptm.c @@ -117,7 +117,9 @@ Setup the new 3DS CPU features (overclock, 4 cores ...) @newonly @function configureNew3DSCPU @tparam boolean enable enable the New3DS CPU features -@treturn boolean `true` if everything went fine +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int ptm_configureNew3DSCPU(lua_State *L) { u8 conf = false; diff --git a/source/qtm.c b/source/qtm.c index 1356d0d..dfa9ca4 100644 --- a/source/qtm.c +++ b/source/qtm.c @@ -22,6 +22,9 @@ static const struct luaL_Reg qtm_methods[]; /*** Initialize the QTM module. @function init +@treturn[1] boolean `true` if everything went fine +@treturn[2] boolean `false` in case of error +@treturn[2] integer error code */ static int qtm_init(lua_State *L) { Result ret = qtmInit(); @@ -121,8 +124,10 @@ Convert QTM coordinates to screen coordinates @tparam number coordinates index @tparam[opt=400] number screenWidth specify a screen width @tparam[opt=320] number screenHeight specify a screen height -@treturn number screen X coordinate -@treturn number screen Y coordinate +@treturn[1] number screen X coordinate +@treturn[1] number screen Y coordinate +@treturn[2] nil in case of error +@treturn[2] string error message */ static int qtm_convertCoordToScreen(lua_State *L) { qtm_userdata *info = luaL_checkudata(L, 1, "LQTM"); @@ -130,7 +135,7 @@ static int qtm_convertCoordToScreen(lua_State *L) { index = index - 1; // Lua index begins at 1 if (index > 3 || index < 0) { lua_pushnil(L); - lua_pushnil(L); + lua_pushstring(L, "Index must be between 1 and 3"); return 2; } float screenWidth = luaL_optnumber(L, 3, 400.0f); diff --git a/source/socket.c b/source/socket.c index 7eb61f8..27fa28f 100644 --- a/source/socket.c +++ b/source/socket.c @@ -109,7 +109,9 @@ static int socket_shutdown(lua_State *L) { /*** Return a TCP socket. @function tcp -@treturn TCPMaster TCP socket +@treturn[1] TCPMaster TCP socket +@treturn[2] nil in case of error +@treturn[2] string error message */ static int socket_tcp(lua_State *L) { socket_userdata *userdata = lua_newuserdata(L, sizeof(*userdata)); @@ -134,7 +136,9 @@ static int socket_tcp(lua_State *L) { /*** Return an UDP socket. @function udp -@treturn UDPMaster UDP socket +@treturn[1] UDPMaster UDP socket +@treturn[2] nil in case of error +@treturn[2] string error message */ static int socket_udp(lua_State *L) { socket_userdata *userdata = lua_newuserdata(L, sizeof(*userdata)); @@ -371,7 +375,9 @@ If no data is avaible, it returns an empty string (non-blocking). "a" to receive everything, "l" to receive the next line, skipping the end of line, "L" to receive the next line, keeping the end of line. -@treturn string data +@treturn[1] string data +@treturn[2] nil in case of error +@treturn[2] integer error code */ static int socket_receive(lua_State *L) { socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); @@ -440,7 +446,9 @@ static int socket_receive(lua_State *L) { Send some data over the TCP socket. @function :send @tparam string data data to send -@treturn number amount of data sent +@treturn[1] number amount of data sent +@treturn[2] nil in case of error +@treturn[2] integer/string error code/message */ static int socket_send(lua_State *L) { socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); diff --git a/source/texture.c b/source/texture.c index df87a22..f995386 100644 --- a/source/texture.c +++ b/source/texture.c @@ -43,7 +43,9 @@ Load a texture from a file. Supported formats: PNG, JPEG, BMP. @tparam string path path to the image file @tparam[opt=PLACE_RAM] number place where to put the loaded texture @tparam[opt=auto] number type type of the image -@treturn texture the loaded texture object +@treturn[1] texture the loaded texture object +@treturn[2] nil in case of error +@treturn[2] string error message */ static int texture_load(lua_State *L) { const char *path = luaL_checkstring(L, 1); @@ -272,7 +274,7 @@ Save a texture to a file. @tparam string filename path to the file to save the texture to @tparam[opt=TYPE_PNG] number type type of the image to save. Can be TYPE_PNG or TYPE_BMP @treturn[1] boolean true on success -@treturn[2] nil +@treturn[2] boolean `false` in case of error @treturn[2] string error message */ static int texture_save(lua_State *L) { @@ -284,7 +286,7 @@ static int texture_save(lua_State *L) { if (type == 0) { // PNG FILE* file = fopen(path, "wb"); if (file == NULL) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushstring(L, "Can open file"); return 2; } @@ -317,7 +319,7 @@ static int texture_save(lua_State *L) { } else if (type == 2) { // BMP u32* buff = malloc(texture->texture->width * texture->texture->height * 4); if (buff == NULL) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushstring(L, "Failed to allocate buffer"); return 2; } @@ -330,13 +332,13 @@ static int texture_save(lua_State *L) { free(buff); } else { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushstring(L, "Not a valid type"); return 2; } if (result == 0) { - lua_pushnil(L); + lua_pushboolean(L, false); lua_pushstring(L, "Failed to save the texture"); return 2; }