From d52f81e43da29d94122d62c1b1be34946960033c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 29 Mar 2007 22:56:13 -0700 Subject: Initial xbacklight program --- Makefile.am | 64 +++++++++++++++++ configure.ac | 42 +++++++++++ xbacklight.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xbacklight.man | 53 ++++++++++++++ 4 files changed, 373 insertions(+) create mode 100644 Makefile.am create mode 100644 configure.ac create mode 100644 xbacklight.c create mode 100644 xbacklight.man diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..a63bd35 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,64 @@ +# +# Copyright 2005 Red Hat, Inc. +# +# Permission to use, copy, modify, distribute, and sell this software and its +# documentation for any purpose is hereby granted without fee, provided that +# the above copyright notice appear in all copies and that both that +# copyright notice and this permission notice appear in supporting +# documentation, and that the name of Red Hat not be used in +# advertising or publicity pertaining to distribution of the software without +# specific, written prior permission. Red Hat makes no +# representations about the suitability of this software for any purpose. It +# is provided "as is" without express or implied warranty. +# +# RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO +# EVENT SHALL RED HAT BE LIABLE FOR ANY SPECIAL, INDIRECT OR +# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, +# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. + +AUTOMAKE_OPTIONS = foreign + +bin_PROGRAMS = xbacklight + +AM_CFLAGS = $(XRANDR_CFLAGS) +xbacklight_LDADD = $(XRANDR_LIBS) + +xbacklight_SOURCES = \ + xbacklight.c + +appman_PRE = \ + xbacklight.man + +appmandir = $(APP_MAN_DIR) + +appman_DATA = $(appman_PRE:man=@APP_MAN_SUFFIX@) + +EXTRA_DIST = $(appman_PRE) +CLEANFILES = $(appman_DATA) + +SED = sed + +# Strings to replace in man pages +XORGRELSTRING = @PACKAGE_STRING@ + XORGMANNAME = X Version 11 + +MAN_SUBSTS = \ + -e 's|__vendorversion__|"$(XORGRELSTRING)" "$(XORGMANNAME)"|' \ + -e 's|__xorgversion__|"$(XORGRELSTRING)" "$(XORGMANNAME)"|' \ + -e 's|__xservername__|Xorg|g' \ + -e 's|__xconfigfile__|xorg.conf|g' \ + -e 's|__projectroot__|$(prefix)|g' \ + -e 's|__apploaddir__|$(appdefaultdir)|' \ + -e 's|__appmansuffix__|$(APP_MAN_SUFFIX)|g' \ + -e 's|__libmansuffix__|$(LIB_MAN_SUFFIX)|g' \ + -e 's|__adminmansuffix__|$(ADMIN_MAN_SUFFIX)|g' \ + -e 's|__miscmansuffix__|$(MISC_MAN_SUFFIX)|g' \ + -e 's|__filemansuffix__|$(FILE_MAN_SUFFIX)|g' + +SUFFIXES = .$(APP_MAN_SUFFIX) .man + +.man.$(APP_MAN_SUFFIX): + sed $(MAN_SUBSTS) < $< > $@ diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..e2eabb7 --- /dev/null +++ b/configure.ac @@ -0,0 +1,42 @@ + +dnl Copyright 2005 Red Hat, Inc. +dnl +dnl Permission to use, copy, modify, distribute, and sell this software and its +dnl documentation for any purpose is hereby granted without fee, provided that +dnl the above copyright notice appear in all copies and that both that +dnl copyright notice and this permission notice appear in supporting +dnl documentation, and that the name of Red Hat not be used in +dnl advertising or publicity pertaining to distribution of the software without +dnl specific, written prior permission. Red Hat makes no +dnl representations about the suitability of this software for any purpose. It +dnl is provided "as is" without express or implied warranty. +dnl +dnl RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +dnl INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO +dnl EVENT SHALL RED HAT BE LIABLE FOR ANY SPECIAL, INDIRECT OR +dnl CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, +dnl DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +dnl TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +dnl PERFORMANCE OF THIS SOFTWARE. +dnl +dnl Process this file with autoconf to create configure. + +AC_PREREQ([2.57]) +AC_INIT(xbacklight,[0.0.1], [https://bugs.freedesktop.org/enter_bug.cgi?product=xorg],xbacklight) +AM_INIT_AUTOMAKE([dist-bzip2]) +AM_MAINTAINER_MODE + +AM_CONFIG_HEADER(config.h) + +AC_PROG_CC +AC_PROG_INSTALL + +# Checks for pkg-config packages +PKG_CHECK_MODULES(XRANDR, xrandr >= 1.2.0 xrender x11) +AC_SUBST(XRANDR_CFLAGS) +AC_SUBST(XRANDR_LIBS) + +XORG_MANPAGE_SECTIONS +XORG_RELEASE_VERSION + +AC_OUTPUT([Makefile]) diff --git a/xbacklight.c b/xbacklight.c new file mode 100644 index 0000000..df99905 --- /dev/null +++ b/xbacklight.c @@ -0,0 +1,214 @@ +#include +#include +#include +#include +#include + +typedef enum { Get, Set, Inc, Dec } op_t; + +static char *program_name; + +static Atom backlight; + +static void +usage (void) +{ + fprintf(stderr, "usage: %s [options]\n", program_name); + fprintf(stderr, " where options are:\n"); + fprintf(stderr, " -display or -d \n"); + fprintf(stderr, " -help\n"); + fprintf(stderr, " -set or = \n"); + fprintf(stderr, " -inc or + \n"); + fprintf(stderr, " -dec or - \n"); + fprintf(stderr, " -get\n"); + /*NOTREACHED*/ + exit (1); +} + +static long +backlight_get (Display *dpy, RROutput output) +{ + unsigned long nitems; + unsigned long bytes_after; + unsigned char *prop; + Atom actual_type; + int actual_format; + long value; + + if (XRRGetOutputProperty (dpy, output, backlight, + 0, 4, False, False, None, + &actual_type, &actual_format, + &nitems, &bytes_after, &prop) != Success) + return -1; + if (actual_type != XA_INTEGER || nitems != 1 || actual_format != 32) + value = -1; + else + value = *((long *) prop); + XFree (prop); + return value; +} + +static void +backlight_set (Display *dpy, RROutput output, long value) +{ + XRRChangeOutputProperty (dpy, output, backlight, XA_INTEGER, 32, + PropModeReplace, (unsigned char *) &value, 1); +} + +int +main (int argc, char **argv) +{ + char *dpy_name = NULL; + Display *dpy; + int screen; + int major, minor; + op_t op = Get; + int value = 0; + int i; + + program_name = argv[0]; + + for (i = 1; i < argc; i++) + { + if (!strcmp (argv[i], "-display") || !strcmp ("-d", argv[i])) + { + if (++i >= argc) usage(); + dpy_name = argv[i]; + continue; + } + if (!strcmp (argv[i], "-set") || !strcmp (argv[i], "=")) + { + if (++i >= argc) usage(); + op = Set; + value = atoi (argv[i]); + continue; + } + if (argv[i][0] == '=' && isdigit (argv[i][1])) + { + op = Set; + value = atoi (argv[i] + 1); + continue; + } + if (!strcmp (argv[i], "-inc") || !strcmp (argv[i], "+")) + { + if (++i >= argc) usage(); + op = Inc; + value = atoi (argv[i]); + continue; + } + if (argv[i][0] == '+' && isdigit (argv[i][1])) + { + op = Inc; + value = atoi (argv[i] + 1); + continue; + } + if (!strcmp (argv[i], "-dec") || !strcmp (argv[i], "-")) + { + if (++i >= argc) usage(); + op = Dec; + value = atoi (argv[i]); + continue; + } + if (argv[i][0] == '-' && isdigit (argv[i][1])) + { + op = Dec; + value = atoi (argv[i] + 1); + continue; + } + if (!strcmp (argv[i], "-get") || !strcmp (argv[i], "-g")) + { + op = Get; + continue; + } + if (!strcmp (argv[i], "-help") || !strcmp (argv[i], "-?")) + { + usage (); + } + } + dpy = XOpenDisplay (dpy_name); + if (!dpy) + { + fprintf (stderr, "Cannot open display \"%s\"\n", + XDisplayName (dpy_name)); + exit (1); + } + if (!XRRQueryVersion (dpy, &major, &minor)) + { + fprintf (stderr, "RandR extension missing\n"); + exit (1); + } + if (major < 1 || (major == 1 && minor < 2)) + { + fprintf (stderr, "RandR version %d.%d too old\n", major, minor); + exit (1); + } + backlight = XInternAtom (dpy, "BACKLIGHT", True); + if (backlight == None) + { + fprintf (stderr, "No outputs have backlight property\n"); + exit (1); + } + for (screen = 0; screen < ScreenCount (dpy); screen++) + { + Window root = RootWindow (dpy, screen); + XRRScreenResources *resources = XRRGetScreenResources (dpy, root); + int o; + + if (!resources) continue; + for (o = 0; o < resources->noutput; o++) + { + RROutput output = resources->outputs[o]; + XRRPropertyInfo *info; + double cur, new, step; + double min, max; + double set; + + cur = backlight_get (dpy, output); + if (cur != -1) + { + info = XRRQueryOutputProperty (dpy, output, backlight); + if (info) + { + if (info->range && info->num_values == 2) + { + min = info->values[0]; + max = info->values[1]; + if (op == Get) { + printf ("%f\n", (cur - min) * 100 / (max - min)); + } else { + set = value * (max - min) / 100; + switch (op) { + case Set: + new = min + set; + break; + case Inc: + new = cur + set; + break; + case Dec: + new = cur - set; + break; + } + if (new > max) new = max; + if (new < min) new = min; + step = (new - cur) / 50; + for (i = 0; i < 50; i++) + { + if (i == 49) + cur = new; + else + cur += step; + backlight_set (dpy, output, (long) cur); + XFlush (dpy); + usleep (10 * 1000); + } + } + } + XFree (info); + } + } + } + + XRRFreeScreenResources (resources); + } + XSync (dpy, False); +} diff --git a/xbacklight.man b/xbacklight.man new file mode 100644 index 0000000..0a998f6 --- /dev/null +++ b/xbacklight.man @@ -0,0 +1,53 @@ +.\" +.\" Copyright © 2007 Keith Packard.\" +.\" Permission to use, copy, modify, distribute, and sell this software and its +.\" documentation for any purpose is hereby granted without fee, provided that +.\" the above copyright notice appear in all copies and that both that +.\" copyright notice and this permission notice appear in supporting +.\" documentation, and that the name of Keith Packard not be used in +.\" advertising or publicity pertaining to distribution of the software without +.\" specific, written prior permission. Keith Packard makes no +.\" representations about the suitability of this software for any purpose. It +.\" is provided "as is" without express or implied warranty. +.\" +.\" KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +.\" INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO +.\" EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR +.\" CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, +.\" DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +.\" TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +.\" PERFORMANCE OF THIS SOFTWARE. +.\" +.\" +.TH XBACKLIGHT __appmansuffix__ __vendorversion__ +.SH NAME +xbacklight \- adjust backlight brightness using RandR extension +.SH SYNOPSIS +.B "xbacklight" +[-help] [-display \fIdisplay\fP] +[-get] +[-set percent] +[-inc percent] +[-dec percent] +.SH DESCRIPTION +.I Xbacklight +is used to adjust the backlight brightness where supported. It finds all +outputs on the X server supporting backlight brightness control and changes +them all in the same way. +.IP -get +Print out the current backlight brightness of each output with such a +control. The brightness is represented as a percentage of the maximum +brightness supported. +.IP "-set \fIpercent\fP" +Sets each backlight brightness to the specified level. +.IP "-inc \fIpercent\fP" +Increases brightness by the specified amount. +.IP "-dec \fIpercent\fP" +Decreases brightness by the specified amount. +.IP --help +Print out a summary of the usage and exit. +.SH "SEE ALSO" +Xrandr(3) +.SH AUTHORS +Keith Packard, +Open Source Technology Center, Intel Corporation. -- cgit v1.2.3