diff options
| author | 2026-02-17 21:19:02 -0500 | |
|---|---|---|
| committer | 2026-02-17 21:19:02 -0500 | |
| commit | c259a3c0bb5231ab3797d553503b710adb3c24e8 (patch) | |
| tree | 16ffb85bd6b39f81b70ca45f35789179a14159ec | |
initial commit
| -rw-r--r-- | .clang-format | 5 | ||||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | LICENSE | 675 | ||||
| -rw-r--r-- | Makefile | 31 | ||||
| -rw-r--r-- | src/config.h | 82 | ||||
| -rw-r--r-- | src/defs.h | 222 | ||||
| -rw-r--r-- | src/tilite.c | 2503 |
7 files changed, 3520 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0fc6dd1 --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +--- +UseTab: Always +IndentWidth: 4 +TabWidth: 4 +# BasedOnStyle: LLVM diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01298db --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +tilite @@ -0,0 +1,675 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + 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. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<https://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<https://www.gnu.org/licenses/why-not-lgpl.html>. + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e796520 --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +CC = cc + +PREFIX = /usr/local +LIBS = -lX11 -lXinerama -lXcursor + +CPPFLAGS = -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=700 +CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os ${CPPFLAGS} -fdiagnostics-color=always -I/usr/X11R6/include +LDFLAGS = ${LIBS} -L/usr/X11R6/lib + +SRC = src/tilite.c +OBJ = build/tilite.o + +all: tilite + +build/tilite.o: src/tilite.c + mkdir -p build + ${CC} -c ${CFLAGS} src/tilite.c -o build/tilite.o + +tilite: ${OBJ} + ${CC} -o tilite ${OBJ} ${LDFLAGS} + +clean: + rm -rf build tilite + +install: all + mkdir -p ${PREFIX}/bin + cp -f tilite ${PREFIX}/bin/ + chmod 755 ${PREFIX}/bin/tilite + +uninstall: + rm -f ${PREFIX}/bin/tilite diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..67e4cf4 --- /dev/null +++ b/src/config.h @@ -0,0 +1,82 @@ +#pragma once +#include <X11/X.h> +#include <X11/keysym.h> +#include "defs.h" + +#define MODKEY Mod4Mask + +#define CFG_FOCUSED_BORDER_COL "#89B4FA" +#define CFG_UNFOCUSED_BORDER_COL "#1E1E2E" +#define CFG_SWAP_BORDER_COL "#1E1E2E" + +#define CFG_GAPS 5 +#define CFG_BORDER_WIDTH 3 +#define CFG_MASTER_WIDTH 0.70f +#define CFG_RESIZE_MASTER_AMT 1 +#define CFG_RESIZE_STACK_AMT 20 +#define CFG_MOVE_WINDOW_AMT 50 +#define CFG_RESIZE_WINDOW_AMT 50 +#define CFG_SNAP_DISTANCE 5 +#define CFG_MOTION_THROTTLE 60 +#define CFG_NEW_WIN_FOCUS True +#define CFG_WARP_CURSOR True +#define CFG_FLOATING_ON_TOP True +#define CFG_NEW_WIN_MASTER False + +#define CFG_BINDS \ + /* Application launchers */ \ + { MODKEY, XK_Return, 0, { .cmd = build_argv("kitty") }, TYPE_CMD }, \ + { MODKEY, XK_w, 0, { .cmd = build_argv("surf ecosia.org") }, TYPE_CMD }, \ + { MODKEY, XK_space, 0, { .cmd = build_argv("dmenu_run") }, TYPE_CMD }, \ + { MODKEY, XK_equal, 0, { .cmd = build_argv("pactl set-sink-volume @DEFAULT_SINK@ +5%") }, TYPE_CMD }, \ + { MODKEY, XK_minus, 0, { .cmd = build_argv("pactl set-sink-volume @DEFAULT_SINK@ -5%") }, TYPE_CMD }, \ + { MODKEY, XK_0, 0, { .cmd = build_argv("pactl set-sink-mute @DEFAULT_SINK@ toggle") }, TYPE_CMD }, \ + /* Window management */ \ + { MODKEY, XK_q, 0, { .fn = close_focused }, TYPE_FUNC }, \ + { MODKEY|ShiftMask, XK_e, 0, { .fn = quit }, TYPE_FUNC }, \ + { MODKEY, XK_m, 0, { .fn = toggle_monocle }, TYPE_FUNC }, \ + /* Focus */ \ + { MODKEY, XK_j, 0, { .fn = focus_next }, TYPE_FUNC }, \ + { MODKEY, XK_k, 0, { .fn = focus_prev }, TYPE_FUNC }, \ + /* Master/stack movement */ \ + { MODKEY|ShiftMask, XK_j, 0, { .fn = move_master_next }, TYPE_FUNC }, \ + { MODKEY|ShiftMask, XK_k, 0, { .fn = move_master_prev }, TYPE_FUNC }, \ + /* Master resize */ \ + { MODKEY, XK_l, 0, { .fn = resize_master_add }, TYPE_FUNC }, \ + { MODKEY, XK_h, 0, { .fn = resize_master_sub }, TYPE_FUNC }, \ + /* Stack resize */ \ + { MODKEY|ControlMask, XK_l, 0, { .fn = resize_stack_add }, TYPE_FUNC }, \ + { MODKEY|ControlMask, XK_h, 0, { .fn = resize_stack_sub }, TYPE_FUNC }, \ + /* Keyboard window movement */ \ + { MODKEY, XK_Up, 0, { .fn = move_win_up }, TYPE_FUNC }, \ + { MODKEY, XK_Down, 0, { .fn = move_win_down }, TYPE_FUNC }, \ + { MODKEY, XK_Left, 0, { .fn = move_win_left }, TYPE_FUNC }, \ + { MODKEY, XK_Right, 0, { .fn = move_win_right }, TYPE_FUNC }, \ + /* Keyboard window resize */ \ + { MODKEY|ShiftMask, XK_Up, 0, { .fn = resize_win_up }, TYPE_FUNC }, \ + { MODKEY|ShiftMask, XK_Down, 0, { .fn = resize_win_down }, TYPE_FUNC }, \ + { MODKEY|ShiftMask, XK_Left, 0, { .fn = resize_win_left }, TYPE_FUNC }, \ + { MODKEY|ShiftMask, XK_Right, 0, { .fn = resize_win_right }, TYPE_FUNC }, \ + /* Floating / fullscreen */ \ + { MODKEY, XK_f, 0, { .fn = toggle_floating }, TYPE_FUNC }, \ + { MODKEY|ShiftMask, XK_space, 0, { .fn = toggle_floating_global }, TYPE_FUNC }, \ + { MODKEY|ShiftMask, XK_f, 0, { .fn = toggle_fullscreen }, TYPE_FUNC }, \ + /* Workspaces 1–9 */ \ + { MODKEY, XK_1, 0, { .ws = 0 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_1, 0, { .ws = 0 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_2, 0, { .ws = 1 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_2, 0, { .ws = 1 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_3, 0, { .ws = 2 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_3, 0, { .ws = 2 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_4, 0, { .ws = 3 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_4, 0, { .ws = 3 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_5, 0, { .ws = 4 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_5, 0, { .ws = 4 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_6, 0, { .ws = 5 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_6, 0, { .ws = 5 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_7, 0, { .ws = 6 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_7, 0, { .ws = 6 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_8, 0, { .ws = 7 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_8, 0, { .ws = 7 }, TYPE_WS_MOVE }, \ + { MODKEY, XK_9, 0, { .ws = 8 }, TYPE_WS_CHANGE }, \ + { MODKEY|ShiftMask, XK_9, 0, { .ws = 8 }, TYPE_WS_MOVE }, diff --git a/src/defs.h b/src/defs.h new file mode 100644 index 0000000..95c3ea1 --- /dev/null +++ b/src/defs.h @@ -0,0 +1,222 @@ +#pragma once +#include <X11/Xlib.h> +#define VERSION "tilite ver. 1.0" +#define AUTHOR "(C) Lance Borden 2026" +#define LICENSE "Licensed under the GPL v3.0" + +#define MF_MIN 0.05f +#define MF_MAX 0.95f +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define UDIST(a, b) abs((int)(a) - (int)(b)) +#define CLAMP(x, lo, hi) (((x) < (lo)) ? (lo) : ((x) > (hi)) ? (hi) : (x)) + +#define MAX_BINDS 256 +#define MAX_CLIENTS 99 +#define MAX_SCRATCHPADS 32 +#define MAX_ITEMS 256 +#define MIN_WINDOW_SIZE 20 +#define PATH_MAX 4096 + +/* workspaces */ +#define TYPE_WS_CHANGE 0 +#define TYPE_WS_MOVE 1 +/* fn/cmd */ +#define TYPE_FUNC 2 +#define TYPE_CMD 3 + +#define NUM_WORKSPACES 9 +#define WORKSPACE_NAMES \ + "1" \ + "\0" \ + "2" \ + "\0" \ + "3" \ + "\0" \ + "4" \ + "\0" \ + "5" \ + "\0" \ + "6" \ + "\0" \ + "7" \ + "\0" \ + "8" \ + "\0" \ + "9" \ + "\0" + +typedef enum { DRAG_NONE, DRAG_MOVE, DRAG_RESIZE, DRAG_SWAP } DragMode; +typedef void (*event_t)(XEvent *); + +typedef union { + const char **cmd; + void (*fn)(void); + int ws; +} action_t; + +typedef struct { + int mods; + KeySym keysym; + KeyCode keycode; + action_t action; + int type; +} binding_t; + +typedef struct client_t { + Window win; + int x, y, w, h; + int orig_x, orig_y, orig_w, orig_h; + int custom_stack_height; + int ws; + Bool fixed; + Bool floating; + Bool fullscreen; + Bool mapped; + pid_t pid; + struct client_t *next; +} client_t; + +typedef struct { + int modkey; + int gaps; + int border_width; + long border_foc_col; + long border_ufoc_col; + long border_swap_col; + float master_width; + int motion_throttle; + int resize_master_amt; + int resize_stack_amt; + int snap_distance; + int n_binds; + int move_window_amt; + int resize_window_amt; + Bool new_win_focus; + Bool warp_cursor; + Bool floating_on_top; + Bool new_win_master; + binding_t binds[MAX_ITEMS]; + char *to_run[MAX_ITEMS]; +} config_t; + +typedef struct { + const char *name; + void (*fn)(void); +} command_t; + +typedef enum { + ATOM_NET_ACTIVE_WINDOW, + ATOM_NET_CURRENT_DESKTOP, + ATOM_NET_SUPPORTED, + ATOM_NET_WM_STATE, + ATOM_NET_WM_STATE_FULLSCREEN, + ATOM_WM_STATE, + ATOM_NET_WM_WINDOW_TYPE, + ATOM_NET_WORKAREA, + ATOM_WM_DELETE_WINDOW, + ATOM_NET_WM_STRUT, + ATOM_NET_WM_STRUT_PARTIAL, + ATOM_NET_SUPPORTING_WM_CHECK, + ATOM_NET_WM_NAME, + ATOM_UTF8_STRING, + ATOM_NET_WM_DESKTOP, + ATOM_NET_CLIENT_LIST, + ATOM_NET_FRAME_EXTENTS, + ATOM_NET_NUMBER_OF_DESKTOPS, + ATOM_NET_DESKTOP_NAMES, + ATOM_NET_WM_PID, + ATOM_NET_WM_WINDOW_TYPE_DOCK, + ATOM_NET_WM_WINDOW_TYPE_UTILITY, + ATOM_NET_WM_WINDOW_TYPE_DIALOG, + ATOM_NET_WM_WINDOW_TYPE_TOOLBAR, + ATOM_NET_WM_WINDOW_TYPE_SPLASH, + ATOM_NET_WM_WINDOW_TYPE_POPUP_MENU, + ATOM_NET_WM_WINDOW_TYPE_MENU, + ATOM_NET_WM_WINDOW_TYPE_DROPDOWN_MENU, + ATOM_NET_WM_WINDOW_TYPE_TOOLTIP, + ATOM_NET_WM_WINDOW_TYPE_NOTIFICATION, + ATOM_NET_WM_STATE_MODAL, + ATOM_WM_PROTOCOLS, + ATOM_COUNT +} atom_type_t; + +const char **build_argv(const char *cmd); +client_t *add_client(Window w, int ws); +void apply_fullscreen(client_t *c, Bool on); +void change_workspace(int ws); +int check_parent(pid_t p, pid_t c); +int clean_mask(int mask); +void close_focused(void); +client_t *find_client(Window w); +Window find_toplevel(Window w); +void focus_next(void); +void focus_prev(void); +pid_t get_parent_process(pid_t c); +pid_t get_pid(Window w); +int get_workspace_for_window(Window w); +void grab_button(Mask button, Mask mod, Window w, Bool owner_events, + Mask masks); +void grab_keys(void); +void hdl_button(XEvent *xev); +void hdl_button_release(XEvent *xev); +void hdl_client_msg(XEvent *xev); +void hdl_config_ntf(XEvent *xev); +void hdl_config_req(XEvent *xev); +void hdl_dummy(XEvent *xev); +void hdl_destroy_ntf(XEvent *xev); +void hdl_keypress(XEvent *xev); +void hdl_mapping_ntf(XEvent *xev); +void hdl_map_req(XEvent *xev); +void hdl_motion(XEvent *xev); +void hdl_property_ntf(XEvent *xev); +void hdl_unmap_ntf(XEvent *xev); +void init_defaults(void); +Bool is_child_proc(pid_t pid1, pid_t pid2); +void move_master_next(void); +void move_master_prev(void); +void move_to_workspace(int ws); +void move_win_down(void); +void move_win_left(void); +void move_win_right(void); +void move_win_up(void); +void other_wm(void); +int other_wm_err(Display *d, XErrorEvent *ee); +long parse_col(const char *hex); +void quit(void); +void resize_master_add(void); +void resize_master_sub(void); +void resize_stack_add(void); +void resize_stack_sub(void); +void resize_win_down(void); +void resize_win_left(void); +void resize_win_right(void); +void resize_win_up(void); +void run(void); +void scan_existing_windows(void); +void select_input(Window w, Mask masks); +void send_wm_take_focus(Window w); +void setup(void); +void setup_atoms(void); +void set_frame_extents(Window w); +void set_input_focus(client_t *c, Bool raise_win, Bool warp); +void set_wm_state(Window w, long state); +int snap_coordinate(int pos, int size, int screen_size, int snap_dist); +void spawn(const char *const *argv); +void swap_clients(client_t *a, client_t *b); +void tile(void); +void toggle_floating(void); +void toggle_floating_global(void); +void toggle_fullscreen(void); +void toggle_monocle(void); +void update_borders(void); +void update_client_desktop_properties(void); +void update_modifier_masks(void); +void update_net_client_list(void); +void update_struts(void); +void update_workarea(void); +void warp_cursor(client_t *c); +Bool window_has_ewmh_state(Window w, Atom state); +void window_set_ewmh_state(Window w, Atom state, Bool add); +int xerr(Display *d, XErrorEvent *ee); +void xev_case(XEvent *xev); diff --git a/src/tilite.c b/src/tilite.c new file mode 100644 index 0000000..46f3112 --- /dev/null +++ b/src/tilite.c @@ -0,0 +1,2503 @@ +#include <ctype.h> +#include <signal.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#include <X11/X.h> +#include <X11/Xatom.h> +#include <X11/Xlib.h> +#include <X11/Xproto.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> + +#include <X11/Xcursor/Xcursor.h> +#include <X11/extensions/Xinerama.h> + +#include "config.h" +#include "defs.h" + +static Atom atoms[ATOM_COUNT]; +static const char *atom_names[ATOM_COUNT] = { + [ATOM_NET_ACTIVE_WINDOW] = "_NET_ACTIVE_WINDOW", + [ATOM_NET_CURRENT_DESKTOP] = "_NET_CURRENT_DESKTOP", + [ATOM_NET_SUPPORTED] = "_NET_SUPPORTED", + [ATOM_NET_WM_STATE] = "_NET_WM_STATE", + [ATOM_NET_WM_STATE_FULLSCREEN] = "_NET_WM_STATE_FULLSCREEN", + [ATOM_WM_STATE] = "WM_STATE", + [ATOM_NET_WM_WINDOW_TYPE] = "_NET_WM_WINDOW_TYPE", + [ATOM_NET_WORKAREA] = "_NET_WORKAREA", + [ATOM_WM_DELETE_WINDOW] = "WM_DELETE_WINDOW", + [ATOM_NET_WM_STRUT] = "_NET_WM_STRUT", + [ATOM_NET_WM_STRUT_PARTIAL] = "_NET_WM_STRUT_PARTIAL", + [ATOM_NET_SUPPORTING_WM_CHECK] = "_NET_SUPPORTING_WM_CHECK", + [ATOM_NET_WM_NAME] = "_NET_WM_NAME", + [ATOM_UTF8_STRING] = "UTF8_STRING", + [ATOM_NET_WM_DESKTOP] = "_NET_WM_DESKTOP", + [ATOM_NET_CLIENT_LIST] = "_NET_CLIENT_LIST", + [ATOM_NET_FRAME_EXTENTS] = "_NET_FRAME_EXTENTS", + [ATOM_NET_NUMBER_OF_DESKTOPS] = "_NET_NUMBER_OF_DESKTOPS", + [ATOM_NET_DESKTOP_NAMES] = "_NET_DESKTOP_NAMES", + [ATOM_NET_WM_PID] = "_NET_WM_PID", + [ATOM_NET_WM_WINDOW_TYPE_DOCK] = "_NET_WM_WINDOW_TYPE_DOCK", + [ATOM_NET_WM_WINDOW_TYPE_UTILITY] = "_NET_WM_WINDOW_TYPE_UTILITY", + [ATOM_NET_WM_WINDOW_TYPE_DIALOG] = "_NET_WM_WINDOW_TYPE_DIALOG", + [ATOM_NET_WM_WINDOW_TYPE_TOOLBAR] = "_NET_WM_WINDOW_TYPE_TOOLBAR", + [ATOM_NET_WM_WINDOW_TYPE_SPLASH] = "_NET_WM_WINDOW_TYPE_SPLASH", + [ATOM_NET_WM_WINDOW_TYPE_POPUP_MENU] = "_NET_WM_WINDOW_TYPE_POPUP_MENU", + [ATOM_NET_WM_WINDOW_TYPE_MENU] = "_NET_WM_WINDOW_TYPE_MENU", + [ATOM_NET_WM_WINDOW_TYPE_DROPDOWN_MENU] = + "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", + [ATOM_NET_WM_WINDOW_TYPE_TOOLTIP] = "_NET_WM_WINDOW_TYPE_TOOLTIP", + [ATOM_NET_WM_WINDOW_TYPE_NOTIFICATION] = "_NET_WM_WINDOW_TYPE_NOTIFICATION", + [ATOM_NET_WM_STATE_MODAL] = "_NET_WM_STATE_MODAL", + [ATOM_WM_PROTOCOLS] = "WM_PROTOCOLS", +}; + +Cursor cursor_normal; +Cursor cursor_move; +Cursor cursor_resize; + +client_t *workspaces[NUM_WORKSPACES] = {NULL}; +config_t user_config; +DragMode drag_mode = DRAG_NONE; +client_t *drag_client = NULL; +client_t *swap_target = NULL; +client_t *focused = NULL; +client_t *ws_focused[NUM_WORKSPACES] = {NULL}; +event_t evtable[LASTEvent]; +Display *dpy; +Window root; +Window wm_check_win; +int current_ws = 0; +long last_motion_time = 0; +Bool global_floating = False; +Bool in_ws_switch = False; +Bool running = False; +Bool monocle = False; + +Mask numlock_mask = 0; +Mask mode_switch_mask = 0; + +int scr_width; +int scr_height; +int open_windows = 0; +int drag_start_x, drag_start_y; +int drag_orig_x, drag_orig_y, drag_orig_w, drag_orig_h; + +int reserve_left = 0; +int reserve_right = 0; +int reserve_top = 0; +int reserve_bottom = 0; + +static char **split_cmd(const char *cmd, int *out_argc) { + enum { NORMAL, IN_QUOTE } state = NORMAL; + size_t cap = 8, argc = 0, toklen = 0; + char *token = malloc(strlen(cmd) + 1); + char **argv = malloc(cap * sizeof *argv); + + if (!token || !argv) + goto err; + + for (const char *p = cmd; *p; p++) { + if (state == NORMAL && isspace((unsigned char)*p)) { + if (toklen) { + token[toklen] = '\0'; + if (argc + 1 >= cap) { + cap *= 2; + char **tmp = realloc(argv, cap * sizeof *argv); + if (!tmp) + goto err; + + argv = tmp; + } + argv[argc++] = strdup(token); + toklen = 0; + } + } else if (*p == '"') + state = (state == NORMAL) ? IN_QUOTE : NORMAL; + else if (*p == '\'') + state = (state == NORMAL) ? IN_QUOTE : NORMAL; + else + token[toklen++] = *p; + } + + if (toklen) { + token[toklen] = '\0'; + argv[argc++] = strdup(token); + } + argv[argc] = NULL; + *out_argc = argc; + free(token); + return argv; + +err: + free(token); + if (argv) { + for (size_t i = 0; i < argc; i++) + free(argv[i]); + free(argv); + } + return NULL; +} + +const char **build_argv(const char *cmd) { + int argc = 0; + char **tmp = split_cmd(cmd, &argc); + if (!tmp) + return NULL; + + return (const char **)tmp; +} + +static void load_config(void) { + /* scalar values */ + user_config.modkey = MODKEY; + user_config.gaps = CFG_GAPS; + user_config.border_width = CFG_BORDER_WIDTH; + user_config.master_width = CFG_MASTER_WIDTH; + user_config.resize_master_amt = CFG_RESIZE_MASTER_AMT; + user_config.resize_stack_amt = CFG_RESIZE_STACK_AMT; + user_config.move_window_amt = CFG_MOVE_WINDOW_AMT; + user_config.resize_window_amt = CFG_RESIZE_WINDOW_AMT; + user_config.snap_distance = CFG_SNAP_DISTANCE; + user_config.motion_throttle = CFG_MOTION_THROTTLE; + user_config.new_win_focus = CFG_NEW_WIN_FOCUS; + user_config.warp_cursor = CFG_WARP_CURSOR; + user_config.floating_on_top = CFG_FLOATING_ON_TOP; + user_config.new_win_master = CFG_NEW_WIN_MASTER; + + /* colours — parse_col needs dpy to be open, so call after XOpenDisplay */ + user_config.border_foc_col = parse_col(CFG_FOCUSED_BORDER_COL); + user_config.border_ufoc_col = parse_col(CFG_UNFOCUSED_BORDER_COL); + user_config.border_swap_col = parse_col(CFG_SWAP_BORDER_COL); + + /* keybinds */ + binding_t binds[] = {CFG_BINDS}; + user_config.n_binds = (int)(sizeof(binds) / sizeof(binds[0])); + for (int i = 0; i < user_config.n_binds; i++) + user_config.binds[i] = binds[i]; +} + +client_t *add_client(Window w, int ws) { + client_t *c = malloc(sizeof(client_t)); + if (!c) { + fprintf(stderr, "tilite: could not alloc memory for client\n"); + return NULL; + } + + c->win = w; + c->next = NULL; + c->ws = ws; + c->pid = get_pid(w); + + if (!workspaces[ws]) { + workspaces[ws] = c; + } else { + if (user_config.new_win_master) { + c->next = workspaces[ws]; + workspaces[ws] = c; + } else { + client_t *tail = workspaces[ws]; + while (tail->next) + tail = tail->next; + tail->next = c; + } + } + open_windows++; + + /* subscribing to certain events */ + Mask window_masks = EnterWindowMask | LeaveWindowMask | FocusChangeMask | + PropertyChangeMask | StructureNotifyMask | + ButtonPressMask | ButtonReleaseMask | PointerMotionMask; + select_input(w, window_masks); + grab_button(Button1, None, w, False, ButtonPressMask); + grab_button(Button1, user_config.modkey, w, False, ButtonPressMask); + grab_button(Button1, user_config.modkey | ShiftMask, w, False, + ButtonPressMask); + grab_button(Button3, user_config.modkey, w, False, ButtonPressMask); + + /* allow for more graceful exitting */ + Atom protos[] = {atoms[ATOM_WM_DELETE_WINDOW]}; + XSetWMProtocols(dpy, w, protos, 1); + + XWindowAttributes wa; + XGetWindowAttributes(dpy, w, &wa); + c->x = wa.x; + c->y = wa.y; + c->w = wa.width; + c->h = wa.height; + + /* set client defaults */ + c->fixed = False; + c->floating = False; + c->fullscreen = False; + c->mapped = True; + c->custom_stack_height = 0; + + if (global_floating) + c->floating = True; + + /* remember first created client per workspace as a fallback */ + if (!ws_focused[ws]) + ws_focused[ws] = c; + + if (ws == current_ws && !focused) { + focused = c; + } + + /* associate client with workspace n */ + long desktop = ws; + XChangeProperty(dpy, w, atoms[ATOM_NET_WM_DESKTOP], XA_CARDINAL, 32, + PropModeReplace, (unsigned char *)&desktop, 1); + XRaiseWindow(dpy, w); + return c; +} + +void apply_fullscreen(client_t *c, Bool on) { + if (!c || !c->mapped || c->fullscreen == on) + return; + + if (on) { + XWindowAttributes win_attr; + if (!XGetWindowAttributes(dpy, c->win, &win_attr)) + return; + + c->orig_x = win_attr.x; + c->orig_y = win_attr.y; + c->orig_w = win_attr.width; + c->orig_h = win_attr.height; + + c->fullscreen = True; + + XSetWindowBorderWidth(dpy, c->win, 0); + XMoveResizeWindow(dpy, c->win, 0, 0, scr_width, scr_height); + + c->x = 0; + c->y = 0; + c->w = scr_width; + c->h = scr_height; + + XRaiseWindow(dpy, c->win); + window_set_ewmh_state(c->win, atoms[ATOM_NET_WM_STATE_FULLSCREEN], + True); + } else { + c->fullscreen = False; + + /* restore win attributes */ + XMoveResizeWindow(dpy, c->win, c->orig_x, c->orig_y, c->orig_w, + c->orig_h); + XSetWindowBorderWidth(dpy, c->win, user_config.border_width); + window_set_ewmh_state(c->win, atoms[ATOM_NET_WM_STATE_FULLSCREEN], + False); + + c->x = c->orig_x; + c->y = c->orig_y; + c->w = c->orig_w; + c->h = c->orig_h; + + tile(); + update_borders(); + } +} + +void change_workspace(int ws) { + if (ws >= NUM_WORKSPACES || ws == current_ws) + return; + + /* remember last focus for workspace we are leaving */ + ws_focused[current_ws] = focused; + + in_ws_switch = True; + XGrabServer(dpy); /* freeze rendering for tearless switching */ + + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (c->mapped) { + XUnmapWindow(dpy, c->win); + } + } + + current_ws = ws; + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (c->mapped) { + XMapWindow(dpy, c->win); + } + } + + tile(); + + /* restore last focused client for this workspace */ + focused = ws_focused[current_ws]; + + if (focused) { + client_t *found = NULL; + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (c == focused && c->mapped) { + found = c; + break; + } + } + if (!found) + focused = NULL; + } + + if (!focused && workspaces[current_ws]) { + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (!c->mapped) + continue; + if (!focused) + focused = c; + } + } + + set_input_focus(focused, False, True); + + long current_desktop = current_ws; + XChangeProperty(dpy, root, atoms[ATOM_NET_CURRENT_DESKTOP], XA_CARDINAL, 32, + PropModeReplace, (unsigned char *)¤t_desktop, 1); + update_client_desktop_properties(); + + XUngrabServer(dpy); + XSync(dpy, False); + in_ws_switch = False; +} + +int check_parent(pid_t p, pid_t c) { + while (p != c && c != 0) /* walk proc tree until parent found */ + c = get_parent_process(c); + + return (int)c; +} + +int clean_mask(int mask) { + return mask & ~(LockMask | numlock_mask | mode_switch_mask); +} + +void close_focused(void) { + if (!focused) + return; + + Atom *protocols; + int n_protocols; + /* get number of protocols a window possesses and check if any == + * WM_DELETE_WINDOW (supports it) */ + if (XGetWMProtocols(dpy, focused->win, &protocols, &n_protocols) && + protocols) { + for (int i = 0; i < n_protocols; i++) { + if (protocols[i] == atoms[ATOM_WM_DELETE_WINDOW]) { + XEvent ev = { + .xclient = {.type = ClientMessage, + .window = focused->win, + .message_type = atoms[ATOM_WM_PROTOCOLS], + .format = 32}}; + + ev.xclient.data.l[0] = atoms[ATOM_WM_DELETE_WINDOW]; + ev.xclient.data.l[1] = CurrentTime; + XSendEvent(dpy, focused->win, False, NoEventMask, &ev); + XFree(protocols); + return; + } + } + XUnmapWindow(dpy, focused->win); + XFree(protocols); + } + XUnmapWindow(dpy, focused->win); + XKillClient(dpy, focused->win); +} + +client_t *find_client(Window w) { + for (int ws = 0; ws < NUM_WORKSPACES; ws++) + for (client_t *c = workspaces[ws]; c; c = c->next) + if (c->win == w) + return c; + + return NULL; +} + +Window find_toplevel(Window w) { + if (!w || w == None) + return root; + + Window root_win = None; + Window parent; + Window *kids; + unsigned n_kids; + + while (True) { + if (w == root_win) + break; + if (XQueryTree(dpy, w, &root_win, &parent, &kids, &n_kids) == 0) + break; + if (kids) + XFree(kids); + if (parent == root_win || parent == None) + break; + w = parent; + } + return w; +} + +void focus_next(void) { + if (!workspaces[current_ws]) + return; + + client_t *start = focused ? focused : workspaces[current_ws]; + client_t *c = start; + + /* loop until we find a mapped client or return to start */ + do + c = c->next ? c->next : workspaces[current_ws]; + while (!c->mapped && c != start); + + /* if we return to start: */ + if (!c->mapped) + return; + + focused = c; + set_input_focus(focused, True, True); +} + +void focus_prev(void) { + if (!workspaces[current_ws]) + return; + + client_t *start = focused ? focused : workspaces[current_ws]; + client_t *c = start; + + /* loop until we find a mapped client or return to starting point */ + do { + client_t *p = workspaces[current_ws]; + client_t *prev = NULL; + while (p && p != c) { + prev = p; + p = p->next; + } + + if (prev) { + c = prev; + } else { + /* wrap to tail */ + p = workspaces[current_ws]; + while (p->next) + p = p->next; + c = p; + } + } while (!c->mapped && c != start); + + /* this stops invisible windows being detected or focused */ + if (!c->mapped) + return; + + focused = c; + set_input_focus(focused, True, True); +} + +pid_t get_parent_process(pid_t c) { + pid_t v = -1; + FILE *f; + char buf[256]; + + snprintf(buf, sizeof(buf), "/proc/%u/stat", (unsigned)c); + if (!(f = fopen(buf, "r"))) + return 0; + + int no_error = fscanf(f, "%*u %*s %*c %d", &v); + (void)no_error; + fclose(f); + return (pid_t)v; +} + +pid_t get_pid(Window w) { + pid_t pid = 0; + Atom actual_type; + int actual_format; + unsigned long n_items, bytes_after; + unsigned char *prop = NULL; + + if (XGetWindowProperty(dpy, w, atoms[ATOM_NET_WM_PID], 0, 1, False, + XA_CARDINAL, &actual_type, &actual_format, &n_items, + &bytes_after, &prop) == Success && + prop) { + if (actual_format == 32 && n_items == 1) + pid = *(pid_t *)prop; + XFree(prop); + } + return pid; +} + +int get_workspace_for_window(Window w) { + XClassHint ch = {0}; + if (!XGetClassHint(dpy, w, &ch)) + return current_ws; + + XFree(ch.res_class); + XFree(ch.res_name); + + return current_ws; /* default */ +} + +void grab_button(Mask button, Mask mod, Window w, Bool owner_events, + Mask masks) { + if (w == root) /* grabbing for wm */ + XGrabButton(dpy, button, mod, w, owner_events, masks, GrabModeAsync, + GrabModeAsync, None, None); + else /* grabbing for windows */ + XGrabButton(dpy, button, mod, w, owner_events, masks, GrabModeSync, + GrabModeAsync, None, None); +} + +void grab_keys(void) { + Mask guards[] = {0, + LockMask, + numlock_mask, + LockMask | numlock_mask, + mode_switch_mask, + LockMask | mode_switch_mask, + numlock_mask | mode_switch_mask, + LockMask | numlock_mask | mode_switch_mask}; + XUngrabKey(dpy, AnyKey, AnyModifier, root); + + for (int i = 0; i < user_config.n_binds; i++) { + binding_t *bind = &user_config.binds[i]; + + if ((bind->type == TYPE_WS_CHANGE && + bind->mods != user_config.modkey) || + (bind->type == TYPE_WS_MOVE && + bind->mods != (user_config.modkey | ShiftMask))) { + continue; + } + + bind->keycode = XKeysymToKeycode(dpy, bind->keysym); + if (!bind->keycode) + continue; + + for (size_t guard = 0; guard < sizeof(guards) / sizeof(guards[0]); + guard++) { + XGrabKey(dpy, bind->keycode, bind->mods | guards[guard], root, True, + GrabModeAsync, GrabModeAsync); + } + } +} + +void hdl_button(XEvent *xev) { + XButtonEvent *xbutton = &xev->xbutton; + Window w = + (xbutton->subwindow != None) ? xbutton->subwindow : xbutton->window; + w = find_toplevel(w); + + Mask left_click = Button1; + Mask right_click = Button3; + + XAllowEvents(dpy, ReplayPointer, xbutton->time); + if (!w) + return; + + client_t *head = workspaces[current_ws]; + for (client_t *c = head; c; c = c->next) { + if (c->win != w) + continue; + + Bool is_swap_mode = (xbutton->state & user_config.modkey) && + (xbutton->state & ShiftMask) && + xbutton->button == left_click && !c->floating; + if (is_swap_mode) { + drag_client = c; + drag_start_x = xbutton->x_root; + drag_start_y = xbutton->y_root; + drag_orig_x = c->x; + drag_orig_y = c->y; + drag_orig_w = c->w; + drag_orig_h = c->h; + drag_mode = DRAG_SWAP; + XGrabPointer(dpy, root, True, ButtonReleaseMask | PointerMotionMask, + GrabModeAsync, GrabModeAsync, None, cursor_move, + CurrentTime); + focused = c; + set_input_focus(focused, False, False); + XSetWindowBorder(dpy, c->win, user_config.border_swap_col); + return; + } + + Bool is_move_resize = + (xbutton->state & user_config.modkey) && + (xbutton->button == left_click || xbutton->button == right_click) && + !c->floating; + if (is_move_resize) { + focused = c; + toggle_floating(); + } + + Bool is_single_click = !(xbutton->state & user_config.modkey) && + xbutton->button == left_click; + if (is_single_click) { + focused = c; + set_input_focus(focused, True, False); + return; + } + + if (!c->floating) + return; + + if (c->fixed && xbutton->button == right_click) + return; + + Cursor cursor = + (xbutton->button == left_click) ? cursor_move : cursor_resize; + XGrabPointer(dpy, root, True, ButtonReleaseMask | PointerMotionMask, + GrabModeAsync, GrabModeAsync, None, cursor, CurrentTime); + + drag_client = c; + drag_start_x = xbutton->x_root; + drag_start_y = xbutton->y_root; + drag_orig_x = c->x; + drag_orig_y = c->y; + drag_orig_w = c->w; + drag_orig_h = c->h; + drag_mode = (xbutton->button == left_click) ? DRAG_MOVE : DRAG_RESIZE; + focused = c; + + set_input_focus(focused, True, False); + return; + } +} + +void hdl_button_release(XEvent *xev) { + (void)xev; + + if (drag_mode == DRAG_SWAP) { + if (swap_target) { + XSetWindowBorder(dpy, swap_target->win, + (swap_target == focused + ? user_config.border_foc_col + : user_config.border_ufoc_col)); + swap_clients(drag_client, swap_target); + } + tile(); + update_borders(); + } + + XUngrabPointer(dpy, CurrentTime); + + drag_mode = DRAG_NONE; + drag_client = NULL; + swap_target = NULL; +} + +void hdl_client_msg(XEvent *xev) { + if (xev->xclient.message_type == atoms[ATOM_NET_CURRENT_DESKTOP]) { + int ws = (int)xev->xclient.data.l[0]; + change_workspace(ws); + return; + } + + if (xev->xclient.message_type == atoms[ATOM_NET_WM_STATE]) { + XClientMessageEvent *client_msg_ev = &xev->xclient; + Window w = client_msg_ev->window; + client_t *c = find_client(find_toplevel(w)); + if (!c) + return; + + /* 0=remove, 1=add, 2=toggle */ + long action = client_msg_ev->data.l[0]; + Atom a1 = (Atom)client_msg_ev->data.l[1]; + Atom a2 = (Atom)client_msg_ev->data.l[2]; + + Atom state_atoms[2] = {a1, a2}; + for (int i = 0; i < 2; i++) { + if (state_atoms[i] == None) + continue; + + if (state_atoms[i] == atoms[ATOM_NET_WM_STATE_FULLSCREEN]) { + Bool want = c->fullscreen; + if (action == 0) + want = False; + else if (action == 1) + want = True; + else if (action == 2) + want = !want; + + apply_fullscreen(c, want); + + if (want) + XRaiseWindow(dpy, c->win); + } + } + return; + } +} + +void hdl_config_ntf(XEvent *xev) { + if (xev->xconfigure.window == root) { + tile(); + update_borders(); + } +} + +void hdl_config_req(XEvent *xev) { + XConfigureRequestEvent *config_ev = &xev->xconfigurerequest; + client_t *c = NULL; + + for (int i = 0; i < NUM_WORKSPACES && !c; i++) + for (c = workspaces[i]; c; c = c->next) + if (c->win == config_ev->window) + break; + + if (!c || c->floating || c->fullscreen) { + /* allow client to configure itself */ + XWindowChanges wc = {.x = config_ev->x, + .y = config_ev->y, + .width = config_ev->width, + .height = config_ev->height, + .border_width = config_ev->border_width, + .sibling = config_ev->above, + .stack_mode = config_ev->detail}; + XConfigureWindow(dpy, config_ev->window, config_ev->value_mask, &wc); + return; + } +} + +void hdl_dummy(XEvent *xev) { (void)xev; } + +void hdl_destroy_ntf(XEvent *xev) { + Window w = xev->xdestroywindow.window; + + for (int i = 0; i < NUM_WORKSPACES; i++) { + client_t *prev = NULL; + client_t *c = workspaces[i]; + + while (c && c->win != w) { + prev = c; + c = c->next; + } + + if (!c) + continue; + + for (int ws = 0; ws < NUM_WORKSPACES; ws++) + if (ws_focused[ws] == c) + ws_focused[ws] = NULL; + + if (focused == c) + focused = NULL; + + /* unlink from workspace list */ + if (!prev) + workspaces[i] = c->next; + else + prev->next = c->next; + + free(c); + update_net_client_list(); + open_windows--; + + if (i == current_ws) { + tile(); + update_borders(); + + /* prefer previous window else next */ + client_t *foc_new = NULL; + if (prev && prev->mapped) + foc_new = prev; + else { + for (client_t *p = workspaces[i]; p; p = p->next) { + if (!p->mapped) + continue; + foc_new = p; + break; + } + } + + if (foc_new) + set_input_focus(foc_new, True, True); + else + set_input_focus(NULL, False, False); + } + + return; + } +} + +void hdl_keypress(XEvent *xev) { + KeyCode code = xev->xkey.keycode; + int mods = clean_mask(xev->xkey.state); + + for (int i = 0; i < user_config.n_binds; i++) { + binding_t *bind = &user_config.binds[i]; + if (bind->keycode == code && clean_mask(bind->mods) == mods) { + switch (bind->type) { + case TYPE_CMD: + spawn(bind->action.cmd); + break; + case TYPE_FUNC: + if (bind->action.fn) + bind->action.fn(); + break; + case TYPE_WS_CHANGE: + change_workspace(bind->action.ws); + update_net_client_list(); + break; + case TYPE_WS_MOVE: + move_to_workspace(bind->action.ws); + update_net_client_list(); + break; + } + return; + } + } +} + +void hdl_mapping_ntf(XEvent *xev) { + XRefreshKeyboardMapping(&xev->xmapping); + update_modifier_masks(); + grab_keys(); +} + +void hdl_map_req(XEvent *xev) { + Window w = xev->xmaprequest.window; + XWindowAttributes win_attr; + + if (!XGetWindowAttributes(dpy, w, &win_attr)) + return; + + /* skips invisible windows */ + if (win_attr.override_redirect || win_attr.width <= 0 || + win_attr.height <= 0) { + XMapWindow(dpy, w); + return; + } + + /* check if this window is already managed on any workspace */ + client_t *c = find_client(w); + if (c) { + if (c->ws == current_ws) { + if (!c->mapped) { + XMapWindow(dpy, w); + c->mapped = True; + } + if (user_config.new_win_focus) { + focused = c; + set_input_focus(c, True, True); + return; /* set_input_focus already calls update_borders */ + } + update_borders(); + } + return; + } + + Atom type; + int format; + unsigned long n_items, after; + Atom *types = NULL; + Bool should_float = False; + + if (XGetWindowProperty(dpy, w, atoms[ATOM_NET_WM_WINDOW_TYPE], 0, 4, False, + XA_ATOM, &type, &format, &n_items, &after, + (unsigned char **)&types) == Success && + types) { + + for (unsigned long i = 0; i < n_items; i++) { + if (types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_DOCK]) { + XFree(types); + XMapWindow(dpy, w); + return; + } + + if (types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_UTILITY] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_DIALOG] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_TOOLBAR] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_SPLASH] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_POPUP_MENU] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_DROPDOWN_MENU] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_MENU] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_TOOLTIP] || + types[i] == atoms[ATOM_NET_WM_WINDOW_TYPE_NOTIFICATION]) { + should_float = True; + break; + } + } + XFree(types); + } + + if (open_windows == MAX_CLIENTS) { + fprintf(stderr, "tilite: max clients reached, ignoring map request\n"); + return; + } + + int target_ws = get_workspace_for_window(w); + c = add_client(w, target_ws); + if (!c) + return; + set_wm_state(w, NormalState); + + Window transient; + if (!should_float && XGetTransientForHint(dpy, w, &transient)) + should_float = True; + + XSizeHints size_hints; + long supplied_ret; + + if (!should_float && + XGetWMNormalHints(dpy, w, &size_hints, &supplied_ret) && + (size_hints.flags & PMinSize) && (size_hints.flags & PMaxSize) && + size_hints.min_width == size_hints.max_width && + size_hints.min_height == size_hints.max_height) { + + should_float = True; + c->fixed = True; + } + + if (should_float || global_floating) + c->floating = True; + + /* center floating windows & set border */ + if (c->floating && !c->fullscreen) { + int w_ = MAX(c->w, 64), h_ = MAX(c->h, 64); + int x = (scr_width - w_) / 2, y = (scr_height - h_) / 2; + c->x = x; + c->y = y; + c->w = w_; + c->h = h_; + XMoveResizeWindow(dpy, w, x, y, w_, h_); + XSetWindowBorderWidth(dpy, w, user_config.border_width); + } + + update_net_client_list(); + if (target_ws != current_ws) + return; + + /* map & borders */ + if (!global_floating && !c->floating) + tile(); + else if (c->floating) + XRaiseWindow(dpy, w); + + if (window_has_ewmh_state(w, atoms[ATOM_NET_WM_STATE_FULLSCREEN])) { + c->fullscreen = True; + c->floating = False; + } + + XMapWindow(dpy, w); + c->mapped = True; + if (c->fullscreen) + apply_fullscreen(c, True); + set_frame_extents(w); + + if (user_config.new_win_focus) { + focused = c; + set_input_focus(focused, True, True); + return; + } + update_borders(); +} + +void hdl_motion(XEvent *xev) { + XMotionEvent *motion_ev = &xev->xmotion; + + if ((drag_mode == DRAG_NONE || !drag_client) || + (motion_ev->time - last_motion_time <= + (1000 / (Time)user_config.motion_throttle))) + return; + last_motion_time = motion_ev->time; + + if (drag_mode == DRAG_SWAP) { + Window root_ret, child; + int rx, ry, wx, wy; + unsigned int mask; + XQueryPointer(dpy, root, &root_ret, &child, &rx, &ry, &wx, &wy, &mask); + + client_t *new_target = NULL; + + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (c == drag_client || c->floating) + continue; + if (c->win == child) { + new_target = c; + break; + } + Window root_ret2, parent; + Window *children; + unsigned int n_children; + if (XQueryTree(dpy, child, &root_ret2, &parent, &children, + &n_children)) { + if (children) + XFree(children); + if (parent == c->win) { + new_target = c; + break; + } + } + } + + if (new_target != swap_target) { + if (swap_target) { + XSetWindowBorder(dpy, swap_target->win, + (swap_target == focused + ? user_config.border_foc_col + : user_config.border_ufoc_col)); + } + if (new_target) + XSetWindowBorder(dpy, new_target->win, + user_config.border_swap_col); + } + + swap_target = new_target; + return; + } else if (drag_mode == DRAG_MOVE) { + int dx = motion_ev->x_root - drag_start_x; + int dy = motion_ev->y_root - drag_start_y; + int nx = drag_orig_x + dx; + int ny = drag_orig_y + dy; + + int outer_w = drag_client->w + 2 * user_config.border_width; + int outer_h = drag_client->h + 2 * user_config.border_width; + + int rel_x = nx; + int rel_y = ny; + + rel_x = snap_coordinate(rel_x, outer_w, scr_width, + user_config.snap_distance); + rel_y = snap_coordinate(rel_y, outer_h, scr_height, + user_config.snap_distance); + + nx = rel_x; + ny = rel_y; + + if (!drag_client->floating && + (UDIST(nx, drag_client->x) > user_config.snap_distance || + UDIST(ny, drag_client->y) > user_config.snap_distance)) { + toggle_floating(); + } + + XMoveWindow(dpy, drag_client->win, nx, ny); + drag_client->x = nx; + drag_client->y = ny; + } else if (drag_mode == DRAG_RESIZE) { + int dx = motion_ev->x_root - drag_start_x; + int dy = motion_ev->y_root - drag_start_y; + int nw = drag_orig_w + dx; + int nh = drag_orig_h + dy; + + int max_w = (scr_width - drag_client->x); + int max_h = (scr_height - drag_client->y); + + drag_client->w = CLAMP(nw, MIN_WINDOW_SIZE, max_w); + drag_client->h = CLAMP(nh, MIN_WINDOW_SIZE, max_h); + + XResizeWindow(dpy, drag_client->win, drag_client->w, drag_client->h); + } +} + +void hdl_property_ntf(XEvent *xev) { + XPropertyEvent *property_ev = &xev->xproperty; + + if (property_ev->window == root) { + if (property_ev->atom == atoms[ATOM_NET_CURRENT_DESKTOP]) { + long *val = NULL; + Atom actual; + int fmt; + unsigned long n; + unsigned long after; + if (XGetWindowProperty(dpy, root, atoms[ATOM_NET_CURRENT_DESKTOP], + 0, 1, False, XA_CARDINAL, &actual, &fmt, &n, + &after, (unsigned char **)&val) == Success && + val) { + change_workspace((int)val[0]); + XFree(val); + } + } else if (property_ev->atom == atoms[ATOM_NET_WM_STRUT_PARTIAL]) { + update_struts(); + tile(); + update_borders(); + } + } + + /* client window properties */ + if (property_ev->atom == atoms[ATOM_NET_WM_STATE]) { + client_t *c = find_client(find_toplevel(property_ev->window)); + if (!c) + return; + + Bool want = + window_has_ewmh_state(c->win, atoms[ATOM_NET_WM_STATE_FULLSCREEN]); + if (want != c->fullscreen) + apply_fullscreen(c, want); + } +} + +void hdl_unmap_ntf(XEvent *xev) { + if (!in_ws_switch) { + Window w = xev->xunmap.window; + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (c->win == w) { + c->mapped = False; + break; + } + } + } + + update_net_client_list(); + tile(); + update_borders(); +} + +void inc_gaps(void) { + user_config.gaps++; + tile(); + update_borders(); +} + +void init_defaults(void) { + user_config.modkey = Mod4Mask; + user_config.gaps = 10; + user_config.border_width = 1; + user_config.border_foc_col = parse_col("#c0cbff"); + user_config.border_ufoc_col = parse_col("#555555"); + user_config.border_swap_col = parse_col("#fff4c0"); + user_config.move_window_amt = 10; + user_config.resize_window_amt = 10; + + user_config.master_width = 50 / 100.0f; + + user_config.motion_throttle = 60; + user_config.resize_master_amt = 5; + user_config.resize_stack_amt = 20; + user_config.snap_distance = 5; + user_config.n_binds = 0; + user_config.new_win_focus = True; + user_config.warp_cursor = True; + user_config.new_win_master = False; + user_config.floating_on_top = True; +} + +Bool is_child_proc(pid_t parent_pid, pid_t child_pid) { + if (parent_pid <= 0 || child_pid <= 0) + return False; + + char path[PATH_MAX]; + FILE *f; + pid_t current_pid = child_pid; + int max_iterations = 20; + + while (current_pid > 1 && max_iterations-- > 0) { + snprintf(path, sizeof(path), "/proc/%d/stat", current_pid); + f = fopen(path, "r"); + if (!f) { + fprintf(stderr, "tilite: could not open %s\n", path); + return False; + } + + int ppid = 0; + if (fscanf(f, "%*d %*s %*c %d", &ppid) != 1) { + fprintf(stderr, "tilite: failed to read ppid from %s\n", path); + fclose(f); + return False; + } + fclose(f); + + if (ppid == parent_pid) + return True; + + if (ppid <= 1) { + fprintf(stderr, + "tilite: reached init/kernel, no relationship found\n"); + break; + } + current_pid = ppid; + } + return False; +} + +void move_master_next(void) { + if (!workspaces[current_ws] || !workspaces[current_ws]->next) + return; + + client_t *first = workspaces[current_ws]; + client_t *old_focused = focused; + + workspaces[current_ws] = first->next; + first->next = NULL; + + client_t *tail = workspaces[current_ws]; + while (tail->next) + tail = tail->next; + + tail->next = first; + + tile(); + + if (user_config.warp_cursor && old_focused) + warp_cursor(old_focused); + + if (old_focused) + send_wm_take_focus(old_focused->win); + + update_borders(); +} + +void move_master_prev(void) { + if (!workspaces[current_ws] || !workspaces[current_ws]->next) + return; + + client_t *prev = NULL; + client_t *cur = workspaces[current_ws]; + client_t *old_focused = focused; + + while (cur->next) { + prev = cur; + cur = cur->next; + } + + if (prev) + prev->next = NULL; + + cur->next = workspaces[current_ws]; + workspaces[current_ws] = cur; + + tile(); + if (user_config.warp_cursor && old_focused) + warp_cursor(old_focused); + if (old_focused) + send_wm_take_focus(old_focused->win); + + update_borders(); +} + +void move_to_workspace(int ws) { + if (!focused || ws >= NUM_WORKSPACES || ws == current_ws) + return; + + client_t *moved = focused; + int from_ws = current_ws; + + XUnmapWindow(dpy, moved->win); + + /* remove from current list */ + client_t **pp = &workspaces[from_ws]; + while (*pp && *pp != moved) + pp = &(*pp)->next; + + if (*pp) + *pp = moved->next; + + /* push to target list */ + moved->next = workspaces[ws]; + workspaces[ws] = moved; + moved->ws = ws; + long desktop = ws; + XChangeProperty(dpy, moved->win, atoms[ATOM_NET_WM_DESKTOP], XA_CARDINAL, + 32, PropModeReplace, (unsigned char *)&desktop, 1); + + /* remember it as last-focused for the target workspace */ + ws_focused[ws] = moved; + + /* retile current workspace and pick a new focus there */ + tile(); + focused = workspaces[from_ws]; + if (focused) + set_input_focus(focused, False, False); + else + set_input_focus(NULL, False, False); +} + +void move_win_down(void) { + if (!focused || !focused->floating) + return; + + focused->y += user_config.move_window_amt; + XMoveWindow(dpy, focused->win, focused->x, focused->y); +} + +void move_win_left(void) { + if (!focused || !focused->floating) + return; + + focused->x -= user_config.move_window_amt; + XMoveWindow(dpy, focused->win, focused->x, focused->y); +} + +void move_win_right(void) { + if (!focused || !focused->floating) + return; + focused->x += user_config.move_window_amt; + XMoveWindow(dpy, focused->win, focused->x, focused->y); +} + +void move_win_up(void) { + if (!focused || !focused->floating) + return; + + focused->y -= user_config.move_window_amt; + XMoveWindow(dpy, focused->win, focused->x, focused->y); +} + +void other_wm(void) { + XSetErrorHandler(other_wm_err); + XChangeWindowAttributes( + dpy, root, CWEventMask, + &(XSetWindowAttributes){.event_mask = SubstructureRedirectMask}); + XSync(dpy, False); + XSetErrorHandler(xerr); + XChangeWindowAttributes(dpy, root, CWEventMask, + &(XSetWindowAttributes){.event_mask = 0}); + XSync(dpy, False); +} + +int other_wm_err(Display *d, XErrorEvent *ee) { + fprintf(stderr, + "can't start because another window manager is already running"); + exit(EXIT_FAILURE); + return 0; + (void)d; + (void)ee; +} + +long parse_col(const char *hex) { + XColor col; + Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy)); + + if (!XParseColor(dpy, cmap, hex, &col)) { + fprintf(stderr, "tilite: cannot parse color %s\n", hex); + return WhitePixel(dpy, DefaultScreen(dpy)); + } + + if (!XAllocColor(dpy, cmap, &col)) { + fprintf(stderr, "tilite: cannot allocate color %s\n", hex); + return WhitePixel(dpy, DefaultScreen(dpy)); + } + + /* possibly unsafe BUT i dont think it can cause any problems. + * used to make sure borders are opaque with compositor like picom */ + return ((long)col.pixel) | (0xffL << 24); +} + +void quit(void) { + /* Kill all clients on exit... + + for (int ws = 0; ws < NUM_WORKSPACES; ws++) { + for (Client *c = workspaces[ws]; c; c = c->next) { + XUnmapWindow(dpy, c->win); + XKillClient(dpy, c->win); + } + } + */ + + XSync(dpy, False); + XFreeCursor(dpy, cursor_move); + XFreeCursor(dpy, cursor_normal); + XFreeCursor(dpy, cursor_resize); + XCloseDisplay(dpy); + puts("quitting..."); + running = False; +} + +void resize_master_add(void) { + float *mw = &user_config.master_width; + + if (*mw < MF_MAX - 0.001f) + *mw += ((float)user_config.resize_master_amt / 100); + + tile(); + update_borders(); +} + +void resize_master_sub(void) { + float *mw = &user_config.master_width; + + if (*mw > MF_MIN + 0.001f) + *mw -= ((float)user_config.resize_master_amt / 100); + + tile(); + update_borders(); +} + +void resize_stack_add(void) { + if (!focused || focused->floating || focused == workspaces[current_ws]) + return; + + int bw2 = 2 * user_config.border_width; + int raw_cur = (focused->custom_stack_height > 0) + ? focused->custom_stack_height + : (focused->h + bw2); + + int raw_new = raw_cur + user_config.resize_stack_amt; + + /* Calculate maximum allowed height to prevent extending off-screen */ + int gaps = user_config.gaps; + int tile_height = MAX(1, scr_height - 2 * gaps); + + /* Count stack windows (excluding master) */ + int n_stack = 0; + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (c->mapped && !c->floating && !c->fullscreen && + c != workspaces[current_ws]) + n_stack++; + } + + /* Maximum height: tile_height minus space for other stack windows + * (min_height + gap each) */ + int min_stack_height = bw2 + 1; + int other_stack_space = + (n_stack > 1) ? (n_stack - 1) * (min_stack_height + gaps) : 0; + int max_raw = tile_height - other_stack_space; + + if (raw_new > max_raw) + raw_new = max_raw; + + focused->custom_stack_height = raw_new; + tile(); +} + +void resize_stack_sub(void) { + if (!focused || focused->floating || focused == workspaces[current_ws]) + return; + + int bw2 = 2 * user_config.border_width; + int raw_cur = (focused->custom_stack_height > 0) + ? focused->custom_stack_height + : (focused->h + bw2); + + int raw_new = raw_cur - user_config.resize_stack_amt; + int min_raw = bw2 + 1; + + if (raw_new < min_raw) + raw_new = min_raw; + + focused->custom_stack_height = raw_new; + tile(); +} + +void resize_win_down(void) { + if (!focused || !focused->floating) + return; + + int new_h = focused->h + user_config.resize_window_amt; + int max_h = scr_height - focused->y; + focused->h = CLAMP(new_h, MIN_WINDOW_SIZE, max_h); + XResizeWindow(dpy, focused->win, focused->w, focused->h); +} + +void resize_win_up(void) { + if (!focused || !focused->floating) + return; + + int new_h = focused->h - user_config.resize_window_amt; + focused->h = CLAMP(new_h, MIN_WINDOW_SIZE, focused->h); + XResizeWindow(dpy, focused->win, focused->w, focused->h); +} + +void resize_win_right(void) { + if (!focused || !focused->floating) + return; + + int new_w = focused->w + user_config.resize_window_amt; + int max_w = scr_width - focused->x; + focused->w = CLAMP(new_w, MIN_WINDOW_SIZE, max_w); + XResizeWindow(dpy, focused->win, focused->w, focused->h); +} + +void resize_win_left(void) { + if (!focused || !focused->floating) + return; + + int new_w = focused->w - user_config.resize_window_amt; + focused->w = CLAMP(new_w, MIN_WINDOW_SIZE, focused->w); + XResizeWindow(dpy, focused->win, focused->w, focused->h); +} + +void run(void) { + running = True; + XEvent xev; + while (running) { + XNextEvent(dpy, &xev); + xev_case(&xev); + } +} + +void scan_existing_windows(void) { + Window root_return; + Window parent_return; + Window *children; + unsigned int n_children; + + if (XQueryTree(dpy, root, &root_return, &parent_return, &children, + &n_children)) { + for (unsigned int i = 0; i < n_children; i++) { + XWindowAttributes wa; + if (!XGetWindowAttributes(dpy, children[i], &wa) || + wa.override_redirect || wa.map_state != IsViewable) + continue; + + XEvent fake_event = {None}; + fake_event.type = MapRequest; + fake_event.xmaprequest.window = children[i]; + hdl_map_req(&fake_event); + } + if (children) + XFree(children); + } +} + +void select_input(Window w, Mask masks) { XSelectInput(dpy, w, masks); } + +void send_wm_take_focus(Window w) { + Atom wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False); + Atom wm_take_focus = XInternAtom(dpy, "WM_TAKE_FOCUS", False); + Atom *protos; + int n; + + if (XGetWMProtocols(dpy, w, &protos, &n)) { + for (int i = 0; i < n; i++) { + if (protos[i] == wm_take_focus) { + XEvent ev = {.xclient = {.type = ClientMessage, + .window = w, + .message_type = wm_protocols, + .format = 32}}; + ev.xclient.data.l[0] = wm_take_focus; + ev.xclient.data.l[1] = CurrentTime; + XSendEvent(dpy, w, False, NoEventMask, &ev); + } + } + XFree(protos); + } +} + +void setup(void) { + if ((dpy = XOpenDisplay(NULL)) == NULL) { + fprintf(stderr, "can't open display.\nquitting..."); + exit(EXIT_FAILURE); + } + root = XDefaultRootWindow(dpy); + + setup_atoms(); + other_wm(); + init_defaults(); + load_config(); + update_modifier_masks(); + grab_keys(); + + cursor_normal = XcursorLibraryLoadCursor(dpy, "left_ptr"); + cursor_move = XcursorLibraryLoadCursor(dpy, "fleur"); + cursor_resize = XcursorLibraryLoadCursor(dpy, "bottom_right_corner"); + XDefineCursor(dpy, root, cursor_normal); + + scr_width = XDisplayWidth(dpy, DefaultScreen(dpy)); + scr_height = XDisplayHeight(dpy, DefaultScreen(dpy)); + + /* select events wm should look for on root */ + Mask wm_masks = StructureNotifyMask | SubstructureRedirectMask | + SubstructureNotifyMask | KeyPressMask | PropertyChangeMask; + select_input(root, wm_masks); + + /* grab mouse button events on root window */ + Mask root_click_masks = + ButtonPressMask | ButtonReleaseMask | PointerMotionMask; + Mask root_swap_masks = + ButtonPressMask | ButtonReleaseMask | PointerMotionMask; + Mask root_resize_masks = + ButtonPressMask | ButtonReleaseMask | PointerMotionMask; + grab_button(Button1, user_config.modkey, root, True, root_click_masks); + grab_button(Button1, user_config.modkey | ShiftMask, root, True, + root_swap_masks); + grab_button(Button3, user_config.modkey, root, True, root_resize_masks); + XSync(dpy, False); + + for (int i = 0; i < LASTEvent; i++) + evtable[i] = hdl_dummy; + evtable[ButtonPress] = hdl_button; + evtable[ButtonRelease] = hdl_button_release; + evtable[ClientMessage] = hdl_client_msg; + evtable[ConfigureNotify] = hdl_config_ntf; + evtable[ConfigureRequest] = hdl_config_req; + evtable[DestroyNotify] = hdl_destroy_ntf; + evtable[KeyPress] = hdl_keypress; + evtable[MappingNotify] = hdl_mapping_ntf; + evtable[MapRequest] = hdl_map_req; + evtable[MotionNotify] = hdl_motion; + evtable[PropertyNotify] = hdl_property_ntf; + evtable[UnmapNotify] = hdl_unmap_ntf; + scan_existing_windows(); + + /* prevent child processes from becoming zombies */ + signal(SIGCHLD, SIG_IGN); +} + +void setup_atoms(void) { + for (int i = 0; i < ATOM_COUNT; i++) + atoms[i] = XInternAtom(dpy, atom_names[i], False); + + /* checking window */ + wm_check_win = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0); + /* root property -> child window */ + XChangeProperty(dpy, root, atoms[ATOM_NET_SUPPORTING_WM_CHECK], XA_WINDOW, + 32, PropModeReplace, (unsigned char *)&wm_check_win, 1); + /* child window -> child window */ + XChangeProperty(dpy, wm_check_win, atoms[ATOM_NET_SUPPORTING_WM_CHECK], + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&wm_check_win, 1); + /* name the wm */ + const char *wmname = "tilite"; + XChangeProperty(dpy, wm_check_win, atoms[ATOM_NET_WM_NAME], + atoms[ATOM_UTF8_STRING], 8, PropModeReplace, + (const unsigned char *)wmname, strlen(wmname)); + + /* workspace setup */ + long num_workspaces = NUM_WORKSPACES; + XChangeProperty(dpy, root, atoms[ATOM_NET_NUMBER_OF_DESKTOPS], XA_CARDINAL, + 32, PropModeReplace, (const unsigned char *)&num_workspaces, + 1); + + const char workspace_names[] = WORKSPACE_NAMES; + int names_len = sizeof(workspace_names); + XChangeProperty(dpy, root, atoms[ATOM_NET_DESKTOP_NAMES], + atoms[ATOM_UTF8_STRING], 8, PropModeReplace, + (const unsigned char *)workspace_names, names_len); + + XChangeProperty(dpy, root, atoms[ATOM_NET_CURRENT_DESKTOP], XA_CARDINAL, 32, + PropModeReplace, (const unsigned char *)¤t_ws, 1); + + /* load supported list */ + XChangeProperty(dpy, root, atoms[ATOM_NET_SUPPORTED], XA_ATOM, 32, + PropModeReplace, (const unsigned char *)atoms, ATOM_COUNT); + + update_workarea(); +} + +void set_frame_extents(Window w) { + long extents[4] = {user_config.border_width, user_config.border_width, + user_config.border_width, user_config.border_width}; + XChangeProperty(dpy, w, atoms[ATOM_NET_FRAME_EXTENTS], XA_CARDINAL, 32, + PropModeReplace, (unsigned char *)extents, 4); +} + +void set_input_focus(client_t *c, Bool raise_win, Bool warp) { + if (c && c->mapped) { + focused = c; + + /* update remembered focus */ + if (c->ws >= 0 && c->ws < NUM_WORKSPACES) + ws_focused[c->ws] = c; + + Window w = find_toplevel(c->win); + + XSetInputFocus(dpy, w, RevertToPointerRoot, CurrentTime); + send_wm_take_focus(w); + + if (raise_win) { + /* always raise in monocle, otherwise respect floating_on_top */ + if (monocle || c->floating || !user_config.floating_on_top) + XRaiseWindow(dpy, w); + } + /* EWMH focus hint */ + XChangeProperty(dpy, root, atoms[ATOM_NET_ACTIVE_WINDOW], XA_WINDOW, 32, + PropModeReplace, (unsigned char *)&w, 1); + + update_borders(); + + if (warp && user_config.warp_cursor) + warp_cursor(c); + } else { + /* no client */ + XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); + XDeleteProperty(dpy, root, atoms[ATOM_NET_ACTIVE_WINDOW]); + + focused = NULL; + ws_focused[current_ws] = NULL; + update_borders(); + } + + XFlush(dpy); +} + +void reset_opacity(Window w) { + Atom atom = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False); + XDeleteProperty(dpy, w, atom); +} + +void set_opacity(Window w, double opacity) { + if (opacity < 0.0) + opacity = 0.0; + + if (opacity > 1.0) + opacity = 1.0; + + unsigned long op = (unsigned long)(opacity * 0xFFFFFFFFu); + Atom atom = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False); + XChangeProperty(dpy, w, atom, XA_CARDINAL, 32, PropModeReplace, + (unsigned char *)&op, 1); +} + +void set_wm_state(Window w, long state) { + long data[2] = {state, None}; /* state, icon window */ + XChangeProperty(dpy, w, atoms[ATOM_WM_STATE], atoms[ATOM_WM_STATE], 32, + PropModeReplace, (unsigned char *)data, 2); +} + +int snap_coordinate(int pos, int size, int screen_size, int snap_dist) { + if (UDIST(pos, 0) <= snap_dist) + return 0; + if (UDIST(pos + size, screen_size) <= snap_dist) + return screen_size - size; + return pos; +} + +void spawn(const char *const *argv) { + int argc = 0; + while (argv[argc]) + argc++; + + int cmd_count = 1; + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "|") == 0) + cmd_count++; + } + + const char ***commands = malloc(cmd_count * sizeof(char **)); /* *** bruh */ + if (!commands) { + perror("malloc commands"); + return; + } + + /* initialize all command pointers to NULL for safe cleanup */ + for (int i = 0; i < cmd_count; i++) + commands[i] = NULL; + + int cmd_idx = 0; + int arg_start = 0; + for (int i = 0; i <= argc; i++) { + if (!argv[i] || strcmp(argv[i], "|") == 0) { + int len = i - arg_start; + const char **cmd_args = malloc((len + 1) * sizeof(char *)); + + if (!cmd_args) { + perror("malloc cmd_args"); + + for (int j = 0; j < cmd_idx; j++) + free(commands[j]); + + free(commands); + return; + } + + for (int j = 0; j < len; j++) + cmd_args[j] = argv[arg_start + j]; + + cmd_args[len] = NULL; + commands[cmd_idx++] = cmd_args; + arg_start = i + 1; + } + } + + int (*pipes)[2] = malloc(sizeof(int[2]) * (cmd_count - 1)); + if (!pipes) { + perror("malloc pipes"); + + for (int j = 0; j < cmd_count; j++) + free(commands[j]); + + free(commands); + return; + } + + for (int i = 0; i < cmd_count - 1; i++) { + if (pipe(pipes[i]) == -1) { + perror("pipe"); + + for (int j = 0; j < cmd_count; j++) + free(commands[j]); + + free(commands); + free(pipes); + return; + } + } + + for (int i = 0; i < cmd_count; i++) { + if (!commands[i] || !commands[i][0]) + continue; + + pid_t pid = fork(); + if (pid < 0) { + perror("fork"); + + for (int k = 0; k < cmd_count - 1; k++) { + close(pipes[k][0]); + close(pipes[k][1]); + } + + for (int j = 0; j < cmd_count; j++) + free(commands[j]); + + free(commands); + free(pipes); + return; + } + if (pid == 0) { + close(ConnectionNumber(dpy)); + + if (i > 0) + dup2(pipes[i - 1][0], STDIN_FILENO); + + if (i < cmd_count - 1) + dup2(pipes[i][1], STDOUT_FILENO); + + for (int k = 0; k < cmd_count - 1; k++) { + close(pipes[k][0]); + close(pipes[k][1]); + } + + execvp(commands[i][0], (char *const *)(void *)commands[i]); + fprintf(stderr, "tilite: execvp '%s' failed\n", commands[i][0]); + exit(EXIT_FAILURE); + } + } + + for (int i = 0; i < cmd_count - 1; i++) { + close(pipes[i][0]); + close(pipes[i][1]); + } + + for (int i = 0; i < cmd_count; i++) + free(commands[i]); + + free(commands); + free(pipes); +} + +void swap_clients(client_t *a, client_t *b) { + if (!a || !b || a == b) + return; + + client_t **head = &workspaces[current_ws]; + client_t **pa = head, **pb = head; + + while (*pa && *pa != a) + pa = &(*pa)->next; + + while (*pb && *pb != b) + pb = &(*pb)->next; + + if (!*pa || !*pb) + return; + + /* if next to it swap */ + if (*pa == b && *pb == a) { + client_t *tmp = b->next; + b->next = a; + a->next = tmp; + *pa = b; + return; + } + + /* full swap */ + client_t *ta = *pa; + client_t *tb = *pb; + client_t *ta_next = ta->next; + client_t *tb_next = tb->next; + + *pa = tb; + tb->next = ta_next == tb ? ta : ta_next; + + *pb = ta; + ta->next = tb_next == ta ? tb : tb_next; +} + +void tile(void) { + update_struts(); + client_t *head = workspaces[current_ws]; + int total = 0; + + for (client_t *c = head; c; c = c->next) { + if (c->mapped && !c->floating && !c->fullscreen) + total++; + } + + if (total == 0) + return; + + if (monocle) { + for (client_t *c = head; c; c = c->next) { + if (!c->mapped || c->floating || c->fullscreen) + continue; + + int border_width = user_config.border_width; + int gaps = user_config.gaps; + + int x = reserve_left + gaps; + int y = reserve_top + gaps; + int w = scr_width - reserve_left - reserve_right - 2 * gaps; + int h = scr_height - reserve_top - reserve_bottom - 2 * gaps; + + XWindowChanges wc = {.x = x, + .y = y, + .width = MAX(1, w - 2 * border_width), + .height = MAX(1, h - 2 * border_width), + .border_width = border_width}; + XConfigureWindow(dpy, c->win, + CWX | CWY | CWWidth | CWHeight | CWBorderWidth, + &wc); + + c->x = wc.x; + c->y = wc.y; + c->w = wc.width; + c->h = wc.height; + } + + if (focused && focused->mapped && !focused->floating && + !focused->fullscreen) + XRaiseWindow(dpy, focused->win); + + update_borders(); + return; + } + + int mon_x = reserve_left; + int mon_y = reserve_top; + int mon_width = MAX(1, scr_width - reserve_left - reserve_right); + int mon_height = MAX(1, scr_height - reserve_top - reserve_bottom); + + client_t *tileable[MAX_CLIENTS] = {0}; + int n_tileable = 0; + for (client_t *c = head; c && n_tileable < MAX_CLIENTS; c = c->next) { + if (c->mapped && !c->floating && !c->fullscreen) + tileable[n_tileable++] = c; + } + + if (n_tileable == 0) + return; + + int gaps = user_config.gaps; + int tile_x = mon_x + gaps; + int tile_y = mon_y + gaps; + int tile_width = MAX(1, mon_width - 2 * gaps); + int tile_height = MAX(1, mon_height - 2 * gaps); + float master_frac = CLAMP(user_config.master_width, MF_MIN, MF_MAX); + int master_width = + (n_tileable > 1) ? (int)(tile_width * master_frac) : tile_width; + int stack_width = (n_tileable > 1) ? (tile_width - master_width - gaps) : 0; + + { + client_t *c = tileable[0]; + int border_width = 2 * user_config.border_width; + XWindowChanges wc = {.x = tile_x, + .y = tile_y, + .width = MAX(1, master_width - border_width), + .height = MAX(1, tile_height - border_width), + .border_width = user_config.border_width}; + + Bool geom_differ = c->x != wc.x || c->y != wc.y || c->w != wc.width || + c->h != wc.height; + if (geom_differ) + XConfigureWindow(dpy, c->win, + CWX | CWY | CWWidth | CWHeight | CWBorderWidth, + &wc); + + c->x = wc.x; + c->y = wc.y; + c->w = wc.width; + c->h = wc.height; + } + + if (n_tileable == 1) { + update_borders(); + return; + } + + int border_width = 2 * user_config.border_width; + int n_stack = n_tileable - 1; + int min_stack_height = border_width + 1; + int total_fixed_heights = 0; + int n_auto = 0; /* automatically take up leftover space */ + int heights_final[MAX_CLIENTS] = {0}; + + for (int i = 1; i < n_tileable; i++) { /* i=1 - we are excluding master */ + if (tileable[i]->custom_stack_height > 0) + total_fixed_heights += tileable[i]->custom_stack_height; + else + n_auto++; + } + + int total_vgaps = (n_stack - 1) * gaps; + int remaining = tile_height - total_fixed_heights - total_vgaps; + + if (n_auto > 0 && remaining >= n_auto * min_stack_height) { + int used = 0; + int count = 0; + int auto_height = remaining / n_auto; + + for (int i = 1; i < n_tileable; i++) { + if (tileable[i]->custom_stack_height > 0) { + heights_final[i] = tileable[i]->custom_stack_height; + } else { + count++; + heights_final[i] = + (count < n_auto) ? auto_height : remaining - used; + used += auto_height; + } + } + } else { + for (int i = 1; i < n_tileable; i++) { + if (tileable[i]->custom_stack_height > 0) + heights_final[i] = tileable[i]->custom_stack_height; + else + heights_final[i] = min_stack_height; + } + } + + int total_height = total_vgaps; + for (int i = 1; i < n_tileable; i++) + total_height += heights_final[i]; + + int overfill = total_height - tile_height; + if (overfill > 0) { + /* shrink from top down, excluding bottom */ + for (int i = 1; i < n_tileable - 1 && overfill > 0; i++) { + int shrink = MIN(overfill, heights_final[i] - min_stack_height); + heights_final[i] -= shrink; + overfill -= shrink; + } + } + + /* if its not perfectly filled stretch bottom to absorb remainder */ + int actual_height = total_vgaps; + for (int i = 1; i < n_tileable; i++) + actual_height += heights_final[i]; + + int shortfall = tile_height - actual_height; + if (shortfall > 0) + heights_final[n_tileable - 1] += shortfall; + + int stack_y = tile_y; + for (int i = 1; i < n_tileable; i++) { + client_t *c = tileable[i]; + XWindowChanges wc = { + .x = tile_x + master_width + gaps, + .y = stack_y, + .width = MAX(1, stack_width - (2 * user_config.border_width)), + .height = MAX(1, heights_final[i] - (2 * user_config.border_width)), + .border_width = user_config.border_width}; + + Bool geom_differ = c->x != wc.x || c->y != wc.y || c->w != wc.width || + c->h != wc.height; + if (geom_differ) + XConfigureWindow(dpy, c->win, + CWX | CWY | CWWidth | CWHeight | CWBorderWidth, + &wc); + + c->x = wc.x; + c->y = wc.y; + c->w = wc.width; + c->h = wc.height; + + stack_y += heights_final[i] + gaps; + } + update_borders(); +} + +void toggle_floating(void) { + if (!focused) + return; + + if (focused->fullscreen) { + focused->fullscreen = False; + tile(); + XSetWindowBorderWidth(dpy, focused->win, user_config.border_width); + } + + focused->floating = !focused->floating; + + if (focused->floating) { + XWindowAttributes wa; + if (XGetWindowAttributes(dpy, focused->win, &wa)) { + focused->x = wa.x; + focused->y = wa.y; + focused->w = wa.width; + focused->h = wa.height; + + XConfigureWindow(dpy, focused->win, CWX | CWY | CWWidth | CWHeight, + &(XWindowChanges){.x = focused->x, + .y = focused->y, + .width = focused->w, + .height = focused->h}); + } + } + tile(); + update_borders(); + + /* raise and refocus floating window */ + if (focused->floating) + set_input_focus(focused, True, False); +} + +void toggle_floating_global(void) { + global_floating = !global_floating; + Bool any_tiled = False; + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + if (!c->floating) { + any_tiled = True; + break; + } + } + + for (client_t *c = workspaces[current_ws]; c; c = c->next) { + c->floating = any_tiled; + if (c->floating) { + XWindowAttributes wa; + XGetWindowAttributes(dpy, c->win, &wa); + c->x = wa.x; + c->y = wa.y; + c->w = wa.width; + c->h = wa.height; + + XConfigureWindow( + dpy, c->win, CWX | CWY | CWWidth | CWHeight, + &(XWindowChanges){ + .x = c->x, .y = c->y, .width = c->w, .height = c->h}); + XRaiseWindow(dpy, c->win); + } + } + + tile(); + update_borders(); +} + +void toggle_fullscreen(void) { + if (!focused) + return; + + apply_fullscreen(focused, !focused->fullscreen); +} + +void toggle_monocle(void) { + monocle = !monocle; + tile(); + update_borders(); + if (focused) + set_input_focus(focused, True, True); +} + +void update_borders(void) { + for (client_t *c = workspaces[current_ws]; c; c = c->next) + XSetWindowBorder(dpy, c->win, + (c == focused ? user_config.border_foc_col + : user_config.border_ufoc_col)); + + if (focused) { + Window w = focused->win; + XChangeProperty(dpy, root, atoms[ATOM_NET_ACTIVE_WINDOW], XA_WINDOW, 32, + PropModeReplace, (unsigned char *)&w, 1); + } +} + +void update_client_desktop_properties(void) { + for (int ws = 0; ws < NUM_WORKSPACES; ws++) { + for (client_t *c = workspaces[ws]; c; c = c->next) { + long desktop = ws; + XChangeProperty(dpy, c->win, atoms[ATOM_NET_WM_DESKTOP], + XA_CARDINAL, 32, PropModeReplace, + (unsigned char *)&desktop, 1); + } + } +} + +void update_modifier_masks(void) { + XModifierKeymap *mod_mapping = XGetModifierMapping(dpy); + KeyCode num = XKeysymToKeycode(dpy, XK_Num_Lock); + KeyCode mode = XKeysymToKeycode(dpy, XK_Mode_switch); + numlock_mask = 0; + mode_switch_mask = 0; + + int n_masks = 8; + for (int i = 0; i < n_masks; i++) { + for (int j = 0; j < mod_mapping->max_keypermod; j++) { + /* keycode at mod[i][j] */ + KeyCode keycode = + mod_mapping->modifiermap[i * mod_mapping->max_keypermod + j]; + if (keycode == num) + numlock_mask = (1u << i); /* which mod bit == NumLock key */ + if (keycode == mode) + mode_switch_mask = + (1u << i); /* which mod bit == Mode_switch key */ + } + } + XFreeModifiermap(mod_mapping); +} + +void update_net_client_list(void) { + Window wins[MAX_CLIENTS]; + int n = 0; + for (int ws = 0; ws < NUM_WORKSPACES; ws++) + for (client_t *c = workspaces[ws]; c; c = c->next) + wins[n++] = c->win; + + XChangeProperty(dpy, root, atoms[ATOM_NET_CLIENT_LIST], XA_WINDOW, 32, + PropModeReplace, (unsigned char *)wins, n); +} + +void update_struts(void) { + reserve_left = 0; + reserve_right = 0; + reserve_top = 0; + reserve_bottom = 0; + + Window root_ret; + Window parent_ret; + Window *children = NULL; + unsigned int n_children = 0; + + if (!XQueryTree(dpy, root, &root_ret, &parent_ret, &children, &n_children)) + return; + + int screen_w = scr_width; + int screen_h = scr_height; + + for (unsigned int i = 0; i < n_children; i++) { + Window w = children[i]; + + Atom actual_type; + int actual_format; + unsigned long n_items, bytes_after; + Atom *types = NULL; + + if (XGetWindowProperty(dpy, w, atoms[ATOM_NET_WM_WINDOW_TYPE], 0, 4, + False, XA_ATOM, &actual_type, &actual_format, + &n_items, &bytes_after, + (unsigned char **)&types) != Success || + !types) + continue; + + Bool is_dock = False; + for (unsigned long j = 0; j < n_items; j++) { + if (types[j] == atoms[ATOM_NET_WM_WINDOW_TYPE_DOCK]) { + is_dock = True; + break; + } + } + XFree(types); + if (!is_dock) + continue; + + long *str = NULL; + Atom actual; + int sfmt; + unsigned long len; + unsigned long rem; + + if (XGetWindowProperty(dpy, w, atoms[ATOM_NET_WM_STRUT_PARTIAL], 0, 12, + False, XA_CARDINAL, &actual, &sfmt, &len, &rem, + (unsigned char **)&str) == Success && + str && len >= 12) { + + /* + ewmh: + [0] left, [1] right, [2] top, [3] bottom + + [4] left_start_y, [5] left_end_y + [6] right_start_y, [7] right_end_y + [8] top_start_x, [9] top_end_x + [10] bottom_start_x,[11] bottom_end_x + + all coords are in root space. + */ + long left = str[0]; + long right = str[1]; + long top = str[2]; + long bottom = str[3]; + long left_start_y = str[4]; + long left_end_y = str[5]; + long right_start_y = str[6]; + long right_end_y = str[7]; + long top_start_x = str[8]; + long top_end_x = str[9]; + long bot_start_x = str[10]; + long bot_end_x = str[11]; + + XFree(str); + + /* skip empty struts */ + if (!left && !right && !top && !bottom) + continue; + + if (left > 0) { + long span_start = left_start_y; + long span_end = left_end_y; + if (span_end >= 0 && span_start <= scr_height - 1) { + int reserve = (int)MAX(0, left); + if (reserve > 0) + reserve_left = MAX(reserve_left, reserve); + } + } + + if (right > 0) { + long span_start = right_start_y; + long span_end = right_end_y; + if (span_end >= 0 && span_start <= scr_height - 1) { + int global_reserved_left = screen_w - (int)right; + int overlap = scr_width - global_reserved_left; + int reserve = MAX(0, overlap); + if (reserve > 0) + reserve_right = MAX(reserve_right, reserve); + } + } + + if (top > 0) { + long span_start = top_start_x; + long span_end = top_end_x; + if (span_end >= 0 && span_start <= scr_width - 1) { + int reserve = (int)MAX(0, top); + if (reserve > 0) + reserve_top = MAX(reserve_top, reserve); + } + } + + if (bottom > 0) { + long span_start = bot_start_x; + long span_end = bot_end_x; + if (span_end >= 0 && span_start <= scr_width - 1) { + int global_reserved_top = screen_h - (int)bottom; + int overlap = scr_height - global_reserved_top; + int reserve = MAX(0, overlap); + if (reserve > 0) + reserve_bottom = MAX(reserve_bottom, reserve); + } + } + } + } + + if (children) + XFree(children); + + update_workarea(); +} + +void update_workarea(void) { + long workarea[4]; + + workarea[0] = reserve_left; + workarea[1] = reserve_top; + workarea[2] = scr_width - reserve_left - reserve_right; + workarea[3] = scr_height - reserve_top - reserve_bottom; + + XChangeProperty(dpy, root, atoms[ATOM_NET_WORKAREA], XA_CARDINAL, 32, + PropModeReplace, (unsigned char *)workarea, 4); +} + +void warp_cursor(client_t *c) { + if (!c) + return; + + int center_x = c->x + (c->w / 2); + int center_y = c->y + (c->h / 2); + + XWarpPointer(dpy, None, root, 0, 0, 0, 0, center_x, center_y); + XSync(dpy, False); +} + +Bool window_has_ewmh_state(Window w, Atom state) { + Atom type; + int format; + unsigned long n_atoms = 0; + unsigned long unread = 0; + Atom *found_atoms = NULL; + + if (XGetWindowProperty(dpy, w, atoms[ATOM_NET_WM_STATE], 0, 1024, False, + XA_ATOM, &type, &format, &n_atoms, &unread, + (unsigned char **)&found_atoms) == Success && + found_atoms) { + + for (unsigned long i = 0; i < n_atoms; i++) { + if (found_atoms[i] == state) { + XFree(found_atoms); + return True; + } + } + XFree(found_atoms); + } + return False; +} + +void window_set_ewmh_state(Window w, Atom state, Bool add) { + Atom type; + int format; + unsigned long n_atoms = 0; + unsigned long unread = 0; + Atom *found_atoms = NULL; + + if (XGetWindowProperty(dpy, w, atoms[ATOM_NET_WM_STATE], 0, 1024, False, + XA_ATOM, &type, &format, &n_atoms, &unread, + (unsigned char **)&found_atoms) != Success) { + found_atoms = NULL; + n_atoms = 0; + } + + /* build new list */ + Atom buf[16]; + Atom *list = buf; + unsigned long list_len = 0; + + if (found_atoms) { + for (unsigned long i = 0; i < n_atoms; i++) { + if (found_atoms[i] != state) + list[list_len++] = found_atoms[i]; + } + } + if (add && list_len < 16) + list[list_len++] = state; + + if (list_len == 0) + XDeleteProperty(dpy, w, atoms[ATOM_NET_WM_STATE]); + else + XChangeProperty(dpy, w, atoms[ATOM_NET_WM_STATE], XA_ATOM, 32, + PropModeReplace, (unsigned char *)list, list_len); + + if (found_atoms) + XFree(found_atoms); +} + +int xerr(Display *d, XErrorEvent *ee) { + /* ignore noise & non fatal errors */ + const struct { + int req, code; + } ignore[] = { + {0, BadWindow}, + {X_GetGeometry, BadDrawable}, + {X_SetInputFocus, BadMatch}, + {X_ConfigureWindow, BadMatch}, + }; + + for (size_t i = 0; i < sizeof(ignore) / sizeof(ignore[0]); i++) { + if ((ignore[i].req == 0 || ignore[i].req == ee->request_code) && + (ignore[i].code == ee->error_code)) + return 0; + } + + return 0; + (void)d; + (void)ee; +} + +void xev_case(XEvent *xev) { + if (xev->type >= 0 && xev->type < LASTEvent) + evtable[xev->type](xev); + else + fprintf(stderr, "tilite: invalid event type: %d\n", xev->type); +} + +int main(int ac, char **av) { + if (ac > 1) { + if (strcmp(av[1], "-v") == 0 || strcmp(av[1], "--version") == 0) { + printf("%s\n%s\n%s\n", VERSION, AUTHOR, LICENSE); + return EXIT_SUCCESS; + } else { + printf("usage:\n"); + printf("\t[-v || --version]: See the version of tilite\n"); + return EXIT_SUCCESS; + } + } + setup(); + puts("tilite: starting..."); + run(); + return EXIT_SUCCESS; +} |
