v3.0.0-rc1

This commit is contained in:
Michael Theall 2020-04-05 14:16:16 -05:00
parent ce2fcc010b
commit 86be27cd31
74 changed files with 9974 additions and 4773 deletions

94
.clang-format Normal file
View File

@ -0,0 +1,94 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
#AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
# SplitEmptyFunction: true
# SplitEmptyRecord: true
# SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
#BreakConstructorInitializers: AfterColon
BreakConstructorInitializersBeforeComma: false
BreakStringLiterals: true
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros: [ foreach ]
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '$'
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Always
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: ForIndentation
...

4
.gitignore vendored
View File

@ -9,5 +9,9 @@
*.nso
*.pfs0
*.smdh
.gdb_history
build.*/
ftpd
romfs.3ds/*.t3x
romfs.switch/*.zst
romfs.switch/shaders/*.dksh

689
LICENSE
View File

@ -1,25 +1,674 @@
This is free and unencumbered software released into the public domain.
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
Preamble
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
For more information, please refer to <http://unlicense.org>
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

View File

@ -1,42 +1,79 @@
.PHONY: all nro 3dsx cia clean linux
.PHONY: all nro 3dsx cia clean linux 3dslink nxlink format release release-3dsx release-cia release-3ds release-nro
TARGET := $(notdir $(CURDIR))
export GITREV := $(shell git rev-parse HEAD 2>/dev/null | cut -c1-8)
export VERSION_MAJOR := 2
export VERSION_MINOR := 3
export VERSION_MICRO := 1
export VERSION := $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_MICRO)
export VERSION_MAJOR := 3
export VERSION_MINOR := 0
export VERSION_MICRO := 0
export VERSION := $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_MICRO)-rc1
ifneq ($(strip $(GITREV)),)
export VERSION := $(VERSION)-$(GITREV)
endif
all:
@echo please choose 3dsx, cia, linux, or nro
all: 3dsx nro linux
release:
# can't let these three run in parallel with each other due to using same
# ftpd.elf file name
@$(MAKE) -f Makefile.switch all
@$(MAKE) -f Makefile.3ds 3dsx
@$(MAKE) -f Makefile.3ds cia
@xz -c <ftpd.3dsx >ftpd.3dsx.xz
@xz -c <ftpd.cia >ftpd.cia.xz
@xz -c <ftpd.nro >ftpd.nro.xz
nxlink:
@$(MAKE) -f Makefile.switch nxlink
3dslink: 3dsx
@/opt/devkitpro/tools/bin/3dslink $(TARGET)-3ds.3dsx
format:
@clang-format -style=file -i $(filter-out \
include/imgui.h \
source/pc/imgui_impl_glfw.cpp \
source/pc/imgui_impl_glfw.h \
source/pc/imgui_impl_opengl3.cpp \
source/pc/imgui_impl_opengl3.h \
source/pc/KHR/khrplatform.h \
source/pc/glad.c \
source/pc/glad/glad.h \
source/imgui/imgui.cpp \
source/imgui/imgui_demo.cpp \
source/imgui/imgui_draw.cpp \
source/imgui/imgui_widgets.cpp \
source/imgui/imstb_rectpack.h \
source/imgui/imstb_textedit.h \
source/imgui/imstb_truetype.h \
source/imgui/imgui_internal.h, \
$(shell find source include -type f -name \*.c -o -name \*.cpp -o -name \*.h))
release: release-3ds release-nro
@xz -c <$(TARGET)-3ds.3dsx >ftpd.3dsx.xz
@echo xz -c <$(TARGET)-3ds.cia >ftpd.cia.xz
@echo xz -c <$(TARGET)-nx.nro >ftpd.nro.xz
nro:
@$(MAKE) -f Makefile.switch all
release-nro:
@$(MAKE) DEFINES=-DNDEBUG -f Makefile.switch all
3dsx:
@$(MAKE) -f Makefile.3ds 3dsx
cia:
release-3dsx:
@$(MAKE) DEFINES=-DNDEBUG -f Makefile.3ds 3dsx
cia: 3dsx
@$(MAKE) -f Makefile.3ds cia
release-cia: release-3dsx
@$(MAKE) DEFINES=-NDEBUG -f Makefile.3ds cia
release-3ds:
# can't let these three run in parallel with each other due to using same
# .elf file name
@$(MAKE) DEFINES=-DNDEBUG -f Makefile.3ds 3dsx
@$(MAKE) DEFINES=-DNDEBUG -f Makefile.3ds cia
linux:
@$(MAKE) -f Makefile.linux
clean:
@$(MAKE) -f Makefile.switch clean
@$(MAKE) -f Makefile.3ds clean
@$(MAKE) -f Makefile.switch clean
@$(MAKE) -f Makefile.3ds clean
@$(MAKE) -f Makefile.linux clean
@$(RM) ftpd.3dsx.xz ftpd.cia.xz ftpd.nro.xz

View File

@ -15,6 +15,10 @@ include $(DEVKITARM)/3ds_rules
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# GRAPHICS is a list of directories containing graphics files
# GFXBUILD is the directory where converted graphics files will be placed
# If set to $(BUILD), it will statically link in the converted
# files as if they were data files.
#
# NO_SMDH: if set to anything, no SMDH file is generated.
# ROMFS is the directory which contains the RomFS, relative to the Makefile (Optional)
@ -27,14 +31,16 @@ include $(DEVKITARM)/3ds_rules
# - icon.png
# - <libctru folder>/default_icon.png
#---------------------------------------------------------------------------------
TARGET := ftpd
TARGET := $(notdir $(CURDIR))-3ds
BUILD := build.3ds
SOURCES := source
SOURCES := source source/3ds source/imgui
DATA := data
INCLUDES := include
ROMFS :=
GRAPHICS := gfx.3ds
ROMFS := romfs.3ds
GFXBUILD := $(ROMFS)
APP_TITLE := ftpd snap!
APP_TITLE := ftpd
APP_DESCRIPTION := v$(VERSION)
APP_AUTHOR := mtheall
@ -46,21 +52,23 @@ RSF_FILE := meta/ftpd-cia.rsf
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
OPTIMIZE := -O2
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
CFLAGS := -g -Wall -O3 -mword-relocations \
-fomit-frame-pointer -ffunction-sections \
$(ARCH) \
-DSTATUS_STRING="\"ftpd v$(VERSION)\""
CFLAGS := -g -Wall $(OPTIMIZE) -mword-relocations \
-fomit-frame-pointer -ffunction-sections -fdata-sections \
$(ARCH) $(DEFINES)
CFLAGS += $(INCLUDE) -DARM11 -D_3DS
CFLAGS += $(INCLUDE) -DARM11 -D_3DS \
-DSTATUS_STRING="\"ftpd v$(VERSION)\"" \
-DIMGUI_DISABLE_INCLUDE_IMCONFIG_H=1
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++17
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(TARGET).map
LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(TARGET).map $(OPTIMIZE)
LIBS := -lctru -lm
LIBS := -lcitro3d -lctru -lm
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
@ -80,14 +88,18 @@ export OUTPUT := $(CURDIR)/$(TARGET)
export TOPDIR := $(CURDIR)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
PICAFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.v.pica)))
SHLISTFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.shlist)))
GFXFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.t3s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
@ -99,16 +111,38 @@ else
endif
#---------------------------------------------------------------------------------
export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) \
$(CFILES:.c=.o) \
$(SFILES:.s=.o)
#---------------------------------------------------------------------------------
ifeq ($(GFXBUILD),$(BUILD))
#---------------------------------------------------------------------------------
export T3XFILES := $(GFXFILES:.t3s=.t3x)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export ROMFS_T3XFILES := $(patsubst %.t3s,$(GFXBUILD)/%.t3x,$(GFXFILES))
export T3XHFILES := $(patsubst %.t3s,$(BUILD)/%.h,$(GFXFILES))
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export OFILES_BIN := $(addsuffix .o,$(BINFILES)) \
$(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \
$(addsuffix .o,$(T3XFILES))
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
export HFILES := $(PICAFILES:.v.pica=_shbin.h) $(SHLISTFILES:.shlist=_shbin.h) \
$(addsuffix .h,$(subst .,_,$(BINFILES))) \
$(GFXFILES:.t3s=.h)
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export _3DSXDEPS := $(if $(NO_SMDH),,$(OUTPUT).smdh)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.png)
@ -131,36 +165,54 @@ ifneq ($(ROMFS),)
export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS)
endif
.PHONY: $(BUILD) clean all
.PHONY: $(BUILD) clean all 3dsx cia
#---------------------------------------------------------------------------------
all: $(BUILD)
all: $(BUILD) $(GFXBUILD) $(DEPSDIR) $(ROMFS_T3XFILES) $(T3XHFILES)
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.3ds
3dsx: $(BUILD)
$(BUILD):
@mkdir -p $@
ifneq ($(GFXBUILD),$(BUILD))
$(GFXBUILD):
@mkdir -p $@
endif
ifneq ($(DEPSDIR),$(BUILD))
$(DEPSDIR):
mkdir -p $@
endif
#---------------------------------------------------------------------------------
$(GFXBUILD)/%.t3x $(BUILD)/%.h: %.t3s
#---------------------------------------------------------------------------------
@echo $(notdir $<)
tex3ds -i $< -H $(BUILD)/$*.h -d $(DEPSDIR)/$*.d -o $(GFXBUILD)/$*.t3x
3dsx: $(BUILD) $(GFXBUILD) $(DEPSDIR) $(ROMFS_T3XFILES) $(T3XHFILES)
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.3ds 3dsx
cia: $(BUILD)
cia: $(BUILD) $(GFXBUILD) $(DEPSDIR) $(ROMFS_T3XFILES) $(T3XHFILES)
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.3ds cia
$(BUILD):
@[ -d $@ ] || mkdir -p $@
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@$(RM) -r $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf $(TARGET).cia output/
@$(RM) -r $(BUILD) \
$(TARGET).3dsx \
$(OUTPUT).smdh \
$(TARGET).elf \
$(TARGET).cia \
$(ROMFS_T3XFILES) \
output/
#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
#all: $(OUTPUT).cia $(OUTPUT).3dsx
all: $(OUTPUT).cia $(OUTPUT).3dsx
3dsx: $(OUTPUT).3dsx
@ -173,11 +225,56 @@ $(OUTPUT).smdh: $(TOPDIR)/Makefile $(TOPDIR)/Makefile.3ds
$(OUTPUT).3dsx: $(OUTPUT).smdh
endif
$(OUTPUT).3dsx: $(OUTPUT).elf
$(OUTPUT).3dsx: $(OUTPUT).elf $(_3DSXDEPS)
$(OFILES_SOURCES): $(HFILES)
$(OUTPUT).elf: $(OFILES)
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o %_bin.h: %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
$(OFILES): $(TOPDIR)/Makefile $(TOPDIR)/Makefile.3ds
#---------------------------------------------------------------------------------
.PRECIOUS: %.t3x
#---------------------------------------------------------------------------------
%.t3x.o %_t3x.h: %.t3x
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
#---------------------------------------------------------------------------------
# rules for assembling GPU shaders
#---------------------------------------------------------------------------------
define shader-as
$(eval CURBIN := $*.shbin)
$(eval DEPSFILE := $(DEPSDIR)/$*.shbin.d)
echo "$(CURBIN).o: $< $1" > $(DEPSFILE)
echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(CURBIN) | tr . _)`.h
echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(CURBIN) | tr . _)`.h
echo "extern const u32" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(CURBIN) | tr . _)`.h
picasso -o $(CURBIN) $1
bin2s $(CURBIN) | $(AS) -o $*.shbin.o
endef
%.shbin.o %_shbin.h : %.v.pica %.g.pica
@echo $(notdir $^)
@$(call shader-as,$^)
%.shbin.o %_shbin.h : %.v.pica
@echo $(notdir $<)
@$(call shader-as,$<)
%.shbin.o %_shbin.h : %.shlist
@echo $(notdir $<)
@$(call shader-as,$(foreach file,$(shell cat $<),$(dir $<)$(file)))
$(OUTPUT).cia: $(OUTPUT).elf $(OUTPUT).smdh $(TARGET).bnr $(TOPDIR)/$(RSF_FILE)
@makerom -f cia -target t -exefslogo -o $@ \
-elf $(OUTPUT).elf -rsf $(TOPDIR)/$(RSF_FILE) \
@ -190,15 +287,8 @@ $(TARGET).bnr: $(TOPDIR)/$(BNR_IMAGE) $(TOPDIR)/$(BNR_AUDIO)
@bannertool makebanner -o $@ -i $(TOPDIR)/$(BNR_IMAGE) -a $(TOPDIR)/$(BNR_AUDIO)
@echo "built ... $@"
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o: %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS)
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------------
endif

View File

@ -1,23 +1,36 @@
TARGET := ftpd
BUILD := build.linux
CFILES := $(wildcard source/*.c)
OFILES := $(patsubst source/%,build.linux/%,$(CFILES:.c=.o))
CFILES := $(wildcard source/pc/*.c)
OFILES := $(patsubst source/%,$(BUILD)/%,$(CFILES:.c=.c.o))
CXXFILES := $(wildcard source/*.cpp source/imgui/*.cpp source/pc/*.cpp)
OXXFILES := $(patsubst source/%,$(BUILD)/%,$(CXXFILES:.cpp=.cpp.o))
CFLAGS := -g -Wall -Iinclude -DSTATUS_STRING="\"ftpd v$(VERSION)\""
LDFLAGS :=
CPPFLAGS := -g -Wall -pthread -Iinclude -Isource/pc \
`pkg-config --cflags gl glfw3` \
-DSTATUS_STRING="\"ftpd v$(VERSION)\"" \
-DIMGUI_DISABLE_INCLUDE_IMCONFIG_H=1 \
-DIMGUI_IMPL_OPENGL_LOADER_GLAD=1
CFLAGS := $(CPPFLAGS)
CXXFLAGS := $(CPPFLAGS) -std=gnu++17
LDFLAGS := -pthread `pkg-config --libs gl glfw3` -ldl
.PHONY: all clean
all: build.linux $(TARGET)
all: $(TARGET)
build.linux:
@mkdir build.linux/
$(TARGET): $(OFILES) $(OXXFILES)
$(CXX) -o $@ $^ $(LDFLAGS)
$(TARGET): $(OFILES)
@$(CC) -o $@ $^ $(LDFLAGS)
$(OFILES): $(BUILD)/%.c.o : source/%.c
@[ -d $(dir $@) ] || mkdir -p $(dir $@)
$(CC) -MMD -MP -MF $(BUILD)/$*.c.d $(CFLAGS) -c $< -o $@
$(OFILES): build.linux/%.o : source/%.c
@$(CC) -o $@ -c $< $(CFLAGS)
$(OXXFILES): $(BUILD)/%.cpp.o : source/%.cpp
@[ -d $(dir $@) ] || mkdir -p $(dir $@)
$(CXX) -MMD -MP -MF $(BUILD)/$*.c.d $(CXXFLAGS) -c $< -o $@
clean:
@$(RM) -r build.linux/ $(TARGET)
@$(RM) -r $(BUILD) $(TARGET)
-include $(shell find $(BUILD) -name \*.d 2>/dev/null)

View File

@ -15,7 +15,7 @@ include $(DEVKITPRO)/libnx/switch_rules
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm".
# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional)
#
# NO_ICON: if set to anything, do not use icon.
# NO_NACP: if set to anything, no .nacp file is generated.
@ -28,36 +28,51 @@ include $(DEVKITPRO)/libnx/switch_rules
# - <Project name>.jpg
# - icon.jpg
# - <libnx folder>/default_icon.jpg
#
# CONFIG_JSON is the filename of the NPDM config file (.json), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.json
# - config.json
# If a JSON file is provided or autodetected, an ExeFS PFS0 (.nsp) is built instead
# of a homebrew executable (.nro). This is intended to be used for sysmodules.
# NACP building is skipped as well.
#---------------------------------------------------------------------------------
APP_TITLE := ftpd snap! $(VERSION)
APP_AUTHOR := mtheall, TuxSH, WinterMute
ICON := meta/ftpd.jpg
ICON := meta/ftpd.jpg
APP_VERSION := $(VERSION)
#---------------------------------------------------------------------------------
TARGET := ftpd
TARGET := $(notdir $(CURDIR))-nx
BUILD := build.switch
SOURCES := source
SOURCES := source source/imgui source/nx
DATA := data
INCLUDES := include
EXEFS_SRC := exefs_src
GRAPHICS := gfx.switch
ROMFS := romfs.switch
# Output folders for autogenerated files in romfs
OUT_SHADERS := shaders
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a -mtp=soft -fPIE
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS := -g -Wall -O2 \
-ffast-math \
CFLAGS := -g -Wall -Wno-narrowing -Os -ffunction-sections -fdata-sections -save-temps \
$(ARCH) $(DEFINES)
CFLAGS += $(INCLUDE) -D__SWITCH__ -DSTATUS_STRING="\"ftpd v$(VERSION)\""
CFLAGS += $(INCLUDE) -D__SWITCH__ \
-DSTATUS_STRING="\"ftpd v$(VERSION)\"" \
-DIMGUI_DISABLE_INCLUDE_IMCONFIG_H=1 \
`$(PREFIX)pkg-config --cflags libzstd`
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
CXXFLAGS := $(CFLAGS) -std=gnu++17 -fno-exceptions -fno-rtti
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) \
-Wl,-Map,$(notdir $*.map) -Wl,--gc-sections
LIBS := -lnx
LIBS := `$(PREFIX)pkg-config --libs libzstd` -ldeko3dd -lnx
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
@ -84,6 +99,8 @@ export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
GLSLFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.glsl)))
GFXFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
@ -91,17 +108,35 @@ BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
ifneq ($(strip $(ROMFS)),)
ROMFS_TARGETS :=
ROMFS_FOLDERS :=
ifneq ($(strip $(OUT_SHADERS)),)
ROMFS_SHADERS := $(ROMFS)/$(OUT_SHADERS)
ROMFS_TARGETS += $(patsubst %.glsl, $(ROMFS_SHADERS)/%.dksh, $(GLSLFILES))
ROMFS_FOLDERS += $(ROMFS_SHADERS)
endif
ROMFS_GFX := $(addprefix $(ROMFS)/,$(GFXFILES:.png=.rgba.zst))
ROMFS_TARGETS += $(ROMFS_GFX)
ROMFS_FOLDERS += $(ROMFS)
export ROMFS_DEPS := $(foreach file,$(ROMFS_TARGETS),$(CURDIR)/$(file))
endif
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
@ -109,7 +144,18 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC)
ifeq ($(strip $(CONFIG_JSON)),)
jsons := $(wildcard *.json)
ifneq (,$(findstring $(TARGET).json,$(jsons)))
export APP_JSON := $(TOPDIR)/$(TARGET).json
else
ifneq (,$(findstring config.json,$(jsons)))
export APP_JSON := $(TOPDIR)/config.json
endif
endif
else
export APP_JSON := $(TOPDIR)/$(CONFIG_JSON)
endif
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.jpg)
@ -136,19 +182,69 @@ ifneq ($(APP_TITLEID),)
export NACPFLAGS += --titleid=$(APP_TITLEID)
endif
.PHONY: $(BUILD) clean all
ifneq ($(ROMFS),)
export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS)
endif
.PHONY: all clean
#---------------------------------------------------------------------------------
all: $(BUILD)
all: $(ROMFS_TARGETS) | $(BUILD)
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.switch
nxlink: all
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.switch nxlink
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.switch
@mkdir -p $@
ifneq ($(strip $(ROMFS_TARGETS)),)
$(ROMFS_TARGETS): | $(ROMFS_FOLDERS)
$(ROMFS_FOLDERS):
@mkdir -p $@
$(BUILD)/%.rgba: $(GRAPHICS)/%.png | $(BUILD)
@convert $< $@
$(ROMFS_GFX): $(ROMFS)/%.rgba.zst: $(BUILD)/%.rgba
@zstd $< -o $@ --ultra -22
$(ROMFS_SHADERS)/%_vsh.dksh: %_vsh.glsl
@echo {vert} $(notdir $<)
@uam -s vert -o $@ $<
$(ROMFS_SHADERS)/%_tcsh.dksh: %_tcsh.glsl
@echo {tess_ctrl} $(notdir $<)
@uam -s tess_ctrl -o $@ $<
$(ROMFS_SHADERS)/%_tesh.dksh: %_tesh.glsl
@echo {tess_eval} $(notdir $<)
@uam -s tess_eval -o $@ $<
$(ROMFS_SHADERS)/%_gsh.dksh: %_gsh.glsl
@echo {geom} $(notdir $<)
@uam -s geom -o $@ $<
$(ROMFS_SHADERS)/%_fsh.dksh: %_fsh.glsl
@echo {frag} $(notdir $<)
@uam -s frag -o $@ $<
$(ROMFS_SHADERS)/%.dksh: %.glsl
@echo {comp} $(notdir $<)
@uam -s comp -o $@ $<
endif
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf
ifeq ($(strip $(APP_JSON)),)
@$(RM) -r $(BUILD) $(ROMFS)/*.zst $(ROMFS_FOLDERS) $(TARGET).nro $(TARGET).nacp $(TARGET).elf
else
@$(RM) -r $(BUILD) $(ROMFS_FOLDERS) $(TARGET).nsp $(TARGET).nso $(TARGET).npdm $(TARGET).elf
endif
#---------------------------------------------------------------------------------
@ -160,24 +256,37 @@ DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
all : $(OUTPUT).pfs0 $(OUTPUT).nro
ifeq ($(strip $(APP_JSON)),)
$(OUTPUT).pfs0 : $(OUTPUT).nso
all : $(OUTPUT).nro
nxlink: $(OUTPUT).nro
@nxlink -s $(OUTPUT).nro
ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp $(ROMFS_DEPS)
else
$(OUTPUT).nro : $(OUTPUT).elf $(ROMFS_DEPS)
endif
else
all : $(OUTPUT).nsp
$(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm
$(OUTPUT).nso : $(OUTPUT).elf
ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
else
$(OUTPUT).nro : $(OUTPUT).elf
endif
$(OUTPUT).elf : $(OFILES)
$(OFILES_SRC) : $(HFILES_BIN)
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o : %.bin
%.bin.o %_bin.h : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)

View File

@ -10,15 +10,15 @@ FTP Server for 3DS/Switch/Linux.
## Latest Builds
CIA: https://mtheall.com/~mtheall/ftpd.cia
CIA: https://mtheall.com/~mtheall/ftpd-3ds.cia
3DSX: https://mtheall.com/~mtheall/ftpd.3dsx
3DSX: https://mtheall.com/~mtheall/ftpd-3ds.3dsx
NRO: https://mtheall.com/~mtheall/ftpd.nro
NRO: https://mtheall.com/~mtheall/ftpd-nx.nro
CIA QR Code
![ftpd.cia](https://github.com/mtheall/ftpd/raw/master/ftpd_qr.png)
![ftpd-3ds.cia](https://github.com/mtheall/ftpd/raw/master/ftpd_qr.png)
## Build and install
@ -26,7 +26,7 @@ You must set up the [development environment](https://devkitpro.org/wiki/Getting
### 3DSX
The following pacman packages are required to build `ftpd.3dsx`:
The following pacman packages are required to build `ftpd-3ds.3dsx`:
3dstools
devkitARM
@ -34,13 +34,13 @@ The following pacman packages are required to build `ftpd.3dsx`:
They are available as part of the `3ds-dev` meta-package.
Build `ftpd.3dsx`:
Build `ftpd-3ds.3dsx`:
make 3dsx
### NRO
The following pacman packages are required to build `ftpd.nro`:
The following pacman packages are required to build `ftpd-nx.nro`:
devkitA64
libnx
@ -48,7 +48,7 @@ The following pacman packages are required to build `ftpd.nro`:
They are available as part of the `switch-dev` meta-package.
Build `ftpd.nro`:
Build `ftpd-nx.nro`:
make nro

View File

@ -1,9 +0,0 @@
#!/usr/bin/env python
import sys
import re
if __name__ == '__main__':
regex = re.compile('\x1b\[[0-9]*;[0-9]*H')
for line in sys.stdin:
print(regex.sub('', line).strip())

Binary file not shown.

Before

Width:  |  Height:  |  Size: 621 B

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
gfx.3ds/battery0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/battery1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/battery2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/battery3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/battery4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/batteryCharge.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/c3dlogo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 791 B

12
gfx.3ds/gfx.t3s Normal file
View File

@ -0,0 +1,12 @@
-f rgba -z auto --atlas
battery0.png
battery1.png
battery2.png
battery3.png
battery4.png
batteryCharge.png
c3dlogo.png
wifiNull.png
wifi1.png
wifi2.png
wifi3.png

BIN
gfx.3ds/wifi1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/wifi2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/wifi3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.3ds/wifiNull.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
gfx.switch/deko3d.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -1,44 +0,0 @@
#pragma once
#ifdef _3DS
#include <3ds.h>
#elif defined(__SWITCH__)
#include <switch.h>
#endif
#if defined(_3DS) || defined(__SWITCH__)
#define ESC(x) "\x1b[" #x
#define RESET ESC(0m)
#define BLACK ESC(30m)
#define RED ESC(31;1m)
#define GREEN ESC(32;1m)
#define YELLOW ESC(33;1m)
#define BLUE ESC(34;1m)
#define MAGENTA ESC(35;1m)
#define CYAN ESC(36;1m)
#define WHITE ESC(37;1m)
#else
#define ESC(x)
#define RESET
#define BLACK
#define RED
#define GREEN
#define YELLOW
#define BLUE
#define MAGENTA
#define CYAN
#define WHITE
#endif
void console_init(void);
__attribute__((format(printf,1,2)))
void console_set_status(const char *fmt, ...);
__attribute__((format(printf,1,2)))
void console_print(const char *fmt, ...);
__attribute__((format(printf,1,2)))
void debug_print(const char *fmt, ...);
void console_render(void);

113
include/fs.h Normal file
View File

@ -0,0 +1,113 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <dirent.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>
namespace fs
{
std::string printSize (std::uint64_t size_);
class File
{
public:
~File ();
File ();
File (File const &that_) = delete;
File (File &&that_);
File &operator= (File const &that_) = delete;
File &operator= (File &&that_);
operator bool () const;
operator FILE * () const;
void setBufferSize (std::size_t size_);
bool open (char const *path_, char const *mode_ = "rb");
void close ();
ssize_t seek (std::size_t pos_, int origin_);
ssize_t read (void *data_, std::size_t size_);
bool readAll (void *data_, std::size_t size_);
ssize_t write (void const *data_, std::size_t size_);
bool writeAll (void const *data_, std::size_t size_);
template <typename T>
T read ()
{
T data;
if (read (&data, sizeof (data)) != sizeof (data))
std::abort ();
return data;
}
template <typename T>
void write (T const &data_)
{
if (write (&data_, sizeof (data_)) != sizeof (data_))
std::abort ();
}
private:
std::unique_ptr<std::FILE, int (*) (std::FILE *)> m_fp{nullptr, nullptr};
std::unique_ptr<char[]> m_buffer;
std::size_t m_bufferSize = 0;
};
class Dir
{
public:
~Dir ();
Dir ();
Dir (Dir const &that_) = delete;
Dir (Dir &&that_);
Dir &operator= (Dir const &that_) = delete;
Dir &operator= (Dir &&that_);
operator bool () const;
operator DIR * () const;
bool open (char const *const path_);
void close ();
struct dirent *read ();
private:
std::unique_ptr<DIR, int (*) (DIR *)> m_dp{nullptr, nullptr};
};
}

View File

@ -1,13 +0,0 @@
#pragma once
/*! Loop status */
typedef enum
{
LOOP_CONTINUE, /*!< Continue looping */
LOOP_RESTART, /*!< Reinitialize */
LOOP_EXIT, /*!< Terminate looping */
} loop_status_t;
int ftp_init(void);
loop_status_t ftp_loop(void);
void ftp_exit(void);

73
include/ftpServer.h Normal file
View File

@ -0,0 +1,73 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "ftpSession.h"
#include "log.h"
#include "platform.h"
#include "socket.h"
#include <atomic>
#include <chrono>
#include <cstdint>
#include <memory>
#include <vector>
class FtpServer;
using UniqueFtpServer = std::unique_ptr<FtpServer>;
class FtpServer
{
public:
~FtpServer ();
void draw ();
static UniqueFtpServer create (std::uint16_t port_);
static void updateFreeSpace ();
static std::time_t startTime ();
private:
FtpServer (std::uint16_t port_);
void handleStartButton ();
void handleStopButton ();
void loop ();
void threadFunc ();
platform::Thread m_thread;
platform::Mutex m_lock;
UniqueSocket m_socket;
std::string m_name;
SharedLog m_log;
std::vector<UniqueFtpSession> m_sessions;
std::uint16_t m_port;
std::atomic<bool> m_quit;
};

205
include/ftpSession.h Normal file
View File

@ -0,0 +1,205 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "fs.h"
#include "ioBuffer.h"
#include "platform.h"
#include "socket.h"
#include <chrono>
#include <memory>
#include <string_view>
#include <utility>
#include <vector>
class FtpSession;
using UniqueFtpSession = std::unique_ptr<FtpSession>;
class FtpSession
{
public:
~FtpSession ();
bool dead ();
void draw ();
static UniqueFtpSession create (UniqueSocket commandSocket_);
static void poll (std::vector<UniqueFtpSession> const &sessions_);
private:
constexpr static auto COMMAND_BUFFERSIZE = 4096;
constexpr static auto RESPONSE_BUFFERSIZE = 32768;
constexpr static auto XFER_BUFFERSIZE = 65536;
constexpr static auto FILE_BUFFERSIZE = 4 * XFER_BUFFERSIZE;
#ifdef _3DS
constexpr static auto SOCK_BUFFERSIZE = 32768;
constexpr static auto POSITION_HISTORY = 100;
#else
constexpr static auto SOCK_BUFFERSIZE = XFER_BUFFERSIZE;
constexpr static auto POSITION_HISTORY = 300;
#endif
enum class State
{
COMMAND,
DATA_CONNECT,
DATA_TRANSFER,
};
enum class XferFileMode
{
RETR,
STOR,
APPE,
};
enum class XferDirMode
{
LIST,
MLSD,
MLST,
NLST,
STAT,
};
FtpSession (UniqueSocket commandSocket_);
void setState (State state_, bool closePasv_, bool closeData_);
void closeData ();
bool changeDir (char const *args_);
bool dataAccept ();
bool dataConnect ();
void updateFreeSpace ();
int fillDirent (struct stat const &st_, std::string_view path_, char const *type_ = nullptr);
int fillDirent (std::string const &path_, char const *type_ = nullptr);
void xferFile (char const *args_, XferFileMode mode_);
void xferDir (char const *args_, XferDirMode mode_, bool workaround_);
void readCommand (int events_);
void writeResponse ();
__attribute__ ((format (printf, 2, 3))) void sendResponse (char const *fmt_, ...);
void sendResponse (std::string_view response_);
bool (FtpSession::*m_transfer) () = nullptr;
bool listTransfer ();
bool retrieveTransfer ();
bool storeTransfer ();
platform::Mutex m_lock;
SharedSocket m_commandSocket;
UniqueSocket m_pasvSocket;
SharedSocket m_dataSocket;
std::vector<SharedSocket> m_pendingCloseSocket;
IOBuffer m_commandBuffer;
IOBuffer m_responseBuffer;
IOBuffer m_xferBuffer;
SockAddr m_pasvAddr;
SockAddr m_portAddr;
std::string m_cwd = "/";
std::string m_lwd;
std::string m_rename;
std::string m_workItem;
std::string m_windowName;
std::string m_plotName;
std::uint64_t m_restartPosition = 0;
std::uint64_t m_filePosition = 0;
std::uint64_t m_fileSize = 0;
platform::steady_clock::time_point m_filePositionTime;
std::uint64_t m_filePositionHistory[POSITION_HISTORY];
float m_filePositionDeltas[POSITION_HISTORY];
float m_xferRate;
State m_state = State::COMMAND;
fs::File m_file;
fs::Dir m_dir;
XferDirMode m_xferDirMode;
bool m_pasv : 1;
bool m_port : 1;
bool m_recv : 1;
bool m_send : 1;
bool m_urgent : 1;
bool m_mlstType : 1;
bool m_mlstSize : 1;
bool m_mlstModify : 1;
bool m_mlstPerm : 1;
bool m_mlstUnixMode : 1;
void ABOR (char const *args_);
void ALLO (char const *args_);
void APPE (char const *args_);
void CDUP (char const *args_);
void CWD (char const *args_);
void DELE (char const *args_);
void FEAT (char const *args_);
void HELP (char const *args_);
void LIST (char const *args_);
void MDTM (char const *args_);
void MKD (char const *args_);
void MLSD (char const *args_);
void MLST (char const *args_);
void MODE (char const *args_);
void NLST (char const *args_);
void NOOP (char const *args_);
void OPTS (char const *args_);
void PASS (char const *args_);
void PASV (char const *args_);
void PORT (char const *args_);
void PWD (char const *args_);
void QUIT (char const *args_);
void REST (char const *args_);
void RETR (char const *args_);
void RMD (char const *args_);
void RNFR (char const *args_);
void RNTO (char const *args_);
void SIZE (char const *args_);
void STAT (char const *args_);
void STOR (char const *args_);
void STOU (char const *args_);
void STRU (char const *args_);
void SYST (char const *args_);
void TYPE (char const *args_);
void USER (char const *args_);
static std::vector<std::pair<std::string_view, void (FtpSession::*) (char const *)>> const
handlers;
};

1
include/imgui.h Normal file
View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

51
include/ioBuffer.h Normal file
View File

@ -0,0 +1,51 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <cstddef>
#include <memory>
class IOBuffer
{
public:
~IOBuffer ();
IOBuffer (std::size_t size_);
char *freeArea () const;
std::size_t freeSize () const;
void markFree (std::size_t size_);
char *usedArea () const;
std::size_t usedSize () const;
void markUsed (std::size_t size_);
bool empty () const;
std::size_t capacity () const;
void clear ();
void coalesce ();
private:
std::unique_ptr<char[]> m_buffer;
std::size_t const m_size;
std::size_t m_start = 0;
std::size_t m_end = 0;
};

81
include/log.h Normal file
View File

@ -0,0 +1,81 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "platform.h"
#include <cstdarg>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
class Log;
using SharedLog = std::shared_ptr<Log>;
using WeakLog = std::weak_ptr<Log>;
class Log
{
public:
enum Level
{
DEBUG,
INFO,
ERROR,
COMMAND,
RESPONSE,
};
~Log ();
void draw ();
static SharedLog create ();
static void bind (SharedLog log_);
__attribute__ ((format (printf, 1, 2))) static void debug (char const *fmt_, ...);
__attribute__ ((format (printf, 1, 2))) static void info (char const *fmt_, ...);
__attribute__ ((format (printf, 1, 2))) static void error (char const *fmt_, ...);
__attribute__ ((format (printf, 1, 2))) static void command (char const *fmt_, ...);
__attribute__ ((format (printf, 1, 2))) static void response (char const *fmt_, ...);
static void log (Level level_, char const *fmt_, va_list ap_);
static void log (Level level_, std::string_view message_);
private:
Log ();
void _log (Level level_, char const *fmt_, va_list ap_);
struct Message
{
Message (Level const level_, std::string message_)
: level (level_), message (std::move (message_))
{
}
Level level;
std::string message;
};
std::vector<Message> m_messages;
platform::Mutex m_lock;
};

95
include/platform.h Normal file
View File

@ -0,0 +1,95 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#ifdef _3DS
#include <3ds.h>
#endif
#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
namespace platform
{
bool init ();
bool loop ();
void render ();
void exit ();
#ifdef _3DS
struct steady_clock
{
using rep = std::uint64_t;
using period = std::ratio<1, SYSCLOCK_ARM11>;
using duration = std::chrono::duration<rep, period>;
using time_point = std::chrono::time_point<steady_clock>;
constexpr static bool is_steady = true;
static time_point now () noexcept
{
return time_point (duration (svcGetSystemTick ()));
}
};
#else
using steady_clock = std::chrono::steady_clock;
#endif
class Thread
{
public:
~Thread ();
Thread ();
Thread (std::function<void ()> func_);
Thread (Thread const &that_) = delete;
Thread (Thread &&that_);
Thread &operator= (Thread const &that_) = delete;
Thread &operator= (Thread &&that_);
void join ();
static void sleep (std::chrono::milliseconds timeout_);
private:
class privateData_t;
std::unique_ptr<privateData_t> m_d;
};
class Mutex
{
public:
~Mutex ();
Mutex ();
void lock ();
void unlock ();
private:
class privateData_t;
std::unique_ptr<privateData_t> m_d;
};
}

70
include/sockAddr.h Normal file
View File

@ -0,0 +1,70 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <netinet/in.h>
#include <sys/socket.h>
#include <cstdint>
class SockAddr
{
public:
~SockAddr ();
SockAddr ();
SockAddr (SockAddr const &that_);
SockAddr (SockAddr &&that_);
SockAddr &operator= (SockAddr const &that_);
SockAddr &operator= (SockAddr &&that_);
SockAddr (struct sockaddr const &addr_);
SockAddr (struct sockaddr_in const &addr_);
#ifndef _3DS
SockAddr (struct sockaddr_in6 const &addr_);
#endif
SockAddr (struct sockaddr_storage const &addr_);
operator struct sockaddr_in const & () const;
#ifndef _3DS
operator struct sockaddr_in6 const & () const;
#endif
operator struct sockaddr_storage const & () const;
operator struct sockaddr * ();
operator struct sockaddr const * () const;
std::uint16_t port () const;
char const *name (char *buffer_, std::size_t size_) const;
char const *name () const;
private:
struct sockaddr_storage m_addr = {};
};

97
include/socket.h Normal file
View File

@ -0,0 +1,97 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "ioBuffer.h"
#include "sockAddr.h"
#include <chrono>
#include <memory>
class Socket;
using UniqueSocket = std::unique_ptr<Socket>;
using SharedSocket = std::shared_ptr<Socket>;
class Socket
{
public:
struct PollInfo
{
std::reference_wrapper<Socket> socket;
int events;
int revents;
};
~Socket ();
UniqueSocket accept ();
int atMark ();
bool bind (SockAddr const &addr_);
bool connect (SockAddr const &addr_);
bool listen (int backlog_);
bool shutdown (int how_);
bool setLinger (bool enable_, std::chrono::seconds time_);
bool setNonBlocking (bool nonBlocking_ = true);
bool setReuseAddress (bool reuse_ = true);
bool setRecvBufferSize (std::size_t size_);
bool setSendBufferSize (std::size_t size_);
ssize_t read (void *buffer_, std::size_t size_, bool oob_ = false);
ssize_t read (IOBuffer &buffer_, bool oob_ = false);
ssize_t write (void const *buffer_, std::size_t size_);
ssize_t write (IOBuffer &buffer_);
SockAddr const &sockName () const;
SockAddr const &peerName () const;
static UniqueSocket create ();
static int poll (PollInfo *info_, std::size_t count_, std::chrono::milliseconds timeout_);
int fd () const
{
return m_fd;
}
private:
Socket () = delete;
Socket (int fd_);
Socket (int fd_, SockAddr const &sockName_, SockAddr const &peerName_);
Socket (Socket const &that_) = delete;
Socket (Socket &&that_) = delete;
Socket &operator= (Socket const &that_) = delete;
Socket &operator= (Socket &&that_) = delete;
SockAddr m_sockName;
SockAddr m_peerName;
int const m_fd;
bool m_listening : 1;
bool m_connected : 1;
};

View File

@ -0,0 +1,623 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "imgui_citro3d.h"
#include <citro3d.h>
#include "vshader_shbin.h"
#include "imgui.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <vector>
namespace
{
std::vector<ImWchar> s_fontRanges;
constexpr auto CLEAR_COLOR = 0x204B7AFF;
constexpr auto DISPLAY_TRANSFER_FLAGS =
GX_TRANSFER_FLIP_VERT (0) | GX_TRANSFER_OUT_TILED (0) | GX_TRANSFER_RAW_COPY (0) |
GX_TRANSFER_IN_FORMAT (GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_OUT_FORMAT (GX_TRANSFER_FMT_RGB8) |
GX_TRANSFER_SCALING (GX_TRANSFER_SCALE_NO);
C3D_RenderTarget *s_top = nullptr;
C3D_RenderTarget *s_bottom = nullptr;
DVLB_s *s_vsh = nullptr;
shaderProgram_s s_program;
int s_projLocation;
C3D_Mtx s_projTop;
C3D_Mtx s_projBottom;
std::vector<C3D_Tex> s_fontTextures;
float s_textScale;
std::uint32_t s_boundScissor[4];
ImDrawVert *s_boundVtxData;
C3D_Tex *s_boundTexture;
ImDrawVert *s_vtxData = nullptr;
std::size_t s_vtxSize = 0;
ImDrawIdx *s_idxData = nullptr;
std::size_t s_idxSize = 0;
std::uint32_t fontCodePointFromGlyphIndex (CFNT_s *const font_, int const glyphIndex_)
{
for (auto cmap = fontGetInfo (font_)->cmap; cmap; cmap = cmap->next)
{
switch (cmap->mappingMethod)
{
case CMAP_TYPE_DIRECT:
assert (cmap->codeEnd >= cmap->codeBegin);
if (glyphIndex_ >= cmap->indexOffset &&
glyphIndex_ <= cmap->codeEnd - cmap->codeBegin + cmap->indexOffset)
return glyphIndex_ - cmap->indexOffset + cmap->codeBegin;
break;
case CMAP_TYPE_TABLE:
for (int i = 0; i <= cmap->codeEnd - cmap->codeBegin; ++i)
{
if (cmap->indexTable[i] == glyphIndex_)
return cmap->codeBegin + i;
}
break;
case CMAP_TYPE_SCAN:
for (unsigned i = 0; i < cmap->nScanEntries; ++i)
{
assert (cmap->scanEntries[i].code >= cmap->codeBegin);
assert (cmap->scanEntries[i].code <= cmap->codeEnd);
if (glyphIndex_ == cmap->scanEntries[i].glyphIndex)
return cmap->scanEntries[i].code;
}
break;
}
}
return 0;
}
void setupRenderState (gfxScreen_t const screen_)
{
C3D_CullFace (GPU_CULL_NONE);
// configure attributes for user with vertex shader
auto const attrInfo = C3D_GetAttrInfo ();
AttrInfo_Init (attrInfo);
AttrInfo_AddLoader (attrInfo, 0, GPU_FLOAT, 2); // v0 = inPos
AttrInfo_AddLoader (attrInfo, 1, GPU_FLOAT, 2); // v1 = inUv
AttrInfo_AddLoader (attrInfo, 2, GPU_UNSIGNED_BYTE, 4); // v2 = inColor
std::memset (s_boundScissor, 0xFF, sizeof (s_boundScissor));
s_boundVtxData = nullptr;
s_boundTexture = nullptr;
C3D_BindProgram (&s_program);
C3D_DepthTest (true, GPU_GREATER, GPU_WRITE_COLOR);
C3D_AlphaBlend (GPU_BLEND_ADD,
GPU_BLEND_ADD,
GPU_SRC_ALPHA,
GPU_ONE_MINUS_SRC_ALPHA,
GPU_SRC_ALPHA,
GPU_ONE_MINUS_SRC_ALPHA);
if (screen_ == GFX_TOP)
C3D_FVUnifMtx4x4 (GPU_VERTEX_SHADER, s_projLocation, &s_projTop);
else
C3D_FVUnifMtx4x4 (GPU_VERTEX_SHADER, s_projLocation, &s_projBottom);
}
}
void imgui::citro3d::init ()
{
// Setup back-end capabilities flags
ImGuiIO &io = ImGui::GetIO ();
io.BackendRendererName = "citro3d";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
C3D_Init (C3D_DEFAULT_CMDBUF_SIZE);
s_top = C3D_RenderTargetCreate (240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
C3D_RenderTargetSetOutput (s_top, GFX_TOP, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);
s_bottom = C3D_RenderTargetCreate (240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
C3D_RenderTargetSetOutput (s_bottom, GFX_BOTTOM, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);
s_vsh = DVLB_ParseFile (
const_cast<std::uint32_t *> (reinterpret_cast<std::uint32_t const *> (vshader_shbin)),
vshader_shbin_size);
shaderProgramInit (&s_program);
shaderProgramSetVsh (&s_program, &s_vsh->DVLE[0]);
s_projLocation = shaderInstanceGetUniformLocation (s_program.vertexShader, "proj");
Mtx_OrthoTilt (&s_projTop, 0.0f, 800.0f, 480.0f, 0.0f, -1.0f, 1.0f, false);
Mtx_OrthoTilt (&s_projBottom, 80.0f, 720.0f, 960.0f, 480.0f, -1.0f, 1.0f, false);
s_vtxSize = 65536;
s_vtxData = reinterpret_cast<ImDrawVert *> (linearAlloc (sizeof (ImDrawVert) * s_vtxSize));
if (!s_vtxData)
svcBreak (USERBREAK_PANIC);
s_idxSize = 65536;
s_idxData = reinterpret_cast<ImDrawIdx *> (linearAlloc (sizeof (ImDrawIdx) * s_idxSize));
if (!s_idxData)
svcBreak (USERBREAK_PANIC);
// ensure the shared system font is mapped
if (R_FAILED (fontEnsureMapped ()))
svcBreak (USERBREAK_PANIC);
// load the glyph texture sheets
auto const font = fontGetSystemFont ();
auto const fontInfo = fontGetInfo (font);
auto const glyphInfo = fontGetGlyphInfo (font);
assert (s_fontTextures.empty ());
s_fontTextures.resize (glyphInfo->nSheets + 1);
std::memset (s_fontTextures.data (), 0x00, s_fontTextures.size () * sizeof (s_fontTextures[0]));
s_textScale = 30.0f / glyphInfo->cellHeight;
for (unsigned i = 0; i < glyphInfo->nSheets; ++i)
{
auto &tex = s_fontTextures[i];
tex.data = fontGetGlyphSheetTex (font, i);
if (!tex.data)
svcBreak (USERBREAK_PANIC);
tex.fmt = static_cast<GPU_TEXCOLOR> (glyphInfo->sheetFmt);
tex.size = glyphInfo->sheetSize;
tex.width = glyphInfo->sheetWidth;
tex.height = glyphInfo->sheetHeight;
tex.param = GPU_TEXTURE_MAG_FILTER (GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER (GPU_LINEAR) |
GPU_TEXTURE_WRAP_S (GPU_REPEAT) | GPU_TEXTURE_WRAP_T (GPU_REPEAT);
tex.border = 0xFFFFFFFF;
tex.lodParam = 0;
}
{
auto &tex = s_fontTextures[glyphInfo->nSheets];
C3D_TexInit (&tex, 8, 8, GPU_A4);
std::uint32_t size;
auto data = C3D_Tex2DGetImagePtr (&tex, 0, &size);
if (!data || !size)
svcBreak (USERBREAK_PANIC);
std::memset (data, 0xFF, size);
}
ImWchar alterChar = fontCodePointFromGlyphIndex (font, fontInfo->alterCharIndex);
if (!alterChar)
alterChar = '?';
std::vector<ImWchar> charSet;
for (auto cmap = fontInfo->cmap; cmap; cmap = cmap->next)
{
switch (cmap->mappingMethod)
{
case CMAP_TYPE_DIRECT:
case CMAP_TYPE_TABLE:
assert (cmap->codeEnd >= cmap->codeBegin);
charSet.reserve (charSet.size () + cmap->codeEnd - cmap->codeBegin + 1);
for (auto i = cmap->codeBegin; i <= cmap->codeEnd; ++i)
charSet.emplace_back (i);
break;
case CMAP_TYPE_SCAN:
charSet.reserve (charSet.size () + cmap->nScanEntries);
for (unsigned i = 0; i < cmap->nScanEntries; ++i)
{
assert (cmap->scanEntries[i].code >= cmap->codeBegin);
assert (cmap->scanEntries[i].code <= cmap->codeEnd);
charSet.emplace_back (cmap->scanEntries[i].code);
}
break;
}
}
if (charSet.empty ())
svcBreak (USERBREAK_PANIC);
std::sort (std::begin (charSet), std::end (charSet));
charSet.erase (std::unique (std::begin (charSet), std::end (charSet)), std::end (charSet));
auto it = std::begin (charSet);
ImWchar start = *it++;
ImWchar prev = start;
while (it != std::end (charSet))
{
if (*it != prev + 1)
{
s_fontRanges.emplace_back (start);
s_fontRanges.emplace_back (prev);
start = *it;
}
prev = *it++;
}
s_fontRanges.emplace_back (start);
s_fontRanges.emplace_back (prev);
s_fontRanges.emplace_back (0);
auto const atlas = ImGui::GetIO ().Fonts;
atlas->Clear ();
atlas->TexWidth = glyphInfo->sheetWidth;
atlas->TexHeight = glyphInfo->sheetHeight * glyphInfo->nSheets;
atlas->TexUvScale = ImVec2 (1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight);
atlas->TexUvWhitePixel = ImVec2 (0.5f / 8.0f, glyphInfo->nSheets + 0.5f / 8.0f);
atlas->TexPixelsAlpha8 = static_cast<unsigned char *> (IM_ALLOC (1)); // dummy allocation
ImFontConfig config;
config.FontData = nullptr;
config.FontDataSize = 0;
config.FontDataOwnedByAtlas = true;
config.FontNo = 0;
config.SizePixels = 14.0f;
config.OversampleH = 3;
config.OversampleV = 1;
config.PixelSnapH = false;
config.GlyphExtraSpacing = ImVec2 (0.0f, 0.0f);
config.GlyphOffset = ImVec2 (0.0f, 0.0f);
config.GlyphRanges = s_fontRanges.data ();
config.GlyphMinAdvanceX = 0.0f;
config.GlyphMaxAdvanceX = std::numeric_limits<float>::max ();
config.MergeMode = false;
config.RasterizerFlags = 0;
config.RasterizerMultiply = 1.0f;
config.EllipsisChar = 0x2026;
std::memset (config.Name, 0, sizeof (config.Name));
auto const imFont = IM_NEW (ImFont);
config.DstFont = imFont;
atlas->ConfigData.push_back (config);
atlas->Fonts.push_back (imFont);
// atlas->CustomRectIds[0] = atlas->AddCustomRectRegular (0x80000000, 108 * 2 + 1, 27);
// atlas->CustomRects[0].X = 0;
// atlas->CustomRects[0].Y = 0;
atlas->SetTexID (s_fontTextures.data ());
imFont->FallbackAdvanceX = fontInfo->defaultWidth.charWidth;
imFont->FontSize = fontInfo->lineFeed;
fontGlyphPos_s glyphPos;
for (auto const &code : charSet)
{
auto const glyphIndex = fontGlyphIndexFromCodePoint (font, code);
if (glyphIndex < 0)
svcBreak (USERBREAK_PANIC);
fontCalcGlyphPos (&glyphPos,
font,
glyphIndex,
GLYPH_POS_CALC_VTXCOORD | GLYPH_POS_AT_BASELINE,
1.0f,
1.0f);
ImFontGlyph glyph;
glyph.Codepoint = code;
glyph.AdvanceX = glyphPos.xAdvance;
glyph.X0 = glyphPos.vtxcoord.left;
glyph.Y0 = glyphPos.vtxcoord.top;
glyph.X1 = glyphPos.vtxcoord.right;
glyph.Y1 = glyphPos.vtxcoord.bottom;
glyph.U0 = glyphPos.texcoord.left;
glyph.V0 = glyphPos.sheetIndex + glyphPos.texcoord.top;
glyph.U1 = glyphPos.texcoord.right;
glyph.V1 = glyphPos.sheetIndex + glyphPos.texcoord.bottom;
imFont->Glyphs.push_back (glyph);
imFont->MetricsTotalSurface +=
static_cast<int> ((glyph.U1 - glyph.U0) * atlas->TexWidth + 1.99f) *
static_cast<int> ((glyph.V1 - glyph.V0) * atlas->TexHeight + 1.99f);
}
imFont->BuildLookupTable ();
imFont->DisplayOffset.x = 0.0f;
imFont->DisplayOffset.y = fontInfo->ascent;
imFont->ContainerAtlas = atlas;
imFont->ConfigData = &atlas->ConfigData[0];
imFont->ConfigDataCount = 1;
imFont->FallbackChar = alterChar;
imFont->EllipsisChar = config.EllipsisChar;
imFont->Scale = 1.0f;
imFont->Ascent = fontInfo->ascent;
imFont->Descent = 0.0f;
}
void imgui::citro3d::exit ()
{
linearFree (s_idxData);
linearFree (s_vtxData);
assert (!s_fontTextures.empty ());
C3D_TexDelete (&s_fontTextures.back ());
shaderProgramFree (&s_program);
DVLB_Free (s_vsh);
C3D_RenderTargetDelete (s_bottom);
C3D_RenderTargetDelete (s_top);
C3D_Fini ();
}
void imgui::citro3d::newFrame ()
{
}
void imgui::citro3d::render ()
{
C3D_FrameBegin (C3D_FRAME_SYNCDRAW);
C3D_RenderTargetClear (s_top, C3D_CLEAR_ALL, CLEAR_COLOR, 0);
C3D_RenderTargetClear (s_bottom, C3D_CLEAR_ALL, CLEAR_COLOR, 0);
auto const drawData = ImGui::GetDrawData ();
if (drawData->CmdListsCount <= 0)
{
C3D_FrameEnd (0);
return;
}
unsigned width = drawData->DisplaySize.x * drawData->FramebufferScale.x;
unsigned height = drawData->DisplaySize.y * drawData->FramebufferScale.y;
if (width <= 0 || height <= 0)
{
C3D_FrameEnd (0);
return;
}
if (s_vtxSize < static_cast<std::size_t> (drawData->TotalVtxCount))
{
linearFree (s_vtxData);
// add 10% to avoid growing many frames in a row
s_vtxSize = drawData->TotalVtxCount * 1.1f;
s_vtxData = reinterpret_cast<ImDrawVert *> (linearAlloc (sizeof (ImDrawVert) * s_vtxSize));
if (!s_vtxData)
svcBreak (USERBREAK_PANIC);
}
if (s_idxSize < static_cast<std::size_t> (drawData->TotalIdxCount))
{
// add 10% to avoid growing many frames in a row
s_idxSize = drawData->TotalIdxCount * 1.1f;
s_idxData = reinterpret_cast<ImDrawIdx *> (linearAlloc (sizeof (ImDrawIdx) * s_idxSize));
if (!s_vtxData)
svcBreak (USERBREAK_PANIC);
}
// Will project scissor/clipping rectangles into framebuffer space
// (0,0) unless using multi-viewports
auto const clipOff = drawData->DisplayPos;
// (1,1) unless using retina display which are often (2,2)
auto const clipScale = drawData->FramebufferScale;
// copy data into vertex/index buffers
std::size_t offsetVtx = 0;
std::size_t offsetIdx = 0;
for (int i = 0; i < drawData->CmdListsCount; ++i)
{
auto const &cmdList = *drawData->CmdLists[i];
if (s_vtxSize - offsetVtx < static_cast<std::size_t> (cmdList.VtxBuffer.Size))
svcBreak (USERBREAK_PANIC);
if (s_idxSize - offsetIdx < static_cast<std::size_t> (cmdList.IdxBuffer.Size))
svcBreak (USERBREAK_PANIC);
std::memcpy (&s_vtxData[offsetVtx],
cmdList.VtxBuffer.Data,
sizeof (ImDrawVert) * cmdList.VtxBuffer.Size);
std::memcpy (&s_idxData[offsetIdx],
cmdList.IdxBuffer.Data,
sizeof (ImDrawIdx) * cmdList.IdxBuffer.Size);
offsetVtx += cmdList.VtxBuffer.Size;
offsetIdx += cmdList.IdxBuffer.Size;
}
for (auto const &screen : {GFX_TOP, GFX_BOTTOM})
{
if (screen == GFX_TOP)
C3D_FrameDrawOn (s_top);
else
C3D_FrameDrawOn (s_bottom);
setupRenderState (screen);
offsetVtx = 0;
offsetIdx = 0;
// Render command lists
for (int i = 0; i < drawData->CmdListsCount; ++i)
{
auto const &cmdList = *drawData->CmdLists[i];
for (auto const &cmd : cmdList.CmdBuffer)
{
if (cmd.UserCallback)
{
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user
// to request the renderer to reset render state.)
if (cmd.UserCallback == ImDrawCallback_ResetRenderState)
setupRenderState (screen);
else
cmd.UserCallback (&cmdList, &cmd);
}
else
{
// Project scissor/clipping rectangles into framebuffer space
ImVec4 clip;
clip.x = (cmd.ClipRect.x - clipOff.x) * clipScale.x;
clip.y = (cmd.ClipRect.y - clipOff.y) * clipScale.y;
clip.z = (cmd.ClipRect.z - clipOff.x) * clipScale.x;
clip.w = (cmd.ClipRect.w - clipOff.y) * clipScale.y;
if (clip.x >= width || clip.y >= height || clip.z < 0.0f || clip.w < 0.0f)
continue;
if (clip.x < 0.0f)
clip.x = 0.0f;
if (clip.y < 0.0f)
clip.y = 0.0f;
if (screen == GFX_TOP)
{
// check if clip starts on bottom screen
if (clip.y > 240.0f)
continue;
auto const x1 = std::clamp<unsigned> (240.0f - clip.w, 0, 240);
auto const y1 = std::clamp<unsigned> (400.0f - clip.z, 0, 400);
auto const x2 = std::clamp<unsigned> (240.0f - clip.y, 0, 240);
auto const y2 = std::clamp<unsigned> (400.0f - clip.x, 0, 400);
C3D_SetScissor (GPU_SCISSOR_NORMAL, x1, y1, x2, y2);
}
else
{
// check if clip ends on top screen
if (clip.w < 240.0f)
continue;
// check if clip ends before left edge of bottom screen
if (clip.z < 40.0f)
continue;
// check if clip starts after right edge of bottom screen
if (clip.x > 360.0f)
continue;
auto const x1 = std::clamp<unsigned> (480.0f - clip.w, 0, 240);
auto const y1 = std::clamp<unsigned> (360.0f - clip.z, 0, 320);
auto const x2 = std::clamp<unsigned> (480.0f - clip.y, 0, 240);
auto const y2 = std::clamp<unsigned> (360.0f - clip.x, 0, 320);
if (s_boundScissor[0] != x1 || s_boundScissor[1] != y1 ||
s_boundScissor[2] != x2 || s_boundScissor[3] != y2)
{
s_boundScissor[0] = x1;
s_boundScissor[1] = y1;
s_boundScissor[2] = x2;
s_boundScissor[3] = y2;
C3D_SetScissor (GPU_SCISSOR_NORMAL, x1, y1, x2, y2);
}
}
auto const vtxData = &s_vtxData[cmd.VtxOffset + offsetVtx];
if (vtxData != s_boundVtxData)
{
s_boundVtxData = &s_vtxData[cmd.VtxOffset + offsetVtx];
auto const bufInfo = C3D_GetBufInfo ();
BufInfo_Init (bufInfo);
BufInfo_Add (bufInfo, s_boundVtxData, sizeof (ImDrawVert), 3, 0x210);
}
auto tex = static_cast<C3D_Tex *> (cmd.TextureId);
if (tex == s_fontTextures.data ())
{
assert (cmd.ElemCount % 3 == 0);
// TODO get by idx not consecutive vtx
auto const getSheet = [] (auto const vtx_, auto const idx_) {
unsigned const sheet = std::min (
{vtx_[idx_[0]].uv.y, vtx_[idx_[1]].uv.y, vtx_[idx_[2]].uv.y});
for (unsigned i = 0; i < 3; ++i)
assert (vtx_[idx_[i]].uv.y - sheet <= 1.0f);
return sheet;
};
unsigned boundSheet = getSheet (&s_vtxData[cmd.VtxOffset + offsetVtx],
&s_idxData[cmd.IdxOffset + offsetIdx]);
unsigned offset = 0;
C3D_TexBind (0, &s_fontTextures[boundSheet]);
auto const env = C3D_GetTexEnv (0);
C3D_TexEnvInit (env);
C3D_TexEnvSrc (
env, C3D_RGB, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR);
C3D_TexEnvFunc (env, C3D_RGB, GPU_REPLACE);
C3D_TexEnvSrc (
env, C3D_Alpha, GPU_TEXTURE0, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR);
C3D_TexEnvFunc (env, C3D_Alpha, GPU_MODULATE);
for (unsigned i = 3; i < cmd.ElemCount; i += 3)
{
unsigned const sheet = getSheet (&s_vtxData[cmd.VtxOffset + offsetVtx],
&s_idxData[cmd.IdxOffset + offsetIdx + i]);
if (boundSheet != sheet)
{
C3D_DrawElements (GPU_TRIANGLES,
i - offset,
C3D_UNSIGNED_SHORT,
&s_idxData[cmd.IdxOffset + offsetIdx + offset]);
boundSheet = sheet;
offset = i;
C3D_TexBind (0, &s_fontTextures[boundSheet]);
}
}
assert ((cmd.ElemCount - offset) % 3 == 0);
C3D_DrawElements (GPU_TRIANGLES,
cmd.ElemCount - offset,
C3D_UNSIGNED_SHORT,
&s_idxData[cmd.IdxOffset + offsetIdx + offset]);
}
else
{
if (tex != s_boundTexture)
{
C3D_TexBind (0, tex);
auto const env = C3D_GetTexEnv (0);
C3D_TexEnvInit (env);
C3D_TexEnvSrc (
env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR);
C3D_TexEnvFunc (env, C3D_Both, GPU_MODULATE);
}
C3D_DrawElements (GPU_TRIANGLES,
cmd.ElemCount,
C3D_UNSIGNED_SHORT,
&s_idxData[cmd.IdxOffset + offsetIdx]);
}
s_boundTexture = tex;
}
}
offsetVtx += cmdList.VtxBuffer.Size;
offsetIdx += cmdList.IdxBuffer.Size;
}
}
C3D_FrameEnd (0);
}

View File

@ -0,0 +1,33 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
namespace imgui
{
namespace citro3d
{
void init ();
void exit ();
void newFrame ();
void render ();
}
}

163
source/3ds/imgui_ctru.cpp Normal file
View File

@ -0,0 +1,163 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "imgui_ctru.h"
#include "imgui.h"
#include "fs.h"
#include <3ds.h>
#include <chrono>
#include <cstring>
#include <functional>
#include <string>
#include <tuple>
using namespace std::chrono_literals;
namespace
{
constexpr auto SCREEN_WIDTH = 400.0f;
constexpr auto SCREEN_HEIGHT = 480.0f;
std::string s_clipboard;
char const *getClipboardText (void *const userData_)
{
(void)userData_;
return s_clipboard.c_str ();
}
void setClipboardText (void *const userData_, char const *const text_)
{
(void)userData_;
s_clipboard = text_;
}
void updateTouch (ImGuiIO &io_)
{
if (!((hidKeysDown () | hidKeysHeld ()) & KEY_TOUCH))
{
io_.MousePos = ImVec2 (-10.0f, -10.0f);
io_.MouseDown[0] = false;
return;
}
touchPosition pos;
hidTouchRead (&pos);
// transform to bottom-screen space
io_.MousePos = ImVec2 ((pos.px + 40.0f) * 2.0f, (pos.py + 240.0f) * 2.0f);
io_.MouseDown[0] = true;
}
void updateGamepads (ImGuiIO &io_)
{
std::memset (io_.NavInputs, 0, sizeof (io_.NavInputs));
auto const buttonMapping = {
std::make_pair (KEY_A, ImGuiNavInput_Activate),
std::make_pair (KEY_B, ImGuiNavInput_Cancel),
std::make_pair (KEY_X, ImGuiNavInput_Input),
std::make_pair (KEY_Y, ImGuiNavInput_Menu),
std::make_pair (KEY_L, ImGuiNavInput_FocusPrev),
std::make_pair (KEY_L, ImGuiNavInput_TweakSlow),
std::make_pair (KEY_R, ImGuiNavInput_FocusNext),
std::make_pair (KEY_R, ImGuiNavInput_TweakFast),
std::make_pair (KEY_DUP, ImGuiNavInput_DpadUp),
std::make_pair (KEY_DRIGHT, ImGuiNavInput_DpadRight),
std::make_pair (KEY_DDOWN, ImGuiNavInput_DpadDown),
std::make_pair (KEY_DLEFT, ImGuiNavInput_DpadLeft),
};
auto const keys = hidKeysHeld ();
for (auto const &[in, out] : buttonMapping)
{
if (keys & in)
io_.NavInputs[out] = 1.0f;
}
circlePosition cpad;
auto const analogMapping = {
std::make_tuple (std::ref (cpad.dx), ImGuiNavInput_LStickLeft, -0.3f, -0.9f),
std::make_tuple (std::ref (cpad.dx), ImGuiNavInput_LStickRight, +0.3f, +0.9f),
std::make_tuple (std::ref (cpad.dy), ImGuiNavInput_LStickUp, +0.3f, +0.9f),
std::make_tuple (std::ref (cpad.dy), ImGuiNavInput_LStickDown, -0.3f, -0.9f),
};
hidCircleRead (&cpad);
for (auto const &[in, out, min, max] : analogMapping)
{
auto const value = in / static_cast<float> (0x9C);
auto const v = std::min (1.0f, (value - min) / (max - min));
io_.NavInputs[out] = std::max (io_.NavInputs[out], v);
}
}
}
bool imgui::ctru::init ()
{
ImGuiIO &io = ImGui::GetIO ();
io.IniFilename = nullptr;
io.ConfigFlags |= ImGuiConfigFlags_IsTouchScreen;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
io.BackendPlatformName = "3ds";
io.MouseDrawCursor = false;
io.SetClipboardTextFn = setClipboardText;
io.GetClipboardTextFn = getClipboardText;
io.ClipboardUserData = nullptr;
return true;
}
void imgui::ctru::newFrame ()
{
ImGuiIO &io = ImGui::GetIO ();
IM_ASSERT (io.Fonts->IsBuilt () &&
"Font atlas not built! It is generally built by the renderer back-end. Missing call "
"to renderer _NewFrame() function?");
io.DisplaySize = ImVec2 (SCREEN_WIDTH * 2.0f, SCREEN_HEIGHT * 2.0f);
io.DisplayFramebufferScale = ImVec2 (0.5f, 0.5f);
// Setup time step
static auto const start = svcGetSystemTick ();
static auto prev = start;
auto const now = svcGetSystemTick ();
io.DeltaTime = (now - prev) / static_cast<float> (SYSCLOCK_ARM11);
prev = now;
updateTouch (io);
updateGamepads (io);
}
void imgui::ctru::exit ()
{
}

32
source/3ds/imgui_ctru.h Normal file
View File

@ -0,0 +1,32 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
namespace imgui
{
namespace ctru
{
bool init ();
void exit ();
void newFrame ();
}
}

348
source/3ds/platform.cpp Normal file
View File

@ -0,0 +1,348 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "platform.h"
#include "fs.h"
#include "log.h"
#include "imgui_citro3d.h"
#include "imgui_ctru.h"
#include "imgui.h"
#include <3ds.h>
#include <citro3d.h>
#include <tex3ds.h>
#include "gfx.h"
#include <cassert>
#include <chrono>
#include <ctime>
#include <malloc.h>
namespace
{
constexpr auto STACK_SIZE = 32768;
constexpr auto SOCU_ALIGN = 0x1000;
constexpr auto SOCU_BUFFERSIZE = 0x100000;
static_assert (SOCU_BUFFERSIZE % SOCU_ALIGN == 0);
bool s_socuActive = false;
u32 *s_socuBuffer = nullptr;
C3D_Tex s_gfxTexture;
Tex3DS_Texture s_gfxT3x;
void startNetwork ()
{
if (s_socuActive)
return;
std::uint32_t wifi = 0;
if (R_FAILED (ACU_GetWifiStatus (&wifi)) || !wifi)
return;
if (!s_socuBuffer)
s_socuBuffer = static_cast<u32 *> (::memalign (SOCU_ALIGN, SOCU_BUFFERSIZE));
if (!s_socuBuffer)
return;
if (R_FAILED (socInit (s_socuBuffer, SOCU_BUFFERSIZE)))
return;
s_socuActive = true;
Log::info ("Wifi connected\n");
}
void drawLogo ()
{
auto subTex = Tex3DS_GetSubTexture (s_gfxT3x, gfx_c3dlogo_idx);
ImGuiIO &io = ImGui::GetIO ();
auto const screenWidth = io.DisplaySize.x;
auto const screenHeight = io.DisplaySize.y;
auto const logoWidth = subTex->width / io.DisplayFramebufferScale.x;
auto const logoHeight = subTex->height / io.DisplayFramebufferScale.y;
auto const x1 = (screenWidth - logoWidth) / 2.0f;
auto const x2 = x1 + logoWidth;
auto const y1 = (screenHeight / 2.0f - logoHeight) / 2.0f;
auto const y2 = y1 + logoHeight;
auto const uv1 = ImVec2 (subTex->left, subTex->top);
auto const uv2 = ImVec2 (subTex->right, subTex->bottom);
ImGui::GetBackgroundDrawList ()->AddImage (
&s_gfxTexture, ImVec2 (x1, y1), ImVec2 (x2, y2), uv1, uv2);
ImGui::GetBackgroundDrawList ()->AddImage (&s_gfxTexture,
ImVec2 (x1, y1 + screenHeight / 2.0f),
ImVec2 (x2, y2 + screenHeight / 2.0f),
uv1,
uv2);
}
void drawStatus ()
{
constexpr unsigned batteryLevels[] = {
gfx_battery0_idx,
gfx_battery0_idx,
gfx_battery1_idx,
gfx_battery2_idx,
gfx_battery3_idx,
gfx_battery4_idx,
};
constexpr unsigned wifiLevels[] = {
gfx_wifiNull_idx,
gfx_wifi1_idx,
gfx_wifi2_idx,
gfx_wifi3_idx,
};
static u8 charging = 0;
static u8 level = 5;
PTMU_GetBatteryChargeState (&charging);
if (!charging)
{
PTMU_GetBatteryLevel (&level);
if (level >= std::extent_v<decltype (batteryLevels)>)
svcBreak (USERBREAK_PANIC);
}
auto const &io = ImGui::GetIO ();
auto const &style = ImGui::GetStyle ();
auto const screenWidth = io.DisplaySize.x;
auto const battery =
Tex3DS_GetSubTexture (s_gfxT3x, charging ? gfx_batteryCharge_idx : batteryLevels[level]);
auto const batteryWidth = battery->width / io.DisplayFramebufferScale.x;
auto const batteryHeight = battery->height / io.DisplayFramebufferScale.y;
auto const p1 = ImVec2 (screenWidth - batteryWidth, 0.0f);
auto const p2 = ImVec2 (screenWidth, batteryHeight);
auto const uv1 = ImVec2 (battery->left, battery->top);
auto const uv2 = ImVec2 (battery->right, battery->bottom);
ImGui::GetForegroundDrawList ()->AddImage (&s_gfxTexture, p1, p2, uv1, uv2);
auto const wifiStrength = osGetWifiStrength ();
auto const wifi = Tex3DS_GetSubTexture (s_gfxT3x, wifiLevels[wifiStrength]);
auto const wifiWidth = wifi->width / io.DisplayFramebufferScale.x;
auto const wifiHeight = wifi->height / io.DisplayFramebufferScale.y;
auto const p3 = ImVec2 (p1.x - wifiWidth - 4.0f, 0.0f);
auto const p4 = ImVec2 (p1.x - 4.0f, wifiHeight);
auto const uv3 = ImVec2 (wifi->left, wifi->top);
auto const uv4 = ImVec2 (wifi->right, wifi->bottom);
ImGui::GetForegroundDrawList ()->AddImage (&s_gfxTexture, p3, p4, uv3, uv4);
char buffer[64];
auto const now = std::time (nullptr);
std::strftime (buffer, sizeof (buffer), "%H:%M:%S", std::localtime (&now));
ImGui::GetForegroundDrawList ()->AddText (
ImVec2 (p3.x - 130.0f, style.FramePadding.y), 0xFFFFFFFF, buffer);
}
}
bool platform::init ()
{
osSetSpeedupEnable (true);
acInit ();
ptmuInit ();
romfsInit ();
gfxInitDefault ();
gfxSet3D (false);
sdmcWriteSafe (false);
#ifndef NDEBUG
consoleDebugInit (debugDevice_SVC);
std::setvbuf (stderr, nullptr, _IOLBF, 0);
#endif
IMGUI_CHECKVERSION ();
ImGui::CreateContext ();
if (!imgui::ctru::init ())
{
ImGui::DestroyContext ();
return false;
}
imgui::citro3d::init ();
{
fs::File file;
if (!file.open ("romfs:/gfx.t3x"))
svcBreak (USERBREAK_PANIC);
s_gfxT3x = Tex3DS_TextureImportStdio (file, &s_gfxTexture, nullptr, true);
if (!s_gfxT3x)
svcBreak (USERBREAK_PANIC);
}
C3D_TexSetFilter (&s_gfxTexture, GPU_LINEAR, GPU_LINEAR);
return true;
}
bool platform::loop ()
{
if (!aptMainLoop ())
return false;
startNetwork ();
hidScanInput ();
if (hidKeysDown () & KEY_START)
return false;
imgui::ctru::newFrame ();
imgui::citro3d::newFrame ();
ImGui::NewFrame ();
return true;
}
void platform::render ()
{
drawLogo ();
drawStatus ();
ImGui::Render ();
imgui::citro3d::render ();
}
void platform::exit ()
{
Tex3DS_TextureFree (s_gfxT3x);
C3D_TexDelete (&s_gfxTexture);
ImGui::DestroyContext ();
imgui::citro3d::exit ();
imgui::ctru::exit ();
socExit ();
s_socuActive = false;
std::free (s_socuBuffer);
gfxExit ();
ptmuExit ();
acExit ();
}
///////////////////////////////////////////////////////////////////////////
class platform::Thread::privateData_t
{
public:
privateData_t ()
{
if (thread)
threadFree (thread);
}
privateData_t (std::function<void ()> func_) : thread (nullptr)
{
s32 priority = 0x30;
svcGetThreadPriority (&priority, CUR_THREAD_HANDLE);
thread = threadCreate (&privateData_t::threadFunc, this, STACK_SIZE, priority, -1, false);
assert (thread);
}
static void threadFunc (void *const arg_)
{
auto const t = static_cast<privateData_t *> (arg_);
t->func ();
}
::Thread thread = nullptr;
std::function<void ()> func;
};
///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;
platform::Thread::Thread () : m_d (new privateData_t ())
{
}
platform::Thread::Thread (std::function<void ()> func_) : m_d (new privateData_t (func_))
{
}
platform::Thread::Thread (Thread &&that_) : m_d (new privateData_t ())
{
std::swap (m_d, that_.m_d);
}
platform::Thread &platform::Thread::operator= (Thread &&that_)
{
std::swap (m_d, that_.m_d);
return *this;
}
void platform::Thread::join ()
{
threadJoin (m_d->thread, UINT64_MAX);
}
void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
svcSleepThread (std::chrono::nanoseconds (timeout_).count ());
}
///////////////////////////////////////////////////////////////////////////
class platform::Mutex::privateData_t
{
public:
LightLock mutex;
};
///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;
platform::Mutex::Mutex () : m_d (new privateData_t ())
{
LightLock_Init (&m_d->mutex);
}
void platform::Mutex::lock ()
{
LightLock_Lock (&m_d->mutex);
}
void platform::Mutex::unlock ()
{
LightLock_Unlock (&m_d->mutex);
}

61
source/3ds/vshader.v.pica Normal file
View File

@ -0,0 +1,61 @@
; ftpd is a server implementation based on the following:
; - RFC 959 (https://tools.ietf.org/html/rfc959)
; - RFC 3659 (https://tools.ietf.org/html/rfc3659)
; - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
;
; Copyright (C) 2020 Michael Theall
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
; Example PICA200 vertex shader
; Uniforms
.fvec proj[4]
; Constants
.constf constants(1.0, 0.0, 0.00392156862745, 0.0)
; Outputs
.out outPos position
.out outUv texcoord0
.out outColor color
; Inputs (defined as aliases for convenience)
.alias inPos v0
.alias inUv v1
.alias inColor v2
.proc main
; Force inPos.z = 0.0, inPos.w = 1.0
mov r0.xy, inPos.xy
mov r0.zw, constants.yx
; outPos = proj * inPos
dp4 outPos.x, proj[0], r0
dp4 outPos.y, proj[1], r0
dp4 outPos.z, proj[2], r0
dp4 outPos.w, proj[3], r0
; outUv = inUv
mov outUv, inUv
; normalize inColor
mul r1, constants.zzzz, inColor
; outColor = inColor
mov outColor, r1
; We're finished
end
.end

View File

@ -1,276 +0,0 @@
#include "console.h"
#include <arpa/inet.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _3DS
#include <3ds.h>
#define CONSOLE_WIDTH 50
#define CONSOLE_HEIGHT 30
#elif defined(__SWITCH__)
#include <switch.h>
#define CONSOLE_WIDTH 80
#define CONSOLE_HEIGHT 45
#endif
#if defined(_3DS) || defined (__SWITCH__)
static PrintConsole status_console;
static PrintConsole main_console;
#endif
#if ENABLE_LOGGING
static bool disable_logging = false;
#endif
#if defined(_3DS)
static PrintConsole tcp_console;
/*! initialize console subsystem */
void
console_init(void)
{
consoleInit(GFX_TOP, &status_console);
consoleSetWindow(&status_console, 0, 0, 50, 1);
consoleInit(GFX_TOP, &main_console);
consoleSetWindow(&main_console, 0, 1, 50, 29);
consoleInit(GFX_BOTTOM, &tcp_console);
consoleSelect(&main_console);
}
/*! print tcp tables */
static void
print_tcp_table(void)
{
static SOCU_TCPTableEntry tcp_entries[32];
socklen_t optlen;
size_t i;
int rc, lines = 0;
#ifdef ENABLE_LOGGING
disable_logging = true;
#endif
consoleSelect(&tcp_console);
console_print("\x1b[0;0H\x1b[K");
optlen = sizeof(tcp_entries);
rc = SOCU_GetNetworkOpt(SOL_CONFIG, NETOPT_TCP_TABLE, tcp_entries, &optlen);
if(rc != 0 && errno != ENODEV)
console_print(RED "tcp table: %d %s\n\x1b[J\n" RESET, errno, strerror(errno));
else if(rc == 0)
{
for(i = 0; lines < 30 && i < optlen / sizeof(SOCU_TCPTableEntry); ++i)
{
SOCU_TCPTableEntry *entry = &tcp_entries[i];
struct sockaddr_in *local = (struct sockaddr_in*)&entry->local;
struct sockaddr_in *remote = (struct sockaddr_in*)&entry->remote;
console_print(GREEN "%stcp[%zu]: ", i == 0 ? "" : "\n", i);
switch(entry->state)
{
case TCP_STATE_CLOSED:
console_print("CLOSED\x1b[K");
local = remote = NULL;
break;
case TCP_STATE_LISTEN:
console_print("LISTEN\x1b[K");
remote = NULL;
break;
case TCP_STATE_ESTABLISHED:
console_print("ESTABLISHED\x1b[K");
break;
case TCP_STATE_FINWAIT1:
console_print("FINWAIT1\x1b[K");
break;
case TCP_STATE_FINWAIT2:
console_print("FINWAIT2\x1b[K");
break;
case TCP_STATE_CLOSE_WAIT:
console_print("CLOSE_WAIT\x1b[K");
break;
case TCP_STATE_LAST_ACK:
console_print("LAST_ACK\x1b[K");
break;
case TCP_STATE_TIME_WAIT:
console_print("TIME_WAIT\x1b[K");
break;
default:
console_print("State %lu\x1b[K", entry->state);
break;
}
++lines;
if(local && (lines++ < 30))
console_print("\n Local %s:%u\x1b[K", inet_ntoa(local->sin_addr),
ntohs(local->sin_port));
if(remote && (lines++ < 30))
console_print("\n Peer %s:%u\x1b[K", inet_ntoa(remote->sin_addr),
ntohs(remote->sin_port));
}
console_print(RESET "\x1b[J");
}
else
console_print("\x1b[2J");
consoleSelect(&main_console);
#ifdef ENABLE_LOGGING
disable_logging = false;
#endif
}
#elif defined(__SWITCH__)
/*! initialize console subsystem */
void
console_init(void)
{
consoleInit(&status_console);
consoleSetWindow(&status_console, 0, 0, CONSOLE_WIDTH, 1);
consoleInit( &main_console);
consoleSetWindow(&main_console, 0, 1, CONSOLE_WIDTH, CONSOLE_HEIGHT-1);
consoleSelect(&main_console);
}
#endif
#if defined(_3DS) || defined(__SWITCH__)
/*! set status bar contents
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
console_set_status(const char *fmt, ...)
{
va_list ap;
consoleSelect(&status_console);
va_start(ap, fmt);
vprintf(fmt, ap);
#ifdef ENABLE_LOGGING
vfprintf(stderr, fmt, ap);
#endif
va_end(ap);
consoleSelect(&main_console);
}
/*! add text to the console
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
console_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
#ifdef ENABLE_LOGGING
if(!disable_logging)
vfprintf(stderr, fmt, ap);
#endif
va_end(ap);
}
/*! print debug message
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
debug_print(const char *fmt, ...)
{
#ifdef ENABLE_LOGGING
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
#endif
}
/*! draw console to screen */
void
console_render(void)
{
/* print tcp table */
#ifdef _3DS
print_tcp_table();
#endif
/* flush framebuffer */
#ifdef _3DS
gfxFlushBuffers();
gspWaitForVBlank();
gfxSwapBuffers();
#endif
#ifdef __SWITCH__
consoleUpdate(NULL);
#endif
}
#else
/* this is a lot easier when you have a real console */
void
console_init(void)
{
}
void
console_set_status(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
fputc('\n', stdout);
}
void
console_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
void
debug_print(const char *fmt, ...)
{
#ifdef ENABLE_LOGGING
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
#endif
}
void console_render(void)
{
}
#endif

211
source/fs.cpp Normal file
View File

@ -0,0 +1,211 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "fs.h"
#include <cinttypes>
#include <cstdio>
std::string fs::printSize (std::uint64_t const size_)
{
constexpr std::uint64_t const KiB = 1024;
constexpr std::uint64_t const MiB = 1024 * KiB;
constexpr std::uint64_t const GiB = 1024 * MiB;
constexpr std::uint64_t const TiB = 1024 * GiB;
constexpr std::uint64_t const PiB = 1024 * TiB;
constexpr std::uint64_t const EiB = 1024 * PiB;
char buffer[64] = {};
for (auto const &[name, bin] : {
// clang-format off
std::make_pair ("EiB", EiB),
std::make_pair ("PiB", PiB),
std::make_pair ("TiB", TiB),
std::make_pair ("GiB", GiB),
std::make_pair ("MiB", MiB),
std::make_pair ("KiB", KiB)
// clang-format on
})
{
auto const whole = size_ / bin;
if (size_ >= 100 * bin)
{
std::sprintf (buffer, "%" PRIu64 "%s", whole, name);
return buffer;
}
auto const frac = size_ - whole * bin;
if (size_ >= 10 * bin)
{
std::sprintf (buffer, "%" PRIu64 ".%" PRIu64 "%s", whole, frac * 10 / bin, name);
return buffer;
}
if (size_ >= 1000 * (bin / KiB))
{
std::sprintf (buffer, "%" PRIu64 ".%02" PRIu64 "%s", whole, frac * 100 / bin, name);
return buffer;
}
}
std::sprintf (buffer, "%" PRIu64, size_);
return buffer;
}
///////////////////////////////////////////////////////////////////////////
fs::File::~File () = default;
fs::File::File () = default;
fs::File::File (File &&that_) = default;
fs::File &fs::File::operator= (File &&that_) = default;
fs::File::operator bool () const
{
return static_cast<bool> (m_fp);
}
fs::File::operator FILE * () const
{
return m_fp.get ();
}
void fs::File::setBufferSize (std::size_t const size_)
{
if (m_bufferSize != size_)
{
m_buffer = std::make_unique<char[]> (size_);
m_bufferSize = size_;
}
if (m_fp)
std::setvbuf (m_fp.get (), m_buffer.get (), _IOFBF, m_bufferSize);
}
bool fs::File::open (char const *const path_, char const *const mode_)
{
auto const fp = std::fopen (path_, mode_);
if (!fp)
return false;
m_fp = std::unique_ptr<std::FILE, int (*) (std::FILE *)> (fp, &std::fclose);
if (m_buffer)
std::setvbuf (m_fp.get (), m_buffer.get (), _IOFBF, m_bufferSize);
return true;
}
void fs::File::close ()
{
m_fp.reset ();
}
ssize_t fs::File::seek (std::size_t const pos_, int const origin_)
{
return std::fseek (m_fp.get (), pos_, origin_);
}
ssize_t fs::File::read (void *const data_, std::size_t const size_)
{
return std::fread (data_, 1, size_, m_fp.get ());
}
bool fs::File::readAll (void *const data_, std::size_t const size_)
{
auto p = static_cast<char *> (data_);
std::size_t bytes = 0;
while (bytes < size_)
{
auto const rc = read (p, size_ - bytes);
if (rc <= 0)
return false;
p += rc;
bytes += rc;
}
return true;
}
ssize_t fs::File::write (void const *const data_, std::size_t const size_)
{
return std::fwrite (data_, 1, size_, m_fp.get ());
}
bool fs::File::writeAll (void const *const data_, std::size_t const size_)
{
auto p = static_cast<char const *> (data_);
std::size_t bytes = 0;
while (bytes < size_)
{
auto const rc = write (p, size_ - bytes);
if (rc <= 0)
return false;
p += rc;
bytes += rc;
}
return true;
}
///////////////////////////////////////////////////////////////////////////
fs::Dir::~Dir () = default;
fs::Dir::Dir () = default;
fs::Dir::Dir (Dir &&that_) = default;
fs::Dir &fs::Dir::operator= (Dir &&that_) = default;
fs::Dir::operator bool () const
{
return static_cast<bool> (m_dp);
}
fs::Dir::operator DIR * () const
{
return m_dp.get ();
}
bool fs::Dir::open (char const *const path_)
{
auto const dp = ::opendir (path_);
if (!dp)
return false;
m_dp = std::unique_ptr<DIR, int (*) (DIR *)> (dp, &::closedir);
return true;
}
void fs::Dir::close ()
{
m_dp.reset ();
}
struct dirent *fs::Dir::read ()
{
return ::readdir (m_dp.get ());
}

File diff suppressed because it is too large Load Diff

253
source/ftpServer.cpp Normal file
View File

@ -0,0 +1,253 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "ftpServer.h"
#include "fs.h"
#include "imgui.h"
#include <arpa/inet.h>
#include <poll.h>
#include <unistd.h>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <mutex>
#include <thread>
using namespace std::chrono_literals;
#ifndef _3DS
#define MULTITHREADED 1
#else
#define MULTITHREADED 0
#endif
namespace
{
auto const s_startTime = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now ());
platform::Mutex s_lock;
std::string s_freeSpace;
}
///////////////////////////////////////////////////////////////////////////
FtpServer::~FtpServer ()
{
m_quit = true;
m_thread.join ();
}
FtpServer::FtpServer (std::uint16_t const port_)
: m_log (Log::create ()), m_port (port_), m_quit (false)
{
Log::bind (m_log);
handleStartButton ();
#if MULTITHREADED
m_thread = platform::Thread (std::bind (&FtpServer::threadFunc, this));
#endif
}
void FtpServer::draw ()
{
#if !MULTITHREADED
loop ();
#endif
ImGuiIO &io = ImGui::GetIO ();
auto const width = io.DisplaySize.x;
auto const height = io.DisplaySize.y;
ImGui::SetNextWindowPos (ImVec2 (0, 0), ImGuiCond_FirstUseEver);
#ifdef _3DS
// top screen
ImGui::SetNextWindowSize (ImVec2 (width, height / 2.0f));
#else
ImGui::SetNextWindowSize (ImVec2 (width, height));
#endif
ImGui::Begin (STATUS_STRING,
nullptr,
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
{
auto const lock = std::scoped_lock (m_lock);
if (!m_socket)
{
if (ImGui::Button ("Start"))
handleStartButton ();
}
else if (ImGui::Button ("Stop"))
handleStopButton ();
if (m_socket)
{
ImGui::SameLine ();
ImGui::TextUnformatted (m_name.c_str ());
}
}
{
auto const lock = std::scoped_lock (s_lock);
if (!s_freeSpace.empty ())
{
ImGui::SameLine ();
ImGui::TextUnformatted (s_freeSpace.c_str ());
}
}
ImGui::Separator ();
#ifdef _3DS
ImGui::BeginChild ("Logs", ImVec2 (0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
#else
ImGui::BeginChild ("Logs", ImVec2 (0, 200), false, ImGuiWindowFlags_HorizontalScrollbar);
#endif
m_log->draw ();
ImGui::EndChild ();
#ifdef _3DS
ImGui::End ();
// bottom screen
ImGui::SetNextWindowSize (ImVec2 (width * 0.8f, height / 2.0f));
ImGui::SetNextWindowPos (ImVec2 (width * 0.1f, height / 2.0f), ImGuiCond_FirstUseEver);
ImGui::Begin ("Sessions",
nullptr,
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
#else
ImGui::Separator ();
#endif
for (auto &session : m_sessions)
session->draw ();
ImGui::End ();
}
UniqueFtpServer FtpServer::create (std::uint16_t const port_)
{
updateFreeSpace ();
return UniqueFtpServer (new FtpServer (port_));
}
void FtpServer::updateFreeSpace ()
{
#if defined(_3DS) || defined(__SWITCH__)
struct statvfs st;
if (::statvfs ("sdmc:/", &st) != 0)
return;
auto const lock = std::scoped_lock (s_lock);
s_freeSpace = fs::printSize (static_cast<std::uint64_t> (st.f_bsize) * st.f_bfree);
#endif
}
std::time_t FtpServer::startTime ()
{
return s_startTime;
}
void FtpServer::handleStartButton ()
{
if (m_socket)
return;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
#if defined(_3DS) || defined(__SWITCH__)
addr.sin_addr.s_addr = gethostid ();
#else
addr.sin_addr.s_addr = INADDR_ANY;
#endif
addr.sin_port = htons (m_port);
auto socket = Socket::create ();
if (!socket)
return;
if (m_port != 0 && !socket->setReuseAddress (true))
return;
if (!socket->bind (addr))
return;
if (!socket->listen (10))
return;
auto const &sockName = socket->sockName ();
auto const name = sockName.name ();
m_name.resize (std::strlen (name) + 3 + 5);
m_name.resize (std::sprintf (&m_name[0], "[%s]:%u", name, sockName.port ()));
Log::info ("Started server at %s\n", m_name.c_str ());
m_socket = std::move (socket);
}
void FtpServer::handleStopButton ()
{
m_socket.reset ();
Log::info ("Stopped server at %s\n", m_name.c_str ());
}
void FtpServer::loop ()
{
{
auto const lock = std::scoped_lock (m_lock);
if (m_socket)
{
Socket::PollInfo info{*m_socket, POLLIN, 0};
if (Socket::poll (&info, 1, 0ms) > 0)
{
auto socket = m_socket->accept ();
if (socket)
m_sessions.emplace_back (FtpSession::create (std::move (socket)));
}
}
}
for (auto it = std::begin (m_sessions); it != std::end (m_sessions);)
{
auto const &session = *it;
if (session->dead ())
it = m_sessions.erase (it);
else
++it;
}
if (!m_sessions.empty ())
FtpSession::poll (m_sessions);
#if MULTITHREADED
else
platform::Thread::sleep (16ms);
#endif
}
void FtpServer::threadFunc ()
{
Log::bind (m_log);
while (!m_quit)
loop ();
}

2482
source/ftpSession.cpp Normal file

File diff suppressed because it is too large Load Diff

1
source/imgui/imgui.cpp Normal file
View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui/releases/tag/v1.75"

107
source/ioBuffer.cpp Normal file
View File

@ -0,0 +1,107 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "ioBuffer.h"
#include <cassert>
#include <cstring>
///////////////////////////////////////////////////////////////////////////
IOBuffer::~IOBuffer () = default;
IOBuffer::IOBuffer (std::size_t const size_)
: m_buffer (std::make_unique<char[]> (size_)), m_size (size_)
{
}
char *IOBuffer::freeArea () const
{
assert (m_end < m_size);
return &m_buffer[m_end];
}
std::size_t IOBuffer::freeSize () const
{
assert (m_size >= m_end);
return m_size - m_end;
}
void IOBuffer::markFree (std::size_t size_)
{
assert (m_end >= m_start);
assert (m_end - m_start >= size_);
m_start += size_;
// reset back to beginning
if (m_start == m_end)
{
m_start = 0;
m_end = 0;
}
}
char *IOBuffer::usedArea () const
{
assert (m_start < m_size);
return &m_buffer[m_start];
}
std::size_t IOBuffer::usedSize () const
{
assert (m_end >= m_start);
return m_end - m_start;
}
void IOBuffer::markUsed (std::size_t size_)
{
assert (m_size >= m_end);
assert (m_size - m_end >= size_);
m_end += size_;
}
bool IOBuffer::empty () const
{
assert (m_end >= m_start);
return (m_end - m_start) == 0;
}
std::size_t IOBuffer::capacity () const
{
return m_size;
}
void IOBuffer::clear ()
{
m_start = 0;
m_end = 0;
}
void IOBuffer::coalesce ()
{
assert (m_size >= m_end);
assert (m_end >= m_start);
auto const size = m_end - m_start;
if (size != 0)
std::memmove (&m_buffer[0], &m_buffer[m_start], size);
m_end -= m_start;
m_start = 0;
}

196
source/log.cpp Normal file
View File

@ -0,0 +1,196 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "log.h"
#include "imgui.h"
#include <mutex>
namespace
{
#ifdef _3DS
constexpr auto MAX_LOGS = 250;
#else
constexpr auto MAX_LOGS = 10000;
#endif
thread_local WeakLog s_log;
static char const *const s_prefix[] = {
[Log::DEBUG] = "[DEBUG]",
[Log::INFO] = "[INFO]",
[Log::ERROR] = "[ERROR]",
[Log::COMMAND] = "[COMMAND]",
[Log::RESPONSE] = "[RESPONSE]",
};
}
///////////////////////////////////////////////////////////////////////////
Log::~Log () = default;
Log::Log () = default;
void Log::draw ()
{
auto const lock = std::scoped_lock (m_lock);
if (m_messages.size () > MAX_LOGS)
{
auto const begin = std::begin (m_messages);
auto const end = std::next (begin, m_messages.size () - MAX_LOGS);
m_messages.erase (begin, end);
}
static ImVec4 const s_colors[] = {
[Log::DEBUG] = ImVec4 (1.0f, 1.0f, 0.4f, 1.0f),
[Log::INFO] = ImVec4 (1.0f, 1.0f, 1.0f, 1.0f),
[Log::ERROR] = ImVec4 (1.0f, 0.4f, 0.4f, 1.0f),
[Log::COMMAND] = ImVec4 (0.4f, 1.0f, 0.4f, 1.0f),
[Log::RESPONSE] = ImVec4 (0.4f, 1.0f, 1.0f, 1.0f),
};
static char const *const s_prefix[] = {
[Log::DEBUG] = "[DEBUG]",
[Log::INFO] = "[INFO]",
[Log::ERROR] = "[ERROR]",
[Log::COMMAND] = "[COMMAND]",
[Log::RESPONSE] = "[RESPONSE]",
};
for (auto const &message : m_messages)
{
ImGui::PushStyleColor (ImGuiCol_Text, s_colors[message.level]);
ImGui::TextUnformatted (s_prefix[message.level]);
ImGui::SameLine ();
ImGui::TextUnformatted (message.message.c_str ());
ImGui::PopStyleColor ();
}
// auto scroll if scroll bar is at end
if (ImGui::GetScrollY () >= ImGui::GetScrollMaxY ())
ImGui::SetScrollHereY (1.0f);
}
SharedLog Log::create ()
{
return std::shared_ptr<Log> (new Log ());
}
void Log::bind (SharedLog log_)
{
s_log = log_;
}
void Log::debug (char const *const fmt_, ...)
{
#ifndef NDEBUG
va_list ap;
va_start (ap, fmt_);
log (DEBUG, fmt_, ap);
va_end (ap);
#endif
}
void Log::info (char const *const fmt_, ...)
{
va_list ap;
va_start (ap, fmt_);
log (INFO, fmt_, ap);
va_end (ap);
}
void Log::error (char const *const fmt_, ...)
{
va_list ap;
va_start (ap, fmt_);
log (ERROR, fmt_, ap);
va_end (ap);
}
void Log::command (char const *const fmt_, ...)
{
va_list ap;
va_start (ap, fmt_);
log (COMMAND, fmt_, ap);
va_end (ap);
}
void Log::response (char const *const fmt_, ...)
{
va_list ap;
va_start (ap, fmt_);
log (RESPONSE, fmt_, ap);
va_end (ap);
}
void Log::log (Level const level_, char const *const fmt_, va_list ap_)
{
#ifdef NDEBUG
if (level_ == DEBUG)
return;
#endif
auto log = s_log.lock ();
if (!log)
return;
thread_local static char buffer[1024];
std::vsnprintf (buffer, sizeof (buffer), fmt_, ap_);
buffer[sizeof (buffer) - 1] = '\0';
auto const lock = std::scoped_lock (log->m_lock);
#ifndef NDEBUG
std::fprintf (stderr, "%s", s_prefix[level_]);
std::fputs (buffer, stderr);
#endif
log->m_messages.emplace_back (level_, buffer);
}
void Log::log (Level const level_, std::string_view const message_)
{
#ifdef NDEBUG
if (level_ == DEBUG)
return;
#endif
auto log = s_log.lock ();
if (!log)
return;
auto msg = std::string (message_);
for (auto &c : msg)
{
if (c == '\0')
c = '?';
}
auto const lock = std::scoped_lock (log->m_lock);
#ifndef NDEBUG
std::fprintf (stderr, "%s", s_prefix[level_]);
std::fwrite (msg.data (), 1, msg.size (), stderr);
#endif
log->m_messages.emplace_back (level_, msg);
}

View File

@ -1,182 +0,0 @@
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#ifdef _3DS
#include <3ds.h>
#elif defined(__SWITCH__)
#include <switch.h>
#endif
#include "console.h"
#include "ftp.h"
/*! looping mechanism
*
* @param[in] callback function to call during each iteration
*
* @returns loop status
*/
static loop_status_t
loop(loop_status_t (*callback)(void))
{
loop_status_t status = LOOP_CONTINUE;
#ifdef _3DS
while(aptMainLoop())
{
status = callback();
console_render();
if(status != LOOP_CONTINUE)
return status;
}
return LOOP_EXIT;
#elif defined(__SWITCH__)
while(appletMainLoop())
{
console_render();
status = callback();
if(status != LOOP_CONTINUE)
return status;
}
return LOOP_EXIT;
#else
while(status == LOOP_CONTINUE)
status = callback();
return status;
#endif
}
#ifdef _3DS
/*! wait until the B button is pressed
*
* @returns loop status
*/
static loop_status_t
wait_for_b(void)
{
/* update button state */
hidScanInput();
/* check if B was pressed */
if(hidKeysDown() & KEY_B)
return LOOP_EXIT;
/* B was not pressed */
return LOOP_CONTINUE;
}
#elif defined(__SWITCH__)
/*! wait until the B button is pressed
*
* @returns loop status
*/
static loop_status_t
wait_for_b(void)
{
/* update button state */
hidScanInput();
/* check if B was pressed */
if(hidKeysDown(CONTROLLER_P1_AUTO) & KEY_B)
return LOOP_EXIT;
/* B was not pressed */
return LOOP_CONTINUE;
}
#endif
/*! entry point
*
* @param[in] argc unused
* @param[in] argv unused
*
* returns exit status
*/
int
main(int argc,
char *argv[])
{
loop_status_t status = LOOP_RESTART;
#ifdef _3DS
/* initialize needed 3DS services */
acInit();
gfxInitDefault();
gfxSet3D(false);
sdmcWriteSafe(false);
#elif defined(__SWITCH__)
/* initialize needed Switch services */
nifmInitialize(NifmServiceType_User);
#endif
/* initialize console subsystem */
console_init();
#ifdef ENABLE_LOGGING
/* open log file */
#ifdef _3DS
FILE *fp = freopen("/ftpd.log", "wb", stderr);
#else
FILE *fp = freopen("ftpd.log", "wb", stderr);
#endif
if(fp == NULL)
{
console_print(RED "freopen: 0x%08X\n" RESET, errno);
goto log_fail;
}
/* truncate log file */
if(ftruncate(fileno(fp), 0) != 0)
{
console_print(RED "ftruncate: 0x%08X\n" RESET, errno);
goto log_fail;
}
#endif
console_set_status("\n" GREEN STATUS_STRING
#ifdef ENABLE_LOGGING
" DEBUG"
#endif
RESET);
while(status == LOOP_RESTART)
{
/* initialize ftp subsystem */
if(ftp_init() == 0)
{
/* ftp loop */
status = loop(ftp_loop);
/* done with ftp */
ftp_exit();
}
else
status = LOOP_EXIT;
}
#if defined(_3DS) || defined(__SWITCH__)
console_print("Press B to exit\n");
#endif
#ifdef ENABLE_LOGGING
log_fail:
if(fclose(stderr) != 0)
console_print(RED "fclose(%d): 0x%08X\n" RESET, fileno(stderr), errno);
#endif
#ifdef _3DS
loop(wait_for_b);
/* deinitialize 3DS services */
gfxExit();
acExit();
#elif defined(__SWITCH__)
loop(wait_for_b);
/* deinitialize Switch services */
consoleExit(NULL);
nifmExit();
#endif
return 0;
}

52
source/main.cpp Normal file
View File

@ -0,0 +1,52 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "platform.h"
#include "ftpServer.h"
#include "imgui.h"
#include <cstdio>
#include <cstdlib>
int main (int argc_, char *argv_[])
{
if (!platform::init ())
return EXIT_FAILURE;
auto &style = ImGui::GetStyle ();
style.WindowRounding = 0.0f;
#ifdef _3DS
style.Colors[ImGuiCol_WindowBg].w = 0.5f;
#endif
auto server = FtpServer::create (5000);
while (platform::loop ())
{
server->draw ();
platform::render ();
}
platform::exit ();
}

681
source/nx/imgui_deko3d.cpp Normal file
View File

@ -0,0 +1,681 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "imgui_deko3d.h"
#include "imgui.h"
#include "fs.h"
#include "log.h"
#include <deko3d.hpp>
#include <switch.h>
#include <zstd.h>
#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#define GLM_FORCE_INTRINSICS
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/mat4x4.hpp>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <numeric>
namespace
{
constexpr auto LOGO_WIDTH = 500;
constexpr auto LOGO_HEIGHT = 493;
constexpr auto FB_NUM = 2u;
constexpr auto CMDBUF_SIZE = 1024 * 1024;
constexpr auto DATABUF_SIZE = 1024 * 1024;
constexpr auto INDEXBUF_SIZE = 1024 * 1024;
constexpr auto IMAGEBUF_SIZE = 16 * 1024 * 1024;
struct VertUBO
{
glm::mat4 projMtx;
};
struct FragUBO
{
std::uint32_t font;
};
constexpr std::array VertexAttribState = {
// clang-format off
DkVtxAttribState{0, 0, offsetof (ImDrawVert, pos), DkVtxAttribSize_2x32, DkVtxAttribType_Float, 0},
DkVtxAttribState{0, 0, offsetof (ImDrawVert, uv), DkVtxAttribSize_2x32, DkVtxAttribType_Float, 0},
DkVtxAttribState{0, 0, offsetof (ImDrawVert, col), DkVtxAttribSize_4x8, DkVtxAttribType_Unorm, 0},
// clang-format on
};
constexpr std::array VertexBufferState = {
DkVtxBufferState{sizeof (ImDrawVert), 0},
};
dk::UniqueDevice s_device;
dk::UniqueMemBlock s_depthMemBlock;
dk::Image s_depthBuffer;
dk::UniqueMemBlock s_fbMemBlock;
dk::Image s_frameBuffers[FB_NUM];
dk::Image s_fontTexture;
dk::Image s_logoTexture;
dk::UniqueSwapchain s_swapchain;
dk::UniqueMemBlock s_codeMemBlock;
dk::Shader s_shaders[2];
dk::UniqueMemBlock s_uboMemBlock;
dk::UniqueMemBlock s_vtxMemBlock[FB_NUM];
dk::UniqueMemBlock s_idxMemBlock[FB_NUM];
dk::UniqueMemBlock s_cmdMemBlock[FB_NUM];
dk::UniqueCmdBuf s_cmdBuf[FB_NUM];
dk::UniqueMemBlock s_imageMemBlock;
dk::UniqueMemBlock s_descriptorMemBlock;
dk::UniqueQueue s_queue;
constexpr auto MAX_SAMPLERS = 1;
constexpr auto MAX_IMAGES = 2;
dk::SamplerDescriptor *s_samplerDescriptors = nullptr;
dk::ImageDescriptor *s_imageDescriptors = nullptr;
std::uintptr_t s_boundDescriptor = 0;
unsigned s_width = 0;
unsigned s_height = 0;
template <typename T, typename U>
constexpr inline std::uint32_t align (T const &size_, U const &align_)
{
return static_cast<std::uint32_t> (size_ + align_ - 1) & ~(align_ - 1);
}
void rebuildSwapchain (unsigned const width_, unsigned const height_)
{
s_swapchain = nullptr;
dk::ImageLayout depthLayout;
dk::ImageLayoutMaker{s_device}
.setFlags (DkImageFlags_UsageRender | DkImageFlags_HwCompression)
.setFormat (DkImageFormat_Z24S8)
.setDimensions (width_, height_)
.initialize (depthLayout);
auto const depthAlign = depthLayout.getAlignment ();
auto const depthSize = depthLayout.getSize ();
if (!s_depthMemBlock)
{
s_depthMemBlock = dk::MemBlockMaker{s_device,
align (depthSize, std::max<unsigned> (depthAlign, DK_MEMBLOCK_ALIGNMENT))}
.setFlags (DkMemBlockFlags_GpuCached | DkMemBlockFlags_Image)
.create ();
}
s_depthBuffer.initialize (depthLayout, s_depthMemBlock, 0);
dk::ImageLayout fbLayout;
dk::ImageLayoutMaker{s_device}
.setFlags (
DkImageFlags_UsageRender | DkImageFlags_UsagePresent | DkImageFlags_HwCompression)
.setFormat (DkImageFormat_RGBA8_Unorm)
.setDimensions (width_, height_)
.initialize (fbLayout);
auto const fbAlign = fbLayout.getAlignment ();
auto const fbSize = fbLayout.getSize ();
if (!s_fbMemBlock)
{
s_fbMemBlock = dk::MemBlockMaker{s_device,
align (FB_NUM * fbSize, std::max<unsigned> (fbAlign, DK_MEMBLOCK_ALIGNMENT))}
.setFlags (DkMemBlockFlags_GpuCached | DkMemBlockFlags_Image)
.create ();
}
std::array<DkImage const *, FB_NUM> swapchainImages;
for (unsigned i = 0; i < FB_NUM; ++i)
{
swapchainImages[i] = &s_frameBuffers[i];
s_frameBuffers[i].initialize (fbLayout, s_fbMemBlock, i * fbSize);
}
s_swapchain = dk::SwapchainMaker{s_device, nwindowGetDefault (), swapchainImages}.create ();
}
void loadShaders ()
{
struct ShaderFile
{
ShaderFile (dk::Shader &shader_, char const *const path_)
: shader (shader_), path (path_), size (getSize (path_))
{
}
static std::size_t getSize (char const *const path_)
{
struct stat st;
auto const rc = stat (path_, &st);
if (rc != 0)
{
std::fprintf (stderr, "stat(%s): %s\n", path_, std::strerror (errno));
std::abort ();
}
return st.st_size;
}
dk::Shader &shader;
char const *const path;
std::size_t const size;
};
auto shaderFiles = {ShaderFile{s_shaders[0], "romfs:/shaders/imgui_vsh.dksh"},
ShaderFile{s_shaders[1], "romfs:/shaders/imgui_fsh.dksh"}};
auto const codeSize = std::accumulate (std::begin (shaderFiles),
std::end (shaderFiles),
DK_SHADER_CODE_UNUSABLE_SIZE,
[] (auto const sum_, auto const &file_) {
return sum_ + align (file_.size, DK_SHADER_CODE_ALIGNMENT);
});
s_codeMemBlock = dk::MemBlockMaker{s_device, align (codeSize, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached |
DkMemBlockFlags_Code)
.create ();
auto const addr = static_cast<std::uint8_t *> (s_codeMemBlock.getCpuAddr ());
std::size_t offset = 0;
for (auto &file : shaderFiles)
{
std::uint32_t const codeOffset = offset;
fs::File fp;
if (!fp.open (file.path))
{
std::fprintf (stderr, "open(%s): %s\n", file.path, std::strerror (errno));
std::abort ();
}
if (!fp.readAll (&addr[offset], file.size))
{
std::fprintf (stderr, "read(%s): %s\n", file.path, std::strerror (errno));
std::abort ();
}
dk::ShaderMaker{s_codeMemBlock, codeOffset}.initialize (file.shader);
offset = align (offset + file.size, DK_SHADER_CODE_ALIGNMENT);
}
}
DkCmdList setupRenderState (int const slot_,
ImDrawData *const drawData_,
unsigned const width_,
unsigned const height_)
{
// Setup viewport, orthographic projection matrix
// Our visible imgui space lies from drawData_->DisplayPos (top left) to
// drawData_->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single
// viewport apps.
auto const L = drawData_->DisplayPos.x;
auto const R = drawData_->DisplayPos.x + drawData_->DisplaySize.x;
auto const T = drawData_->DisplayPos.y;
auto const B = drawData_->DisplayPos.y + drawData_->DisplaySize.y;
VertUBO vertUBO;
vertUBO.projMtx = glm::orthoRH_ZO (L, R, B, T, -1.0f, 1.0f);
s_cmdBuf[slot_].setViewports (0, DkViewport{0.0f, 0.0f, width_, height_});
s_cmdBuf[slot_].bindShaders (DkStageFlag_GraphicsMask, {&s_shaders[0], &s_shaders[1]});
s_cmdBuf[slot_].bindUniformBuffer (DkStage_Vertex,
0,
s_uboMemBlock.getGpuAddr (),
align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT));
s_cmdBuf[slot_].pushConstants (s_uboMemBlock.getGpuAddr (),
align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT),
0,
sizeof (VertUBO),
&vertUBO);
s_cmdBuf[slot_].bindUniformBuffer (DkStage_Fragment,
0,
s_uboMemBlock.getGpuAddr () + align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT),
align (sizeof (FragUBO), DK_UNIFORM_BUF_ALIGNMENT));
s_cmdBuf[slot_].bindRasterizerState (dk::RasterizerState{}.setCullMode (DkFace_None));
s_cmdBuf[slot_].bindColorState (dk::ColorState{}.setBlendEnable (0, true));
s_cmdBuf[slot_].bindColorWriteState (dk::ColorWriteState{});
s_cmdBuf[slot_].bindDepthStencilState (dk::DepthStencilState{}.setDepthTestEnable (false));
s_cmdBuf[slot_].bindBlendStates (0,
dk::BlendState{}.setFactors (DkBlendFactor_SrcAlpha,
DkBlendFactor_InvSrcAlpha,
DkBlendFactor_InvSrcAlpha,
DkBlendFactor_Zero));
s_cmdBuf[slot_].bindVtxAttribState (VertexAttribState);
s_cmdBuf[slot_].bindVtxBufferState (VertexBufferState);
return s_cmdBuf[slot_].finishList ();
}
}
void imgui::deko3d::init ()
{
// Setup back-end capabilities flags
ImGuiIO &io = ImGui::GetIO ();
io.BackendRendererName = "deko3d";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
}
void imgui::deko3d::exit ()
{
s_queue.waitIdle ();
s_queue = nullptr;
s_descriptorMemBlock = nullptr;
s_imageMemBlock = nullptr;
for (unsigned i = 0; i < FB_NUM; ++i)
{
s_cmdBuf[i] = nullptr;
s_cmdMemBlock[i] = nullptr;
s_idxMemBlock[i] = nullptr;
s_vtxMemBlock[i] = nullptr;
}
s_uboMemBlock = nullptr;
s_codeMemBlock = nullptr;
s_swapchain = nullptr;
s_fbMemBlock = nullptr;
s_depthMemBlock = nullptr;
s_device = nullptr;
}
void imgui::deko3d::newFrame ()
{
if (s_device)
return;
s_device = dk::DeviceMaker{}.create ();
rebuildSwapchain (1920, 1080);
loadShaders ();
s_uboMemBlock = dk::MemBlockMaker{s_device,
align (align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT) +
align (sizeof (FragUBO), DK_UNIFORM_BUF_ALIGNMENT),
DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
for (std::size_t i = 0; i < FB_NUM; ++i)
{
s_vtxMemBlock[i] = dk::MemBlockMaker{s_device, align (DATABUF_SIZE, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
s_idxMemBlock[i] = dk::MemBlockMaker{s_device, align (INDEXBUF_SIZE, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
s_cmdMemBlock[i] = dk::MemBlockMaker{s_device, align (CMDBUF_SIZE, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
s_cmdBuf[i] = dk::CmdBufMaker{s_device}.create ();
s_cmdBuf[i].addMemory (s_cmdMemBlock[i], 0, s_cmdMemBlock[i].getSize ());
}
s_queue = dk::QueueMaker{s_device}.setFlags (DkQueueFlags_Graphics).create ();
s_imageMemBlock = dk::MemBlockMaker{s_device, align (IMAGEBUF_SIZE, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_GpuCached | DkMemBlockFlags_Image)
.create ();
// Build texture atlas
ImGuiIO &io = ImGui::GetIO ();
io.Fonts->SetTexID (nullptr);
unsigned char *pixels;
int width;
int height;
io.Fonts->GetTexDataAsAlpha8 (&pixels, &width, &height);
dk::UniqueMemBlock memBlock =
dk::MemBlockMaker{s_device, align (width * height, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
std::memcpy (memBlock.getCpuAddr (), pixels, width * height);
static_assert (sizeof (dk::ImageDescriptor) == DK_IMAGE_DESCRIPTOR_ALIGNMENT);
static_assert (sizeof (dk::SamplerDescriptor) == DK_SAMPLER_DESCRIPTOR_ALIGNMENT);
static_assert (DK_IMAGE_DESCRIPTOR_ALIGNMENT == DK_SAMPLER_DESCRIPTOR_ALIGNMENT);
s_descriptorMemBlock = dk::MemBlockMaker{s_device,
align ((MAX_SAMPLERS + MAX_IMAGES) * sizeof (dk::ImageDescriptor), DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
s_samplerDescriptors =
static_cast<dk::SamplerDescriptor *> (s_descriptorMemBlock.getCpuAddr ());
s_imageDescriptors =
reinterpret_cast<dk::ImageDescriptor *> (&s_samplerDescriptors[MAX_SAMPLERS]);
s_samplerDescriptors[0].initialize (
dk::Sampler{}
.setFilter (DkFilter_Linear, DkFilter_Linear)
.setWrapMode (DkWrapMode_ClampToEdge, DkWrapMode_ClampToEdge, DkWrapMode_ClampToEdge));
auto &cmdBuf = s_cmdBuf[0];
dk::ImageLayout layout;
dk::ImageLayoutMaker{s_device}
.setFlags (0)
.setFormat (DkImageFormat_R8_Unorm)
.setDimensions (width, height)
.initialize (layout);
s_fontTexture.initialize (layout, s_imageMemBlock, 0);
s_imageDescriptors[0].initialize (s_fontTexture);
dk::ImageView imageView{s_fontTexture};
cmdBuf.copyBufferToImage ({memBlock.getGpuAddr ()}, imageView, {0, 0, 0, width, height, 1});
cmdBuf.bindSamplerDescriptorSet (s_descriptorMemBlock.getGpuAddr (), MAX_SAMPLERS);
cmdBuf.bindImageDescriptorSet (
s_descriptorMemBlock.getGpuAddr () + MAX_SAMPLERS * sizeof (dk::SamplerDescriptor),
MAX_IMAGES);
s_queue.submitCommands (cmdBuf.finishList ());
s_queue.waitIdle ();
{
auto const path = "romfs:/deko3d.rgba.zst";
struct stat st;
if (stat (path, &st) != 0)
{
std::fprintf (stderr, "stat(%s): %s\n", path, std::strerror (errno));
std::abort ();
}
fs::File fp;
if (!fp.open (path))
{
std::fprintf (stderr, "open(%s): %s\n", path, std::strerror (errno));
std::abort ();
}
std::vector<char> buffer (st.st_size);
if (!fp.readAll (buffer.data (), st.st_size))
{
std::fprintf (stderr, "read(%s): %s\n", path, std::strerror (errno));
std::abort ();
}
fp.close ();
auto const size = ZSTD_getFrameContentSize (buffer.data (), st.st_size);
if (ZSTD_isError (size))
{
std::fprintf (stderr, "ZSTD_getFrameContentSize: %s\n", ZSTD_getErrorName (size));
std::abort ();
}
memBlock = dk::MemBlockMaker{s_device, align (size, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
auto const decoded =
ZSTD_decompress (memBlock.getCpuAddr (), size, buffer.data (), st.st_size);
if (ZSTD_isError (decoded))
{
std::fprintf (stderr, "ZSTD_decompress: %s\n", ZSTD_getErrorName (decoded));
std::abort ();
}
dk::ImageLayout layout;
dk::ImageLayoutMaker{s_device}
.setFlags (0)
.setFormat (DkImageFormat_RGBA8_Unorm)
.setDimensions (LOGO_WIDTH, LOGO_HEIGHT)
.initialize (layout);
auto const offset = align (width * height, DK_IMAGE_LINEAR_STRIDE_ALIGNMENT);
s_logoTexture.initialize (layout, s_imageMemBlock, offset);
s_imageDescriptors[1].initialize (s_logoTexture);
dk::ImageView imageView{s_logoTexture};
cmdBuf.copyBufferToImage (
{memBlock.getGpuAddr ()}, imageView, {0, 0, 0, LOGO_WIDTH, LOGO_HEIGHT, 1});
s_queue.submitCommands (cmdBuf.finishList ());
s_queue.waitIdle ();
}
cmdBuf.clear ();
}
void imgui::deko3d::render ()
{
auto const drawData = ImGui::GetDrawData ();
if (drawData->CmdListsCount <= 0)
return;
unsigned width = drawData->DisplaySize.x * drawData->FramebufferScale.x;
unsigned height = drawData->DisplaySize.y * drawData->FramebufferScale.y;
if (width <= 0 || height <= 0)
return;
if (width != s_width || height != s_height)
{
s_width = width;
s_height = height;
s_queue.waitIdle ();
rebuildSwapchain (width, height);
}
auto const slot = s_queue.acquireImage (s_swapchain);
s_cmdBuf[slot].clear ();
dk::ImageView colorTarget{s_frameBuffers[slot]};
dk::ImageView depthTarget{s_depthBuffer};
s_cmdBuf[slot].bindRenderTargets (&colorTarget, &depthTarget);
s_cmdBuf[slot].clearColor (0, DkColorMask_RGBA, 0.125f, 0.294f, 0.478f, 1.0f);
s_cmdBuf[slot].clearDepthStencil (true, 1.0f, 0xFF, 0);
s_queue.submitCommands (s_cmdBuf[slot].finishList ());
// Setup desired render state
auto const setupCmd = setupRenderState (slot, drawData, width, height);
s_queue.submitCommands (setupCmd);
s_boundDescriptor = ~static_cast<std::uintptr_t> (0);
// Will project scissor/clipping rectangles into framebuffer space
// (0,0) unless using multi-viewports
auto const clipOff = drawData->DisplayPos;
// (1,1) unless using retina display which are often (2,2)
auto const clipScale = drawData->FramebufferScale;
if (s_vtxMemBlock[slot].getSize () < drawData->TotalVtxCount * sizeof (ImDrawVert))
{
s_vtxMemBlock[slot] = nullptr;
s_vtxMemBlock[slot] =
dk::MemBlockMaker{s_device,
align (drawData->TotalVtxCount * sizeof (ImDrawVert), DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
}
if (s_idxMemBlock[slot].getSize () < drawData->TotalIdxCount * sizeof (ImDrawIdx))
{
s_idxMemBlock[slot] = nullptr;
s_idxMemBlock[slot] =
dk::MemBlockMaker{s_device,
align (drawData->TotalIdxCount * sizeof (ImDrawIdx), DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
}
auto const cpuVtx = static_cast<std::uint8_t *> (s_vtxMemBlock[slot].getCpuAddr ());
auto const cpuIdx = static_cast<std::uint8_t *> (s_idxMemBlock[slot].getCpuAddr ());
auto const gpuVtx = s_vtxMemBlock[slot].getGpuAddr ();
auto const gpuIdx = s_idxMemBlock[slot].getGpuAddr ();
auto const sizeVtx = s_vtxMemBlock[slot].getSize ();
auto const sizeIdx = s_idxMemBlock[slot].getSize ();
static_assert (sizeof (ImDrawIdx) == 2);
s_cmdBuf[slot].bindVtxBuffer (0, gpuVtx, sizeVtx);
s_cmdBuf[slot].bindIdxBuffer (DkIdxFormat_Uint16, gpuIdx);
// Render command lists
std::size_t offsetVtx = 0;
std::size_t offsetIdx = 0;
for (int i = 0; i < drawData->CmdListsCount; ++i)
{
auto const &cmdList = *drawData->CmdLists[i];
auto const vtxSize = cmdList.VtxBuffer.Size * sizeof (ImDrawVert);
auto const idxSize = cmdList.IdxBuffer.Size * sizeof (ImDrawIdx);
if (sizeVtx - offsetVtx < vtxSize)
{
std::fprintf (stderr, "Not enough vertex buffer\n");
std::fprintf (stderr, "\t%zu/%u used, need %zu\n", offsetVtx, sizeVtx, vtxSize);
continue;
}
if (sizeIdx - offsetIdx < idxSize)
{
std::fprintf (stderr, "Not enough index buffer\n");
std::fprintf (stderr, "\t%zu/%u used, need %zu\n", offsetIdx, sizeIdx, idxSize);
continue;
}
std::memcpy (cpuVtx + offsetVtx, cmdList.VtxBuffer.Data, vtxSize);
std::memcpy (cpuIdx + offsetIdx, cmdList.IdxBuffer.Data, idxSize);
for (auto const &cmd : cmdList.CmdBuffer)
{
if (cmd.UserCallback)
{
s_queue.submitCommands (s_cmdBuf[slot].finishList ());
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to
// request the renderer to reset render state.)
if (cmd.UserCallback == ImDrawCallback_ResetRenderState)
s_queue.submitCommands (setupCmd);
else
cmd.UserCallback (&cmdList, &cmd);
}
else
{
// Project scissor/clipping rectangles into framebuffer space
ImVec4 clip;
clip.x = (cmd.ClipRect.x - clipOff.x) * clipScale.x;
clip.y = (cmd.ClipRect.y - clipOff.y) * clipScale.y;
clip.z = (cmd.ClipRect.z - clipOff.x) * clipScale.x;
clip.w = (cmd.ClipRect.w - clipOff.y) * clipScale.y;
if (clip.x < width && clip.y < height && clip.z >= 0.0f && clip.w >= 0.0f)
{
if (clip.x < 0.0f)
clip.x = 0.0f;
if (clip.y < 0.0f)
clip.y = 0.0f;
s_cmdBuf[slot].setScissors (
0, DkScissor{clip.x, clip.y, clip.z - clip.x, clip.w - clip.y});
auto const descriptor = reinterpret_cast<std::uintptr_t> (cmd.TextureId);
if (descriptor >= MAX_IMAGES)
continue;
if (descriptor != s_boundDescriptor)
{
s_boundDescriptor = descriptor;
s_cmdBuf[slot].bindTextures (
DkStage_Fragment, 0, dkMakeTextureHandle (descriptor, 0));
FragUBO fragUBO;
fragUBO.font = (descriptor == 0);
s_cmdBuf[slot].pushConstants (
s_uboMemBlock.getGpuAddr () +
align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT),
align (sizeof (FragUBO), DK_UNIFORM_BUF_ALIGNMENT),
0,
sizeof (FragUBO),
&fragUBO);
}
s_cmdBuf[slot].drawIndexed (DkPrimitive_Triangles,
cmd.ElemCount,
1,
cmd.IdxOffset + offsetIdx / sizeof (ImDrawIdx),
cmd.VtxOffset + offsetVtx / sizeof (ImDrawVert),
0);
}
}
}
offsetVtx += vtxSize;
offsetIdx += idxSize;
}
s_cmdBuf[slot].barrier (DkBarrier_Fragments, 0);
s_cmdBuf[slot].discardDepthStencil ();
s_queue.submitCommands (s_cmdBuf[slot].finishList ());
s_queue.presentImage (s_swapchain, slot);
}
void imgui::deko3d::test ()
{
auto const x1 = (s_width - LOGO_WIDTH) / 2.0f;
auto const x2 = x1 + LOGO_WIDTH;
auto const y1 = (s_height - LOGO_HEIGHT) / 2.0f;
auto const y2 = y1 + LOGO_HEIGHT;
ImGui::GetBackgroundDrawList ()->AddImage (
reinterpret_cast<ImTextureID> (1), ImVec2 (x1, y1), ImVec2 (x2, y2));
}

35
source/nx/imgui_deko3d.h Normal file
View File

@ -0,0 +1,35 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
namespace imgui
{
namespace deko3d
{
void init ();
void exit ();
void newFrame ();
void render ();
void test ();
}
}

40
source/nx/imgui_fsh.glsl Normal file
View File

@ -0,0 +1,40 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#version 460
layout (location = 0) in vec2 vtxUv;
layout (location = 1) in vec4 vtxColor;
layout (binding = 0) uniform sampler2D tex;
layout (std140, binding = 0) uniform FragUBO {
uint font;
} ubo;
layout (location = 0) out vec4 outColor;
void main()
{
if (ubo.font != 0)
outColor = vtxColor * vec4 (vec3 (1.0), texture (tex, vtxUv).r);
else
outColor = vtxColor * texture (tex, vtxUv);
}

1655
source/nx/imgui_nx.cpp Normal file

File diff suppressed because it is too large Load Diff

32
source/nx/imgui_nx.h Normal file
View File

@ -0,0 +1,32 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
namespace imgui
{
namespace nx
{
bool init ();
void exit ();
void newFrame ();
}
}

40
source/nx/imgui_vsh.glsl Normal file
View File

@ -0,0 +1,40 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#version 460
layout (location = 0) in vec2 inPos;
layout (location = 1) in vec2 inUv;
layout (location = 2) in vec4 inColor;
layout (location = 0) out vec2 vtxUv;
layout (location = 1) out vec4 vtxColor;
layout (std140, binding = 0) uniform VertUBO
{
mat4 projMtx;
} ubo;
void main()
{
gl_Position = ubo.projMtx * vec4 (inPos, 0.0, 1.0);
vtxUv = inUv;
vtxColor = inColor;
}

77
source/nx/init.c Normal file
View File

@ -0,0 +1,77 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <switch.h>
#include <unistd.h>
#ifndef NDEBUG
static int s_fd = -1;
#endif
static SocketInitConfig const s_socketInitConfig = {
.bsdsockets_version = 1,
.tcp_tx_buf_size = 1 * 1024 * 1024,
.tcp_rx_buf_size = 1 * 1024 * 1024,
.tcp_tx_buf_max_size = 4 * 1024 * 1024,
.tcp_rx_buf_max_size = 4 * 1024 * 1024,
.udp_tx_buf_size = 0x2400,
.udp_rx_buf_size = 0xA500,
.sb_efficiency = 8,
.num_bsd_sessions = 3,
.bsd_service_type = BsdServiceType_User,
};
u32 __nx_fs_num_sessions = 3;
void userAppInit ()
{
appletLockExit ();
romfsInit ();
plInitialize ();
if (R_FAILED (socketInitialize (&s_socketInitConfig)))
return;
#ifndef NDEBUG
s_fd = nxlinkStdio ();
#endif
}
void userAppExit ()
{
#ifndef NDEBUG
if (s_fd >= 0)
{
close (s_fd);
socketExit ();
s_fd = -1;
}
#endif
plExit ();
romfsExit ();
appletUnlockExit ();
}

167
source/nx/platform.cpp Normal file
View File

@ -0,0 +1,167 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "platform.h"
#include "imgui_deko3d.h"
#include "imgui_nx.h"
#include "imgui.h"
#include <switch.h>
#include <mutex>
#include <thread>
bool platform::init ()
{
IMGUI_CHECKVERSION ();
ImGui::CreateContext ();
if (!imgui::nx::init ())
{
ImGui::DestroyContext ();
return false;
}
imgui::deko3d::init ();
return true;
}
bool platform::loop ()
{
if (!appletMainLoop ())
return false;
hidScanInput ();
auto const keysDown = hidKeysDown (CONTROLLER_P1_AUTO);
if (keysDown & KEY_PLUS)
return false;
imgui::nx::newFrame ();
imgui::deko3d::newFrame ();
ImGui::NewFrame ();
imgui::deko3d::test ();
return true;
}
void platform::render ()
{
ImGui::Render ();
imgui::deko3d::render ();
}
void platform::exit ()
{
imgui::nx::exit ();
imgui::deko3d::exit ();
ImGui::DestroyContext ();
}
///////////////////////////////////////////////////////////////////////////
class platform::Thread::privateData_t
{
public:
privateData_t () = default;
privateData_t (std::function<void ()> func_) : thread (func_)
{
}
std::thread thread;
};
///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;
platform::Thread::Thread () : m_d (new privateData_t ())
{
}
platform::Thread::Thread (std::function<void ()> func_) : m_d (new privateData_t (func_))
{
}
platform::Thread::Thread (Thread &&that_) : m_d (new privateData_t ())
{
std::swap (m_d, that_.m_d);
}
platform::Thread &platform::Thread::operator= (Thread &&that_)
{
std::swap (m_d, that_.m_d);
return *this;
}
void platform::Thread::join ()
{
m_d->thread.join ();
}
void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
std::this_thread::sleep_for (timeout_);
}
///////////////////////////////////////////////////////////////////////////
#define USE_STD_MUTEX 1
class platform::Mutex::privateData_t
{
public:
#if USE_STD_MUTEX
std::mutex mutex;
#else
::Mutex mutex;
#endif
};
///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;
platform::Mutex::Mutex () : m_d (new privateData_t ())
{
#if !USE_STD_MUTEX
mutexInit (&m_d->mutex);
#endif
}
void platform::Mutex::lock ()
{
#if USE_STD_MUTEX
m_d->mutex.lock ();
#else
mutexLock (&m_d->mutex);
#endif
}
void platform::Mutex::unlock ()
{
#if USE_STD_MUTEX
m_d->mutex.unlock ();
#else
mutexUnlock (&m_d->mutex);
#endif
}

View File

@ -0,0 +1 @@
#error "Please use https://glad.dav1d.de/#profile=core&language=c&specification=gl&loader=on&api=gl%3D4.3"

1
source/pc/glad.c Normal file
View File

@ -0,0 +1 @@
#error "Please use https://glad.dav1d.de/#profile=core&language=c&specification=gl&loader=on&api=gl%3D4.3"

1
source/pc/glad/glad.h Normal file
View File

@ -0,0 +1 @@
#error "Please use https://glad.dav1d.de/#profile=core&language=c&specification=gl&loader=on&api=gl%3D4.3"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui"

View File

@ -0,0 +1 @@
#error "Please use https://github.com/ocornut/imgui"

259
source/pc/platform.cpp Normal file
View File

@ -0,0 +1,259 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "platform.h"
#include "imgui.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <cstdio>
#include <memory>
#include <mutex>
#include <thread>
namespace
{
std::unique_ptr<GLFWwindow, void (*) (GLFWwindow *)> s_mainWindow (nullptr, glfwDestroyWindow);
void windowResize (GLFWwindow *const window_, int const width_, int const height_)
{
(void)window_;
if (!width_ || !height_)
return;
glViewport (0, 0, width_, height_);
}
#ifndef NDEBUG
void logCallback (GLenum const source_,
GLenum const type_,
GLuint const id_,
GLenum const severity_,
GLsizei const length_,
GLchar const *const message_,
void const *const userParam_)
{
(void)source_;
(void)type_;
(void)id_;
(void)severity_;
(void)length_;
(void)userParam_;
// std::fprintf (stderr, "%s\n", message_);
}
#endif
}
bool platform::init ()
{
if (!glfwInit ())
{
std::fprintf (stderr, "Failed to initialize GLFW\n");
return false;
}
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifndef NDEBUG
glfwWindowHint (GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
#endif
// set depth buffer size
glfwWindowHint (GLFW_DEPTH_BITS, 24);
glfwWindowHint (GLFW_STENCIL_BITS, 8);
s_mainWindow.reset (glfwCreateWindow (1280, 720, "Test Game", nullptr, nullptr));
if (!s_mainWindow)
{
std::fprintf (stderr, "Failed to create window\n");
glfwTerminate ();
return false;
}
glfwSwapInterval (1);
// create context
glfwMakeContextCurrent (s_mainWindow.get ());
glfwSetFramebufferSizeCallback (s_mainWindow.get (), windowResize);
if (!gladLoadGL ())
{
std::fprintf (stderr, "gladLoadGL: failed\n");
platform::exit ();
return false;
}
#ifndef NDEBUG
GLint flags;
glGetIntegerv (GL_CONTEXT_FLAGS, &flags);
if (flags & GL_CONTEXT_FLAG_DEBUG_BIT)
{
glEnable (GL_DEBUG_OUTPUT);
glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback (logCallback, nullptr);
glDebugMessageControl (GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
}
#endif
glEnable (GL_CULL_FACE);
glFrontFace (GL_CCW);
glCullFace (GL_BACK);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glClearColor (104.0f / 255.0f, 176.0f / 255.0f, 216.0f / 255.0f, 1.0f);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
std::printf ("Renderer: %s\n", glGetString (GL_RENDERER));
std::printf ("OpenGL Version: %s\n", glGetString (GL_VERSION));
IMGUI_CHECKVERSION ();
ImGui::CreateContext ();
ImGui_ImplGlfw_InitForOpenGL (s_mainWindow.get (), true);
ImGui_ImplOpenGL3_Init ("#version 150");
ImGuiIO &io = ImGui::GetIO ();
io.IniFilename = nullptr;
return true;
}
bool platform::loop ()
{
bool inactive;
do
{
inactive = glfwGetWindowAttrib (s_mainWindow.get (), GLFW_ICONIFIED);
(inactive ? glfwWaitEvents : glfwPollEvents) ();
if (glfwWindowShouldClose (s_mainWindow.get ()))
return false;
} while (inactive);
ImGui_ImplOpenGL3_NewFrame ();
ImGui_ImplGlfw_NewFrame ();
ImGui::NewFrame ();
return true;
}
void platform::render ()
{
ImGui::Render ();
int width;
int height;
glfwGetFramebufferSize (s_mainWindow.get (), &width, &height);
glViewport (0, 0, width, height);
glClearColor (0.45f, 0.55f, 0.60f, 1.00f);
glClear (GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData (ImGui::GetDrawData ());
glfwSwapBuffers (s_mainWindow.get ());
}
void platform::exit ()
{
ImGui::DestroyContext ();
s_mainWindow.reset ();
glfwTerminate ();
}
///////////////////////////////////////////////////////////////////////////
class platform::Thread::privateData_t
{
public:
privateData_t () = default;
privateData_t (std::function<void ()> func_) : thread (func_)
{
}
std::thread thread;
};
///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;
platform::Thread::Thread () : m_d (new privateData_t ())
{
}
platform::Thread::Thread (std::function<void ()> func_) : m_d (new privateData_t (func_))
{
}
platform::Thread::Thread (Thread &&that_) : m_d (new privateData_t ())
{
std::swap (m_d, that_.m_d);
}
platform::Thread &platform::Thread::operator= (Thread &&that_)
{
std::swap (m_d, that_.m_d);
return *this;
}
void platform::Thread::join ()
{
m_d->thread.join ();
}
void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
std::this_thread::sleep_for (timeout_);
}
///////////////////////////////////////////////////////////////////////////
class platform::Mutex::privateData_t
{
public:
std::mutex mutex;
};
///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;
platform::Mutex::Mutex () : m_d (new privateData_t ())
{
}
void platform::Mutex::lock ()
{
m_d->mutex.lock ();
}
void platform::Mutex::unlock ()
{
m_d->mutex.unlock ();
}

150
source/sockAddr.cpp Normal file
View File

@ -0,0 +1,150 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "sockAddr.h"
#include <arpa/inet.h>
#include <cassert>
#include <cstring>
///////////////////////////////////////////////////////////////////////////
SockAddr::~SockAddr () = default;
SockAddr::SockAddr () = default;
SockAddr::SockAddr (SockAddr const &that_) = default;
SockAddr::SockAddr (SockAddr &&that_) = default;
SockAddr &SockAddr::operator= (SockAddr const &that_) = default;
SockAddr &SockAddr::operator= (SockAddr &&that_) = default;
SockAddr::SockAddr (struct sockaddr const &addr_)
{
switch (addr_.sa_family)
{
case AF_INET:
std::memcpy (&m_addr, &addr_, sizeof (struct sockaddr_in));
break;
#ifndef _3DS
case AF_INET6:
std::memcpy (&m_addr, &addr_, sizeof (struct sockaddr_in6));
break;
#endif
}
}
SockAddr::SockAddr (struct sockaddr_in const &addr_)
: SockAddr (reinterpret_cast<struct sockaddr const &> (addr_))
{
}
#ifndef _3DS
SockAddr::SockAddr (struct sockaddr_in6 const &addr_)
: SockAddr (reinterpret_cast<struct sockaddr const &> (addr_))
{
}
#endif
SockAddr::SockAddr (struct sockaddr_storage const &addr_)
: SockAddr (reinterpret_cast<struct sockaddr const &> (addr_))
{
}
SockAddr::operator struct sockaddr_in const & () const
{
assert (m_addr.ss_family == AF_INET);
return reinterpret_cast<struct sockaddr_in const &> (m_addr);
}
#ifndef _3DS
SockAddr::operator struct sockaddr_in6 const & () const
{
assert (m_addr.ss_family == AF_INET6);
return reinterpret_cast<struct sockaddr_in6 const &> (m_addr);
}
#endif
SockAddr::operator struct sockaddr_storage const & () const
{
return m_addr;
}
SockAddr::operator struct sockaddr * ()
{
return reinterpret_cast<struct sockaddr *> (&m_addr);
}
SockAddr::operator struct sockaddr const * () const
{
return reinterpret_cast<struct sockaddr const *> (&m_addr);
}
std::uint16_t SockAddr::port () const
{
switch (m_addr.ss_family)
{
case AF_INET:
return ntohs (reinterpret_cast<struct sockaddr_in const *> (&m_addr)->sin_port);
#ifndef _3DS
case AF_INET6:
return ntohs (reinterpret_cast<struct sockaddr_in6 const *> (&m_addr)->sin6_port);
#endif
}
return 0;
}
char const *SockAddr::name (char *buffer_, std::size_t size_) const
{
switch (m_addr.ss_family)
{
case AF_INET:
return inet_ntop (AF_INET,
&reinterpret_cast<struct sockaddr_in const *> (&m_addr)->sin_addr,
buffer_,
size_);
#ifndef _3DS
case AF_INET6:
return inet_ntop (AF_INET6,
&reinterpret_cast<struct sockaddr_in6 const *> (&m_addr)->sin6_addr,
buffer_,
size_);
#endif
}
return nullptr;
}
char const *SockAddr::name () const
{
#if defined(_3DS)
thread_local static char buffer[INET_ADDRSTRLEN];
#else
thread_local static char buffer[INET6_ADDRSTRLEN];
#endif
return name (buffer, sizeof (buffer));
}

334
source/socket.cpp Normal file
View File

@ -0,0 +1,334 @@
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "socket.h"
#include "log.h"
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <poll.h>
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstring>
///////////////////////////////////////////////////////////////////////////
Socket::~Socket ()
{
if (m_listening)
Log::info ("Stop listening on [%s]:%u\n", m_sockName.name (), m_sockName.port ());
if (m_connected)
Log::info ("Closing connection to [%s]:%u\n", m_peerName.name (), m_peerName.port ());
if (::close (m_fd) != 0)
Log::error ("close: %s\n", std::strerror (errno));
}
Socket::Socket (int const fd_) : m_fd (fd_), m_listening (false), m_connected (false)
{
}
Socket::Socket (int const fd_, SockAddr const &sockName_, SockAddr const &peerName_)
: m_sockName (sockName_),
m_peerName (peerName_),
m_fd (fd_),
m_listening (false),
m_connected (true)
{
}
UniqueSocket Socket::accept ()
{
SockAddr addr;
socklen_t addrLen = sizeof (struct sockaddr_storage);
auto const fd = ::accept (m_fd, addr, &addrLen);
if (fd < 0)
{
Log::error ("accept: %s\n", std::strerror (errno));
return {nullptr, {}};
}
Log::info ("Accepted connection from [%s]:%u\n", addr.name (), addr.port ());
return UniqueSocket (new Socket (fd, m_sockName, addr));
}
int Socket::atMark ()
{
auto const rc = ::sockatmark (m_fd);
if (rc < 0)
Log::error ("sockatmark: %s\n", std::strerror (errno));
return rc;
}
bool Socket::bind (SockAddr const &addr_)
{
switch (static_cast<struct sockaddr_storage const &> (addr_).ss_family)
{
case AF_INET:
if (::bind (m_fd, addr_, sizeof (struct sockaddr_in)) != 0)
{
Log::error ("bind: %s\n", std::strerror (errno));
return false;
}
break;
#ifndef _3DS
case AF_INET6:
if (::bind (m_fd, addr_, sizeof (struct sockaddr_in6)) != 0)
{
Log::error ("bind: %s\n", std::strerror (errno));
return false;
}
break;
#endif
default:
errno = EINVAL;
Log::error ("bind: %s\n", std::strerror (errno));
break;
}
if (addr_.port () == 0)
{
socklen_t addrLen = sizeof (struct sockaddr_storage);
if (::getsockname (m_fd, m_sockName, &addrLen) != 0)
Log::error ("getsockname: %s\n", std::strerror (errno));
}
else
m_sockName = addr_;
return true;
}
bool Socket::connect (SockAddr const &addr_)
{
if (::connect (m_fd, addr_, sizeof (struct sockaddr_storage)) != 0)
{
if (errno != EINPROGRESS)
Log::error ("connect: %s\n", std::strerror (errno));
else
{
m_peerName = addr_;
m_connected = true;
Log::info ("Connecting to [%s]:%u\n", addr_.name (), addr_.port ());
}
return false;
}
m_peerName = addr_;
m_connected = true;
Log::info ("Connected to [%s]:%u\n", addr_.name (), addr_.port ());
return true;
}
bool Socket::listen (int const backlog_)
{
if (::listen (m_fd, backlog_) != 0)
{
Log::error ("listen: %s\n", std::strerror (errno));
return false;
}
m_listening = true;
return true;
}
bool Socket::shutdown (int const how_)
{
if (::shutdown (m_fd, how_) != 0)
{
Log::info ("shutdown: %s\n", std::strerror (errno));
return false;
}
return true;
}
bool Socket::setLinger (bool const enable_, std::chrono::seconds const time_)
{
struct linger linger;
linger.l_onoff = enable_;
linger.l_linger = time_.count ();
if (::setsockopt (m_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof (linger)) != 0)
{
Log::error ("setsockopt(SO_LINGER, %s, %lus): %s\n",
enable_ ? "on" : "off",
static_cast<unsigned long> (time_.count ()),
std::strerror (errno));
return false;
}
return true;
}
bool Socket::setNonBlocking (bool const nonBlocking_)
{
auto flags = ::fcntl (m_fd, F_GETFL, 0);
if (flags == -1)
{
Log::error ("fcntl(F_GETFL): %s\n", std::strerror (errno));
return false;
}
if (nonBlocking_)
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
if (::fcntl (m_fd, F_SETFL, flags) != 0)
{
Log::error ("fcntl(F_SETFL): %s\n", std::strerror (errno));
return false;
}
return true;
}
bool Socket::setReuseAddress (bool const reuse_)
{
int reuse = reuse_;
if (::setsockopt (m_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse)) != 0)
{
Log::error ("setsockopt(SO_REUSEADDR, %u): %s\n", reuse_, std::strerror (errno));
return false;
}
return true;
}
bool Socket::setRecvBufferSize (std::size_t const size_)
{
int size = size_;
if (::setsockopt (m_fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) != 0)
{
Log::error ("setsockopt(SO_RCVBUF, %zu): %s\n", size_, std::strerror (errno));
return false;
}
return true;
}
bool Socket::setSendBufferSize (std::size_t const size_)
{
int size = size_;
if (::setsockopt (m_fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) != 0)
{
Log::error ("setsockopt(SO_SNDBUF, %zu): %s\n", size_, std::strerror (errno));
return false;
}
return true;
}
ssize_t Socket::read (void *const buffer_, std::size_t const size_, bool const oob_)
{
assert (buffer_);
assert (size_);
auto const rc = ::recv (m_fd, buffer_, size_, oob_ ? MSG_OOB : 0);
if (rc < 0 && errno != EWOULDBLOCK)
Log::error ("recv: %s\n", std::strerror (errno));
return rc;
}
ssize_t Socket::read (IOBuffer &buffer_, bool const oob_)
{
auto const rc = read (buffer_.freeArea (), buffer_.freeSize (), oob_);
if (rc > 0)
buffer_.markUsed (rc);
return rc;
}
ssize_t Socket::write (void const *const buffer_, std::size_t const size_)
{
assert (buffer_);
assert (size_);
auto const rc = ::send (m_fd, buffer_, size_, 0);
if (rc < 0 && errno != EWOULDBLOCK)
Log::error ("send: %s\n", std::strerror (errno));
return rc;
}
ssize_t Socket::write (IOBuffer &buffer_)
{
auto const rc = write (buffer_.usedArea (), buffer_.usedSize ());
if (rc > 0)
buffer_.markFree (rc);
return rc;
}
SockAddr const &Socket::sockName () const
{
return m_sockName;
}
SockAddr const &Socket::peerName () const
{
return m_peerName;
}
UniqueSocket Socket::create ()
{
auto const fd = ::socket (AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
Log::error ("socket: %s\n", std::strerror (errno));
return nullptr;
}
return UniqueSocket (new Socket (fd));
}
int Socket::poll (PollInfo *const info_,
std::size_t const count_,
std::chrono::milliseconds const timeout_)
{
if (count_ == 0)
return 0;
auto const pfd = std::make_unique<struct pollfd[]> (count_);
for (std::size_t i = 0; i < count_; ++i)
{
pfd[i].fd = info_[i].socket.get ().m_fd;
pfd[i].events = info_[i].events;
pfd[i].revents = 0;
}
auto const rc = ::poll (pfd.get (), count_, timeout_.count ());
if (rc < 0)
{
Log::error ("poll: %s\n", std::strerror (errno));
return rc;
}
for (std::size_t i = 0; i < count_; ++i)
info_[i].revents = pfd[i].revents;
return rc;
}