Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions src/gamma-randr.c
Original file line number Diff line number Diff line change
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,37 @@ 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 */
char *local_value_marker = value;
while (*local_value_marker != 0) {
if (strncmp(local_value_marker, ",", 1) == 0)
++local_value_marker;
if (*local_value_marker == 0)
break;

errno = 0;
strtol(local_value_marker, &tail, 0);
if (!errno)
++(state->crtc_num_count);
local_value_marker = tail;
}

/* Configure all given crtcs */
state->crtc_num = calloc(state->crtc_num_count, sizeof(int));
local_value_marker = value;
for (int i = 0; i < state->crtc_num_count; ++i)
{
if (strncmp(local_value_marker, ",", 1) == 0)
++local_value_marker;

errno = 0;
int parsed = strtol(local_value_marker, &tail, 0);
if (!errno)
state->crtc_num[i] = parsed;
local_value_marker = tail;
}
} else if (strcasecmp(key, "preserve") == 0) {
state->preserve = atoi(value);
} else {
Expand All @@ -309,7 +342,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 +409,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
Original file line number Diff line number Diff line change
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