pax_global_header00006660000000000000000000000064135555703550014527gustar00rootroot0000000000000052 comment=b914819dd4338fd29212ef65cab438b103b68f69 light-1.2.1/000077500000000000000000000000001355557035500126375ustar00rootroot00000000000000light-1.2.1/.gitignore000066400000000000000000000003041355557035500146240ustar00rootroot00000000000000*~ *.o .deps GPATH GRTAGS GTAGS light light.1.gz aclocal.m4 autom4te.cache compile config.h config.h.in config.log config.status configure depcomp install-sh missing stamp-h1 Makefile Makefile.in light-1.2.1/90-backlight.rules000066400000000000000000000005601355557035500160720ustar00rootroot00000000000000ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness" ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness" ACTION=="add", SUBSYSTEM=="leds", RUN+="/bin/chgrp video /sys/class/leds/%k/brightness" ACTION=="add", SUBSYSTEM=="leds", RUN+="/bin/chmod g+w /sys/class/leds/%k/brightness" light-1.2.1/COPYING000066400000000000000000001045131355557035500136760ustar00rootroot00000000000000 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 . light-1.2.1/ChangeLog.md000066400000000000000000000074271355557035500150220ustar00rootroot00000000000000Change Log ========== All relevant changes to the project are documented in this file. [v1.2][] - 2018-09-23 --------------------- ### Changes - Converted to GNU configure & build system - Major rewrite to add a device system, folds in all kinds of display/keyboard/LED controllers under one roof. Note, this change break command line options from previous releases - Support for installing as non-SUID root using an udev rule enabled `--with-udev` to the new configure script - Migrated to use `~/.cache/light` instead of `/etc/light` for unpriviliged operation. Respects XDG_CACHE_HOME if set - Added proper light.1 man page, remvoes help2man dependency - Update presentation of commands and options in built-in help text, as well as in the README and man page - Overhaul of coding style, see DOCUMENTATION.md for details - Add Fedora installation instructions [v1.1.2][] - 2018-06-20 ----------------------- Panic release to fix save/restore. ### Changes - Add help2man dependency in README - Better Support for Overriding Install Prefix - Restore DESTDIR support ### Fixes - Issue #29: Fix save and restore arguments - Issue #27: Use the install command instead of raw cp/mv/chmod. [v1.1][] - 2017-11-23 --------------------- Various fixes and improvements. Credits to Abdullah ibn Nadjo ### Changes - Add `-k` flag for keyboard backlight support - Cache max brightness data from automatic controller detection - Improve overall logging - Logging of clamps, saves and restores - Support for save, restore, get [max] brightness etc. for both screen and keyboard controllers ### Fixes - Avoid checking for write permission if just getting value - Check if controller is accessible before getting value - Avoid redondant checking - Don't truncate file contents when checking if file is writable - Fix `light_controllerAccessible()` and `light_getBrightness()` this functions were: - Reading values from the controller - Checking write permission even when we just want reading values - Checking the mincap file instead of the actual controller - Don't try to read brightness values when only targetting max bright - Fix issues with string buffers and pointers - Use `NAME_MAX` and `PATH_MAX` instead of hardcoded values - Allow paths to be longer than 256 chars - Check pointers everywhere - Use `strncpy()`/`snprintf()` instead of `strcpy()`/`sprintf()` - Validate controllers' name (`-s` flag + a very long name = bad things happening) - Get rid of globals for dir iteration [v1.0][] - 2016-05-10 --------------------- First major release. Light has been around for a while now and seems to make some people happy. Also someone wanted a new release, so here you go! ### Changes - Added save/restore functionality - Generate man page on `make install` ### Fixes - Issue #5: Can't increase brightness on ATI propietary driver - Issue #10: Honor `$DESTDIR` on man page installation [v0.9][] - 2014-06-08 --------------------- ### Changes - Complete rewrite of program (Every single byte) - Cleaner, safer code - Completely new intuitive usage (Sorry, it was needed) - Added functionality: - Ability to set/get minimum brightness directly from commandline - Ability to specify the backlight controller to use directly from commandline - Better verbosity - Probably missed some stuff v0.7 - 2012-11-18 ----------------- ### Changes - Ported bash script to C [UNRELEASED]: https://github.com/haikarainen/light/compare/v1.2...HEAD [v1.2]: https://github.com/haikarainen/light/compare/v1.1.2...v1.2 [v1.1.2]: https://github.com/haikarainen/light/compare/v1.1...v1.1.2 [v1.1]: https://github.com/haikarainen/light/compare/v1.0...v1.1 [v1.0]: https://github.com/haikarainen/light/compare/v0.9...v1.0 [v0.9]: https://github.com/haikarainen/light/compare/v0.7...v0.9 light-1.2.1/DOCUMENTATION.md000066400000000000000000000147371355557035500152060ustar00rootroot00000000000000# Developer Instructions This file is aimed at developers of light, or developers who want to implement "drivers" (enumerators) for their own hardware. ## Coding Standards Light is a small project, which helps keep it clean. However we still would like to see a consistent styling throughout the project, as well as in third-party enumerator implementations. The actual source code may not be fully "up to code" yet, but it's getting there. We use 4 spaces for indentation. We have an empty line at the top and bottom of each file. The following two sources should be clear enough examples of our coding style: ### Header files ```c #pragma once #include #include #include /* foo_type_t */ typedef struct _some_struct_t some_struct_t; struct _some_struct_t { uint64_t id; foo_type_t my_foo_thing; foo_type_t *my_foo_ptr; } /* Describe what the purpose of this function is, what it does with/to foo_struct, and what it returns. */ bool do_some_stuff(some_struct_t *foo_struct); ``` ### Source files The second line of each source file should be the include to the corresponding header file, followed by an empty line. Internal/static functions are always prefixed with an underscore (_). ```c #include "some_struct.h" static void _increment_one(uint64_t *out_value) { *out_value += 1; } bool do_some_stuff(some_struct_t *foo_struct) { _increment_one(foo_struct->id); if(foo_struct->id > 33) { return false; } if(foo_struct->my_foo_ptr != NULL) { free(foo_struct->my_foo_ptr); } foo_struct->my_foo_ptr = malloc(sizeof(foo_type_t)); return true; } ``` ## Implementing an enumerator Implementing your own devices through an enumerator is pretty easy. The required steps are as follows: ### Step 1 Create a headerfile and a corresponding sourcefile under the `impl` folder, Call them `foo.h` and `foo.c`. Open up the `sysfs.c` and `sysfs.h` files for reference implementations. ### Step 2 In the header, you need to first do a `#pragma once` (obviously), then `#include "light.h"` to get access to some struct declarations, then at the bare minimum declare 6 functions. If you need to store your own data for each device or device-target, you will need to declare structs for these as well. ```c #pragma once #include "light.h" // Implementation of the foo enumerator // Enumerates devices for quacking ducks // Device target data struct _impl_foo_data_t { int32_t internal_quack_id; }; typedef struct _impl_foo_data_t impl_foo_data_t; bool impl_foo_init(light_device_enumerator_t *enumerator); bool impl_foo_free(light_device_enumerator_t *enumerator); bool impl_foo_set(light_device_target_t *target, uint64_t in_value); bool impl_foo_get(light_device_target_t *target, uint64_t *out_value); bool impl_foo_getmax(light_device_target_t *target, uint64_t *out_value); bool impl_foo_command(light_device_target_t *target, char const *command_string); ``` ### Step 3 In the sourcefile, you need to implement the 6 methods. Make sure to return `true` on success and `false` on failure. If you do not actually implement a function (for example `impl_foo_command`), just return `true`. The job of the enumerator is to identify/enumerate a bunch of different devices (or just one, or even zero if it doesnt find any). You are also responsible to create the device targets for them (i.e, the things that you actually write to on the device). You do this by setting the devices and targets up in `impl_foo_init`. You are not required to do anything in `impl_foo_free`, any allocated memory will be automatically free'd by light, including device/target data that you allocate yourself. You may use `impl_foo_free` to free resources you allocate outside of the light API. ```c #include "impl/foo.h" #include "light.h" #include "helpers.h" bool impl_foo_init(light_device_enumerator_t *enumerator) { /* Lets create a single device, with a single target, for simplicity */ /* Create a new device called new_device_name, we dont need any userdata so pass NULL to the device_data parameter */ light_device_t *new_device = light_create_device(enumerator, "new_device_name", NULL) /* Setup userdata specific to the target we will create*/ /* Useful to for example reference an ID in a third-party API or likewise */ /* NOTE: The userdata will be free()'d automatically on exit, so you do not need to free it yourself */ impl_foo_data_t *custom_data = malloc(sizeof(impl_foo_data_t)); custom_data->internal_quack_id = 333; /* Create a new device target called new_target_name, and pass in the functions and userdata that we just allocated */ light_create_device_target(new_device, "new_target_name", impl_foo_set, impl_foo_get, impl_foo_getmax, impl_foo_command, custom_data) /* Return true because we didnt get any errors! */ return true; } bool impl_foo_free(light_device_enumerator_t *enumerator) { /* We dont need to do anything here, but if we want to, we can free some third-party API resources */ return true; } /* Implement the other functions to do their thing. Get, Set and GetMax should be self-explanatory. Command is reserved for future use, but basically will allow the user to run custom commands on a target. */ ``` ### Step 4 Now that you have implemented your enumerator, it is time to inject it to the application itself. You will be able to compile your enumerator into a plugin in the future, but for now, locate the `light_initialize` function inside `light.c`. You will see some calls (perhaps just one call) to `light_create_enumerator` inside of this function. Add one more call to this function to register your enumerator in the application: The first argument is the application context, the second is the name that your enumerator will get, and the last two are the init and free functions that we implemented. ```c light_create_enumerator(new_ctx, "foo", &impl_foo_init, &impl_foo_free); ``` Once you do this, you should be able to find your device target when running `light -L`, and it should be called something like `foo/new_device_name/new_target_name` if you followed this guide. The only thing left now is to create a pull request so that the rest of the world can share the functionality that you just implemented! ## Troubleshooting If you run into any issues, feel free to create a new Github issue, even if you are just asking for "support" or likewise. light-1.2.1/Makefile.am000066400000000000000000000016141355557035500146750ustar00rootroot00000000000000SUBDIRS = src dist_man1_MANS = light.1 doc_DATA = README.md COPYING ChangeLog.md EXTRA_DIST = README.md COPYING ChangeLog.md if UDEV udev_DATA = 90-backlight.rules EXTRA_DIST += $(top_srcdir)/90-backlight.rules endif # lintian --profile debian -i -I --show-overrides ../$PKG.changes deb: dpkg-buildpackage -uc -us -B # # Target to run when building a release # release: distcheck @for file in $(DIST_ARCHIVES); do \ md5sum $$file > ../$$file.md5; \ done @mv $(DIST_ARCHIVES) ../ @echo @echo "Resulting release files:" @echo "=================================================================" @for file in $(DIST_ARCHIVES); do \ printf "%-32s Distribution tarball\n" $$file; \ printf "%-32s " $$file.md5; cat ../$$file.md5 | cut -f1 -d' '; \ done @for file in `cd ..; ls $(PACKAGE)_$(VERSION)*`; do \ printf "%-32s Debian/Ubuntu file\n" $$file; \ done light-1.2.1/README.md000066400000000000000000000137741355557035500141320ustar00rootroot00000000000000Light - A program to control backlights (and other hardware lights) in GNU/Linux ================================================== *Copyright (C) 2012 - 2018* *Author: Fredrik Haikarainen* *Contributor & Maintainer: Joachim Nilsson* *This is free software, see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE* - [Introduction](#introduction) - [Examples](#examples) - [Usage](#usage) - [Command options](#command-options) - [Extra options](#extra-options) - [Installation](#installation) - [Arch Linux](#arch-linux) - [Fedora](#fedora) - [Manual](#manual) - [Permissions](#permissions) - [Origin & References](#origin--references) Introduction ------------ [Light][] is a program to control backlights and other lights under GNU/Linux: * Works where other software has proven unreliable (xbacklight etc.) * Works even in a fully CLI-environment, i.e. it does not rely on X * Provides functionality to automatically control backlights with the highest precision available * Extra features, like setting a minimum brightness value for controllers, or saving/restoring the value for poweroffs/boots. See the following sections for the detailed descriptions of all available commands, options and how to access different controllers. Light is available in many GNU/Linux distributions already. Examples -------- Get the current backlight brightness in percent light -G or light Increase backlight brightness by 5 percent light -A 5 Set the minimum cap to 2 in raw value on the sysfs/backlight/acpi_video0 device: light -Nrs "sysfs/backlight/acpi_video0" 2 List available devices light -L Activate the Num. Lock keyboard LED, here `sysfs/leds/input3::numlock` is used, but this varies between different systems: light -Srs "sysfs/leds/input3::numlock" 1 Usage ----- Usage follows the following pattern, where options are optional and the neccesity of value depends on the options used light [options] ### Command options You may only specify one command flag at a time. These flags decide what the program will ultimately end up doing. * `-H` Show help and exit * `-V` Show program version and exit * `-L` List available devices * `-A` Increase brightness by value (value needed!) * `-U` Decrease brightness by value (value needed!) * `-S` Set brightness to value (value needed!) * `-G` Get brightness * `-N` Set minimum brightness to value (value needed!) * `-P` Get minimum brightness * `-O` Save the current brightness * `-I` Restore the previously saved brightness Without any extra options, the command will operate on the device called `sysfs/backlight/auto`, which works as it's own device however it proxies the backlight device that has the highest controller resolution (read: highest precision). Values are interpreted and printed as percentage between 0.0 - 100.0. **Note:** If something goes wrong, you can find out by maxing out the verbosity flag by passing `-v 3` to the options. This will activate the logging of warnings, errors and notices. Light will never print these by default, as it is designed to primarily interface with other applications and not humanbeings directly. ### Extra options These can be mixed, combined and matched after convenience. * `-r` Raw mode, values (printed and interpreted from commandline) will be treated as integers in the controllers native range, instead of in percent. * `-v ` Specifies the verbosity level. 0 is default and prints nothing. 1 prints only errors, 2 prints only errors and warnings, and 3 prints both errors, warnings and notices. * `-s ` Specifies which device to work on. List available devices with the -L command. Full path is needed. Installation ------------ ### Arch Linux The latest stable release is available in official repos, install with: pacman -S light Additionally, the latest development branch (master) is available on AUR: [light-git][] ### Fedora Fedora already has light packaged in main repos, so just run: dnf install light and you're good to go. ### Debian/Ubuntu Pre-built .deb files, for the latest Ubuntu release, can be downloaded from the [GitHub](https://github.com/haikarainen/light/releases/) releases page. If you want to build your own there is native support available in the GIT sources. Clone and follow the development branch guidelines below followed by: make deb ### NixOS/nix You can add the following line to your `configuration.nix`: programs.light.enable = true; For more detail on Backlight control in NixOS and setting system keybindings, visit the [NixOS Wiki page](https://nixos.wiki/wiki/Backlight) ### Manual If you download a stable release, these are the commands that will get you up and running: tar xf light-x.yy.tar.gz cd light-x.yy/ ./configure && make sudo make install However the latest development branch requires some extras. Clone the repository and run the `autogen.sh` script. This requires that `automake` and `autoconf` is installed on your system. ./autogen.sh ./configure && make sudo make install The `configure` script and `Makefile.in` files are not part of GIT because they are generated at release time with `make release`. ### Permissions Optionally, instead of the classic SUID root mode of operation, udev rules can be set up to manage the kernel sysfs permissions. Use the configure script to enable this mode of operation: ./configure --with-udev && make sudo make install This installs the `90-backlight.rules` into `/usr/lib/udev/rules.d/`. If your udev rules are located elsewhere, use `--with-udev=PATH`. **Note:** make sure that your user is part of the `video` group, otherwise you will not get access to the devices. **Note:** in this mode `light` runs unpriviliged, so the `/etc/light` directory (for cached settings) is not used, instead the per-user specific `~/.config/light` is used. [Light]: https://github.com/haikarainen/light/ [light-git]: https://aur.archlinux.org/packages/light-git light-1.2.1/autogen.sh000077500000000000000000000000541355557035500146370ustar00rootroot00000000000000#!/bin/sh autoreconf -W portability -visfm light-1.2.1/configure.ac000066400000000000000000000015661355557035500151350ustar00rootroot00000000000000AC_INIT([light], [1.2], [https://github.com/haikarainen/light/issues]) AM_INIT_AUTOMAKE([1.11 foreign subdir-objects]) AM_SILENT_RULES([yes]) AC_CONFIG_SRCDIR([src/light.c]) AC_CONFIG_HEADER([config.h]) AC_CONFIG_FILES([Makefile src/Makefile]) AC_PROG_CC AC_PROG_INSTALL AC_HEADER_STDC AC_ARG_WITH([udev], AS_HELP_STRING([--with-udev@<:@=PATH@:>@], [use udev instead of SUID root, optional rules.d path]), [udev=$withval], [udev=no]) AC_MSG_CHECKING(for udev rules.d) AS_IF([test "x$udev" != "xno"], [ AS_IF([test "x$udev" = "xyes"], [ udevdir="\${prefix}/lib/udev/rules.d" ],[ udevdir="$udev" ]) AC_SUBST(udevdir) AC_MSG_RESULT([$udevdir]) ],[ AC_MSG_RESULT([disabled, classic SUID root mode]) ]) # Allow classic SUID root behavior if udev rule is not used AM_CONDITIONAL(UDEV, [test "x$udev" != "xno"]) AM_CONDITIONAL(CLASSIC, [test "x$udev" = "xno"]) AC_OUTPUT light-1.2.1/debian/000077500000000000000000000000001355557035500140615ustar00rootroot00000000000000light-1.2.1/debian/.gitignore000066400000000000000000000000521355557035500160460ustar00rootroot00000000000000files light.debhelper.log light.substvars light-1.2.1/debian/changelog000066400000000000000000000002141355557035500157300ustar00rootroot00000000000000light (1.2) unstable; urgency=low * Initial Debian Release. -- Joachim Nilsson Sat, 22 Sep 2018 11:53:32 +0200 light-1.2.1/debian/compat000066400000000000000000000000031355557035500152600ustar00rootroot0000000000000010 light-1.2.1/debian/control000066400000000000000000000017361355557035500154730ustar00rootroot00000000000000Source: light Section: x11 Priority: optional Maintainer: Joachim Nilsson Homepage: https://github.com/haikarainen/light Build-Depends: debhelper (>= 10) Vcs-Git: https://github.com/haikarainen/light.git Vcs-Browser: https://github.com/haikarainen/light/commits/ Standards-Version: 4.2.1 Package: light Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} Description: Control display backlight controllers and LEDs Light is a useful tool to control display brightness in lightweight desktops or window managers that do not have bundled appliations for this purpose. . Most modern laptops have moved away from hardware controlled brightness and require software control. Light works where other software has proven to be unreliable, e.g. xbacklight. It can even be used from the console as it does not rely on X. . Light has features like setting a minimum brightness value, as well as saving and restoring the brightness at reboot and startup. light-1.2.1/debian/copyright000066400000000000000000000022251355557035500160150ustar00rootroot00000000000000Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: light Source: https://github.com/haikarainen/light Files: * Copyright: Copyright 2012-2018 Fredrik Haikarainen License: GPL-3 Files: debian/* Copyright: Copyright 2018 Joachim Nilsson License: GPL-3 License: GPL-3 Indicator Weather is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. . Indicator Weather 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. http://www.gnu.org/licenses/ . You should have received a copy of the GNU General Public License along with this package; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA . On Debian systems, the full text of the GNU General Public License version 3 can be found in the file `/usr/share/common-licenses/GPL-3'.light-1.2.1/debian/docs000066400000000000000000000000121355557035500147250ustar00rootroot00000000000000README.md light-1.2.1/debian/rules000077500000000000000000000006121355557035500151400ustar00rootroot00000000000000#!/usr/bin/make -f # export DH_VERBOSE=1 export DEB_BUILD_MAINT_OPTIONS = hardening=+all %: dh $@ override_dh_auto_configure: dh_auto_configure -- --with-udev=/lib/udev/rules.d override_dh_installchangelogs: dh_installchangelogs ChangeLog.md override_dh_auto_install: dh_auto_install rm -f debian/light/usr/share/doc/light/COPYING rm -f debian/light/usr/share/doc/light/ChangeLog.md light-1.2.1/debian/source/000077500000000000000000000000001355557035500153615ustar00rootroot00000000000000light-1.2.1/debian/source/format000066400000000000000000000000151355557035500165700ustar00rootroot000000000000003.0 (native) light-1.2.1/debian/source/options000066400000000000000000000000171355557035500167750ustar00rootroot00000000000000compression=xz light-1.2.1/debian/watch000066400000000000000000000002271355557035500151130ustar00rootroot00000000000000version=3 opts=filenamemangle=s/.+\/v?(\d\S*)\.tar\.gz/light-$1\.tar\.gz/ \ https://github.com/haikarainen/light/releases .*/light-?(\d\S*)\.tar\.gz light-1.2.1/light.1000066400000000000000000000037131355557035500140340ustar00rootroot00000000000000.\" -*- nroff -*- .Dd August 4, 2018 .Os GNU/Linux .Dt LIGHT 1 URM .Sh NAME .Nm light .Nd a program to control backlight controllers .Sh SYNOPSIS .Nm light .Op Ar OPTIONS .Op Ar VALUE .Sh DESCRIPTION .Nm is a program to control backlight display and keyboard controllers under GNU/Linux. .Pp .Bl -bullet -compact .It Operates independently of X (X-Window) .It Can automatically figure out the best controller to use, making full use of the underlying hardware .It Supports a minimum cap on the brightness value, as some controllers set the display to be pitch black at a vaĺue of 0 (or higher) .El .Sh COMMANDS The following unique commands are supported: .Pp .Bl -tag -width Ds .It Fl H , Fl h Show help text and exit .It Fl V Show program version and exit .It Fl L List available devices .It Fl A Increase brightness by value .It Fl U Decrease brightness by value .It Fl S Set brightness to value .It Fl G Get brightness, default .It Fl N Set minimum brightness to value .It Fl P Get minimum brightness .It Fl O Save current brightness .It Fl I Restore previously saved brightness .El .Sh OPTIONS The behavior of the above commands can be modified using these options: .Pp .Bl -tag -width Ds .It Fl r Interpret input and output values in raw mode .It Fl s Ar PATH Specify device target path. Use .Fl L to list available devices .It Fl v Ar LEVEL Set verbosity level, by default .Nm only outputs read values: .Pp .Bl -tag -width 0: -compact .It 0: Read values .It 1: Read values, Errors .It 2: Read values, Errors, Warnings .It 3: Read values, Errors, Warnings, Notices .El .El .Sh FILES When run in its classic SUID root mode .Nm caches settings and current brightness in .Pa /etc/light . In its non-privileged mode of operation the .Pa ~/.cache/light directory is used instead. .Sh AUTHORS Copyright \(co 2012-2018 Fredrik Haikarainen .Pp This is free software, see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE light-1.2.1/src/000077500000000000000000000000001355557035500134265ustar00rootroot00000000000000light-1.2.1/src/Makefile.am000066400000000000000000000006011355557035500154570ustar00rootroot00000000000000bin_PROGRAMS = light light_SOURCES = main.c light.c light.h helpers.c helpers.h impl/sysfs.c impl/sysfs.h impl/util.h impl/util.c impl/razer.h impl/razer.c light_CPPFLAGS = -I../include -D_GNU_SOURCE light_CFLAGS = -W -Wall -Wextra -std=gnu99 -Wno-type-limits -Wno-format-truncation -Wno-unused-parameter if CLASSIC install-exec-hook: chmod 4755 $(DESTDIR)$(bindir)/light endif light-1.2.1/src/helpers.c000066400000000000000000000053261355557035500152420ustar00rootroot00000000000000#include "helpers.h" #include #include #include #include // access #include #include #include // errno #include // dirname bool light_file_read_uint64(char const *filename, uint64_t *val) { FILE *fp; uint64_t data; fp = fopen(filename, "r"); if(!fp) { LIGHT_PERMERR("reading"); return false; } if(fscanf(fp, "%lu", &data) != 1) { LIGHT_ERR("Couldn't parse an unsigned integer from '%s'", filename); fclose(fp); return false; } *val = data; fclose(fp); return true; } bool light_file_write_uint64(char const *filename, uint64_t val) { FILE *fp; fp = fopen(filename, "w"); if(!fp) { LIGHT_PERMERR("writing"); return false; } if(fprintf(fp, "%lu", val) < 0) { LIGHT_ERR("fprintf failed"); fclose(fp); return false; } fclose(fp); return true; } bool light_file_exists (char const *filename) { return access( filename, F_OK ) != -1; } /* Returns true if file is writable, false otherwise */ bool light_file_is_writable(char const *filename) { FILE *fp; fp = fopen(filename, "r+"); if(!fp) { LIGHT_PERMWARN("writing"); return false; } fclose(fp); return true; } /* Returns true if file is readable, false otherwise */ bool light_file_is_readable(char const *filename) { FILE *fp; fp = fopen(filename, "r"); if(!fp) { LIGHT_PERMWARN("reading"); return false; } fclose(fp); return true; } /* Prints a notice about a value which was below `x` and was adjusted to it */ uint64_t light_log_clamp_min(uint64_t min) { LIGHT_NOTE("too small value, adjusting to minimum %lu (raw)", min); return min; } /* Prints a notice about a value which was above `x` and was adjusted to it */ uint64_t light_log_clamp_max(uint64_t max) { LIGHT_NOTE("too large value, adjusting to maximum %lu (raw)", max); return max; } /* Clamps the `percent` value between 0% and 100% */ double light_percent_clamp(double val) { if(val < 0.0) { LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val); return 0.0; } if(val > 100.0) { LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val); return 100.0; } return val; } int light_mkpath(char *dir, mode_t mode) { struct stat sb; if(!dir) { errno = EINVAL; return -1; } if(!stat(dir, &sb)) return 0; char *tempdir = strdup(dir); light_mkpath(dirname(tempdir), mode); free(tempdir); return mkdir(dir, mode); } light-1.2.1/src/helpers.h000066400000000000000000000034131355557035500152420ustar00rootroot00000000000000 #pragma once #include #include #include #include /* Clamps x(value) between y(min) and z(max) in a nested ternary operation */ #define LIGHT_CLAMP(val, min, max) (val < min ? light_log_clamp_min(min) : (val > max ? light_log_clamp_max(max) : val)) /* Verbosity levels: * 0 - No output * 1 - Errors * 2 - Errors, warnings * 3 - Errors, warnings, notices */ typedef enum { LIGHT_ERROR_LEVEL = 1, LIGHT_WARN_LEVEL, LIGHT_NOTE_LEVEL } light_loglevel_t; light_loglevel_t light_loglevel; #define LIGHT_LOG(lvl, fp, fmt, args...)\ if(light_loglevel >= lvl)\ fprintf(fp, "%s:%d:" fmt "\n", __FILE__, __LINE__, ##args) #define LIGHT_NOTE(fmt, args...) LIGHT_LOG(LIGHT_NOTE_LEVEL, stdout, " Notice: " fmt, ##args) #define LIGHT_WARN(fmt, args...) LIGHT_LOG(LIGHT_WARN_LEVEL, stderr, " Warning: " fmt, ##args) #define LIGHT_ERR(fmt, args...) LIGHT_LOG(LIGHT_ERROR_LEVEL, stderr, " Error: " fmt, ##args) #define LIGHT_MEMERR() LIGHT_ERR("memory error"); #define LIGHT_PERMLOG(act, log)\ do {\ log("could not open '%s' for " act, filename);\ log("Verify it exists with the right permissions");\ } while(0) #define LIGHT_PERMERR(x) LIGHT_PERMLOG(x, LIGHT_ERR) #define LIGHT_PERMWARN(x) LIGHT_PERMLOG(x, LIGHT_WARN) bool light_file_write_uint64 (char const *filename, uint64_t val); bool light_file_read_uint64 (char const *filename, uint64_t *val); bool light_file_exists (char const *filename); bool light_file_is_writable (char const *filename); bool light_file_is_readable (char const *filename); uint64_t light_log_clamp_min(uint64_t min); uint64_t light_log_clamp_max(uint64_t max); double light_percent_clamp(double percent); int light_mkpath(char *dir, mode_t mode); light-1.2.1/src/impl/000077500000000000000000000000001355557035500143675ustar00rootroot00000000000000light-1.2.1/src/impl/razer.c000066400000000000000000000076051355557035500156660ustar00rootroot00000000000000 #include "impl/razer.h" #include "light.h" #include "helpers.h" #include //snprintf #include // malloc, free #include // opendir, readdir static void _impl_razer_add_target(light_device_t *device, char const *name, char const *filename, uint64_t max_brightness) { impl_razer_data_t *target_data = malloc(sizeof(impl_razer_data_t)); snprintf(target_data->brightness, sizeof(target_data->brightness), "/sys/bus/hid/drivers/razerkbd/%s/%s", device->name, filename); target_data->max_brightness = max_brightness; // Only add targets that actually exist, as we aren't fully sure exactly what targets exist for a given device if(light_file_exists(target_data->brightness)) { light_create_device_target(device, name, impl_razer_set, impl_razer_get, impl_razer_getmax, impl_razer_command, target_data); } else { LIGHT_WARN("razer: couldn't add target %s to device %s, the file %s doesn't exist", name, device->name, filename); // target_data will not be freed automatically if we dont add a device target with it as userdata, so free it here free(target_data); } } static void _impl_razer_add_device(light_device_enumerator_t *enumerator, char const *device_id) { // Create a new razer device light_device_t *new_device = light_create_device(enumerator, device_id, NULL); // Setup a target to backlight _impl_razer_add_target(new_device, "backlight", "matrix_brightness", 255); // Setup targets to different possible leds _impl_razer_add_target(new_device, "game_led", "game_led_state", 1); _impl_razer_add_target(new_device, "macro_led", "macro_led_state", 1); _impl_razer_add_target(new_device, "logo_led", "logo_led_state", 1); _impl_razer_add_target(new_device, "profile_led_r", "profile_led_red", 1); _impl_razer_add_target(new_device, "profile_led_g", "profile_led_green", 1); _impl_razer_add_target(new_device, "profile_led_b", "profile_led_blue", 1); } bool impl_razer_init(light_device_enumerator_t *enumerator) { // Iterate through the led controllers and create a device_target for each controller DIR *razer_dir; struct dirent *curr_entry; if((razer_dir = opendir("/sys/bus/hid/drivers/razerkbd/")) == NULL) { // Razer driver isnt properly installed, so we cant add devices in this enumerator return true; } while((curr_entry = readdir(razer_dir)) != NULL) { // Skip dot entries if(curr_entry->d_name[0] == '.') { continue; } _impl_razer_add_device(enumerator, curr_entry->d_name); } closedir(razer_dir); return true; } bool impl_razer_free(light_device_enumerator_t *enumerator) { return true; } bool impl_razer_set(light_device_target_t *target, uint64_t in_value) { impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data; if(!light_file_write_uint64(data->brightness, in_value)) { LIGHT_ERR("failed to write to razer device"); return false; } return true; } bool impl_razer_get(light_device_target_t *target, uint64_t *out_value) { impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data; if(!light_file_read_uint64(data->brightness, out_value)) { LIGHT_ERR("failed to read from razer device"); return false; } return true; } bool impl_razer_getmax(light_device_target_t *target, uint64_t *out_value) { impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data; *out_value = data->max_brightness; return true; } bool impl_razer_command(light_device_target_t *target, char const *command_string) { // No current need for custom commands in sysfs enumerator // To implement support, simply parse the command string to your liking, and return false on invalid input or results! return true; } light-1.2.1/src/impl/razer.h000066400000000000000000000013711355557035500156650ustar00rootroot00000000000000 #pragma once #include "light.h" // Implementation of the razer enumerator // Enumerates devices for the openrazer driver https://github.com/openrazer/openrazer // Device target data struct _impl_razer_data_t { char brightness[NAME_MAX]; uint64_t max_brightness; }; typedef struct _impl_razer_data_t impl_razer_data_t; bool impl_razer_init(light_device_enumerator_t *enumerator); bool impl_razer_free(light_device_enumerator_t *enumerator); bool impl_razer_set(light_device_target_t *target, uint64_t in_value); bool impl_razer_get(light_device_target_t *target, uint64_t *out_value); bool impl_razer_getmax(light_device_target_t *target, uint64_t *out_value); bool impl_razer_command(light_device_target_t *target, char const *command_string); light-1.2.1/src/impl/sysfs.c000066400000000000000000000132141355557035500157030ustar00rootroot00000000000000 #include "impl/sysfs.h" #include "light.h" #include "helpers.h" #include //snprintf #include // malloc, free #include // opendir, readdir static bool _impl_sysfs_init_leds(light_device_enumerator_t *enumerator) { // Create a new backlight device light_device_t *leds_device = light_create_device(enumerator, "leds", NULL); // Iterate through the led controllers and create a device_target for each controller DIR *leds_dir; struct dirent *curr_entry; if((leds_dir = opendir("/sys/class/leds")) == NULL) { LIGHT_ERR("failed to open leds controller directory for reading"); return false; } while((curr_entry = readdir(leds_dir)) != NULL) { // Skip dot entries if(curr_entry->d_name[0] == '.') { continue; } // Setup the target data impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t)); snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/leds/%s/brightness", curr_entry->d_name); snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/leds/%s/max_brightness", curr_entry->d_name); // Create a new device target for the controller light_create_device_target(leds_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data); } closedir(leds_dir); return true; } static bool _impl_sysfs_init_backlight(light_device_enumerator_t *enumerator) { // Create a new backlight device light_device_t *backlight_device = light_create_device(enumerator, "backlight", NULL); // Iterate through the backlight controllers and create a device_target for each controller DIR *backlight_dir; struct dirent *curr_entry; // Keep track of the best controller, and create an autodevice from that char best_controller[NAME_MAX]; uint64_t best_value = 0; if((backlight_dir = opendir("/sys/class/backlight")) == NULL) { LIGHT_ERR("failed to open backlight controller directory for reading"); return false; } while((curr_entry = readdir(backlight_dir)) != NULL) { // Skip dot entries if(curr_entry->d_name[0] == '.') { continue; } // Setup the target data impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t)); snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", curr_entry->d_name); snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", curr_entry->d_name); // Create a new device target for the controller light_create_device_target(backlight_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data); // Read the max brightness to get the best one uint64_t curr_value = 0; if(light_file_read_uint64(dev_data->max_brightness, &curr_value)) { if(curr_value > best_value) { best_value = curr_value; snprintf(best_controller, sizeof(best_controller), "%s", curr_entry->d_name); } } } closedir(backlight_dir); // If we found at least one usable controller, create an auto target mapped to that controller if(best_value > 0) { // Setup the target data impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t)); snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", best_controller); snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", best_controller); // Create a new device target for the controller light_create_device_target(backlight_device, "auto", impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data); } return true; } bool impl_sysfs_init(light_device_enumerator_t *enumerator) { // Create a device for the backlight _impl_sysfs_init_backlight(enumerator); // Create devices for the leds _impl_sysfs_init_leds(enumerator); return true; } bool impl_sysfs_free(light_device_enumerator_t *enumerator) { return true; } bool impl_sysfs_set(light_device_target_t *target, uint64_t in_value) { impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data; if(!light_file_write_uint64(data->brightness, in_value)) { LIGHT_ERR("failed to write to sysfs device"); return false; } return true; } bool impl_sysfs_get(light_device_target_t *target, uint64_t *out_value) { impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data; if(!light_file_read_uint64(data->brightness, out_value)) { LIGHT_ERR("failed to read from sysfs device"); return false; } return true; } bool impl_sysfs_getmax(light_device_target_t *target, uint64_t *out_value) { impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data; if(!light_file_read_uint64(data->max_brightness, out_value)) { LIGHT_ERR("failed to read from sysfs device"); return false; } return true; } bool impl_sysfs_command(light_device_target_t *target, char const *command_string) { // No current need for custom commands in sysfs enumerator // To implement support, simply parse the command string to your liking, and return false on invalid input or results! return true; } light-1.2.1/src/impl/sysfs.h000066400000000000000000000013271355557035500157120ustar00rootroot00000000000000 #pragma once #include "light.h" // Implementation of the sysfs enumerator // Enumerates devices for backlights and leds // Device target data struct _impl_sysfs_data_t { char brightness[NAME_MAX]; char max_brightness[NAME_MAX]; }; typedef struct _impl_sysfs_data_t impl_sysfs_data_t; bool impl_sysfs_init(light_device_enumerator_t *enumerator); bool impl_sysfs_free(light_device_enumerator_t *enumerator); bool impl_sysfs_set(light_device_target_t *target, uint64_t in_value); bool impl_sysfs_get(light_device_target_t *target, uint64_t *out_value); bool impl_sysfs_getmax(light_device_target_t *target, uint64_t *out_value); bool impl_sysfs_command(light_device_target_t *target, char const *command_string); light-1.2.1/src/impl/util.c000066400000000000000000000027521355557035500155160ustar00rootroot00000000000000 #include "impl/util.h" #include "light.h" #include "helpers.h" #include //snprintf #include // malloc, free #include // opendir, readdir #include // PRIu64 bool impl_util_init(light_device_enumerator_t *enumerator) { light_device_t *util_device = light_create_device(enumerator, "test", NULL); light_create_device_target(util_device, "dryrun", impl_util_dryrun_set, impl_util_dryrun_get, impl_util_dryrun_getmax, impl_util_dryrun_command, NULL); return true; } bool impl_util_free(light_device_enumerator_t *enumerator) { return true; } bool impl_util_dryrun_set(light_device_target_t *target, uint64_t in_value) { LIGHT_NOTE("impl_util_dryrun_set: writing brightness %" PRIu64 " to utility target %s", in_value, target->name); return true; } bool impl_util_dryrun_get(light_device_target_t *target, uint64_t *out_value) { LIGHT_NOTE("impl_util_dryrun_get: reading brightness (0) from utility target %s", target->name); *out_value = 0; return true; } bool impl_util_dryrun_getmax(light_device_target_t *target, uint64_t *out_value) { LIGHT_NOTE("impl_util_dryrun_getmax: reading max. brightness (255) from utility target %s", target->name); *out_value = 255; return true; } bool impl_util_dryrun_command(light_device_target_t *target, char const *command_string) { LIGHT_NOTE("impl_util_dryrun_command: running custom command on utility target %s: \"%s\"", target->name, command_string); return true; } light-1.2.1/src/impl/util.h000066400000000000000000000010771355557035500155220ustar00rootroot00000000000000 #pragma once #include "light.h" // Implementation of the util enumerator // Enumerates devices for utilities and testing bool impl_util_init(light_device_enumerator_t *enumerator); bool impl_util_free(light_device_enumerator_t *enumerator); bool impl_util_dryrun_set(light_device_target_t *target, uint64_t in_value); bool impl_util_dryrun_get(light_device_target_t *target, uint64_t *out_value); bool impl_util_dryrun_getmax(light_device_target_t *target, uint64_t *out_value); bool impl_util_dryrun_command(light_device_target_t *target, char const *command_string); light-1.2.1/src/light.c000066400000000000000000000754571355557035500147230ustar00rootroot00000000000000 #include "light.h" #include "helpers.h" // The different device implementations #include "impl/sysfs.h" #include "impl/util.h" #include "impl/razer.h" #include // malloc, free #include // strstr #include // snprintf #include // geteuid #include // geteuid #include #include // PRIu64 /* Static helper functions for this file only, prefix with _ */ static void _light_add_enumerator_device(light_device_enumerator_t *enumerator, light_device_t *new_device) { // Create a new device array uint64_t new_num_devices = enumerator->num_devices + 1; light_device_t **new_devices = malloc(new_num_devices * sizeof(light_device_t*)); // Copy old device array to new one for(uint64_t i = 0; i < enumerator->num_devices; i++) { new_devices[i] = enumerator->devices[i]; } // Set the new device new_devices[enumerator->num_devices] = new_device; // Free the old devices array, if needed if(enumerator->devices != NULL) { free(enumerator->devices); } // Replace the devices array with the new one enumerator->devices = new_devices; enumerator->num_devices = new_num_devices; } static void _light_add_device_target(light_device_t *device, light_device_target_t *new_target) { // Create a new targets array uint64_t new_num_targets = device->num_targets + 1; light_device_target_t **new_targets = malloc(new_num_targets * sizeof(light_device_target_t*)); // Copy old targets array to new one for(uint64_t i = 0; i < device->num_targets; i++) { new_targets[i] = device->targets[i]; } // Set the new target new_targets[device->num_targets] = new_target; // Free the old targets array, if needed if(device->targets != NULL) { free(device->targets); } // Replace the targets array with the new one device->targets= new_targets; device->num_targets = new_num_targets; } static void _light_get_target_path(light_context_t* ctx, char* output_path, size_t output_size) { snprintf(output_path, output_size, "%s/targets/%s/%s/%s", ctx->sys_params.conf_dir, ctx->run_params.device_target->device->enumerator->name, ctx->run_params.device_target->device->name, ctx->run_params.device_target->name ); } static void _light_get_target_file(light_context_t* ctx, char* output_path, size_t output_size, char const * file) { snprintf(output_path, output_size, "%s/targets/%s/%s/%s/%s", ctx->sys_params.conf_dir, ctx->run_params.device_target->device->enumerator->name, ctx->run_params.device_target->device->name, ctx->run_params.device_target->name, file ); } static uint64_t _light_get_min_cap(light_context_t *ctx) { char target_path[NAME_MAX]; _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum"); uint64_t minimum_value = 0; if(!light_file_read_uint64(target_path, &minimum_value)) { return 0; } return minimum_value; } static light_device_enumerator_t* _light_find_enumerator(light_context_t *ctx, char const *comp) { for(uint64_t e = 0; e < ctx->num_enumerators; e++) { if(strncmp(comp, ctx->enumerators[e]->name, NAME_MAX) == 0) { return ctx->enumerators[e]; } } return NULL; } static light_device_t* _light_find_device(light_device_enumerator_t *en, char const *comp) { for(uint64_t d = 0; d < en->num_devices; d++) { if(strncmp(comp, en->devices[d]->name, NAME_MAX) == 0) { return en->devices[d]; } } return NULL; } static light_device_target_t* _light_find_target(light_device_t * dev, char const *comp) { for(uint64_t t = 0; t < dev->num_targets; t++) { if(strncmp(comp, dev->targets[t]->name, NAME_MAX) == 0) { return dev->targets[t]; } } return NULL; } static bool _light_raw_to_percent(light_device_target_t *target, uint64_t inraw, double *outpercent) { double inraw_d = (double)inraw; uint64_t max_value = 0; if(!target->get_max_value(target, &max_value)) { LIGHT_ERR("couldn't read from target"); return false; } double max_value_d = (double)max_value; double percent = light_percent_clamp((inraw_d / max_value_d) * 100.0); *outpercent = percent; return true; } static bool _light_percent_to_raw(light_device_target_t *target, double inpercent, uint64_t *outraw) { uint64_t max_value = 0; if(!target->get_max_value(target, &max_value)) { LIGHT_ERR("couldn't read from target"); return false; } double max_value_d = (double)max_value; double target_value_d = max_value_d * (light_percent_clamp(inpercent) / 100.0); uint64_t target_value = LIGHT_CLAMP((uint64_t)target_value_d, 0, max_value); *outraw = target_value; return true; } static void _light_print_usage() { printf("Usage:\n" " light [OPTIONS] [VALUE]\n" "\n" "Commands:\n" " -H, -h Show this help and exit\n" " -V Show program version and exit\n" " -L List available devices\n" " -A Increase brightness by value\n" " -U Decrease brightness by value\n" " -T Multiply brightness by value (can be a non-whole number, ignores raw mode)\n" " -S Set brightness to value\n" " -G Get brightness\n" " -N Set minimum brightness to value\n" " -P Get minimum brightness\n" " -O Save the current brightness\n" " -I Restore the previously saved brightness\n" "\n" "Options:\n" " -r Interpret input and output values in raw mode (ignored for -T)\n" " -s Specify device target path to use, use -L to list available\n" " -v Specify the verbosity level (default 0)\n" " 0: Values only\n" " 1: Values, Errors.\n" " 2: Values, Errors, Warnings.\n" " 3: Values, Errors, Warnings, Notices.\n" "\n"); printf("Copyright (C) %s %s\n", LIGHT_YEAR, LIGHT_AUTHOR); printf("This is free software, see the source for copying conditions. There is NO\n" "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n" "\n"); } static bool _light_set_context_command(light_context_t *ctx, LFUNCCOMMAND new_cmd) { if(ctx->run_params.command != NULL) { LIGHT_WARN("a command was already set. ignoring."); return false; } ctx->run_params.command = new_cmd; return true; } static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv) { int32_t curr_arg = -1; int32_t log_level = 0; char ctrl_name[NAME_MAX]; bool need_value = false; bool need_float_value = false; bool need_target = true; // default cmd is get brightness bool specified_target = false; snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto"); while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1) { switch(curr_arg) { // Options case 'v': if(sscanf(optarg, "%i", &log_level) != 1) { fprintf(stderr, "-v argument is not an integer.\n\n"); _light_print_usage(); return false; } if(log_level < 0 || log_level > 3) { fprintf(stderr, "-v argument must be between 0 and 3.\n\n"); _light_print_usage(); return false; } light_loglevel = (light_loglevel_t)log_level; break; case 's': snprintf(ctrl_name, sizeof(ctrl_name), "%s", optarg); specified_target = true; break; case 'r': ctx->run_params.raw_mode = true; break; // Commands case 'H': case 'h': _light_set_context_command(ctx, light_cmd_print_help); break; case 'V': _light_set_context_command(ctx, light_cmd_print_version); break; case 'G': _light_set_context_command(ctx, light_cmd_get_brightness); need_target = true; break; case 'S': _light_set_context_command(ctx, light_cmd_set_brightness); need_value = true; need_target = true; break; case 'L': _light_set_context_command(ctx, light_cmd_list_devices); break; case 'M': _light_set_context_command(ctx, light_cmd_get_max_brightness); need_target = true; break; case 'N': _light_set_context_command(ctx, light_cmd_set_min_brightness); need_target = true; need_value = true; break; case 'P': _light_set_context_command(ctx, light_cmd_get_min_brightness); need_target = true; break; case 'A': _light_set_context_command(ctx, light_cmd_add_brightness); need_target = true; need_value = true; break; case 'U': _light_set_context_command(ctx, light_cmd_sub_brightness); need_target = true; need_value = true; break; case 'T': _light_set_context_command(ctx, light_cmd_mul_brightness); need_target = true; need_float_value = true; break; case 'O': _light_set_context_command(ctx, light_cmd_save_brightness); need_target = true; break; case 'I': _light_set_context_command(ctx, light_cmd_restore_brightness); need_target = true; break; } } if(ctx->run_params.command == NULL) { _light_set_context_command(ctx, light_cmd_get_brightness); } if(need_target) { light_device_target_t *curr_target = light_find_device_target(ctx, ctrl_name); if(curr_target == NULL) { if(specified_target) { fprintf(stderr, "We couldn't find the specified device target at the path \"%s\". Use -L to find one.\n\n", ctrl_name); return false; } else { fprintf(stderr, "No backlight controller was found, so we could not decide an automatic target. The current command will have no effect. Please use -L to find a target and then specify it with -s.\n\n"); curr_target = light_find_device_target(ctx, "util/test/dryrun"); } } ctx->run_params.device_target = curr_target; } if(need_value || need_float_value) { if( (argc - optind) != 1) { fprintf(stderr, "please specify a for this command.\n\n"); _light_print_usage(); return false; } } if(need_value) { if(ctx->run_params.raw_mode) { if(sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1) { fprintf(stderr, " is not an integer.\n\n"); _light_print_usage(); return false; } } else { double percent_value = 0.0; if(sscanf(argv[optind], "%lf", &percent_value) != 1) { fprintf(stderr, " is not a decimal.\n\n"); _light_print_usage(); return false; } percent_value = light_percent_clamp(percent_value); uint64_t raw_value = 0; if(!_light_percent_to_raw(ctx->run_params.device_target, percent_value, &raw_value)) { LIGHT_ERR("failed to convert from percent to raw for device target"); return false; } ctx->run_params.value = raw_value; } } if(need_float_value) { if(sscanf(argv[optind], "%f", &ctx->run_params.float_value) != 1) { fprintf(stderr, " is not a float.\n\n"); _light_print_usage(); return false; } } return true; } /* API function definitions */ light_context_t* light_initialize(int argc, char **argv) { light_context_t *new_ctx = malloc(sizeof(light_context_t)); // Setup default values and runtime params new_ctx->enumerators = NULL; new_ctx->num_enumerators = 0; new_ctx->run_params.command = NULL; new_ctx->run_params.device_target = NULL; new_ctx->run_params.value = 0; new_ctx->run_params.raw_mode = false; // Setup the configuration folder // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config if(geteuid() == 0) { snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light"); } else { char *xdg_conf = getenv("XDG_CONFIG_HOME"); if(xdg_conf != NULL) { snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf); } else { snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME")); } } // Make sure the configuration folder exists, otherwise attempt to create it int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if(rc && errno != EEXIST) { LIGHT_WARN("couldn't create configuration directory"); return false; } // Create the built-in enumerators light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free); light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free); light_create_enumerator(new_ctx, "razer", &impl_razer_init, &impl_razer_free); // This is where we would create enumerators from plugins as well // 1. Run the plugins get_name() function to get its name // 2. Point to the plugins init() and free() functions when creating the enumerator // initialize all enumerators, this will create all the devices and their targets if(!light_init_enumerators(new_ctx)) { LIGHT_WARN("failed to initialize all enumerators"); } // Parse arguments if(!_light_parse_arguments(new_ctx, argc, argv)) { LIGHT_ERR("failed to parse arguments"); return NULL; } return new_ctx; } bool light_execute(light_context_t *ctx) { if(ctx->run_params.command == NULL) { LIGHT_ERR("run parameters command was null, can't execute"); return false; } return ctx->run_params.command(ctx); } void light_free(light_context_t *ctx) { if(!light_free_enumerators(ctx)) { LIGHT_WARN("failed to free all enumerators"); } free(ctx); } light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT init_func, LFUNCENUMFREE free_func) { // Create a new enumerator array uint64_t new_num_enumerators = ctx->num_enumerators + 1; light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*)); // Copy old enumerator array to new one for(uint64_t i = 0; i < ctx->num_enumerators; i++) { new_enumerators[i] = ctx->enumerators[i]; } // Allocate the new enumerator new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t)); light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators]; returner->devices = NULL; returner->num_devices = 0; returner->init = init_func; returner->free = free_func; snprintf(returner->name, sizeof(returner->name), "%s", name); // Free the old enumerator array, if needed if(ctx->enumerators != NULL) { free(ctx->enumerators); } // Replace the enumerator array with the new one ctx->enumerators = new_enumerators; ctx->num_enumerators = new_num_enumerators; // Return newly created device return returner; } bool light_init_enumerators(light_context_t *ctx) { bool success = true; for(uint64_t i = 0; i < ctx->num_enumerators; i++) { light_device_enumerator_t * curr_enumerator = ctx->enumerators[i]; if(!curr_enumerator->init(curr_enumerator)) { success = false; } } return success; } bool light_free_enumerators(light_context_t *ctx) { bool success = true; for(uint64_t i = 0; i < ctx->num_enumerators; i++) { light_device_enumerator_t * curr_enumerator = ctx->enumerators[i]; if(!curr_enumerator->free(curr_enumerator)) { success = false; } if(curr_enumerator->devices != NULL) { for(uint64_t d = 0; d < curr_enumerator->num_devices; d++) { light_delete_device(curr_enumerator->devices[d]); } free(curr_enumerator->devices); curr_enumerator->devices = NULL; } free(curr_enumerator); } free(ctx->enumerators); ctx->enumerators = NULL; ctx->num_enumerators = 0; return success; } bool light_split_target_path(char const *in_path, light_target_path_t *out_path) { char const * begin = in_path; char const * end = strstr(begin, "/"); if(end == NULL) { LIGHT_WARN("invalid path passed to split_target_path"); return false; } size_t size = end - begin; strncpy(out_path->enumerator, begin, size); out_path->enumerator[size] = '\0'; begin = end + 1; end = strstr(begin, "/"); if(end == NULL) { LIGHT_WARN("invalid path passed to split_target_path"); return false; } size = end - begin; strncpy(out_path->device, begin, size); out_path->device[size] = '\0'; strcpy(out_path->target, end + 1); return true; } light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name) { light_target_path_t new_path; if(!light_split_target_path(name, &new_path)) { LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized: \"%s\"", name); return NULL; } /* Uncomment to debug the split function printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n", new_path.enumerator, strlen(new_path.enumerator), new_path.device, strlen(new_path.device), new_path.target, strlen(new_path.target)); */ // find a matching enumerator light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator); if(enumerator == NULL) { LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator); return NULL; } light_device_t *device = _light_find_device(enumerator, new_path.device); if(device == NULL) { LIGHT_WARN("no such device, \"%s\"", new_path.device); return NULL; } light_device_target_t *target = _light_find_target(device, new_path.target); if(target == NULL) { LIGHT_WARN("no such target, \"%s\"", new_path.target); return NULL; } return target; } bool light_cmd_print_help(light_context_t *ctx) { _light_print_usage(); return true; } bool light_cmd_print_version(light_context_t *ctx) { printf("v%s\n", VERSION); return true; } bool light_cmd_list_devices(light_context_t *ctx) { printf("Listing device targets:\n"); for(uint64_t enumerator = 0; enumerator < ctx->num_enumerators; enumerator++) { light_device_enumerator_t *curr_enumerator = ctx->enumerators[enumerator]; for(uint64_t device = 0; device < curr_enumerator->num_devices; device++) { light_device_t *curr_device = curr_enumerator->devices[device]; for(uint64_t target = 0; target < curr_device->num_targets; target++) { light_device_target_t *curr_target = curr_device->targets[target]; printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name); } } } return true; } bool light_cmd_set_brightness(light_context_t *ctx) { light_device_target_t *target = ctx->run_params.device_target; if(target == NULL) { LIGHT_ERR("didn't have a valid target, programmer mistake"); return false; } uint64_t mincap = _light_get_min_cap(ctx); uint64_t value = ctx->run_params.value; if(mincap > value) { value = mincap; } if(!target->set_value(target, value)) { LIGHT_ERR("failed to write to target"); return false; } return true; } bool light_cmd_get_brightness(light_context_t *ctx) { light_device_target_t *target = ctx->run_params.device_target; if(target == NULL) { LIGHT_ERR("didn't have a valid target, programmer mistake"); return false; } uint64_t value = 0; if(!target->get_value(target, &value)) { LIGHT_ERR("failed to read from target"); return false; } if(ctx->run_params.raw_mode) { printf("%" PRIu64 "\n", value); } else { double percent = 0.0; if(!_light_raw_to_percent(target, value, &percent)) { LIGHT_ERR("failed to convert from raw to percent from device target"); return false; } printf("%.2f\n", percent); } return true; } bool light_cmd_get_max_brightness(light_context_t *ctx) { light_device_target_t *target = ctx->run_params.device_target; if(target == NULL) { LIGHT_ERR("didn't have a valid target, programmer mistake"); return false; } if(!ctx->run_params.raw_mode) { printf("100.0\n"); return true; } uint64_t max_value = 0; if(!target->get_max_value(target, &max_value)) { LIGHT_ERR("failed to read from device target"); return false; } printf("%" PRIu64 "\n", max_value); return true; } bool light_cmd_set_min_brightness(light_context_t *ctx) { char target_path[NAME_MAX]; _light_get_target_path(ctx, target_path, sizeof(target_path)); // Make sure the target folder exists, otherwise attempt to create it int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if(rc && errno != EEXIST) { LIGHT_ERR("couldn't create target directory for minimum brightness"); return false; } char target_filepath[NAME_MAX]; _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum"); if(!light_file_write_uint64(target_filepath, ctx->run_params.value)) { LIGHT_ERR("couldn't write value to minimum file"); return false; } return true; } bool light_cmd_get_min_brightness(light_context_t *ctx) { char target_path[NAME_MAX]; _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum"); uint64_t minimum_value = 0; if(!light_file_read_uint64(target_path, &minimum_value)) { if(ctx->run_params.raw_mode) { printf("0\n"); } else { printf("0.00\n"); } return true; } if(ctx->run_params.raw_mode) { printf("%" PRIu64 "\n", minimum_value); } else { double minimum_d = 0.0; if(!_light_raw_to_percent(ctx->run_params.device_target, minimum_value, &minimum_d)) { LIGHT_ERR("failed to convert value from raw to percent for device target"); return false; } printf("%.2f\n", minimum_d); } return true; } bool light_cmd_add_brightness(light_context_t *ctx) { light_device_target_t *target = ctx->run_params.device_target; if(target == NULL) { LIGHT_ERR("didn't have a valid target, programmer mistake"); return false; } uint64_t value = 0; if(!target->get_value(target, &value)) { LIGHT_ERR("failed to read from target"); return false; } uint64_t max_value = 0; if(!target->get_max_value(target, &max_value)) { LIGHT_ERR("failed to read from target"); return false; } value += ctx->run_params.value; uint64_t mincap = _light_get_min_cap(ctx); if(mincap > value) { value = mincap; } if(value > max_value) { value = max_value; } if(!target->set_value(target, value)) { LIGHT_ERR("failed to write to target"); return false; } return true; } bool light_cmd_sub_brightness(light_context_t *ctx) { light_device_target_t *target = ctx->run_params.device_target; if(target == NULL) { LIGHT_ERR("didn't have a valid target, programmer mistake"); return false; } uint64_t value = 0; if(!target->get_value(target, &value)) { LIGHT_ERR("failed to read from target"); return false; } if(value > ctx->run_params.value) { value -= ctx->run_params.value; } else { value = 0; } uint64_t mincap = _light_get_min_cap(ctx); if(mincap > value) { value = mincap; } if(!target->set_value(target, value)) { LIGHT_ERR("failed to write to target"); return false; } return true; } bool light_cmd_mul_brightness(light_context_t *ctx) { light_device_target_t *target = ctx->run_params.device_target; if(target == NULL) { LIGHT_ERR("didn't have a valid target, programmer mistake"); return false; } uint64_t value = 0; if(!target->get_value(target, &value)) { LIGHT_ERR("failed to read from target"); return false; } uint64_t max_value = 0; if(!target->get_max_value(target, &max_value)) { LIGHT_ERR("failed to read from target"); return false; } uint64_t old_value = value; value *= ctx->run_params.float_value; // Check that we actually de/increase value if(value == old_value) { if(ctx->run_params.float_value > 1) value++; if(ctx->run_params.float_value < 1 && value > 0) value--; } uint64_t mincap = _light_get_min_cap(ctx); if(mincap > value) { value = mincap; } if(value > max_value) { value = max_value; } if(!target->set_value(target, value)) { LIGHT_ERR("failed to write to target"); return false; } return true; } bool light_cmd_save_brightness(light_context_t *ctx) { char target_path[NAME_MAX]; _light_get_target_path(ctx, target_path, sizeof(target_path)); // Make sure the target folder exists, otherwise attempt to create it int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if(rc && errno != EEXIST) { LIGHT_ERR("couldn't create target directory for save brightness"); return false; } char target_filepath[NAME_MAX]; _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save"); uint64_t curr_value = 0; if(!ctx->run_params.device_target->get_value(ctx->run_params.device_target, &curr_value)) { LIGHT_ERR("couldn't read from device target"); return false; } if(!light_file_write_uint64(target_filepath, curr_value)) { LIGHT_ERR("couldn't write value to savefile"); return false; } return true; } bool light_cmd_restore_brightness(light_context_t *ctx) { char target_path[NAME_MAX]; _light_get_target_file(ctx, target_path, sizeof(target_path), "save"); uint64_t saved_value = 0; if(!light_file_read_uint64(target_path, &saved_value)) { LIGHT_ERR("couldn't read value from savefile"); return false; } uint64_t mincap = _light_get_min_cap(ctx); if(mincap > saved_value) { saved_value = mincap; } if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value)) { LIGHT_ERR("couldn't write saved value to device target"); return false; } return true; } light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data) { light_device_t *new_device = malloc(sizeof(light_device_t)); new_device->enumerator = enumerator; new_device->targets = NULL; new_device->num_targets = 0; new_device->device_data = device_data; snprintf(new_device->name, sizeof(new_device->name), "%s", name); _light_add_enumerator_device(enumerator, new_device); return new_device; } void light_delete_device(light_device_t *device) { for(uint64_t i = 0; i < device->num_targets; i++) { light_delete_device_target(device->targets[i]); } if(device->targets != NULL) { free(device->targets); } if(device->device_data != NULL) { free(device->device_data); } free(device); } light_device_target_t *light_create_device_target(light_device_t *device, char const *name, LFUNCVALSET setfunc, LFUNCVALGET getfunc, LFUNCMAXVALGET getmaxfunc, LFUNCCUSTOMCMD cmdfunc, void *target_data) { light_device_target_t *new_target = malloc(sizeof(light_device_target_t)); new_target->device = device; new_target->set_value = setfunc; new_target->get_value = getfunc; new_target->get_max_value = getmaxfunc; new_target->custom_command = cmdfunc; new_target->device_target_data = target_data; snprintf(new_target->name, sizeof(new_target->name), "%s", name); _light_add_device_target(device, new_target); return new_target; } void light_delete_device_target(light_device_target_t *device_target) { if(device_target->device_target_data != NULL) { free(device_target->device_target_data); } free(device_target); } light-1.2.1/src/light.h000066400000000000000000000126361355557035500147160ustar00rootroot00000000000000 #pragma once #include #include #include // NAME_MAX #include // NULL #include "config.h" #define LIGHT_YEAR "2012 - 2018" #define LIGHT_AUTHOR "Fredrik Haikarainen" struct _light_device_target_t; typedef struct _light_device_target_t light_device_target_t; struct _light_device_t; typedef struct _light_device_t light_device_t; struct _light_device_enumerator_t; typedef struct _light_device_enumerator_t light_device_enumerator_t; /* Function pointers that implementations have to set for device targets */ typedef bool (*LFUNCVALSET)(light_device_target_t*, uint64_t); typedef bool (*LFUNCVALGET)(light_device_target_t*, uint64_t*); typedef bool (*LFUNCMAXVALGET)(light_device_target_t*, uint64_t*); typedef bool (*LFUNCCUSTOMCMD)(light_device_target_t*, char const *); /* Describes a target within a device (for example a led on a keyboard, or a controller for a backlight) */ struct _light_device_target_t { char name[256]; LFUNCVALSET set_value; LFUNCVALGET get_value; LFUNCMAXVALGET get_max_value; LFUNCCUSTOMCMD custom_command; void *device_target_data; light_device_t *device; }; /* Describes a device (a backlight, a keyboard, a led-strip) */ struct _light_device_t { char name[256]; light_device_target_t **targets; uint64_t num_targets; void *device_data; light_device_enumerator_t *enumerator; }; typedef bool (*LFUNCENUMINIT)(light_device_enumerator_t*); typedef bool (*LFUNCENUMFREE)(light_device_enumerator_t*); /* An enumerator that is responsible for creating and freeing devices as well as their targets */ struct _light_device_enumerator_t { char name[256]; LFUNCENUMINIT init; LFUNCENUMFREE free; light_device_t **devices; uint64_t num_devices; }; typedef struct _light_context_t light_context_t; // A command that can be run (set, get, add, subtract, print help, print version, list devices etc.) typedef bool (*LFUNCCOMMAND)(light_context_t *); struct _light_context_t { struct { LFUNCCOMMAND command; // What command was issued // Only one of value and raw_value is populated; which one depends on the command uint64_t value; // The input value, in raw mode float float_value; // The input value as a float bool raw_mode; // Whether or not we use raw or percentage mode light_device_target_t *device_target; // The device target to act on } run_params; struct { char conf_dir[NAME_MAX]; // The path to the application cache directory } sys_params; light_device_enumerator_t **enumerators; uint64_t num_enumerators; }; // The different available commands bool light_cmd_print_help(light_context_t *ctx); // H,h bool light_cmd_print_version(light_context_t *ctx); // V bool light_cmd_list_devices(light_context_t *ctx); // L bool light_cmd_set_brightness(light_context_t *ctx); // S bool light_cmd_get_brightness(light_context_t *ctx); // G bool light_cmd_get_max_brightness(light_context_t *ctx); // M bool light_cmd_set_min_brightness(light_context_t *ctx); // N bool light_cmd_get_min_brightness(light_context_t *ctx); // P bool light_cmd_add_brightness(light_context_t *ctx); // A bool light_cmd_sub_brightness(light_context_t *ctx); // U bool light_cmd_mul_brightness(light_context_t *ctx); // T bool light_cmd_save_brightness(light_context_t *ctx); // O bool light_cmd_restore_brightness(light_context_t *ctx); // I /* Initializes the application, given the command-line. Returns a context. */ light_context_t* light_initialize(int argc, char **argv); /* Executes the given context. Returns true on success, false on failure. */ bool light_execute(light_context_t*); /* Frees the given context */ void light_free(light_context_t*); /* Create a device enumerator in the given context */ light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT, LFUNCENUMFREE); /* Initializes all the device enumerators (and its devices, targets) */ bool light_init_enumerators(light_context_t *ctx); /* Frees all the device enumerators (and its devices, targets) */ bool light_free_enumerators(light_context_t *ctx); /* Use this to create a device. Will automatically be added to enumerator. */ light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data); /* Use this to delete a device. */ void light_delete_device(light_device_t *device); /* Use this to create a device target. Will automatically be added to device. */ light_device_target_t *light_create_device_target(light_device_t *device, char const *name, LFUNCVALSET setfunc, LFUNCVALGET getfunc, LFUNCMAXVALGET getmaxfunc, LFUNCCUSTOMCMD cmdfunc, void *target_data); /* Use this to delete a device target. */ void light_delete_device_target(light_device_target_t *device_target); typedef struct _light_target_path_t light_target_path_t; struct _light_target_path_t { char enumerator[NAME_MAX]; char device[NAME_MAX]; char target[NAME_MAX]; }; bool light_split_target_path(char const * in_path, light_target_path_t *out_path); /* Returns the found device target, or null. Name should be enumerator/device/target */ light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name); light-1.2.1/src/main.c000066400000000000000000000011411355557035500145130ustar00rootroot00000000000000 #include "light.h" #include "helpers.h" //#include #define LIGHT_RETURNVAL_INITFAIL 2 #define LIGHT_RETURNVAL_EXECFAIL 1 #define LIGHT_RETURNVAL_SUCCESS 0 int main(int argc, char **argv) { light_context_t *light_ctx = light_initialize(argc, argv); if(light_ctx == NULL) { LIGHT_ERR("Initialization failed"); return LIGHT_RETURNVAL_INITFAIL; } if(!light_execute(light_ctx)) { LIGHT_ERR("Execution failed"); light_free(light_ctx); return LIGHT_RETURNVAL_EXECFAIL; } light_free(light_ctx); return LIGHT_RETURNVAL_SUCCESS; }