Use getopt() to parse arguments.

This commit is contained in:
Jon Lund Steffensen
2009-11-04 21:05:58 +01:00
parent 4818f331ce
commit d6164ef0dc
3 changed files with 88 additions and 35 deletions
+1 -1
View File
@@ -4,5 +4,5 @@ sources = ['redshift.c', 'solar.c', 'colortemp.c']
env = Environment()
env.ParseConfig('pkg-config --cflags --libs xcb xcb-randr')
env.Program('redshift', sources,
CFLAGS='-std=c99 -D_BSD_SOURCE',
CFLAGS='-std=gnu99 -O2 -Wall',
LINKFLAGS='-lm')
-3
View File
@@ -217,9 +217,6 @@ colortemp_set_temperature(int temp, float gamma)
interpolate_color(alpha, &blackbody_color[temp_index],
&blackbody_color[temp_index+3], white_point);
printf("White point: %f, %f, %f\n",
white_point[0], white_point[1], white_point[2]);
/* Create new gamma ramps */
uint16_t *gamma_ramps = malloc(3*gamma_ramp_size*sizeof(uint16_t));
if (gamma_ramps == NULL) abort();
+87 -31
View File
@@ -3,7 +3,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <locale.h>
#include "solar.h"
@@ -16,100 +19,153 @@
#define MIN_TEMP 1000
#define MAX_TEMP 10000
#define DEFAULT_DAY_TEMP 5500
#define DEFAULT_NIGHT_TEMP 3700
#define USAGE \
"Usage: %s LAT LON DAY-TEMP NIGHT-TEMP [GAMMA]\n"
"Usage: %s -l LAT:LON -t DAY:NIGHT [OPTIONS...]\n"
#define HELP \
USAGE \
" Set color temperature of display according to time of day.\n" \
" -g GAMMA\tAdditional gamma correction to apply\n" \
" -h\t\tDisplay this help message\n" \
" -l LAT:LON\tYour current location\n" \
" -t DAY:NIGHT\tColor temperature to set at night/day\n" \
" -v\t\tVerbose output\n"
#define DEG_CHAR 0xb0
static void
printtime(time_t time)
{
char s[64];
strftime(s, 64, "%a, %d %b %Y %H:%M:%S %z", localtime(&time));
printf("%s\n", s);
}
int
main(int argc, char *argv[])
{
/* Check extensions needed for color temperature adjustment. */
int r = colortemp_check_extension();
if (r < 0) {
fprintf(stderr, "Unable to set color temperature.\n");
fprintf(stderr, "Unable to set color temperature:"
" Needed extension is missing.\n");
exit(EXIT_FAILURE);
}
/* Init locale for special symbols */
char *loc = setlocale(LC_CTYPE, "");
/* Init locale for special (degree) symbol */
char *loc = setlocale(LC_ALL, "");
if (loc == NULL) {
fprintf(stderr, "Unable to set locale.\n");
exit(EXIT_FAILURE);
}
/* Load arguments */
if (argc < 5) {
printf(USAGE, argv[0]);
float lat = NAN;
float lon = NAN;
int temp_day = DEFAULT_DAY_TEMP;
int temp_night = DEFAULT_NIGHT_TEMP;
float gamma = 1.0;
int verbose = 0;
char *s;
int opt;
while ((opt = getopt(argc, argv, "g:hl:t:v")) != -1) {
switch (opt) {
case 'g':
gamma = atof(optarg);
break;
case 'h':
printf(HELP, argv[0]);
exit(EXIT_SUCCESS);
break;
case 'l':
s = strchr(optarg, ':');
if (s == NULL) {
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
*(s++) = '\0';
lat = atof(optarg);
lon = atof(s);
break;
case 't':
s = strchr(optarg, ':');
if (s == NULL) {
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
*(s++) = '\0';
temp_day = atoi(optarg);
temp_night = atoi(s);
break;
case 'v':
verbose = 1;
break;
default:
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
}
/* Latitude and longitude must be set */
if (isnan(lat) || isnan(lon)) {
fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, "Latitude and longitude must be set.\n");
exit(EXIT_FAILURE);
}
if (verbose) {
printf("Location: %f%lc, %f%lc\n",
lat, DEG_CHAR, lon, DEG_CHAR);
}
/* Latitude */
float lat = atof(argv[1]);
if (lat < MIN_LAT || lat > MAX_LAT) {
fprintf(stderr,
"Latitude must be between %.1f%lc and %.1f%lc\n",
"Latitude must be between %.1f%lc and %.1f%lc.\n",
MIN_LAT, DEG_CHAR, MAX_LAT, DEG_CHAR);
exit(EXIT_FAILURE);
}
/* Longitude */
float lon = atof(argv[2]);
if (lon < MIN_LON || lon > MAX_LON) {
fprintf(stderr,
"Longitude must be between %.1f%lc and %.1f%lc\n",
"Longitude must be between %.1f%lc and %.1f%lc.\n",
MIN_LON, DEG_CHAR, MAX_LON, DEG_CHAR);
exit(EXIT_FAILURE);
}
/* Color temperature at daytime */
int temp_day = atoi(argv[3]);
if (temp_day < MIN_TEMP || temp_day >= MAX_TEMP) {
fprintf(stderr, "Temperature must be between %uK and %uK\n",
fprintf(stderr, "Temperature must be between %uK and %uK.\n",
MIN_TEMP, MAX_TEMP);
exit(EXIT_FAILURE);
}
/* Color temperature at night */
int temp_night = atoi(argv[4]);
if (temp_night < MIN_TEMP || temp_night >= MAX_TEMP) {
fprintf(stderr, "Temperature must be between %uK and %uK\n",
fprintf(stderr, "Temperature must be between %uK and %uK.\n",
MIN_TEMP, MAX_TEMP);
exit(EXIT_FAILURE);
}
/* Gamma value */
float gamma = 1.0;
if (argc > 5) gamma = atof(argv[5]);
/* Current angular elevation of the sun */
time_t now = time(NULL);
double elevation = solar_elevation(now, lat, lon);
printf("Solar elevation is %f\n", elevation);
if (verbose) {
printf("Solar elevation is %f%lc.\n", elevation, DEG_CHAR);
}
/* Use elevation of sun to set color temperature */
int temp = 0;
if (elevation < SOLAR_CIVIL_TWILIGHT_ELEV) {
temp = temp_night;
printf("Set color temperature to %uK\n", temp_night);
} else if (elevation < SOLAR_DAYTIME_ELEV) {
/* Interpolate */
float a = (SOLAR_DAYTIME_ELEV - elevation) /
(SOLAR_DAYTIME_ELEV - SOLAR_CIVIL_TWILIGHT_ELEV);
temp = (1.0-a)*temp_day + a*temp_night;
printf("Interpolated (%f) temperature is %uK\n", a, temp);
} else {
temp = temp_day;
printf("Set color temperature to %uK\n", temp_day);
}
if (verbose) {
printf("Set color temperature to %uK.\n", temp_day);
}
/* Set color temperature */