From 92a3e69cced57f76c850ba52433dc70dd890aae0 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Fri, 13 Jul 2018 21:10:48 +0900 Subject: [PATCH 01/21] Change requires --- libnx.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnx.nimble b/libnx.nimble index 98fafcf..2692787 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -9,7 +9,7 @@ srcDir = "src" # Deps requires "nim >= 0.18.1", "https://github.com/genotrance/nimgen#head" -requires "https://github.com/jyapayne/switch-build#head" +requires "switch-build >= 0.1.6" task setup, "Download and generate bindings": echo "Building libnx..." From ea55f461b578084d20b8129363f87831c2ce1043 Mon Sep 17 00:00:00 2001 From: Joey Date: Fri, 13 Jul 2018 21:52:03 +0900 Subject: [PATCH 02/21] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dc05a29..7453dff 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Simply run ```bash # Because of a bug in nimble right now, you must install this first! -nimble install "https://github.com/jyapayne/nimgenEx#head" +nimble install nimgen@#head nimble install ``` From a66af737d8f6a232729767bd9781d2c9d7dea020 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 14 Jul 2018 11:07:57 +0900 Subject: [PATCH 03/21] Make nimble windows friendly --- libnx.nimble | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/libnx.nimble b/libnx.nimble index 2692787..2311792 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -1,23 +1,32 @@ # Package -version = "0.1.6" +version = "0.1.7" author = "Joey Payne" description = "Nintendo Switch library libnx for Nim." license = "The Unlicense" srcDir = "src" +import distros + +var prefix = "" +var username = "$USER" +if detectOs(Windows): + prefix = "cmd /c " + username = "%username%" + # Deps requires "nim >= 0.18.1", "https://github.com/genotrance/nimgen#head" requires "switch-build >= 0.1.6" task setup, "Download and generate bindings": echo "Building libnx..." - exec "nimgen libnxGen.cfg" + exec prefix & "nimgen libnxGen.cfg" task buildExamples, "Build switch examples": - exec "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author='jyapayne' --version='1.0.0' examples/helloworld/helloworld.nim" - exec "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author='jyapayne' --version='1.0.0' examples/accounts/account_ex.nim" + echo prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/helloworld/helloworld.nim" + exec prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/helloworld/helloworld.nim" + exec prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/accounts/account_ex.nim" before install: setupTask() From 919ff32b1b0a4b8b786290c3c54e1786c7e7bbd8 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sun, 15 Jul 2018 15:54:01 +0900 Subject: [PATCH 04/21] Add windows support --- config.nims | 4 -- libnx.nimble | 2 +- libnxGen.cfg | 90 +++++++++++++++++++++---------------------- src/libnx/config.nims | 4 -- src/libnx/console.nim | 2 +- 5 files changed, 45 insertions(+), 57 deletions(-) delete mode 100644 config.nims delete mode 100644 src/libnx/config.nims diff --git a/config.nims b/config.nims deleted file mode 100644 index dc3dbe4..0000000 --- a/config.nims +++ /dev/null @@ -1,4 +0,0 @@ -switch("passC", "-I" & thisDir() & "/src/libnx/wrapper/nx/include") -switch("passL", "-specs="&thisDir()&"/src/libnx/wrapper/nx/switch.specs -L"&thisDir()&"/src/libnx/wrapper/nx/lib -lnx") -#switch("passC", "-I$DEVKITPRO/libnx/include") -#switch("passL", "-specs=$DEVKITPRO/libnx/switch.specs -L$DEVKITPRO/libnx/lib -lnx") diff --git a/libnx.nimble b/libnx.nimble index 2311792..2961780 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.7" +version = "0.1.8" author = "Joey Payne" description = "Nintendo Switch library libnx for Nim." license = "The Unlicense" diff --git a/libnxGen.cfg b/libnxGen.cfg index 3a35ed1..0a8ea3c 100644 --- a/libnxGen.cfg +++ b/libnxGen.cfg @@ -22,12 +22,46 @@ filter=lock "${output}/nim.cfg" "${output}/config.nims" +[svc.h] +search = "PACKED" +replace = "" + +search.noreturn = "NORETURN" +replace.noreturn = "" + +defines=true + +[result.h] +defines = true + +[hid.h] +defines = true +search.static_assert = "static_assert" +replace.static_assert = "// static_assert" +search.touch = "touchPosition" +replace.touch = "TouchPosition" + +[ipc.h] +defines = true + +[romfs_dev.h] +search = "romfs_" +replace = "Romfs_" + +[gfx.h] +defines = true + +[ioctl.h] +defines = true +search = "_NV_" +replace = "UNV_" + +search.nv = "__nv_" +replace.nv = "DUnv_" + [n.wildcard] wildcard.1 = "console.h" -rename = "$replace(console=con)" - -search = "_Bool" -replace = "bool" +rename = "$replace(console=cons)" wildcard.2 = "*.h" @@ -68,10 +102,7 @@ replace.cdecl = "cdecl" [n.prepare] git = "https://github.com/switchbrew/libnx" -execute = """ -cd ${output} -make -""" +#execute = """cd ${output}; make;""" [n.post] reset=true @@ -81,56 +112,21 @@ create = """ --path:"../" --path:"../../" """ +noprocess=true [src/libnx/wrapper/config.nims] create = """ switch("passC", "-I" & thisDir() & "/nx/include") switch("passL", "-specs=" & thisDir() & "/nx/switch.specs -L" & thisDir() & "/nx/lib -lnx") """ - -[result.h] -defines = true - -[hid.h] -defines = true -search.static_assert = "static_assert" -replace.static_assert = "// static_assert" -search.touch = "touchPosition" -replace.touch = "TouchPosition" - -[ipc.h] -defines = true - -[romfs_dev.h] -search = "romfs_" -replace = "Romfs_" - -[gfx.h] -defines = true - -[ioctl.h] -defines = true -search = "_NV_" -replace = "UNV_" - -search.nv = "__nv_" -replace.nv = "DUnv_" - -[svc.h] -search = "PACKED" -replace = "" - -search.noreturn = "NORETURN" -replace.noreturn = "" - -defines=true -pipe = "vim -es +'g/^static inline.*{/norm f{d%r;' -es +%print -es +q! $file" +noprocess=true [switch.h] preprocess = true defines = true recurse = true + [types.nim] search.o = " uint8*" prepend.o = """ @@ -313,7 +309,7 @@ prepend.o = """ import libnx/ext/integer128 """ -[con.nim] +[cons.nim] search.o = "type\n" prepend.o = """ diff --git a/src/libnx/config.nims b/src/libnx/config.nims deleted file mode 100644 index b83d14a..0000000 --- a/src/libnx/config.nims +++ /dev/null @@ -1,4 +0,0 @@ -switch("passC", "-I" & thisDir() & "/wrapper/nx/include") -switch("passL", "-specs=" & thisDir() & "/wrapper/nx/switch.specs -L" & thisDir() & "/wrapper/nx/lib -lnx") -#switch("passC", "-I$DEVKITPRO/libnx/include") -#switch("passL", "-specs=$DEVKITPRO/libnx/switch.specs -L$DEVKITPRO/libnx/lib -lnx") diff --git a/src/libnx/console.nim b/src/libnx/console.nim index 3747d88..2b76cb6 100644 --- a/src/libnx/console.nim +++ b/src/libnx/console.nim @@ -1,4 +1,4 @@ -import libnx/wrapper/con +import libnx/wrapper/cons import libnx/utils import macros, strutils From 8e556a448b36431f85f67c430013c76b711de782 Mon Sep 17 00:00:00 2001 From: Joey Date: Sun, 15 Jul 2018 17:23:02 +0900 Subject: [PATCH 05/21] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 7453dff..3cf5631 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,9 @@ Libnx ported to the Nim programming language. You will need a nim compiler with You also must have DevkitPro and switch (libnx) libraries for [Mac and Linux](https://github.com/devkitPro/pacman/releases) or [Windows](https://github.com/devkitPro/installer/releases) installed. The DEVKITPRO environment variable must also exist and point to a directory with the following structure: -- `DEVKITPRO/portlibs/switch/lib` - `DEVKITPRO/libnx/lib` -- `DEVKITPRO/portlibs/switch/include` - `DEVKITPRO/libnx/include` - ##Install Simply run From 0efc49828645ea6a9f09ddab6ead0f3c383f3e9d Mon Sep 17 00:00:00 2001 From: Joey Date: Sun, 15 Jul 2018 17:23:31 +0900 Subject: [PATCH 06/21] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3cf5631..bb016b7 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ You also must have DevkitPro and switch (libnx) libraries for [Mac and Linux](ht - `DEVKITPRO/libnx/lib` - `DEVKITPRO/libnx/include` -##Install + +## Install Simply run From 1955c8d3ec4fc04e4972c81715a9cdef003b44a9 Mon Sep 17 00:00:00 2001 From: Joey Date: Sun, 15 Jul 2018 17:39:33 +0900 Subject: [PATCH 07/21] Update libnx.nimble --- libnx.nimble | 1 - 1 file changed, 1 deletion(-) diff --git a/libnx.nimble b/libnx.nimble index 2961780..a64fa29 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -24,7 +24,6 @@ task setup, "Download and generate bindings": exec prefix & "nimgen libnxGen.cfg" task buildExamples, "Build switch examples": - echo prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/helloworld/helloworld.nim" exec prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/helloworld/helloworld.nim" exec prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/accounts/account_ex.nim" From 626b66b8d7a8fab50c5b917a9b828d8b2e362acc Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Wed, 18 Jul 2018 11:04:09 +0900 Subject: [PATCH 08/21] Fix issues with latest Nim devel compiler --- libnxGen.cfg | 3 ++- src/libnx/account.nim | 22 +++++++++++----------- src/libnx/console.nim | 4 ++-- src/libnx/input.nim | 2 +- src/libnx/results.nim | 35 ++++++++++++----------------------- src/libnx/utils.nim | 11 ++++++++--- 6 files changed, 36 insertions(+), 41 deletions(-) diff --git a/libnxGen.cfg b/libnxGen.cfg index 0a8ea3c..a8f8a94 100644 --- a/libnxGen.cfg +++ b/libnxGen.cfg @@ -102,7 +102,8 @@ replace.cdecl = "cdecl" [n.prepare] git = "https://github.com/switchbrew/libnx" -#execute = """cd ${output}; make;""" +execute.lin = """cd ${output}; make;""" +execute.mac = """cd ${output}; make;""" [n.post] reset=true diff --git a/src/libnx/account.nim b/src/libnx/account.nim index 4cc63ee..aa46b36 100644 --- a/src/libnx/account.nim +++ b/src/libnx/account.nim @@ -53,7 +53,7 @@ proc init() = if code.failed: raiseEx( AccountInitError, - "Error, account api could not be initialized: " & code.description + "Error, account api could not be initialized", code ) enabled = true @@ -88,7 +88,7 @@ proc getImageSize*(profile: AccountProfile): int = result = 0 let code = accountProfileGetImageSize(profile.unsafeAddr, res.addr).newResult if code.failed: - raiseEx(AccountImageSizeError, "Error, could not get image size: " & code.description) + raiseEx(AccountImageSizeError, "Error, could not get image size", code) result = res.int @@ -99,7 +99,7 @@ proc imageSize*(user: User): int = let res = accountProfileGetImageSize(prof.addr, size.addr).newResult if res.failed: - raiseEx(AccountImageSizeError, "Error, could not get image size: " & res.description) + raiseEx(AccountImageSizeError, "Error, could not get image size", res) result = size.int @@ -108,7 +108,7 @@ proc getProfileHelper(userID: u128): AccountProfile = let res = accountGetProfile(result.addr, userID).newResult if res.failed: - raiseEx(AccountUserProfileError, "Error, could not get user profile: " & res.description) + raiseEx(AccountUserProfileError, "Error, could not get user profile", res) proc loadImage*(user: User): AccountImage = @@ -128,7 +128,7 @@ proc loadImage*(user: User): AccountImage = if res.failed: prof.close() - raiseEx(AccountImageLoadError, "Error, could not load image: " & res.description) + raiseEx(AccountImageLoadError, "Error, could not load image", res) prof.close() @@ -152,7 +152,7 @@ proc getUser*(userID: u128): User = let res = accountProfileGet(prof.addr, userData.addr, profBase.addr).newResult if res.failed: - raiseEx(AccountUserDataError, "Error, could not get user data: " & res.description) + raiseEx(AccountUserDataError, "Error, could not get user data", res) result.username = profBase.username.join("") result.lastEdited = profBase.lastEditTimestamp @@ -173,7 +173,7 @@ proc getActiveUser*(): User = var res = accountGetActiveUser(userID.addr, selected.addr).newResult if res.failed: - raiseEx(AccountActiveUserError, "Error, could not get active user ID: " & res.description) + raiseEx(AccountActiveUserError, "Error, could not get active user ID", res) if not selected: raiseEx(AccountUserNotSelectedError, "No user currently selected!") @@ -189,7 +189,7 @@ proc getActiveUser*(): User = res = accountProfileGet(prof.addr, userData.addr, profBase.addr).newResult if res.failed: - raiseEx(AccountUserDataError, "Error, could not get user data: " & res.description) + raiseEx(AccountUserDataError, "Error, could not get user data", res) result.username = profBase.username.join("") result.lastEdited = profBase.lastEditTimestamp @@ -209,7 +209,7 @@ proc getProfile*(user: User): Profile = if res.failed: raiseEx( AccountUserProfileError, - "Error, could not get account profile: " & res.description + "Error, could not get account profile", res ) result.service = newService(prof.s) @@ -224,7 +224,7 @@ proc getUserCount*(): int32 = if res.failed: raiseEx( AccountUserCountError, - "Error, could not get user count: " & res.description + "Error, could not get user count", res ) result = count @@ -247,7 +247,7 @@ proc listAllUsers*(): seq[User] = if res.failed: raiseEx( AccountUserListError, - "Error, could not list users: " & res.description + "Error, could not list users", res ) for i in 0 ..< usersReturned.int: diff --git a/src/libnx/console.nim b/src/libnx/console.nim index 2b76cb6..39e7ae2 100644 --- a/src/libnx/console.nim +++ b/src/libnx/console.nim @@ -1,6 +1,6 @@ +import macros, strutils, sets import libnx/wrapper/cons import libnx/utils -import macros, strutils type @@ -64,7 +64,7 @@ proc toConsole(pconsole: ptr PrintConsole): Console = result.tabSize = pconsole.tabSize result.fg = pconsole.fg result.bg = pconsole.bg - result.flags = {} + result.flags = initSet[Style]() result.pcon.PrintChar = proc (con: pointer, c: cint): bool {.cdecl.} = let console = cast[ptr PrintConsole](con).toConsole() diff --git a/src/libnx/input.nim b/src/libnx/input.nim index af28149..15b93bb 100644 --- a/src/libnx/input.nim +++ b/src/libnx/input.nim @@ -421,7 +421,7 @@ proc initializeVibrationDevices*( if rc.failed: raiseEx( VibrationInitError, - "Could not init vibration for controller #$ with type #$" % + "Could not init vibration for controller $# with type $#" % [$controller, $ctype] ) for i in 0 ..< numDevices: diff --git a/src/libnx/results.nim b/src/libnx/results.nim index af76ef6..3c1ae26 100644 --- a/src/libnx/results.nim +++ b/src/libnx/results.nim @@ -1,3 +1,4 @@ +import strutils import libnx/wrapper/result, libnx/wrapper/types @@ -5,7 +6,7 @@ import type Module {.pure.} = enum - Invalid = 0, # This is just so the nim compiler is happy + Success = 0, Kernel = 1, Libnx = 345, LibnxNvidia = 348 @@ -13,16 +14,8 @@ type Result* = ref object code*: uint32 module*: string - description*: string - case kind*: Module - of Module.Kernel: - kernelError*: KernelError - of Module.Libnx: - libnxError*: LibnxError - of Module.LibnxNvidia: - libnxNvidiaError*: LibnxNvidiaError - else: - discard + kind*: Module + error*: string KernelError* {.pure.} = enum Timeout = 117 @@ -98,8 +91,6 @@ proc newResult*(code: uint32): Result = result = new(Result) result.code = code - if code.R_SUCCEEDED: - return let moduleCode = code.R_MODULE let module = Module(moduleCode) @@ -108,20 +99,18 @@ proc newResult*(code: uint32): Result = let descCode = code.R_DESCRIPTION - var description = "" + var error = "Unknown error: module: $# description: $#" % + [$moduleCode, $descCode] try: case module - of Module.Invalid: - discard + of Module.Success: + return of Module.Kernel: - result.kernelError = KernelError(descCode) - description = $result.kernelError + error = $KernelError(descCode) of Module.Libnx: - result.libnxError = LibnxError(descCode) - description = $result.libnxError + error = $LibnxError(descCode) of Module.LibnxNvidia: - result.libnxNvidiaError = LibnxNvidiaError(descCode) - description = $result.libnxNvidiaError + error = $LibnxNvidiaError(descCode) except: echo "Converting to result failed: " & getCurrentExceptionMsg() echo "Code: " & $code @@ -130,4 +119,4 @@ proc newResult*(code: uint32): Result = echo "DescCode: " & $descCode result.module = $module - result.description = description + result.error = error diff --git a/src/libnx/utils.nim b/src/libnx/utils.nim index d18b135..dc11285 100644 --- a/src/libnx/utils.nim +++ b/src/libnx/utils.nim @@ -1,6 +1,11 @@ -import macros, strutils, math +import macros, strutils, math, hashes import libnx/results + +proc hash*(obj: SomeOrdinal): Hash = + ## Allow hashing of enums with holes + result = cast[int](obj) + proc size*(enumTy: typedesc): int = # Returns the number of items in a bit set enum log2(float64(enumTy.high)).int + 1 @@ -90,8 +95,8 @@ type data*: ptr UncheckedArray[T] template raiseEx*(ty: untyped, message: string, rc: Result): untyped = - ## Raise an exception with a result description - raise newException(ty, message & ": " & rc.description) + ## Raise an exception with a result error + raise newException(ty, message & ": " & rc.error) template raiseEx*(ty: untyped, message: string): untyped = ## Raise an exception From 3fff14bd0b5bcaca079b0a6f0e14ad10fe6d43f2 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Wed, 18 Jul 2018 11:07:52 +0900 Subject: [PATCH 09/21] Update versions --- libnx.nimble | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libnx.nimble b/libnx.nimble index a64fa29..24b4195 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.8" +version = "0.1.9" author = "Joey Payne" description = "Nintendo Switch library libnx for Nim." license = "The Unlicense" @@ -17,7 +17,7 @@ if detectOs(Windows): # Deps requires "nim >= 0.18.1", "https://github.com/genotrance/nimgen#head" -requires "switch-build >= 0.1.6" +requires "switch_build >= 0.1.3" task setup, "Download and generate bindings": echo "Building libnx..." From 291abf324942d06c4e3f0c6a13226d76eef52a52 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Wed, 18 Jul 2018 11:31:13 +0900 Subject: [PATCH 10/21] Add template for error handling and fix console printing --- examples/accounts/account_ex.nim | 4 +- examples/helloworld/helloworld.nim | 4 +- src/libnx/app.nim | 36 +++++++- src/libnx/console.nim | 40 ++++----- src/libnx/input.nim | 131 ----------------------------- 5 files changed, 57 insertions(+), 158 deletions(-) diff --git a/examples/accounts/account_ex.nim b/examples/accounts/account_ex.nim index b73216c..b6a15ab 100644 --- a/examples/accounts/account_ex.nim +++ b/examples/accounts/account_ex.nim @@ -2,7 +2,7 @@ import sets, strutils import libnx/[graphics, console, account, input, app] import libnx/ext/integer128 -proc main() = +mainFunction: graphics.initDefault() console.init() @@ -43,5 +43,3 @@ proc main() = if ControllerKey.Plus in keysDown: break - -main() diff --git a/examples/helloworld/helloworld.nim b/examples/helloworld/helloworld.nim index a56a303..3796e3b 100644 --- a/examples/helloworld/helloworld.nim +++ b/examples/helloworld/helloworld.nim @@ -2,7 +2,7 @@ import sets import libnx/[graphics, console, app, input] -proc main() = +mainFunction: initDefault() console.init() @@ -15,5 +15,3 @@ proc main() = if ControllerKey.Plus in keysDown: break - -main() diff --git a/src/libnx/app.nim b/src/libnx/app.nim index e94e192..7934d59 100644 --- a/src/libnx/app.nim +++ b/src/libnx/app.nim @@ -1,8 +1,40 @@ -import libnx/graphics, libnx/wrapper/hid, libnx/wrapper/applet +import libnx/graphics, libnx/input, libnx/wrapper/applet + +template mainFunction*(code: untyped): untyped = + proc main() = + try: + code + except: + let e = getCurrentException() + echo "" + echo e.getStackTrace() + echo getCurrentExceptionMsg() + echo "" + echo "Your program has crashed. Press + on Controller 1 to exit safely." + echo "" + + while appletMainLoop(): + scanInput() + + let keysDown = keysDown(Controller.P1_AUTO) + + if ControllerKey.Plus in keysDown: + break + + flushBuffers() + swapBuffers() + waitForVSync() + try: + graphics.exit() + quit(0) + except: + quit(0) + + main() template mainLoop*(code: untyped): untyped = while appletMainLoop(): - hidScanInput() + scanInput() code diff --git a/src/libnx/console.nim b/src/libnx/console.nim index 39e7ae2..b075146 100644 --- a/src/libnx/console.nim +++ b/src/libnx/console.nim @@ -37,18 +37,19 @@ type tabSize*: int fg*: int bg*: int - flags*: set[Style] + flags*: HashSet[Style] printCharCallback*: PrintCallback initialised*: bool DebugDevice* {.size: sizeof(cint), pure.} = enum Null, Service, Console, ThreeDMOO +var currentConsole {.threadvar.}: Console + proc toConsole(pconsole: ptr PrintConsole): Console = result = new(Console) result.pcon = pconsole result.font = pconsole.font - result.cursorX = pconsole.cursorX result.frameBuffer = cast[ptr UncheckedArray[uint32]](pconsole.frameBuffer) result.frameBuffer2 = cast[ptr UncheckedArray[uint32]](pconsole.frameBuffer2) result.cursorX = pconsole.cursorX @@ -86,20 +87,21 @@ proc setWindow*(console: Console, x, y, width, height: int) = console.windowY = console.pcon.windowY.int proc getDefault*(): Console = + ## Gets the default console with default values let pcon = consoleGetDefault() result = pcon.toConsole() proc select*(console: Console): Console = - consoleSelect(console.pcon).toConsole() + currentConsole = consoleInit(console.pcon).toConsole() + return currentConsole -proc init*(console: Console = nil): Console {.discardable.} = - var newCon = console - if newCon.isNil: - newCon = new(Console) +proc init*(console: Console): Console {.discardable.} = + currentConsole = consoleInit(console.pcon).toConsole() + return currentConsole - let pcon = consoleInit(newCon.pcon) - newCon = pcon.toConsole() - return newCon +proc init*(): Console {.discardable.} = + currentConsole = consoleInit(nil).toConsole() + return currentConsole proc debugInit*(device: DebugDevice) = if device == ThreeDMOO: @@ -110,17 +112,17 @@ proc debugInit*(device: DebugDevice) = proc clear*() = consoleClear() -var printPos: tuple[row: int, col: int] = (0, 0) - -proc setPrintPos*(pos: tuple[row: int, col: int]) = - printPos = pos - proc printAt*(pos: tuple[row: int, col: int], args: varargs[string, `$`]) = - setPrintPos(pos) - echo ("\x1b[$#;$#H" % [$pos[0], $pos[1]]), args.join("") + echo CONSOLE_ESC("2K"), (CONSOLE_ESC("$#;$#H") % [$pos.row, $pos.col]), args.join("") + currentConsole.pcon.cursorX = pos.col.int32 + currentConsole.pcon.cursorY = pos.row.int32 + currentConsole.cursorX = pos.col + currentConsole.cursorY = pos.row proc print*(args: varargs[string, `$`]) = - ## Will print at the previously set printPos using ``printAt`` or ``setPrintPos`` + ## Will print at the previously set printPos using ``printAt`` ## and then increment the row by one + let + pcon = currentConsole.pcon + printPos = (pcon.cursorY.int+1, pcon.cursorX.int) printAt printPos, args - printPos.row += 1 diff --git a/src/libnx/input.nim b/src/libnx/input.nim index 15b93bb..68f5916 100644 --- a/src/libnx/input.nim +++ b/src/libnx/input.nim @@ -26,136 +26,6 @@ type VibrationInitError* = object of InputError ControllerMergeError* = object of InputError - ####################### Shared memory data ################################ - TouchScreenHeader* = ref object - timestampTicks*: uint64 - numEntries*: uint64 - latestEntry*: uint64 - maxEntryIndex*: uint64 - timestamp*: uint64 - - TouchScreenEntryHeader* = ref object - timestamp*: uint64 - numTouches*: uint64 - - TouchScreenEntryTouch* = ref object - timestamp*: uint64 - padding*: uint32 - touchIndex*: uint32 - x*: uint32 - y*: uint32 - diameterX*: uint32 - diameterY*: uint32 - angle*: uint32 - padding2*: uint32 - - TouchScreenEntry* = ref object - header*: TouchScreenEntryHeader - touches*: Buffer[HidTouchScreenEntryTouch] - unk*: uint64 - - TouchScreen* = ref object - header*: TouchScreenHeader - entries*: Buffer[HidTouchScreenEntry] - padding*: Buffer[uint8] - - MouseHeader* = ref object - timestampTicks*: uint64 - numEntries*: uint64 - latestEntry*: uint64 - maxEntryIndex*: uint64 - - -type - MouseEntry* = ref object - timestamp*: uint64 - timestamp2*: uint64 - position*: MousePosition - buttons*: uint64 - - -type - Mouse* = ref object - header*: MouseHeader - entries*: Buffer[MouseEntry] - padding*: Buffer[uint8] - - KeyboardHeader* = ref object - timestampTicks*: uint64 - numEntries*: uint64 - latestEntry*: uint64 - maxEntryIndex*: uint64 - - KeyboardEntry* = ref object - timestamp*: uint64 - timestamp2*: uint64 - modifier*: uint64 - keys*: Buffer[uint32] - - - KeyboardSection* = ref object - header*: KeyboardHeader - entries*: array[17, KeyboardEntry] - padding*: array[0x00000028, uint8] - - ControllerMAC* = object - timestamp*: uint64 - mac*: array[0x00000008, uint8] - unk*: uint64 - timestamp2*: uint64 - - ControllerHeader* = object - `type`* {.importc: "type".}: uint32 - isHalf* {.importc: "isHalf".}: uint32 - singleColorsDescriptor* {.importc: "singleColorsDescriptor".}: uint32 - singleColorBody* {.importc: "singleColorBody".}: uint32 - singleColorButtons* {.importc: "singleColorButtons".}: uint32 - splitColorsDescriptor* {.importc: "splitColorsDescriptor".}: uint32 - leftColorBody* {.importc: "leftColorBody".}: uint32 - leftColorButtons* {.importc: "leftColorButtons".}: uint32 - rightColorBody* {.importc: "rightColorBody".}: uint32 - rightColorbuttons* {.importc: "rightColorbuttons".}: uint32 - - - ControllerLayoutHeader* = object - timestampTicks* {.importc: "timestampTicks".}: uint64 - numEntries* {.importc: "numEntries".}: uint64 - latestEntry* {.importc: "latestEntry".}: uint64 - maxEntryIndex* {.importc: "maxEntryIndex".}: uint64 - - - HidControllerInputEntry* = object - timestamp* {.importc: "timestamp".}: uint64 - timestamp_2* {.importc: "timestamp_2".}: uint64 - buttons* {.importc: "buttons".}: uint64 - joysticks* {.importc: "joysticks".}: array[JOYSTICK_NUM_STICKS, JoystickPosition] - connectionState* {.importc: "connectionState".}: uint64 - - - ControllerLayoutSection* = ref object - header*: ControllerLayoutHeader - entries* {.importc: "entries".}: array[17, HidControllerInputEntry] - - - ControllerSection* = ref object - header* {.importc: "header".}: ControllerHeader - layouts* {.importc: "layouts".}: Buffer[ControllerLayoutSection] - unk_1*: Buffer[uint8] - macLeft*: HidControllerMAC - macRight*: HidControllerMAC - unk_2* {.importc: "unk_2".}: array[0x00000DF8, uint8] - - - InputSharedMemory* = ref object - header*: Buffer[uint8] - touchscreen*: TouchScreen - mouse*: Mouse - keyboard*: KeyboardSection - controllerSerials*: Buffer[uint8] - controllers*: Buffer[ControllerSection] - - ####################### END Shared memory data ################################ - VibrationDeviceInfo* = HidVibrationDeviceInfo VibrationValue* = HidVibrationValue @@ -165,7 +35,6 @@ type JoyconMode {.pure.} = enum Single, Dual - proc init*(): Result = hidInitialize().newResult proc exit*() = hidExit() proc reset*() = hidReset() From fb136ae95aa44cff4fcbcd0c98db9d79de1017ea Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Wed, 18 Jul 2018 11:31:57 +0900 Subject: [PATCH 11/21] Bump version --- libnx.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnx.nimble b/libnx.nimble index 24b4195..6f6915e 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.9" +version = "0.2.0" author = "Joey Payne" description = "Nintendo Switch library libnx for Nim." license = "The Unlicense" From 0bddb6f1ef710defa799a3aebc6bcc37f66d5f25 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Fri, 20 Jul 2018 17:00:39 +0900 Subject: [PATCH 12/21] Fix more windows issues --- libnx.nimble | 8 ++++---- libnxGen.cfg | 5 ++--- src/nim.cfg | 2 -- 3 files changed, 6 insertions(+), 9 deletions(-) delete mode 100644 src/nim.cfg diff --git a/libnx.nimble b/libnx.nimble index 6f6915e..196da2d 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -10,10 +10,10 @@ srcDir = "src" import distros var prefix = "" -var username = "$USER" +var username = getEnv("USER") if detectOs(Windows): prefix = "cmd /c " - username = "%username%" + username = getEnv("USERNAME") # Deps requires "nim >= 0.18.1", "https://github.com/genotrance/nimgen#head" @@ -24,8 +24,8 @@ task setup, "Download and generate bindings": exec prefix & "nimgen libnxGen.cfg" task buildExamples, "Build switch examples": - exec prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/helloworld/helloworld.nim" - exec prefix & "switch_build --libnxPath='" & thisDir() & "/src/libnx/wrapper/nx/' --author=\"" & username & "\" --version='1.0.0' examples/accounts/account_ex.nim" + exec prefix & "switch_build --libnxPath=\"" & thisDir() & "/src/libnx/wrapper/nx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/helloworld/helloworld.nim" + exec prefix & "switch_build --libnxPath=\"" & thisDir() & "/src/libnx/wrapper/nx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/accounts/account_ex.nim" before install: setupTask() diff --git a/libnxGen.cfg b/libnxGen.cfg index a8f8a94..f13d50d 100644 --- a/libnxGen.cfg +++ b/libnxGen.cfg @@ -16,7 +16,6 @@ filter=lock "${output}/nx/include/switch/runtime/util" "${output}/nx/include/switch/runtime/devices" "${DEVKITPRO}/devkitA64/aarch64-none-elf/include/" -"${DEVKITA64}/aarch64-none-elf/include/" [n.exclude] "${output}/nim.cfg" @@ -102,8 +101,8 @@ replace.cdecl = "cdecl" [n.prepare] git = "https://github.com/switchbrew/libnx" -execute.lin = """cd ${output}; make;""" -execute.mac = """cd ${output}; make;""" +execute-lin = """cd ${output}; make;""" +execute-mac = """cd ${output}; make;""" [n.post] reset=true diff --git a/src/nim.cfg b/src/nim.cfg deleted file mode 100644 index 72c20da..0000000 --- a/src/nim.cfg +++ /dev/null @@ -1,2 +0,0 @@ - ---path="../ext" From 65299fc325aeff0d586382a879f4cb3fc6051ea7 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Fri, 20 Jul 2018 17:02:02 +0900 Subject: [PATCH 13/21] Bump version --- libnx.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnx.nimble b/libnx.nimble index 196da2d..e68ac3e 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.0" +version = "0.2.1" author = "Joey Payne" description = "Nintendo Switch library libnx for Nim." license = "The Unlicense" From 6d465e4de1dc1fdaa971f6fb23f6c21af070d34f Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Fri, 20 Jul 2018 17:12:06 +0900 Subject: [PATCH 14/21] Make buildexamples use dkp for windows --- libnx.nimble | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libnx.nimble b/libnx.nimble index e68ac3e..b8e3758 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -16,7 +16,7 @@ if detectOs(Windows): username = getEnv("USERNAME") # Deps -requires "nim >= 0.18.1", "https://github.com/genotrance/nimgen#head" +requires "nim >= 0.18.1", "nimgen#dc9943a22c9c8f6a5a6a92f0055e1de4dfaf87d2" requires "switch_build >= 0.1.3" task setup, "Download and generate bindings": @@ -24,8 +24,16 @@ task setup, "Download and generate bindings": exec prefix & "nimgen libnxGen.cfg" task buildExamples, "Build switch examples": - exec prefix & "switch_build --libnxPath=\"" & thisDir() & "/src/libnx/wrapper/nx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/helloworld/helloworld.nim" - exec prefix & "switch_build --libnxPath=\"" & thisDir() & "/src/libnx/wrapper/nx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/accounts/account_ex.nim" + if detectOs(Windows): + let devkitPath = getEnv("DEVKITPRO") + if devkitPath == "" or not dirExists(devkitPath): + echo "You must set the DEVKITPRO environment variable to something valid!" + else: + exec prefix & "switch_build --libnxPath=\"" & devkitPath & "/libnx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/helloworld/helloworld.nim" + exec prefix & "switch_build --libnxPath=\"" & devkitPath & "/libnx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/accounts/account_ex.nim" + else: + exec prefix & "switch_build --libnxPath=\"" & thisDir() & "/src/libnx/wrapper/nx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/helloworld/helloworld.nim" + exec prefix & "switch_build --libnxPath=\"" & thisDir() & "/src/libnx/wrapper/nx/\" --author=\"" & username & "\" --version=\"1.0.0\" examples/accounts/account_ex.nim" before install: setupTask() From 075e6d34b09d40e13b483cad13ceeeda1b862c8c Mon Sep 17 00:00:00 2001 From: Joey Date: Fri, 20 Jul 2018 17:15:18 +0900 Subject: [PATCH 15/21] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bb016b7..8070620 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # nim-libnx -Libnx ported to the Nim programming language. You will need a nim compiler with Nintendo switch support which can be found [here](https://github.com/jyapayne/Nim/tree/nintendo_switch_support) until the PR is accepted. +Libnx ported to the Nim programming language. You will need a Nim compiler with Nintendo switch support which can be found in the latest devel branch of the Nim compiler. You also must have DevkitPro and switch (libnx) libraries for [Mac and Linux](https://github.com/devkitPro/pacman/releases) or [Windows](https://github.com/devkitPro/installer/releases) installed. The DEVKITPRO environment variable must also exist and point to a directory with the following structure: From 09b93cef6227a7457aeca16768eaac3515941d63 Mon Sep 17 00:00:00 2001 From: Joey Date: Fri, 20 Jul 2018 17:19:24 +0900 Subject: [PATCH 16/21] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 8070620..062feca 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,13 @@ You also must have DevkitPro and switch (libnx) libraries for [Mac and Linux](ht - `DEVKITPRO/libnx/lib` - `DEVKITPRO/libnx/include` +OR you must specify a valid libnx path and/or devkitpro path to the `switch_build` utility: + +```bash +switch_build --libnxPath:"C:\devkitPro\libnx" --author:"Joey" --version:"1.0.0" .\examples\accounts\account_ex.nim +# OR +switch_build --devkitProPath:"C:\devkitPro" --author:"Joey" --version:"1.0.0" .\examples\accounts\account_ex.nim +``` ## Install From 8937928b3e8568649cbfb5ae3f8f14e372ea98f1 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Thu, 27 Sep 2018 20:17:30 -0600 Subject: [PATCH 17/21] Update config file for new libnx --- libnxGen.cfg | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libnxGen.cfg b/libnxGen.cfg index f13d50d..e2e4ecf 100644 --- a/libnxGen.cfg +++ b/libnxGen.cfg @@ -8,13 +8,14 @@ filter=lock "${output}/nx/include" "${output}/nx/include/switch" "${output}/nx/include/switch/arm" -"${output}/nx/include/switch/kernel" -"${output}/nx/include/switch/services" "${output}/nx/include/switch/audio" -"${output}/nx/include/switch/gfx" +"${output}/nx/include/switch/display" +"${output}/nx/include/switch/kernel" +"${output}/nx/include/switch/nvidia" "${output}/nx/include/switch/runtime" "${output}/nx/include/switch/runtime/util" "${output}/nx/include/switch/runtime/devices" +"${output}/nx/include/switch/services" "${DEVKITPRO}/devkitA64/aarch64-none-elf/include/" [n.exclude] From 4b7874c83704f3c0afc48448d4347d9e8fecb036 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 29 Sep 2018 11:10:42 -0600 Subject: [PATCH 18/21] Fix more build script issues --- libnxGen.cfg | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libnxGen.cfg b/libnxGen.cfg index e2e4ecf..4404075 100644 --- a/libnxGen.cfg +++ b/libnxGen.cfg @@ -34,6 +34,9 @@ defines=true [result.h] defines = true +[thread_context.h] +preprocess = false + [hid.h] defines = true search.static_assert = "static_assert" @@ -166,6 +169,26 @@ import libnx/ext/integer128 template BIT*(n): auto = (1.uint shl n) """ +[thread_context.nim] +search.timport = "../types" +replace.timport = "libnx/wrapper/types" + +prepend.o = """ +import libnx/ext/integer128 +""" + +search.cpuall = "RegisterGroup_CpuAll =" +comment.cpuall = 1 + +search.fpugprs = "RegisterGroup_FpuGprs =" +prepend.fpugprs = "RegisterGroup_CpuAll = BIT(0) or BIT(1), ## /< All CPU registers." + +search.fpuall = "RegisterGroup_FpuGprs or RegisterGroup_FpuSprs" +replace.fpuall = "BIT(2) or BIT(3)" + +search.groupall = "RegisterGroup_CpuAll or RegisterGroup_FpuAll" +replace.groupall = "BIT(0) or BIT(1) or BIT(2) or BIT(3)" + [svc.nim] search.timport = "../types" replace.timport = "libnx/wrapper/types" @@ -310,6 +333,11 @@ prepend.o = """ import libnx/ext/integer128 """ +[fence.nim] +prepend.o = """ +import libnx/wrapper/types +""" + [cons.nim] search.o = "type\n" prepend.o = """ @@ -346,11 +374,16 @@ replace.threedmoo = "debugDevice_3DMOO*" search.timport = "../types" replace.timport = "libnx/wrapper/types" +search.fimport = "../nvidia/fence" +replace.fimport = "libnx/wrapper/fence" + [romfs_dev.nim] search.timport = "../types" replace.timport = "libnx/wrapper/types" search.servimport = "../services/" replace.servimport = "libnx/wrapper/" +search.o = "../libnx" +replace.o = "libnx" [n.sourcefile] "${output}/*.nim" From 71d0f65903611a81519f10c4fb7645f7e21b7dd4 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 29 Sep 2018 11:11:32 -0600 Subject: [PATCH 19/21] Fix graphics init error --- src/libnx/graphics.nim | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/libnx/graphics.nim b/src/libnx/graphics.nim index 668ab0f..eedc9a2 100644 --- a/src/libnx/graphics.nim +++ b/src/libnx/graphics.nim @@ -2,10 +2,12 @@ import strutils import libnx/wrapper/types, libnx/wrapper/gfx, + libnx/results, libnx/utils type GraphicsError* = object of Exception + GraphicsInitError* = object of GraphicsError InitResolutionError* = object of GraphicsError CropBoundsError* = object of GraphicsError @@ -53,7 +55,12 @@ var enabled = false ## proc initDefault*() = if not enabled: - gfxInitDefault() + let code = gfxInitDefault().newResult + if code.failed: + raiseEx( + GraphicsInitError, + "Error, graphics could not be initialized", code + ) enabled = true ## * @@ -184,10 +191,6 @@ proc getFramebuffer*(): Framebuffer = ## / Sets the \ref GfxMode. proc setMode*(mode: GfxMode) = gfxSetMode(gfx.GfxMode(mode)) -## / Controls whether a vertical-flip is done when determining the pixel-offset within -## the actual framebuffer. By default this is enabled. -proc setDrawFlip*(enabled: bool) = gfxSetDrawFlip(enabled) - ## / Configures transform. See the NATIVE_WINDOW_TRANSFORM_* enums in buffer_producer.h. ## The default is NATIVE_WINDOW_TRANSFORM_FLIP_V. proc configureTransform*(transform: BufferTransform) = From 497a07902051bf2fefe6faf03063c8f9f2dd399d Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 29 Sep 2018 11:12:21 -0600 Subject: [PATCH 20/21] Version bump --- libnx.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnx.nimble b/libnx.nimble index b8e3758..6724840 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.1" +version = "0.2.2" author = "Joey Payne" description = "Nintendo Switch library libnx for Nim." license = "The Unlicense" From 01dda9d2ce50f3a2fc5b2601f43ffe8e533aa01d Mon Sep 17 00:00:00 2001 From: Joey Date: Sat, 7 Sep 2019 08:38:04 -0600 Subject: [PATCH 21/21] Update README.md --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 062feca..3c398da 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,17 @@ # nim-libnx Libnx ported to the Nim programming language. You will need a Nim compiler with Nintendo switch support which can be found in the latest devel branch of the Nim compiler. -You also must have DevkitPro and switch (libnx) libraries for [Mac and Linux](https://github.com/devkitPro/pacman/releases) or [Windows](https://github.com/devkitPro/installer/releases) installed. The DEVKITPRO environment variable must also exist and point to a directory with the following structure: +You also must have DevkitPro and switch (libnx) libraries for [Mac and Linux](https://github.com/devkitPro/pacman/releases) or [Windows](https://github.com/devkitPro/installer/releases) installed. + +From dkp-pacman, the switch libraries can be installed with: + +``` +dkp-pacman -Syu +dkp-pacman -S switch-dev +## When it asks for installation options, choose the default which will install everything +``` + +The DEVKITPRO environment variable must also exist and point to a directory with the following structure: - `DEVKITPRO/libnx/lib` - `DEVKITPRO/libnx/include`