I made some modifications to the original bl1.c program from Nicolas Boichat. In summary, this modifications allow:

Specify an absolute brightness value

Let x be the absolute brightness value, such that 0<x<16, that is, the value must between 1 and 15, where 1 is the minimum brightness level before turning off the screen, and 15 is the maximum brightness supported by the LCD screen.

For example:

./bl1 15
./bl1 1

Specify an increment or decrement (delta)

Let d be the delta, and x the current brightness value, then the new brightness value y is y=x+d, and 0<y<16.

y = min(max(1, x + d), 15)

Source code

/*
 * Apple Macbook Pro LCD backlight control
 *
 * Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
 * Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdio .h>
#include <sys /io.h>
#include <stdlib .h>

void init()
{
	if (ioperm(0xB2, 0xB3, 1) < 0)
	{
		perror("ioperm failed (you should be root).");
		exit(2);
	}
}

int get_current_value()
{
	outb(0x03, 0xB3);
	outb(0xBF, 0xB2);
	char t = inb(0xB3) >> 4;
	return t;
}

int calculate_new_value(const char *arg)
{
	int val, new = atoi(arg);

	if (arg[0] == '+' || arg[0] == '-')
		val = new + get_current_value();
	else
		val = new;

	if (val > 15)
		val = 15;
	else if (val < 1)
		val = 1;

	return val;
}

int main(int argc, char** argv)
{
	if (argc > 2)
	{
		printf("Usage:\n");
		printf("%s : read current value\n", argv[0]);
		printf("%s value : write value [0-15]\n", argv[0]);
		exit(1);
	}

	init();

	if (argc < 2)
	{
		printf("Current value : %d\n", get_current_value());
		exit(0);
	}

	if (argc == 2)
	{
		int value = calculate_new_value(argv[1]);
		outb(0x04 | (value << 4), 0xB3);
		outb(0xBF, 0xB2);
		printf("new value: %d\n", value);
	}

	return 0;
}

20 Responses to “Basic backlight support for MacBook Pro”

  1. » Re: installing Ubuntu on Mac - ubuntu.sitebolt.net Says:

    [...] On 11/5/06, Felipe Alfaro Solana wrote: > On 10/4/06, Artur Boicu wrote: > > Dear friends, > > How cand I install Ubuntu on my Mac. It asks me to set up a partition > > called Yaboot. > > Could you give me a step by step instructions? > > Maybe these posts I wrote some time ago may help you: > > http://felipe-alfaro.org/blog/2006/08/19/installing-ubuntu-linux-on-a-macbook-pro/ > http://felipe-alfaro.org/blog/2006/09/19/installing-refit-on-the-hidden-efi-system-partition/ > http://felipe-alfaro.org/blog/2006/09/11/basic-backlight-support-for-macbook-pro/ > http://felipe-alfaro.org/blog/2006/09/11/modifying-screen-brightness-on-ac-state-changes/ > [...]

  2. windi Says:

    Thank you, this works great on iMac version “5,1″ as well (also known as “Late 2006″).

  3. Bouboune Says:

    i’m looking for a toll like that but i’m newbie… i’ve got an iMac Alu (mid 2007) 20″ with ATI HD2400Pro.

    How to make bli.c work on my computer (i’m under Ubuntu Gusty – Studio version)
    Reply at : bouboulabulle[at]caramail.com
    Thx !

  4. AndrewL733 Says:

    Hi. I could not get your source file to compile. I fixed one error — extra spaces in the include files. But there must be other errors that I don’t understand. Fortunately, I found a pre-compiled binary on another Ubuntu page. You were credited with program.

    Here are the errors:
    backlight.increment.c: In function ‘calculate_new_value’:
    backlight.increment.c:48: error: stray ‘\342’ in program
    backlight.increment.c:48: error: stray ‘\200’ in program
    backlight.increment.c:48: error: stray ‘\230’ in program
    backlight.increment.c:48: error: stray ‘\342’ in program
    backlight.increment.c:48: error: stray ‘\200’ in program
    backlight.increment.c:48: error: stray ‘\231’ in program
    backlight.increment.c:48: error: expected expression before ‘||’ token
    backlight.increment.c:48: error: stray ‘\342’ in program
    backlight.increment.c:48: error: stray ‘\200’ in program
    backlight.increment.c:48: error: stray ‘\230’ in program
    backlight.increment.c:48: error: stray ‘\342’ in program
    backlight.increment.c:48: error: stray ‘\200’ in program
    backlight.increment.c:48: error: stray ‘\231’ in program
    backlight.increment.c: In function ‘main’:
    backlight.increment.c:65: error: stray ‘\342’ in program
    backlight.increment.c:65: error: stray ‘\200’ in program
    backlight.increment.c:65: error: stray ‘\235’ in program
    backlight.increment.c:65: error: ‘Usage’ undeclared (first use in this function)
    backlight.increment.c:65: error: (Each undeclared identifier is reported only once
    backlight.increment.c:65: error: for each function it appears in.)
    backlight.increment.c:65: error: expected ‘)’ before ‘:’ token
    backlight.increment.c:65: error: stray ‘\’ in program
    backlight.increment.c:65: error: stray ‘\342’ in program
    backlight.increment.c:65: error: stray ‘\200’ in program
    backlight.increment.c:65: error: stray ‘\235’ in program
    backlight.increment.c:66: error: stray ‘\342’ in program
    backlight.increment.c:66: error: stray ‘\200’ in program
    backlight.increment.c:66: error: stray ‘\235’ in program
    backlight.increment.c:66: error: expected expression before ‘%’ token
    backlight.increment.c:66: error: stray ‘\’ in program
    backlight.increment.c:66: error: stray ‘\342’ in program
    backlight.increment.c:66: error: stray ‘\200’ in program
    backlight.increment.c:66: error: stray ‘\235’ in program
    backlight.increment.c:67: error: stray ‘\342’ in program
    backlight.increment.c:67: error: stray ‘\200’ in program
    backlight.increment.c:67: error: stray ‘\235’ in program
    backlight.increment.c:67: error: expected expression before ‘%’ token
    backlight.increment.c:67: error: stray ‘\’ in program
    backlight.increment.c:67: error: stray ‘\342’ in program
    backlight.increment.c:67: error: stray ‘\200’ in program
    backlight.increment.c:67: error: stray ‘\235’ in program
    backlight.increment.c:75: error: stray ‘\342’ in program
    backlight.increment.c:75: error: stray ‘\200’ in program
    backlight.increment.c:75: error: stray ‘\235’ in program
    backlight.increment.c:75: error: ‘Current’ undeclared (first use in this function)
    backlight.increment.c:75: error: expected ‘)’ before ‘value’
    backlight.increment.c:75: error: stray ‘\’ in program
    backlight.increment.c:75: error: stray ‘\342’ in program
    backlight.increment.c:75: error: stray ‘\200’ in program
    backlight.increment.c:75: error: stray ‘\235’ in program
    backlight.increment.c:82: error: stray ‘\303’ in program
    backlight.increment.c:82: error: stray ‘\227’ in program
    backlight.increment.c:82: error: expected ‘)’ before numeric constant
    backlight.increment.c:82: error: too few arguments to function ‘outb’
    backlight.increment.c:84: error: stray ‘\342’ in program
    backlight.increment.c:84: error: stray ‘\200’ in program
    backlight.increment.c:84: error: stray ‘\235’ in program
    backlight.increment.c:84: error: ‘new’ undeclared (first use in this function)
    backlight.increment.c:84: error: expected ‘)’ before ‘value’
    backlight.increment.c:84: error: stray ‘\’ in program
    backlight.increment.c:84: error: stray ‘\342’ in program
    backlight.increment.c:84: error: stray ‘\200’ in program
    backlight.increment.c:84: error: stray ‘\235’ in program

  5. Josephine Says:

    Some compilers (e.g Linux) don’t support back quotes, so you need to review your code and replace any such with quotes you type yourself, particularly if you copied your code from some place.

    i.e instead of ” ’n’ ” you should enter ” ‘n’ “

  6. Josephine Says:

    Umm – the back quotes in my example haven’t come out clearly in my posted reply, but I hope you caught my gist :) .

    Alternatively make sure any double quotes in your program are really double quotes and not read as two adjacent single quotes.

  7. Ubuntu on Intel iMac Review | More Zonkyness Says:

    [...] In any install, there are always little niggles that need fixing. The most obvious is a way to control the brightness of the screen which by default is far too bright. There may well be better solutions out there, but a bit of C coming from http://www.felipe-alfaro.org/blog/2006/09/11/basic-backlight-support-for-macbook-pro [...]

  8. Edward Says:

    hey can somebody help me set this up i am a complete newbie w/ linux and i am using it on an intel mac but the brightness is KILLING MY EYES. thanks :)

  9. dx/dy Says:

    Thank you for this fix! Works great!
    Just copied your code into gedit(not my editor of choice) dropped it in my home directory, checked and fixed syntax
    ran
    cc brightnesscontrol.c
    at the command line

    then ran sudo ./a.out 11

    and now my eyes are happy once more!

    cheers :)

  10. SukkoPera Says:

    Hi,

    seems to be working OK on iMac8,1.

    Thanks a lot!
    G.

  11. kokoklems Says:

    Hello,
    I’m running ubuntu 10.10 on iMac 24″. Compilation of the code was successfull, but then when I run the code, the returned current value is ok, but the brightness is not changed… See:

    $ sudo ./a.out
    Current value : 15
    $ sudo ./a.out 10
    new value: 10
    $ sudo ./a.out
    Current value : 15

    This thing is still killing my eyes!! Help!

  12. James Says:

    Works great….had a WAY too bring iMac.

  13. Palkal Says:

    Running ubuntu studio 10.10 on iMac10.1. Code compiled, binary running but not changing the values. Same as “kokoklems”.

  14. Palkal Says:

    Ok, I’m back. I managed to adjust the brightness through the compiz setting manager application (install compizconfig-setting-manager from synaptic). I have maped my brightness keys to do the job although this only works for active aplication. If you find a way to make it work for the whole system please let us know.

  15. Dmitri Says:

    The code works great for me (Ubuntu 10.04 and 10.10 with aliminum iMac 21″ and 27″).

    Thanks.

  16. bloom Says:

    Here, on an iMac 10,1 (21.5″, ATI Radeon HD 4670), the code compiles and runs but has no effect.

  17. Quozl Says:

    Here, on an iMac 11,1, the code compiles and runs and has an effect, for brightness levels 0 through to 15, but brightness level 0 is some way above the lowest brightness set by Mac OS X. If I set the brightness to minimum in Mac OS X and then boot to Ubuntu, I get what I want. It is as if there may be another port for the scaling factor.

  18. Stephan Lokietek Says:

    An absorbing treatment is couturier notice. I conceive that you should make solon on this message, it power not be a prejudice somebody but mostly grouping are not enough to talk on such topics. To the succeeding. Cheers like your ALTARED …we rock for JESUS.

  19. Annice Flenaugh Says:

    I used to be suggested this web site by my cousin. I’m not positive whether or not this publish is written by means of him as nobody else recognise such specific approximately my trouble. You’re wonderful! Thank you!

  20. Slyvia Figlioli Says:

    I wouldn’t say it’s excellent, but I do my best. I’m constantly researching ways to increase it, if you offer an strategy.

Leave a Reply