I like to use a GNU/Linux machine than a Windows machine for at least two reasons:
In this post, I will tell you how an OS upgrade broke a functionality on my machine and how I fixed it (which was a fun experience).
Update 1: zerosk8 says in a comment that the command
Update 2: The technique described in this post worked on my Lenovo laptop (with an ATI graphics card). It didn’t, however, work on my Samsung N150 laptop (which has an Intel graphics card). Check this blog post of mine to see what works on the Samsung machine.
Original post:
My upgrade to Kubuntu Gutsy broke the functionality of Fn+Home and Fn+End keys on my ThinkPad. (If you use Sony Vaio, look at Daniel’s comment. Thanks Daniel for the information!) These keys are used to control the brightness of the LCD screen. When I was Googling around to find a solution, I learned that the LCD screen driver provides a file system-like interface for controlling it. The file
In case you didn’t know exactly how to install this program, or too lazy to type commands, use these commands:
Chose the keyboard shortcut Ctrl+Alt+=.
For command, I gave the path to
Likewise, I assigned Ctrl+Alt+- to
- GNU/Linux user environment (especially KDE) is customizable.
- You get to see how things are implemented, which is kinda fun.
In this post, I will tell you how an OS upgrade broke a functionality on my machine and how I fixed it (which was a fun experience).
Update 1: zerosk8 says in a comment that the command
xbacklight can be used to control screen brightness from the command line. On Ubuntu machines, run sudo apt-get install xbacklight to install the command on your machine. Thank you zerosk8! (Near the end of this post I am discussing how I set up keyboard shortcuts for adjusting brightness. This part might be useful even if you use xbacklight command.)Update 2: The technique described in this post worked on my Lenovo laptop (with an ATI graphics card). It didn’t, however, work on my Samsung N150 laptop (which has an Intel graphics card). Check this blog post of mine to see what works on the Samsung machine.
Original post:
My upgrade to Kubuntu Gutsy broke the functionality of Fn+Home and Fn+End keys on my ThinkPad. (If you use Sony Vaio, look at Daniel’s comment. Thanks Daniel for the information!) These keys are used to control the brightness of the LCD screen. When I was Googling around to find a solution, I learned that the LCD screen driver provides a file system-like interface for controlling it. The file
/sys/class/backlight/acpi_video0/brightness acts as the interface for adjusting screen brightness. So I wrote the following shell script to adjust screen brightness:#!/bin/sh
#
# Adjust LCD brightness of ThinkPad laptops.
CTRL_FILE=/sys/class/backlight/acpi_video0/brightness
usage() {
echo >&2 Usage: lcd-brightness [value]
}
case $# in
0)
cat $CTRL_FILE
;;
1)
echo $1 >$CTRL_FILE
;;
*)
usage
esac
The code feels right when looking at it. Let’s try it out.So this problem looks like a permission issue. Just to make sure:$ ./lcd-brightness 80 $ ./lcd-brightness 70 ./lcd-brightness: line 16: /sys/class/backlight/acpi_video0/brightness: Permission denied$ sudo ./lcd-brightness # sudo runs the command as root. 80 $ sudo ./lcd-brightness 70 # This worked -- screen brightness changed.
So only root can change the screen brightness. But I don’t want to$ ls -l/sys/class/backlight/acpi_video0/brightness -rw-r--r-- 1 root root 4.0K Jan 17 16:47 /sys/class/backlight/acpi_video0/brightness
sudo all the time to adjust screen brightness. I said to myself, “I know how to handle this.” And I installed my script like this:Time to test this newly installed script.$ sudo cp ./lcd-brightness /usr/local/bin/ $ sudo chmod 755 /usr/local/bin/lcd-brightness $ sudo chmod +s/usr/local/bin/lcd-brightness $ ls -l/usr/local/bin/lcd-brightness -rwsr-sr-x 1 root root 7.3K Jan 17 08:21 /usr/local/bin/lcd-brightness
Looks like setuid bit is not working as I expected. Some more Googling around gave me the answer. Wikipedia article on setuid says: “Due to the increased likelihood of security flaws, many operating systems ignore the setuid attribute when applied to executable shell scripts.“ That explains everything. So my next option is to write it as a C program. I translated this shell script into C as follows:$ type lcd-brightness lcd-brightness is /usr/local/bin/lcd-brightness $ lcd-brightness 70 $ lcd-brightness 80/usr/local/bin/lcd-brightness: line 16: /sys/class/backlight/acpi_video0/brightness: Permission denied# Huh? Let’s try again as root?$ sudo lcd-brightness 80 # This changes the brightness
/*
* Change/query the brightness of LCD screen.
*/
#include <stdio.h>
void usage()
{
fprintf(stderr, "Usage: lcd-brightness [value]\n");
}
int main(int argc, char *argv[])
{
FILE *fp;
int bright = 0;
const char *kFileName =
"/sys/class/backlight/acpi_video0/brightness";
switch (argc) {
case 1:
fp = fopen(kFileName, "r");
fscanf(fp, "%d", &bright);
printf("%d\n", bright);
break;
case 2:
fp = fopen(kFileName, "w");
bright = atoi(argv[1]);
fprintf(fp, "%d\n", bright);
break;
default:
usage();
return -1;
}
fclose(fp);
return 0;
}
Compiling this file and installing the binary like earlier does the job. Cool :-)In case you didn’t know exactly how to install this program, or too lazy to type commands, use these commands:
But this is not the end of story. Later I found a usability problem with this program. I use this program only when I want to either increase or decrease the screen brightness. That is a two-step process — first I have to run the program and find the screen brightness. Then run it again with appropriate brightness argument. This is not an optimal way to use a program like this. So I decided to write two more shell scripts on top of$ gcc lcd-brightness.c $ sudo cp ./a.out /usr/local/bin/lcd-brightness $ sudo chmod +s/usr/local/bin/lcd-brightness $ ls -l /usr/local/bin/lcd-brightness -rwsr-sr-x 1 root root 7.3K Jun 7 21:47 /usr/local/bin/lcd-brightness
lcd-brightness command. Here are those scripts:$ cat increase-lcd-brightness
#/bin/bash
#
# Increase LCD brightness by 10.
# Assumes that lcd-brightness command is on $PATH.
lcd-brightness $(expr $(lcd-brightness) + 10)
$ cat decrease-lcd-brightness
#/bin/bash
#
# Decrease LCD brightness by 10.
# Assumes that lcd-brightness command is on $PATH.
lcd-brightness $(expr $(lcd-brightness) - 10)
The scripts are very simple, aren’t they? But there’s still another usability issue: I have to switch to a terminal to invoke these commands, which might be a distraction when I am coding or reading some document. Correct approach would be to use shortcut keys for this. So I went ahead and defined two new global shortcut keys on my machine. On KDE it’s done from Control Center. I opened the corresponding applet by selecting K Menu > System Settings > Accessibility > Input Actions. Added a new action called “Increase LCD Brightness” with type as “Keyboard Shortcut -> Command/URL (simple)”.Chose the keyboard shortcut Ctrl+Alt+=.
For command, I gave the path to
increase-lcd-brightness script.Likewise, I assigned Ctrl+Alt+- to
decrease-lcd-brightness script. And that’s about it. Now I can use shortcut keys for adjusting screen brightness. Now you understand when I say “Linux is customizable,” don’t you? ;-)






