Commit 305366dd authored by Gerald Wu's avatar Gerald Wu
Browse files

Initial commit

main: kbdbacklight.c
gcc -O2 -o kbdbacklight kbdbacklight.c
# Automatic keyboard backlight
C program taken from [the Arch Linux Wiki]( to automatically enable keyboard backlighting when the user starts typing.
Added a bash script to change backlight brightness on the fly, as well as restart the systemd service.
Argument (case-insensitive) should be given as follows:
Off - Disable backlighting
Med - Enable medium brightness
Max - Enable maximum brightness
This has only been tested in the Thinkpad X1 Carbon Gen 2. For more information about how to install, please visit [the Arch Linux Wiki](
if [ -z "$@" ];
echo "Current setting is $(grep -E "#define BRGHT BRGHT_(.*)" kbdbacklight.c | rev | cut -d' ' -f1 | cut -d'_' -f1 | rev)"
echo -e "Argument (case-insensitive) should be given as follows:\n \t Off - Disable backlighting \n\t Med - Enable medium brightness \n\t Max - Enable maximum brightness"
exit 1
sed -i "s/#define BRGHT BRGHT_.*/#define BRGHT BRGHT_`echo "$1" | awk '{print toupper($0)}'`/" kbdbacklight.c
read -r -p "Would you like to restart the systemd service? [Y/n] " response
case "$response" in
systemctl restart kbdbacklight.service
echo "Please restart systemd service manually"
File added
/* Original Author: Howard Chu <> 2013-01-15
* compile as "gcc -O2 -o kbdbacklight kbdbacklight.c" and run it in the background, or arrange to have it run at bootup.
* adapted by 2017-01-22
* using
* based on
* original code found at
* sigterm catching done as shown in
* monitor keyboard activity and toggle keyboard backlight
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
static char dummybuf[8192];
/** @brief How many milliseconds before turning off kbd light */
#ifndef IDLE_MSEC
#define IDLE_MSEC 2000
#ifndef BRGHT_OFF
#define BRGHT_OFF 3
#ifndef BRGHT_MED
#define BRGHT_MED 67
#ifndef BRGHT_MAX
#define BRGHT_MAX 131
#ifndef BRGHT
volatile sig_atomic_t running = 1;
void term(int signum)
// sigterm == 15
running = 0;
int main(int argc, char *argv[])
struct sigaction action;
struct pollfd pfd;
int rc, blfd;
int timeout, prev = -1;
/* possible brightness levels for x1
* - 3 off
* - 67 medium
* - 131 max
char bm[1];
bm[0] = BRGHT;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
// potentially needs
// sudo modprobe -r ec_sys
// sudo modprobe ec_sys write_support=1
blfd = open("/sys/kernel/debug/ec/ec0/io", O_WRONLY);
// needs the event bound to the keyboard
// for Xorg ie find using
// cat /var/log/Xorg.0.log | grep "keyboard.*event"
pfd.fd = open("/dev/input/event0", O_RDONLY); = POLLIN;
timeout = IDLE_MSEC;
while (running) {
rc = poll(&pfd, 1, timeout);
if (rc) {
/* got keyboard input, flush it all and
* wait for the next event.
read(pfd.fd, dummybuf, sizeof(dummybuf));
timeout = IDLE_MSEC;
bm[0] = BRGHT;
} else {
/* once we've gotten a timeout, turn off
* kbd backlight and wait forever for
* the next keypress
timeout = -1;
bm[0] = BRGHT_OFF;
if (bm[0] == prev)
lseek(blfd, 13, SEEK_SET);
write(blfd, bm, 1);
prev = bm[0];
// clean up after sigterm
bm[0] = BRGHT_OFF;
lseek(blfd, 13, SEEK_SET);
write(blfd, bm, 1);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment