Skip to content

Commit

Permalink
Merge pull request #421 from jonls/randr-adjust-multiple
Browse files Browse the repository at this point in the history
XRandR: Allow multiple but not all CRTCs to be redshifted
  • Loading branch information
jonls committed Jan 14, 2017
2 parents d4694be + e027590 commit 460cd9c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
65 changes: 57 additions & 8 deletions src/gamma-randr.c
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#ifdef ENABLE_NLS
# include <libintl.h>
Expand All @@ -46,8 +47,9 @@ randr_init(randr_state_t *state)
{
/* Initialize state. */
state->screen_num = -1;
state->crtc_num = -1;
state->crtc_num = NULL;

state->crtc_num_count = 0;
state->crtc_count = 0;
state->crtcs = NULL;

Expand Down Expand Up @@ -263,6 +265,7 @@ randr_free(randr_state_t *state)
free(state->crtcs[i].saved_ramps);
}
free(state->crtcs);
free(state->crtc_num);

/* Close connection */
xcb_disconnect(state->conn);
Expand All @@ -277,7 +280,7 @@ randr_print_help(FILE *f)
/* TRANSLATORS: RANDR help output
left column must not be translated */
fputs(_(" screen=N\t\tX screen to apply adjustments to\n"
" crtc=N\t\tCRTC to apply adjustments to\n"
" crtc=N\tList of comma separated CRTCs to apply adjustments to\n"
" preserve={0,1}\tWhether existing gamma should be"
" preserved\n"),
f);
Expand All @@ -290,7 +293,50 @@ randr_set_option(randr_state_t *state, const char *key, const char *value)
if (strcasecmp(key, "screen") == 0) {
state->screen_num = atoi(value);
} else if (strcasecmp(key, "crtc") == 0) {
state->crtc_num = atoi(value);
char *tail;

/* Check how many crtcs are configured */
const char *local_value = value;
while (1) {
errno = 0;
int parsed = strtol(local_value, &tail, 0);
if (parsed == 0 && (errno != 0 ||
tail == local_value)) {
fprintf(stderr, _("Unable to read screen"
" number: `%s'.\n"), value);
return -1;
} else {
state->crtc_num_count += 1;
}
local_value = tail;

if (*local_value == ',') {
local_value += 1;
} else if (*local_value == '\0') {
break;
}
}

/* Configure all given crtcs */
state->crtc_num = calloc(state->crtc_num_count, sizeof(int));
local_value = value;
for (int i = 0; i < state->crtc_num_count; i++) {
errno = 0;
int parsed = strtol(local_value, &tail, 0);
if (parsed == 0 && (errno != 0 ||
tail == local_value)) {
return -1;
} else {
state->crtc_num[i] = parsed;
}
local_value = tail;

if (*local_value == ',') {
local_value += 1;
} else if (*local_value == '\0') {
break;
}
}
} else if (strcasecmp(key, "preserve") == 0) {
state->preserve = atoi(value);
} else {
Expand All @@ -309,7 +355,7 @@ randr_set_temperature_for_crtc(randr_state_t *state, int crtc_num,

if (crtc_num >= state->crtc_count || crtc_num < 0) {
fprintf(stderr, _("CRTC %d does not exist. "),
state->crtc_num);
crtc_num);
if (state->crtc_count > 1) {
fprintf(stderr, _("Valid CRTCs are [0-%d].\n"),
state->crtc_count-1);
Expand Down Expand Up @@ -376,17 +422,20 @@ randr_set_temperature(randr_state_t *state,
{
int r;

/* If no CRTC number has been specified,
/* If no CRTC numbers have been specified,
set temperature on all CRTCs. */
if (state->crtc_num < 0) {
if (state->crtc_num_count == 0) {
for (int i = 0; i < state->crtc_count; i++) {
r = randr_set_temperature_for_crtc(state, i,
setting);
if (r < 0) return -1;
}
} else {
return randr_set_temperature_for_crtc(state, state->crtc_num,
setting);
for (int i = 0; i < state->crtc_num_count; ++i) {
r = randr_set_temperature_for_crtc(
state, state->crtc_num[i], setting);
if (r < 0) return -1;
}
}

return 0;
Expand Down
3 changes: 2 additions & 1 deletion src/gamma-randr.h
Expand Up @@ -41,7 +41,8 @@ typedef struct {
int preferred_screen;
int preserve;
int screen_num;
int crtc_num;
int crtc_num_count;
int* crtc_num;
unsigned int crtc_count;
randr_crtc_state_t *crtcs;
} randr_state_t;
Expand Down

0 comments on commit 460cd9c

Please sign in to comment.