diff options
author | Mattias Andrée <maandree@kth.se> | 2019-10-06 15:42:19 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-10-06 15:42:19 +0200 |
commit | 60f0fced886d2d5afd05cc157bee65f41869ce6d (patch) | |
tree | c6cf516c040002911d2184b6c96a4ba9a05c54ee /src | |
parent | typo (diff) | |
download | radharc-60f0fced886d2d5afd05cc157bee65f41869ce6d.tar.gz radharc-60f0fced886d2d5afd05cc157bee65f41869ce6d.tar.bz2 radharc-60f0fced886d2d5afd05cc157bee65f41869ce6d.tar.xz |
Rewrite everything
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | src/arg.h | 78 | ||||
-rw-r--r-- | src/macros.h | 57 | ||||
-rw-r--r-- | src/radharc.c | 50 | ||||
-rw-r--r-- | src/settings.c | 294 | ||||
-rw-r--r-- | src/settings.h | 162 | ||||
-rw-r--r-- | src/state.c | 352 | ||||
-rw-r--r-- | src/state.h | 74 |
7 files changed, 0 insertions, 1067 deletions
diff --git a/src/arg.h b/src/arg.h deleted file mode 100644 index 77a519d..0000000 --- a/src/arg.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - - - -/** - * The name of the process. - */ -extern char *argv0; - - - -/** - * Start command line parsing. - * - * `argv` and `argc` must be available as given to `main`. - * The global variable `argv0` must be declare (not necessary - * the the same file) and will be set to `argv[0]`. - * - * Only short options and "--" are supported. Short options - * may start with a '+', in which case `plus` will be 1. - * - * @example - * ARGBEGIN { - * case 'a': printf("%s\n", plus ? "+a" : "-a"); break; - * case 'b': if (plus) usage(1); printf("-b\n"); break; - * case 'c': if (plus) printf("+c\n"); else printf("-c %s\n", ARGF()); break; - * default: usage(1); break; - * } ARGEND; - */ -#define ARGBEGIN \ -do { \ - for (argv0 = *argv++, argc -= !!argv0; *argv; argv++, argc--) { \ - int plus = argv[0][0] == '+', next__ = 0; \ - if (((argv[0][0] != '-') && !plus) || (argv[0][1] == (plus ? '+' : '-'))) { \ - if ((argv[0][0] == argv[0][1]) && (argv[0][1] == '-') && (argv[0][2] == '\0')) \ - argc--, argv++; \ - break; \ - } \ - for (argv[0]++; argv[0][0] && !next__;) { \ - switch (*(argv[0])++) - -/** - * End of command line parsing, - */ -#define ARGEND \ - } \ - } \ -} while (0) - -/** - * Get the argument of the current option. - * Do not use more once per option. - * - * The function `noreturn void usage(int)` must be available. - * `usage` shall exit if its argument is `1`. - * - * @return The argument of the current option. - */ -#define ARGF() \ - (next__ = 1, argv[0][0] ? (argv[0]) \ - : (argv++, (argv[0] ? (argc--, argv[0]) \ - : (usage(1), NULL)))) - diff --git a/src/macros.h b/src/macros.h deleted file mode 100644 index 298a41a..0000000 --- a/src/macros.h +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include <string.h> -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> - - - -#define try(...) do { if (!(__VA_ARGS__)) goto fail; } while (0) -#define t(...) do { if (__VA_ARGS__) goto fail; } while (0) -#define CLEANUP(...) do { int cleanup__ = errno; __VA_ARGS__; errno = cleanup__; } while (0) - -#define xstrdup(outp, ...) \ -do { \ - const char *xstrdup__ = (__VA_ARGS__); \ - if (xstrdup__) \ - try (*(outp) = strdup(xstrdup__)); \ - else \ - *(outp) = NULL; \ -} while (0) - -#define xpread(fd, buf, len, off) t (pread(fd, buf, len, off) < (ssize_t)(len)) -#define xpwrite(fd, buf, len, off) t (pwrite(fd, buf, len, off) < (ssize_t)(len)) -#define xread(fd, buf, len) t (read(fd, buf, len) < (ssize_t)(len)) -#define xwrite(fd, buf, len) t (write(fd, buf, len) < (ssize_t)(len)) - -#define xcalloc(outp, num) try (*(outp) = calloc(num, sizeof(**(outp)))) -#define xmalloc(outp, num) try (*(outp) = malloc((num) * sizeof(**(outp)))) -#define xrealloc(outp, num) \ -do { \ - size_t n__ = (num); \ - void *new__ = realloc(*(outp), n__ * sizeof(**(outp))); \ - t (n__ && !new__); \ - *(outp) = new__; \ -} while (0) - -#define SHRINK(outp, num) \ -do { \ - void *new__ = realloc(*(outp), (num) * sizeof(**(outp))); \ - if (new__) *(outp) = new__; \ -} while (0) - diff --git a/src/radharc.c b/src/radharc.c deleted file mode 100644 index a0b1344..0000000 --- a/src/radharc.c +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "state.h" -#include "macros.h" - -#include <libred.h> -#include <libhaiku.h> - - - -/** - * The name of the process. - */ -char *argv0 = "radharc"; - - - -int -main(int argc, char *argv[]) -{ - struct settings settings; - - t (libred_check_timetravel()); - parse_command_line(argc, argv, &settings); - argv0 = argv0 ? argv0 : "radharc"; - t (get_state_pathname(&settings)); - t (libred_init_colour()); - - return 0; - -fail: - libhaiku_perror(argv0); - libred_term_colour(); - return 1; -} - diff --git a/src/settings.c b/src/settings.c deleted file mode 100644 index 1c32a2f..0000000 --- a/src/settings.c +++ /dev/null @@ -1,294 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "settings.h" -#include "arg.h" -#include "macros.h" -#include <ctype.h> -#include <errno.h> -#include <math.h> -#include <limits.h> - -#include <libhaiku.h> - - - -/** - * Print usage information and exit if a condition is met. - * - * @param condition Do no do anything iff this is zero. - */ -static void -usage(int condition) -{ - if (!condition) return; - fprintf(stderr, - "Usage: %s [OPTIONS]...\n" - "See `man 1 radharc` for more information.\n", - !argv0 ? "radharc" : argv0), exit(2); -} - - -/** - * Parse a temperature string. - * - * @param str The temperature string. - * @param temp Output parameter for the temperature. - * Overflow is truncated. - * @param direction Unless `NULL`, will be set to +1 if `str` - * starts with a '+', `-1` if `str` starts - * with a '-', 0 otherwise. If `NULL` and - * `str` starts with a '-' or a '+', the - * function will fail. - * @param lower The function shall fail if the evaluated - * temperature is below this. - * @return 0 on success, -1 on error. - */ -static int -parse_temperature(const char *str, long int *temp, int *direction, int lower) -{ - char *end; - int dir = *str == '-' ? -1 : *str == '+' ? +1 : 0; - str += !!dir; - t (dir && !direction); - if (dir) *direction = dir; - t (!isdigit(*str)); - *temp = (errno = 0, strtol)(str, &end, 10); - t ((errno && !((errno == ERANGE) && (*temp == LONG_MAX))) || *end || (*temp < lower)); - return 0; -fail: - return -1; -} - - -/** - * Parse a string as a positively valued `struct timespec`. - * - * @param str The string. - * @param ts Output parameter for the value. - * @return 0 on success, -1 on error. - */ -static int -parse_timespec(const char *str, struct timespec *ts) -{ -#define DIGIT(var, c) var += (var) * 10 + ((c) & 15); -#define ALL_DIGITS(cond, var) while ((cond) && isdigit(*str)) DIGIT(var, *str++) - - int points = 0; - memset(ts, 0, sizeof(*ts)); - - /* Parse seconds. */ - t (!isdigit(*str)); - ALL_DIGITS(1, ts->tv_sec); - - /* End? */ - if (!*str) return 0; - t (*str != '.'); - - /* Parse nanoseconds.*/ - ALL_DIGITS(points++ < 9, ts->tv_nsec); - if ((points == 9) && isdigit(*str) && (*str++ >= '5') && (++(ts->tv_nsec) == 1000000000L)) - ts->tv_sec += 1, ts->tv_nsec = 0; - while (isdigit(*str)) str++; - t (*str); - - /* End! */ - return 0; -fail: - return -1; -} - - -/** - * Parse a latitude or a longitude value. - * - * @param str The string. - * @param loc Output parameter for the value. - * @param limit The limit of the absolute value. - * @return 0 on success, -1 on error. - */ -static int -parse_location(char *str, double *loc, double limit) -{ - char* end; - if (strstr(str, "−") == str) /* Support proper minus. */ - *(str += strlen("−") - 1) = '-'; - if ((*str != '-') && (*str != '+') && (*str != '.') && !isdigit(*str)) - return -1; - *loc = (errno = 0, strtod)(str, &end); - return -(errno || *loc || (fabs(*loc) > limit)); -} - - -/** - * Parse the command line. - * - * @param argc The number of elements in `argv`. - * @param argv The commnad line arguments including the zeroth elemenet. - * @param settings Output parameter for the settings. - */ -void -parse_command_line(int argc, char *argv[], struct settings *s) -{ - int location_set = 0; - char *p; - char *arg; - int c = 0; - - memset(s, 0, sizeof(*s)); - s->natural_temp = 6500; - s->day_temp = 5500; - s->night_temp = 3500; - s->trans_speed = 50; - -#define BOOLEAN(var) var = !plus; break -#define PLUS(...) (plus ? (__VA_ARGS__) : 0) - ARGBEGIN { - case 'l': - PLUS(location_set = 0); - usage(!(p = strchr(arg = ARGF(), ':'))); - *p++ = '\0', location_set = 1; - usage(parse_location(arg, &(s->latitude), 90.0)); - usage(parse_location(p, &(s->longitude), 180.0)); - break; - case 't': - PLUS(s->day_temp = 5500, s->night_temp = 3500); - s->temp = s->day_temp = s->night_temp = 0, s->temp_direction = 0; - if ((p = strchr(arg = ARGF(), ':'))) { - *p++ = '\0'; - usage(parse_temperature(arg, &(s->day_temp), NULL, 1000)); - usage(parse_temperature(p, &(s->night_temp), NULL, 1000)); - } else { - usage(parse_temperature(arg, &(s->temp), &(s->temp_direction), 1000)); - } - break; - case 'T': - PLUS(s->natural_temp = 6500); - usage(parse_temperature(ARGF(), &(s->natural_temp), NULL, 1000)); - break; - case 's': - PLUS(s->trans_speed = 50); - s->trans_speed = 0; - usage(parse_timespec(ARGF(), &(s->transition))); - break; - case 'S': - PLUS(s->trans_speed = 0); - usage(parse_temperature(ARGF(), &(s->trans_speed), NULL, 1)); - break; - case 'h': - s->hookpath = (plus ? NULL : ARGF()); - break; - case 'd': c++; /* Fall though. */ - case 'e': c++; /* Fall though. */ - case 'm': c++; - PLUS(s->monitors_n = 0, free(s->monitors_id), free(s->monitors_arg)); - xrealloc(&(s->monitors_id), s->monitors_n + 1); - xrealloc(&(s->monitors_arg), s->monitors_n + 1); - s->monitors_id[s->monitors_n] = ARGF(); - s->monitors_arg[s->monitors_n++] = (c == 3 ? 'm' : c == 2 ? 'e' : 'd'), c = 0; - break; - case 'p': BOOLEAN(s->print_status); - case 'n': BOOLEAN(s->panic_start); - case 'N': BOOLEAN(s->panic_else); - case 'o': BOOLEAN(s->set_and_exit); - case 'x': BOOLEAN(s->ignore_calib); - case 'i': BOOLEAN(s->negative); - case 'b': BOOLEAN(s->use_bus); - default: usage(1); break; - } ARGEND; - usage(argc); - - if (!location_set && !(s->temp)) - fprintf(stderr, - "%s: The -l option is mandatory, unless single value -t is used. " - "See `man 1 radharc` for more information.\n", - !argv0 ? "radharc" : argv0), exit(2); - - return; -fail: - libhaiku_perror(argv0 ? argv0 : "radharc"); - exit(1); -} - - -/** - * Marshal settings into a buffer. - * - * @param buffer The buffer, `NULL` if you want to know the required size. - * @param settings The settings to marshal. - * @return The size of the output. - */ -size_t -marshal_settings(char *buffer, const struct settings *settings) -{ -#define MARSHAL(N, DATUMP) (n += aux = (N), (buf ? (memcpy(buf, DATUMP, aux), buf += aux, 0) : 0)) - - size_t aux, n = 0, i = 0; - char *buf = buffer; - - if (buffer) i = marshal_settings(NULL, settings); - MARSHAL(sizeof(i), &i); - - MARSHAL(sizeof(*settings), settings); - if (settings->hookpath) MARSHAL(strlen(settings->hookpath) + 1, settings->hookpath); - if (settings->monitors_arg) MARSHAL(settings->monitors_n, settings->monitors_arg); - for (i = 0; i < settings->monitors_n; i++) - MARSHAL(strlen(settings->monitors_id[i]) + 1, settings->monitors_id[i]); - - return n; -} - - -/** - * Unmarshal settings from a buffer. - * - * @param buffer The buffer. - * @param settings Output parameter for the settings, will be allocated. - * @return The number of unmarshalled bytes, 0 on error. - */ -size_t -unmarshal_settings(char *buffer, struct settings **settings) -{ -#define UNMARSHAL(N, DATUMP) (aux = (N), memcpy((DATUMP), buf, aux), buf += aux) - - size_t n, aux, i; - struct settings s_, *s = NULL; - char *buf = buffer; - - UNMARSHAL(sizeof(n), &n); - if (!(s = *settings = malloc(n - sizeof(n)))) return 0; - s->monitors_id = NULL, s->monitors_arg = NULL; - - UNMARSHAL(sizeof(s_), &s_); - if (s_.monitors_n) { - *s = s_; - xmalloc(&(s->monitors_id), s_.monitors_n); - xmalloc(&(s->monitors_arg), s_.monitors_n); - } - - buf = strchr(s->hookpath = buf, '\0') + 1; - - UNMARSHAL(s_.monitors_n, s->monitors_arg); - for (i = 0; i < s_.monitors_n; i++) - UNMARSHAL(strlen(buf + 1), s->monitors_id[i]); - - return n; - -fail: - CLEANUP(free(s->monitors_id), free(s->monitors_arg), free(s), *settings = NULL); - return 0; -} - diff --git a/src/settings.h b/src/settings.h deleted file mode 100644 index 492e4b0..0000000 --- a/src/settings.h +++ /dev/null @@ -1,162 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include <time.h> - - - -/** - * Settings from the command line. - */ -struct settings { - /** - * Print current status and exit? - */ - int print_status : 1; - - /** - * Start without transition? - */ - int panic_start : 1; - - /** - * Never transition, apart from at start? - */ - int panic_else : 1; - - /** - * Set temperature, possibly with transition, and exit? - */ - int set_and_exit : 1; - - /** - * Ignore calibrations? - */ - int ignore_calib : 1; - - /** - * Apply negative image filter? - */ - int negative : 1; - - /** - * Broadcast event with bus? - */ - int use_bus : 1; - - /** - * -1 to decrease temperature, - * +1 to increase temperature, - * 0 to set temperature. - */ - int temp_direction; - - /** - * The temperature, if use, the program will exit when it is done. - */ - long int temp; - - /** - * The temperature at full daytime. - */ - long int day_temp; - - /** - * The temperature at full night. - */ - long int night_temp; - - /** - * The temperature when disabled. - */ - long int natural_temp; - - /** - * Pathname to the hook script. - * Do not free this. - */ - char *hookpath; - - /** - * The number of seconds the transition takes. - */ - struct timespec transition; - - /** - * The number of kelvins per seconds the - * temperature is adjusted during transition. - */ - long int trans_speed; - - /** - * The user's latitudinal position. - */ - double latitude; - - /** - * The user's longitudinal position. - */ - double longitude; - - /** - * The number of elements in `monitors_id` and in `monitors_arg`. - */ - size_t monitors_n; - - /** - * Values for -d, -e, and -m, in order. - * Do not free its elements. - */ - char **monitors_id; - - /** - * The option (the character after the dash) - * the option used in correspnding element - * in `monitors_id`. - */ - char *monitors_arg; -}; - - - -/** - * Parse the command line. - * - * @param argc The number of elements in `argv`. - * @param argv The commnad line arguments including the zeroth elemenet. - * @param s Output parameter for the settings. - */ -void parse_command_line(int argc, char *argv[], struct settings *s); - - -/** - * Marshal settings into a buffer. - * - * @param param The buffer, `NULL` if you want to know the required size. - * @param settings The settings to marshal. - * @return The size of the output. - */ -size_t marshal_settings(char *buffer, const struct settings *settings); - -/** - * Unmarshal settings from a buffer. - * - * @param buffer The buffer. - * @param settings Output parameter for the settings, will be allocated. - * @return The number of unmarshalled bytes, 0 on error. - */ -size_t unmarshal_settings(char *buffer, struct settings **settings); - diff --git a/src/state.c b/src/state.c deleted file mode 100644 index e5c2a42..0000000 --- a/src/state.c +++ /dev/null @@ -1,352 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "state.h" -#include "macros.h" -#include <errno.h> -#include <limits.h> -#include <stdarg.h> - -#include <libred.h> -#include <libgamma.h> - - - -/** - * The name of the process. - */ -extern char *argv0; - - - -/** - * All sites. - */ -static libgamma_site_state_t *sites = NULL; - -/** - * The number of sites stored in `sites`. - */ -static size_t sites_n = 0; - -/** - * All partitions. - */ -static libgamma_partition_state_t *parts = NULL; - -/** - * The number of partitions stored in `parts`. - */ -static size_t parts_n = 0; - - - -/** - * Is it daytime, night, perhaps some kind of twilight? - * - * @param elevation The Sun's apparent elevation. - * @return The time of the day. - */ -enum darkness -get_darkness(double elevation) -{ - if (elevation > LIBRED_SOLAR_ELEVATION_SUNSET_SUNRISE) return DAYTIME; - if (elevation > LIBRED_SOLAR_ELEVATION_CIVIL_DUSK_DAWN) return CIVIL_TWILIGHT; - if (elevation > LIBRED_SOLAR_ELEVATION_NAUTICAL_DUSK_DAWN) return NAUTICAL_TWILIGHT; - if (elevation > LIBRED_SOLAR_ELEVATION_ASTRONOMICAL_DUSK_DAWN) return ASTRONOMICAL_TWILIGHT; - return NIGHT; -} - - -/** - * Remove the screen number for a display server instance identifier. - * - * @param s Display server instance identifier. - * @return Set the value to which this pointer points to '.'. Do nothing if it is `NULL`. - */ -static char * -strip_screen(char *s) -{ -#define S(V, CD) ((V = ((CD)[1] == 'r' ? strrchr : strchr)(V, (CD)[0]))) - char *p = strchr(s, '='); - if ((p++) && (*p != '/') && S(p, ":r") && S(p, ".l")) *p = '\0'; else p = NULL; - return p; -#undef S -} - - -/** - * `stpmulcpy(o, a, b, c, NULL)` is equivalent to - * `stpcpy(stpcpy(stpcpy(o, a), b), c)` - */ -#ifdef __GNUC__ -__attribute__((__sentinel__)) -#endif -static char * -stpmulcpy(char *out, ... /*, NULL */) -{ - va_list args; - char *p = out; - const char *s; - va_start(args, out); - while ((s = va_arg(args, const char *))) p = stpcpy(p, s); - va_end(args); - return p; -} - - -/** - * Compare two display server environment strings. - * - * @param a_ One of the string. - * @param b_ The other string. - * @return -1, 0, or +1. - */ -static int -displayenvcmp(const void *a_, const void *b_) -{ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" -#endif - char *a = a_; - char *b = b_; -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif - char *p = strip_screen(a); - char *q = strip_screen(b); - int rc; - - rc = strcmp(a, b); - if (p) *p = '.'; - if (q) *q = '.'; - return rc; -} - - -/** - * Make an display string safe for a pathname. - * - * @param str The string. - * @return A safe version of the string, `NULL` on error. - */ -static char * -escape_display(const char* str) -{ - char *r, *w, *rc = NULL; - int s = 0; - xmalloc(&rc, 2 * strlen(str) + 1); strcpy(rc, str); - for (r = w = strchr(rc, '=') + 1; *r; r++) { - if (!s || (*r != '/')) { - if (strchr("@=/", *r)) *w++ = '@'; - *w++ = ((s = (*r == '/')) ? 's' : *r); - } - } - w[s ? -2 : 0] = '\0'; -fail: - return rc; -} - - -/** - * The string of display servers. - * - * @param settings The settings. - * @return The string, `NULL` on error. - */ -static char * -get_display_string(const struct settings *settings) -{ -#define DISPLAY(VAR, D) p = strip_screen(D); try (VAR = escape_display(D)); if (p) *p = '.', p = NULL - - const char *var, *val; - char *r, *p = NULL, *d = NULL, *rc = NULL, **displays; - size_t i, n = 0, len = 0; - int method; - - xmalloc(&displays, settings->monitors_n); - for (i = 0; i < settings->monitors_n; i++) - if ((settings->monitors_arg[i] == 'd') && strchr(settings->monitors_id[i], '=')) - len += 1 + strlen(displays[n++] = settings->monitors_id[i]); - if (n) goto custom; - free(displays), displays = NULL; - - if (!libgamma_list_methods(&method, 1, 0)) { - fprintf(stderr, "No display was found.\n" - "DRM support missing.\n" - "Can you even see?\n"); - return errno = 0, NULL; - } - - var = libgamma_method_default_site_variable(method); - val = libgamma_method_default_site(method); - if (!val || !*val) return strdup(""); - xmalloc(&d, 3 + strlen(var) + strlen(val)); - stpmulcpy(d, ".", var, "=", val, NULL); - DISPLAY(rc, d); - return free(d), rc; - -custom: - qsort(displays, n, sizeof(*displays), displayenvcmp); - xmalloc(&rc, 2 * len + 1); - for (r = rc, i = 0; i < n; i++) { - DISPLAY(d, displays[i]); - r = stpmulcpy(r, ".", d, NULL), free(d), d = NULL; - } - free(displays); - return rc; - -fail: - if (p) *p = '.'; - CLEANUP(free(rc), free(d), free(displays)); - return NULL; -} - - -/** - * Set $RADHARC_STATE. - * - * @param settings The settings. - * @return 0 on success, -1 on error. - */ -int -get_state_pathname(const struct settings *settings) -{ - const char *dir = getenv("XGD_RUNTIME_DIR"); - char *display = NULL; - char *env = NULL; - int rc = -1; - try (display = get_display_string(settings)); - if (!dir || !*dir) dir = "/run"; - xmalloc(&env, strlen(dir) + sizeof("/radharc/") + strlen(display)); - stpmulcpy(env, dir, "/radharc/", display, NULL); - rc = setenv("RADHARC_STATE", env, 1); -fail: - CLEANUP(free(env), free(display)); - return rc; -} - - -/** - * Parse a value for the -d option, or select preferred adjustment method. - * - * @param display The argument for the -d option. `NULL` for the preferred adjustment method. - * @return The adjustment method. -1 if not found. - */ -static int -get_clut_method(const char *display) -{ -#define HAIKU(TEXT) t ((msg = (TEXT))) -#define TEST(STR, ID) if (!strcasecmp(display, STR)) return ID - - const char *env, *msg; - int method; - - /* Default? */ - if (!display) { - if (!libgamma_list_methods(&method, 1, 0)) - HAIKU("No display was found.\n" - "DRM support missing.\n" - "Can you even see?\n"); - return method; - } - - /* Single-sited? */ - TEST("none", INT_MAX); - TEST("drm", LIBGAMMA_METHOD_LINUX_DRM); - - /* Unrecognised single-sited? */ - if (!strchr(display, '=')) - HAIKU("Specified display\n" - "cannot be recognised.\n" - "Try something else.\n"); - - /* Multi-sited? */ - for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) { - env = libgamma_method_default_site_variable(method); - if (env && (strstr(display, env) == display) && (display[strlen(env)] == '=')) - return method; - } - - /* Unrecognised multi-sited. */ - HAIKU("Specified display\n" - "cannot be recognised.\n" - "Try to recompile.\n"); - -fail: - fprintf(stderr, "%s", msg); - return errno = 0, -1; -} - - -/** - * Initialise the CLUT support. - * - * @param settings The settings. - * @return 0 on success, -1 on error. - */ -int -initialise_clut(const struct settings *settings) -{ -#define NONE_METHOD (method == INT_MAX) - - int method = 0, error = 0; - const char *sitename_; - char *sitename = NULL; - size_t i, j, parts_off = 0; - libgamma_site_state_t site; - - xcalloc(&sites, settings->monitors_n + 1); - - for (i = 0; i < settings->monitors_n; i++) { - switch (settings->monitors_arg[i]) { - case 'd': - parts_off = parts_n; - t ((method = get_clut_method(sitename_ = settings->monitors_id[i])) < 0); - if (NONE_METHOD) break; - sitename_ = strchr(sitename_, '='); - xstrdup(&sitename, sitename_ ? sitename_ + 1 : NULL); - t ((error = libgamma_site_initialise(sites + sites_n, method, sitename))); - sitename = NULL, site = sites[sites_n++]; - xrealloc(&parts, parts_n + site.partitions_available); - for (j = 0; j < site.partitions_available; j++) { - t ((error = libgamma_partition_initialise(parts + parts_n, &site, j))); - parts_n++; - } - break; - - case 'm': - if (NONE_METHOD) break; - /* TODO -m */ - break; - - case 'e': - if (NONE_METHOD) break; - /* TODO -e */ - break; - } - } - - SHRINK(&sites, sites_n + 1); - - return 0; -fail: - if (error) libgamma_perror(argv0, error), errno = 0; - CLEANUP(free(sitename)); - return -1; -} - diff --git a/src/state.h b/src/state.h deleted file mode 100644 index f2c6b11..0000000 --- a/src/state.h +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "settings.h" - - - -/** - * The times of the day, by degree of darkness. - */ -enum darkness { - /** - * Not calculated yet. - */ - UNKNOWN = -1, - - /** - * Hopefully, it is bright outside. - */ - DAYTIME = 0, - - /** - * The sky is golden. The golden "hour". - * Also known as BMCT (dawn) or EECT (dusk). - */ - CIVIL_TWILIGHT = 1, - - /** - * The sky is pink and blue. - */ - NAUTICAL_TWILIGHT = 2, - - /** - * The sky is medium dark blue. - */ - ASTRONOMICAL_TWILIGHT = 3, - - /** - * The sky is really dark blue. - */ - NIGHT = 4 -}; - - - -/** - * Is it daytime, night, perhaps some kind of twilight? - * - * @param elevation The Sun's apparent elevation. - * @return The time of the day. - */ -enum darkness get_darkness(double elevation); - -/** - * Set $RADHARC_STATE. - * - * @param settings The settings. - * @return 0 on success, -1 on error. - */ -int get_state_pathname(const struct settings *settings); - |