2007-11-24 10:04:53 -07:00
|
|
|
/*
|
|
|
|
* 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 the copyright holders not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS 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.
|
|
|
|
*/
|
2018-05-21 10:11:22 -06:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2007-11-24 10:04:53 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-08-15 11:09:24 -06:00
|
|
|
#include <errno.h>
|
2014-05-03 13:36:41 -06:00
|
|
|
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_util.h>
|
|
|
|
#include <xcb/xproto.h>
|
|
|
|
#include <xcb/randr.h>
|
2007-11-24 10:04:53 -07:00
|
|
|
|
2009-10-10 04:22:10 -06:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-11-24 10:04:53 -07:00
|
|
|
typedef enum { Get, Set, Inc, Dec } op_t;
|
|
|
|
|
|
|
|
static char *program_name;
|
|
|
|
|
2014-05-03 13:36:41 -06:00
|
|
|
static xcb_atom_t backlight, backlight_new, backlight_legacy;
|
2007-11-24 10:04:53 -07:00
|
|
|
|
|
|
|
static void
|
2018-05-21 10:11:22 -06:00
|
|
|
usage (int exitcode)
|
2007-11-24 10:04:53 -07:00
|
|
|
{
|
2014-05-03 13:36:41 -06:00
|
|
|
fprintf(stderr, "usage: %s [options]\n%s", program_name,
|
|
|
|
" where options are:\n"
|
|
|
|
" -display <display> or -d <display>\n"
|
|
|
|
" -help\n"
|
2018-05-21 10:11:22 -06:00
|
|
|
" -version\n"
|
2014-05-03 13:36:41 -06:00
|
|
|
" -set <percentage> or = <percentage>\n"
|
|
|
|
" -inc <percentage> or + <percentage>\n"
|
|
|
|
" -dec <percentage> or - <percentage>\n"
|
|
|
|
" -get\n"
|
|
|
|
" -time <fade time in milliseconds>\n"
|
|
|
|
" -steps <number of steps in fade>\n");
|
2018-05-21 10:11:22 -06:00
|
|
|
exit (exitcode);
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:09:24 -06:00
|
|
|
static double
|
|
|
|
atof_or_die (char *str)
|
|
|
|
{
|
|
|
|
double retval;
|
|
|
|
char *endptr = NULL;
|
|
|
|
errno = 0;
|
|
|
|
retval = strtod(str, &endptr);
|
|
|
|
if ((errno != 0) || (endptr == str)) {
|
|
|
|
fprintf(stderr, "%s: invalid argument '%s'\n", program_name, str);
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-05-21 10:11:22 -06:00
|
|
|
static void
|
|
|
|
missing_arg (const char *option)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: %s requires an argument\n", program_name, option);
|
|
|
|
usage(1);
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
2014-05-03 13:36:41 -06:00
|
|
|
backlight_get (xcb_connection_t *conn, xcb_randr_output_t output)
|
2007-11-24 10:04:53 -07:00
|
|
|
{
|
2014-05-03 13:36:41 -06:00
|
|
|
xcb_generic_error_t *error;
|
|
|
|
xcb_randr_get_output_property_reply_t *prop_reply = NULL;
|
|
|
|
xcb_randr_get_output_property_cookie_t prop_cookie;
|
|
|
|
long value;
|
2007-11-24 10:04:53 -07:00
|
|
|
|
2009-10-10 04:22:10 -06:00
|
|
|
backlight = backlight_new;
|
2014-05-03 13:36:41 -06:00
|
|
|
if (backlight != XCB_ATOM_NONE) {
|
|
|
|
prop_cookie = xcb_randr_get_output_property (conn, output,
|
|
|
|
backlight, XCB_ATOM_NONE,
|
|
|
|
0, 4, 0, 0);
|
|
|
|
prop_reply = xcb_randr_get_output_property_reply (conn, prop_cookie, &error);
|
|
|
|
if (error != NULL || prop_reply == NULL) {
|
|
|
|
backlight = backlight_legacy;
|
|
|
|
if (backlight != XCB_ATOM_NONE) {
|
|
|
|
prop_cookie = xcb_randr_get_output_property (conn, output,
|
|
|
|
backlight, XCB_ATOM_NONE,
|
|
|
|
0, 4, 0, 0);
|
|
|
|
prop_reply = xcb_randr_get_output_property_reply (conn, prop_cookie, &error);
|
|
|
|
if (error != NULL || prop_reply == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-10 04:22:10 -06:00
|
|
|
}
|
|
|
|
|
2014-05-03 13:36:41 -06:00
|
|
|
if (prop_reply == NULL ||
|
|
|
|
prop_reply->type != XCB_ATOM_INTEGER ||
|
|
|
|
prop_reply->num_items != 1 ||
|
|
|
|
prop_reply->format != 32) {
|
2007-11-24 10:04:53 -07:00
|
|
|
value = -1;
|
2014-05-03 13:36:41 -06:00
|
|
|
} else {
|
|
|
|
value = *((int32_t *) xcb_randr_get_output_property_data (prop_reply));
|
|
|
|
}
|
|
|
|
|
|
|
|
free (prop_reply);
|
2007-11-24 10:04:53 -07:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-03 13:36:41 -06:00
|
|
|
backlight_set (xcb_connection_t *conn, xcb_randr_output_t output, long value)
|
2007-11-24 10:04:53 -07:00
|
|
|
{
|
2014-05-03 13:36:41 -06:00
|
|
|
xcb_randr_change_output_property (conn, output, backlight, XCB_ATOM_INTEGER,
|
|
|
|
32, XCB_PROP_MODE_REPLACE,
|
|
|
|
1, (unsigned char *)&value);
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *dpy_name = NULL;
|
|
|
|
op_t op = Get;
|
2018-05-21 10:11:22 -06:00
|
|
|
double value = 0;
|
2007-11-24 10:04:53 -07:00
|
|
|
int i;
|
|
|
|
int total_time = 200; /* ms */
|
|
|
|
int steps = 20;
|
|
|
|
|
2014-05-03 13:36:41 -06:00
|
|
|
xcb_connection_t *conn;
|
|
|
|
xcb_generic_error_t *error;
|
|
|
|
|
|
|
|
xcb_randr_query_version_cookie_t ver_cookie;
|
|
|
|
xcb_randr_query_version_reply_t *ver_reply;
|
|
|
|
|
|
|
|
xcb_intern_atom_cookie_t backlight_cookie[2];
|
|
|
|
xcb_intern_atom_reply_t *backlight_reply;
|
|
|
|
|
|
|
|
xcb_screen_iterator_t iter;
|
|
|
|
|
2007-11-24 10:04:53 -07:00
|
|
|
program_name = argv[0];
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp (argv[i], "-display") || !strcmp ("-d", argv[i]))
|
|
|
|
{
|
2018-05-21 10:11:22 -06:00
|
|
|
if (++i >= argc) missing_arg (argv[i-1]);
|
2007-11-24 10:04:53 -07:00
|
|
|
dpy_name = argv[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-set") || !strcmp (argv[i], "="))
|
|
|
|
{
|
2018-05-21 10:11:22 -06:00
|
|
|
if (++i >= argc) missing_arg (argv[i-1]);
|
2007-11-24 10:04:53 -07:00
|
|
|
op = Set;
|
2019-08-15 11:09:24 -06:00
|
|
|
value = atof_or_die (argv[i]);
|
2007-11-24 10:04:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (argv[i][0] == '=' && isdigit (argv[i][1]))
|
|
|
|
{
|
|
|
|
op = Set;
|
2019-08-15 11:09:24 -06:00
|
|
|
value = atof_or_die (argv[i] + 1);
|
2007-11-24 10:04:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-inc") || !strcmp (argv[i], "+"))
|
|
|
|
{
|
2018-05-21 10:11:22 -06:00
|
|
|
if (++i >= argc) missing_arg (argv[i-1]);
|
2007-11-24 10:04:53 -07:00
|
|
|
op = Inc;
|
2019-08-15 11:09:24 -06:00
|
|
|
value = atof_or_die (argv[i]);
|
2007-11-24 10:04:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (argv[i][0] == '+' && isdigit (argv[i][1]))
|
|
|
|
{
|
|
|
|
op = Inc;
|
2019-08-15 11:09:24 -06:00
|
|
|
value = atof_or_die (argv[i] + 1);
|
2007-11-24 10:04:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-dec") || !strcmp (argv[i], "-"))
|
|
|
|
{
|
2018-05-21 10:11:22 -06:00
|
|
|
if (++i >= argc) missing_arg (argv[i-1]);
|
2007-11-24 10:04:53 -07:00
|
|
|
op = Dec;
|
2019-08-15 11:09:24 -06:00
|
|
|
value = atof_or_die (argv[i]);
|
2007-11-24 10:04:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (argv[i][0] == '-' && isdigit (argv[i][1]))
|
|
|
|
{
|
|
|
|
op = Dec;
|
2019-08-15 11:09:24 -06:00
|
|
|
value = atof_or_die (argv[i] + 1);
|
2007-11-24 10:04:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-get") || !strcmp (argv[i], "-g"))
|
|
|
|
{
|
|
|
|
op = Get;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-time"))
|
|
|
|
{
|
2018-05-21 10:11:22 -06:00
|
|
|
if (++i >= argc) missing_arg (argv[i-1]);
|
2007-11-24 10:04:53 -07:00
|
|
|
total_time = atoi (argv[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-steps"))
|
|
|
|
{
|
2018-05-21 10:11:22 -06:00
|
|
|
if (++i >= argc) missing_arg (argv[i-1]);
|
2007-11-24 10:04:53 -07:00
|
|
|
steps = atoi (argv[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-help") || !strcmp (argv[i], "-?"))
|
|
|
|
{
|
2018-05-21 10:11:22 -06:00
|
|
|
usage (0);
|
|
|
|
}
|
|
|
|
if (!strcmp (argv[i], "-version"))
|
|
|
|
{
|
|
|
|
puts (PACKAGE_STRING);
|
|
|
|
exit (0);
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|
2018-05-21 10:11:22 -06:00
|
|
|
fprintf(stderr, "%s: unrecognized argument '%s'\n",
|
|
|
|
program_name, argv[i]);
|
|
|
|
usage (1);
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|
2014-05-03 13:36:41 -06:00
|
|
|
conn = xcb_connect (dpy_name, NULL);
|
|
|
|
ver_cookie = xcb_randr_query_version (conn, 1, 2);
|
|
|
|
ver_reply = xcb_randr_query_version_reply (conn, ver_cookie, &error);
|
|
|
|
if (error != NULL || ver_reply == NULL) {
|
|
|
|
int ec = error ? error->error_code : -1;
|
|
|
|
fprintf (stderr, "RANDR Query Version returned error %d\n", ec);
|
2007-11-24 10:04:53 -07:00
|
|
|
exit (1);
|
|
|
|
}
|
2014-05-03 13:36:41 -06:00
|
|
|
if (ver_reply->major_version != 1 ||
|
|
|
|
ver_reply->minor_version < 2) {
|
|
|
|
fprintf (stderr, "RandR version %d.%d too old\n",
|
|
|
|
ver_reply->major_version, ver_reply->minor_version);
|
2007-11-24 10:04:53 -07:00
|
|
|
exit (1);
|
|
|
|
}
|
2014-05-03 13:36:41 -06:00
|
|
|
free (ver_reply);
|
|
|
|
|
|
|
|
backlight_cookie[0] = xcb_intern_atom (conn, 1, strlen("Backlight"), "Backlight");
|
|
|
|
backlight_cookie[1] = xcb_intern_atom (conn, 1, strlen("BACKLIGHT"), "BACKLIGHT");
|
|
|
|
|
|
|
|
backlight_reply = xcb_intern_atom_reply (conn, backlight_cookie[0], &error);
|
|
|
|
if (error != NULL || backlight_reply == NULL) {
|
|
|
|
int ec = error ? error->error_code : -1;
|
|
|
|
fprintf (stderr, "Intern Atom returned error %d\n", ec);
|
2007-11-24 10:04:53 -07:00
|
|
|
exit (1);
|
|
|
|
}
|
2009-10-10 04:22:10 -06:00
|
|
|
|
2014-05-03 13:36:41 -06:00
|
|
|
backlight_new = backlight_reply->atom;
|
|
|
|
free (backlight_reply);
|
|
|
|
|
|
|
|
backlight_reply = xcb_intern_atom_reply (conn, backlight_cookie[1], &error);
|
|
|
|
if (error != NULL || backlight_reply == NULL) {
|
|
|
|
int ec = error ? error->error_code : -1;
|
|
|
|
fprintf (stderr, "Intern Atom returned error %d\n", ec);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
backlight_legacy = backlight_reply->atom;
|
|
|
|
free (backlight_reply);
|
|
|
|
|
|
|
|
if (backlight_new == XCB_NONE && backlight_legacy == XCB_NONE) {
|
2007-11-24 10:04:53 -07:00
|
|
|
fprintf (stderr, "No outputs have backlight property\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2014-05-03 13:36:41 -06:00
|
|
|
|
|
|
|
iter = xcb_setup_roots_iterator (xcb_get_setup (conn));
|
|
|
|
while (iter.rem) {
|
|
|
|
xcb_screen_t *screen = iter.data;
|
|
|
|
xcb_window_t root = screen->root;
|
|
|
|
xcb_randr_output_t *outputs;
|
|
|
|
|
2018-05-21 10:11:22 -06:00
|
|
|
xcb_randr_get_screen_resources_current_cookie_t resources_cookie;
|
|
|
|
xcb_randr_get_screen_resources_current_reply_t *resources_reply;
|
2014-05-03 13:36:41 -06:00
|
|
|
|
2018-05-21 10:11:22 -06:00
|
|
|
resources_cookie = xcb_randr_get_screen_resources_current (conn, root);
|
|
|
|
resources_reply = xcb_randr_get_screen_resources_current_reply (conn, resources_cookie, &error);
|
2014-05-03 13:36:41 -06:00
|
|
|
if (error != NULL || resources_reply == NULL) {
|
|
|
|
int ec = error ? error->error_code : -1;
|
|
|
|
fprintf (stderr, "RANDR Get Screen Resources returned error %d\n", ec);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-21 10:11:22 -06:00
|
|
|
outputs = xcb_randr_get_screen_resources_current_outputs (resources_reply);
|
2014-05-03 13:36:41 -06:00
|
|
|
for (int o = 0; o < resources_reply->num_outputs; o++)
|
2007-11-24 10:04:53 -07:00
|
|
|
{
|
2014-05-03 13:36:41 -06:00
|
|
|
xcb_randr_output_t output = outputs[o];
|
2007-11-24 10:04:53 -07:00
|
|
|
double cur, new, step;
|
|
|
|
double min, max;
|
|
|
|
double set;
|
|
|
|
|
2014-05-03 13:36:41 -06:00
|
|
|
cur = backlight_get (conn, output);
|
2007-11-24 10:04:53 -07:00
|
|
|
if (cur != -1)
|
|
|
|
{
|
2014-05-03 13:36:41 -06:00
|
|
|
xcb_randr_query_output_property_cookie_t prop_cookie;
|
|
|
|
xcb_randr_query_output_property_reply_t *prop_reply;
|
|
|
|
|
|
|
|
prop_cookie = xcb_randr_query_output_property (conn, output, backlight);
|
|
|
|
prop_reply = xcb_randr_query_output_property_reply (conn, prop_cookie, &error);
|
|
|
|
|
|
|
|
if (error != NULL || prop_reply == NULL) continue;
|
|
|
|
|
|
|
|
if (prop_reply->range &&
|
|
|
|
xcb_randr_query_output_property_valid_values_length (prop_reply) == 2) {
|
|
|
|
int32_t *values = xcb_randr_query_output_property_valid_values (prop_reply);
|
|
|
|
min = values[0];
|
|
|
|
max = 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;
|
|
|
|
default:
|
|
|
|
xcb_aux_sync (conn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (new > max) new = max;
|
|
|
|
if (new < min) new = min;
|
|
|
|
step = (new - cur) / steps;
|
|
|
|
for (i = 0; i < steps && step != 0; i++)
|
|
|
|
{
|
|
|
|
if (i == steps - 1)
|
|
|
|
cur = new;
|
|
|
|
else
|
|
|
|
cur += step;
|
2019-08-15 11:09:24 -06:00
|
|
|
backlight_set (conn, output, (long) (cur + 0.5));
|
2014-05-03 13:36:41 -06:00
|
|
|
xcb_flush (conn);
|
|
|
|
usleep (total_time * 1000 / steps);
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-03 13:36:41 -06:00
|
|
|
free (prop_reply);
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-03 13:36:41 -06:00
|
|
|
|
|
|
|
free (resources_reply);
|
|
|
|
xcb_screen_next (&iter);
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|
2014-05-03 13:36:41 -06:00
|
|
|
xcb_aux_sync (conn);
|
2009-10-10 04:22:10 -06:00
|
|
|
|
|
|
|
return 0;
|
2007-11-24 10:04:53 -07:00
|
|
|
}
|