commit 21232e19c8afa1bdaabd72c0fcbd26539f28aee6 Author: wanp Date: Mon Nov 30 23:38:35 2026 -0300 initial commit please diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8204e4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Copyright (C) 2026 wanp +# +# This file is part of Wavy. +# +# Wavy 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. +# +# Wavy 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 Wavy. If not, see . + +build/ diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + 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. + + + Copyright (C) + + 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 . + +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: + + Copyright (C) + 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 +. + + 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 +. diff --git a/README.md b/README.md new file mode 100644 index 0000000..919b396 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Wavy + +## Overview +Wavy is a scanty, pixelly music player. + +It doesn't intend to support every use case under the sun. In particular, it only +tries to support FLAC and MP3 files at the moment. + +## Building +Wavy is a Meson project. See the options file for options you can set. +Other than that, mostly, it's a C99 program. You'll have to install the +development packages for SDL3, libFLAC, and mpg123. + +As a reminder: first run `meson setup build`, then `meson compile -C build` to +compile it, and on Unix-like systems, `meson install` to install that (or if you +prefer [muon](https://git.sr.ht/~lattis/muon), `muon setup build` then `muon samu +-C build` and `muon install`). Note that by default you get an unoptimized debug +build. + +## License +### Code +Wavy's source code is distributed under the terms of the GNU General Public +License, version 3 or later. See the COPYING file for details. + +### Resources (e.g. images) +Licensed under [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) diff --git a/files/README.txt b/files/README.txt new file mode 100644 index 0000000..5d1444c --- /dev/null +++ b/files/README.txt @@ -0,0 +1,97 @@ +Wavy +Copyright (C) 2026 wanp + +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 . + + +Also, Wavy uses other stuff. + + +Simple DirectMedia Layer +Copyright (C) 1997-2026 Sam Lantinga + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. + + +libFLAC +Copyright (C) 2000-2009 Josh Coalson +Copyright (C) 2011-2025 Xiph.Org Foundation + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +- Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +- Neither the name of the Xiph.Org Foundation nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +mpg123 +Copyright (c) 1995-2020 by Michael Hipp and others, +free software under the terms of the LGPL v2.1 +https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt + + +libtags +Copyright © 2013-2024 Sigrid Solveig Haflínudóttir + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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 OR +COPYRIGHT HOLDERS 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. diff --git a/files/data/icon.png b/files/data/icon.png new file mode 100644 index 0000000..229ae29 Binary files /dev/null and b/files/data/icon.png differ diff --git a/files/data/widgets.png b/files/data/widgets.png new file mode 100644 index 0000000..0993b2d Binary files /dev/null and b/files/data/widgets.png differ diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..8132728 --- /dev/null +++ b/meson.build @@ -0,0 +1,88 @@ +# Copyright (C) 2026 wanp +# +# This file is part of Wavy. +# +# Wavy 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. +# +# Wavy 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 Wavy. If not, see . + +project('Wavy', 'c', + version: '0', + license: 'GPL-3.0-or-later', + license_files: ['COPYING'], + default_options: { + 'c_std': 'c99', + 'warning_level': '2', + }) + +cc = meson.get_compiler('c') + +if get_option('single_process') + args = ['-D_XOPEN_SOURCE=500'] + single_process = cc.has_function('open', args: args, + prefix: '#include ') + single_process = single_process and cc.has_function( + 'close', args: args, + prefix: '#include ') + single_process = single_process and cc.has_function( + 'remove', args: args, + prefix: '#include ') + single_process = single_process and cc.has_function( + 'lockf', args: args, + prefix: '#include ') +endif + +src = [ + 'src/main.c', + 'src/mem.c', + 'src/files.c', + 'src/app.c', + 'src/popup.c', + 'src/playback.c', + 'src/flac.c', + 'src/mp3.c', + 'src/widgets.c', +] + +compile_flags = [ + '-DAPP_DEV_ID="wanp"', + '-DAPP_ID="com.wanpnet.wavy"', + '-DAPP_NAME="Wavy"', + '-DAPP_VERSION="' + meson.project_version() + '"', + '-DDATA_DIR="' + get_option('datadir') / 'wavy' + '"', +] + +if single_process + compile_flags += '-DSINGLE_PROCESS' +endif + + +m_dep = cc.find_library('m', required: false) +sdl3_dep = dependency('sdl3', version: '>=3.4.0') +flac_dep = dependency('flac') +libmpg123_dep = dependency('libmpg123') + +libtags_proj = subproject('libtags') +libtags_inc = 'subprojects/libtags' +libtags_dep = libtags_proj.get_variable('libtags') + + +executable('wavy', + sources: src, + c_args: compile_flags, + install: true, + win_subsystem: 'windows', + include_directories: [libtags_inc], + dependencies: [m_dep, sdl3_dep, libtags_dep, flac_dep, libmpg123_dep]) + +install_data(['COPYING']) +install_subdir('files', install_dir: 'share/wavy', strip_directory: true) diff --git a/meson.options b/meson.options new file mode 100644 index 0000000..88cbcbb --- /dev/null +++ b/meson.options @@ -0,0 +1,22 @@ +# Copyright (C) 2026 wanp +# +# This file is part of Wavy. +# +# Wavy 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. +# +# Wavy 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 Wavy. If not, see . + +option('single_process', + type: 'boolean', + value: true, + description: 'Don\'t start the program if there\'s an instance running.' + + 'Requires the C POSIX library.') diff --git a/resources/icon.svg b/resources/icon.svg new file mode 100644 index 0000000..a4d6372 --- /dev/null +++ b/resources/icon.svg @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + diff --git a/src/app.c b/src/app.c new file mode 100644 index 0000000..d95809f --- /dev/null +++ b/src/app.c @@ -0,0 +1,338 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifdef SINGLE_PROCESS +#define _XOPEN_SOURCE 500 +#endif + +#include "app.h" +#include "mem.h" +#include "files.h" +#include "popup.h" +#include "widgets.h" +#include "playback.h" +#include "flac.h" +#include "mp3.h" + +#include +#include + +#include +#ifdef SINGLE_PROCESS +#include +#include +#include +#endif + + +static SDL_HitTestResult SDLCALL on_hit_test(SDL_Window *win, + const SDL_Point *area, + void *data) { + (void)win; + + App *app = data; + + /* popup is shown, don't drag right now */ + if (!(SDL_GetWindowFlags(app->popup.window) & SDL_WINDOW_HIDDEN)) { + return SDL_HITTEST_NORMAL; + } + + Widgets *widgets = &app->widgets; + SDL_FPoint point; + SDL_RenderCoordinatesFromWindow(app->core.renderer, area->x, area->y, + &point.x, &point.y); + + for (size_t i = 0; i < widgets->buttons_len; ++i) { + Button *button = &widgets->buttons[i]; + if (SDL_PointInRectFloat(&point, &button->rect)) { + return SDL_HITTEST_NORMAL; + } + } + for (size_t i = 0; i < widgets->sliders_len; ++i) { + Slider *slider = &widgets->sliders[i]; + if (SDL_PointInRectFloat(&point, &slider->rect)) { + return SDL_HITTEST_NORMAL; + } + } + + return SDL_HITTEST_DRAGGABLE; +} + + +static void SDLCALL on_tray_toggle(void *userdata, SDL_TrayEntry *invoker) { + SDL_Window *window = userdata; + + if (SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN) { + SDL_SetTrayEntryLabel(invoker, "Hide Window"); + SDL_ShowWindow(window); + } else { + SDL_SetTrayEntryLabel(invoker, "Show Window"); + SDL_HideWindow(window); + } +} + +static void SDLCALL on_tray_quit(void *userdata, SDL_TrayEntry *invoker) { + (void)userdata, (void)invoker; + + SDL_Event e = { .type = SDL_EVENT_QUIT }; + SDL_PushEvent(&e); +} + + +static bool init_files(App *app) { + app->core.pref_dir = SDL_GetPrefPath(APP_DEV_ID, APP_NAME); + if (app->core.pref_dir == NULL) { + char *msg; + SDL_asprintf(&msg, "Couldn't initialize pref dir: %s", SDL_GetError()); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to start!", msg, NULL); + SDL_free(msg); + return false; + } + +#ifdef SINGLE_PROCESS + /* try to make sure there's only one instance of the app running */ + SDL_asprintf(&app->core.instance_file_path, "%s/.instance", app->core.pref_dir); + mode_t mode = (S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH; + app->core.instance_file = open(app->core.instance_file_path, O_CREAT | O_WRONLY, mode); + if (lockf(app->core.instance_file, F_TLOCK, 0) == -1) { + SDL_free(app->core.instance_file_path); + app->core.instance_file_path = NULL; + return false; + } +#endif + + /* you can make it look for files somewhere else, mainly for dev purposes */ + const char *env_search_path = SDL_getenv("WAVY_DATA_PATH"); + if (env_search_path != NULL && path_exists(env_search_path)) { + app->core.base_dir = SDL_strdup(env_search_path); + } else { + if (DATA_DIR[0] != '\0' && path_exists(DATA_DIR)) { + /* fixed dir compiled in */ + app->core.base_dir = SDL_strdup(DATA_DIR); + } else { + /* nothing specific available, so just use the base path */ + app->core.base_dir = SDL_strdup(SDL_GetBasePath()); + } + } + + /* a separate array is made and then copied, to avoid hardcoding a length */ + SDL_DialogFileFilter file_filters[] = { + { "Music files (*.flac *.mp3)", "flac;mp3" }, + }; + app->core.file_filters = amalloc(sizeof file_filters); + memcpy(app->core.file_filters, file_filters, sizeof file_filters); + app->core.file_filters_len = SDL_arraysize(file_filters); + + return true; +} + +static void deinit_files(App *app) { + free(app->core.file_filters); + SDL_free(app->core.base_dir); + +#ifdef SINGLE_PROCESS + close(app->core.instance_file); + if (app->core.instance_file_path != NULL) { + remove(app->core.instance_file_path); + SDL_free(app->core.instance_file_path); + } +#endif + + SDL_free(app->core.pref_dir); +} + + +static bool init_system(App *app) { + float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); + SDL_CreateWindowAndRenderer(APP_NAME, + (APP_WIDTH * APP_SCALE) * main_scale, + (APP_HEIGHT * APP_SCALE) * main_scale, + SDL_WINDOW_HIDDEN | SDL_WINDOW_BORDERLESS | + SDL_WINDOW_HIGH_PIXEL_DENSITY, + &app->core.window, + &app->core.renderer); + if (app->core.window == NULL) { + char *msg; + SDL_asprintf(&msg, "Couldn't create window: %s", SDL_GetError()); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to start!", msg, NULL); + SDL_free(msg); + return false; + } + + /* since this is a borderless window, SDL needs to know which areas you can use to drag it */ + SDL_SetWindowHitTest(app->core.window, on_hit_test, app); + + SDL_SetRenderLogicalPresentation(app->core.renderer, APP_WIDTH, APP_HEIGHT, + SDL_LOGICAL_PRESENTATION_STRETCH); + + /* i don't _think_ it needs to be optional, but i don't know */ + SDL_SetRenderVSync(app->core.renderer, 1); + + /* SDL_AppIterate will run only when there was an event */ + SDL_SetHint(SDL_HINT_MAIN_CALLBACK_RATE, "waitevent"); + + Uint32 user_events = SDL_RegisterEvents(1); + app->playback.playback_progress_event = user_events; + + /* the app/tray icon*/ + char *icon_path; + SDL_asprintf(&icon_path, "%s/data/icon.png", app->core.base_dir); + SDL_Surface *icon_surface = load_png(icon_path); + SDL_free(icon_path); + + SDL_SetWindowIcon(app->core.window, icon_surface); + + /* the tray button, used mainly to hide and show the app. + currently (SDL 3.4.x) you can't set a callback for clicking the tray button itself */ + app->core.tray = SDL_CreateTray(icon_surface, NULL); + SDL_DestroySurface(icon_surface); + + SDL_TrayMenu *tray_menu = SDL_CreateTrayMenu(app->core.tray); + + app->core.tray_entry_toggle = + SDL_InsertTrayEntryAt(tray_menu, -1, "Hide Window", SDL_TRAYENTRY_BUTTON); + SDL_SetTrayEntryCallback(app->core.tray_entry_toggle, on_tray_toggle, app->core.window); + + SDL_TrayEntry *tray_entry_quit = + SDL_InsertTrayEntryAt(tray_menu, -1, "Quit", SDL_TRAYENTRY_BUTTON); + SDL_SetTrayEntryCallback(tray_entry_quit, on_tray_quit, NULL); + + /* the multipurpose right-click popup */ + /* NOTE: `options` is realloc'd when needed, so it's left as NULL here */ + app->popup.window = SDL_CreatePopupWindow(app->core.window, + 0, 0, + POPUP_WIDTH*APP_SCALE, POPUP_HEIGHT*APP_SCALE, + SDL_WINDOW_POPUP_MENU | SDL_WINDOW_HIDDEN); + app->popup.renderer = SDL_CreateRenderer(app->popup.window, NULL); + SDL_SetRenderLogicalPresentation(app->popup.renderer, + POPUP_WIDTH, POPUP_HEIGHT, + SDL_LOGICAL_PRESENTATION_STRETCH); + + return true; +} + +static void deinit_system(App *app) { + SDL_DestroyTray(app->core.tray); + + free(app->popup.options); + SDL_DestroyRenderer(app->popup.renderer); + SDL_DestroyWindow(app->popup.window); + + SDL_DestroyRenderer(app->core.renderer); + SDL_DestroyWindow(app->core.window); +} + + +static bool init_audio(App *app) { + app->playback.output = + SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL, on_audio, &app->playback); + if (app->playback.output == NULL) { + char *msg; + SDL_asprintf(&msg, "Couldn't initialize audio: %s", SDL_GetError()); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to start!", msg, NULL); + SDL_free(msg); + return false; + } + SDL_SetAudioPostmixCallback(SDL_GetAudioStreamDevice(app->playback.output), + on_postmix, + &app->playback); + + return true; +} + +static void deinit_audio(App *app) { + SDL_free(app->playback.file_metadata.title); + SDL_free(app->playback.file_metadata.artist); + SDL_free(app->playback.file_metadata.album); + + SDL_free(app->playback.file_path); + + free(app->playback.buffer); + if (app->playback.flac_decoder != NULL) { + destroy_flac(&app->playback); + } + if (app->playback.mp3_decoder != NULL) { + destroy_mp3(&app->playback); + } + + SDL_DestroyAudioStream(app->playback.output); +} + + +static bool init_widgets(App *app) { + char *spritesheet_path; + SDL_asprintf(&spritesheet_path, "%s/data/widgets.png", app->core.base_dir); + SDL_Surface *spritesheet_surface = load_png(spritesheet_path); + SDL_free(spritesheet_path); + + app->widgets.spritesheet = + SDL_CreateTextureFromSurface(app->core.renderer, spritesheet_surface); + SDL_DestroySurface(spritesheet_surface); + SDL_SetTextureScaleMode(app->widgets.spritesheet, SDL_SCALEMODE_PIXELART); + + Button buttons[] = { + make_btn_minimize(), + make_btn_close(), + make_btn_play(), + make_btn_pause(), + make_btn_stop(), + }; + app->widgets.buttons = amalloc(sizeof buttons); + memcpy(app->widgets.buttons, buttons, sizeof buttons); + app->widgets.buttons_len = SDL_arraysize(buttons); + + Slider sliders[] = { + make_slider_seekbar(), + }; + app->widgets.sliders = amalloc(sizeof sliders); + memcpy(app->widgets.sliders, sliders, sizeof sliders); + app->widgets.sliders_len = SDL_arraysize(sliders); + + return true; +} + +static void deinit_widgets(App *app) { + free(app->widgets.buttons); + SDL_DestroyTexture(app->widgets.spritesheet); +} + + +App *make_app(void) { + App *app = amalloc(sizeof *app); + *app = (App){ 0 }; + + if (!init_files(app) || !init_system(app) || !init_audio(app) || !init_widgets(app)) { + destroy_app(app); + return NULL; + } + + return app; +} + +void destroy_app(App *app) { + /* NOTE: anything allocated with an SDL function gets freed with `SDL_free` + instead of `free`, just to be safe about it. */ + + deinit_widgets(app); + deinit_audio(app); + deinit_system(app); + deinit_files(app); + + free(app); +} diff --git a/src/app.h b/src/app.h new file mode 100644 index 0000000..e04e67e --- /dev/null +++ b/src/app.h @@ -0,0 +1,68 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef APP_H +#define APP_H + +#include "popup.h" +#include "playback.h" +#include "widgets.h" + +#include + + +#define BORDER_SIZE 2 +#define APP_WIDTH (256 + (BORDER_SIZE*2)) +#define APP_HEIGHT (128 + (BORDER_SIZE*2)) +#define APP_SCALE 2 + + +typedef struct Core { + SDL_Window *window; + SDL_Renderer *renderer; + + char *pref_dir; + char *base_dir; + +#ifdef SINGLE_PROCESS + char *instance_file_path; + int instance_file; +#endif + + SDL_DialogFileFilter *file_filters; + size_t file_filters_len; + bool is_file_dialog_open; + + SDL_Tray *tray; + SDL_TrayEntry *tray_entry_toggle; +} Core; + + +typedef struct App { + Core core; + Popup popup; + Playback playback; + Widgets widgets; +} App; + + +App *make_app(void); +void destroy_app(App *app); + +#endif diff --git a/src/files.c b/src/files.c new file mode 100644 index 0000000..75e1145 --- /dev/null +++ b/src/files.c @@ -0,0 +1,34 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#include "files.h" + +#include + + +SDL_Surface *load_png(const char *path) { + SDL_Surface *surface = SDL_LoadPNG(path); + SDL_PremultiplySurfaceAlpha(surface, false); + return surface; +} + + +bool path_exists(const char *path) { + return SDL_GetPathInfo(path, NULL); +} diff --git a/src/files.h b/src/files.h new file mode 100644 index 0000000..5334fcb --- /dev/null +++ b/src/files.h @@ -0,0 +1,29 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef FILES_H +#define FILES_H + +#include + + +SDL_Surface *load_png(const char *path); +bool path_exists(const char *path); + +#endif diff --git a/src/flac.c b/src/flac.c new file mode 100644 index 0000000..85f7d2c --- /dev/null +++ b/src/flac.c @@ -0,0 +1,159 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#include "flac.h" +#include "app.h" + +#include + +#include +#include + + +static FLAC__StreamDecoderWriteStatus +write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, + const FLAC__int32 *const buffer[], void *client_data) { + (void)decoder; + + Playback* playback = client_data; + playback->buffer_len = sizeof (*playback->buffer) * frame->header.blocksize * + frame->header.channels; + + /* copy the data interleaved */ + float *dst = playback->buffer; + double range = ((1 << (frame->header.bits_per_sample-1))) - 1; /* e.g. 8 bps -> 127 */ + + for (size_t frame_idx = 0; frame_idx < frame->header.blocksize; ++frame_idx) { + for (size_t channel = 0; channel < frame->header.channels; ++channel) { + double sample = (double)buffer[channel][frame_idx]; + *(dst++) = sample / range; + } + } + + return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; +} + +static void metadata_callback(const FLAC__StreamDecoder *decoder, + const FLAC__StreamMetadata *metadata, + void *client_data) { + (void)decoder, (void)client_data; + + if (metadata->type != FLAC__METADATA_TYPE_STREAMINFO) { + return; + } + + const FLAC__StreamMetadata_StreamInfo *stream_info = + &metadata->data.stream_info; + + Playback *playback = client_data; + SDL_assert(SDL_AudioStreamDevicePaused(playback->output)); + + size_t buffer_capacity = sizeof (*playback->buffer) * + stream_info->max_blocksize * stream_info->channels; + + playback->spec = (SDL_AudioSpec){ + .format = SDL_AUDIO_F32, + .channels = stream_info->channels, + .freq = stream_info->sample_rate, + }; + playback->buffer_capacity = buffer_capacity; +} + +static void error_callback(const FLAC__StreamDecoder *decoder, + FLAC__StreamDecoderErrorStatus status, + void *client_data) { + (void)decoder, (void)client_data; + + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FLAC error: %s", + FLAC__StreamDecoderErrorStatusString[status]); +} + + +FLAC__StreamDecoder *create_flac(void) { + return FLAC__stream_decoder_new(); +} + + +bool init_flac(Playback *playback, const char *path) { + FLAC__StreamDecoderInitStatus init_status; + init_status = FLAC__stream_decoder_init_file(playback->flac_decoder, + path, + write_callback, + metadata_callback, + error_callback, + playback); + if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Failed to initialize FLAC decoder: %s", + FLAC__StreamDecoderInitStatusString[init_status]); + return false; + } + + /* most certain way to figure out the total samples is to seek through it */ + FLAC__uint64 total_samples = FLAC__stream_decoder_find_total_samples(playback->flac_decoder); + if (total_samples == 0) { + FLAC__StreamDecoderState state = + FLAC__stream_decoder_get_state(playback->flac_decoder); + + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Failed to get total samples in FLAC: %s", + FLAC__StreamDecoderStateString[state]); + + FLAC__stream_decoder_finish(playback->flac_decoder); + return false; + } + FLAC__stream_decoder_reset(playback->flac_decoder); + + /* will call `metadata_callback` and maybe `error_callback` */ + FLAC__stream_decoder_process_until_end_of_metadata(playback->flac_decoder); + + /* this is the number of "interchannel samples" (SDL's "sample frames"), + not all of the individual samples that are present. */ + playback->file_metadata.duration_frames = total_samples; + + return true; +} + + +void stop_flac(Playback *playback) { + FLAC__stream_decoder_finish(playback->flac_decoder); +} + + +void destroy_flac(Playback *playback) { + FLAC__stream_decoder_delete(playback->flac_decoder); +} + + +void seek_flac(Playback *playback, uint64_t frame_idx) { + FLAC__stream_decoder_seek_absolute(playback->flac_decoder, frame_idx); +} + + +bool decode_flac(FLAC__StreamDecoder *decoder) { + /* will call `write_callback` and maybe `error_callback` */ + FLAC__stream_decoder_process_single(decoder); + + FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder); + if (state == FLAC__STREAM_DECODER_END_OF_STREAM) { + return false; + } + + return true; +} diff --git a/src/flac.h b/src/flac.h new file mode 100644 index 0000000..1712b21 --- /dev/null +++ b/src/flac.h @@ -0,0 +1,37 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef FLAC_H +#define FLAC_H + +#include "app.h" + +#include + +#include + + +FLAC__StreamDecoder *create_flac(void); +bool init_flac(Playback *playback, const char *path); +void stop_flac(Playback *playback); +void destroy_flac(Playback *playback); +void seek_flac(Playback *playback, uint64_t frame_idx); +bool decode_flac(FLAC__StreamDecoder *decoder); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..87a1fc3 --- /dev/null +++ b/src/main.c @@ -0,0 +1,260 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#define SDL_MAIN_USE_CALLBACKS 1 +#include +#include + +#include "app.h" +#include "mem.h" +#include "popup.h" +#include "widgets.h" + +#include + + +static void draw(App *app) { + SDL_Renderer *renderer = app->core.renderer; + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, 255, 241, 232, 255); + SDL_FRect border_rect = { + BORDER_SIZE, + BORDER_SIZE, + APP_WIDTH - (BORDER_SIZE*2), + APP_HEIGHT - (BORDER_SIZE*2), + }; + SDL_RenderFillRect(renderer, &border_rect); + + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + for (size_t i = 0; i < app->widgets.buttons_len; ++i) { + button_draw(&app->widgets.buttons[i], app); + } + for (size_t i = 0; i < app->widgets.sliders_len; ++i) { + slider_draw(&app->widgets.sliders[i], app); + } + + SDL_RenderPresent(renderer); +} + + +static void handle_mouse_down(Uint8 button, SDL_FPoint pos, App *app) { + if (!(SDL_GetWindowFlags(app->popup.window) & SDL_WINDOW_HIDDEN)) { + SDL_HideWindow(app->popup.window); + return; + } + + if (button == SDL_BUTTON_LEFT) { + for (size_t i = 0; i < app->widgets.buttons_len; ++i) { + Button *button = &app->widgets.buttons[i]; + if (SDL_PointInRectFloat(&pos, &button->rect)) { + button->is_held = true; + if (button_down(button, app)) { + return; + } + } + } + for (size_t i = 0; i < app->widgets.sliders_len; ++i) { + Slider *slider = &app->widgets.sliders[i]; + if (SDL_PointInRectFloat(&pos, &slider->rect)) { + slider->is_sliding = true; + if (slider_down(slider, app, pos)) { + return; + } + } + } + } else if (button == SDL_BUTTON_RIGHT) { + show_popup(pos, app); + } +} + +static void handle_mouse_motion(SDL_MouseButtonFlags state, SDL_FPoint pos, App *app) { + if (!(state & SDL_BUTTON_LMASK)) { + return; + } + + for (size_t i = 0; i < app->widgets.sliders_len; ++i) { + Slider *slider = &app->widgets.sliders[i]; + if (slider->is_sliding || SDL_PointInRectFloat(&pos, &slider->rect)) { + slider_drag(slider, app, pos); + } + } +} + +static void handle_mouse_up(Uint8 button, SDL_FPoint pos, App *app) { + if (button != SDL_BUTTON_LEFT) { + return; + } + + for (size_t i = 0; i < app->widgets.buttons_len; ++i) { + Button *button = &app->widgets.buttons[i]; + bool input_was_consumed = false; + /* held buttons only call their up functions if it happens while inside them */ + if (button->is_held && SDL_PointInRectFloat(&pos, &button->rect)) { + input_was_consumed = button_up(button, app); + } + button->is_held = false; + if (input_was_consumed) { + return; + } + } + for (size_t i = 0; i < app->widgets.sliders_len; ++i) { + Slider *slider = &app->widgets.sliders[i]; + bool input_was_consumed = false; + /* held sliders always call their up functions */ + if (slider->is_sliding) { + input_was_consumed = slider_up(slider, app, pos); + } + slider->is_sliding = false; + if (input_was_consumed) { + return; + } + } +} + +static void handle_playback_progress(App *app) { + /* HACK: well it's not too bad, but it feels like a smell. + this is a very specific event that almost nothing will care about, + so maybe it should be handled more directly. */ + for (size_t i = 0; i < app->widgets.sliders_len; ++i) { + Slider *slider = &app->widgets.sliders[i]; + if (!slider->is_sliding) { + slider_playback_progress(slider, app); + } + } +} + + +SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { + (void)argc, (void)argv; + + /* init SDL */ + /* it's just going to explode and try to do a message box if allocs fail */ + SDL_SetMemoryFunctions(amalloc, acalloc, arealloc, free); + + /* the app metadata macros are from the Makefile */ + SDL_SetAppMetadata(APP_NAME, APP_VERSION, APP_ID); + SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, APP_DEV_ID); + + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { + /* TODO: this is everywhere, put it in a function */ + char *msg; + SDL_asprintf(&msg, "Couldn't initialize SDL: %s", SDL_GetError()); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to start!", msg, NULL); + SDL_free(msg); + return SDL_APP_FAILURE; + } + + /* init app */ + App *app = make_app(); + if (app == NULL) { + return SDL_APP_FAILURE; + } + *appstate = app; + + SDL_ShowWindow(app->core.window); + draw(app); + return SDL_APP_CONTINUE; +} + + +SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { + App *app = appstate; + + switch (event->type) { + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + case SDL_EVENT_QUIT: { + return SDL_APP_SUCCESS; + } + case SDL_EVENT_MOUSE_BUTTON_DOWN: { + /* fine, it's a little ugly */ + if (event->button.windowID == SDL_GetWindowID(app->core.window)) { + SDL_ConvertEventToRenderCoordinates(app->core.renderer, event); + SDL_FPoint pos = { event->button.x, event->button.y }; + handle_mouse_down(event->button.button, pos, app); + } else if (event->button.windowID == SDL_GetWindowID(app->popup.window)) { + SDL_ConvertEventToRenderCoordinates(app->popup.renderer, event); + SDL_FPoint pos = { event->button.x, event->button.y }; + popup_mouse_down(event->button.button, pos, app); + } + break; + } + case SDL_EVENT_MOUSE_MOTION: { + if (event->motion.windowID == SDL_GetWindowID(app->core.window)) { + SDL_ConvertEventToRenderCoordinates(app->core.renderer, event); + SDL_FPoint pos = { event->motion.x, event->motion.y }; + handle_mouse_motion(event->motion.state, pos, app); + } else if (event->motion.windowID == SDL_GetWindowID(app->popup.window)) { + SDL_ConvertEventToRenderCoordinates(app->popup.renderer, event); + SDL_FPoint pos = { event->motion.x, event->motion.y }; + popup_mouse_motion(event->motion.state, pos, app); + } + break; + } + case SDL_EVENT_MOUSE_BUTTON_UP: { + if (event->button.windowID == SDL_GetWindowID(app->core.window)) { + SDL_ConvertEventToRenderCoordinates(app->core.renderer, event); + SDL_FPoint pos = { event->button.x, event->button.y }; + handle_mouse_up(event->button.button, pos, app); + } else if (event->button.windowID == SDL_GetWindowID(app->popup.window)) { + SDL_ConvertEventToRenderCoordinates(app->popup.renderer, event); + SDL_FPoint pos = { event->button.x, event->button.y }; + popup_mouse_up(event->button.button, pos, app); + } + break; + } + case SDL_EVENT_WINDOW_FOCUS_LOST: { + if (event->window.windowID == SDL_GetWindowID(app->popup.window)) { + SDL_HideWindow(app->popup.window); + } + break; + } + default: + break; + } + + if (event->type == app->playback.playback_progress_event) { + handle_playback_progress(app); + } + + return SDL_APP_CONTINUE; +} + + +SDL_AppResult SDL_AppIterate(void *appstate) { + App *app = appstate; + + draw(app); + if (!(SDL_GetWindowFlags(app->popup.window) & SDL_WINDOW_HIDDEN)) { + draw_popup(app); + } + + return SDL_APP_CONTINUE; +} + + +void SDL_AppQuit(void *appstate, SDL_AppResult result) { + if (result == SDL_APP_FAILURE) { + return; + } + + destroy_app(appstate); +} diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 0000000..5f5f478 --- /dev/null +++ b/src/mem.c @@ -0,0 +1,56 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#include "mem.h" + +#include + +#include +#include + + +void *amalloc(size_t size) { + void *ptr = malloc(size); + if (ptr == NULL) { + fputs("Memory allocation failure\n", stderr); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Ow!", "Memory allocation failure", NULL); + abort(); + } + return ptr; +} + +void *acalloc(size_t num, size_t size) { + void *ptr = calloc(num, size); + if (ptr == NULL) { + fputs("Memory allocation failure\n", stderr); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Ow!", "Memory allocation failure", NULL); + abort(); + } + return ptr; +} + +void *arealloc(void *ptr, size_t new_size) { + void *new_ptr = realloc(ptr, new_size); + if (new_ptr == NULL) { + fputs("Memory allocation failure\n", stderr); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Ow!", "Memory allocation failure", NULL); + abort(); + } + return new_ptr; +} diff --git a/src/mem.h b/src/mem.h new file mode 100644 index 0000000..b22430e --- /dev/null +++ b/src/mem.h @@ -0,0 +1,30 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef MEM_H +#define MEM_H + +#include +#include + +void *amalloc(size_t size); +void *acalloc(size_t num, size_t size); +void *arealloc(void *ptr, size_t new_size); + +#endif diff --git a/src/mp3.c b/src/mp3.c new file mode 100644 index 0000000..487e80a --- /dev/null +++ b/src/mp3.c @@ -0,0 +1,83 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#include "mp3.h" +#include "playback.h" + +#include +#include + + +mpg123_handle *create_mp3(void) { + int err; + mpg123_handle *mh = mpg123_new(NULL, &err); + if (mh != NULL) { + mpg123_param(mh, MPG123_FLAGS, MPG123_QUIET | MPG123_SKIP_ID3V2 | MPG123_NO_FRANKENSTEIN, 0); + } + return mh; +} + +bool init_mp3(Playback *playback, const char *path) { + mpg123_format_none(playback->mp3_decoder); + + mpg123_format2(playback->mp3_decoder, + 0, /* we'll take any sample rate */ + MPG123_MONO | MPG123_STEREO, + MPG123_ENC_FLOAT_32); + + long rate; + int channels, encoding; + if (mpg123_open(playback->mp3_decoder, path) != MPG123_OK || + mpg123_getformat(playback->mp3_decoder, &rate, &channels, &encoding) != MPG123_OK) { + mpg123_close(playback->mp3_decoder); + return false; + } + + playback->spec = (SDL_AudioSpec){ + .format = SDL_AUDIO_F32, + .channels = channels, + .freq = rate, + }; + playback->buffer_capacity = mpg123_outblock(playback->mp3_decoder); + + /* most certain way to figure out the total samples is to seek through it */ + mpg123_scan(playback->mp3_decoder); + playback->file_metadata.duration_frames = mpg123_length64(playback->mp3_decoder); + + return true; +} + +void stop_mp3(Playback *playback) { + mpg123_close(playback->mp3_decoder); +} + +void destroy_mp3(Playback *playback) { + mpg123_delete(playback->mp3_decoder); +} + +void seek_mp3(Playback *playback, uint64_t frame_idx) { + mpg123_seek64(playback->mp3_decoder, frame_idx, SEEK_SET); +} + +bool decode_mp3(Playback *playback) { + size_t done; + int err = mpg123_read(playback->mp3_decoder, playback->buffer, playback->buffer_capacity, &done); + playback->buffer_len = done; + return done != 0 && err == MPG123_OK; +} diff --git a/src/mp3.h b/src/mp3.h new file mode 100644 index 0000000..fa06fa9 --- /dev/null +++ b/src/mp3.h @@ -0,0 +1,40 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef MP3_H +#define MP3_H + +#include + +#include +#include +#include + + +typedef struct Playback Playback; + + +mpg123_handle *create_mp3(void); +bool init_mp3(Playback *playback, const char *path); +void stop_mp3(Playback *playback); +void destroy_mp3(Playback *playback); +void seek_mp3(Playback *playback, uint64_t frame_idx); +bool decode_mp3(Playback *playback); + +#endif diff --git a/src/playback.c b/src/playback.c new file mode 100644 index 0000000..a3b7e1d --- /dev/null +++ b/src/playback.c @@ -0,0 +1,380 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ +#include "playback.h" +#include "app.h" +#include "mem.h" +#include "flac.h" +#include "mp3.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + + +/* playback spec and buffer capacity should have been set before calling, + by a decoder init function. + yes, that's ugly, i know. */ +static void init_playback(Playback *playback) { + SDL_SetAudioStreamFormat(playback->output, &playback->spec, NULL); + playback->buffer_capacity = playback->buffer_capacity; + + playback->buffer = amalloc(playback->buffer_capacity); +} + + +/* after calling this, you don't need to worry about locking the audio stream + because it will be paused, so the callback won't be called. */ +static void reset_playback(Playback *playback) { + /* TODO: decide what this function should really "reset", i don't love it */ + set_paused(playback, true); + SDL_ClearAudioStream(playback->output); + + free(playback->buffer); + playback->buffer = NULL; + playback->buffer_capacity = 0; + playback->buffer_len = 0; +} + + +static int tag_read(Tagctx *ctx, void *buf, int cnt) { + Playback *playback = ctx->aux; + return fread(buf, 1, cnt, playback->file); +} + +static int tag_seek(Tagctx *ctx, int offset, int whence) { + Playback *playback = ctx->aux; + + /* libtags expects this return value to be like lseek(), not fseek() */ + if (fseek(playback->file, offset, whence) == -1) { + return -1; + } else { + return ftell(playback->file); + } +} + +/* libtags: Callback that is used by libtags to inform about the tags of a file. + "type" is the tag's type (Tartist, ...) or Tunknown if libtags doesn't + know how to map a tag kind to any of these. + ANY tag type that isn't Timage is considered a text tag. */ +static void tag_callback(Tagctx *ctx, Tagtype type, Tag *tag) { + if (type == Timage) { + return; + } + + Playback *playback = ctx->aux; + Metadata *metadata = &playback->file_metadata; + + switch (type) { + case Ttitle: { + SDL_free(playback->file_metadata.title); + metadata->title = SDL_strdup(tag->text.v); + metadata->has_title = true; + break; + } + case Tartist: { + SDL_free(playback->file_metadata.artist); + metadata->artist = SDL_strdup(tag->text.v); + metadata->has_artist = true; + break; + } + case Talbum: { + SDL_free(playback->file_metadata.album); + metadata->album = SDL_strdup(tag->text.v); + metadata->has_album = true; + break; + } + case Tdate: { + metadata->year = atoi(tag->text.v); + metadata->has_year = true; + break; + } + case Ttrack: { + metadata->track = atoi(tag->text.v); + metadata->has_track = true; + break; + } + case Tdisc: { + metadata->disc = atoi(tag->text.v); + metadata->has_disc = true; + break; + } + default: + break; + } +} + + +bool play(const char *file_path, Playback *playback) { + /* playback will be initialized when we have the necessary metadata */ + reset_playback(playback); + + /* `file_path` may be == `playback->file_path`, so dup the former _before_ + freeing the latter. */ + char *path = SDL_strdup(file_path); + /* also, for the same reason, you should not use `file_path` (the argument) + after freeing `playback->file_path` (the `playback` member). + Are we having fun yet? */ + SDL_free(playback->file_path); + playback->file_path = path; + + /* metadata */ + playback->file_metadata = (Metadata){ 0 }; + char tag_buf[256]; + Tagctx tag_ctx = { + .read = tag_read, + .seek = tag_seek, + .tag = tag_callback, + .aux = playback, + .buf = tag_buf, + .bufsz = sizeof tag_buf, + }; + playback->file = fopen(playback->file_path, "rb"); + tagsget(&tag_ctx); + fclose(playback->file); + + if (playback->file_metadata.has_title) { + SDL_Log("title: %s", playback->file_metadata.title); + } + if (playback->file_metadata.has_artist) { + SDL_Log("artist: %s", playback->file_metadata.artist); + } + if (playback->file_metadata.has_album) { + SDL_Log("album: %s", playback->file_metadata.album); + } + if (playback->file_metadata.has_year) { + SDL_Log("year: %d", playback->file_metadata.year); + } + if (playback->file_metadata.has_track) { + SDL_Log("track: %d", playback->file_metadata.track); + } + if (playback->file_metadata.has_disc) { + SDL_Log("disc: %d", playback->file_metadata.disc); + } + printf("\n"); + + if (tag_ctx.format == Fflac) { + if (playback->flac_decoder == NULL) { + playback->flac_decoder = create_flac(); + if (playback->flac_decoder == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create FLAC decoder."); + return false; + } + destroy_mp3(playback); + playback->mp3_decoder = NULL; + } + + if (!init_flac(playback, playback->file_path)) { + SDL_free(playback->file_path); + playback->file_path = NULL; + playback->file = NULL; + return false; + } + } else if (tag_ctx.format == Fmp3) { + if (playback->mp3_decoder == NULL) { + playback->mp3_decoder = create_mp3(); + if (playback->mp3_decoder == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create MP3 decoder."); + return false; + } + destroy_flac(playback); + playback->flac_decoder = NULL; + } + + if (!init_mp3(playback, playback->file_path)) { + SDL_free(playback->file_path); + playback->file_path = NULL; + playback->file = NULL; + return false; + } + } + init_playback(playback); + + playback->is_playing = true; + playback->is_paused = true; + playback->frame_idx = 0; + + set_paused(playback, false); + + return true; +} + + +void stop(Playback *playback) { + if (!playback->is_playing) { + return; + } + + reset_playback(playback); + + if (playback->flac_decoder != NULL) { + stop_flac(playback); + } + if (playback->mp3_decoder != NULL) { + stop_mp3(playback); + } + + /* `file_path` is kept, so the user can play it again */ + playback->file = NULL; + + playback->is_playing = false; + playback->is_paused = true; + + playback->frame_idx = 0; + SDL_Event event = { .type = playback->playback_progress_event}; + SDL_PushEvent(&event); +} + + +void seek(Playback *playback, float position) { + position = SDL_clamp(position, 0, 1); + + SDL_LockAudioStream(playback->output); + + SDL_ClearAudioStream(playback->output); + + uint64_t total = playback->file_metadata.duration_frames; + playback->frame_idx = (double)total * (double)position; + + if (playback->frame_idx == total) { + stop(playback); + } else { + if (playback->flac_decoder != NULL) { + seek_flac(playback, playback->frame_idx); + } + if (playback->mp3_decoder != NULL) { + seek_mp3(playback, playback->frame_idx); + } + } + + SDL_UnlockAudioStream(playback->output); +} + + +void set_paused(Playback *playback, bool value) { + SDL_LockAudioStream(playback->output); + + if (value) { + SDL_PauseAudioStreamDevice(playback->output); + } else { + SDL_ResumeAudioStreamDevice(playback->output); + } + + playback->is_paused = value; + + SDL_UnlockAudioStream(playback->output); +} + + +/* dump decoded data into `buffer` and set `buffer_len`. + returns false when there was no data to decode */ +static bool decode(Playback *playback) { + if (playback->flac_decoder != NULL) { + return decode_flac(playback->flac_decoder); + } + if (playback->mp3_decoder != NULL) { + return decode_mp3(playback); + } + + return false; +} + +void SDLCALL on_audio(void *userdata, SDL_AudioStream *stream, + int additional_amount, int total_amount) { + (void)total_amount; + + /* nothing needed right now */ + if (additional_amount == 0) { + return; + } + + Playback *playback = userdata; + + /* SDL would like some amount of data, so... */ + int put_amount = 0; + do { + /* ...decode a bit of audio (unless there's nothing left)... */ + if (!decode(playback)) { + stop(playback); + break; + } + + /* ...and shove all that was decoded into the output stream, no matter + how much SDL says it wants, since it takes just what it needs from + the stream anyway if it's more than enough. */ + SDL_PutAudioStreamData(stream, playback->buffer, playback->buffer_len); + put_amount += playback->buffer_len; + } while (put_amount <= additional_amount); /* go again if it wasn't enough. */ +} + + +void SDLCALL on_postmix(void *userdata, const SDL_AudioSpec *spec, + float *buffer, int buflen) { + (void)buffer; + + /* updating `playback->frame_idx` here instead of the audio callback makes + it represent what's _currently being played_, as opposed to what was _decoded_ + (and fed to SDL), and less importantly, the smaller, more frequent updates + make it smoother. + But I'll tell you a few things about this callback. They're important + enough that I'll even repeat things that are in the SDL docs even though + you've totally read them. + + * It will not run if the device is paused, but it will run if it has no + data while unpaused (the buffer will be silence). hence the check for + `playback->is_playing` down there, but not `playback->is_paused`. + + * While the buffer is always in f32 format, `spec` here describes the + final audio mix the device will get. that means the channel count and + sample rate could be anything, not necessarily what's been stored in + `playback`. so, what's added to `playback->frame_idx` here is really a + guess (hopefully a good one) + + * it may run in the audio thread, but unlike the audio callback, it is for + the audio _device_, so it obviously won't lock the output _stream_. + That's why this function locks it, while `on_audio` just assumes it was + already locked. */ + + Playback *playback = userdata; + SDL_LockAudioStream(playback->output); + + if (!playback->is_playing) { + SDL_UnlockAudioStream(playback->output); + return; + } + + /* TODO: but admittedly this math needs to be tested with weirder setups */ + int num_samples = buflen / sizeof *buffer; + float desired_channel_ratio = (float)playback->spec.channels / (float)spec->channels; + float desired_freq_ratio = (float)playback->spec.freq / (float)spec->freq; + int num_source_samples = num_samples * desired_channel_ratio * desired_freq_ratio; + int num_source_frames = num_source_samples / playback->spec.channels; + playback->frame_idx += num_source_frames; + + SDL_Event event = { .type = playback->playback_progress_event}; + SDL_UnlockAudioStream(playback->output); + + SDL_PushEvent(&event); +} diff --git a/src/playback.h b/src/playback.h new file mode 100644 index 0000000..580591d --- /dev/null +++ b/src/playback.h @@ -0,0 +1,89 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef PLAYBACK_H +#define PLAYBACK_H + +#include +#include +#include + + +typedef struct App App; + + +typedef struct Metadata { + char *title; + char *artist; + char *album; + uint16_t year; + uint16_t track; + uint8_t disc; + bool has_title; + bool has_artist; + bool has_album; + bool has_year; + bool has_track; + bool has_disc; + uint64_t duration_frames; +} Metadata; + +/* lock the output stream when accessing this, if it isn't paused */ +typedef struct Playback { + SDL_AudioStream* output; + SDL_AudioSpec spec; + + float *buffer; + size_t buffer_capacity; + int buffer_len; + + char *file_path; + FILE *file; + Metadata file_metadata; + + FLAC__StreamDecoder *flac_decoder; + mpg123_handle *mp3_decoder; + + bool is_playing; + bool is_paused; + uint64_t frame_idx; /* out of `file_metadata.duration_frames` */ + Uint32 playback_progress_event; +} Playback; + + +uint64_t ms_to_frames(int sample_rate, int ms); + +bool play(const char *file_path, Playback *playback); + +void stop(Playback *playback); + +/* Seek to a position in the audio file between 0 and 1. */ +/* Values outside of that range are clamped. */ +void seek(Playback *playback, float position); + +void set_paused(Playback *playback, bool value); + +/* Audio output callback. */ +void SDLCALL on_audio(void *userdata, SDL_AudioStream *stream, + int additional_amount, int total_amount); +/* Audio postmix callback. */ +void SDLCALL on_postmix(void *userdata, const SDL_AudioSpec *spec, + float *buffer, int buflen); + +#endif diff --git a/src/popup.c b/src/popup.c new file mode 100644 index 0000000..776275d --- /dev/null +++ b/src/popup.c @@ -0,0 +1,184 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#include "popup.h" +#include "app.h" +#include "mem.h" +#include "files.h" + +#include + + +void popup_mouse_down(Uint8 button, SDL_FPoint pos, App *app) { + if (button != SDL_BUTTON_LEFT || button != SDL_BUTTON_RIGHT) { + return; + } + + for (size_t i = 0; i < app->popup.options_len; ++i) { + PopupOption *option = &app->popup.options[i]; + if (!option->is_disabled && SDL_PointInRectFloat(&pos, &option->rect)) { + option->is_held = true; + return; + } + } +} + +void popup_mouse_motion(SDL_MouseButtonFlags state, SDL_FPoint pos, App *app) { + (void)state; + + for (size_t i = 0; i < app->popup.options_len; ++i) { + PopupOption *option = &app->popup.options[i]; + + option->is_hovered = false; + if (SDL_PointInRectFloat(&pos, &option->rect)) { + option->is_hovered = true; + } + } +} + +void popup_mouse_up(Uint8 button, SDL_FPoint pos, App *app) { + if (button != SDL_BUTTON_LEFT && button != SDL_BUTTON_RIGHT) { + return; + } + + for (size_t i = 0; i < app->popup.options_len; ++i) { + PopupOption *option = &app->popup.options[i]; + if (SDL_PointInRectFloat(&pos, &option->rect)) { + SDL_HideWindow(app->popup.window); + option->on_up(option, app); + } + option->is_held = false; + } +} + + +static void SDLCALL on_file_dialog(void *userdata, const char *const *filelist, int filter) { + (void)filter; + + App *app = userdata; + app->core.is_file_dialog_open = false; + + if (filelist == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "File dialog error: %s", SDL_GetError()); + return; + } else if (*filelist == NULL) { + /* no file selected */ + return; + } + + /* TODO: multiple files */ + const char *path = *filelist; + if (path_exists(path)) { + stop(&app->playback); + play(path, &app->playback); + } +} + +static void on_open_file(PopupOption *option, App *app) { + (void)option, (void)app; + + if (app->core.is_file_dialog_open) { + return; + } + + SDL_ShowOpenFileDialog(on_file_dialog, + app, + app->core.window, + app->core.file_filters, app->core.file_filters_len, + NULL, + 0); + app->core.is_file_dialog_open = true; +} + +static void on_quit(PopupOption *option, App *app) { + (void)option, (void)app; + + SDL_Event e = { .type = SDL_EVENT_QUIT }; + SDL_PushEvent(&e); +} + + +void show_popup(SDL_FPoint pos, App *app) { + PopupOption options[] = { + { .label = "open file...", .on_up = (PopupOptionHandler)on_open_file }, + { .label = "quit", .on_up = (PopupOptionHandler)on_quit }, + }; + int popup_height = 0; + for (size_t i = 0; i < SDL_arraysize(options); ++i) { + options[i].rect = (SDL_FRect){ + POPUP_BORDER_SIZE, + POPUP_BORDER_SIZE + (POPUP_OPTION_HEIGHT * i), + POPUP_WIDTH - (POPUP_BORDER_SIZE*2), + POPUP_OPTION_HEIGHT, + }; + popup_height += POPUP_OPTION_HEIGHT; + } + + app->popup.options = arealloc(app->popup.options, sizeof options); + memcpy(app->popup.options, options, sizeof options); + app->popup.options_len = SDL_arraysize(options); + + SDL_SetWindowSize(app->popup.window, POPUP_WIDTH*APP_SCALE, (popup_height + POPUP_BORDER_SIZE*2)*APP_SCALE); + SDL_SetRenderLogicalPresentation(app->popup.renderer, + POPUP_WIDTH, popup_height + POPUP_BORDER_SIZE*2, + SDL_LOGICAL_PRESENTATION_STRETCH); + + SDL_FPoint window_pos; + SDL_RenderCoordinatesToWindow(app->core.renderer, pos.x, pos.y, &window_pos.x, &window_pos.y); + SDL_SetWindowPosition(app->popup.window, window_pos.x, window_pos.y); + + SDL_ShowWindow(app->popup.window); + draw_popup(app); +} + + +void draw_popup(App *app) { + SDL_Renderer *renderer = app->popup.renderer; + int render_w, render_h; + SDL_GetRenderLogicalPresentation(renderer, &render_w, &render_h, NULL); + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, 255, 241, 232, 255); + SDL_FRect border_rect = { + POPUP_BORDER_SIZE, + POPUP_BORDER_SIZE, + render_w - (POPUP_BORDER_SIZE*2), + render_h - (POPUP_BORDER_SIZE*2), + }; + SDL_RenderFillRect(renderer, &border_rect); + + for (size_t i = 0; i < app->popup.options_len; ++i) { + PopupOption *option = &app->popup.options[i]; + + if (option->is_hovered) { + SDL_SetRenderDrawColor(renderer, 194, 195, 199, 255); + SDL_RenderFillRect(renderer, &option->rect); + } + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderDebugText(renderer, + POPUP_BORDER_SIZE + 2, + 2 + option->rect.y, + option->label); + } + + SDL_RenderPresent(renderer); +} diff --git a/src/popup.h b/src/popup.h new file mode 100644 index 0000000..27a69c0 --- /dev/null +++ b/src/popup.h @@ -0,0 +1,61 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef POPUP_H +#define POPUP_H + +#include + + +#define POPUP_BORDER_SIZE 2 +#define POPUP_WIDTH (128 + (POPUP_BORDER_SIZE*2)) +#define POPUP_HEIGHT (128 + (POPUP_BORDER_SIZE*2)) +#define POPUP_OPTION_HEIGHT 12 + + +typedef struct App App; + +typedef struct PopupOption PopupOption; +typedef void (*PopupOptionHandler)(PopupOption *option, App *app); + +typedef struct PopupOption { + SDL_FRect rect; + const char *label; + PopupOptionHandler on_up; + bool is_disabled; + bool is_hovered; + bool is_held; +} PopupOption; + +typedef struct Popup { + SDL_Window *window; + SDL_Renderer *renderer; + + PopupOption *options; + size_t options_len; +} Popup; + + +void popup_mouse_down(Uint8 button, SDL_FPoint pos, App *app); +void popup_mouse_motion(SDL_MouseButtonFlags state, SDL_FPoint pos, App *app); +void popup_mouse_up(Uint8 button, SDL_FPoint pos, App *app); +void show_popup(SDL_FPoint pos, App *app); +void draw_popup(App *app); + +#endif diff --git a/src/widgets.c b/src/widgets.c new file mode 100644 index 0000000..774caeb --- /dev/null +++ b/src/widgets.c @@ -0,0 +1,323 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#include "widgets.h" + +#include "app.h" +#include "playback.h" + +#include + + +#define WINDOW_BTN_MARGIN 1 + +/* minimize button */ +static void btn_minimize_up(Button *button, App *app); + +Button make_btn_minimize(void) { + Button btn = { + .id = BUTTON_WINDOW_MINIMIZE, + .on_up = (ButtonHandler)btn_minimize_up, + .rect = { APP_WIDTH-32 - WINDOW_BTN_MARGIN, WINDOW_BTN_MARGIN, 16, 16 }, + .src_rect = { 0, 0, 16, 16 }, + .src_rect_held = { 0, 16, 16, 16 }, + }; + + return btn; +} + +static void btn_minimize_up(Button *button, App *app) { + (void)button; + SDL_MinimizeWindow(app->core.window); +} + + +/* close button */ +static void btn_close_up(Button *button, App *app); + +Button make_btn_close(void) { + Button btn = { + .id = BUTTON_WINDOW_CLOSE, + .on_up = (ButtonHandler)btn_close_up, + .rect = { APP_WIDTH-16 - WINDOW_BTN_MARGIN, WINDOW_BTN_MARGIN, 16, 16 }, + .src_rect = { 16, 0, 16, 16 }, + .src_rect_held = { 16, 16, 16, 16 }, + }; + + return btn; +} + +static void btn_close_up(Button *button, App *app) { + (void)button; + SDL_SetTrayEntryLabel(app->core.tray_entry_toggle, "Show Window"); + SDL_HideWindow(app->core.window); +} + + +/* play button */ +static void btn_play_up(Button *button, App *app); + +Button make_btn_play(void) { + Button btn = { + .id = BUTTON_PLAYBACK_PLAY, + .on_up = (ButtonHandler)btn_play_up, + .rect = { 32, 96, 16, 16 }, + .src_rect = { 32, 0, 16, 16 }, + .src_rect_held = { 32, 16, 16, 16 }, + }; + + return btn; +} + +static void btn_play_up(Button *button, App *app) { + (void)button; + + Playback *playback = &app->playback; + + set_paused(playback, false); + + if (playback->is_playing) { + seek(playback, 0); + } else { + if (playback->file_path != NULL) { + play(playback->file_path, playback); + } + } +} + + +/* pause button */ +static void btn_pause_up(Button *button, App *app); + +Button make_btn_pause(void) { + Button btn = { + .id = BUTTON_PLAYBACK_PAUSE, + .on_up = (ButtonHandler)btn_pause_up, + .rect = { 48, 96, 16, 16 }, + .src_rect = { 48, 0, 16, 16 }, + .src_rect_held = { 48, 16, 16, 16 }, + }; + + return btn; +} + +static void btn_pause_up(Button *button, App *app) { + (void)button; + + Playback *playback = &app->playback; + if (playback->is_playing) { + set_paused(playback, !playback->is_paused); + } else if (playback->file_path != NULL) { + play(playback->file_path, playback); + } +} + + +/* stop button */ +static void btn_stop_up(Button *button, App *app); + +Button make_btn_stop(void) { + Button btn = { + .id = BUTTON_PLAYBACK_STOP, + .on_up = (ButtonHandler)btn_stop_up, + .rect = { 64, 96, 16, 16 }, + .src_rect = { 64, 0, 16, 16 }, + .src_rect_held = { 64, 16, 16, 16 }, + }; + + return btn; +} + +static void btn_stop_up(Button *button, App *app) { + (void)button; + + stop(&app->playback); +} + + +static void draw_button(Button *button, Core *core, Widgets *widgets) { + SDL_RenderTexture(core->renderer, + widgets->spritesheet, + button->is_held ? &button->src_rect_held : &button->src_rect, + &button->rect); +} + + +enum FuncType { + FUNC_TYPE_DOWN, + FUNC_TYPE_UP, + FUNC_TYPE_DRAG, + FUNC_TYPE_PLAYBACK_PROGRESS, + FUNC_TYPE_DRAW, +}; + + +static bool button_func(enum FuncType type, Button *button, App *app) { + if (type == FUNC_TYPE_DRAW) { + draw_button(button, &app->core, &app->widgets); + return false; + } + + if (type == FUNC_TYPE_UP) { + if (button->on_up != NULL) { + button->on_up(button, app); + return true; + } + } + + return false; +} + +/* it's a little nicer externally if it's separate functions instead of an enum */ +bool button_down(Button *button, App *app) { + return button_func(FUNC_TYPE_DOWN, button, app); +} + +bool button_up(Button *button, App *app) { + return button_func(FUNC_TYPE_UP, button, app); +} + +void button_draw(Button *button, App *app) { + button_func(FUNC_TYPE_DRAW, button, app); +} + + +/* seekbar */ +static void slider_seekbar_up(Slider *slider, App *app); +static void slider_seekbar_playback_progress(Slider *slider, App *app); + +Slider make_slider_seekbar(void) { + Slider slider = { + .id = SLIDER_PLAYBACK_SEEKBAR, + .on_up = (SliderHandler)slider_seekbar_up, + .on_playback_progress = (SliderHandler)slider_seekbar_playback_progress, + .rect = { 16, 64, 224, 16 }, + .bg_src_rect = { 0, 32, 1, 1 }, + .knob_size = 16, + .knob_src_rect = { 1, 32, 1, 1 }, + .knob_src_rect_held = { 2, 32, 1, 1 }, + }; + + return slider; +} + +static void slider_seekbar_up(Slider *slider, App *app) { + Playback *playback = &app->playback; + + SDL_LockAudioStream(playback->output); + bool is_playing = playback->is_playing; + char *file_path = playback->file_path; + SDL_UnlockAudioStream(playback->output); + + if (!is_playing) { + if (file_path != NULL) { + play(file_path, playback); + } else { + slider->position = 0; + return; + } + } + + seek(playback, slider->position); +} + +static void slider_seekbar_playback_progress(Slider *slider, App *app) { + Playback *playback = &app->playback; + + SDL_LockAudioStream(playback->output); + slider->position = (double)playback->frame_idx / (double)playback->file_metadata.duration_frames; + SDL_UnlockAudioStream(playback->output); + slider->position = SDL_clamp(slider->position, 0, 1); +} + + +static void draw_slider(Slider *slider, Core *core, Widgets *widgets) { + /* background */ + SDL_RenderTexture(core->renderer, + widgets->spritesheet, + &slider->bg_src_rect, + &slider->rect); + + /* knob */ + SDL_FRect knob_rect = { + slider->rect.x + (slider->position * (slider->rect.w - slider->knob_size)), + slider->rect.y, + slider->knob_size, + slider->knob_size, + }; + knob_rect.x = SDL_clamp(knob_rect.x, + slider->rect.x, + slider->rect.x + (slider->rect.w - slider->knob_size)); + knob_rect.x = roundf(knob_rect.x); + knob_rect.y = roundf(knob_rect.y); + SDL_RenderTexture(core->renderer, + widgets->spritesheet, + slider->is_sliding ? &slider->knob_src_rect_held : &slider->knob_src_rect, + &knob_rect); +} + + +static bool slider_func(enum FuncType type, Slider *slider, App *app, SDL_FPoint mouse_pos) { + if (type == FUNC_TYPE_DRAW) { + draw_slider(slider, &app->core, &app->widgets); + return false; + } + + if (type != FUNC_TYPE_PLAYBACK_PROGRESS) { + float x_in_slider = (mouse_pos.x - slider->knob_size/2) - slider->rect.x; + slider->position = x_in_slider / (slider->rect.w - slider->knob_size); + slider->position = SDL_clamp(slider->position, 0, 1); + } + + if (type == FUNC_TYPE_UP) { + if (slider->on_up != NULL) { + slider->on_up(slider, app); + return true; + } + } else if (type == FUNC_TYPE_PLAYBACK_PROGRESS) { + if (slider->on_playback_progress != NULL) { + slider->on_playback_progress(slider, app); + return true; + } + } + + return false; +} + +bool slider_down(Slider *slider, App *app, SDL_FPoint mouse_pos) { + return slider_func(FUNC_TYPE_DOWN, slider, app, mouse_pos); +} + +bool slider_drag(Slider *slider, App *app, SDL_FPoint mouse_pos) { + return slider_func(FUNC_TYPE_DRAG, slider, app, mouse_pos); +} + +bool slider_up(Slider *slider, App *app, SDL_FPoint mouse_pos) { + return slider_func(FUNC_TYPE_UP, slider, app, mouse_pos); +} + +void slider_playback_progress(Slider *slider, App *app) { + SDL_FPoint mouse_pos = { 0 }; + slider_func(FUNC_TYPE_PLAYBACK_PROGRESS, slider, app, mouse_pos); +} + +void slider_draw(Slider *slider, App *app) { + SDL_FPoint mouse_pos = { 0 }; + slider_func(FUNC_TYPE_DRAW, slider, app, mouse_pos); +} diff --git a/src/widgets.h b/src/widgets.h new file mode 100644 index 0000000..dcf5bd4 --- /dev/null +++ b/src/widgets.h @@ -0,0 +1,101 @@ +/* + Copyright (C) 2026 wanp + + This file is part of Wavy. + + Wavy 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. + + Wavy 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 Wavy. If not, see . +*/ + +#ifndef WIDGETS_H +#define WIDGETS_H + +#include + + +typedef struct App App; + + +typedef struct Widgets { + SDL_Texture *spritesheet; /* all widgets use the same texture */ + + struct Button *buttons; + size_t buttons_len; + struct Slider *sliders; + size_t sliders_len; +} Widgets; + + +enum ButtonID { + BUTTON_WINDOW_MINIMIZE, + BUTTON_WINDOW_CLOSE, + BUTTON_PLAYBACK_PLAY, + BUTTON_PLAYBACK_PAUSE, + BUTTON_PLAYBACK_STOP, +}; + +typedef struct Button Button; +typedef void (*ButtonHandler)(Button *button, App *app); + +typedef struct Button { + enum ButtonID id; + ButtonHandler on_up; + SDL_FRect rect; + SDL_FRect src_rect; + SDL_FRect src_rect_held; + bool is_held; +} Button; + +Button make_btn_minimize(void); +Button make_btn_close(void); +Button make_btn_play(void); +Button make_btn_pause(void); +Button make_btn_stop(void); + +bool button_down(Button *button, App *app); +bool button_up(Button *button, App *app); +void button_draw(Button *button, App *app); + + +enum SliderID { + SLIDER_PLAYBACK_SEEKBAR, +}; + +typedef struct Slider Slider; +typedef void (*SliderHandler)(Slider *slider, App *app); + +typedef struct Slider { + enum SliderID id; + SliderHandler on_up; + SliderHandler on_playback_progress; + + SDL_FRect rect; + SDL_FRect bg_src_rect; + int knob_size; + SDL_FRect knob_src_rect; + SDL_FRect knob_src_rect_held; + bool is_vertical; + + bool is_sliding; + float position; +} Slider; + +Slider make_slider_seekbar(void); + +bool slider_down(Slider *slider, App *app, SDL_FPoint mouse_pos); +bool slider_drag(Slider *slider, App *app, SDL_FPoint mouse_pos); +bool slider_up(Slider *slider, App *app, SDL_FPoint mouse_pos); +void slider_playback_progress(Slider *slider, App *app); +void slider_draw(Slider *slider, App *app); + +#endif diff --git a/subprojects/libtags.wrap b/subprojects/libtags.wrap new file mode 100644 index 0000000..af77944 --- /dev/null +++ b/subprojects/libtags.wrap @@ -0,0 +1,21 @@ +# Copyright (C) 2026 wanp +# +# This file is part of Wavy. +# +# Wavy 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. +# +# Wavy 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 Wavy. If not, see . + +[wrap-git] +url = https://git.sr.ht/~ft/libtags +revision = 18cd628fc77dfb072cb65e44bdb2927e3e93bb0c +depth = 1