Skip to content

Commit

Permalink
Add CLI options for screen and CRTC selection (#28)
Browse files Browse the repository at this point in the history
- Add options for screen and CRTC selection.
- Address #25 .
  • Loading branch information
faf0 authored Jun 1, 2022
1 parent 0dadad0 commit a00a2f8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
PROJECT: https://github.com/faf0/sct

1.7: zikzik on May 30 2022
1.8: faf0 on 01 Jun 2022
* Options --screen and --crtc added to select a specific screen and CRTC to use

1.7: zikzik on 30 May 2022
* Options to specify the screen index and CRTC index to change the color
temperature of a specific screen and CTRC combination only

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ Execute xsct using the following command:
./xsct 3700
~~~

The first parameter (`3700` above) denotes the color temperature.
The first parameter (`3700` above) represents the color temperature.

If `xsct` is called with parameter 0, the color temperature is set to `6500`.

If `xsct` is called without parameters, the current display temperature is estimated.

If `xsct` is called with a temperature parameter as well as a screen index
followed by a CRTC index, the color temperature of the specified screen and
CRTC combination is changed only.
The following options, which can be specified before the optional temperature parameter, are supported:
- `-h`, `--help`: display the help page
- `-v`, `--verbose`: display debugging information
- `-d <delta>`, `--delta <delta>`: shift temperature by the temperature value
- `-s <screen>`, `--screen <screen>` `N`: use the screen specified by given zero-based index
- `-c <crtc>`, `--crtc <crtc>` `N`: use the CRTC specified by given zero-based index

Test xsct using the following command:
~~~sh
Expand Down
24 changes: 11 additions & 13 deletions xsct.1
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
.TH xsct 1 "Aug 2020" "1.6" "User Manual"
.TH xsct 1 "Jun 2022" "1.8" "User Manual"
.SH NAME
xsct \- X11 set screen color temperature
.SH SYNOPSIS
.B xsct
[options]
.I [temperature]
.I [screen_index]
.I [crtc_index]

.SH DESCRIPTION
.B xsct
sets the screen's color temperature.

.SH OPTIONS
.TP
.B -d, --delta
Shift temperature by given value
.TP
.B -h, --help
Display usage information and exit
.TP
.B -v, --verbose
Display debugging information
.TP
.B -d, --delta
Shift temperature by temperature value
.TP
.B -s, --screen N
Zero-based index of screen to use.
.TP
.B -c, --crtc N
Zero-based index of CRTC to use.
.TP
.I [temperature]
Black body temperature
.br
If the value is 0, xsct sets the color temperature to the default of 6500
.br
If no arguments are passed, xsct estimates the current display temperature
.TP
.I [screen_index]
Screen to change temperature of.
.TP
.I [crtc_index]
CRTC (output) of specified screen to change temperature of.

.SH AUTHOR
xsct was written by Ted Unangst <[email protected]>
xsct is based on sct by Ted Unangst <[email protected]>

.SH SEE ALSO
redshift(1), xtemp(1)
65 changes: 47 additions & 18 deletions xsct.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

static void usage(char * pname)
{
printf("Xsct (1.6)\n"
"Usage: %s [options] [temperature] [screen_index] [crtc_index]\n"
printf("Xsct (1.8)\n"
"Usage: %s [options] [temperature]\n"
"\tIf the argument is 0, xsct resets the display to the default temperature (6500K)\n"
"\tIf no arguments are passed, xsct estimates the current display temperature\n"
"Options:\n"
"\t-h, --help \t xsct will display this usage information\n"
"\t-v, --verbose \t xsct will display debugging information\n"
"\t-d, --delta \t xsct will shift temperature by given value\n"
"\t-h, --help \t xsct will display this usage information\n", pname);
"\t-d, --delta\t xsct will shift temperature by the temperature value\n"
"\t-s, --screen N\t xsct will only select screen specified by given zero-based index\n"
"\t-c, --crtc N\t xsct will only select CRTC specified by given zero-based index\n", pname);
}

#define TEMPERATURE_NORM 6500
Expand Down Expand Up @@ -64,8 +66,8 @@ static int get_sct_for_screen(Display *dpy, int screen, int icrtc, int fdebug)
double gammar = 0.0, gammag = 0.0, gammab = 0.0, gammam = 1.0, gammad = 0.0;

n = res->ncrtc;
int icrtc_is_specified = icrtc >= 0 && icrtc < n;
for (c = icrtc_is_specified ? icrtc : 0; c < (icrtc_is_specified ? icrtc + 1 : n); c++)
int icrtc_specified = icrtc >= 0 && icrtc < n;
for (c = icrtc_specified ? icrtc : 0; c < (icrtc_specified ? icrtc + 1 : n); c++)
{
RRCrtc crtcxid;
int size;
Expand Down Expand Up @@ -143,8 +145,8 @@ static void sct_for_screen(Display *dpy, int screen, int icrtc, int temp, int fd
}
if (fdebug > 0) fprintf(stderr, "DEBUG: Gamma: %f, %f, %f\n", gammar, gammag, gammab);
n = res->ncrtc;
int icrtc_is_specified = icrtc >= 0 && icrtc < n;
for (c = icrtc_is_specified ? icrtc : 0; c < (icrtc_is_specified ? icrtc + 1 : n); c++)
int icrtc_specified = icrtc >= 0 && icrtc < n;
for (c = icrtc_specified ? icrtc : 0; c < (icrtc_specified ? icrtc + 1 : n); c++)
{
int size, i;
RRCrtc crtcxid;
Expand Down Expand Up @@ -179,7 +181,7 @@ int main(int argc, char **argv)
if (!dpy)
{
perror("XOpenDisplay(NULL) failed");
fprintf(stderr, "Make sure DISPLAY is set correctly.\n");
fprintf(stderr, "ERROR! Ensure DISPLAY is set correctly!\n");
return EXIT_FAILURE;
}
screens = XScreenCount(dpy);
Expand All @@ -190,31 +192,58 @@ int main(int argc, char **argv)
crtc_specified = -1;
for (i = 1; i < argc; i++)
{
if ((strcmp(argv[i],"-v") == 0) || (strcmp(argv[i],"--verbose") == 0)) fdebug = 1;
if ((strcmp(argv[i],"-h") == 0) || (strcmp(argv[i],"--help") == 0)) fhelp = 1;
else if ((strcmp(argv[i],"-v") == 0) || (strcmp(argv[i],"--verbose") == 0)) fdebug = 1;
else if ((strcmp(argv[i],"-d") == 0) || (strcmp(argv[i],"--delta") == 0)) fdelta = 1;
else if ((strcmp(argv[i],"-h") == 0) || (strcmp(argv[i],"--help") == 0)) fhelp = 1;
else if ((strcmp(argv[i],"-s") == 0) || (strcmp(argv[i],"--screen") == 0))
{
i++;
if (i < argc)
{
screen_specified = atoi(argv[i]);
} else {
fprintf(stderr, "ERROR! Required value for screen not specified!\n");
fhelp = 1;
}
}
else if ((strcmp(argv[i],"-c") == 0) || (strcmp(argv[i],"--crtc") == 0))
{
i++;
if (i < argc)
{
crtc_specified = atoi(argv[i]);
} else {
fprintf(stderr, "ERROR! Required value for crtc not specified!\n");
fhelp = 1;
}
}
else if (temp == -1) temp = atoi(argv[i]);
else if (screen_specified == -1) screen_specified = atoi(argv[i]);
else crtc_specified = atoi(argv[i]);
else
{
fprintf(stderr, "ERROR! Unknown parameter: %s\n!", argv[i]);
fhelp = 1;
}
}

if (fhelp > 0)
if (fhelp > 0)
{
usage(argv[0]);
}
else if (screen_specified >= screens) {
fprintf(stderr, "ERROR! Invalid screen index: %d\n", screen_specified);
else if (screen_specified >= screens)
{
fprintf(stderr, "ERROR! Invalid screen index: %d!\n", screen_specified);
}
else
{
if (screen_specified >= 0) {
if (screen_specified >= 0)
{
screen_first = screen_specified;
screen_last = screen_specified;
}
if ((temp < 0) && (fdelta == 0))
{
// No arguments, so print estimated temperature for each screen
for (screen = 0; screen < screens; screen++)
for (screen = screen_first; screen <= screen_last; screen++)
{
temp = get_sct_for_screen(dpy, screen, crtc_specified, fdebug);
printf("Screen %d: temperature ~ %d\n", screen, temp);
Expand Down

0 comments on commit a00a2f8

Please sign in to comment.