From 60f0fced886d2d5afd05cc157bee65f41869ce6d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 6 Oct 2019 15:42:19 +0200 Subject: Rewrite everything MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 23 +- COPYING | 674 ------------------------------------ INSTALL | 83 ----- LICENSE | 15 + Makefile | 29 ++ NEWS | 6 - README | 181 ---------- arg.h | 63 ++++ cg-base.c | 927 ++++++++++++++++++++++++++++++++++++++++++++++++++ cg-base.h | 254 ++++++++++++++ config.mk | 6 + extra/restart-radharc | 59 ---- radharc.c | 402 ++++++++++++++++++++++ src/arg.h | 78 ----- src/macros.h | 57 ---- src/radharc.c | 50 --- src/settings.c | 294 ---------------- src/settings.h | 162 --------- src/state.c | 352 ------------------- src/state.h | 74 ---- 20 files changed, 1699 insertions(+), 2090 deletions(-) delete mode 100644 COPYING delete mode 100644 INSTALL create mode 100644 LICENSE create mode 100644 Makefile delete mode 100644 NEWS delete mode 100644 README create mode 100644 arg.h create mode 100644 cg-base.c create mode 100644 cg-base.h create mode 100644 config.mk delete mode 100755 extra/restart-radharc create mode 100644 radharc.c delete mode 100644 src/arg.h delete mode 100644 src/macros.h delete mode 100644 src/radharc.c delete mode 100644 src/settings.c delete mode 100644 src/settings.h delete mode 100644 src/state.c delete mode 100644 src/state.h diff --git a/.gitignore b/.gitignore index 892d3ac..fc30b43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,6 @@ -_/ -/bin/ -/aux/ -/obj/ -\#*\# -.* -!.git* +*\#* *~ -*.bak -*.new -*.swo -*.swp -*.out *.o +*.a *.su -*.gch -*.info -*.pdf -*.ps -*.dvi -/config.status -/Makefile - +radharc diff --git a/COPYING b/COPYING deleted file mode 100644 index 94a9ed0..0000000 --- a/COPYING +++ /dev/null @@ -1,674 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/INSTALL b/INSTALL deleted file mode 100644 index e632718..0000000 --- a/INSTALL +++ /dev/null @@ -1,83 +0,0 @@ -Type './configure --help' for configuration options. The normal settings for a -GNU package should work. If not, please make a bug report. - - On a common GNU/Linux distribution the following should - be sufficient for most users: - - ./configure --prefix=/usr - make - make install DESTDIR="somewhere you want the files for now" - # Now let the package manager put the files in place... - - Or for an unstaged install: - - ./configure --prefix=/usr - make - sudo make install - -By default any applicable pre-install, post-install, pre-uninstall, and -post-install commands is run. This suppress these, run 'make' with 'N=true' -or 'N=:'. IF you want to know which these commands are, you can use the -methods specificed in the GNU Coding Standards. Another method is found in -mk/README. However, for this packages, these will be: - - infodir="usr/local/share/info" ## Assuming default prefix. - - post_install () { - install-info -- "${infodir}/radharc.info" "${infodir}/dir" - } - - pre_uninstall () { - install-info --delete -- "${infodir}/radharc.info" "${infodir}/dir" - } - - -──────────────────────────────────────────────────────────────────────────────── -CUSTOMISED COMPILATION -──────────────────────────────────────────────────────────────────────────────── - -The makefile is configured to compile the C code with -O2 -g, you can -change this by setting OPTIMISE, or with CFLAGS and LDFLAGS if you want -to change all optional flags compiling and linking flags: - - ./configure OPTIMISE="-Og -g" - - -──────────────────────────────────────────────────────────────────────────────── -CUSTOMISED INSTALLATION -──────────────────────────────────────────────────────────────────────────────── - -If you want to install absolutely everything, you can -instead use the commands below: - - make everything - make install-everything DESTDIR="pkg" - -Or if you only want to absolute basics: - - make base - make install-base DESTDIR="pkg" - -You can select freely what parts of the package to install and not -to install. This rules are available: - -┌─────────────┬─────────────────────┬────────────────────────────────────────────┐ -│ COMPILATION │ INSTALLATION │ DESCRIPTION │ -├─────────────┼─────────────────────┼────────────────────────────────────────────┤ -│ base │ install-base │ Install the basics: │ -│ cmd │ install-cmd │ Install the radharc commands. │ -│ │ install-copyright │ Install the Expat License. │ -│ doc │ install-doc │ Include all manuals: │ -│ info │ install-info │ Include info manual. (Texinfo) │ -│ dvi │ install-dvi │ Include DVI manual. (Texinfo) │ -│ pdf │ install-pdf │ Include PDF manual. (Texinfo) │ -│ ps │ install-ps │ Include PostScript manual. (Texinfo) │ -│ html │ install-html │ Include multifile HTML manual. (Texinfo) │ -│ │ install-man │ Include man pages. │ -│ locale │ install-locale │ Include locales. │ -└─────────────┴─────────────────────┴────────────────────────────────────────────┘ - -install, install-everything, install-base, and install-cmd, have alternatives -that installs a stripped binary: install-strip, install-everything-strip, -install-base-strip, and install-cmd-strip, respectively. - diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c437151 --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +ISC License + +© 2019 Mattias Andrée + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..64843a7 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +.POSIX: + +CONFIGFILE = config.mk +include $(CONFIGFILE) + +OBJ =\ + cg-base.o\ + radharc.o + +HDR =\ + arg.h\ + cg-base.h + +all: radharc +$(OBJ): $(@:.o=.c) $(HDR) + +.c.o: + $(CC) -c -o $@ $< $(CPPFLAGS) $(CFLAGS) + +radharc: $(OBJ) + $(CC) -o $@ $(OBJ) $(LDFLAGS) + +clean: + -rm -f -- radharc *.o + +.SUFFIXES: +.SUFFIXES: .c .o + +.PHONY: all check install uninstall clean diff --git a/NEWS b/NEWS deleted file mode 100644 index e07540c..0000000 --- a/NEWS +++ /dev/null @@ -1,6 +0,0 @@ -radharc NEWS -*- outline -*- - -* Noteworthy changes in release 1.0 (TO BE DETERMINED) [stable] - - Initial release. - diff --git a/README b/README deleted file mode 100644 index be663a0..0000000 --- a/README +++ /dev/null @@ -1,181 +0,0 @@ -NAME - radharc - Reduce eye strain and improve sleep. - -PRONUNCIATION - ɹˈaɪɐrk (depending on dialect) - -SYNOPSIS - radharc [OPTIONS]... - - The -l option is mandatory, unless single value -t is used. - -OPTIONS - -l LATITUDE:LONGITUDE - Tell radharc where you are. This is mandatory. - The values are measured in degrees and in the - GPS (you probably do not have too care about that, - the differences between the systems should not - be significant another), and must be in decimal. - - Reminder for Americans (particularly US Americans), - you are an the western hemisphere, not the eastern, - thus your longtiude is negative. If you experience - weird colour temperatures, 100 % of the times it is - because you forgot the minus sign. But no need to - feel stupid, it is a really common mistake. - - No complicated stuff please, only latitudes within - ±90° and longitudes within ±180°. No unit thought. - - -t DAY:NIGHT - Select colour temperature to use during full daytime - and full night. This should be a integer. Do not - include the unit (the 'K'). The temperatures must - be at least 1000 K. - - -t TEMPERATURE - Select temperature to use. The program will exit - when it is done setting the temperature. The - natural colour temperature is 6500 K ('-t 6500'). - The temperature must be at least 1000 K. - - -t +DELTA - Increase the colour temperature by DELTA kelvin. - - -t -DELTA - Decrease the colour temperature by DELTA kelvin. - - -T TEMPERATURE - Temperature that shall be used when the program - is disabled (via SIGUSR1). - - -p Print the current status. - - -n Set the temperature immediately, do not transition. - - -N Do not transition when exiting, disabling, or - reenabling. - - -o Set the colour, and exit. - - -x Ignore the current calibrations on the monitors. - - -s SECONDS - The start and exit transitions shall take SECONDS - seconds. This may be a floating point number, with - an explicit unit. - - -S KELVINS - The transitions speed, in kelvin per second. - This most be a positive integer. - - -i - Apply negative image filter. Radharc will detect - which monitors have this one when it starts. - - -h PATHNAME - Use a hook script for events. - - -b - Broadcast events with bus. - - -d SERVER=DISPLAY - Use the display server whose identifier is - DISPLAY and whose identifier is stored in the - environment variable SERVER, for example - -d DISPLAY=:0 for the X display :0. - - -d SYSTEM - Use a subsystem which does not have identifiers. - For example 'drm' for the Direct Rendering Manager. - You can also select 'none', this is useful if you - don't want any adjustments, but want events to be - broadcasted. - - -e EDID - Select monitor to use by its EDID. - - -m NUM - Select monitor to use by its global index. - - -m SCREEN:NUM - Select monitor to use by its index without - a screen or graphic card. The later is for when - not inside a graphics environment. - - All options also have a '+' variant, for example '+n'. - These undo the affect of previous '-' variant. - '+d', '+e', and '+m' all undo the affect of all previous - '-d', '-e', and '-m' options (not respectively). - Additionally, with the exception of '-d', '-e', '-m', - subsequent options override the previous of the the option. - -SIGNALS - SIGHUP - Perform an online update to a newer version. - - SIGUSR1 - Enable or disable radharc. - - SIGUSR2 - Toggle negative image filter. Enable on all - monitors if it is enable on some but not all. - - SIGRTMIN+N - Toggle negative image on monitor N, if monitors - have been selected manually, it will be in the - order they where selected. - -ENVIRONMENT - XDG_RUNTIME_DIR - This environment variable names the directory in which - the state file is stored. If unset or empty, /run is - used. - - RADHARC_STATE - The pathname to the state file, will be determined - automatically if not set. If not set, you may only - have one instance running per display server instance. - -RATIONALE - Your location is determined use GeoClue because it clunky, - unreliable, inaccurate and requires an Internet connection - or GPS device, it is way to common that neither is available. - And I do not want to handle all bug reports for this. Besides, - you can use an external program. - - Your timezone is used to determine your location approximately - when not specified, You can be a really poor approximation. - It is too much work supporting summer time, especially when - summer time is not necessarily +1 hour, additionally you can - have scheme where it is first normal time, then summer time, - then double summer time, then back to summer time, and the back - normal time; complicated stuff, additionally, the Russian - Federation is in permanent summer time. And have you heard about - the People's Republic of China (not to be confused with the - less known Republic of China,) they have one timezone, which is - far from in the middle of the country. You can use an external - program. To keep it simple, we are not even using your timezone - to santity check your specified location. - -NOTES - I suggest using a local script named radharc that sets all - options for you. - -KNOWN ISSUES - In X.org, the gamma ramps do not apply to hardware cursors, - because the developers thinks it is the graphics drivers - that shall fix this, and they are not accepting patches for - this. You can use xorg-server-hwcursor-gamma, however, that - patched version does not apply apply the gamma ramps until - the cursor changes image. You can also use sortware cursors - if this really bothers you. - - Wayland does not support this because it [Wayland] sucks. - - Haiku does not support this yet. - -SEE ALSO - redshift(1), blueshift(1), redshift-adjust(1), nightshift(1), - locateme(1) - diff --git a/arg.h b/arg.h new file mode 100644 index 0000000..aeab52a --- /dev/null +++ b/arg.h @@ -0,0 +1,63 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +/* Handles obsolete -NUM syntax */ +#define ARGNUM case '0':\ + case '1':\ + case '2':\ + case '3':\ + case '4':\ + case '5':\ + case '6':\ + case '7':\ + case '8':\ + case '9' + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/cg-base.c b/cg-base.c new file mode 100644 index 0000000..9f77937 --- /dev/null +++ b/cg-base.c @@ -0,0 +1,927 @@ +/** + * cg-tools -- Cooperative gamma-enabled tools + * Copyright (C) 2016, 2018 Mattias Andrée (maandree@kth.se) + * + * 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 . + */ +#include "cg-base.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + + +/** + * The process's name + */ +const char* argv0 = NULL; + +/** + * The libcoopgamma context + */ +libcoopgamma_context_t cg; + +/** + * The names of the selected CRTC:s + */ +char** crtcs = NULL; + +/** + * Gamma ramp updates for each CRTC + */ +filter_update_t* crtc_updates = NULL; + +/** + * CRTC and monitor information about + * each selected CRTC and connect monitor + */ +libcoopgamma_crtc_info_t* crtc_info = NULL; + +/** + * The number of selected CRTC:s + */ +size_t crtcs_n = 0; + +/** + * The number of filters + */ +size_t filters_n = 0; + + +/** + * Contexts for asynchronous ramp updates + */ +static libcoopgamma_async_context_t* asyncs = NULL; + +/** + * The number of pending receives + */ +static size_t pending_recvs = 0; + +/** + * Whether message must be flushed + */ +static int flush_pending = 0; + + + +/** + * Data used to sort CRTC:s + */ +struct crtc_sort_data +{ + /** + * The gamma ramp type + */ + libcoopgamma_depth_t depth; + + /** + * Should be 0 + */ + int __padding; + + /** + * The size of the red gamma ramp + */ + size_t red_size; + + /** + * The size of the green gamma ramp + */ + size_t green_size; + + /** + * The size of the blue gamma ramp + */ + size_t blue_size; + + /** + * The index of the CRTC + */ + size_t index; +}; + + + +/** + * Compare two strings + * + * @param a Return -1 if this string is `NULL` or less than `b` + * @param b Return +1 if this string is less than `a` + * @return See `a` and `b`, 0 is returned if `a` and `b` are equal + */ +static int nulstrcmp(const char *a, const char *b) +{ + return (a == NULL) ? -1 : strcmp(a, b); +} + + +/** + * Compare two instances of `crtc_sort_data` + * + * @param a_ Return -1 if this one is lower + * @param b_ Return +1 if this one is higher + * @return See `a_` and `b_`, only -1 or +1 can be returned + */ +static int crtc_sort_data_cmp(const void* a_, const void* b_) +{ + const struct crtc_sort_data* a = a_; + const struct crtc_sort_data* b = b_; + int cmp = memcmp(a, b, sizeof(*a) - sizeof(a->index)); + return cmp ? cmp : a->index < b->index ? -1 : +1; +} + + +/** + * Make elements in `crtc_updates` slaves where appropriate + * + * @return Zero on success, -1 on error + */ +int make_slaves(void) +{ + struct crtc_sort_data* data; + size_t i, j, n = 0, master = 0, master_i; + + data = alloca(filters_n * sizeof(*data)); + memset(data, 0, filters_n * sizeof(*data)); + for (i = 0; i < filters_n; i++) + { + if (!(crtc_info[crtc_updates[i].crtc].supported)) + continue; + + data[n].depth = crtc_updates[i].filter.depth; + data[n].red_size = crtc_updates[i].filter.ramps.u8.red_size; + data[n].green_size = crtc_updates[i].filter.ramps.u8.green_size; + data[n].blue_size = crtc_updates[i].filter.ramps.u8.blue_size; + data[n].index = i; + n++; + } + + qsort(data, n, sizeof(*data), crtc_sort_data_cmp); + if (n == 0) + return 0; + + master_i = data[0].index; + for (i = 1; i < n; i++) + if (memcmp(data + i, data + master, sizeof(*data) - sizeof(data->index))) + { + if (master + 1 < i) + { + crtc_updates[master_i].slaves = calloc(i - master, sizeof(size_t)); + if (crtc_updates[master_i].slaves == NULL) + return -1; + for (j = 1; master + j < i; j++) + crtc_updates[master_i].slaves[j - 1] = data[master + j].index; + } + master = i; + master_i = data[master].index; + } + else + { + libcoopgamma_ramps_destroy(&(crtc_updates[data[i].index].filter.ramps.u8)); + crtc_updates[data[i].index].master = 0; + crtc_updates[data[i].index].filter.ramps.u8 = crtc_updates[master_i].filter.ramps.u8; + } + + if (master + 1 < i) + { + crtc_updates[master_i].slaves = calloc(i - master, sizeof(size_t)); + if (crtc_updates[master_i].slaves == NULL) + return -1; + for (j = 1; master + j < i; j++) + crtc_updates[master_i].slaves[j - 1] = data[master + j].index; + } + + return 0; +} + + +/** + * Update a filter and synchronise calls + * + * @param index The index of the CRTC + * @param timeout The number of milliseconds a call to `poll` may block, + * -1 if it may block forever + * @return 1: Success, no pending synchronisations + * 0: Success, with still pending synchronisations + * -1: Error, `errno` set + * -2: Error, `cg.error` set + * + * @throws EINTR Call to `poll` was interrupted by a signal + * @throws EAGAIN Call to `poll` timed out + */ +int update_filter(size_t index, int timeout) +{ + filter_update_t* filter = crtc_updates + index; + + if (!(filter->synced) || filter->failed) + abort(); + + pending_recvs += 1; + + if (libcoopgamma_set_gamma_send(&(filter->filter), &cg, asyncs + index) < 0) + switch (errno) + { + case EINTR: + case EAGAIN: +#if EAGAIN != EWOULDBLOCK + case EWOULDBLOCK: +#endif + flush_pending = 1; + break; + default: + return -1; + } + + filter->synced = 0; + return synchronise(timeout); +} + + +/** + * Synchronised calls + * + * @param timeout The number of milliseconds a call to `poll` may block, + * -1 if it may block forever + * @return 1: Success, no pending synchronisations + * 0: Success, with still pending synchronisations + * -1: Error, `errno` set + * -2: Error, `cg.error` set + * + * @throws EINTR Call to `poll` was interrupted by a signal + * @throws EAGAIN Call to `poll` timed out + */ +int synchronise(int timeout) +{ + struct pollfd pollfd; + size_t selected; + + pollfd.fd = cg.fd; + pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; + if (flush_pending > 0) + pollfd.events |= POLLOUT; + + pollfd.revents = 0; + if (poll(&pollfd, (nfds_t)1, timeout) < 0) + return -1; + + if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) + { + if (libcoopgamma_flush(&cg) < 0) + goto sync; + flush_pending = 0; + } + + if ((timeout < 0) && (pending_recvs > 0)) + if (!(pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI))) + { + pollfd.revents = 0; + if (poll(&pollfd, (nfds_t)1, -1) < 0) + return -1; + } + + sync: + if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI | POLLERR | POLLHUP | POLLNVAL)) + for (;;) + { + if (libcoopgamma_synchronise(&cg, asyncs, filters_n, &selected) < 0) + { + if (errno == 0) + continue; + else + goto fail; + } + if (crtc_updates[selected].synced) + continue; + crtc_updates[selected].synced = 1; + pending_recvs -= 1; + if (libcoopgamma_set_gamma_recv(&cg, asyncs + selected) < 0) + { + if (cg.error.server_side) + { + crtc_updates[selected].error = cg.error; + crtc_updates[selected].failed = 1; + memset(&(cg.error), 0, sizeof(cg.error)); + } + else + goto cg_fail; + } + } + + return pending_recvs == 0; + cg_fail: + return -2; + fail: + switch (errno) + { + case EINTR: + case EAGAIN: +#if EAGAIN != EWOULDBLOCK + case EWOULDBLOCK: +#endif + return pending_recvs == 0; + default: + return -1; + } +} + + +/** + * Initialise the process, specifically + * reset the signal mask and signal handlers + * + * @return Zero on success, -1 on error + */ +static int initialise_proc(void) +{ + sigset_t sigmask; + int sig; + + for (sig = 1; sig < _NSIG; sig++) + if (signal(sig, SIG_DFL) == SIG_ERR) + if (sig == SIGCHLD) + return -1; + + if (sigemptyset(&sigmask) < 0) + return -1; + if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0) + return -1; + + return 0; +} + + +/** + * Print, to stdout, a list of all + * recognised adjustment methods + * + * @return Zero on success, -1 on error + */ +static int list_methods(void) +{ + char** list; + size_t i; + + list = libcoopgamma_get_methods(); + if (list == NULL) + return -1; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; +} + + +/** + * Print, to stdout, a list of all CRTC:s + * + * A connection to the coopgamma server + * must have been made + * + * @return Zero on success, -1 on error, -2 + * on libcoopgamma error + */ +static int list_crtcs(void) +{ + char** list; + size_t i; + + list = libcoopgamma_get_crtcs_sync(&cg); + if (list == NULL) + return -2; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; +} + + +/** + * Fill the list of CRTC information + * + * @return Zero on success, -1 on error, -2 + * on libcoopgamma error + */ +static int get_crtc_info(void) +{ + size_t i, unsynced = 0, selected; + char* synced; + int need_flush = 0; + struct pollfd pollfd; + + synced = alloca(crtcs_n * sizeof(*synced)); + memset(synced, 0, crtcs_n * sizeof(*synced)); + + i = 0; + pollfd.fd = cg.fd; + pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; + + while ((unsynced > 0) || (i < crtcs_n)) + { + wait: + if (i < crtcs_n) + pollfd.events |= POLLOUT; + else + pollfd.events &= ~POLLOUT; + + pollfd.revents = 0; + if (poll(&pollfd, (nfds_t)1, -1) < 0) + goto fail; + + if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) + { + if (need_flush && (libcoopgamma_flush(&cg) < 0)) + goto send_fail; + need_flush = 0; + for (; i < crtcs_n; i++) + if (unsynced++, libcoopgamma_get_gamma_info_send(crtcs[i], &cg, asyncs + i) < 0) + goto send_fail; + goto send_done; + send_fail: + switch (errno) + { + case EINTR: + case EAGAIN: +#if EAGAIN != EWOULDBLOCK + case EWOULDBLOCK: +#endif + i++; + need_flush = 1; + break; + default: + goto fail; + } + } + send_done: + + if ((unsynced == 0) && (i == crtcs_n)) + break; + + if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) + while (unsynced > 0) + switch (libcoopgamma_synchronise(&cg, asyncs, i, &selected)) + { + case 0: + if (synced[selected]) + { + libcoopgamma_skip_message(&cg); + break; + } + synced[selected] = 1; + unsynced -= 1; + if (libcoopgamma_get_gamma_info_recv(crtc_info + selected, &cg, asyncs + selected) < 0) + goto cg_fail; + break; + case -1: + switch (errno) + { + case 0: + break; + case EINTR: + case EAGAIN: +#if EAGAIN != EWOULDBLOCK + case EWOULDBLOCK: +#endif + goto wait; + default: + goto fail; + } + break; + } + } + + return 0; + fail: + return -1; + cg_fail: + return -2; +} + + +/** + * -M METHOD + * Select adjustment method. If METHOD is "?", + * available methods will be printed to stdout. + * + * -S SITE + * Select site (display server instance). + * + * -c CRTC + * Select CRT controller. If CRTC is "?", CRTC:s + * will be printed to stdout. + * + * This option can be used multiple times. If it + * is not used at all, all CRTC:s will be selected. + * + * -p PRIORITY + * Select the priority for the filter, this should + * be a signed two's-complement integer. If + * PRIORITY is "?", the default priority for the + * program is printed to stdout. + * + * -R RULE + * The rule of the filter, that is, the last part + * of the class which is its identifier. If RULE + * is "?" the default rule is printed to stdout, + * if RULE is "??" the default class is printed + * to stdout. + * + * @param argc The number of command line arguments + * @param argv The command line arguments + * @return 0 on success, 1 on error + */ +int main(int argc, char* argv[]) +{ + int stage = 0; + int dealloc_crtcs = 0; + int rc = 0; + char* method = NULL; + char* site = NULL; + size_t crtc_i = 0; + int64_t priority = default_priority; + char* prio = NULL; + char* rule = NULL; + char* class = default_class; + char** classes = NULL; + size_t classes_n = 0; + int explicit_crtcs = 0; + int have_crtc_q = 0; + size_t i, filter_i; + + argv0 = *argv++, argc--; + + if (initialise_proc() < 0) + goto fail; + + crtcs = alloca(argc * sizeof(*crtcs)); + + for (; *argv; argv++, argc--) + { + char* args = *argv; + char opt[3]; + if (!strcmp(args, "--")) + { + argv++, argc--; + break; + } + opt[0] = *args++; + opt[2] = '\0'; + if ((*opt != '-') && (*opt != '+')) + break; + while (*args) + { + char* arg; + int at_end; + opt[1] = *args++; + arg = args; + if ((at_end = !*arg)) + arg = argv[1]; + if (!strcmp(opt, "-M")) + { + if ((method != NULL) || ((method = arg) == NULL)) + usage(); + } + else if (!strcmp(opt, "-S")) + { + if ((site != NULL) || ((site = arg) == NULL)) + usage(); + } + else if (!strcmp(opt, "-c")) + { + if (arg == NULL) + usage(); + crtcs[crtc_i++] = arg; + explicit_crtcs = 1; + if (!have_crtc_q && !strcmp(arg, "?")) + have_crtc_q = 1; + } + else if (!strcmp(opt, "-p")) + { + if ((prio != NULL) || ((prio = arg) == NULL)) + usage(); + } + else if (!strcmp(opt, "-R")) + { + if ((rule != NULL) || ((rule = arg) == NULL)) + usage(); + } + else + switch (handle_opt(opt, arg)) + { + case 0: + goto next_opt; + case 1: + break; + default: + goto fail; + } + argv += at_end; + argc -= at_end; + break; + next_opt:; + } + } + + crtcs_n = crtc_i; + crtcs[crtc_i] = NULL; + if (!have_crtc_q && nulstrcmp(method, "?") && + nulstrcmp(rule, "?") && nulstrcmp(rule, "??") && + ((default_priority == NO_DEFAULT_PRIORITY) || nulstrcmp(prio, "?"))) + if (handle_args(argc, argv, prio) < 0) + goto fail; + + if (default_priority != NO_DEFAULT_PRIORITY) + { + if (!nulstrcmp(prio, "?")) + { + printf("%" PRIi64 "\n", priority); + return 0; + } + else if (prio != NULL) + { + char *end; + errno = 0; + priority = (int64_t)strtoll(prio, &end, 10); + if (errno || *end || !*prio) + usage(); + } + } + + if (!nulstrcmp(rule, "??")) + { + size_t i; + if (*class_suffixes == NULL) + printf("%s\n", class); + else + for (i = 0; class_suffixes[i] != NULL; i++) + printf("%s%s\n", class, class_suffixes[i]); + return 0; + } + else if (!nulstrcmp(rule, "?")) + { + printf("%s\n", strstr(strstr(class, "::") + 2, "::") + 2); + return 0; + } + else if (rule != NULL) + { + char* p = strstr(strstr(class, "::") + 2, "::") + 2; + size_t n = (size_t)(p - class); + class = alloca(strlen(rule) + n + (size_t)1); + memcpy(class, default_class, n); + strcpy(class + n, rule); + if (strchr(class, '\n')) + { + fprintf(stderr, "%s: LF character is not allowed in the filter's class\n", argv0); + goto custom_fail; + } + } + + if (!nulstrcmp(method, "?")) + { + if (list_methods() < 0) + goto fail; + return 0; + } + + if (libcoopgamma_context_initialise(&cg) < 0) + goto fail; + stage++; + if (libcoopgamma_connect(method, site, &cg) < 0) + { + fprintf(stderr, "%s: server failed to initialise\n", argv0); + goto custom_fail; + } + stage++; + + if (have_crtc_q) + switch (list_crtcs()) + { + case 0: + goto done; + case -1: + goto fail; + default: + goto cg_fail; + } + + if (crtcs_n == 0) + { + crtcs = libcoopgamma_get_crtcs_sync(&cg); + if (crtcs == NULL) + goto cg_fail; + dealloc_crtcs = 1; + for (; crtcs[crtcs_n] != NULL; crtcs_n++); + } + + if (crtcs_n == 0) + { + fprintf(stderr, "%s: no CRTC:s are available\n", argv0); + goto custom_fail; + } + + if (*class_suffixes == NULL) + { + classes = &class; + classes_n = 1; + } + else + { + size_t len = strlen(class); + while (class_suffixes[classes_n]) + classes_n++; + classes = alloca(classes_n * sizeof(*classes)); + for (i = 0; i < classes_n; i++) + { + classes[i] = alloca(len + strlen(class_suffixes[i]) + sizeof(":")); + stpcpy(stpcpy(stpcpy(classes[i], class), ":"), class_suffixes[i]); + } + } + filters_n = classes_n * crtcs_n; + + crtc_info = alloca(crtcs_n * sizeof(*crtc_info)); + memset(crtc_info, 0, crtcs_n * sizeof(*crtc_info)); + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) + if (libcoopgamma_crtc_info_initialise(crtc_info + crtc_i) < 0) + goto cg_fail; + + if (libcoopgamma_set_nonblocking(&cg, 1) < 0) + goto fail; + + asyncs = alloca(filters_n * sizeof(*asyncs)); + memset(asyncs, 0, filters_n * sizeof(*asyncs)); + for (filter_i = 0; filter_i < filters_n; filter_i++) + if (libcoopgamma_async_context_initialise(asyncs + filter_i) < 0) + goto fail; + + switch (get_crtc_info()) + { + case 0: + break; + case -1: + goto fail; + case -2: + goto cg_fail; + } + + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) + { + if (explicit_crtcs && !(crtc_info[crtc_i].supported)) + fprintf(stderr, "%s: warning: gamma adjustments not supported on CRTC: %s\n", + argv0, crtcs[crtc_i]); + if (crtc_info[crtc_i].cooperative == 0) + fprintf(stderr, "%s: warning: cooperative gamma server not running for CRTC: %s\n", + argv0, crtcs[crtc_i]); + } + + crtc_updates = alloca(filters_n * sizeof(*crtc_updates)); + memset(crtc_updates, 0, filters_n * sizeof(*crtc_updates)); + for (filter_i = i = 0; i < classes_n; i++) + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++, filter_i++) + { + if (libcoopgamma_filter_initialise(&(crtc_updates[filter_i].filter)) < 0) + goto fail; + if (libcoopgamma_error_initialise(&(crtc_updates[filter_i].error)) < 0) + goto fail; + crtc_updates[filter_i].crtc = crtc_i; + crtc_updates[filter_i].synced = 1; + crtc_updates[filter_i].failed = 0; + crtc_updates[filter_i].master = 1; + crtc_updates[filter_i].slaves = NULL; + crtc_updates[filter_i].filter.crtc = crtcs[crtc_i]; + crtc_updates[filter_i].filter.class = classes[i]; + crtc_updates[filter_i].filter.priority = priority; + crtc_updates[filter_i].filter.depth = crtc_info[crtc_i].depth; + crtc_updates[filter_i].filter.ramps.u8.red_size = crtc_info[crtc_i].red_size; + crtc_updates[filter_i].filter.ramps.u8.green_size = crtc_info[crtc_i].green_size; + crtc_updates[filter_i].filter.ramps.u8.blue_size = crtc_info[crtc_i].blue_size; + switch (crtc_updates[filter_i].filter.depth) + { +#define X(CONST, MEMBER, MAX, TYPE)\ + case CONST:\ + libcoopgamma_ramps_initialise(&(crtc_updates[filter_i].filter.ramps.MEMBER));\ + libclut_start_over(&(crtc_updates[filter_i].filter.ramps.MEMBER), MAX, TYPE, 1, 1, 1);\ + break; +LIST_DEPTHS +#undef X + default: + fprintf(stderr, "%s: internal error: gamma ramp type is unrecognised: %i\n", + argv0, crtc_updates[filter_i].filter.depth); + goto custom_fail; + } + } + + switch (start()) + { + case 0: + break; + case -1: + goto fail; + case -2: + goto cg_fail; + case -3: + goto custom_fail; + } + + for (filter_i = 0; filter_i < filters_n; filter_i++) + if (crtc_updates[filter_i].failed) + { + const char* side = cg.error.server_side ? "server" : "client"; + const char* crtc = crtc_updates[filter_i].filter.crtc; + if (cg.error.custom) + { + if ((cg.error.number != 0) && (cg.error.description != NULL)) + fprintf(stderr, "%s: %s-side error number %" PRIu64 " for CRTC %s: %s\n", + argv0, side, cg.error.number, crtc, cg.error.description); + else if (cg.error.number != 0) + fprintf(stderr, "%s: %s-side error number %" PRIu64 " for CRTC %s\n", + argv0, side, cg.error.number, crtc); + else if (cg.error.description != NULL) + fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", argv0, side, crtc, cg.error.description); + } + else if (cg.error.description != NULL) + fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", argv0, side, crtc, cg.error.description); + else + fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", argv0, side, crtc, strerror(cg.error.number)); + } + + done: + if (dealloc_crtcs) + free(crtcs); + if (crtc_info != NULL) + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) + libcoopgamma_crtc_info_destroy(crtc_info + crtc_i); + if (asyncs != NULL) + for (filter_i = 0; filter_i < filters_n; filter_i++) + libcoopgamma_async_context_destroy(asyncs + filter_i); + if (stage >= 1) + libcoopgamma_context_destroy(&cg, stage >= 2); + if (crtc_updates != NULL) + for (filter_i = 0; filter_i < filters_n; filter_i++) + { + if (crtc_updates[filter_i].master == 0) + memset(&(crtc_updates[filter_i].filter.ramps.u8), 0, sizeof(crtc_updates[filter_i].filter.ramps.u8)); + crtc_updates[filter_i].filter.crtc = NULL; + crtc_updates[filter_i].filter.class = NULL; + libcoopgamma_filter_destroy(&(crtc_updates[filter_i].filter)); + libcoopgamma_error_destroy(&(crtc_updates[filter_i].error)); + free(crtc_updates[filter_i].slaves); + } + return rc; + + custom_fail: + rc = 1; + goto done; + + fail: + rc = 1; + if (errno) + perror(argv0); + goto done; + + cg_fail: + rc = 1; + { + const char* side = cg.error.server_side ? "server" : "client"; + if (cg.error.custom) + { + if ((cg.error.number != 0) && (cg.error.description != NULL)) + fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n", + argv0, side, cg.error.number, cg.error.description); + else if (cg.error.number != 0) + fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number); + else if (cg.error.description != NULL) + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + } + else if (cg.error.description != NULL) + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + else + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror(cg.error.number)); + } + goto done; +} + diff --git a/cg-base.h b/cg-base.h new file mode 100644 index 0000000..cd885e7 --- /dev/null +++ b/cg-base.h @@ -0,0 +1,254 @@ +/** + * cg-tools -- Cooperative gamma-enabled tools + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * 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 . + */ +#include + +#include + + + +/** + * Value of `default_priority` that indicates + * that there is no default priority + */ +#define NO_DEFAULT_PRIORITY INT64_MAX + + + +/** + * X-macro that list all gamma ramp types + * + * X will be expanded with 4 arguments: + * 1) The libcoopgamma constant that identifies the type + * 2) The member in `union libcoopgamma_ramps` that + * corresponds to the type + * 3) The max value for the ramp stops + * 4) The type of the ramp stops + */ +#define LIST_DEPTHS\ + X(LIBCOOPGAMMA_UINT8, u8, UINT8_MAX, uint8_t)\ + X(LIBCOOPGAMMA_UINT16, u16, UINT16_MAX, uint16_t)\ + X(LIBCOOPGAMMA_UINT32, u32, UINT32_MAX, uint32_t)\ + X(LIBCOOPGAMMA_UINT64, u64, UINT64_MAX, uint64_t)\ + X(LIBCOOPGAMMA_FLOAT, f, ((float)1), float)\ + X(LIBCOOPGAMMA_DOUBLE, d, ((double)1), double) + + + +/** + * Information (except asynchronous call context) + * required to update the gamma ramps on a CRTC. + */ +typedef struct filter_update +{ + /** + * The filter to update + * + * `.filter.crtc`, `.filter.class`, and + * `.filter.priority` (unless `default_priority` + * is `NO_DEFAULT_PRIORITY`), `.filter.depth` + * are preconfigured, and `.filter.ramps` + * is preinitialised and preset to an + * identity ramp + */ + libcoopgamma_filter_t filter; + + /** + * The index of the CRTC + */ + size_t crtc; + + /** + * Has the update been synchronised? + */ + int synced; + + /** + * Did the update fail? + */ + int failed; + + /** + * Error description if `.failed` is true + */ + libcoopgamma_error_t error; + + /** + * If zero, the ramps in `.filter` shall + * neither be modified nor freed + */ + int master; + + /** + * 0-terminated list of elements in + * `.crtc_updates` which shares gamma + * ramps with this instance + * + * This will only be set if `.master` + * is true + */ + size_t* slaves; + +} filter_update_t; + + + +/** + * The process's name + */ +extern const char* argv0; + +/** + * The libcoopgamma context + */ +extern libcoopgamma_context_t cg; + +/** + * The names of the selected CRTC:s + */ +extern char** crtcs; + +/** + * Gamma ramp updates for each CRTC + */ +extern filter_update_t* crtc_updates; + +/** + * CRTC and monitor information about + * each selected CRTC and connect monitor + */ +extern libcoopgamma_crtc_info_t* crtc_info; + +/** + * The number of selected CRTC:s + */ +extern size_t crtcs_n; + +/** + * The number of filters + */ +extern size_t filters_n; + + + +/** + * The default filter priority for the program + */ +extern const int64_t default_priority; + +/** + * The default class for the program + */ +extern char default_class[]; + +/** + * Class suffixes + */ +extern const char* const* class_suffixes; + + + +/** + * Make elements in `crtc_updates` slaves where appropriate + * + * @return Zero on success, -1 on error + */ +int make_slaves(void); + +/** + * Update a filter and synchronise calls + * + * @param index The index of the CRTC + * @param timeout The number of milliseconds a call to `poll` may block, + * -1 if it may block forever + * @return 1: Success, no pending synchronisations + * 0: Success, with still pending synchronisations + * -1: Error, `errno` set + * -2: Error, `cg.error` set + * + * @throws EINTR Call to `poll` was interrupted by a signal + * @throws EAGAIN Call to `poll` timed out + */ +int update_filter(size_t index, int timeout); + +/** + * Synchronised calls + * + * @param timeout The number of milliseconds a call to `poll` may block, + * -1 if it may block forever + * @return 1: Success, no pending synchronisations + * 0: Success, with still pending synchronisations + * -1: Error, `errno` set + * -2: Error, `cg.error` set + * + * @throws EINTR Call to `poll` was interrupted by a signal + * @throws EAGAIN Call to `poll` timed out + */ +int synchronise(int timeout); + + +/** + * Print usage information and exit + */ +#if defined(__GNUC__) +__attribute__((__noreturn__)) +#endif +extern void usage(void); + +/** + * Handle a command line option + * + * @param opt The option, it is a NUL-terminate two-character + * string starting with either '-' or '+', if the + * argument is not recognised, call `usage`. This + * string will not be "-M", "-S", "-c", "-p", or "-R". + * @param arg The argument associated with `opt`, + * `NULL` there is no next argument, if this + * parameter is `NULL` but needed, call `usage` + * @return 0 if `arg` was not used, + * 1 if `arg` was used, + * -1 on error + */ +#if defined(__GNUC__) +__attribute__((__nonnull__(1))) +#endif +extern int handle_opt(char* opt, char* arg); + +/** + * This function is called after the last + * call to `handle_opt` + * + * @param argc The number of unparsed arguments + * @param argv `NULL` terminated list of unparsed arguments + * @param prio The argument associated with the "-p" option + * @return Zero on success, -1 on error + */ +#if defined(__GNUC__) +__attribute__((__nonnull__(2))) +#endif +extern int handle_args(int argc, char* argv[], char* prio); + +/** + * The main function for the program-specific code + * + * @return 0: Success + * -1: Error, `errno` set + * -2: Error, `cg.error` set + * -3: Error, message already printed + */ +extern int start(void); + diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..66d6676 --- /dev/null +++ b/config.mk @@ -0,0 +1,6 @@ +PREFIX = /usr +MANPREFIX = $(PREFIX)/share/man + +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE +CFLAGS = -std=c99 -Wall -g +LDFLAGS = -lcoopgamma -lred -lm diff --git a/extra/restart-radharc b/extra/restart-radharc deleted file mode 100755 index 96af888..0000000 --- a/extra/restart-radharc +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/sh - -# Copyright © 2016 Mattias Andrée -# -# 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 . - -# DEPENDENCIES: findutils, coreutils, sed, util-linux, linux, sh - -# Frequent international traveller? Set up cron to run this every -# now and then. It will restart all your instances of radharc, and -# if you have a custom script in $PATH named radharc (as your should -# have) that figures out your location and starts radharc, radharc -# will br aware of your no location. - - -# Close all file descriptors (except stderr,) so that radharc does not inherity any. -for fd in $(ls -1 /dev/fd/); do - if [ ! "$fd" = 2 ]; then - eval "exec ${fd}<&-"; - fi -done - -# Get the user's real UID, in a sh-portable way. -uid="$(cat /proc/self/status | grep '^Uid:' | cut -f 2)" - -# Restart all instances of radharc. -for pid in pgrep -x -U $uid radharc; do - # Open environ and cmdline and be sure that they we successfully opened. - # It is possible that the process has exited. - exec 3/dev/null - if [ ! $? = 0 ]; then - continue - fi - exec 4/dev/null - if [ ! $? = 0 ]; then - continue - fi - - # Restart radharc - (<&3 cat - printf 'radharc\x00' - <&4 xargs -0 printf '%s\n' | sed 1d | grep -v '^-n$' | xargs printf '%s\x00' - printf '%s\x00' '-n' - exec 3<&- - exec 4<&- - ) | (kill -KILL "$pid" ; exec xargs -0 setsid env -i -- <>/dev/null 2>/dev/null &) -done - diff --git a/radharc.c b/radharc.c new file mode 100644 index 0000000..023afab --- /dev/null +++ b/radharc.c @@ -0,0 +1,402 @@ +/* See LICENSE file for copyright and license details. */ +#include "cg-base.h" + +#include +#include +#include +#include +#include + +#include +#include + +/** + * The default filter priority for the program + */ +const int64_t default_priority = (int64_t)7 << 61; + +/** + * The default class for the program + */ +char default_class[] = "radharc::radharc::standard"; + +/** + * Class suffixes + */ +const char *const *class_suffixes = (const char *const[]){NULL}; + +/** + * The effect fade-in time, in centiseconds + */ +static unsigned long int fade_in_cs = 0; + +/** + * The effect fade-out time, in centiseconds + */ +static unsigned long int fade_out_cs = 0; + +/** + * The highest elevation of the Sun where the lowest + * colour temperature is applied + */ +static double low_elev = -6; + +/** + * The lowest colour temperature that may be applied + */ +static double low_temp = 2500; + +/** + * The lowest elevation of the Sun where the highest + * colour temperature is applied + */ +static double high_elev = 3; + +/** + * The highest colour temperature that may be applied + */ +static double high_temp = 5000; + +/** + * The temperature choosen with the -f flag, negative if none + */ +static double choosen_temperature = -1; + +/** + * The latitude coordiate of the GPS coordiates of + * the user's location + */ +static double latitude; + +/** + * The longitude coordiate of the GPS coordiates of + * the user's location + */ +static double longitude; + +/** + * Whether the user's location has been specified + */ +static int have_location = 0; + +/** + * Whether the -d flag (keep process running and remove + * effect when killed) has been specified + */ +static int dflag = 0; + +/** + * Whether the -x flag (remove applied effect) + * has been specified + */ +static int xflag = 0; + +/** + * Print usage information and exit + */ +void +usage(void) +{ + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] [-p priority]" + " [-f fade-in] [-F fade-out] [-h [high-temp][@high-elev]] [-l [low-temp][@low-elev]]" + " (-L latitude:longitude | -t temperature [-d] | -x)\n", argv0); + exit(1); +} + +/** + * Parse a non-negative double encoded as a string + * + * @param out Output parameter for the value + * @param str The string + * @return Zero on success, -1 if the string is invalid + */ +static int +parse_double(double *out, const char *str) +{ + char *end; + errno = 0; + *out = strtod(str, &end); + if (errno || *out < 0 || isinf(*out) || isnan(*out) || *end) + return -1; + if (!*str || !strchr("0123456789.", *str)) + return -1; + return 0; +} + +/** + * Handle a command line option + * + * @param opt The option, it is a NUL-terminate two-character + * string starting with either '-' or '+', if the + * argument is not recognised, call `usage`. This + * string will not be "-M", "-S", "-c", "-p", or "-R". + * @param arg The argument associated with `opt`, + * `NULL` there is no next argument, if this + * parameter is `NULL` but needed, call `usage` + * @return 0 if `arg` was not used, + * 1 if `arg` was used, + * -1 on error + */ +int +handle_opt(char *opt, char *arg) +{ + double t; + char *p; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + dflag = 1; + xflag = 0; + break; + case 'f': + if (parse_double(&t, arg)) + usage(); + fade_in_cs = (unsigned long int)(t * 100 + 0.5); + return 1; + case 'F': + if (parse_double(&t, arg)) + usage(); + fade_out_cs = (unsigned long int)(t * 100 + 0.5); + return 1; + case 'h': + p = strchr(arg, '@'); + if (p) + *p++ = '\0'; + if (*arg && parse_double(&high_temp, arg)) + usage(); + if (*p && parse_double(&high_elev, p)) + usage(); + return 1; + case 'l': + p = strchr(arg, '@'); + if (p) + *p++ = '\0'; + if (*arg && parse_double(&low_temp, arg)) + usage(); + if (*p && parse_double(&low_elev, p)) + usage(); + return 1; + case 'L': + p = strchr(arg, ':'); + if (!p) + usage(); + *p++ = '\0'; + if (parse_double(&latitude, arg) || latitude < -90 || latitude > 90) + usage(); + if (parse_double(&longitude, p) || longitude < -180 || longitude > 180) + usage(); + choosen_temperature = -1; + have_location = 1; + dflag = 0; + xflag = 0; + return 1; + case 't': + if (parse_double(&choosen_temperature, arg)) + usage(); + xflag = 0; + return 1; + case 'x': + xflag = 1; + dflag = 0; + break; + default: + usage(); + } + } else { + usage(); + } + return 0; +} + +/** + * This function is called after the last + * call to `handle_opt` + * + * @param argc The number of unparsed arguments + * @param argv `NULL` terminated list of unparsed arguments + * @param prio The argument associated with the "-p" option + * @return Zero on success, -1 on error + */ +int +handle_args(int argc, char *argv[], char *prio) +{ + if (argc || (!xflag && !have_location && choosen_temperature < 0)) + usage(); + return 0; + (void) argv; + (void) prio; +} + +/** + * Fill a filter + * + * @param filter The filter to fill + * @param red The red brightness + * @param green The green brightness + * @param blue The blue brightness + */ +static void +fill_filter(libcoopgamma_filter_t *restrict filter, double red, double green, double blue) +{ + switch (filter->depth) { +#define X(CONST, MEMBER, MAX, TYPE)\ + case CONST:\ + libclut_start_over(&(filter->ramps.MEMBER), MAX, TYPE, 1, 1, 1);\ + libclut_rgb_brightness(&(filter->ramps.MEMBER), MAX, TYPE, red, green, blue);\ + break; +LIST_DEPTHS +#undef X + default: + abort(); + } +} + +/** + * Set the gamma ramps + * + * @param red The red brightness + * @param green The green brightness + * @param blue The blue brightness + * @return 0: Success + * -1: Error, `errno` set + * -2: Error, `cg.error` set + * -3: Error, message already printed + */ +static int +set_ramps(double red, double green, double blue) +{ + int r; + size_t i, j; + + for (i = 0, r = 1; i < filters_n; i++) { + if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) + continue; + fill_filter(&(crtc_updates[i].filter), red, green, blue); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j] != 0; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } + } + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + return 0; +} + +/** + * Get the colour temperature for the current time + * + * @param tp Output parameter for the colour temperature + * @return 0 on success, -1 on failure + */ +static int +get_temperature(double *tp) +{ + if (choosen_temperature < 0) { + if (libred_solar_elevation(latitude, longitude, tp)) + return -1; + printf("elevation: %g\n", *tp); + if (*tp < low_elev) + *tp = low_elev; + if (*tp > high_elev) + *tp = high_elev; + *tp = (*tp - low_elev) / (high_elev - low_elev); + *tp = low_temp + *tp * (high_temp - low_temp); + printf("temperature: %g\n", *tp); + } else { + *tp = choosen_temperature; + } + return 0; +} + + +/** + * The main function for the program-specific code + * + * @return 0: Success + * -1: Error, `errno` set + * -2: Error, `cg.error` set + * -3: Error, message already printed + */ +int +start(void) +{ + int r, tfd; + size_t i; + double temperature, red, green, blue; + uint64_t overrun; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (choosen_temperature >= 0 && !dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + + if (!xflag) { + if (libred_check_timetravel()) + return -1; + if (choosen_temperature < 0) + dflag = 1; + } + + if (xflag) + return set_ramps(1, 1, 1); + + if ((r = make_slaves()) < 0) + return r; + + if (!fade_in_cs) + goto no_fade_in; + + tfd = timerfd_create(CLOCK_MONOTONIC, 0); + if (tfd < 0) + return -1; + if (timerfd_settime(tfd, 0, &(struct itimerspec){{0, 10000000L}, {0, 10000000L}}, NULL)) + return -1; + + for (i = 0; i < (size_t)fade_in_cs;) { + if (i % 600 == 0) + if ((r = get_temperature(&temperature)) < 0) + return r; + if (libred_get_colour((long int)(6500 - (6500 - temperature) * i / fade_in_cs), &red, &green, &blue)) + return -1; + if ((r = set_ramps(red, green, blue)) < 0) + return r; + + if (read(tfd, &overrun, sizeof(overrun)) != sizeof(overrun)) + return -1; + if (overrun > fade_in_cs - i) + overrun = fade_in_cs - i; + i += overrun; + } + + close(tfd); + +no_fade_in: + for (;;) { + if ((r = get_temperature(&temperature)) < 0) + return r; + if (libred_get_colour((long int)temperature, &red, &green, &blue)) + return -1; + if ((r = set_ramps(red, green, blue)) < 0) + return r; + + if (!dflag) + return 0; + + sleep(6); + } +} diff --git a/src/arg.h b/src/arg.h deleted file mode 100644 index 77a519d..0000000 --- a/src/arg.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée - * - * 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 . - */ - - - -/** - * The name of the process. - */ -extern char *argv0; - - - -/** - * Start command line parsing. - * - * `argv` and `argc` must be available as given to `main`. - * The global variable `argv0` must be declare (not necessary - * the the same file) and will be set to `argv[0]`. - * - * Only short options and "--" are supported. Short options - * may start with a '+', in which case `plus` will be 1. - * - * @example - * ARGBEGIN { - * case 'a': printf("%s\n", plus ? "+a" : "-a"); break; - * case 'b': if (plus) usage(1); printf("-b\n"); break; - * case 'c': if (plus) printf("+c\n"); else printf("-c %s\n", ARGF()); break; - * default: usage(1); break; - * } ARGEND; - */ -#define ARGBEGIN \ -do { \ - for (argv0 = *argv++, argc -= !!argv0; *argv; argv++, argc--) { \ - int plus = argv[0][0] == '+', next__ = 0; \ - if (((argv[0][0] != '-') && !plus) || (argv[0][1] == (plus ? '+' : '-'))) { \ - if ((argv[0][0] == argv[0][1]) && (argv[0][1] == '-') && (argv[0][2] == '\0')) \ - argc--, argv++; \ - break; \ - } \ - for (argv[0]++; argv[0][0] && !next__;) { \ - switch (*(argv[0])++) - -/** - * End of command line parsing, - */ -#define ARGEND \ - } \ - } \ -} while (0) - -/** - * Get the argument of the current option. - * Do not use more once per option. - * - * The function `noreturn void usage(int)` must be available. - * `usage` shall exit if its argument is `1`. - * - * @return The argument of the current option. - */ -#define ARGF() \ - (next__ = 1, argv[0][0] ? (argv[0]) \ - : (argv++, (argv[0] ? (argc--, argv[0]) \ - : (usage(1), NULL)))) - diff --git a/src/macros.h b/src/macros.h deleted file mode 100644 index 298a41a..0000000 --- a/src/macros.h +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée - * - * 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 . - */ -#include -#include -#include -#include - - - -#define try(...) do { if (!(__VA_ARGS__)) goto fail; } while (0) -#define t(...) do { if (__VA_ARGS__) goto fail; } while (0) -#define CLEANUP(...) do { int cleanup__ = errno; __VA_ARGS__; errno = cleanup__; } while (0) - -#define xstrdup(outp, ...) \ -do { \ - const char *xstrdup__ = (__VA_ARGS__); \ - if (xstrdup__) \ - try (*(outp) = strdup(xstrdup__)); \ - else \ - *(outp) = NULL; \ -} while (0) - -#define xpread(fd, buf, len, off) t (pread(fd, buf, len, off) < (ssize_t)(len)) -#define xpwrite(fd, buf, len, off) t (pwrite(fd, buf, len, off) < (ssize_t)(len)) -#define xread(fd, buf, len) t (read(fd, buf, len) < (ssize_t)(len)) -#define xwrite(fd, buf, len) t (write(fd, buf, len) < (ssize_t)(len)) - -#define xcalloc(outp, num) try (*(outp) = calloc(num, sizeof(**(outp)))) -#define xmalloc(outp, num) try (*(outp) = malloc((num) * sizeof(**(outp)))) -#define xrealloc(outp, num) \ -do { \ - size_t n__ = (num); \ - void *new__ = realloc(*(outp), n__ * sizeof(**(outp))); \ - t (n__ && !new__); \ - *(outp) = new__; \ -} while (0) - -#define SHRINK(outp, num) \ -do { \ - void *new__ = realloc(*(outp), (num) * sizeof(**(outp))); \ - if (new__) *(outp) = new__; \ -} while (0) - diff --git a/src/radharc.c b/src/radharc.c deleted file mode 100644 index a0b1344..0000000 --- a/src/radharc.c +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée - * - * 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 . - */ -#include "state.h" -#include "macros.h" - -#include -#include - - - -/** - * The name of the process. - */ -char *argv0 = "radharc"; - - - -int -main(int argc, char *argv[]) -{ - struct settings settings; - - t (libred_check_timetravel()); - parse_command_line(argc, argv, &settings); - argv0 = argv0 ? argv0 : "radharc"; - t (get_state_pathname(&settings)); - t (libred_init_colour()); - - return 0; - -fail: - libhaiku_perror(argv0); - libred_term_colour(); - return 1; -} - diff --git a/src/settings.c b/src/settings.c deleted file mode 100644 index 1c32a2f..0000000 --- a/src/settings.c +++ /dev/null @@ -1,294 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée - * - * 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 . - */ -#include "settings.h" -#include "arg.h" -#include "macros.h" -#include -#include -#include -#include - -#include - - - -/** - * Print usage information and exit if a condition is met. - * - * @param condition Do no do anything iff this is zero. - */ -static void -usage(int condition) -{ - if (!condition) return; - fprintf(stderr, - "Usage: %s [OPTIONS]...\n" - "See `man 1 radharc` for more information.\n", - !argv0 ? "radharc" : argv0), exit(2); -} - - -/** - * Parse a temperature string. - * - * @param str The temperature string. - * @param temp Output parameter for the temperature. - * Overflow is truncated. - * @param direction Unless `NULL`, will be set to +1 if `str` - * starts with a '+', `-1` if `str` starts - * with a '-', 0 otherwise. If `NULL` and - * `str` starts with a '-' or a '+', the - * function will fail. - * @param lower The function shall fail if the evaluated - * temperature is below this. - * @return 0 on success, -1 on error. - */ -static int -parse_temperature(const char *str, long int *temp, int *direction, int lower) -{ - char *end; - int dir = *str == '-' ? -1 : *str == '+' ? +1 : 0; - str += !!dir; - t (dir && !direction); - if (dir) *direction = dir; - t (!isdigit(*str)); - *temp = (errno = 0, strtol)(str, &end, 10); - t ((errno && !((errno == ERANGE) && (*temp == LONG_MAX))) || *end || (*temp < lower)); - return 0; -fail: - return -1; -} - - -/** - * Parse a string as a positively valued `struct timespec`. - * - * @param str The string. - * @param ts Output parameter for the value. - * @return 0 on success, -1 on error. - */ -static int -parse_timespec(const char *str, struct timespec *ts) -{ -#define DIGIT(var, c) var += (var) * 10 + ((c) & 15); -#define ALL_DIGITS(cond, var) while ((cond) && isdigit(*str)) DIGIT(var, *str++) - - int points = 0; - memset(ts, 0, sizeof(*ts)); - - /* Parse seconds. */ - t (!isdigit(*str)); - ALL_DIGITS(1, ts->tv_sec); - - /* End? */ - if (!*str) return 0; - t (*str != '.'); - - /* Parse nanoseconds.*/ - ALL_DIGITS(points++ < 9, ts->tv_nsec); - if ((points == 9) && isdigit(*str) && (*str++ >= '5') && (++(ts->tv_nsec) == 1000000000L)) - ts->tv_sec += 1, ts->tv_nsec = 0; - while (isdigit(*str)) str++; - t (*str); - - /* End! */ - return 0; -fail: - return -1; -} - - -/** - * Parse a latitude or a longitude value. - * - * @param str The string. - * @param loc Output parameter for the value. - * @param limit The limit of the absolute value. - * @return 0 on success, -1 on error. - */ -static int -parse_location(char *str, double *loc, double limit) -{ - char* end; - if (strstr(str, "−") == str) /* Support proper minus. */ - *(str += strlen("−") - 1) = '-'; - if ((*str != '-') && (*str != '+') && (*str != '.') && !isdigit(*str)) - return -1; - *loc = (errno = 0, strtod)(str, &end); - return -(errno || *loc || (fabs(*loc) > limit)); -} - - -/** - * Parse the command line. - * - * @param argc The number of elements in `argv`. - * @param argv The commnad line arguments including the zeroth elemenet. - * @param settings Output parameter for the settings. - */ -void -parse_command_line(int argc, char *argv[], struct settings *s) -{ - int location_set = 0; - char *p; - char *arg; - int c = 0; - - memset(s, 0, sizeof(*s)); - s->natural_temp = 6500; - s->day_temp = 5500; - s->night_temp = 3500; - s->trans_speed = 50; - -#define BOOLEAN(var) var = !plus; break -#define PLUS(...) (plus ? (__VA_ARGS__) : 0) - ARGBEGIN { - case 'l': - PLUS(location_set = 0); - usage(!(p = strchr(arg = ARGF(), ':'))); - *p++ = '\0', location_set = 1; - usage(parse_location(arg, &(s->latitude), 90.0)); - usage(parse_location(p, &(s->longitude), 180.0)); - break; - case 't': - PLUS(s->day_temp = 5500, s->night_temp = 3500); - s->temp = s->day_temp = s->night_temp = 0, s->temp_direction = 0; - if ((p = strchr(arg = ARGF(), ':'))) { - *p++ = '\0'; - usage(parse_temperature(arg, &(s->day_temp), NULL, 1000)); - usage(parse_temperature(p, &(s->night_temp), NULL, 1000)); - } else { - usage(parse_temperature(arg, &(s->temp), &(s->temp_direction), 1000)); - } - break; - case 'T': - PLUS(s->natural_temp = 6500); - usage(parse_temperature(ARGF(), &(s->natural_temp), NULL, 1000)); - break; - case 's': - PLUS(s->trans_speed = 50); - s->trans_speed = 0; - usage(parse_timespec(ARGF(), &(s->transition))); - break; - case 'S': - PLUS(s->trans_speed = 0); - usage(parse_temperature(ARGF(), &(s->trans_speed), NULL, 1)); - break; - case 'h': - s->hookpath = (plus ? NULL : ARGF()); - break; - case 'd': c++; /* Fall though. */ - case 'e': c++; /* Fall though. */ - case 'm': c++; - PLUS(s->monitors_n = 0, free(s->monitors_id), free(s->monitors_arg)); - xrealloc(&(s->monitors_id), s->monitors_n + 1); - xrealloc(&(s->monitors_arg), s->monitors_n + 1); - s->monitors_id[s->monitors_n] = ARGF(); - s->monitors_arg[s->monitors_n++] = (c == 3 ? 'm' : c == 2 ? 'e' : 'd'), c = 0; - break; - case 'p': BOOLEAN(s->print_status); - case 'n': BOOLEAN(s->panic_start); - case 'N': BOOLEAN(s->panic_else); - case 'o': BOOLEAN(s->set_and_exit); - case 'x': BOOLEAN(s->ignore_calib); - case 'i': BOOLEAN(s->negative); - case 'b': BOOLEAN(s->use_bus); - default: usage(1); break; - } ARGEND; - usage(argc); - - if (!location_set && !(s->temp)) - fprintf(stderr, - "%s: The -l option is mandatory, unless single value -t is used. " - "See `man 1 radharc` for more information.\n", - !argv0 ? "radharc" : argv0), exit(2); - - return; -fail: - libhaiku_perror(argv0 ? argv0 : "radharc"); - exit(1); -} - - -/** - * Marshal settings into a buffer. - * - * @param buffer The buffer, `NULL` if you want to know the required size. - * @param settings The settings to marshal. - * @return The size of the output. - */ -size_t -marshal_settings(char *buffer, const struct settings *settings) -{ -#define MARSHAL(N, DATUMP) (n += aux = (N), (buf ? (memcpy(buf, DATUMP, aux), buf += aux, 0) : 0)) - - size_t aux, n = 0, i = 0; - char *buf = buffer; - - if (buffer) i = marshal_settings(NULL, settings); - MARSHAL(sizeof(i), &i); - - MARSHAL(sizeof(*settings), settings); - if (settings->hookpath) MARSHAL(strlen(settings->hookpath) + 1, settings->hookpath); - if (settings->monitors_arg) MARSHAL(settings->monitors_n, settings->monitors_arg); - for (i = 0; i < settings->monitors_n; i++) - MARSHAL(strlen(settings->monitors_id[i]) + 1, settings->monitors_id[i]); - - return n; -} - - -/** - * Unmarshal settings from a buffer. - * - * @param buffer The buffer. - * @param settings Output parameter for the settings, will be allocated. - * @return The number of unmarshalled bytes, 0 on error. - */ -size_t -unmarshal_settings(char *buffer, struct settings **settings) -{ -#define UNMARSHAL(N, DATUMP) (aux = (N), memcpy((DATUMP), buf, aux), buf += aux) - - size_t n, aux, i; - struct settings s_, *s = NULL; - char *buf = buffer; - - UNMARSHAL(sizeof(n), &n); - if (!(s = *settings = malloc(n - sizeof(n)))) return 0; - s->monitors_id = NULL, s->monitors_arg = NULL; - - UNMARSHAL(sizeof(s_), &s_); - if (s_.monitors_n) { - *s = s_; - xmalloc(&(s->monitors_id), s_.monitors_n); - xmalloc(&(s->monitors_arg), s_.monitors_n); - } - - buf = strchr(s->hookpath = buf, '\0') + 1; - - UNMARSHAL(s_.monitors_n, s->monitors_arg); - for (i = 0; i < s_.monitors_n; i++) - UNMARSHAL(strlen(buf + 1), s->monitors_id[i]); - - return n; - -fail: - CLEANUP(free(s->monitors_id), free(s->monitors_arg), free(s), *settings = NULL); - return 0; -} - diff --git a/src/settings.h b/src/settings.h deleted file mode 100644 index 492e4b0..0000000 --- a/src/settings.h +++ /dev/null @@ -1,162 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée - * - * 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 . - */ -#include - - - -/** - * Settings from the command line. - */ -struct settings { - /** - * Print current status and exit? - */ - int print_status : 1; - - /** - * Start without transition? - */ - int panic_start : 1; - - /** - * Never transition, apart from at start? - */ - int panic_else : 1; - - /** - * Set temperature, possibly with transition, and exit? - */ - int set_and_exit : 1; - - /** - * Ignore calibrations? - */ - int ignore_calib : 1; - - /** - * Apply negative image filter? - */ - int negative : 1; - - /** - * Broadcast event with bus? - */ - int use_bus : 1; - - /** - * -1 to decrease temperature, - * +1 to increase temperature, - * 0 to set temperature. - */ - int temp_direction; - - /** - * The temperature, if use, the program will exit when it is done. - */ - long int temp; - - /** - * The temperature at full daytime. - */ - long int day_temp; - - /** - * The temperature at full night. - */ - long int night_temp; - - /** - * The temperature when disabled. - */ - long int natural_temp; - - /** - * Pathname to the hook script. - * Do not free this. - */ - char *hookpath; - - /** - * The number of seconds the transition takes. - */ - struct timespec transition; - - /** - * The number of kelvins per seconds the - * temperature is adjusted during transition. - */ - long int trans_speed; - - /** - * The user's latitudinal position. - */ - double latitude; - - /** - * The user's longitudinal position. - */ - double longitude; - - /** - * The number of elements in `monitors_id` and in `monitors_arg`. - */ - size_t monitors_n; - - /** - * Values for -d, -e, and -m, in order. - * Do not free its elements. - */ - char **monitors_id; - - /** - * The option (the character after the dash) - * the option used in correspnding element - * in `monitors_id`. - */ - char *monitors_arg; -}; - - - -/** - * Parse the command line. - * - * @param argc The number of elements in `argv`. - * @param argv The commnad line arguments including the zeroth elemenet. - * @param s Output parameter for the settings. - */ -void parse_command_line(int argc, char *argv[], struct settings *s); - - -/** - * Marshal settings into a buffer. - * - * @param param The buffer, `NULL` if you want to know the required size. - * @param settings The settings to marshal. - * @return The size of the output. - */ -size_t marshal_settings(char *buffer, const struct settings *settings); - -/** - * Unmarshal settings from a buffer. - * - * @param buffer The buffer. - * @param settings Output parameter for the settings, will be allocated. - * @return The number of unmarshalled bytes, 0 on error. - */ -size_t unmarshal_settings(char *buffer, struct settings **settings); - diff --git a/src/state.c b/src/state.c deleted file mode 100644 index e5c2a42..0000000 --- a/src/state.c +++ /dev/null @@ -1,352 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée - * - * 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 . - */ -#include "state.h" -#include "macros.h" -#include -#include -#include - -#include -#include - - - -/** - * The name of the process. - */ -extern char *argv0; - - - -/** - * All sites. - */ -static libgamma_site_state_t *sites = NULL; - -/** - * The number of sites stored in `sites`. - */ -static size_t sites_n = 0; - -/** - * All partitions. - */ -static libgamma_partition_state_t *parts = NULL; - -/** - * The number of partitions stored in `parts`. - */ -static size_t parts_n = 0; - - - -/** - * Is it daytime, night, perhaps some kind of twilight? - * - * @param elevation The Sun's apparent elevation. - * @return The time of the day. - */ -enum darkness -get_darkness(double elevation) -{ - if (elevation > LIBRED_SOLAR_ELEVATION_SUNSET_SUNRISE) return DAYTIME; - if (elevation > LIBRED_SOLAR_ELEVATION_CIVIL_DUSK_DAWN) return CIVIL_TWILIGHT; - if (elevation > LIBRED_SOLAR_ELEVATION_NAUTICAL_DUSK_DAWN) return NAUTICAL_TWILIGHT; - if (elevation > LIBRED_SOLAR_ELEVATION_ASTRONOMICAL_DUSK_DAWN) return ASTRONOMICAL_TWILIGHT; - return NIGHT; -} - - -/** - * Remove the screen number for a display server instance identifier. - * - * @param s Display server instance identifier. - * @return Set the value to which this pointer points to '.'. Do nothing if it is `NULL`. - */ -static char * -strip_screen(char *s) -{ -#define S(V, CD) ((V = ((CD)[1] == 'r' ? strrchr : strchr)(V, (CD)[0]))) - char *p = strchr(s, '='); - if ((p++) && (*p != '/') && S(p, ":r") && S(p, ".l")) *p = '\0'; else p = NULL; - return p; -#undef S -} - - -/** - * `stpmulcpy(o, a, b, c, NULL)` is equivalent to - * `stpcpy(stpcpy(stpcpy(o, a), b), c)` - */ -#ifdef __GNUC__ -__attribute__((__sentinel__)) -#endif -static char * -stpmulcpy(char *out, ... /*, NULL */) -{ - va_list args; - char *p = out; - const char *s; - va_start(args, out); - while ((s = va_arg(args, const char *))) p = stpcpy(p, s); - va_end(args); - return p; -} - - -/** - * Compare two display server environment strings. - * - * @param a_ One of the string. - * @param b_ The other string. - * @return -1, 0, or +1. - */ -static int -displayenvcmp(const void *a_, const void *b_) -{ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" -#endif - char *a = a_; - char *b = b_; -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif - char *p = strip_screen(a); - char *q = strip_screen(b); - int rc; - - rc = strcmp(a, b); - if (p) *p = '.'; - if (q) *q = '.'; - return rc; -} - - -/** - * Make an display string safe for a pathname. - * - * @param str The string. - * @return A safe version of the string, `NULL` on error. - */ -static char * -escape_display(const char* str) -{ - char *r, *w, *rc = NULL; - int s = 0; - xmalloc(&rc, 2 * strlen(str) + 1); strcpy(rc, str); - for (r = w = strchr(rc, '=') + 1; *r; r++) { - if (!s || (*r != '/')) { - if (strchr("@=/", *r)) *w++ = '@'; - *w++ = ((s = (*r == '/')) ? 's' : *r); - } - } - w[s ? -2 : 0] = '\0'; -fail: - return rc; -} - - -/** - * The string of display servers. - * - * @param settings The settings. - * @return The string, `NULL` on error. - */ -static char * -get_display_string(const struct settings *settings) -{ -#define DISPLAY(VAR, D) p = strip_screen(D); try (VAR = escape_display(D)); if (p) *p = '.', p = NULL - - const char *var, *val; - char *r, *p = NULL, *d = NULL, *rc = NULL, **displays; - size_t i, n = 0, len = 0; - int method; - - xmalloc(&displays, settings->monitors_n); - for (i = 0; i < settings->monitors_n; i++) - if ((settings->monitors_arg[i] == 'd') && strchr(settings->monitors_id[i], '=')) - len += 1 + strlen(displays[n++] = settings->monitors_id[i]); - if (n) goto custom; - free(displays), displays = NULL; - - if (!libgamma_list_methods(&method, 1, 0)) { - fprintf(stderr, "No display was found.\n" - "DRM support missing.\n" - "Can you even see?\n"); - return errno = 0, NULL; - } - - var = libgamma_method_default_site_variable(method); - val = libgamma_method_default_site(method); - if (!val || !*val) return strdup(""); - xmalloc(&d, 3 + strlen(var) + strlen(val)); - stpmulcpy(d, ".", var, "=", val, NULL); - DISPLAY(rc, d); - return free(d), rc; - -custom: - qsort(displays, n, sizeof(*displays), displayenvcmp); - xmalloc(&rc, 2 * len + 1); - for (r = rc, i = 0; i < n; i++) { - DISPLAY(d, displays[i]); - r = stpmulcpy(r, ".", d, NULL), free(d), d = NULL; - } - free(displays); - return rc; - -fail: - if (p) *p = '.'; - CLEANUP(free(rc), free(d), free(displays)); - return NULL; -} - - -/** - * Set $RADHARC_STATE. - * - * @param settings The settings. - * @return 0 on success, -1 on error. - */ -int -get_state_pathname(const struct settings *settings) -{ - const char *dir = getenv("XGD_RUNTIME_DIR"); - char *display = NULL; - char *env = NULL; - int rc = -1; - try (display = get_display_string(settings)); - if (!dir || !*dir) dir = "/run"; - xmalloc(&env, strlen(dir) + sizeof("/radharc/") + strlen(display)); - stpmulcpy(env, dir, "/radharc/", display, NULL); - rc = setenv("RADHARC_STATE", env, 1); -fail: - CLEANUP(free(env), free(display)); - return rc; -} - - -/** - * Parse a value for the -d option, or select preferred adjustment method. - * - * @param display The argument for the -d option. `NULL` for the preferred adjustment method. - * @return The adjustment method. -1 if not found. - */ -static int -get_clut_method(const char *display) -{ -#define HAIKU(TEXT) t ((msg = (TEXT))) -#define TEST(STR, ID) if (!strcasecmp(display, STR)) return ID - - const char *env, *msg; - int method; - - /* Default? */ - if (!display) { - if (!libgamma_list_methods(&method, 1, 0)) - HAIKU("No display was found.\n" - "DRM support missing.\n" - "Can you even see?\n"); - return method; - } - - /* Single-sited? */ - TEST("none", INT_MAX); - TEST("drm", LIBGAMMA_METHOD_LINUX_DRM); - - /* Unrecognised single-sited? */ - if (!strchr(display, '=')) - HAIKU("Specified display\n" - "cannot be recognised.\n" - "Try something else.\n"); - - /* Multi-sited? */ - for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) { - env = libgamma_method_default_site_variable(method); - if (env && (strstr(display, env) == display) && (display[strlen(env)] == '=')) - return method; - } - - /* Unrecognised multi-sited. */ - HAIKU("Specified display\n" - "cannot be recognised.\n" - "Try to recompile.\n"); - -fail: - fprintf(stderr, "%s", msg); - return errno = 0, -1; -} - - -/** - * Initialise the CLUT support. - * - * @param settings The settings. - * @return 0 on success, -1 on error. - */ -int -initialise_clut(const struct settings *settings) -{ -#define NONE_METHOD (method == INT_MAX) - - int method = 0, error = 0; - const char *sitename_; - char *sitename = NULL; - size_t i, j, parts_off = 0; - libgamma_site_state_t site; - - xcalloc(&sites, settings->monitors_n + 1); - - for (i = 0; i < settings->monitors_n; i++) { - switch (settings->monitors_arg[i]) { - case 'd': - parts_off = parts_n; - t ((method = get_clut_method(sitename_ = settings->monitors_id[i])) < 0); - if (NONE_METHOD) break; - sitename_ = strchr(sitename_, '='); - xstrdup(&sitename, sitename_ ? sitename_ + 1 : NULL); - t ((error = libgamma_site_initialise(sites + sites_n, method, sitename))); - sitename = NULL, site = sites[sites_n++]; - xrealloc(&parts, parts_n + site.partitions_available); - for (j = 0; j < site.partitions_available; j++) { - t ((error = libgamma_partition_initialise(parts + parts_n, &site, j))); - parts_n++; - } - break; - - case 'm': - if (NONE_METHOD) break; - /* TODO -m */ - break; - - case 'e': - if (NONE_METHOD) break; - /* TODO -e */ - break; - } - } - - SHRINK(&sites, sites_n + 1); - - return 0; -fail: - if (error) libgamma_perror(argv0, error), errno = 0; - CLEANUP(free(sitename)); - return -1; -} - diff --git a/src/state.h b/src/state.h deleted file mode 100644 index f2c6b11..0000000 --- a/src/state.h +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée - * - * 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 . - */ -#include "settings.h" - - - -/** - * The times of the day, by degree of darkness. - */ -enum darkness { - /** - * Not calculated yet. - */ - UNKNOWN = -1, - - /** - * Hopefully, it is bright outside. - */ - DAYTIME = 0, - - /** - * The sky is golden. The golden "hour". - * Also known as BMCT (dawn) or EECT (dusk). - */ - CIVIL_TWILIGHT = 1, - - /** - * The sky is pink and blue. - */ - NAUTICAL_TWILIGHT = 2, - - /** - * The sky is medium dark blue. - */ - ASTRONOMICAL_TWILIGHT = 3, - - /** - * The sky is really dark blue. - */ - NIGHT = 4 -}; - - - -/** - * Is it daytime, night, perhaps some kind of twilight? - * - * @param elevation The Sun's apparent elevation. - * @return The time of the day. - */ -enum darkness get_darkness(double elevation); - -/** - * Set $RADHARC_STATE. - * - * @param settings The settings. - * @return 0 on success, -1 on error. - */ -int get_state_pathname(const struct settings *settings); - -- cgit v1.2.3-70-g09d2