├── debian ├── docs ├── source │ └── format ├── watch ├── light.postinst ├── changelog ├── rules ├── control └── copyright ├── autogen.sh ├── .gitignore ├── 90-backlight.rules ├── src ├── Makefile.am ├── impl │ ├── util.h │ ├── sysfs.h │ ├── razer.h │ ├── util.c │ ├── razer.c │ └── sysfs.c ├── main.c ├── helpers.h ├── helpers.c ├── light.h └── light.c ├── configure.ac ├── Makefile.am ├── light.1 ├── ChangeLog.md ├── README.md ├── DOCUMENTATION.md └── COPYING /debian/docs: -------------------------------------------------------------------------------- 1 | README* 2 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (quilt) 2 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | autoreconf -W portability -visfm 4 | -------------------------------------------------------------------------------- /debian/watch: -------------------------------------------------------------------------------- 1 | version=4 2 | 3 | https://github.com/haikarainen/light/releases .*/archive/v?(\d\S*)\.tar\.gz 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.o 3 | .deps 4 | GPATH 5 | GRTAGS 6 | GTAGS 7 | light 8 | light.1.gz 9 | aclocal.m4 10 | autom4te.cache 11 | compile 12 | config.h 13 | config.h.in 14 | config.log 15 | config.status 16 | configure 17 | depcomp 18 | install-sh 19 | missing 20 | stamp-h1 21 | Makefile 22 | Makefile.in 23 | -------------------------------------------------------------------------------- /90-backlight.rules: -------------------------------------------------------------------------------- 1 | ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness" 2 | ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness" 3 | ACTION=="add", SUBSYSTEM=="leds", RUN+="/bin/chgrp video /sys/class/leds/%k/brightness" 4 | ACTION=="add", SUBSYSTEM=="leds", RUN+="/bin/chmod g+w /sys/class/leds/%k/brightness" 5 | -------------------------------------------------------------------------------- /debian/light.postinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | #DEBHELPER# 6 | 7 | udevadm control --reload 8 | udevadm trigger --subsystem-match=backlight --subsystem-match=leds --action=change --settle 9 | 10 | # Reset udev rule action back to 'add', it was set to 'change' during 11 | # dh_auto_configure to make sure they are effective right away. 12 | sed -i 's/^ACTION=="change"/ACTION=="add"/g' /usr/lib/udev/rules.d/90-backlight.rules 13 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS = light 2 | 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 3 | light_CPPFLAGS = -I../include -D_GNU_SOURCE 4 | light_CFLAGS = -W -Wall -Wextra -std=gnu99 -Wno-type-limits -Wno-format-truncation -Wno-unused-parameter -fcommon 5 | 6 | if CLASSIC 7 | install-exec-hook: 8 | chmod 6755 $(DESTDIR)$(bindir)/light 9 | endif 10 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | light (1.2.1-1) unstable; urgency=medium 2 | 3 | * Initial Debian Release (Closes: #947974) 4 | Thanks to Joachim Nilsson for the initial work 5 | * d/control: Add dependency on udev 6 | * d/light.postinst: Add udevadm trigger to reload permissions rules 7 | * Fix udevadm trigger to make it effective without reboot 8 | * d/copyright: Add myself 9 | * d/control: Fix typo in description 10 | 11 | -- Samuel Henrique Thu, 02 Jan 2020 22:45:35 +0000 12 | -------------------------------------------------------------------------------- /src/impl/util.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "light.h" 5 | 6 | // Implementation of the util enumerator 7 | // Enumerates devices for utilities and testing 8 | 9 | bool impl_util_init(light_device_enumerator_t *enumerator); 10 | bool impl_util_free(light_device_enumerator_t *enumerator); 11 | 12 | bool impl_util_dryrun_set(light_device_target_t *target, uint64_t in_value); 13 | bool impl_util_dryrun_get(light_device_target_t *target, uint64_t *out_value); 14 | bool impl_util_dryrun_getmax(light_device_target_t *target, uint64_t *out_value); 15 | bool impl_util_dryrun_command(light_device_target_t *target, char const *command_string); 16 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | # export DH_VERBOSE=1 3 | export DEB_BUILD_MAINT_OPTIONS = hardening=+all 4 | 5 | %: 6 | dh $@ 7 | 8 | override_dh_auto_configure: 9 | dh_auto_configure -- --with-udev 10 | # Change action type of udev rules to make it effective after 11 | # installation, they are reset back with the poinst script. 12 | sed -i 's/^ACTION=="add"/ACTION=="change"/g' 90-backlight.rules 13 | 14 | override_dh_installchangelogs: 15 | dh_installchangelogs ChangeLog.md 16 | 17 | override_dh_auto_install: 18 | dh_auto_install 19 | rm -f debian/light/usr/share/doc/light/COPYING 20 | rm -f debian/light/usr/share/doc/light/ChangeLog.md 21 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include "light.h" 3 | #include "helpers.h" 4 | 5 | //#include 6 | 7 | #define LIGHT_RETURNVAL_INITFAIL 2 8 | #define LIGHT_RETURNVAL_EXECFAIL 1 9 | #define LIGHT_RETURNVAL_SUCCESS 0 10 | 11 | int main(int argc, char **argv) 12 | { 13 | light_context_t *light_ctx = light_initialize(argc, argv); 14 | if(light_ctx == NULL) { 15 | LIGHT_ERR("Initialization failed"); 16 | return LIGHT_RETURNVAL_INITFAIL; 17 | } 18 | 19 | if(!light_execute(light_ctx)) { 20 | LIGHT_ERR("Execution failed"); 21 | light_free(light_ctx); 22 | return LIGHT_RETURNVAL_EXECFAIL; 23 | } 24 | 25 | light_free(light_ctx); 26 | return LIGHT_RETURNVAL_SUCCESS; 27 | } 28 | -------------------------------------------------------------------------------- /src/impl/sysfs.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "light.h" 5 | 6 | // Implementation of the sysfs enumerator 7 | // Enumerates devices for backlights and leds 8 | 9 | // Device target data 10 | struct _impl_sysfs_data_t 11 | { 12 | char brightness[NAME_MAX]; 13 | char max_brightness[NAME_MAX]; 14 | }; 15 | 16 | typedef struct _impl_sysfs_data_t impl_sysfs_data_t; 17 | 18 | bool impl_sysfs_init(light_device_enumerator_t *enumerator); 19 | bool impl_sysfs_free(light_device_enumerator_t *enumerator); 20 | 21 | bool impl_sysfs_set(light_device_target_t *target, uint64_t in_value); 22 | bool impl_sysfs_get(light_device_target_t *target, uint64_t *out_value); 23 | bool impl_sysfs_getmax(light_device_target_t *target, uint64_t *out_value); 24 | bool impl_sysfs_command(light_device_target_t *target, char const *command_string); 25 | -------------------------------------------------------------------------------- /src/impl/razer.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "light.h" 5 | 6 | // Implementation of the razer enumerator 7 | // Enumerates devices for the openrazer driver https://github.com/openrazer/openrazer 8 | 9 | // Device target data 10 | struct _impl_razer_data_t 11 | { 12 | char brightness[NAME_MAX]; 13 | uint64_t max_brightness; 14 | }; 15 | 16 | typedef struct _impl_razer_data_t impl_razer_data_t; 17 | 18 | bool impl_razer_init(light_device_enumerator_t *enumerator); 19 | bool impl_razer_free(light_device_enumerator_t *enumerator); 20 | 21 | bool impl_razer_set(light_device_target_t *target, uint64_t in_value); 22 | bool impl_razer_get(light_device_target_t *target, uint64_t *out_value); 23 | bool impl_razer_getmax(light_device_target_t *target, uint64_t *out_value); 24 | bool impl_razer_command(light_device_target_t *target, char const *command_string); 25 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT([light], [1.2], [https://github.com/haikarainen/light/issues]) 2 | AM_INIT_AUTOMAKE([1.11 foreign subdir-objects]) 3 | AM_SILENT_RULES([yes]) 4 | 5 | AC_CONFIG_SRCDIR([src/light.c]) 6 | AC_CONFIG_HEADER([config.h]) 7 | AC_CONFIG_FILES([Makefile src/Makefile]) 8 | 9 | AC_PROG_CC 10 | AC_PROG_INSTALL 11 | AC_HEADER_STDC 12 | 13 | AC_ARG_WITH([udev], 14 | AS_HELP_STRING([--with-udev@<:@=PATH@:>@], [use udev instead of SUID root, optional rules.d path]), 15 | [udev=$withval], [udev=no]) 16 | 17 | AC_MSG_CHECKING(for udev rules.d) 18 | AS_IF([test "x$udev" != "xno"], [ 19 | AS_IF([test "x$udev" = "xyes"], [ 20 | udevdir="\${prefix}/lib/udev/rules.d" 21 | ],[ 22 | udevdir="$udev" 23 | ]) 24 | AC_SUBST(udevdir) 25 | AC_MSG_RESULT([$udevdir]) 26 | ],[ 27 | AC_MSG_RESULT([disabled, classic SUID root mode]) 28 | ]) 29 | 30 | # Allow classic SUID root behavior if udev rule is not used 31 | AM_CONDITIONAL(UDEV, [test "x$udev" != "xno"]) 32 | AM_CONDITIONAL(CLASSIC, [test "x$udev" = "xno"]) 33 | 34 | AC_OUTPUT 35 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = src 2 | dist_man1_MANS = light.1 3 | doc_DATA = README.md COPYING ChangeLog.md 4 | EXTRA_DIST = README.md COPYING ChangeLog.md 5 | 6 | if UDEV 7 | udev_DATA = 90-backlight.rules 8 | EXTRA_DIST += $(top_srcdir)/90-backlight.rules 9 | endif 10 | 11 | # lintian --profile debian -i -I --show-overrides ../$PKG.changes 12 | deb: 13 | dpkg-buildpackage -uc -us -B 14 | 15 | # 16 | # Target to run when building a release 17 | # 18 | release: distcheck 19 | @for file in $(DIST_ARCHIVES); do \ 20 | md5sum $$file > ../$$file.md5; \ 21 | done 22 | @mv $(DIST_ARCHIVES) ../ 23 | @echo 24 | @echo "Resulting release files:" 25 | @echo "=================================================================" 26 | @for file in $(DIST_ARCHIVES); do \ 27 | printf "%-32s Distribution tarball\n" $$file; \ 28 | printf "%-32s " $$file.md5; cat ../$$file.md5 | cut -f1 -d' '; \ 29 | done 30 | @for file in `cd ..; ls $(PACKAGE)_$(VERSION)*`; do \ 31 | printf "%-32s Debian/Ubuntu file\n" $$file; \ 32 | done 33 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: light 2 | Section: misc 3 | Priority: optional 4 | Maintainer: Samuel Henrique 5 | Homepage: https://github.com/haikarainen/light 6 | Build-Depends: debhelper-compat (= 12) 7 | Vcs-Browser: https://salsa.debian.org/debian/light 8 | Vcs-Git: https://salsa.debian.org/debian/light.git 9 | Standards-Version: 4.4.1 10 | Rules-Requires-Root: no 11 | 12 | Package: light 13 | Architecture: any 14 | Depends: udev, ${shlibs:Depends}, ${misc:Depends} 15 | Description: control display backlight controllers and LEDs 16 | Light is a useful tool to control display brightness in lightweight 17 | desktops or window managers that do not have bundled applications for 18 | this purpose. 19 | . 20 | Most modern laptops have moved away from hardware controlled brightness 21 | and require software control. Light works where other software has 22 | proven to be unreliable, e.g. xbacklight. It can even be used from the 23 | console as it does not rely on X. 24 | . 25 | Light has features like setting a minimum brightness value, as well as 26 | saving and restoring the brightness at reboot and startup. 27 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Upstream-Name: light 3 | Source: https://github.com/haikarainen/light 4 | 5 | Files: * 6 | Copyright: 2012-2018 Fredrik Haikarainen 7 | License: GPL-3 8 | 9 | Files: debian/* 10 | Copyright: 2018 Joachim Nilsson 11 | 2019 Samuel Henrique 12 | License: GPL-3 13 | 14 | License: GPL-3 15 | This program is free software: you can redistribute it and/or modify 16 | it under the terms of the GNU General Public License as published by 17 | the Free Software Foundation, either version 3 of the License, or 18 | (at your option) any later version. 19 | . 20 | This program is distributed in the hope that it will be useful, 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | GNU General Public License for more details. 24 | . 25 | On Debian systems, the full text of the GNU General Public 26 | License version 3 can be found in the file 27 | `/usr/share/common-licenses/GPL-3'. 28 | -------------------------------------------------------------------------------- /src/impl/util.c: -------------------------------------------------------------------------------- 1 | 2 | #include "impl/util.h" 3 | #include "light.h" 4 | #include "helpers.h" 5 | 6 | #include //snprintf 7 | #include // malloc, free 8 | #include // opendir, readdir 9 | #include // PRIu64 10 | 11 | bool impl_util_init(light_device_enumerator_t *enumerator) 12 | { 13 | light_device_t *util_device = light_create_device(enumerator, "test", NULL); 14 | light_create_device_target(util_device, "dryrun", impl_util_dryrun_set, impl_util_dryrun_get, impl_util_dryrun_getmax, impl_util_dryrun_command, NULL); 15 | return true; 16 | } 17 | 18 | bool impl_util_free(light_device_enumerator_t *enumerator) 19 | { 20 | return true; 21 | } 22 | 23 | bool impl_util_dryrun_set(light_device_target_t *target, uint64_t in_value) 24 | { 25 | LIGHT_NOTE("impl_util_dryrun_set: writing brightness %" PRIu64 " to utility target %s", in_value, target->name); 26 | return true; 27 | } 28 | 29 | bool impl_util_dryrun_get(light_device_target_t *target, uint64_t *out_value) 30 | { 31 | LIGHT_NOTE("impl_util_dryrun_get: reading brightness (0) from utility target %s", target->name); 32 | *out_value = 0; 33 | return true; 34 | } 35 | 36 | bool impl_util_dryrun_getmax(light_device_target_t *target, uint64_t *out_value) 37 | { 38 | LIGHT_NOTE("impl_util_dryrun_getmax: reading max. brightness (255) from utility target %s", target->name); 39 | *out_value = 255; 40 | return true; 41 | } 42 | 43 | bool impl_util_dryrun_command(light_device_target_t *target, char const *command_string) 44 | { 45 | LIGHT_NOTE("impl_util_dryrun_command: running custom command on utility target %s: \"%s\"", target->name, command_string); 46 | return true; 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/helpers.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* Clamps x(value) between y(min) and z(max) in a nested ternary operation */ 10 | #define LIGHT_CLAMP(val, min, max) (val < min ? light_log_clamp_min(min) : (val > max ? light_log_clamp_max(max) : val)) 11 | 12 | /* Verbosity levels: 13 | * 0 - No output 14 | * 1 - Errors 15 | * 2 - Errors, warnings 16 | * 3 - Errors, warnings, notices 17 | */ 18 | typedef enum { 19 | LIGHT_ERROR_LEVEL = 1, 20 | LIGHT_WARN_LEVEL, 21 | LIGHT_NOTE_LEVEL 22 | } light_loglevel_t; 23 | 24 | light_loglevel_t light_loglevel; 25 | 26 | #define LIGHT_LOG(lvl, fp, fmt, args...)\ 27 | if(light_loglevel >= lvl)\ 28 | fprintf(fp, "%s:%d:" fmt "\n", __FILE__, __LINE__, ##args) 29 | 30 | #define LIGHT_NOTE(fmt, args...) LIGHT_LOG(LIGHT_NOTE_LEVEL, stdout, " Notice: " fmt, ##args) 31 | #define LIGHT_WARN(fmt, args...) LIGHT_LOG(LIGHT_WARN_LEVEL, stderr, " Warning: " fmt, ##args) 32 | #define LIGHT_ERR(fmt, args...) LIGHT_LOG(LIGHT_ERROR_LEVEL, stderr, " Error: " fmt, ##args) 33 | #define LIGHT_MEMERR() LIGHT_ERR("memory error"); 34 | #define LIGHT_PERMLOG(act, log)\ 35 | do {\ 36 | log("could not open '%s' for " act, filename);\ 37 | log("Verify it exists with the right permissions");\ 38 | } while(0) 39 | 40 | #define LIGHT_PERMERR(x) LIGHT_PERMLOG(x, LIGHT_ERR) 41 | #define LIGHT_PERMWARN(x) LIGHT_PERMLOG(x, LIGHT_WARN) 42 | 43 | bool light_file_write_uint64 (char const *filename, uint64_t val); 44 | bool light_file_read_uint64 (char const *filename, uint64_t *val); 45 | 46 | bool light_file_exists (char const *filename); 47 | bool light_file_is_writable (char const *filename); 48 | bool light_file_is_readable (char const *filename); 49 | 50 | uint64_t light_log_clamp_min(uint64_t min); 51 | uint64_t light_log_clamp_max(uint64_t max); 52 | 53 | double light_percent_clamp(double percent); 54 | 55 | int light_mkpath(char *dir, mode_t mode); 56 | 57 | -------------------------------------------------------------------------------- /light.1: -------------------------------------------------------------------------------- 1 | .\" -*- nroff -*- 2 | .Dd August 4, 2018 3 | .Os GNU/Linux 4 | .Dt LIGHT 1 URM 5 | .Sh NAME 6 | .Nm light 7 | .Nd a program to control backlight controllers 8 | .Sh SYNOPSIS 9 | .Nm light 10 | .Op Ar OPTIONS 11 | .Op Ar VALUE 12 | .Sh DESCRIPTION 13 | .Nm 14 | is a program to control backlight display and keyboard controllers under 15 | GNU/Linux. 16 | .Pp 17 | .Bl -bullet -compact 18 | .It 19 | Operates independently of X (X-Window) 20 | .It 21 | Can automatically figure out the best controller to use, making full use 22 | of the underlying hardware 23 | .It 24 | Supports a minimum cap on the brightness value, as some controllers set 25 | the display to be pitch black at a vaĺue of 0 (or higher) 26 | .El 27 | .Sh COMMANDS 28 | The following unique commands are supported: 29 | .Pp 30 | .Bl -tag -width Ds 31 | .It Fl H , Fl h 32 | Show help text and exit 33 | .It Fl V 34 | Show program version and exit 35 | .It Fl L 36 | List available devices 37 | .It Fl A 38 | Increase brightness by value 39 | .It Fl U 40 | Decrease brightness by value 41 | .It Fl S 42 | Set brightness to value 43 | .It Fl G 44 | Get brightness, default 45 | .It Fl N 46 | Set minimum brightness to value 47 | .It Fl P 48 | Get minimum brightness 49 | .It Fl O 50 | Save current brightness 51 | .It Fl I 52 | Restore previously saved brightness 53 | .El 54 | .Sh OPTIONS 55 | The behavior of the above commands can be modified using these options: 56 | .Pp 57 | .Bl -tag -width Ds 58 | .It Fl r 59 | Interpret input and output values in raw mode 60 | .It Fl s Ar PATH 61 | Specify device target path. Use 62 | .Fl L 63 | to list available devices 64 | .It Fl v Ar LEVEL 65 | Set verbosity level, by default 66 | .Nm 67 | only outputs read values: 68 | .Pp 69 | .Bl -tag -width 0: -compact 70 | .It 0: 71 | Read values 72 | .It 1: 73 | Read values, Errors 74 | .It 2: 75 | Read values, Errors, Warnings 76 | .It 3: 77 | Read values, Errors, Warnings, Notices 78 | .El 79 | .El 80 | .Sh FILES 81 | When run in its classic SUID root mode 82 | .Nm 83 | caches settings and current brightness in 84 | .Pa /etc/light . 85 | In its non-privileged mode of operation the 86 | .Pa ~/.cache/light 87 | directory is used instead. 88 | .Sh AUTHORS 89 | Copyright \(co 2012-2018 Fredrik Haikarainen 90 | .Pp 91 | This is free software, see the source for copying conditions. There is NO 92 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE 93 | -------------------------------------------------------------------------------- /src/helpers.c: -------------------------------------------------------------------------------- 1 | #include "helpers.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include // access 7 | #include 8 | #include 9 | #include // errno 10 | #include // dirname 11 | 12 | 13 | bool light_file_read_uint64(char const *filename, uint64_t *val) 14 | { 15 | FILE *fp; 16 | uint64_t data; 17 | 18 | fp = fopen(filename, "r"); 19 | if(!fp) 20 | { 21 | LIGHT_PERMERR("reading"); 22 | return false; 23 | } 24 | 25 | if(fscanf(fp, "%lu", &data) != 1) 26 | { 27 | LIGHT_ERR("Couldn't parse an unsigned integer from '%s'", filename); 28 | fclose(fp); 29 | return false; 30 | } 31 | 32 | *val = data; 33 | 34 | fclose(fp); 35 | return true; 36 | } 37 | 38 | bool light_file_write_uint64(char const *filename, uint64_t val) 39 | { 40 | FILE *fp; 41 | 42 | fp = fopen(filename, "w"); 43 | if(!fp) 44 | { 45 | LIGHT_PERMERR("writing"); 46 | return false; 47 | } 48 | 49 | if(fprintf(fp, "%lu", val) < 0) 50 | { 51 | LIGHT_ERR("fprintf failed"); 52 | fclose(fp); 53 | return false; 54 | } 55 | 56 | fclose(fp); 57 | return true; 58 | } 59 | 60 | bool light_file_exists (char const *filename) 61 | { 62 | return access( filename, F_OK ) != -1; 63 | } 64 | 65 | /* Returns true if file is writable, false otherwise */ 66 | bool light_file_is_writable(char const *filename) 67 | { 68 | FILE *fp; 69 | 70 | fp = fopen(filename, "r+"); 71 | if(!fp) 72 | { 73 | LIGHT_PERMWARN("writing"); 74 | return false; 75 | } 76 | 77 | fclose(fp); 78 | return true; 79 | } 80 | 81 | /* Returns true if file is readable, false otherwise */ 82 | bool light_file_is_readable(char const *filename) 83 | { 84 | FILE *fp; 85 | 86 | fp = fopen(filename, "r"); 87 | if(!fp) 88 | { 89 | LIGHT_PERMWARN("reading"); 90 | return false; 91 | } 92 | 93 | fclose(fp); 94 | return true; 95 | } 96 | 97 | /* Prints a notice about a value which was below `x` and was adjusted to it */ 98 | uint64_t light_log_clamp_min(uint64_t min) 99 | { 100 | LIGHT_NOTE("too small value, adjusting to minimum %lu (raw)", min); 101 | return min; 102 | } 103 | 104 | /* Prints a notice about a value which was above `x` and was adjusted to it */ 105 | uint64_t light_log_clamp_max(uint64_t max) 106 | { 107 | LIGHT_NOTE("too large value, adjusting to maximum %lu (raw)", max); 108 | return max; 109 | } 110 | 111 | /* Clamps the `percent` value between 0% and 100% */ 112 | double light_percent_clamp(double val) 113 | { 114 | if(val < 0.0) 115 | { 116 | LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val); 117 | return 0.0; 118 | } 119 | 120 | if(val > 100.0) 121 | { 122 | LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val); 123 | return 100.0; 124 | } 125 | 126 | return val; 127 | } 128 | 129 | int light_mkpath(char *dir, mode_t mode) 130 | { 131 | struct stat sb; 132 | 133 | if(!dir) 134 | { 135 | errno = EINVAL; 136 | return -1; 137 | } 138 | 139 | if(!stat(dir, &sb)) 140 | return 0; 141 | 142 | char *tempdir = strdup(dir); 143 | light_mkpath(dirname(tempdir), mode); 144 | free(tempdir); 145 | 146 | return mkdir(dir, mode); 147 | } 148 | 149 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | All relevant changes to the project are documented in this file. 5 | 6 | 7 | [v1.2][] - 2018-09-23 8 | --------------------- 9 | 10 | ### Changes 11 | - Converted to GNU configure & build system 12 | - Major rewrite to add a device system, folds in all kinds of 13 | display/keyboard/LED controllers under one roof. Note, this 14 | change break command line options from previous releases 15 | - Support for installing as non-SUID root using an udev rule 16 | enabled `--with-udev` to the new configure script 17 | - Migrated to use `~/.cache/light` instead of `/etc/light` for 18 | unpriviliged operation. Respects XDG_CACHE_HOME if set 19 | - Added proper light.1 man page, remvoes help2man dependency 20 | - Update presentation of commands and options in built-in help text, 21 | as well as in the README and man page 22 | - Overhaul of coding style, see DOCUMENTATION.md for details 23 | - Add Fedora installation instructions 24 | 25 | 26 | [v1.1.2][] - 2018-06-20 27 | ----------------------- 28 | 29 | Panic release to fix save/restore. 30 | 31 | ### Changes 32 | - Add help2man dependency in README 33 | - Better Support for Overriding Install Prefix 34 | - Restore DESTDIR support 35 | 36 | ### Fixes 37 | - Issue #29: Fix save and restore arguments 38 | - Issue #27: Use the install command instead of raw cp/mv/chmod. 39 | 40 | 41 | [v1.1][] - 2017-11-23 42 | --------------------- 43 | 44 | Various fixes and improvements. Credits to Abdullah ibn Nadjo 45 | 46 | ### Changes 47 | - Add `-k` flag for keyboard backlight support 48 | - Cache max brightness data from automatic controller detection 49 | - Improve overall logging 50 | - Logging of clamps, saves and restores 51 | - Support for save, restore, get [max] brightness etc. for both screen 52 | and keyboard controllers 53 | 54 | ### Fixes 55 | - Avoid checking for write permission if just getting value 56 | - Check if controller is accessible before getting value 57 | - Avoid redondant checking 58 | - Don't truncate file contents when checking if file is writable 59 | - Fix `light_controllerAccessible()` and `light_getBrightness()` this 60 | functions were: 61 | - Reading values from the controller 62 | - Checking write permission even when we just want reading values 63 | - Checking the mincap file instead of the actual controller 64 | - Don't try to read brightness values when only targetting max bright 65 | - Fix issues with string buffers and pointers 66 | - Use `NAME_MAX` and `PATH_MAX` instead of hardcoded values 67 | - Allow paths to be longer than 256 chars 68 | - Check pointers everywhere 69 | - Use `strncpy()`/`snprintf()` instead of `strcpy()`/`sprintf()` 70 | - Validate controllers' name (`-s` flag + a very long name = bad 71 | things happening) 72 | - Get rid of globals for dir iteration 73 | 74 | 75 | [v1.0][] - 2016-05-10 76 | --------------------- 77 | 78 | First major release. Light has been around for a while now and seems to 79 | make some people happy. Also someone wanted a new release, so here you 80 | go! 81 | 82 | ### Changes 83 | - Added save/restore functionality 84 | - Generate man page on `make install` 85 | 86 | ### Fixes 87 | - Issue #5: Can't increase brightness on ATI propietary driver 88 | - Issue #10: Honor `$DESTDIR` on man page installation 89 | 90 | 91 | [v0.9][] - 2014-06-08 92 | --------------------- 93 | 94 | ### Changes 95 | - Complete rewrite of program (Every single byte) 96 | - Cleaner, safer code 97 | - Completely new intuitive usage (Sorry, it was needed) 98 | - Added functionality: 99 | - Ability to set/get minimum brightness directly from commandline 100 | - Ability to specify the backlight controller to use directly from commandline 101 | - Better verbosity 102 | - Probably missed some stuff 103 | 104 | 105 | v0.7 - 2012-11-18 106 | ----------------- 107 | 108 | ### Changes 109 | - Ported bash script to C 110 | 111 | 112 | [UNRELEASED]: https://github.com/haikarainen/light/compare/v1.2...HEAD 113 | [v1.2]: https://github.com/haikarainen/light/compare/v1.1.2...v1.2 114 | [v1.1.2]: https://github.com/haikarainen/light/compare/v1.1...v1.1.2 115 | [v1.1]: https://github.com/haikarainen/light/compare/v1.0...v1.1 116 | [v1.0]: https://github.com/haikarainen/light/compare/v0.9...v1.0 117 | [v0.9]: https://github.com/haikarainen/light/compare/v0.7...v0.9 118 | -------------------------------------------------------------------------------- /src/impl/razer.c: -------------------------------------------------------------------------------- 1 | 2 | #include "impl/razer.h" 3 | #include "light.h" 4 | #include "helpers.h" 5 | 6 | #include //snprintf 7 | #include // malloc, free 8 | #include // opendir, readdir 9 | 10 | static void _impl_razer_add_target(light_device_t *device, char const *name, char const *filename, uint64_t max_brightness) 11 | { 12 | impl_razer_data_t *target_data = malloc(sizeof(impl_razer_data_t)); 13 | snprintf(target_data->brightness, sizeof(target_data->brightness), "/sys/bus/hid/drivers/razerkbd/%s/%s", device->name, filename); 14 | target_data->max_brightness = max_brightness; 15 | 16 | // Only add targets that actually exist, as we aren't fully sure exactly what targets exist for a given device 17 | if(light_file_exists(target_data->brightness)) 18 | { 19 | light_create_device_target(device, name, impl_razer_set, impl_razer_get, impl_razer_getmax, impl_razer_command, target_data); 20 | } 21 | else 22 | { 23 | LIGHT_WARN("razer: couldn't add target %s to device %s, the file %s doesn't exist", name, device->name, filename); 24 | // target_data will not be freed automatically if we dont add a device target with it as userdata, so free it here 25 | free(target_data); 26 | } 27 | } 28 | 29 | static void _impl_razer_add_device(light_device_enumerator_t *enumerator, char const *device_id) 30 | { 31 | // Create a new razer device 32 | light_device_t *new_device = light_create_device(enumerator, device_id, NULL); 33 | 34 | // Setup a target to backlight 35 | _impl_razer_add_target(new_device, "backlight", "matrix_brightness", 255); 36 | 37 | // Setup targets to different possible leds 38 | _impl_razer_add_target(new_device, "game_led", "game_led_state", 1); 39 | _impl_razer_add_target(new_device, "macro_led", "macro_led_state", 1); 40 | _impl_razer_add_target(new_device, "logo_led", "logo_led_state", 1); 41 | _impl_razer_add_target(new_device, "profile_led_r", "profile_led_red", 1); 42 | _impl_razer_add_target(new_device, "profile_led_g", "profile_led_green", 1); 43 | _impl_razer_add_target(new_device, "profile_led_b", "profile_led_blue", 1); 44 | } 45 | 46 | bool impl_razer_init(light_device_enumerator_t *enumerator) 47 | { 48 | // Iterate through the led controllers and create a device_target for each controller 49 | DIR *razer_dir; 50 | struct dirent *curr_entry; 51 | 52 | if((razer_dir = opendir("/sys/bus/hid/drivers/razerkbd/")) == NULL) 53 | { 54 | // Razer driver isnt properly installed, so we cant add devices in this enumerator 55 | return true; 56 | } 57 | 58 | while((curr_entry = readdir(razer_dir)) != NULL) 59 | { 60 | // Skip dot entries 61 | if(curr_entry->d_name[0] == '.') 62 | { 63 | continue; 64 | } 65 | 66 | _impl_razer_add_device(enumerator, curr_entry->d_name); 67 | } 68 | 69 | closedir(razer_dir); 70 | 71 | return true; 72 | } 73 | 74 | bool impl_razer_free(light_device_enumerator_t *enumerator) 75 | { 76 | return true; 77 | } 78 | 79 | bool impl_razer_set(light_device_target_t *target, uint64_t in_value) 80 | { 81 | impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data; 82 | 83 | if(!light_file_write_uint64(data->brightness, in_value)) 84 | { 85 | LIGHT_ERR("failed to write to razer device"); 86 | return false; 87 | } 88 | 89 | return true; 90 | } 91 | 92 | bool impl_razer_get(light_device_target_t *target, uint64_t *out_value) 93 | { 94 | impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data; 95 | 96 | if(!light_file_read_uint64(data->brightness, out_value)) 97 | { 98 | LIGHT_ERR("failed to read from razer device"); 99 | return false; 100 | } 101 | 102 | return true; 103 | } 104 | 105 | bool impl_razer_getmax(light_device_target_t *target, uint64_t *out_value) 106 | { 107 | impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data; 108 | 109 | *out_value = data->max_brightness; 110 | 111 | return true; 112 | } 113 | 114 | bool impl_razer_command(light_device_target_t *target, char const *command_string) 115 | { 116 | // No current need for custom commands in sysfs enumerator 117 | // To implement support, simply parse the command string to your liking, and return false on invalid input or results! 118 | return true; 119 | } 120 | 121 | 122 | -------------------------------------------------------------------------------- /src/light.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include // NAME_MAX 7 | #include // NULL 8 | 9 | #include "config.h" 10 | 11 | #define LIGHT_YEAR "2012 - 2018" 12 | #define LIGHT_AUTHOR "Fredrik Haikarainen" 13 | 14 | struct _light_device_target_t; 15 | typedef struct _light_device_target_t light_device_target_t; 16 | 17 | struct _light_device_t; 18 | typedef struct _light_device_t light_device_t; 19 | 20 | struct _light_device_enumerator_t; 21 | typedef struct _light_device_enumerator_t light_device_enumerator_t; 22 | 23 | /* Function pointers that implementations have to set for device targets */ 24 | typedef bool (*LFUNCVALSET)(light_device_target_t*, uint64_t); 25 | typedef bool (*LFUNCVALGET)(light_device_target_t*, uint64_t*); 26 | typedef bool (*LFUNCMAXVALGET)(light_device_target_t*, uint64_t*); 27 | typedef bool (*LFUNCCUSTOMCMD)(light_device_target_t*, char const *); 28 | 29 | /* Describes a target within a device (for example a led on a keyboard, or a controller for a backlight) */ 30 | struct _light_device_target_t 31 | { 32 | char name[256]; 33 | LFUNCVALSET set_value; 34 | LFUNCVALGET get_value; 35 | LFUNCMAXVALGET get_max_value; 36 | LFUNCCUSTOMCMD custom_command; 37 | void *device_target_data; 38 | light_device_t *device; 39 | }; 40 | 41 | /* Describes a device (a backlight, a keyboard, a led-strip) */ 42 | struct _light_device_t 43 | { 44 | char name[256]; 45 | light_device_target_t **targets; 46 | uint64_t num_targets; 47 | void *device_data; 48 | light_device_enumerator_t *enumerator; 49 | }; 50 | 51 | 52 | typedef bool (*LFUNCENUMINIT)(light_device_enumerator_t*); 53 | typedef bool (*LFUNCENUMFREE)(light_device_enumerator_t*); 54 | 55 | /* An enumerator that is responsible for creating and freeing devices as well as their targets */ 56 | struct _light_device_enumerator_t 57 | { 58 | char name[256]; 59 | LFUNCENUMINIT init; 60 | LFUNCENUMFREE free; 61 | 62 | light_device_t **devices; 63 | uint64_t num_devices; 64 | }; 65 | 66 | typedef struct _light_context_t light_context_t; 67 | 68 | // A command that can be run (set, get, add, subtract, print help, print version, list devices etc.) 69 | typedef bool (*LFUNCCOMMAND)(light_context_t *); 70 | 71 | struct _light_context_t 72 | { 73 | struct 74 | { 75 | LFUNCCOMMAND command; // What command was issued 76 | // Only one of value and raw_value is populated; which one depends on the command 77 | uint64_t value; // The input value, in raw mode 78 | float float_value; // The input value as a float 79 | bool raw_mode; // Whether or not we use raw or percentage mode 80 | light_device_target_t *device_target; // The device target to act on 81 | } run_params; 82 | 83 | struct 84 | { 85 | char conf_dir[NAME_MAX]; // The path to the application cache directory 86 | } sys_params; 87 | 88 | light_device_enumerator_t **enumerators; 89 | uint64_t num_enumerators; 90 | }; 91 | 92 | // The different available commands 93 | bool light_cmd_print_help(light_context_t *ctx); // H,h 94 | bool light_cmd_print_version(light_context_t *ctx); // V 95 | bool light_cmd_list_devices(light_context_t *ctx); // L 96 | bool light_cmd_set_brightness(light_context_t *ctx); // S 97 | bool light_cmd_get_brightness(light_context_t *ctx); // G 98 | bool light_cmd_get_max_brightness(light_context_t *ctx); // M 99 | bool light_cmd_set_min_brightness(light_context_t *ctx); // N 100 | bool light_cmd_get_min_brightness(light_context_t *ctx); // P 101 | bool light_cmd_add_brightness(light_context_t *ctx); // A 102 | bool light_cmd_sub_brightness(light_context_t *ctx); // U 103 | bool light_cmd_mul_brightness(light_context_t *ctx); // T 104 | bool light_cmd_save_brightness(light_context_t *ctx); // O 105 | bool light_cmd_restore_brightness(light_context_t *ctx); // I 106 | 107 | /* Initializes the application, given the command-line. Returns a context. */ 108 | light_context_t* light_initialize(int argc, char **argv); 109 | 110 | /* Executes the given context. Returns true on success, false on failure. */ 111 | bool light_execute(light_context_t*); 112 | 113 | /* Frees the given context */ 114 | void light_free(light_context_t*); 115 | 116 | /* Create a device enumerator in the given context */ 117 | light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT, LFUNCENUMFREE); 118 | 119 | /* Initializes all the device enumerators (and its devices, targets) */ 120 | bool light_init_enumerators(light_context_t *ctx); 121 | 122 | /* Frees all the device enumerators (and its devices, targets) */ 123 | bool light_free_enumerators(light_context_t *ctx); 124 | 125 | /* Use this to create a device. Will automatically be added to enumerator. */ 126 | light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data); 127 | 128 | /* Use this to delete a device. */ 129 | void light_delete_device(light_device_t *device); 130 | 131 | /* Use this to create a device target. Will automatically be added to device. */ 132 | 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); 133 | 134 | /* Use this to delete a device target. */ 135 | void light_delete_device_target(light_device_target_t *device_target); 136 | 137 | typedef struct _light_target_path_t light_target_path_t; 138 | struct _light_target_path_t 139 | { 140 | char enumerator[NAME_MAX]; 141 | char device[NAME_MAX]; 142 | char target[NAME_MAX]; 143 | }; 144 | 145 | bool light_split_target_path(char const * in_path, light_target_path_t *out_path); 146 | 147 | /* Returns the found device target, or null. Name should be enumerator/device/target */ 148 | light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name); 149 | 150 | -------------------------------------------------------------------------------- /src/impl/sysfs.c: -------------------------------------------------------------------------------- 1 | 2 | #include "impl/sysfs.h" 3 | #include "light.h" 4 | #include "helpers.h" 5 | 6 | #include //snprintf 7 | #include // malloc, free 8 | #include // opendir, readdir 9 | 10 | static bool _impl_sysfs_init_leds(light_device_enumerator_t *enumerator) 11 | { 12 | // Create a new backlight device 13 | light_device_t *leds_device = light_create_device(enumerator, "leds", NULL); 14 | 15 | // Iterate through the led controllers and create a device_target for each controller 16 | DIR *leds_dir; 17 | struct dirent *curr_entry; 18 | 19 | if((leds_dir = opendir("/sys/class/leds")) == NULL) 20 | { 21 | LIGHT_ERR("failed to open leds controller directory for reading"); 22 | return false; 23 | } 24 | 25 | while((curr_entry = readdir(leds_dir)) != NULL) 26 | { 27 | // Skip dot entries 28 | if(curr_entry->d_name[0] == '.') 29 | { 30 | continue; 31 | } 32 | 33 | // Setup the target data 34 | impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t)); 35 | snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/leds/%s/brightness", curr_entry->d_name); 36 | snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/leds/%s/max_brightness", curr_entry->d_name); 37 | 38 | // Create a new device target for the controller 39 | light_create_device_target(leds_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data); 40 | } 41 | 42 | closedir(leds_dir); 43 | 44 | return true; 45 | } 46 | 47 | static bool _impl_sysfs_init_backlight(light_device_enumerator_t *enumerator) 48 | { 49 | // Create a new backlight device 50 | light_device_t *backlight_device = light_create_device(enumerator, "backlight", NULL); 51 | 52 | // Iterate through the backlight controllers and create a device_target for each controller 53 | DIR *backlight_dir; 54 | struct dirent *curr_entry; 55 | 56 | // Keep track of the best controller, and create an autodevice from that 57 | char best_controller[NAME_MAX]; 58 | uint64_t best_value = 0; 59 | 60 | if((backlight_dir = opendir("/sys/class/backlight")) == NULL) 61 | { 62 | LIGHT_ERR("failed to open backlight controller directory for reading"); 63 | return false; 64 | } 65 | 66 | while((curr_entry = readdir(backlight_dir)) != NULL) 67 | { 68 | // Skip dot entries 69 | if(curr_entry->d_name[0] == '.') 70 | { 71 | continue; 72 | } 73 | 74 | // Setup the target data 75 | impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t)); 76 | snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", curr_entry->d_name); 77 | snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", curr_entry->d_name); 78 | 79 | // Create a new device target for the controller 80 | light_create_device_target(backlight_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data); 81 | 82 | // Read the max brightness to get the best one 83 | uint64_t curr_value = 0; 84 | if(light_file_read_uint64(dev_data->max_brightness, &curr_value)) 85 | { 86 | if(curr_value > best_value) 87 | { 88 | best_value = curr_value; 89 | snprintf(best_controller, sizeof(best_controller), "%s", curr_entry->d_name); 90 | } 91 | } 92 | } 93 | 94 | closedir(backlight_dir); 95 | 96 | // If we found at least one usable controller, create an auto target mapped to that controller 97 | if(best_value > 0) 98 | { 99 | // Setup the target data 100 | impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t)); 101 | snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", best_controller); 102 | snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", best_controller); 103 | 104 | // Create a new device target for the controller 105 | light_create_device_target(backlight_device, "auto", impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data); 106 | } 107 | 108 | return true; 109 | } 110 | 111 | bool impl_sysfs_init(light_device_enumerator_t *enumerator) 112 | { 113 | // Create a device for the backlight 114 | _impl_sysfs_init_backlight(enumerator); 115 | 116 | // Create devices for the leds 117 | _impl_sysfs_init_leds(enumerator); 118 | 119 | return true; 120 | } 121 | 122 | bool impl_sysfs_free(light_device_enumerator_t *enumerator) 123 | { 124 | return true; 125 | } 126 | 127 | bool impl_sysfs_set(light_device_target_t *target, uint64_t in_value) 128 | { 129 | impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data; 130 | 131 | if(!light_file_write_uint64(data->brightness, in_value)) 132 | { 133 | LIGHT_ERR("failed to write to sysfs device"); 134 | return false; 135 | } 136 | 137 | return true; 138 | } 139 | 140 | bool impl_sysfs_get(light_device_target_t *target, uint64_t *out_value) 141 | { 142 | impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data; 143 | 144 | if(!light_file_read_uint64(data->brightness, out_value)) 145 | { 146 | LIGHT_ERR("failed to read from sysfs device"); 147 | return false; 148 | } 149 | 150 | return true; 151 | } 152 | 153 | bool impl_sysfs_getmax(light_device_target_t *target, uint64_t *out_value) 154 | { 155 | impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data; 156 | 157 | if(!light_file_read_uint64(data->max_brightness, out_value)) 158 | { 159 | LIGHT_ERR("failed to read from sysfs device"); 160 | return false; 161 | } 162 | 163 | return true; 164 | } 165 | 166 | bool impl_sysfs_command(light_device_target_t *target, char const *command_string) 167 | { 168 | // No current need for custom commands in sysfs enumerator 169 | // To implement support, simply parse the command string to your liking, and return false on invalid input or results! 170 | return true; 171 | } 172 | 173 | 174 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Light - A program to control backlights (and other hardware lights) in GNU/Linux 2 | ================================================== 3 | 4 | *Copyright (C) 2012 - 2018* 5 | 6 | *Author: Fredrik Haikarainen* 7 | 8 | *Contributor & Maintainer: Joachim Nilsson* 9 | 10 | *This is free software, see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE* 11 | 12 | 13 | - [Introduction](#introduction) 14 | - [Examples](#examples) 15 | - [Usage](#usage) 16 | - [Command options](#command-options) 17 | - [Extra options](#extra-options) 18 | - [Installation](#installation) 19 | - [Arch Linux](#arch-linux) 20 | - [Fedora](#fedora) 21 | - [Debian/Ubuntu](#debian) 22 | - [NixOS/nix](#nix) 23 | - [Manual](#manual) 24 | - [Permissions](#permissions) 25 | 26 | 27 | Introduction 28 | ------------ 29 | 30 | [Light][] is a program to control backlights and other lights under GNU/Linux: 31 | 32 | * Works where other software has proven unreliable (xbacklight etc.) 33 | * Works even in a fully CLI-environment, i.e. it does not rely on X 34 | * Provides functionality to automatically control backlights with the highest precision available 35 | * Extra features, like setting a minimum brightness value for controllers, or saving/restoring the value for poweroffs/boots. 36 | 37 | See the following sections for the detailed descriptions of all available commands, options and how to access different controllers. 38 | 39 | Light is available in many GNU/Linux distributions already. 40 | 41 | 42 | Examples 43 | -------- 44 | 45 | Get the current backlight brightness in percent 46 | 47 | light -G 48 | 49 | or 50 | 51 | light 52 | 53 | Increase backlight brightness by 5 percent 54 | 55 | light -A 5 56 | 57 | Set the minimum cap to 2 in raw value on the sysfs/backlight/acpi_video0 device: 58 | 59 | light -Nrs "sysfs/backlight/acpi_video0" 2 60 | 61 | List available devices 62 | 63 | light -L 64 | 65 | Activate the Num. Lock keyboard LED, here `sysfs/leds/input3::numlock` is used, but this varies 66 | between different systems: 67 | 68 | light -Srs "sysfs/leds/input3::numlock" 1 69 | 70 | 71 | Usage 72 | ----- 73 | 74 | Usage follows the following pattern, where options are optional and the neccesity of value depends on the options used 75 | 76 | light [options] 77 | 78 | ### Command options 79 | 80 | You may only specify one command flag at a time. These flags decide what the program will ultimately end up doing. 81 | 82 | * `-H` Show help and exit 83 | * `-V` Show program version and exit 84 | * `-L` List available devices 85 | * `-A` Increase brightness by value (value needed!) 86 | * `-U` Decrease brightness by value (value needed!) 87 | * `-S` Set brightness to value (value needed!) 88 | * `-G` Get brightness 89 | * `-N` Set minimum brightness to value (value needed!) 90 | * `-P` Get minimum brightness 91 | * `-O` Save the current brightness 92 | * `-I` Restore the previously saved brightness 93 | 94 | 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. 95 | 96 | **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. 97 | 98 | ### Extra options 99 | 100 | These can be mixed, combined and matched after convenience. 101 | 102 | * `-r` Raw mode, values (printed and interpreted from commandline) will be treated as integers in the controllers native range, instead of in percent. 103 | * `-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. 104 | * `-s ` Specifies which device to work on. List available devices with the -L command. Full path is needed. 105 | 106 | 107 | Installation 108 | ------------ 109 | 110 | ### Arch Linux 111 | 112 | The latest stable release is available in official repos, install with: 113 | 114 | pacman -S light 115 | 116 | Additionally, the latest development branch (master) is available on AUR: [light-git][] 117 | 118 | ### Fedora 119 | 120 | Fedora already has light packaged in main repos, so just run: 121 | 122 | dnf install light 123 | 124 | and you're good to go. 125 | 126 | ### Debian/Ubuntu 127 | 128 | Pre-built .deb files, for the latest Ubuntu release, can be downloaded 129 | from the [GitHub](https://github.com/haikarainen/light/releases/) releases page. If you want to build your own 130 | there is native support available in the GIT sources. Clone and follow 131 | the development branch guidelines below followed by: 132 | 133 | make deb 134 | 135 | **Note:** you must be in the `video` group. Add yourself to it by running `sudo usermod -a -G video $USER`. 136 | 137 | ### NixOS/nix 138 | 139 | You can add the following line to your `configuration.nix`: 140 | 141 | programs.light.enable = true; 142 | 143 | For more detail on Backlight control in NixOS and setting system keybindings, visit the [NixOS Wiki page](https://nixos.wiki/wiki/Backlight) 144 | 145 | ### Manual 146 | 147 | If you download a stable release, these are the commands that will get you up and running: 148 | 149 | tar xf light-x.yy.tar.gz 150 | cd light-x.yy/ 151 | ./configure && make 152 | sudo make install 153 | 154 | 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. 155 | 156 | ./autogen.sh 157 | ./configure && make 158 | sudo make install 159 | 160 | The `configure` script and `Makefile.in` files are not part of GIT because they are generated at release time with `make release`. 161 | 162 | 163 | ### Permissions 164 | 165 | 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: 166 | 167 | ./configure --with-udev && make 168 | sudo make install 169 | 170 | This installs the `90-backlight.rules` into `/usr/lib/udev/rules.d/`. 171 | If your udev rules are located elsewhere, use `--with-udev=PATH`. 172 | 173 | **Note:** make sure that your user is part of the `video` group, otherwise you will not get access to the devices. 174 | 175 | **Note:** in this mode `light` runs unpriviliged, so the `/etc/light` 176 | directory (for cached settings) is not used, instead the per-user 177 | specific `~/.config/light` is used. 178 | 179 | 180 | [Light]: https://github.com/haikarainen/light/ 181 | [light-git]: https://aur.archlinux.org/packages/light-git 182 | -------------------------------------------------------------------------------- /DOCUMENTATION.md: -------------------------------------------------------------------------------- 1 | # Developer Instructions 2 | 3 | This file is aimed at developers of light, or developers who want to implement "drivers" (enumerators) for their own hardware. 4 | 5 | ## Coding Standards 6 | 7 | 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. 8 | 9 | We use 4 spaces for indentation. We have an empty line at the top and bottom of each file. 10 | 11 | The following two sources should be clear enough examples of our coding style: 12 | 13 | ### Header files 14 | 15 | ```c 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | #include /* foo_type_t */ 22 | 23 | 24 | typedef struct _some_struct_t some_struct_t; 25 | struct _some_struct_t 26 | { 27 | uint64_t id; 28 | foo_type_t my_foo_thing; 29 | foo_type_t *my_foo_ptr; 30 | } 31 | 32 | /* Describe what the purpose of this function is, what it does with/to foo_struct, and what it returns. */ 33 | bool do_some_stuff(some_struct_t *foo_struct); 34 | 35 | ``` 36 | 37 | ### Source files 38 | 39 | The second line of each source file should be the include to the corresponding header file, followed by an empty line. 40 | 41 | Internal/static functions are always prefixed with an underscore (_). 42 | 43 | ```c 44 | 45 | #include "some_struct.h" 46 | 47 | static void _increment_one(uint64_t *out_value) 48 | { 49 | *out_value += 1; 50 | } 51 | 52 | bool do_some_stuff(some_struct_t *foo_struct) 53 | { 54 | _increment_one(foo_struct->id); 55 | 56 | if(foo_struct->id > 33) 57 | { 58 | return false; 59 | } 60 | 61 | if(foo_struct->my_foo_ptr != NULL) 62 | { 63 | free(foo_struct->my_foo_ptr); 64 | } 65 | 66 | foo_struct->my_foo_ptr = malloc(sizeof(foo_type_t)); 67 | 68 | return true; 69 | } 70 | 71 | ``` 72 | 73 | ## Implementing an enumerator 74 | 75 | Implementing your own devices through an enumerator is pretty easy. The required steps are as follows: 76 | 77 | ### Step 1 78 | 79 | 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. 80 | 81 | ### Step 2 82 | 83 | 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. 84 | 85 | ```c 86 | 87 | #pragma once 88 | 89 | #include "light.h" 90 | 91 | // Implementation of the foo enumerator 92 | // Enumerates devices for quacking ducks 93 | 94 | // Device target data 95 | struct _impl_foo_data_t 96 | { 97 | int32_t internal_quack_id; 98 | }; 99 | 100 | typedef struct _impl_foo_data_t impl_foo_data_t; 101 | 102 | bool impl_foo_init(light_device_enumerator_t *enumerator); 103 | bool impl_foo_free(light_device_enumerator_t *enumerator); 104 | 105 | bool impl_foo_set(light_device_target_t *target, uint64_t in_value); 106 | bool impl_foo_get(light_device_target_t *target, uint64_t *out_value); 107 | bool impl_foo_getmax(light_device_target_t *target, uint64_t *out_value); 108 | bool impl_foo_command(light_device_target_t *target, char const *command_string); 109 | 110 | ``` 111 | 112 | ### Step 3 113 | 114 | 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`. 115 | 116 | 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. 117 | 118 | ```c 119 | 120 | #include "impl/foo.h" 121 | 122 | #include "light.h" 123 | #include "helpers.h" 124 | 125 | bool impl_foo_init(light_device_enumerator_t *enumerator) 126 | { 127 | /* Lets create a single device, with a single target, for simplicity */ 128 | 129 | /* Create a new device called new_device_name, we dont need any userdata so pass NULL to the device_data parameter */ 130 | light_device_t *new_device = light_create_device(enumerator, "new_device_name", NULL) 131 | 132 | /* Setup userdata specific to the target we will create*/ 133 | /* Useful to for example reference an ID in a third-party API or likewise */ 134 | /* NOTE: The userdata will be free()'d automatically on exit, so you do not need to free it yourself */ 135 | impl_foo_data_t *custom_data = malloc(sizeof(impl_foo_data_t)); 136 | custom_data->internal_quack_id = 333; 137 | 138 | 139 | /* Create a new device target called new_target_name, and pass in the functions and userdata that we just allocated */ 140 | light_create_device_target(new_device, "new_target_name", impl_foo_set, impl_foo_get, impl_foo_getmax, impl_foo_command, custom_data) 141 | 142 | /* Return true because we didnt get any errors! */ 143 | return true; 144 | } 145 | 146 | bool impl_foo_free(light_device_enumerator_t *enumerator) 147 | { 148 | /* We dont need to do anything here, but if we want to, we can free some third-party API resources */ 149 | return true; 150 | } 151 | 152 | /* 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. */ 153 | 154 | ``` 155 | 156 | ### Step 4 157 | 158 | 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: 159 | 160 | 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. 161 | 162 | ```c 163 | light_create_enumerator(new_ctx, "foo", &impl_foo_init, &impl_foo_free); 164 | ``` 165 | 166 | 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. 167 | 168 | 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! 169 | 170 | 171 | ## Troubleshooting 172 | 173 | If you run into any issues, feel free to create a new Github issue, even if you are just asking for "support" or likewise. 174 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /src/light.c: -------------------------------------------------------------------------------- 1 | 2 | #include "light.h" 3 | #include "helpers.h" 4 | 5 | // The different device implementations 6 | #include "impl/sysfs.h" 7 | #include "impl/util.h" 8 | #include "impl/razer.h" 9 | 10 | #include // malloc, free 11 | #include // strstr 12 | #include // snprintf 13 | #include // geteuid 14 | #include // geteuid 15 | #include 16 | #include // PRIu64 17 | 18 | /* Static helper functions for this file only, prefix with _ */ 19 | 20 | 21 | static void _light_add_enumerator_device(light_device_enumerator_t *enumerator, light_device_t *new_device) 22 | { 23 | // Create a new device array 24 | uint64_t new_num_devices = enumerator->num_devices + 1; 25 | light_device_t **new_devices = malloc(new_num_devices * sizeof(light_device_t*)); 26 | 27 | // Copy old device array to new one 28 | for(uint64_t i = 0; i < enumerator->num_devices; i++) 29 | { 30 | new_devices[i] = enumerator->devices[i]; 31 | } 32 | 33 | // Set the new device 34 | new_devices[enumerator->num_devices] = new_device; 35 | 36 | // Free the old devices array, if needed 37 | if(enumerator->devices != NULL) 38 | { 39 | free(enumerator->devices); 40 | } 41 | 42 | // Replace the devices array with the new one 43 | enumerator->devices = new_devices; 44 | enumerator->num_devices = new_num_devices; 45 | } 46 | 47 | static void _light_add_device_target(light_device_t *device, light_device_target_t *new_target) 48 | { 49 | // Create a new targets array 50 | uint64_t new_num_targets = device->num_targets + 1; 51 | light_device_target_t **new_targets = malloc(new_num_targets * sizeof(light_device_target_t*)); 52 | 53 | // Copy old targets array to new one 54 | for(uint64_t i = 0; i < device->num_targets; i++) 55 | { 56 | new_targets[i] = device->targets[i]; 57 | } 58 | 59 | // Set the new target 60 | new_targets[device->num_targets] = new_target; 61 | 62 | // Free the old targets array, if needed 63 | if(device->targets != NULL) 64 | { 65 | free(device->targets); 66 | } 67 | 68 | // Replace the targets array with the new one 69 | device->targets= new_targets; 70 | device->num_targets = new_num_targets; 71 | } 72 | 73 | static void _light_get_target_path(light_context_t* ctx, char* output_path, size_t output_size) 74 | { 75 | snprintf(output_path, output_size, 76 | "%s/targets/%s/%s/%s", 77 | ctx->sys_params.conf_dir, 78 | ctx->run_params.device_target->device->enumerator->name, 79 | ctx->run_params.device_target->device->name, 80 | ctx->run_params.device_target->name 81 | ); 82 | } 83 | 84 | static void _light_get_target_file(light_context_t* ctx, char* output_path, size_t output_size, char const * file) 85 | { 86 | snprintf(output_path, output_size, 87 | "%s/targets/%s/%s/%s/%s", 88 | ctx->sys_params.conf_dir, 89 | ctx->run_params.device_target->device->enumerator->name, 90 | ctx->run_params.device_target->device->name, 91 | ctx->run_params.device_target->name, 92 | file 93 | ); 94 | } 95 | 96 | static uint64_t _light_get_min_cap(light_context_t *ctx) 97 | { 98 | char target_path[NAME_MAX]; 99 | _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum"); 100 | 101 | uint64_t minimum_value = 0; 102 | if(!light_file_read_uint64(target_path, &minimum_value)) 103 | { 104 | return 0; 105 | } 106 | 107 | return minimum_value; 108 | } 109 | 110 | static light_device_enumerator_t* _light_find_enumerator(light_context_t *ctx, char const *comp) 111 | { 112 | for(uint64_t e = 0; e < ctx->num_enumerators; e++) 113 | { 114 | if(strncmp(comp, ctx->enumerators[e]->name, NAME_MAX) == 0) 115 | { 116 | return ctx->enumerators[e]; 117 | } 118 | } 119 | 120 | return NULL; 121 | } 122 | 123 | static light_device_t* _light_find_device(light_device_enumerator_t *en, char const *comp) 124 | { 125 | for(uint64_t d = 0; d < en->num_devices; d++) 126 | { 127 | if(strncmp(comp, en->devices[d]->name, NAME_MAX) == 0) 128 | { 129 | return en->devices[d]; 130 | } 131 | } 132 | 133 | return NULL; 134 | } 135 | 136 | static light_device_target_t* _light_find_target(light_device_t * dev, char const *comp) 137 | { 138 | for(uint64_t t = 0; t < dev->num_targets; t++) 139 | { 140 | if(strncmp(comp, dev->targets[t]->name, NAME_MAX) == 0) 141 | { 142 | return dev->targets[t]; 143 | } 144 | } 145 | 146 | return NULL; 147 | } 148 | 149 | static bool _light_raw_to_percent(light_device_target_t *target, uint64_t inraw, double *outpercent) 150 | { 151 | double inraw_d = (double)inraw; 152 | uint64_t max_value = 0; 153 | if(!target->get_max_value(target, &max_value)) 154 | { 155 | LIGHT_ERR("couldn't read from target"); 156 | return false; 157 | } 158 | double max_value_d = (double)max_value; 159 | double percent = light_percent_clamp((inraw_d / max_value_d) * 100.0); 160 | *outpercent = percent; 161 | 162 | return true; 163 | } 164 | 165 | static bool _light_percent_to_raw(light_device_target_t *target, double inpercent, uint64_t *outraw) 166 | { 167 | uint64_t max_value = 0; 168 | if(!target->get_max_value(target, &max_value)) 169 | { 170 | LIGHT_ERR("couldn't read from target"); 171 | return false; 172 | } 173 | 174 | double max_value_d = (double)max_value; 175 | double target_value_d = max_value_d * (light_percent_clamp(inpercent) / 100.0); 176 | uint64_t target_value = LIGHT_CLAMP((uint64_t)target_value_d, 0, max_value); 177 | *outraw = target_value; 178 | 179 | return true; 180 | } 181 | 182 | static void _light_print_usage() 183 | { 184 | printf("Usage:\n" 185 | " light [OPTIONS] [VALUE]\n" 186 | "\n" 187 | "Commands:\n" 188 | " -H, -h Show this help and exit\n" 189 | " -V Show program version and exit\n" 190 | " -L List available devices\n" 191 | 192 | " -A Increase brightness by value\n" 193 | " -U Decrease brightness by value\n" 194 | " -T Multiply brightness by value (can be a non-whole number, ignores raw mode)\n" 195 | " -S Set brightness to value\n" 196 | " -G Get brightness\n" 197 | " -N Set minimum brightness to value\n" 198 | " -P Get minimum brightness\n" 199 | " -O Save the current brightness\n" 200 | " -I Restore the previously saved brightness\n" 201 | 202 | 203 | "\n" 204 | "Options:\n" 205 | " -r Interpret input and output values in raw mode (ignored for -T)\n" 206 | " -s Specify device target path to use, use -L to list available\n" 207 | " -v Specify the verbosity level (default 0)\n" 208 | " 0: Values only\n" 209 | " 1: Values, Errors.\n" 210 | " 2: Values, Errors, Warnings.\n" 211 | " 3: Values, Errors, Warnings, Notices.\n" 212 | "\n"); 213 | 214 | printf("Copyright (C) %s %s\n", LIGHT_YEAR, LIGHT_AUTHOR); 215 | printf("This is free software, see the source for copying conditions. There is NO\n" 216 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n" 217 | "\n"); 218 | } 219 | 220 | static bool _light_set_context_command(light_context_t *ctx, LFUNCCOMMAND new_cmd) 221 | { 222 | if(ctx->run_params.command != NULL) 223 | { 224 | LIGHT_WARN("a command was already set. ignoring."); 225 | return false; 226 | } 227 | 228 | ctx->run_params.command = new_cmd; 229 | return true; 230 | } 231 | 232 | static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv) 233 | { 234 | int32_t curr_arg = -1; 235 | int32_t log_level = 0; 236 | 237 | char ctrl_name[NAME_MAX]; 238 | bool need_value = false; 239 | bool need_float_value = false; 240 | bool need_target = true; // default cmd is get brightness 241 | bool specified_target = false; 242 | snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto"); 243 | 244 | while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1) 245 | { 246 | switch(curr_arg) 247 | { 248 | // Options 249 | 250 | case 'v': 251 | if(sscanf(optarg, "%i", &log_level) != 1) 252 | { 253 | fprintf(stderr, "-v argument is not an integer.\n\n"); 254 | _light_print_usage(); 255 | return false; 256 | } 257 | 258 | if(log_level < 0 || log_level > 3) 259 | { 260 | fprintf(stderr, "-v argument must be between 0 and 3.\n\n"); 261 | _light_print_usage(); 262 | return false; 263 | } 264 | 265 | light_loglevel = (light_loglevel_t)log_level; 266 | break; 267 | case 's': 268 | snprintf(ctrl_name, sizeof(ctrl_name), "%s", optarg); 269 | specified_target = true; 270 | break; 271 | case 'r': 272 | ctx->run_params.raw_mode = true; 273 | break; 274 | 275 | // Commands 276 | case 'H': 277 | case 'h': 278 | _light_set_context_command(ctx, light_cmd_print_help); 279 | break; 280 | case 'V': 281 | _light_set_context_command(ctx, light_cmd_print_version); 282 | break; 283 | case 'G': 284 | _light_set_context_command(ctx, light_cmd_get_brightness); 285 | need_target = true; 286 | break; 287 | case 'S': 288 | _light_set_context_command(ctx, light_cmd_set_brightness); 289 | need_value = true; 290 | need_target = true; 291 | break; 292 | case 'L': 293 | _light_set_context_command(ctx, light_cmd_list_devices); 294 | break; 295 | case 'M': 296 | _light_set_context_command(ctx, light_cmd_get_max_brightness); 297 | need_target = true; 298 | break; 299 | case 'N': 300 | _light_set_context_command(ctx, light_cmd_set_min_brightness); 301 | need_target = true; 302 | need_value = true; 303 | break; 304 | case 'P': 305 | _light_set_context_command(ctx, light_cmd_get_min_brightness); 306 | need_target = true; 307 | break; 308 | case 'A': 309 | _light_set_context_command(ctx, light_cmd_add_brightness); 310 | need_target = true; 311 | need_value = true; 312 | break; 313 | case 'U': 314 | _light_set_context_command(ctx, light_cmd_sub_brightness); 315 | need_target = true; 316 | need_value = true; 317 | break; 318 | case 'T': 319 | _light_set_context_command(ctx, light_cmd_mul_brightness); 320 | need_target = true; 321 | need_float_value = true; 322 | break; 323 | case 'O': 324 | _light_set_context_command(ctx, light_cmd_save_brightness); 325 | need_target = true; 326 | break; 327 | case 'I': 328 | _light_set_context_command(ctx, light_cmd_restore_brightness); 329 | need_target = true; 330 | break; 331 | } 332 | } 333 | 334 | if(ctx->run_params.command == NULL) 335 | { 336 | _light_set_context_command(ctx, light_cmd_get_brightness); 337 | } 338 | 339 | if(need_target) 340 | { 341 | light_device_target_t *curr_target = light_find_device_target(ctx, ctrl_name); 342 | if(curr_target == NULL) 343 | { 344 | if(specified_target) 345 | { 346 | fprintf(stderr, "We couldn't find the specified device target at the path \"%s\". Use -L to find one.\n\n", ctrl_name); 347 | return false; 348 | } 349 | else 350 | { 351 | 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"); 352 | curr_target = light_find_device_target(ctx, "util/test/dryrun"); 353 | } 354 | } 355 | 356 | ctx->run_params.device_target = curr_target; 357 | } 358 | 359 | if(need_value || need_float_value) 360 | { 361 | if( (argc - optind) != 1) 362 | { 363 | fprintf(stderr, "please specify a for this command.\n\n"); 364 | _light_print_usage(); 365 | return false; 366 | } 367 | } 368 | 369 | if(need_value) 370 | { 371 | if(ctx->run_params.raw_mode) 372 | { 373 | if(sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1) 374 | { 375 | fprintf(stderr, " is not an integer.\n\n"); 376 | _light_print_usage(); 377 | return false; 378 | } 379 | } 380 | else 381 | { 382 | double percent_value = 0.0; 383 | if(sscanf(argv[optind], "%lf", &percent_value) != 1) 384 | { 385 | fprintf(stderr, " is not a decimal.\n\n"); 386 | _light_print_usage(); 387 | return false; 388 | } 389 | 390 | percent_value = light_percent_clamp(percent_value); 391 | 392 | uint64_t raw_value = 0; 393 | if(!_light_percent_to_raw(ctx->run_params.device_target, percent_value, &raw_value)) 394 | { 395 | LIGHT_ERR("failed to convert from percent to raw for device target"); 396 | return false; 397 | } 398 | 399 | ctx->run_params.value = raw_value; 400 | } 401 | } 402 | 403 | if(need_float_value) 404 | { 405 | if(sscanf(argv[optind], "%f", &ctx->run_params.float_value) != 1) 406 | { 407 | fprintf(stderr, " is not a float.\n\n"); 408 | _light_print_usage(); 409 | return false; 410 | } 411 | } 412 | 413 | return true; 414 | 415 | } 416 | 417 | 418 | 419 | 420 | /* API function definitions */ 421 | 422 | light_context_t* light_initialize(int argc, char **argv) 423 | { 424 | light_context_t *new_ctx = malloc(sizeof(light_context_t)); 425 | 426 | // Setup default values and runtime params 427 | new_ctx->enumerators = NULL; 428 | new_ctx->num_enumerators = 0; 429 | new_ctx->run_params.command = NULL; 430 | new_ctx->run_params.device_target = NULL; 431 | new_ctx->run_params.value = 0; 432 | new_ctx->run_params.raw_mode = false; 433 | 434 | uid_t uid = getuid(); 435 | uid_t euid = geteuid(); 436 | gid_t egid = getegid(); 437 | // If the real user ID is different from the effective user ID (SUID mode) 438 | // and if we have the effective user ID of root (0) 439 | // and if the effective group ID is different from root (0), 440 | // then make sure to set the effective group ID to root (0). 441 | if((uid != euid) && (euid == 0) && (egid != 0)) 442 | { 443 | if(setegid(euid) < 0) 444 | { 445 | LIGHT_ERR("could not change egid from %u to %u (uid: %u, euid: %u)", egid, euid, uid, euid); 446 | return false; 447 | } 448 | } 449 | 450 | // Setup the configuration folder 451 | // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config 452 | if(euid == 0) 453 | { 454 | snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light"); 455 | } 456 | else 457 | { 458 | char *xdg_conf = getenv("XDG_CONFIG_HOME"); 459 | 460 | if(xdg_conf != NULL) 461 | { 462 | snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf); 463 | } 464 | else 465 | { 466 | snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME")); 467 | } 468 | } 469 | 470 | // Make sure the configuration folder exists, otherwise attempt to create it 471 | int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 472 | if(rc && errno != EEXIST) 473 | { 474 | LIGHT_WARN("couldn't create configuration directory"); 475 | return false; 476 | } 477 | 478 | // Create the built-in enumerators 479 | light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free); 480 | light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free); 481 | light_create_enumerator(new_ctx, "razer", &impl_razer_init, &impl_razer_free); 482 | 483 | // This is where we would create enumerators from plugins as well 484 | // 1. Run the plugins get_name() function to get its name 485 | // 2. Point to the plugins init() and free() functions when creating the enumerator 486 | 487 | // initialize all enumerators, this will create all the devices and their targets 488 | if(!light_init_enumerators(new_ctx)) 489 | { 490 | LIGHT_WARN("failed to initialize all enumerators"); 491 | } 492 | 493 | // Parse arguments 494 | if(!_light_parse_arguments(new_ctx, argc, argv)) 495 | { 496 | LIGHT_ERR("failed to parse arguments"); 497 | return NULL; 498 | } 499 | 500 | return new_ctx; 501 | } 502 | 503 | bool light_execute(light_context_t *ctx) 504 | { 505 | if(ctx->run_params.command == NULL) 506 | { 507 | LIGHT_ERR("run parameters command was null, can't execute"); 508 | return false; 509 | } 510 | 511 | return ctx->run_params.command(ctx); 512 | } 513 | 514 | void light_free(light_context_t *ctx) 515 | { 516 | if(!light_free_enumerators(ctx)) 517 | { 518 | LIGHT_WARN("failed to free all enumerators"); 519 | } 520 | 521 | free(ctx); 522 | } 523 | 524 | light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT init_func, LFUNCENUMFREE free_func) 525 | { 526 | // Create a new enumerator array 527 | uint64_t new_num_enumerators = ctx->num_enumerators + 1; 528 | light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*)); 529 | 530 | // Copy old enumerator array to new one 531 | for(uint64_t i = 0; i < ctx->num_enumerators; i++) 532 | { 533 | new_enumerators[i] = ctx->enumerators[i]; 534 | } 535 | 536 | // Allocate the new enumerator 537 | new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t)); 538 | light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators]; 539 | 540 | returner->devices = NULL; 541 | returner->num_devices = 0; 542 | returner->init = init_func; 543 | returner->free = free_func; 544 | snprintf(returner->name, sizeof(returner->name), "%s", name); 545 | 546 | // Free the old enumerator array, if needed 547 | if(ctx->enumerators != NULL) 548 | { 549 | free(ctx->enumerators); 550 | } 551 | 552 | // Replace the enumerator array with the new one 553 | ctx->enumerators = new_enumerators; 554 | ctx->num_enumerators = new_num_enumerators; 555 | 556 | // Return newly created device 557 | return returner; 558 | } 559 | 560 | bool light_init_enumerators(light_context_t *ctx) 561 | { 562 | bool success = true; 563 | for(uint64_t i = 0; i < ctx->num_enumerators; i++) 564 | { 565 | light_device_enumerator_t * curr_enumerator = ctx->enumerators[i]; 566 | if(!curr_enumerator->init(curr_enumerator)) 567 | { 568 | success = false; 569 | } 570 | } 571 | 572 | return success; 573 | } 574 | 575 | bool light_free_enumerators(light_context_t *ctx) 576 | { 577 | bool success = true; 578 | for(uint64_t i = 0; i < ctx->num_enumerators; i++) 579 | { 580 | light_device_enumerator_t * curr_enumerator = ctx->enumerators[i]; 581 | 582 | if(!curr_enumerator->free(curr_enumerator)) 583 | { 584 | success = false; 585 | } 586 | 587 | if(curr_enumerator->devices != NULL) 588 | { 589 | for(uint64_t d = 0; d < curr_enumerator->num_devices; d++) 590 | { 591 | light_delete_device(curr_enumerator->devices[d]); 592 | } 593 | 594 | free(curr_enumerator->devices); 595 | curr_enumerator->devices = NULL; 596 | } 597 | 598 | free(curr_enumerator); 599 | } 600 | 601 | free(ctx->enumerators); 602 | ctx->enumerators = NULL; 603 | ctx->num_enumerators = 0; 604 | 605 | return success; 606 | } 607 | 608 | bool light_split_target_path(char const *in_path, light_target_path_t *out_path) 609 | { 610 | char const * begin = in_path; 611 | char const * end = strstr(begin, "/"); 612 | if(end == NULL) 613 | { 614 | LIGHT_WARN("invalid path passed to split_target_path"); 615 | return false; 616 | } 617 | 618 | size_t size = end - begin; 619 | strncpy(out_path->enumerator, begin, size); 620 | out_path->enumerator[size] = '\0'; 621 | 622 | begin = end + 1; 623 | end = strstr(begin, "/"); 624 | if(end == NULL) 625 | { 626 | LIGHT_WARN("invalid path passed to split_target_path"); 627 | return false; 628 | } 629 | 630 | size = end - begin; 631 | strncpy(out_path->device, begin, size); 632 | out_path->device[size] = '\0'; 633 | 634 | strcpy(out_path->target, end + 1); 635 | 636 | return true; 637 | } 638 | 639 | light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name) 640 | { 641 | light_target_path_t new_path; 642 | if(!light_split_target_path(name, &new_path)) 643 | { 644 | LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized: \"%s\"", name); 645 | return NULL; 646 | } 647 | 648 | /* 649 | Uncomment to debug the split function 650 | printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n", 651 | new_path.enumerator, strlen(new_path.enumerator), 652 | new_path.device, strlen(new_path.device), 653 | new_path.target, strlen(new_path.target)); 654 | */ 655 | 656 | // find a matching enumerator 657 | 658 | light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator); 659 | if(enumerator == NULL) 660 | { 661 | LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator); 662 | return NULL; 663 | } 664 | 665 | light_device_t *device = _light_find_device(enumerator, new_path.device); 666 | if(device == NULL) 667 | { 668 | LIGHT_WARN("no such device, \"%s\"", new_path.device); 669 | return NULL; 670 | } 671 | 672 | light_device_target_t *target = _light_find_target(device, new_path.target); 673 | if(target == NULL) 674 | { 675 | LIGHT_WARN("no such target, \"%s\"", new_path.target); 676 | return NULL; 677 | } 678 | 679 | return target; 680 | } 681 | 682 | bool light_cmd_print_help(light_context_t *ctx) 683 | { 684 | _light_print_usage(); 685 | return true; 686 | } 687 | 688 | bool light_cmd_print_version(light_context_t *ctx) 689 | { 690 | printf("v%s\n", VERSION); 691 | return true; 692 | } 693 | 694 | bool light_cmd_list_devices(light_context_t *ctx) 695 | { 696 | printf("Listing device targets:\n"); 697 | for(uint64_t enumerator = 0; enumerator < ctx->num_enumerators; enumerator++) 698 | { 699 | light_device_enumerator_t *curr_enumerator = ctx->enumerators[enumerator]; 700 | for(uint64_t device = 0; device < curr_enumerator->num_devices; device++) 701 | { 702 | light_device_t *curr_device = curr_enumerator->devices[device]; 703 | for(uint64_t target = 0; target < curr_device->num_targets; target++) 704 | { 705 | light_device_target_t *curr_target = curr_device->targets[target]; 706 | 707 | printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name); 708 | } 709 | } 710 | } 711 | 712 | return true; 713 | } 714 | 715 | bool light_cmd_set_brightness(light_context_t *ctx) 716 | { 717 | light_device_target_t *target = ctx->run_params.device_target; 718 | if(target == NULL) 719 | { 720 | LIGHT_ERR("didn't have a valid target, programmer mistake"); 721 | return false; 722 | } 723 | 724 | 725 | uint64_t mincap = _light_get_min_cap(ctx); 726 | uint64_t value = ctx->run_params.value; 727 | if(mincap > value) 728 | { 729 | value = mincap; 730 | } 731 | 732 | if(!target->set_value(target, value)) 733 | { 734 | LIGHT_ERR("failed to write to target"); 735 | return false; 736 | } 737 | 738 | return true; 739 | } 740 | 741 | bool light_cmd_get_brightness(light_context_t *ctx) 742 | { 743 | light_device_target_t *target = ctx->run_params.device_target; 744 | if(target == NULL) 745 | { 746 | LIGHT_ERR("didn't have a valid target, programmer mistake"); 747 | return false; 748 | } 749 | 750 | uint64_t value = 0; 751 | if(!target->get_value(target, &value)) 752 | { 753 | LIGHT_ERR("failed to read from target"); 754 | return false; 755 | } 756 | 757 | if(ctx->run_params.raw_mode) 758 | { 759 | printf("%" PRIu64 "\n", value); 760 | } 761 | else 762 | { 763 | double percent = 0.0; 764 | if(!_light_raw_to_percent(target, value, &percent)) 765 | { 766 | LIGHT_ERR("failed to convert from raw to percent from device target"); 767 | return false; 768 | } 769 | printf("%.2f\n", percent); 770 | } 771 | 772 | return true; 773 | } 774 | 775 | bool light_cmd_get_max_brightness(light_context_t *ctx) 776 | { 777 | light_device_target_t *target = ctx->run_params.device_target; 778 | if(target == NULL) 779 | { 780 | LIGHT_ERR("didn't have a valid target, programmer mistake"); 781 | return false; 782 | } 783 | 784 | if(!ctx->run_params.raw_mode) 785 | { 786 | printf("100.0\n"); 787 | return true; 788 | } 789 | 790 | uint64_t max_value = 0; 791 | if(!target->get_max_value(target, &max_value)) 792 | { 793 | LIGHT_ERR("failed to read from device target"); 794 | return false; 795 | } 796 | 797 | printf("%" PRIu64 "\n", max_value); 798 | return true; 799 | } 800 | 801 | bool light_cmd_set_min_brightness(light_context_t *ctx) 802 | { 803 | char target_path[NAME_MAX]; 804 | _light_get_target_path(ctx, target_path, sizeof(target_path)); 805 | 806 | // Make sure the target folder exists, otherwise attempt to create it 807 | int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 808 | if(rc && errno != EEXIST) 809 | { 810 | LIGHT_ERR("couldn't create target directory for minimum brightness"); 811 | return false; 812 | } 813 | 814 | char target_filepath[NAME_MAX]; 815 | _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum"); 816 | 817 | if(!light_file_write_uint64(target_filepath, ctx->run_params.value)) 818 | { 819 | LIGHT_ERR("couldn't write value to minimum file"); 820 | return false; 821 | } 822 | 823 | return true; 824 | } 825 | 826 | bool light_cmd_get_min_brightness(light_context_t *ctx) 827 | { 828 | char target_path[NAME_MAX]; 829 | _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum"); 830 | 831 | uint64_t minimum_value = 0; 832 | if(!light_file_read_uint64(target_path, &minimum_value)) 833 | { 834 | if(ctx->run_params.raw_mode) 835 | { 836 | printf("0\n"); 837 | } 838 | else 839 | { 840 | printf("0.00\n"); 841 | } 842 | 843 | return true; 844 | } 845 | 846 | if(ctx->run_params.raw_mode) 847 | { 848 | printf("%" PRIu64 "\n", minimum_value); 849 | } 850 | else 851 | { 852 | double minimum_d = 0.0; 853 | if(!_light_raw_to_percent(ctx->run_params.device_target, minimum_value, &minimum_d)) 854 | { 855 | LIGHT_ERR("failed to convert value from raw to percent for device target"); 856 | return false; 857 | } 858 | 859 | printf("%.2f\n", minimum_d); 860 | } 861 | 862 | return true; 863 | } 864 | 865 | bool light_cmd_add_brightness(light_context_t *ctx) 866 | { 867 | light_device_target_t *target = ctx->run_params.device_target; 868 | if(target == NULL) 869 | { 870 | LIGHT_ERR("didn't have a valid target, programmer mistake"); 871 | return false; 872 | } 873 | 874 | uint64_t value = 0; 875 | if(!target->get_value(target, &value)) 876 | { 877 | LIGHT_ERR("failed to read from target"); 878 | return false; 879 | } 880 | 881 | uint64_t max_value = 0; 882 | if(!target->get_max_value(target, &max_value)) 883 | { 884 | LIGHT_ERR("failed to read from target"); 885 | return false; 886 | } 887 | 888 | value += ctx->run_params.value; 889 | 890 | uint64_t mincap = _light_get_min_cap(ctx); 891 | if(mincap > value) 892 | { 893 | value = mincap; 894 | } 895 | 896 | 897 | if(value > max_value) 898 | { 899 | value = max_value; 900 | } 901 | 902 | if(!target->set_value(target, value)) 903 | { 904 | LIGHT_ERR("failed to write to target"); 905 | return false; 906 | } 907 | 908 | return true; 909 | } 910 | 911 | bool light_cmd_sub_brightness(light_context_t *ctx) 912 | { 913 | light_device_target_t *target = ctx->run_params.device_target; 914 | if(target == NULL) 915 | { 916 | LIGHT_ERR("didn't have a valid target, programmer mistake"); 917 | return false; 918 | } 919 | 920 | uint64_t value = 0; 921 | if(!target->get_value(target, &value)) 922 | { 923 | LIGHT_ERR("failed to read from target"); 924 | return false; 925 | } 926 | 927 | if(value > ctx->run_params.value) 928 | { 929 | value -= ctx->run_params.value; 930 | } 931 | else 932 | { 933 | value = 0; 934 | } 935 | 936 | uint64_t mincap = _light_get_min_cap(ctx); 937 | if(mincap > value) 938 | { 939 | value = mincap; 940 | } 941 | 942 | if(!target->set_value(target, value)) 943 | { 944 | LIGHT_ERR("failed to write to target"); 945 | return false; 946 | } 947 | 948 | return true; 949 | } 950 | 951 | bool light_cmd_mul_brightness(light_context_t *ctx) 952 | { 953 | light_device_target_t *target = ctx->run_params.device_target; 954 | if(target == NULL) 955 | { 956 | LIGHT_ERR("didn't have a valid target, programmer mistake"); 957 | return false; 958 | } 959 | 960 | uint64_t value = 0; 961 | if(!target->get_value(target, &value)) 962 | { 963 | LIGHT_ERR("failed to read from target"); 964 | return false; 965 | } 966 | 967 | uint64_t max_value = 0; 968 | if(!target->get_max_value(target, &max_value)) 969 | { 970 | LIGHT_ERR("failed to read from target"); 971 | return false; 972 | } 973 | 974 | uint64_t old_value = value; 975 | value *= ctx->run_params.float_value; 976 | 977 | // Check that we actually de/increase value 978 | if(value == old_value) 979 | { 980 | if(ctx->run_params.float_value > 1) 981 | value++; 982 | if(ctx->run_params.float_value < 1 && value > 0) 983 | value--; 984 | } 985 | 986 | uint64_t mincap = _light_get_min_cap(ctx); 987 | if(mincap > value) 988 | { 989 | value = mincap; 990 | } 991 | 992 | if(value > max_value) 993 | { 994 | value = max_value; 995 | } 996 | 997 | if(!target->set_value(target, value)) 998 | { 999 | LIGHT_ERR("failed to write to target"); 1000 | return false; 1001 | } 1002 | 1003 | return true; 1004 | } 1005 | 1006 | bool light_cmd_save_brightness(light_context_t *ctx) 1007 | { 1008 | char target_path[NAME_MAX]; 1009 | _light_get_target_path(ctx, target_path, sizeof(target_path)); 1010 | 1011 | // Make sure the target folder exists, otherwise attempt to create it 1012 | int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 1013 | if(rc && errno != EEXIST) 1014 | { 1015 | LIGHT_ERR("couldn't create target directory for save brightness"); 1016 | return false; 1017 | } 1018 | 1019 | char target_filepath[NAME_MAX]; 1020 | _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save"); 1021 | 1022 | uint64_t curr_value = 0; 1023 | if(!ctx->run_params.device_target->get_value(ctx->run_params.device_target, &curr_value)) 1024 | { 1025 | LIGHT_ERR("couldn't read from device target"); 1026 | return false; 1027 | } 1028 | 1029 | if(!light_file_write_uint64(target_filepath, curr_value)) 1030 | { 1031 | LIGHT_ERR("couldn't write value to savefile"); 1032 | return false; 1033 | } 1034 | 1035 | return true; 1036 | } 1037 | 1038 | bool light_cmd_restore_brightness(light_context_t *ctx) 1039 | { 1040 | char target_path[NAME_MAX]; 1041 | _light_get_target_file(ctx, target_path, sizeof(target_path), "save"); 1042 | 1043 | uint64_t saved_value = 0; 1044 | if(!light_file_read_uint64(target_path, &saved_value)) 1045 | { 1046 | LIGHT_ERR("couldn't read value from savefile"); 1047 | return false; 1048 | } 1049 | 1050 | uint64_t mincap = _light_get_min_cap(ctx); 1051 | if(mincap > saved_value) 1052 | { 1053 | saved_value = mincap; 1054 | } 1055 | 1056 | if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value)) 1057 | { 1058 | LIGHT_ERR("couldn't write saved value to device target"); 1059 | return false; 1060 | } 1061 | 1062 | return true; 1063 | } 1064 | 1065 | light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data) 1066 | { 1067 | light_device_t *new_device = malloc(sizeof(light_device_t)); 1068 | new_device->enumerator = enumerator; 1069 | new_device->targets = NULL; 1070 | new_device->num_targets = 0; 1071 | new_device->device_data = device_data; 1072 | 1073 | snprintf(new_device->name, sizeof(new_device->name), "%s", name); 1074 | 1075 | _light_add_enumerator_device(enumerator, new_device); 1076 | 1077 | return new_device; 1078 | } 1079 | 1080 | void light_delete_device(light_device_t *device) 1081 | { 1082 | for(uint64_t i = 0; i < device->num_targets; i++) 1083 | { 1084 | light_delete_device_target(device->targets[i]); 1085 | } 1086 | 1087 | if(device->targets != NULL) 1088 | { 1089 | free(device->targets); 1090 | } 1091 | 1092 | if(device->device_data != NULL) 1093 | { 1094 | free(device->device_data); 1095 | } 1096 | 1097 | free(device); 1098 | } 1099 | 1100 | 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) 1101 | { 1102 | light_device_target_t *new_target = malloc(sizeof(light_device_target_t)); 1103 | new_target->device = device; 1104 | new_target->set_value = setfunc; 1105 | new_target->get_value = getfunc; 1106 | new_target->get_max_value = getmaxfunc; 1107 | new_target->custom_command = cmdfunc; 1108 | new_target->device_target_data = target_data; 1109 | 1110 | snprintf(new_target->name, sizeof(new_target->name), "%s", name); 1111 | 1112 | _light_add_device_target(device, new_target); 1113 | 1114 | return new_target; 1115 | } 1116 | 1117 | void light_delete_device_target(light_device_target_t *device_target) 1118 | { 1119 | if(device_target->device_target_data != NULL) 1120 | { 1121 | free(device_target->device_target_data); 1122 | } 1123 | 1124 | free(device_target); 1125 | } 1126 | --------------------------------------------------------------------------------