Possible to change screen brightness with C?
Asked Answered
S

4

3

Using C in Linux, would it be possible to change the brightness of the viewing screen? This isn't expected to be portable other than running on my installs of Ubuntu and Fedora. I'm asking about how I would interface with the os to change the brightness.

Space answered 27/4, 2011 at 22:15 Comment(2)
Look at how power manager software does it on laptops. It slightly dims the brightness to completely turning off the brightness depending on the remaining battery capacity.Typology
possible duplicate of Linux/Xorg: setting color brightnessTedra
C
8

I'd start with selecting from the following list of ubuntu pacakges, the tool that allows you to manage your screen's brightness (hint: it depends on the brand)

nvidia-settings - Tool of configuring the NVIDIA graphics driver
smartdimmer - Change LCD brightness on Geforce cards
armada-backlight - adjust backlight of Compaq Armada laptops (E300, M500, M700)
ddccontrol - a program to control monitor parameters
eeepc-acpi-scripts - Scripts to support suspend and hotkeys on the Asus Eee PC laptop
fnfxd - ACPI and hotkey daemon for Toshiba laptops
gddccontrol - a program to control monitor parameters
spicctrl - Sony Vaio controller program to set LCD backlight brightness
tpb - program to use the IBM ThinkPad(tm) special keys
xfce4-power-manager - power manager for Xfce desktop
xfce4-power-manager-plugins - power manager plugins for Xfce panel
xvattr - Utility to change Xv attributes

Once you have selected it,

sudo apt-get build-dep <pkgname>
apt-get source --compile <pkgname> 

should get you on the right track

Chapeau answered 27/4, 2011 at 22:34 Comment(0)
T
5

Poke /sys/class/backlight/*/brightness. Yes, even in C.

Tedra answered 27/4, 2011 at 22:33 Comment(7)
In both my installs /sys/class/backlight has nothing in it. And by poke do you mean look into for information? Sorry.Space
Does your laptop have a backlight? And by "poke" I mean "manipulate". As in, feed a value into.Tedra
I am running a desktop with two monitors, does that make a difference?Space
It makes AAAAAAALL the difference in the world.Tedra
Ok. If I created a file in there, would my computer still interpret it as manipulating the monitors brightness?Space
You can't create a file in there. sysfs is a virtual filesystem.Tedra
I wonder if this also works for people who have an external LCD with DDC/CI (which lets the computer control the monitor's parameters).Barcroft
M
2

Yes, but not portably -- you need a platform-specific function, there is nothing in the C standard library.

Mirellamirelle answered 27/4, 2011 at 22:19 Comment(6)
His question is tagged "linux" which probably narrows it down enough to make a halfway decent solution.Barcroft
@John : does it? I'm not so sure. ALso, I think the point of Ben was that there is nothing that ties this functionality to C. And I agree. +1.Eudosia
Yes, this isn't expected to be portable other than running on my installs of ubuntu and fedora. I'm asking about how I would interface with the os to change the brightness.Space
@a sandwich: You would do well to write that in your question then. It's very useful information. :)Barcroft
@John, @a sandwich: I think @sehe's list of tools illustrates how hardware-dependent this function is.Mirellamirelle
@Ben: There certainly are a lot of hardware-specific packages related to this, but maybe there's some hope in the xfce one which presumably contains an implementation that works with multiple types of hardware. Or maybe not!Barcroft
W
2

Check out the xbacklight source. For example, the following code sets the screen brightness to 50%.

// brightness.c
// gcc -o brightness brightness.c -lXrandr -lX11

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

#define BRIGHTNESS 0.5 // Target brightness between 0.0 and 1.0

int main(int argc, char *argv[])
{
        Display *dpy;
        static Atom backlight;
        int screen = 0, o = 0;
        Window root;
        XRRScreenResources *resources;
        RROutput output;
        XRRPropertyInfo *info;
        double min, max;
        long value;

        dpy = XOpenDisplay(NULL);
        backlight = XInternAtom (dpy, "Backlight", True);
        root = RootWindow(dpy, screen);
        resources = XRRGetScreenResources(dpy, root);
        output = resources->outputs[o];
        info = XRRQueryOutputProperty(dpy, output, backlight);
        min = info->values[0];
        max = info->values[1];
        XFree(info); // Don't need this anymore
        XRRFreeScreenResources(resources); // or this

        value = BRIGHTNESS * (max - min) + min;

        XRRChangeOutputProperty(dpy, output, backlight, XA_INTEGER,
                        32, PropModeReplace, (unsigned char *) &value, 1);
        XFlush(dpy);
        XSync(dpy, False);
        return 0;
}
Waynewayolle answered 9/8, 2013 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.