From aa502f8d730c03841a289d009d7c51e48ebddbb8 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Thu, 19 Oct 2017 21:02:36 -0700 Subject: [PATCH] Add macbook brightness --- dotfiles/config/xmonad/xmonad.hs | 4 +- dotfiles/lib/bin/brightness.sh | 3 ++ dotfiles/lib/bin/macbook_brightness.py | 75 ++++++++++++++++++++++++++ dotfiles/lib/bin/show_brightness.sh | 9 ---- 4 files changed, 80 insertions(+), 11 deletions(-) create mode 100755 dotfiles/lib/bin/brightness.sh create mode 100755 dotfiles/lib/bin/macbook_brightness.py delete mode 100755 dotfiles/lib/bin/show_brightness.sh diff --git a/dotfiles/config/xmonad/xmonad.hs b/dotfiles/config/xmonad/xmonad.hs index a33f4c95..0f04d18d 100644 --- a/dotfiles/config/xmonad/xmonad.hs +++ b/dotfiles/config/xmonad/xmonad.hs @@ -1013,8 +1013,8 @@ addKeys conf@XConfig { modMask = modm } = , ((hyper .|. shiftMask, xK_q), spawn "toggle_mute_current_window.sh") , ((hctrl, xK_q), spawn "toggle_mute_current_window.sh only") - , ((0, xF86XK_MonBrightnessUp), spawn "show_brightness.sh") - , ((0, xF86XK_MonBrightnessDown), spawn "show_brightness.sh") + , ((0, xF86XK_MonBrightnessUp), spawn "brightness.sh 5") + , ((0, xF86XK_MonBrightnessDown), spawn "brightness.sh -5") ] ++ diff --git a/dotfiles/lib/bin/brightness.sh b/dotfiles/lib/bin/brightness.sh new file mode 100755 index 00000000..fad0861b --- /dev/null +++ b/dotfiles/lib/bin/brightness.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env zsh + +volnoti-show $(sudo macbook_brightness.py -c "$@" -p) diff --git a/dotfiles/lib/bin/macbook_brightness.py b/dotfiles/lib/bin/macbook_brightness.py new file mode 100755 index 00000000..0efd77ba --- /dev/null +++ b/dotfiles/lib/bin/macbook_brightness.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +import argparse +import os +import sys + + +class BrightnessManager(object): + + @classmethod + def from_path(cls, path): + return cls( + set_brightness_filepath=os.path.join(path, "brightness"), + actual_brightness_filepath=os.path.join(path, "actual_brightness"), + max_brightness_filepath=os.path.join(path, "max_brightness"), + ) + + def __init__( + self, set_brightness_filepath, max_brightness_filepath, actual_brightness_filepath + ): + self.set_brightness_filepath = set_brightness_filepath + self.max_brightness_filepath = max_brightness_filepath + self.actual_brightness_filepath = actual_brightness_filepath + + @property + def current_brightness(self): + with open(self.actual_brightness_filepath) as fd: + return int(fd.read()) + + @property + def max_brightness(self): + with open(self.max_brightness_filepath) as fd: + return int(fd.read()) + + @current_brightness.setter + def current_brightness(self, brightness): + with open(self.set_brightness_filepath, 'w') as fd: + fd.write(str(brightness)) + + def increment_by_proportion(self, proportion): + new_brightness = self.current_brightness + int(self.max_brightness * proportion) + new_brightness = min(new_brightness, self.max_brightness) + self.current_brightness = new_brightness + + @property + def current_proportion(self): + return float(self.current_brightness) / self.max_brightness + + +IntelBrightnessManager = BrightnessManager.from_path( + "/sys/class/backlight/intel_backlight", +) + + +def build_parser(): + parser = argparse.ArgumentParser( + description='Interact with macbook brightness', + ) + parser.add_argument( + "--change", "-c", + help="Change volume by the given percentage", + default=0 + ) + parser.add_argument( + "--print", "-p", dest="do_print", + action='store_true', + default=False + ) + return parser + + +if __name__ == '__main__': + args = build_parser().parse_args() + IntelBrightnessManager.increment_by_proportion(float(args.change) / 100) + if args.do_print: + print(int(IntelBrightnessManager.current_proportion * 100)) diff --git a/dotfiles/lib/bin/show_brightness.sh b/dotfiles/lib/bin/show_brightness.sh deleted file mode 100755 index a10fdd91..00000000 --- a/dotfiles/lib/bin/show_brightness.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env sh - -actual="$(cat /sys/class/backlight/intel_backlight/actual_brightness)" -max="$(cat /sys/class/backlight/intel_backlight/max_brightness)" - -temp="$(( $actual * 100 ))" -percentage="$(( $temp/$max ))" - -volnoti-show "$percentage"