commit b84a808c43f859caff7675b7d4f102f4b7088fe3 Author: Nikolay Puzanov Date: Mon Mar 1 17:39:21 2021 +0300 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0f09541 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 punzik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2d9458f --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: nbackbright.nim + nim c -d:release --opt:size nbackbright.nim + +clean: + rm -rf nbackbright diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b43d6a --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +Simple tool for backlight brightness control. For build from source needs +[Nim](https://nim-lang.org/) language compiler. + +Usage: `nbackbright `, where `` is a percentage of brightness +changing. + +For example: `nbackbright -10` decrease brightness by 10%. diff --git a/nbackbright.nim b/nbackbright.nim new file mode 100644 index 0000000..a11f258 --- /dev/null +++ b/nbackbright.nim @@ -0,0 +1,37 @@ +import os +import streams +import strutils + +const + BL_SYS_DIR = "/sys/class/backlight" + +proc change_brightness(percent: int) = + if dirExists(BL_SYS_DIR): + for kind, path in walkDir(BL_SYS_DIR): + let + br_file = path & "/brightness" + mb_file = path & "/max_brightness" + if fileExists(br_file) and fileExists(mb_file): + try: + let br_stream = openFileStream(br_file) + let br = parseInt(br_stream.readLine()) + br_stream.close() + + let mb_stream = openFileStream(mb_file) + let mb = parseInt(mb_stream.readLine()) + mb_stream.close() + + var new_br = int(float(br) + float(mb) / 100.0 * float(percent)) + if new_br < 0: new_br = 0 + if new_br > mb: new_br = mb + + let br_write = openFileStream(br_file, fmWrite) + br_write.writeLine(new_br) + br_write.close() + except: + echo "Fail to interact with ", br_file + +when isMainModule: + if paramCount() > 0: + let percent = parseInt(paramStr(1)) + change_brightness(percent)