Turn all consoles into devices, allowing Linux VT-like functionality #70
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Feature additions
devoptab_tfor every new console created after the first, namedconN, whereNis a monotonically increasing integer starting at 0 for the default ( console.open()console devices by passingconN:as the filename (the colon is important!)FILE *streams API and C++'sstd::fstreamclasses for opening consoles (make sure to disable buffering!)write()on an opened console file descriptor will simply setcurrentConsoleinconsole.cto the attached console passed throughdevoptab_t.deviceData.currentConsoleis restored after all characters are printed, so that normal use ofstdout/stderrcan continue.Feature demo/test
Initialize two consoles on different bg layers AND map bases, write a simple loop to hide/show the two layers on a button press, open
con0:andcon1:, write to the file descriptors, and observe the output on the different consoles by pressing the configured button. (See here for a more complete test using all bg layers except the last reserved for the keyboard.)Backwards compatibility
To keep backwards compatibility, the first initialized console (first call to
consoleInit()) will still be attached to thestdout(1) andstderr(2) file descriptors. ItsdeviceDatawill be set to itself so that you canopen("con0:")to grab a new file descriptor to that console.Bug discovered in newlib
Not enough
devoptab_listslots are initially filled with&dotab_stdnull, so inAddDevices()the loop can dereference a null pointer. In this PR I'm fixing it on the fly on the firstconsoleInit()call (console.c 511-516). I didn't consider it a huge problem worth PR'ing since creating more than 7 consoles is pretty useless, and there arent many other (official) ways to create devices.Misc changes
devoptab_tstructs inconsole.candkeyboard.c, opting for the cleaner field-setting braced-list initialization. This removes the need to comment every line of the braced-list.con0andkbrespectively.kb(keyboard) makes more sense to see thanstdinas a device, IMHO.dotab_nullinconsole.cwithdotab_stdnullfrom newlib'siosupport.c. It already exists so I don't see why we should make a new one.dotab_stdnullindevoptab_list, and more checks forNULLpointers should be in place instead. This would prevent the bug described above.