From f1c303b27f92aa2f99751df3879125d96b94cdb1 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 12 Oct 2012 23:58:08 +0200 Subject: java implementation + completed --- Adjbacklight.java | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ adjbacklight | 29 +++++++++++++++ adjbacklight.py | 47 ------------------------ 3 files changed, 133 insertions(+), 47 deletions(-) create mode 100755 Adjbacklight.java create mode 100755 adjbacklight delete mode 100755 adjbacklight.py diff --git a/Adjbacklight.java b/Adjbacklight.java new file mode 100755 index 0000000..c632d65 --- /dev/null +++ b/Adjbacklight.java @@ -0,0 +1,104 @@ +import java.io.*; +import java.util.*; + + +/** + * @author Mattias Andrée, maandree@kth.se + */ +public class Adjbacklight +{ + public static void main(final String... args) throws IOException + { + String dir = "/sys/class/backlight/intel_backlight/"; + int width = Integer.parseInt(args[1]); + int max = 0, min = 0, cur = 0; + + { InputStream is = null; + try + { is = new FileInputStream(dir + "brightness"); + Scanner sc = new Scanner(is); + cur = Integer.parseInt(sc.nextLine()); + } + finally + { if (is != null) + is.close(); + } + } + { InputStream is = null; + try + { is = new FileInputStream(dir + "max_brightness"); + Scanner sc = new Scanner(is); + max = Integer.parseInt(sc.nextLine()); + } + finally + { if (is != null) + is.close(); + } + } + + if (max <= min) + System.exit(127); + int step = (max - min) / 100; + int init = cur; + + System.out.print("\n\n\n\n\n\n"); + print(min, max, init, cur, width); + + for (int d; (d = System.in.read()) != -1;) + switch (d) + { + case 'q': + case '\n': + case 4: + System.out.println(); + return; + + case 'A': + case 'C': cur += step << 1; + case 'B': + case 'D': cur -= step; + + if (cur < min) cur = min; + if (cur > max) cur = max; + + { OutputStream os = null; + try + { os = new FileOutputStream(dir + "brightness"); + os.write((cur + "\n").getBytes("UTF-8")); + os.flush(); + print(min, max, init, cur, width); + } + finally + { if (os != null) + os.close(); + } + } + break; + } + } + + private static void print(int min, int max, int init, int cur, int width) + { + String line = "──────────────────────────────────"; + while (line.length() < width - 2) + line += line; + line = line.substring(0, width - 2); + + String space = " "; + while (space.length() < width - 2) + space += space; + space = space.substring(0, width - 2); + + int mid = (int)((cur - min) * (width - 2.) / (max - min) + 0.5); + + System.out.print("\033[1000D\033[6A"); + System.out.println("\033[2K┌" + line + "┐"); + System.out.println("\033[2K│\033[47m" + space.substring(0, mid) + "\033[49m" + space.substring(mid) + "│"); + System.out.println("\033[2K└" + line + "┘"); + System.out.println("\033[2KMaximum brightness: " + max); + System.out.println("\033[2KInitial brightness: " + init); + System.out.println("\033[2KCurrent brightness: " + cur); + } + +} + diff --git a/adjbacklight b/adjbacklight new file mode 100755 index 0000000..91bc9fd --- /dev/null +++ b/adjbacklight @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +if [[ ! -f 'Adjbacklight.class' ]]; then + javac -cp . Adjbacklight.java || exit 1 +fi + +cat <<. + + +If the program is abnormal aborted there may be effects +on the terminal the following commands should reset it: + + stty icanon echo + echo -en '\033[?25h' + + + + +. + +stty -icanon -echo +if [[ $? = 0 ]]; then + echo -en '\033[?25l' + sudo java -cp . Adjbacklight `stty size` + echo -en '\033[?25h' + stty icanon echo +else + echo -e '\e[1;31m::\e[39m Epic failure\e[21m' +fi diff --git a/adjbacklight.py b/adjbacklight.py deleted file mode 100755 index b915114..0000000 --- a/adjbacklight.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import os -import sys -from subprocess import Popen, PIPE - - - -''' -Hack to enforce UTF-8 in output (in the future, if you see anypony not using utf-8 in programs by default, report them to Princess Celestia so she can banish them to the moon) -''' -def print(text = '', end = '\n'): - sys.stdout.buffer.write((str(text) + end).encode('utf-8')) - - - -''' -This is the mane class of the program -''' -class Adjbacklight(): - def __init__(self): - #Popen('stty -icanon'.split(' '), stderr=os.fdopen(2, 'w')).wait() - try: - dir = '/sys/class/backlight/intel_backlight/' - (size, dummy) = Popen('stty size'.split(' '), stdout=PIPE, stderr=PIPE).communicate() - width = int(size.decode('UTF-8').replace('\n', '').split(' ')[1]) - with open(dir + 'brightness', 'r') as file: - current = int(file.readline().replace('\n', '')) - with open(dir + 'max_brightness', 'r') as file: - max = int(file.readline().replace('\n', '')) - self.__interface(0, current, max, width, dir + 'brightness') - finally: - pass#Popen('stty icanon'.split(' '), stderr=os.fdopen(2, 'w')).wait() - - def __interface(self, min, cur, max, width, file): - step = (max - min) // 100 - while (True): - print(sys.stdin.buffer.read1()) - - - -''' -Start the program from Adjbacklight.__init__ if this is the executed file -''' -if __name__ == '__main__': - Adjbacklight() -- cgit v1.2.3-70-g09d2