Skip to content

Instantly share code, notes, and snippets.

@entrity
Last active January 12, 2016 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entrity/3030b308e3d1c44d95e0 to your computer and use it in GitHub Desktop.
Save entrity/3030b308e3d1c44d95e0 to your computer and use it in GitHub Desktop.
Read/write the brightness on my Ubuntu 14.04 LTS installation on my ThinkPad 440s.
/*
The build of this file should have the user id bit set and the owner should be root (and this must be on an unencrypted file system which honours the user id bit) because the system file it must modify is writable only by the root user.
Usage:
bright -- read current level
bright <number> -- set bright to <number>
bright +<number> -- increment bright by <number>
bright -<number> -- decrement bright by <number>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILENAME "/sys/class/backlight/intel_backlight/brightness"
#define BRIGHTMAX 150
#define BRIGHTMIN 1
FILE * out;
int newValue; // value to be set
// Read current value from file
int readCurrentValue ();
// Apply user input to current value arithmetically, return sum/difference
int inferNewValueFromModifier (char * argv[], int multiplier);
int inferNewValueFromModifier (char * argv[], int multiplier)
{
int modifier;
sscanf(&argv[1][1], "%d", &modifier);
return (modifier * multiplier) + readCurrentValue();
}
// Main
int main (int argc, char * argv[])
{
int res;
if (argc <= 1) {
printf("Usage: $0 <brightness>\nCurrent brightness: ");
printf("%d\n", readCurrentValue());
}
else {
int argIntPart;
sscanf(argv[1], "%d", &argIntPart);
if (argv[1][0] == '-')
if (strlen(&argv[1][1]))
newValue = readCurrentValue() + argIntPart; // argIntPart will be negative if there was a minus on the command line
else
newValue = readCurrentValue() / 2;
else if (argv[1][0] == '+')
if (strlen(&argv[1][1]))
newValue = readCurrentValue() + argIntPart;
else
newValue = readCurrentValue() * 2;
else
newValue = argIntPart;
/* Require that newValue for bright never exceeds BRIGHTMAX and never exceeds BRIGHTMIN */
if (newValue > BRIGHTMAX)
newValue = BRIGHTMAX;
else if (newValue < BRIGHTMIN)
newValue = BRIGHTMIN;
/* Write newValue for bright to file. The OS will observe the change and make the screen adjustment accordingly. */
out = fopen(FILENAME, "w");
if (!out) {
fputs("Can't open output file " FILENAME "\n", stderr);
exit(1);
}
res = fprintf(out, "%d", newValue);
if (res < 1) {
fputs("Failed to write to output file " FILENAME "\n", stderr);
exit(2);
}
fclose(out);
fprintf(stdout, "Set brigtness to %d\n", newValue);
}
}
int readCurrentValue ()
{
int curValue, res;
FILE * in = fopen(FILENAME, "r");
if (!in)
{
fputs("Can't open input file " FILENAME "\n", stderr);
exit(3);
}
res = fscanf(in, "%d", &curValue);
if (res < 1)
{
fputs("Failed to read current value from input file " FILENAME "\n", stderr);
exit(4);
}
fclose(in);
return curValue;
}
[Desktop Entry]
Name=Adjust Bright
Exec=/usr/bin/env python /usr/bin/local/brightener.py
Icon=/home/markham/Pictures/Clipart/crops/ghost-blade-128x128.jpg
Type=Application
Terminal=false
StartupNotify=true
Categories=GTK;GNOME;Utility;

Convenient CLI and GUI for adjusting the brightness on my Ubuntu installation in my Thinkpad T440

# Provides a GUI of buttons for quick, repeated clicks to adjust monitor brightness.
from Tkinter import *
from subprocess import Popen, PIPE
from threading import Timer
import ImageTk
import re
def getImg(basename):
img = ImageTk.Image.open("/home/markham/Pictures/Clipart/"+basename)
img.resize((150,150), ImageTk.Image.ANTIALIAS)
pi = ImageTk.PhotoImage(img)
return pi
class CP(Frame):
def __init__(self, parent):
self.parent = parent
Frame.__init__(self, parent)
self.initUI()
self.parent.bind("<Control-q>", self.close)
self.parent.bind("<Control-w>", self.close)
self.set_and_get(None)
def initUI(self):
self.pack(fill=BOTH, expand=True)
self.imgBelmont = getImg("crops/belmont-red-128x128.jpg")
self.imgBelmon2 = getImg("crops/belmont-cross-128x128.jpg")
self.imgGhost = getImg("crops/ghost-blade-128x128.jpg")
self.imgGrave = getImg("grave-angel.png")
imgFrame = Frame(self)
imgFrame.pack(side=TOP)
entryFrame = Frame(self)
entryFrame.pack(side=BOTTOM)
frame1 = Frame(imgFrame)
frame1.pack(side=LEFT)
up5Btn = Button(frame1, text="+5", image=self.imgBelmont, command=(lambda : self.set_and_get('+5')))
up5Btn.pack(side=TOP, padx=5)
dn5Btn = Button(frame1, text="-5", image=self.imgBelmon2, command=(lambda : self.set_and_get('-5')))
dn5Btn.pack(side=BOTTOM, padx=5)
frame2 = Frame(imgFrame)
frame2.pack(side=RIGHT)
up2Btn = Button(frame2, text="x2", image=self.imgGhost, command=(lambda : self.set_and_get('+')))
up2Btn.pack(side=TOP, padx=5)
dn2Btn = Button(frame2, text="/2", image=self.imgGrave, command=(lambda : self.set_and_get('-')))
dn2Btn.pack(side=BOTTOM, padx=5)
self.entry = Entry(entryFrame, width=4)
self.entry.pack(side=LEFT, pady=8)
entryBtn = Button(entryFrame, text="Change", command=(lambda : self.set_and_get(self.entry.get())))
entryBtn.pack(side=RIGHT, pady=8)
def set_and_get(self, arg):
if hasattr(self, 'timer') and self.timer:
self.timer.cancel()
self.timer = Timer(7, self.close, (self,))
self.timer.start()
params = ['bright', arg] if arg else ['bright']
p = Popen(params, stdout=PIPE)
out, err = p.communicate()
m = re.search('(\d+)\s*\Z', out)
if m != None:
self.entry.delete(0, END)
self.entry.insert(0, m.group(1))
def close(self, evt):
self.parent.destroy()
if __name__ == '__main__':
root = Tk()
root.geometry("276x310")
root.title("Brighten Your Day")
app = CP(root)
root.mainloop()
build: bright.c
gcc -o bright bright.c
install:
chown root bright
chmod u+s bright
mv bright /usr/local/bin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment