diff options
author | Mattias Andrée <maandree@kth.se> | 2019-10-07 19:09:51 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-10-07 19:09:51 +0200 |
commit | 0a5ba369adc08a4ccafc10f3fc36bed95d7f3c58 (patch) | |
tree | 2355a9417d1a225bfcfdb0129d5eaba364ab0683 | |
parent | Reorganise, change license, portable makefile, and reference radharc (diff) | |
download | cg-tools-0a5ba369adc08a4ccafc10f3fc36bed95d7f3c58.tar.gz cg-tools-0a5ba369adc08a4ccafc10f3fc36bed95d7f3c58.tar.bz2 cg-tools-0a5ba369adc08a4ccafc10f3fc36bed95d7f3c58.tar.xz |
Change coding style and fix warnings
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | cg-base.c | 1303 | ||||
-rw-r--r-- | cg-base.h | 124 | ||||
-rw-r--r-- | cg-brilliance.c | 332 | ||||
-rw-r--r-- | cg-darkroom.c | 357 | ||||
-rw-r--r-- | cg-gamma.c | 662 | ||||
-rw-r--r-- | cg-icc.c | 1271 | ||||
-rw-r--r-- | cg-limits.c | 896 | ||||
-rw-r--r-- | cg-linear.c | 273 | ||||
-rw-r--r-- | cg-negative.c | 245 | ||||
-rw-r--r-- | cg-query.c | 734 | ||||
-rw-r--r-- | cg-rainbow.c | 251 | ||||
-rw-r--r-- | cg-remove.c | 564 | ||||
-rw-r--r-- | cg-shallow.c | 271 | ||||
-rw-r--r-- | cg-sleepmode.c | 534 | ||||
-rw-r--r-- | config.mk | 4 |
15 files changed, 3887 insertions, 3934 deletions
@@ -17,7 +17,7 @@ /** * The process's name */ -const char* argv0 = NULL; +const char *argv0 = NULL; /** * The libcoopgamma context @@ -27,18 +27,18 @@ libcoopgamma_context_t cg; /** * The names of the selected CRTC:s */ -char** crtcs = NULL; +char **crtcs = NULL; /** * Gamma ramp updates for each CRTC */ -filter_update_t* crtc_updates = NULL; +filter_update_t *crtc_updates = NULL; /** * CRTC and monitor information about * each selected CRTC and connect monitor */ -libcoopgamma_crtc_info_t* crtc_info = NULL; +libcoopgamma_crtc_info_t *crtc_info = NULL; /** * The number of selected CRTC:s @@ -54,7 +54,7 @@ size_t filters_n = 0; /** * Contexts for asynchronous ramp updates */ -static libcoopgamma_async_context_t* asyncs = NULL; +static libcoopgamma_async_context_t *asyncs = NULL; /** * The number of pending receives @@ -73,35 +73,35 @@ static int flush_pending = 0; */ struct crtc_sort_data { - /** - * The gamma ramp type - */ - libcoopgamma_depth_t depth; - - /** - * Should be 0 - */ - int __padding; - - /** - * The size of the red gamma ramp - */ - size_t red_size; - - /** - * The size of the green gamma ramp - */ - size_t green_size; - - /** - * The size of the blue gamma ramp - */ - size_t blue_size; - - /** - * The index of the CRTC - */ - size_t index; + /** + * The gamma ramp type + */ + libcoopgamma_depth_t depth; + + /** + * Should be 0 + */ + int __padding; + + /** + * The size of the red gamma ramp + */ + size_t red_size; + + /** + * The size of the green gamma ramp + */ + size_t green_size; + + /** + * The size of the blue gamma ramp + */ + size_t blue_size; + + /** + * The index of the CRTC + */ + size_t index; }; @@ -113,9 +113,10 @@ struct crtc_sort_data * @param b Return +1 if this string is less than `a` * @return See `a` and `b`, 0 is returned if `a` and `b` are equal */ -static int nulstrcmp(const char *a, const char *b) +static int +nulstrcmp(const char *a, const char *b) { - return (a == NULL) ? -1 : strcmp(a, b); + return !a ? -1 : strcmp(a, b); } @@ -126,12 +127,13 @@ static int nulstrcmp(const char *a, const char *b) * @param b_ Return +1 if this one is higher * @return See `a_` and `b_`, only -1 or +1 can be returned */ -static int crtc_sort_data_cmp(const void* a_, const void* b_) +static int +crtc_sort_data_cmp(const void *a_, const void *b_) { - const struct crtc_sort_data* a = a_; - const struct crtc_sort_data* b = b_; - int cmp = memcmp(a, b, sizeof(*a) - sizeof(a->index)); - return cmp ? cmp : a->index < b->index ? -1 : +1; + const struct crtc_sort_data *a = a_; + const struct crtc_sort_data *b = b_; + int cmp = memcmp(a, b, sizeof(*a) - sizeof(a->index)); + return cmp ? cmp : a->index < b->index ? -1 : +1; } @@ -140,62 +142,58 @@ static int crtc_sort_data_cmp(const void* a_, const void* b_) * * @return Zero on success, -1 on error */ -int make_slaves(void) +int +make_slaves(void) { - struct crtc_sort_data* data; - size_t i, j, n = 0, master = 0, master_i; - - data = alloca(filters_n * sizeof(*data)); - memset(data, 0, filters_n * sizeof(*data)); - for (i = 0; i < filters_n; i++) - { - if (!(crtc_info[crtc_updates[i].crtc].supported)) - continue; - - data[n].depth = crtc_updates[i].filter.depth; - data[n].red_size = crtc_updates[i].filter.ramps.u8.red_size; - data[n].green_size = crtc_updates[i].filter.ramps.u8.green_size; - data[n].blue_size = crtc_updates[i].filter.ramps.u8.blue_size; - data[n].index = i; - n++; - } - - qsort(data, n, sizeof(*data), crtc_sort_data_cmp); - if (n == 0) - return 0; - - master_i = data[0].index; - for (i = 1; i < n; i++) - if (memcmp(data + i, data + master, sizeof(*data) - sizeof(data->index))) - { - if (master + 1 < i) - { - crtc_updates[master_i].slaves = calloc(i - master, sizeof(size_t)); - if (crtc_updates[master_i].slaves == NULL) - return -1; - for (j = 1; master + j < i; j++) - crtc_updates[master_i].slaves[j - 1] = data[master + j].index; - } - master = i; - master_i = data[master].index; - } - else - { - libcoopgamma_ramps_destroy(&(crtc_updates[data[i].index].filter.ramps.u8)); - crtc_updates[data[i].index].master = 0; - crtc_updates[data[i].index].filter.ramps.u8 = crtc_updates[master_i].filter.ramps.u8; - } - - if (master + 1 < i) - { - crtc_updates[master_i].slaves = calloc(i - master, sizeof(size_t)); - if (crtc_updates[master_i].slaves == NULL) - return -1; - for (j = 1; master + j < i; j++) - crtc_updates[master_i].slaves[j - 1] = data[master + j].index; - } - - return 0; + struct crtc_sort_data *data; + size_t i, j, n = 0, master = 0, master_i; + + data = alloca(filters_n * sizeof(*data)); + memset(data, 0, filters_n * sizeof(*data)); + for (i = 0; i < filters_n; i++) { + if (!crtc_info[crtc_updates[i].crtc].supported) + continue; + + data[n].depth = crtc_updates[i].filter.depth; + data[n].red_size = crtc_updates[i].filter.ramps.u8.red_size; + data[n].green_size = crtc_updates[i].filter.ramps.u8.green_size; + data[n].blue_size = crtc_updates[i].filter.ramps.u8.blue_size; + data[n].index = i; + n++; + } + + qsort(data, n, sizeof(*data), crtc_sort_data_cmp); + if (!n) + return 0; + + master_i = data[0].index; + for (i = 1; i < n; i++) { + if (memcmp(data + i, data + master, sizeof(*data) - sizeof(data->index))) { + if (master + 1 < i) { + crtc_updates[master_i].slaves = calloc(i - master, sizeof(size_t)); + if (!crtc_updates[master_i].slaves) + return -1; + for (j = 1; master + j < i; j++) + crtc_updates[master_i].slaves[j - 1] = data[master + j].index; + } + master = i; + master_i = data[master].index; + } else { + libcoopgamma_ramps_destroy(&crtc_updates[data[i].index].filter.ramps.u8); + crtc_updates[data[i].index].master = 0; + crtc_updates[data[i].index].filter.ramps.u8 = crtc_updates[master_i].filter.ramps.u8; + } + } + + if (master + 1 < i) { + crtc_updates[master_i].slaves = calloc(i - master, sizeof(size_t)); + if (!crtc_updates[master_i].slaves) + return -1; + for (j = 1; master + j < i; j++) + crtc_updates[master_i].slaves[j - 1] = data[master + j].index; + } + + return 0; } @@ -213,31 +211,32 @@ int make_slaves(void) * @throws EINTR Call to `poll` was interrupted by a signal * @throws EAGAIN Call to `poll` timed out */ -int update_filter(size_t index, int timeout) +int +update_filter(size_t index, int timeout) { - filter_update_t* filter = crtc_updates + index; - - if (!(filter->synced) || filter->failed) - abort(); - - pending_recvs += 1; - - if (libcoopgamma_set_gamma_send(&(filter->filter), &cg, asyncs + index) < 0) - switch (errno) - { - case EINTR: - case EAGAIN: + filter_update_t *filter = crtc_updates + index; + + if (!filter->synced || filter->failed) + abort(); + + pending_recvs += 1; + + if (libcoopgamma_set_gamma_send(&filter->filter, &cg, asyncs + index) < 0) { + switch (errno) { + case EINTR: + case EAGAIN: #if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif - flush_pending = 1; - break; - default: - return -1; - } + flush_pending = 1; + break; + default: + return -1; + } + } - filter->synced = 0; - return synchronise(timeout); + filter->synced = 0; + return synchronise(timeout); } @@ -254,78 +253,73 @@ int update_filter(size_t index, int timeout) * @throws EINTR Call to `poll` was interrupted by a signal * @throws EAGAIN Call to `poll` timed out */ -int synchronise(int timeout) +int +synchronise(int timeout) { - struct pollfd pollfd; - size_t selected; - - pollfd.fd = cg.fd; - pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; - if (flush_pending > 0) - pollfd.events |= POLLOUT; - - pollfd.revents = 0; - if (poll(&pollfd, (nfds_t)1, timeout) < 0) - return -1; - - if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) - { - if (libcoopgamma_flush(&cg) < 0) - goto sync; - flush_pending = 0; - } - - if ((timeout < 0) && (pending_recvs > 0)) - if (!(pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI))) - { + struct pollfd pollfd; + size_t selected; + + pollfd.fd = cg.fd; + pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; + if (flush_pending > 0) + pollfd.events |= POLLOUT; + pollfd.revents = 0; - if (poll(&pollfd, (nfds_t)1, -1) < 0) - return -1; - } - + if (poll(&pollfd, (nfds_t)1, timeout) < 0) + return -1; + + if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) { + if (libcoopgamma_flush(&cg) < 0) + goto sync; + flush_pending = 0; + } + + if (timeout < 0 && pending_recvs > 0) { + if (!(pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI))) { + pollfd.revents = 0; + if (poll(&pollfd, (nfds_t)1, -1) < 0) + return -1; + } + } + sync: - if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI | POLLERR | POLLHUP | POLLNVAL)) - for (;;) - { - if (libcoopgamma_synchronise(&cg, asyncs, filters_n, &selected) < 0) - { - if (errno == 0) - continue; - else - goto fail; - } - if (crtc_updates[selected].synced) - continue; - crtc_updates[selected].synced = 1; - pending_recvs -= 1; - if (libcoopgamma_set_gamma_recv(&cg, asyncs + selected) < 0) - { - if (cg.error.server_side) - { - crtc_updates[selected].error = cg.error; - crtc_updates[selected].failed = 1; - memset(&(cg.error), 0, sizeof(cg.error)); - } - else - goto cg_fail; - } - } - - return pending_recvs == 0; - cg_fail: - return -2; - fail: - switch (errno) - { - case EINTR: - case EAGAIN: + if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI | POLLERR | POLLHUP | POLLNVAL)) { + for (;;) { + if (libcoopgamma_synchronise(&cg, asyncs, filters_n, &selected) < 0) { + if (!errno) + continue; + goto fail; + } + if (crtc_updates[selected].synced) + continue; + crtc_updates[selected].synced = 1; + pending_recvs -= 1; + if (libcoopgamma_set_gamma_recv(&cg, asyncs + selected) < 0) { + if (cg.error.server_side) { + crtc_updates[selected].error = cg.error; + crtc_updates[selected].failed = 1; + memset(&cg.error, 0, sizeof(cg.error)); + } else { + goto cg_fail; + } + } + } + } + + return !pending_recvs; +cg_fail: + return -2; +fail: + switch (errno) { + case EINTR: + case EAGAIN: #if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif - return pending_recvs == 0; - default: - return -1; - } + return !pending_recvs; + default: + return -1; + } } @@ -335,22 +329,23 @@ int synchronise(int timeout) * * @return Zero on success, -1 on error */ -static int initialise_proc(void) +static int +initialise_proc(void) { - sigset_t sigmask; - int sig; - - for (sig = 1; sig < _NSIG; sig++) - if (signal(sig, SIG_DFL) == SIG_ERR) - if (sig == SIGCHLD) - return -1; - - if (sigemptyset(&sigmask) < 0) - return -1; - if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0) - return -1; - - return 0; + sigset_t sigmask; + int sig; + + for (sig = 1; sig < _NSIG; sig++) + if (signal(sig, SIG_DFL) == SIG_ERR) + if (sig == SIGCHLD) + return -1; + + if (sigemptyset(&sigmask) < 0) + return -1; + if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0) + return -1; + + return 0; } @@ -360,21 +355,22 @@ static int initialise_proc(void) * * @return Zero on success, -1 on error */ -static int list_methods(void) +static int +list_methods(void) { - char** list; - size_t i; - - list = libcoopgamma_get_methods(); - if (list == NULL) - return -1; - for (i = 0; list[i]; i++) - printf("%s\n", list[i]); - free(list); - if (fflush(stdout) < 0) - return -1; - - return 0; + char **list; + size_t i; + + list = libcoopgamma_get_methods(); + if (!list) + return -1; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; } @@ -387,21 +383,22 @@ static int list_methods(void) * @return Zero on success, -1 on error, -2 * on libcoopgamma error */ -static int list_crtcs(void) +static int +list_crtcs(void) { - char** list; - size_t i; - - list = libcoopgamma_get_crtcs_sync(&cg); - if (list == NULL) - return -2; - for (i = 0; list[i]; i++) - printf("%s\n", list[i]); - free(list); - if (fflush(stdout) < 0) - return -1; - - return 0; + char **list; + size_t i; + + list = libcoopgamma_get_crtcs_sync(&cg); + if (!list) + return -2; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; } @@ -411,99 +408,97 @@ static int list_crtcs(void) * @return Zero on success, -1 on error, -2 * on libcoopgamma error */ -static int get_crtc_info(void) +static int +get_crtc_info(void) { - size_t i, unsynced = 0, selected; - char* synced; - int need_flush = 0; - struct pollfd pollfd; - - synced = alloca(crtcs_n * sizeof(*synced)); - memset(synced, 0, crtcs_n * sizeof(*synced)); - - i = 0; - pollfd.fd = cg.fd; - pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; - - while ((unsynced > 0) || (i < crtcs_n)) - { - wait: - if (i < crtcs_n) - pollfd.events |= POLLOUT; - else - pollfd.events &= ~POLLOUT; + size_t i, unsynced = 0, selected; + char *synced; + int need_flush = 0; + struct pollfd pollfd; + + synced = alloca(crtcs_n * sizeof(*synced)); + memset(synced, 0, crtcs_n * sizeof(*synced)); + + i = 0; + pollfd.fd = cg.fd; + pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; + + while (unsynced > 0 || i < crtcs_n) { + wait: + if (i < crtcs_n) + pollfd.events |= POLLOUT; + else + pollfd.events &= ~POLLOUT; - pollfd.revents = 0; - if (poll(&pollfd, (nfds_t)1, -1) < 0) - goto fail; + pollfd.revents = 0; + if (poll(&pollfd, (nfds_t)1, -1) < 0) + goto fail; - if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) - { - if (need_flush && (libcoopgamma_flush(&cg) < 0)) - goto send_fail; - need_flush = 0; - for (; i < crtcs_n; i++) - if (unsynced++, libcoopgamma_get_gamma_info_send(crtcs[i], &cg, asyncs + i) < 0) - goto send_fail; - goto send_done; - send_fail: - switch (errno) - { - case EINTR: - case EAGAIN: + if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) { + if (need_flush && (libcoopgamma_flush(&cg) < 0)) + goto send_fail; + need_flush = 0; + for (; i < crtcs_n; i++) + if (unsynced++, libcoopgamma_get_gamma_info_send(crtcs[i], &cg, asyncs + i) < 0) + goto send_fail; + goto send_done; + send_fail: + switch (errno) { + case EINTR: + case EAGAIN: #if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif - i++; - need_flush = 1; - break; - default: - goto fail; - } - } - send_done: - - if ((unsynced == 0) && (i == crtcs_n)) - break; - - if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) - while (unsynced > 0) - switch (libcoopgamma_synchronise(&cg, asyncs, i, &selected)) - { - case 0: - if (synced[selected]) - { - libcoopgamma_skip_message(&cg); - break; + i++; + need_flush = 1; + break; + default: + goto fail; + } } - synced[selected] = 1; - unsynced -= 1; - if (libcoopgamma_get_gamma_info_recv(crtc_info + selected, &cg, asyncs + selected) < 0) - goto cg_fail; - break; - case -1: - switch (errno) - { - case 0: - break; - case EINTR: - case EAGAIN: + send_done: + + if (!unsynced && i == crtcs_n) + break; + + if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { + while (unsynced > 0) { + switch (libcoopgamma_synchronise(&cg, asyncs, i, &selected)) { + case 0: + if (synced[selected]) { + libcoopgamma_skip_message(&cg); + break; + } + synced[selected] = 1; + unsynced -= 1; + if (libcoopgamma_get_gamma_info_recv(crtc_info + selected, &cg, asyncs + selected) < 0) + goto cg_fail; + break; + case -1: + switch (errno) + { + case 0: + break; + case EINTR: + case EAGAIN: #if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif - goto wait; - default: - goto fail; + goto wait; + default: + goto fail; + } + break; + } + } } - break; - } - } - - return 0; - fail: - return -1; - cg_fail: - return -2; + } + + return 0; +fail: + return -1; +cg_fail: + return -2; } @@ -539,372 +534,342 @@ static int get_crtc_info(void) * @param argv The command line arguments * @return 0 on success, 1 on error */ -int main(int argc, char* argv[]) +int +main(int argc, char *argv[]) { - int stage = 0; - int dealloc_crtcs = 0; - int rc = 0; - char* method = NULL; - char* site = NULL; - size_t crtc_i = 0; - int64_t priority = default_priority; - char* prio = NULL; - char* rule = NULL; - char* class = default_class; - char** classes = NULL; - size_t classes_n = 0; - int explicit_crtcs = 0; - int have_crtc_q = 0; - size_t i, filter_i; - - argv0 = *argv++, argc--; - - if (initialise_proc() < 0) - goto fail; - - crtcs = alloca(argc * sizeof(*crtcs)); - - for (; *argv; argv++, argc--) - { - char* args = *argv; - char opt[3]; - if (!strcmp(args, "--")) - { - argv++, argc--; - break; + int stage = 0; + int dealloc_crtcs = 0; + int rc = 0; + char *method = NULL; + char *site = NULL; + size_t crtc_i = 0; + int64_t priority = default_priority; + char *prio = NULL; + char *rule = NULL; + char *class = default_class; + char **classes = NULL; + size_t classes_n = 0; + int explicit_crtcs = 0; + int have_crtc_q = 0; + size_t i, filter_i; + const char *side, *crtc; + size_t len, n; + char *args, *arg, *end, *p, opt[3]; + int at_end; + + argv0 = *argv++, argc--; + + if (initialise_proc() < 0) + goto fail; + + crtcs = alloca((size_t)argc * sizeof(*crtcs)); + + for (; *argv; argv++, argc--) { + args = *argv; + if (!strcmp(args, "--")) { + argv++, argc--; + break; + } + opt[0] = *args++; + opt[2] = '\0'; + if ((*opt != '-') && (*opt != '+')) + break; + while (*args) { + opt[1] = *args++; + arg = args; + if ((at_end = !*arg)) + arg = argv[1]; + if (!strcmp(opt, "-M")) { + if (method || !(method = arg)) + usage(); + } else if (!strcmp(opt, "-S")) { + if (site || !(site = arg)) + usage(); + } else if (!strcmp(opt, "-c")) { + if (!arg) + usage(); + crtcs[crtc_i++] = arg; + explicit_crtcs = 1; + if (!have_crtc_q && !strcmp(arg, "?")) + have_crtc_q = 1; + } else if (!strcmp(opt, "-p")) { + if (prio || !(prio = arg)) + usage(); + } else if (!strcmp(opt, "-R")) { + if (rule || !(rule = arg)) + usage(); + } else { + switch (handle_opt(opt, arg)) { + case 0: + goto next_opt; + case 1: + break; + default: + goto fail; + } + } + argv += at_end; + argc -= at_end; + break; + next_opt:; + } } - opt[0] = *args++; - opt[2] = '\0'; - if ((*opt != '-') && (*opt != '+')) - break; - while (*args) - { - char* arg; - int at_end; - opt[1] = *args++; - arg = args; - if ((at_end = !*arg)) - arg = argv[1]; - if (!strcmp(opt, "-M")) - { - if ((method != NULL) || ((method = arg) == NULL)) - usage(); - } - else if (!strcmp(opt, "-S")) - { - if ((site != NULL) || ((site = arg) == NULL)) - usage(); - } - else if (!strcmp(opt, "-c")) - { - if (arg == NULL) - usage(); - crtcs[crtc_i++] = arg; - explicit_crtcs = 1; - if (!have_crtc_q && !strcmp(arg, "?")) - have_crtc_q = 1; - } - else if (!strcmp(opt, "-p")) - { - if ((prio != NULL) || ((prio = arg) == NULL)) - usage(); - } - else if (!strcmp(opt, "-R")) - { - if ((rule != NULL) || ((rule = arg) == NULL)) - usage(); - } - else - switch (handle_opt(opt, arg)) - { - case 0: - goto next_opt; - case 1: - break; - default: + + crtcs_n = crtc_i; + crtcs[crtc_i] = NULL; + if (!have_crtc_q && nulstrcmp(method, "?") && + nulstrcmp(rule, "?") && nulstrcmp(rule, "??") && + (default_priority == NO_DEFAULT_PRIORITY || nulstrcmp(prio, "?"))) + if (handle_args(argc, argv, prio) < 0) + goto fail; + + if (default_priority != NO_DEFAULT_PRIORITY) { + if (!nulstrcmp(prio, "?")) { + printf("%" PRIi64 "\n", priority); + return 0; + } else if (prio) { + errno = 0; + priority = (int64_t)strtoll(prio, &end, 10); + if (errno || *end || !*prio) + usage(); + } + } + + if (!nulstrcmp(rule, "??")) { + if (!*class_suffixes) + printf("%s\n", class); + else + for (i = 0; class_suffixes[i]; i++) + printf("%s%s\n", class, class_suffixes[i]); + return 0; + } else if (!nulstrcmp(rule, "?")) { + printf("%s\n", strstr(strstr(class, "::") + 2, "::") + 2); + return 0; + } else if (rule) { + p = strstr(strstr(class, "::") + 2, "::") + 2; + n = (size_t)(p - class); + class = alloca(strlen(rule) + n + (size_t)1); + memcpy(class, default_class, n); + strcpy(class + n, rule); + if (strchr(class, '\n')) { + fprintf(stderr, "%s: LF character is not allowed in the filter's class\n", argv0); + goto custom_fail; + } + } + + if (!nulstrcmp(method, "?")) { + if (list_methods() < 0) + goto fail; + return 0; + } + + if (libcoopgamma_context_initialise(&cg) < 0) goto fail; - } - argv += at_end; - argc -= at_end; - break; - next_opt:; + stage++; + if (libcoopgamma_connect(method, site, &cg) < 0) { + fprintf(stderr, "%s: server failed to initialise\n", argv0); + goto custom_fail; } - } - - crtcs_n = crtc_i; - crtcs[crtc_i] = NULL; - if (!have_crtc_q && nulstrcmp(method, "?") && - nulstrcmp(rule, "?") && nulstrcmp(rule, "??") && - ((default_priority == NO_DEFAULT_PRIORITY) || nulstrcmp(prio, "?"))) - if (handle_args(argc, argv, prio) < 0) - goto fail; - - if (default_priority != NO_DEFAULT_PRIORITY) - { - if (!nulstrcmp(prio, "?")) - { - printf("%" PRIi64 "\n", priority); - return 0; + stage++; + + if (have_crtc_q) { + switch (list_crtcs()) { + case 0: + goto done; + case -1: + goto fail; + default: + goto cg_fail; + } } - else if (prio != NULL) - { - char *end; - errno = 0; - priority = (int64_t)strtoll(prio, &end, 10); - if (errno || *end || !*prio) - usage(); + + if (!crtcs_n) { + crtcs = libcoopgamma_get_crtcs_sync(&cg); + if (!crtcs) + goto cg_fail; + dealloc_crtcs = 1; + for (; crtcs[crtcs_n]; crtcs_n++); } - } - - if (!nulstrcmp(rule, "??")) - { - size_t i; - if (*class_suffixes == NULL) - printf("%s\n", class); - else - for (i = 0; class_suffixes[i] != NULL; i++) - printf("%s%s\n", class, class_suffixes[i]); - return 0; - } - else if (!nulstrcmp(rule, "?")) - { - printf("%s\n", strstr(strstr(class, "::") + 2, "::") + 2); - return 0; - } - else if (rule != NULL) - { - char* p = strstr(strstr(class, "::") + 2, "::") + 2; - size_t n = (size_t)(p - class); - class = alloca(strlen(rule) + n + (size_t)1); - memcpy(class, default_class, n); - strcpy(class + n, rule); - if (strchr(class, '\n')) - { - fprintf(stderr, "%s: LF character is not allowed in the filter's class\n", argv0); - goto custom_fail; + + if (!crtcs_n) { + fprintf(stderr, "%s: no CRTC:s are available\n", argv0); + goto custom_fail; } - } - - if (!nulstrcmp(method, "?")) - { - if (list_methods() < 0) - goto fail; - return 0; - } - - if (libcoopgamma_context_initialise(&cg) < 0) - goto fail; - stage++; - if (libcoopgamma_connect(method, site, &cg) < 0) - { - fprintf(stderr, "%s: server failed to initialise\n", argv0); - goto custom_fail; - } - stage++; - - if (have_crtc_q) - switch (list_crtcs()) - { - case 0: - goto done; - case -1: - goto fail; - default: - goto cg_fail; - } - - if (crtcs_n == 0) - { - crtcs = libcoopgamma_get_crtcs_sync(&cg); - if (crtcs == NULL) - goto cg_fail; - dealloc_crtcs = 1; - for (; crtcs[crtcs_n] != NULL; crtcs_n++); - } - - if (crtcs_n == 0) - { - fprintf(stderr, "%s: no CRTC:s are available\n", argv0); - goto custom_fail; - } - - if (*class_suffixes == NULL) - { - classes = &class; - classes_n = 1; - } - else - { - size_t len = strlen(class); - while (class_suffixes[classes_n]) - classes_n++; - classes = alloca(classes_n * sizeof(*classes)); - for (i = 0; i < classes_n; i++) - { - classes[i] = alloca(len + strlen(class_suffixes[i]) + sizeof(":")); - stpcpy(stpcpy(stpcpy(classes[i], class), ":"), class_suffixes[i]); + + if (!*class_suffixes) { + classes = &class; + classes_n = 1; + } else { + len = strlen(class); + while (class_suffixes[classes_n]) + classes_n++; + classes = alloca(classes_n * sizeof(*classes)); + for (i = 0; i < classes_n; i++) { + classes[i] = alloca(len + strlen(class_suffixes[i]) + sizeof(":")); + stpcpy(stpcpy(stpcpy(classes[i], class), ":"), class_suffixes[i]); + } } - } - filters_n = classes_n * crtcs_n; - - crtc_info = alloca(crtcs_n * sizeof(*crtc_info)); - memset(crtc_info, 0, crtcs_n * sizeof(*crtc_info)); - for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) - if (libcoopgamma_crtc_info_initialise(crtc_info + crtc_i) < 0) - goto cg_fail; - - if (libcoopgamma_set_nonblocking(&cg, 1) < 0) - goto fail; - - asyncs = alloca(filters_n * sizeof(*asyncs)); - memset(asyncs, 0, filters_n * sizeof(*asyncs)); - for (filter_i = 0; filter_i < filters_n; filter_i++) - if (libcoopgamma_async_context_initialise(asyncs + filter_i) < 0) - goto fail; - - switch (get_crtc_info()) - { - case 0: - break; - case -1: - goto fail; - case -2: - goto cg_fail; - } - - for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) - { - if (explicit_crtcs && !(crtc_info[crtc_i].supported)) - fprintf(stderr, "%s: warning: gamma adjustments not supported on CRTC: %s\n", - argv0, crtcs[crtc_i]); - if (crtc_info[crtc_i].cooperative == 0) - fprintf(stderr, "%s: warning: cooperative gamma server not running for CRTC: %s\n", - argv0, crtcs[crtc_i]); - } - - crtc_updates = alloca(filters_n * sizeof(*crtc_updates)); - memset(crtc_updates, 0, filters_n * sizeof(*crtc_updates)); - for (filter_i = i = 0; i < classes_n; i++) - for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++, filter_i++) - { - if (libcoopgamma_filter_initialise(&(crtc_updates[filter_i].filter)) < 0) - goto fail; - if (libcoopgamma_error_initialise(&(crtc_updates[filter_i].error)) < 0) - goto fail; - crtc_updates[filter_i].crtc = crtc_i; - crtc_updates[filter_i].synced = 1; - crtc_updates[filter_i].failed = 0; - crtc_updates[filter_i].master = 1; - crtc_updates[filter_i].slaves = NULL; - crtc_updates[filter_i].filter.crtc = crtcs[crtc_i]; - crtc_updates[filter_i].filter.class = classes[i]; - crtc_updates[filter_i].filter.priority = priority; - crtc_updates[filter_i].filter.depth = crtc_info[crtc_i].depth; - crtc_updates[filter_i].filter.ramps.u8.red_size = crtc_info[crtc_i].red_size; - crtc_updates[filter_i].filter.ramps.u8.green_size = crtc_info[crtc_i].green_size; - crtc_updates[filter_i].filter.ramps.u8.blue_size = crtc_info[crtc_i].blue_size; - switch (crtc_updates[filter_i].filter.depth) - { + filters_n = classes_n * crtcs_n; + + crtc_info = alloca(crtcs_n * sizeof(*crtc_info)); + memset(crtc_info, 0, crtcs_n * sizeof(*crtc_info)); + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) + if (libcoopgamma_crtc_info_initialise(crtc_info + crtc_i) < 0) + goto cg_fail; + + if (libcoopgamma_set_nonblocking(&cg, 1) < 0) + goto fail; + + asyncs = alloca(filters_n * sizeof(*asyncs)); + memset(asyncs, 0, filters_n * sizeof(*asyncs)); + for (filter_i = 0; filter_i < filters_n; filter_i++) + if (libcoopgamma_async_context_initialise(asyncs + filter_i) < 0) + goto fail; + + switch (get_crtc_info()) { + case 0: + break; + case -1: + goto fail; + case -2: + goto cg_fail; + } + + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) { + if (explicit_crtcs && !crtc_info[crtc_i].supported) { + fprintf(stderr, "%s: warning: gamma adjustments not supported on CRTC: %s\n", + argv0, crtcs[crtc_i]); + } + if (!crtc_info[crtc_i].cooperative) { + fprintf(stderr, "%s: warning: cooperative gamma server not running for CRTC: %s\n", + argv0, crtcs[crtc_i]); + } + } + + crtc_updates = alloca(filters_n * sizeof(*crtc_updates)); + memset(crtc_updates, 0, filters_n * sizeof(*crtc_updates)); + for (filter_i = i = 0; i < classes_n; i++) { + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++, filter_i++) { + if (libcoopgamma_filter_initialise(&crtc_updates[filter_i].filter) < 0) + goto fail; + if (libcoopgamma_error_initialise(&crtc_updates[filter_i].error) < 0) + goto fail; + crtc_updates[filter_i].crtc = crtc_i; + crtc_updates[filter_i].synced = 1; + crtc_updates[filter_i].failed = 0; + crtc_updates[filter_i].master = 1; + crtc_updates[filter_i].slaves = NULL; + crtc_updates[filter_i].filter.crtc = crtcs[crtc_i]; + crtc_updates[filter_i].filter.class = classes[i]; + crtc_updates[filter_i].filter.priority = priority; + crtc_updates[filter_i].filter.depth = crtc_info[crtc_i].depth; + crtc_updates[filter_i].filter.ramps.u8.red_size = crtc_info[crtc_i].red_size; + crtc_updates[filter_i].filter.ramps.u8.green_size = crtc_info[crtc_i].green_size; + crtc_updates[filter_i].filter.ramps.u8.blue_size = crtc_info[crtc_i].blue_size; + switch (crtc_updates[filter_i].filter.depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - libcoopgamma_ramps_initialise(&(crtc_updates[filter_i].filter.ramps.MEMBER));\ - libclut_start_over(&(crtc_updates[filter_i].filter.ramps.MEMBER), MAX, TYPE, 1, 1, 1);\ - break; -LIST_DEPTHS + case CONST:\ + libcoopgamma_ramps_initialise(&crtc_updates[filter_i].filter.ramps.MEMBER);\ + libclut_start_over(&crtc_updates[filter_i].filter.ramps.MEMBER, MAX, TYPE, 1, 1, 1);\ + break; + LIST_DEPTHS #undef X - default: - fprintf(stderr, "%s: internal error: gamma ramp type is unrecognised: %i\n", - argv0, crtc_updates[filter_i].filter.depth); - goto custom_fail; - } - } - - switch (start()) - { - case 0: - break; - case -1: - goto fail; - case -2: - goto cg_fail; - case -3: - goto custom_fail; - } - - for (filter_i = 0; filter_i < filters_n; filter_i++) - if (crtc_updates[filter_i].failed) - { - const char* side = cg.error.server_side ? "server" : "client"; - const char* crtc = crtc_updates[filter_i].filter.crtc; - if (cg.error.custom) - { - if ((cg.error.number != 0) && (cg.error.description != NULL)) - fprintf(stderr, "%s: %s-side error number %" PRIu64 " for CRTC %s: %s\n", - argv0, side, cg.error.number, crtc, cg.error.description); - else if (cg.error.number != 0) - fprintf(stderr, "%s: %s-side error number %" PRIu64 " for CRTC %s\n", - argv0, side, cg.error.number, crtc); - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", argv0, side, crtc, cg.error.description); - } - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", argv0, side, crtc, cg.error.description); - else - fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", argv0, side, crtc, strerror(cg.error.number)); - } - - done: - if (dealloc_crtcs) - free(crtcs); - if (crtc_info != NULL) - for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) - libcoopgamma_crtc_info_destroy(crtc_info + crtc_i); - if (asyncs != NULL) - for (filter_i = 0; filter_i < filters_n; filter_i++) - libcoopgamma_async_context_destroy(asyncs + filter_i); - if (stage >= 1) - libcoopgamma_context_destroy(&cg, stage >= 2); - if (crtc_updates != NULL) - for (filter_i = 0; filter_i < filters_n; filter_i++) - { - if (crtc_updates[filter_i].master == 0) - memset(&(crtc_updates[filter_i].filter.ramps.u8), 0, sizeof(crtc_updates[filter_i].filter.ramps.u8)); - crtc_updates[filter_i].filter.crtc = NULL; - crtc_updates[filter_i].filter.class = NULL; - libcoopgamma_filter_destroy(&(crtc_updates[filter_i].filter)); - libcoopgamma_error_destroy(&(crtc_updates[filter_i].error)); - free(crtc_updates[filter_i].slaves); - } - return rc; - - custom_fail: - rc = 1; - goto done; - - fail: - rc = 1; - if (errno) - perror(argv0); - goto done; - - cg_fail: - rc = 1; - { - const char* side = cg.error.server_side ? "server" : "client"; - if (cg.error.custom) - { - if ((cg.error.number != 0) && (cg.error.description != NULL)) - fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n", - argv0, side, cg.error.number, cg.error.description); - else if (cg.error.number != 0) - fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number); - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); - } - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); - else - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror(cg.error.number)); - } - goto done; + default: + fprintf(stderr, "%s: internal error: gamma ramp type is unrecognised: %i\n", + argv0, crtc_updates[filter_i].filter.depth); + goto custom_fail; + } + } + } + + switch (start()) { + case 0: + break; + case -1: + goto fail; + case -2: + goto cg_fail; + case -3: + goto custom_fail; + } + + for (filter_i = 0; filter_i < filters_n; filter_i++) { + if (crtc_updates[filter_i].failed) { + side = cg.error.server_side ? "server" : "client"; + crtc = crtc_updates[filter_i].filter.crtc; + if (cg.error.custom) { + if (cg.error.number && cg.error.description) { + fprintf(stderr, "%s: %s-side error number %" PRIu64 " for CRTC %s: %s\n", + argv0, side, cg.error.number, crtc, cg.error.description); + } else if (cg.error.number) { + fprintf(stderr, "%s: %s-side error number %" PRIu64 " for CRTC %s\n", + argv0, side, cg.error.number, crtc); + } else if (cg.error.description) { + fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", + argv0, side, crtc, cg.error.description); + } + } else if (cg.error.description) { + fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", + argv0, side, crtc, cg.error.description); + } else { + fprintf(stderr, "%s: %s-side error for CRTC %s: %s\n", + argv0, side, crtc, strerror((int)cg.error.number)); + } + } + } + +done: + if (dealloc_crtcs) + free(crtcs); + if (crtc_info) + for (crtc_i = 0; crtc_i < crtcs_n; crtc_i++) + libcoopgamma_crtc_info_destroy(crtc_info + crtc_i); + if (asyncs) + for (filter_i = 0; filter_i < filters_n; filter_i++) + libcoopgamma_async_context_destroy(asyncs + filter_i); + if (stage >= 1) + libcoopgamma_context_destroy(&cg, stage >= 2); + if (crtc_updates) { + for (filter_i = 0; filter_i < filters_n; filter_i++) { + if (!crtc_updates[filter_i].master) { + memset(&crtc_updates[filter_i].filter.ramps.u8, 0, + sizeof(crtc_updates[filter_i].filter.ramps.u8)); + } + crtc_updates[filter_i].filter.crtc = NULL; + crtc_updates[filter_i].filter.class = NULL; + libcoopgamma_filter_destroy(&crtc_updates[filter_i].filter); + libcoopgamma_error_destroy(&crtc_updates[filter_i].error); + free(crtc_updates[filter_i].slaves); + } + } + return rc; + +custom_fail: + rc = 1; + goto done; + +fail: + rc = 1; + if (errno) + perror(argv0); + goto done; + +cg_fail: + rc = 1; + side = cg.error.server_side ? "server" : "client"; + if (cg.error.custom) { + if (cg.error.number && cg.error.description) { + fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n", + argv0, side, cg.error.number, cg.error.description); + } else if (cg.error.number) { + fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number); + } else if (cg.error.description) { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + } + } else if (cg.error.description) { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + } else { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror((int)cg.error.number)); + } + goto done; } @@ -9,7 +9,7 @@ * Value of `default_priority` that indicates * that there is no default priority */ -#define NO_DEFAULT_PRIORITY INT64_MAX +#define NO_DEFAULT_PRIORITY INT64_MAX @@ -24,12 +24,12 @@ * 4) The type of the ramp stops */ #define LIST_DEPTHS\ - X(LIBCOOPGAMMA_UINT8, u8, UINT8_MAX, uint8_t)\ - X(LIBCOOPGAMMA_UINT16, u16, UINT16_MAX, uint16_t)\ - X(LIBCOOPGAMMA_UINT32, u32, UINT32_MAX, uint32_t)\ - X(LIBCOOPGAMMA_UINT64, u64, UINT64_MAX, uint64_t)\ - X(LIBCOOPGAMMA_FLOAT, f, ((float)1), float)\ - X(LIBCOOPGAMMA_DOUBLE, d, ((double)1), double) + X(LIBCOOPGAMMA_UINT8, u8, UINT8_MAX, uint8_t)\ + X(LIBCOOPGAMMA_UINT16, u16, UINT16_MAX, uint16_t)\ + X(LIBCOOPGAMMA_UINT32, u32, UINT32_MAX, uint32_t)\ + X(LIBCOOPGAMMA_UINT64, u64, UINT64_MAX, uint64_t)\ + X(LIBCOOPGAMMA_FLOAT, f, ((float)1), float)\ + X(LIBCOOPGAMMA_DOUBLE, d, ((double)1), double) @@ -39,54 +39,54 @@ */ typedef struct filter_update { - /** - * The filter to update - * - * `.filter.crtc`, `.filter.class`, and - * `.filter.priority` (unless `default_priority` - * is `NO_DEFAULT_PRIORITY`), `.filter.depth` - * are preconfigured, and `.filter.ramps` - * is preinitialised and preset to an - * identity ramp - */ - libcoopgamma_filter_t filter; - - /** - * The index of the CRTC - */ - size_t crtc; - - /** - * Has the update been synchronised? - */ - int synced; - - /** - * Did the update fail? - */ - int failed; - - /** - * Error description if `.failed` is true - */ - libcoopgamma_error_t error; - - /** - * If zero, the ramps in `.filter` shall - * neither be modified nor freed - */ - int master; - - /** - * 0-terminated list of elements in - * `.crtc_updates` which shares gamma - * ramps with this instance - * - * This will only be set if `.master` - * is true - */ - size_t* slaves; - + /** + * The filter to update + * + * `.filter.crtc`, `.filter.class`, and + * `.filter.priority` (unless `default_priority` + * is `NO_DEFAULT_PRIORITY`), `.filter.depth` + * are preconfigured, and `.filter.ramps` + * is preinitialised and preset to an + * identity ramp + */ + libcoopgamma_filter_t filter; + + /** + * The index of the CRTC + */ + size_t crtc; + + /** + * Has the update been synchronised? + */ + int synced; + + /** + * Did the update fail? + */ + int failed; + + /** + * Error description if `.failed` is true + */ + libcoopgamma_error_t error; + + /** + * If zero, the ramps in `.filter` shall + * neither be modified nor freed + */ + int master; + + /** + * 0-terminated list of elements in + * `.crtc_updates` which shares gamma + * ramps with this instance + * + * This will only be set if `.master` + * is true + */ + size_t *slaves; + } filter_update_t; @@ -94,7 +94,7 @@ typedef struct filter_update /** * The process's name */ -extern const char* argv0; +extern const char *argv0; /** * The libcoopgamma context @@ -104,18 +104,18 @@ extern libcoopgamma_context_t cg; /** * The names of the selected CRTC:s */ -extern char** crtcs; +extern char **crtcs; /** * Gamma ramp updates for each CRTC */ -extern filter_update_t* crtc_updates; +extern filter_update_t *crtc_updates; /** * CRTC and monitor information about * each selected CRTC and connect monitor */ -extern libcoopgamma_crtc_info_t* crtc_info; +extern libcoopgamma_crtc_info_t *crtc_info; /** * The number of selected CRTC:s @@ -142,7 +142,7 @@ extern char default_class[]; /** * Class suffixes */ -extern const char* const* class_suffixes; +extern const char *const *class_suffixes; @@ -210,7 +210,7 @@ extern void usage(void); #if defined(__GNUC__) __attribute__((__nonnull__(1))) #endif -extern int handle_opt(char* opt, char* arg); +extern int handle_opt(char *opt, char *arg); /** * This function is called after the last @@ -224,7 +224,7 @@ extern int handle_opt(char* opt, char* arg); #if defined(__GNUC__) __attribute__((__nonnull__(2))) #endif -extern int handle_args(int argc, char* argv[], char* prio); +extern int handle_args(int argc, char *argv[], char *prio); /** * The main function for the program-specific code diff --git a/cg-brilliance.c b/cg-brilliance.c index 8ed3124..00ae873 100644 --- a/cg-brilliance.c +++ b/cg-brilliance.c @@ -24,7 +24,7 @@ char default_class[] = PKGNAME "::cg-brilliance::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; @@ -58,13 +58,14 @@ static double bvalue = 0; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " - "(-x | [-p priority] [-d] (all | red green blue))\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " + "(-x | [-p priority] [-d] (all | red green blue))\n", + argv0); + exit(1); } @@ -82,28 +83,29 @@ void usage(void) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - default: - usage(); - } - else - usage(); - return 0; - (void) arg; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + default: + usage(); + } + } else { + usage(); + } + return 0; + (void) arg; } @@ -114,16 +116,17 @@ int handle_opt(char* opt, char* arg) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_double(double* restrict out, const char* restrict str) +static int +parse_double(double *restrict out, const char *restrict str) { - char* end; - errno = 0; - *out = strtod(str, &end); - if (errno || (*out < 0) || isinf(*out) || isnan(*out) || *end) - return -1; - if (!*str || !strchr("0123456789.", *str)) - return -1; - return 0; + char *end; + errno = 0; + *out = strtod(str, &end); + if (errno || *out < 0 || isinf(*out) || isnan(*out) || *end) + return -1; + if (!*str || !strchr("0123456789.", *str)) + return -1; + return 0; } @@ -136,34 +139,33 @@ static int parse_double(double* restrict out, const char* restrict str) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - char* red = NULL; - char* green = NULL; - char* blue = NULL; - int q = xflag + dflag; - if ((q > 1) || (xflag && (prio != NULL || argc))) - usage(); - if (argc == 1) - red = green = blue = argv[0]; - else if (argc == 3) - { - red = argv[0]; - green = argv[1]; - blue = argv[2]; - } - else if (argc || !xflag) - usage(); - if (argc) - { - if (parse_double(&rvalue, red) < 0) - usage(); - if (parse_double(&gvalue, blue) < 0) - usage(); - if (parse_double(&bvalue, green) < 0) - usage(); - } - return 0; + char *red = NULL; + char *green = NULL; + char *blue = NULL; + int q = xflag + dflag; + if (q > 1 || (xflag && (prio || argc))) + usage(); + if (argc == 1) { + red = green = blue = argv[0]; + } else if (argc == 3) { + red = argv[0]; + green = argv[1]; + blue = argv[2]; + } else if (argc || !xflag) { + usage(); + } + if (argc) { + if (parse_double(&rvalue, red) < 0) + usage(); + if (parse_double(&gvalue, blue) < 0) + usage(); + if (parse_double(&bvalue, green) < 0) + usage(); + } + return 0; } @@ -172,54 +174,45 @@ int handle_args(int argc, char* argv[], char* prio) * * @param filter The filter to fill */ -static void fill_filter(libcoopgamma_filter_t* restrict filter) +static void +fill_filter(libcoopgamma_filter_t *restrict filter) { - size_t i; - switch (filter->depth) - { -#define X(CONST, MAX, TYPE, MEMBER) \ - case CONST: \ - for (i = 0; i < filter->ramps.MEMBER.red_size; i++) \ - { \ - double val = (double)(filter->ramps.MEMBER.red[i]); \ - val *= rvalue; \ - if (val < 0) filter->ramps.MEMBER.red[i] = 0; \ - else if (val > (double)(MAX)) filter->ramps.MEMBER.red[i] = MAX; \ - else filter->ramps.MEMBER.red[i] = (TYPE)val; \ - } \ - for (i = 0; i < filter->ramps.MEMBER.green_size; i++) \ - { \ - double val = (double)(filter->ramps.MEMBER.green[i]); \ - val *= gvalue; \ - if (val < 0) filter->ramps.MEMBER.green[i] = 0; \ - else if (val > (double)(MAX)) filter->ramps.MEMBER.green[i] = MAX; \ - else filter->ramps.MEMBER.green[i] = (TYPE)val; \ - } \ - for (i = 0; i < filter->ramps.MEMBER.blue_size; i++) \ - { \ - double val = (double)(filter->ramps.MEMBER.blue[i]); \ - val *= bvalue; \ - if (val < 0) filter->ramps.MEMBER.blue[i] = 0; \ - else if (val > (double)(MAX)) filter->ramps.MEMBER.blue[i] = MAX; \ - else filter->ramps.MEMBER.blue[i] = (TYPE)val; \ - } \ - break - X(LIBCOOPGAMMA_UINT8, UINT8_MAX, uint8_t, u8); - X(LIBCOOPGAMMA_UINT16, UINT16_MAX, uint16_t, u16); - X(LIBCOOPGAMMA_UINT32, UINT32_MAX, uint32_t, u32); - X(LIBCOOPGAMMA_UINT64, UINT64_MAX, uint64_t, u64); + size_t i; + switch (filter->depth) { +#define X(CONST, MAX, TYPE, MEMBER)\ + case CONST:\ + for (i = 0; i < filter->ramps.MEMBER.red_size; i++) {\ + double val = (double)(filter->ramps.MEMBER.red[i]);\ + val *= rvalue;\ + filter->ramps.MEMBER.red[i] = val < 0 ? 0 : val > (double)(MAX) ? MAX : (TYPE)val;\ + }\ + for (i = 0; i < filter->ramps.MEMBER.green_size; i++) {\ + double val = (double)(filter->ramps.MEMBER.green[i]);\ + val *= gvalue;\ + filter->ramps.MEMBER.green[i] = val < 0 ? 0 : val > (double)(MAX) ? MAX : (TYPE)val;\ + }\ + for (i = 0; i < filter->ramps.MEMBER.blue_size; i++) {\ + double val = (double)(filter->ramps.MEMBER.blue[i]);\ + val *= bvalue;\ + filter->ramps.MEMBER.blue[i] = val < 0 ? 0 : val > (double)(MAX) ? MAX : (TYPE)val;\ + }\ + break + X(LIBCOOPGAMMA_UINT8, UINT8_MAX, uint8_t, u8); + X(LIBCOOPGAMMA_UINT16, UINT16_MAX, uint16_t, u16); + X(LIBCOOPGAMMA_UINT32, UINT32_MAX, uint32_t, u32); + X(LIBCOOPGAMMA_UINT64, UINT64_MAX, uint64_t, u64); #undef X - case LIBCOOPGAMMA_FLOAT: - libclut_rgb_brightness(&(filter->ramps.f), (float)1, float, rvalue, gvalue, bvalue); - libclut_clip(&(filter->ramps.f), (float)1, float, 1, 1, 1); - break; - case LIBCOOPGAMMA_DOUBLE: - libclut_rgb_brightness(&(filter->ramps.d), (double)1, double, rvalue, gvalue, bvalue); - libclut_clip(&(filter->ramps.d), (double)1, double, 1, 1, 1); - break; - default: - abort(); - } + case LIBCOOPGAMMA_FLOAT: + libclut_rgb_brightness(&filter->ramps.f, (float)1, float, rvalue, gvalue, bvalue); + libclut_clip(&filter->ramps.f, (float)1, float, 1, 1, 1); + break; + case LIBCOOPGAMMA_DOUBLE: + libclut_rgb_brightness(&filter->ramps.d, (double)1, double, rvalue, gvalue, bvalue); + libclut_clip(&filter->ramps.d, (double)1, double, 1, 1, 1); + break; + default: + abort(); + } } @@ -231,66 +224,65 @@ static void fill_filter(libcoopgamma_filter_t* restrict filter) * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - - if (xflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - if (!xflag) - if ((r = make_slaves()) < 0) - return r; - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - if (!xflag) - fill_filter(&(crtc_updates[i].filter)); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - if (!dflag) - return 0; - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return -1; - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return -1; + int r; + size_t i, j; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + if (!xflag && (r = make_slaves()) < 0) + return r; + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + if (!xflag) + fill_filter(&crtc_updates[i].filter); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return -1; + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + if (!dflag) + return 0; + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return -1; + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return -1; + } + } + } + +enotrecoverable: + pause(); + return -1; } diff --git a/cg-darkroom.c b/cg-darkroom.c index b9b79f1..8b312c0 100644 --- a/cg-darkroom.c +++ b/cg-darkroom.c @@ -24,7 +24,7 @@ char default_class[] = PKGNAME "::cg-darkroom::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; @@ -48,13 +48,14 @@ static double value = 0.25; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " - "(-x | [-p priority] [-d] [brightness])\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " + "(-x | [-p priority] [-d] [brightness])\n", + argv0); + exit(1); } @@ -72,28 +73,29 @@ void usage(void) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - default: - usage(); - } - else - usage(); - return 0; - (void) arg; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + default: + usage(); + } + } else { + usage(); + } + return 0; + (void) arg; } @@ -104,16 +106,17 @@ int handle_opt(char* opt, char* arg) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_double(double* restrict out, const char* restrict str) +static int +parse_double(double *restrict out, const char *restrict str) { - char* end; - errno = 0; - *out = strtod(str, &end); - if (errno || (*out < 0) || isinf(*out) || isnan(*out) || *end) - return -1; - if (!*str || !strchr("0123456789.", *str)) - return -1; - return 0; + char *end; + errno = 0; + *out = strtod(str, &end); + if (errno || *out < 0 || isinf(*out) || isnan(*out) || *end) + return -1; + if (!*str || !strchr("0123456789.", *str)) + return -1; + return 0; } @@ -126,19 +129,19 @@ static int parse_double(double* restrict out, const char* restrict str) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - int q = xflag + dflag; - if ((q > 1) || (xflag && (prio != NULL || argc))) - usage(); - if (argc == 1) - { - if (parse_double(&value, argv[0]) < 0) - usage(); - } - else if (argc) - usage(); - return 0; + int q = xflag + dflag; + if ((q > 1) || (xflag && (prio || argc))) + usage(); + if (argc == 1) { + if (parse_double(&value, argv[0]) < 0) + usage(); + } else if (argc) { + usage(); + } + return 0; } @@ -148,80 +151,78 @@ int handle_args(int argc, char* argv[], char* prio) * @param filter The filter to fill * @return Zero on success, -1 on error */ -static int fill_filter(libcoopgamma_filter_t* restrict filter) +static int +fill_filter(libcoopgamma_filter_t *restrict filter) { - union libcoopgamma_ramps dramps; - size_t size; - - if ((0 <= value) && (value <= 1)) - { - switch (filter->depth) - { + union libcoopgamma_ramps dramps; + size_t size; + + if (0 <= value && value <= 1) { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - libclut_negative(&(filter->ramps.MEMBER), MAX, TYPE, 1, 0, 0);\ - libclut_rgb_brightness(&(filter->ramps.MEMBER), MAX, TYPE, 1, 0, 0);\ - libclut_cie_brightness(&(filter->ramps.MEMBER), MAX, TYPE, value, value, value);\ - break; -LIST_DEPTHS + case CONST:\ + libclut_negative(&filter->ramps.MEMBER, MAX, TYPE, 1, 0, 0);\ + libclut_rgb_brightness(&filter->ramps.MEMBER, MAX, TYPE, 1, 0, 0);\ + libclut_cie_brightness(&filter->ramps.MEMBER, MAX, TYPE, value, value, value);\ + break; + LIST_DEPTHS #undef X + default: + abort(); + } + return 0; + } + if (filter->depth == LIBCOOPGAMMA_DOUBLE) { + libclut_negative(&filter->ramps.d, (double)1, double, 1, 0, 0); + libclut_rgb_brightness(&filter->ramps.d, (double)1, double, 1, 0, 0); + libclut_cie_brightness(&filter->ramps.d, (double)1, double, value, value, value); + libclut_clip(&filter->ramps.d, (double)1, double, 1, 0, 0); + return 0; + } + if (filter->depth == LIBCOOPGAMMA_FLOAT) { + libclut_negative(&filter->ramps.f, (float)1, float, 1, 0, 0); + libclut_rgb_brightness(&filter->ramps.f, (float)1, float, 1, 0, 0); + libclut_cie_brightness(&filter->ramps.f, (float)1, float, value, value, value); + libclut_clip(&filter->ramps.f, (float)1, float, 1, 0, 0); + return 0; + } + + size = dramps.d.red_size = filter->ramps.d.red_size; + size += dramps.d.green_size = filter->ramps.d.green_size; + size += dramps.d.blue_size = filter->ramps.d.blue_size; + dramps.d.red = calloc(size, sizeof(double)); + if (!dramps.d.red) + return -1; + dramps.d.green = dramps.d.red + dramps.d.red_size; + dramps.d.blue = dramps.d.green + dramps.d.green_size; + + libclut_start_over(&dramps.d, (double)1, double, 1, 0, 0); + libclut_negative(&dramps.d, (double)1, double, 1, 0, 0); + libclut_rgb_brightness(&dramps.d, (double)1, double, 1, 0, 0); + libclut_cie_brightness(&dramps.d, (double)1, double, value, value, value); + libclut_clip(&dramps.d, (double)1, double, 1, 0, 0); + + switch (filter->depth) { + case LIBCOOPGAMMA_UINT8: + libclut_translate(&filter->ramps.u8, UINT8_MAX, uint8_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_UINT16: + libclut_translate(&filter->ramps.u16, UINT16_MAX, uint16_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_UINT32: + libclut_translate(&filter->ramps.u32, UINT32_MAX, uint32_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_UINT64: + libclut_translate(&filter->ramps.u64, UINT64_MAX, uint64_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_FLOAT: + case LIBCOOPGAMMA_DOUBLE: default: - abort(); + abort(); } - return 0; - } - if (filter->depth == LIBCOOPGAMMA_DOUBLE) - { - libclut_negative(&(filter->ramps.d), (double)1, double, 1, 0, 0); - libclut_rgb_brightness(&(filter->ramps.d), (double)1, double, 1, 0, 0); - libclut_cie_brightness(&(filter->ramps.d), (double)1, double, value, value, value); - libclut_clip(&(filter->ramps.d), (double)1, double, 1, 0, 0); - return 0; - } - if (filter->depth == LIBCOOPGAMMA_FLOAT) - { - libclut_negative(&(filter->ramps.f), (float)1, float, 1, 0, 0); - libclut_rgb_brightness(&(filter->ramps.f), (float)1, float, 1, 0, 0); - libclut_cie_brightness(&(filter->ramps.f), (float)1, float, value, value, value); - libclut_clip(&(filter->ramps.f), (float)1, float, 1, 0, 0); - return 0; - } - - size = dramps.d.red_size = filter->ramps.d.red_size; - size += dramps.d.green_size = filter->ramps.d.green_size; - size += dramps.d.blue_size = filter->ramps.d.blue_size; - dramps.d.red = calloc(size, sizeof(double)); - if (dramps.d.red == NULL) - return -1; - dramps.d.green = dramps.d.red + dramps.d.red_size; - dramps.d.blue = dramps.d.green + dramps.d.green_size; - - libclut_start_over(&(dramps.d), (double)1, double, 1, 0, 0); - libclut_negative(&(dramps.d), (double)1, double, 1, 0, 0); - libclut_rgb_brightness(&(dramps.d), (double)1, double, 1, 0, 0); - libclut_cie_brightness(&(dramps.d), (double)1, double, value, value, value); - libclut_clip(&(dramps.d), (double)1, double, 1, 0, 0); - - switch (filter->depth) - { - case LIBCOOPGAMMA_UINT8: - libclut_translate(&(filter->ramps.u8), UINT8_MAX, uint8_t, &(dramps.d), (double)1, double); - break; - case LIBCOOPGAMMA_UINT16: - libclut_translate(&(filter->ramps.u16), UINT16_MAX, uint16_t, &(dramps.d), (double)1, double); - break; - case LIBCOOPGAMMA_UINT32: - libclut_translate(&(filter->ramps.u32), UINT32_MAX, uint32_t, &(dramps.d), (double)1, double); - break; - case LIBCOOPGAMMA_UINT64: - libclut_translate(&(filter->ramps.u64), UINT64_MAX, uint64_t, &(dramps.d), (double)1, double); - break; - default: - abort(); - } - - free(dramps.d.red); - return 0; + + free(dramps.d.red); + return 0; } @@ -233,67 +234,65 @@ LIST_DEPTHS * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - - if (xflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - if (!xflag) - if ((r = make_slaves()) < 0) - return r; - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - if (!xflag) - if ((r = fill_filter(&(crtc_updates[i].filter))) < 0) - return r; - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - if (!dflag) - return 0; - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return -1; - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return -1; + int r; + size_t i, j; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + if (!xflag && (r = make_slaves()) < 0) + return r; + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + if (!xflag && (r = fill_filter(&crtc_updates[i].filter)) < 0) + return r; + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return -1; + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + if (!dflag) + return 0; + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return -1; + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return -1; + } + } + } + +enotrecoverable: + pause(); + return -1; } @@ -27,7 +27,7 @@ char default_class[] = PKGNAME "::cg-gamma::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; @@ -44,7 +44,7 @@ static int xflag = 0; /** * -f: gamma listing file */ -static char* fflag = NULL; +static char *fflag = NULL; /** * The gamma of the red channel @@ -65,38 +65,39 @@ static double bgamma = 1; * `NULL`-terminated list of output * names listed in the configuration file */ -static char** names = NULL; +static char **names = NULL; /** * The gamma of the red channel on monitor * with same index in `names` */ -static double* rgammas = NULL; +static double *rgammas = NULL; /** * The gamma of the green channel on monitor * with same index in `names` */ -static double* ggammas = NULL; +static double *ggammas = NULL; /** * The gamma of the blue channel on monitor * with same index in `names` */ -static double* bgammas = NULL; +static double *bgammas = NULL; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " - "(-x | [-p priority] [-d] [-f file | all | red green blue])\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " + "(-x | [-p priority] [-d] [-f file | all | red green blue])\n", + argv0); + exit(1); } @@ -106,21 +107,21 @@ void usage(void) * @param ret The value to return * @return `ret` is returned as is */ -static int cleanup(int ret) +static int +cleanup(int ret) { - int saved_errno = errno; - if (names != NULL) - { - char** p = names; - while (*p) - free(*p++); - } - free(names); - free(rgammas); - free(ggammas); - free(bgammas); - errno = saved_errno; - return ret; + int saved_errno = errno; + if (names) { + char **p = names; + while (*p) + free(*p++); + } + free(names); + free(rgammas); + free(ggammas); + free(bgammas); + errno = saved_errno; + return ret; } @@ -138,29 +139,30 @@ static int cleanup(int ret) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - case 'f': - if (fflag || !(fflag = arg)) - usage(); - return 1; - default: - usage(); - } - return 0; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + case 'f': + if (fflag || !(fflag = arg)) + usage(); + return 1; + default: + usage(); + } + } + return 0; } @@ -170,25 +172,26 @@ int handle_opt(char* opt, char* arg) * @param confname The filename (excluding directory) of the configuration file * @return The full pathname of the configuration file, `NULL` on error */ -static char* get_conf_file(const char* restrict confname) +static char * +get_conf_file(const char *restrict confname) { - struct passwd* pw; - char* path; - - pw = getpwuid(getuid()); - if ((pw == NULL) || (pw->pw_dir == NULL)) - return NULL; - - path = malloc(strlen(pw->pw_dir) + strlen(confname) + sizeof("/.config/")); - if (path == NULL) - return NULL; - - sprintf(path, "%s/.config/%s", pw->pw_dir, confname); - - if (access(path, F_OK) < 0) - sprintf(path, "/etc/%s", confname); - - return path; + struct passwd *pw; + char *path; + + pw = getpwuid(getuid()); + if (!pw || !pw->pw_dir) + return NULL; + + path = malloc(strlen(pw->pw_dir) + strlen(confname) + sizeof("/.config/")); + if (!path) + return NULL; + + sprintf(path, "%s/.config/%s", pw->pw_dir, confname); + + if (access(path, F_OK) < 0) + sprintf(path, "/etc/%s", confname); + + return path; } @@ -199,16 +202,17 @@ static char* get_conf_file(const char* restrict confname) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_double(double* restrict out, const char* restrict str) +static int +parse_double(double *restrict out, const char *restrict str) { - char* end; - errno = 0; - *out = strtod(str, &end); - if (errno || (*out < 0) || isinf(*out) || isnan(*out) || *end) - return -1; - if (!*str || !strchr("0123456789.", *str)) - return -1; - return 0; + char *end; + errno = 0; + *out = strtod(str, &end); + if (errno || *out < 0 || isinf(*out) || isnan(*out) || *end) + return -1; + if (!*str || !strchr("0123456789.", *str)) + return -1; + return 0; } @@ -218,142 +222,133 @@ static int parse_double(double* restrict out, const char* restrict str) * @param pathname The pathname of the file * @return Zero on success, -1 on error */ -static int parse_gamma_file(const char* restrict pathname) +static int +parse_gamma_file(const char *restrict pathname) { - int fd, saved_errno; - char* line = NULL; - size_t size = 0, lineno = 0, ptr = 0, alloc = 0; - ssize_t n; - FILE* f = NULL; - char* p; - char* q; - char* r; - char* g; - char* b; - - fd = open(pathname, O_RDONLY); - if (fd == -1) - return -1; - - f = fdopen(fd, "rb"); - if (f == NULL) - goto fail; - - while (n = getline(&line, &size, f), n >= 0) - { - lineno += 1; - - if ((n > 0) && (line[n - 1] == '\n')) - line[n - 1] = '\0'; - p = line; - while ((*p == ' ') || (*p == '\t')) p++; - if ((!*p) || (*p == '#')) - continue; - - r = strpbrk(line, " \t"); - if (r == NULL) - goto bad; - while (r[1] == ' ' || r[1] == '\t') r++; - g = strpbrk(r + 1, " \t"); - if (g == NULL) - goto bad; - while (g[1] == ' ' || g[1] == '\t') g++; - b = strpbrk(g + 1, " \t"); - if (b == NULL) - goto bad; - while (b[1] == ' ' || b[1] == '\t') b++; - - for (;;) - { - q = strpbrk(b + 1, " \t"); - if (q == NULL) - break; - while (q[1] == ' ' || q[1] == '\t') q++; - if (!*q) - break; - r = g, g = b, b = q; + int fd, saved_errno; + char *line = NULL; + size_t size = 0, lineno = 0, ptr = 0, alloc = 0; + ssize_t n; + FILE *f = NULL; + char *p, *q; + char *r, *g, *b; + void *new; + size_t new_size; + + fd = open(pathname, O_RDONLY); + if (fd < 0) + return -1; + + f = fdopen(fd, "rb"); + if (!f) + goto fail; + + while ((n = getline(&line, &size, f)) >= 0) { + lineno += 1; + + if (n > 0 && line[n - 1] == '\n') + line[n - 1] = '\0'; + for (p = line; *p == ' ' || *p == '\t'; p++); + if (!*p || *p == '#') + continue; + + r = strpbrk(line, " \t"); + if (!r) + goto bad; + for (; r[1] == ' ' || r[1] == '\t'; r++); + g = strpbrk(r + 1, " \t"); + if (!g) + goto bad; + for (; g[1] == ' ' || g[1] == '\t'; g++); + b = strpbrk(g + 1, " \t"); + if (!b) + goto bad; + for (; b[1] == ' ' || b[1] == '\t'; b++); + + for (;;) { + q = strpbrk(b + 1, " \t"); + if (!q) + break; + for (; q[1] == ' ' || q[1] == '\t'; q++); + if (!*q) + break; + r = g, g = b, b = q; + } + + *r++ = '\0'; + *g++ = '\0'; + *b++ = '\0'; + + if ((q = strpbrk(r, " \t"))) + *q = '\0'; + if ((q = strpbrk(g, " \t"))) + *q = '\0'; + if ((q = strpbrk(b, " \t"))) + *q = '\0'; + + q = strchr(p, '\0'); + while ((q != p) && ((q[-1] == ' ') || (q[-1] == '\t'))) + q--; + *q = '\0'; + + if (ptr == alloc) { + new_size = alloc ? (alloc << 1) : 4; + + new = realloc(rgammas, new_size * sizeof(*rgammas)); + if (!new) + goto fail; + rgammas = new; + + new = realloc(ggammas, new_size * sizeof(*ggammas)); + if (!new) + goto fail; + ggammas = new; + + new = realloc(bgammas, new_size * sizeof(*bgammas)); + if (!new) + goto fail; + bgammas = new; + + new = realloc(names, (new_size + 1) * sizeof(*names)); + if (!new) + goto fail; + names = new; + memset(names + alloc, 0, (new_size + 1 - alloc) * sizeof(*names)); + + alloc = new_size; + } + + if (parse_double(rgammas + ptr, r) < 0 || + parse_double(ggammas + ptr, g) < 0 || + parse_double(bgammas + ptr, b) < 0) + goto bad; + names[ptr] = malloc(strlen(p) + 1); + if (!names[ptr]) + goto fail; + strcpy(names[ptr], p); + ptr++; + + continue; + bad: + fprintf(stderr, "%s: ignoring malformatted line in %s: %zu\n", argv0, pathname, lineno); } - - *r++ = '\0'; - *g++ = '\0'; - *b++ = '\0'; - - q = strpbrk(r, " \t"); - if (q != NULL) - *q = '\0'; - q = strpbrk(g, " \t"); - if (q != NULL) - *q = '\0'; - q = strpbrk(b, " \t"); - if (q != NULL) - *q = '\0'; - - q = strchr(p, '\0'); - while ((q != p) && ((q[-1] == ' ') || (q[-1] == '\t'))) - q--; - *q = '\0'; - - if (ptr == alloc) - { - void* new; - size_t new_size = alloc ? (alloc << 1) : 4; - - new = realloc(rgammas, new_size * sizeof(*rgammas)); - if (new == NULL) - goto fail; - rgammas = new; - - new = realloc(ggammas, new_size * sizeof(*ggammas)); - if (new == NULL) - goto fail; - ggammas = new; - - new = realloc(bgammas, new_size * sizeof(*bgammas)); - if (new == NULL) - goto fail; - bgammas = new; - - new = realloc(names, (new_size + 1) * sizeof(*names)); - if (new == NULL) - goto fail; - names = new; - memset(names + alloc, 0, (new_size + 1 - alloc) * sizeof(*names)); - - alloc = new_size; + + if (fclose(f) < 0) { + f = NULL; + goto fail; } - - if ((parse_double(rgammas + ptr, r) < 0) || - (parse_double(ggammas + ptr, g) < 0) || - (parse_double(bgammas + ptr, b) < 0)) - goto bad; - names[ptr] = malloc(strlen(p) + 1); - if (names[ptr] == NULL) - goto fail; - strcpy(names[ptr], p); - ptr++; - - continue; - bad: - fprintf(stderr, "%s: ignoring malformatted line in %s: %zu\n", argv0, pathname, lineno); - } - - if (fclose(f) < 0) - { - f = NULL; - goto fail; - } - close(fd); - free(line); - return 0; - fail: - saved_errno = errno; - free(line); - if (f != NULL) - fclose(f); - if (fd >= 0) - close(fd); - errno = saved_errno; - return -1; + close(fd); + free(line); + return 0; +fail: + saved_errno = errno; + free(line); + if (f) + fclose(f); + if (fd >= 0) + close(fd); + errno = saved_errno; + return -1; } @@ -366,48 +361,48 @@ static int parse_gamma_file(const char* restrict pathname) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - int free_fflag = 0, saved_errno; - int q = xflag + dflag; - if ((q > 1) || (fflag && argc) || (xflag && ((fflag != NULL) || (argc > 0) || (prio != NULL)))) - usage(); - if (argc == 1) - { - if (parse_double(&rgamma, argv[0]) < 0) - usage(); - bgamma = ggamma = rgamma; - } - else if (argc == 3) - { - if (parse_double(&rgamma, argv[0]) < 0) - usage(); - if (parse_double(&ggamma, argv[1]) < 0) - usage(); - if (parse_double(&bgamma, argv[2]) < 0) - usage(); - } - else if (argc) - usage(); - if (!argc && !fflag && !xflag) - { - fflag = get_conf_file("gamma"); - if (fflag == NULL) - return -1; - free_fflag = 1; - } - if (fflag) - if (parse_gamma_file(fflag) < 0) - goto fail; - if (free_fflag) - free(fflag), fflag = NULL; - return 0; - fail: - saved_errno = errno; - if (free_fflag) - free(fflag), fflag = NULL; - errno = saved_errno; - return cleanup(-1); + int free_fflag = 0, saved_errno; + int q = xflag + dflag; + if (q > 1 || (fflag && argc) || (xflag && (fflag || argc > 0 || prio))) + usage(); + if (argc == 1) { + if (parse_double(&rgamma, argv[0]) < 0) + usage(); + bgamma = ggamma = rgamma; + } else if (argc == 3) { + if (parse_double(&rgamma, argv[0]) < 0) + usage(); + if (parse_double(&ggamma, argv[1]) < 0) + usage(); + if (parse_double(&bgamma, argv[2]) < 0) + usage(); + } else if (argc) { + usage(); + } + if (!argc && !fflag && !xflag) { + fflag = get_conf_file("gamma"); + if (!fflag) + return -1; + free_fflag = 1; + } + if (fflag && parse_gamma_file(fflag) < 0) + goto fail; + if (free_fflag) { + free(fflag); + fflag = NULL; + } + return 0; +fail: + saved_errno = errno; + if (free_fflag) { + free(fflag); + fflag = NULL; + } + errno = saved_errno; + return cleanup(-1); } @@ -419,19 +414,19 @@ int handle_args(int argc, char* argv[], char* prio) * @param g The green gamma * @param b The blue gamma */ -static void fill_filter(libcoopgamma_filter_t* restrict filter, double r, double g, double b) +static void +fill_filter(libcoopgamma_filter_t *restrict filter, double r, double g, double b) { - switch (filter->depth) - { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - libclut_gamma(&(filter->ramps.MEMBER), MAX, TYPE, r, g, b);\ - break; -LIST_DEPTHS + case CONST:\ + libclut_gamma(&filter->ramps.MEMBER, MAX, TYPE, r, g, b);\ + break; + LIST_DEPTHS #undef X - default: - abort(); - } + default: + abort(); + } } @@ -443,82 +438,81 @@ LIST_DEPTHS * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - - if (xflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - if (!xflag && (names != NULL)) - if ((r = make_slaves()) < 0) - return cleanup(r); - - if (names == NULL) - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - if (!xflag) - fill_filter(&(crtc_updates[i].filter), rgamma, ggamma, bgamma); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return cleanup(r); - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return cleanup(r); - } - } - else - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_info[crtc_updates[i].crtc].supported)) - continue; - for (j = 0; names[j] != NULL; j++) - if (!strcasecmp(crtc_updates[i].filter.crtc, names[j])) - { - fill_filter(&(crtc_updates[i].filter), rgammas[j], ggammas[j], bgammas[j]); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) + int r; + size_t i, j; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + if (!xflag && names && (r = make_slaves()) < 0) return cleanup(r); - break; - } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return cleanup(r); - - if (!dflag) - return cleanup(0); - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return cleanup(-1); - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return cleanup(-1); + + if (!names) { + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + if (!xflag) + fill_filter(&crtc_updates[i].filter, rgamma, ggamma, bgamma); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + } + } + } + } else { + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_info[crtc_updates[i].crtc].supported) + continue; + for (j = 0; names[j]; j++) { + if (!strcasecmp(crtc_updates[i].filter.crtc, names[j])) { + fill_filter(&crtc_updates[i].filter, rgammas[j], ggammas[j], bgammas[j]); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + break; + } + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return cleanup(-1); + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return cleanup(r); + + if (!dflag) + return cleanup(0); + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return cleanup(-1); + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return cleanup(-1); + } + } + } + +enotrecoverable: + pause(); + return cleanup(-1); } @@ -22,18 +22,18 @@ /** * Magic number for dual-byte precision lookup table based profiles */ -#define MLUT_TAG 0x6D4C5554L +#define MLUT_TAG 0x6D4C5554L /** * Magic number for gamma–brightness–contrast based profiles * and for variable precision lookup table profiles */ -#define VCGT_TAG 0x76636774L +#define VCGT_TAG 0x76636774L /** * The filename of the configuration file */ -#define ICCTAB "icctab" +#define ICCTAB "icctab" @@ -50,7 +50,7 @@ char default_class[] = PKGNAME "::cg-icc::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; @@ -67,7 +67,7 @@ static int xflag = 0; /** * The panhame of the selected ICC profile */ -static const char* icc_pathname = NULL; +static const char *icc_pathname = NULL; /** * Gamma ramps loaded from `icc_pathname` @@ -82,13 +82,13 @@ static libcoopgamma_depth_t unidepth = 0; /** * Parsed ICC profiles for each CRTC */ -static libcoopgamma_ramps_t* rampses = NULL; +static libcoopgamma_ramps_t *rampses = NULL; /** * The datatype of the stops in the ramps of * corresponding element in `rampses` */ -static libcoopgamma_depth_t* depths = NULL; +static libcoopgamma_depth_t *depths = NULL; /** * File descriptor for configuration directory @@ -98,26 +98,27 @@ static int confdirfd = -1; /** * List of CRTC:s */ -static char** crtc_icc_keys = NULL; +static char **crtc_icc_keys = NULL; /** * List of ICC profile pathnames for corresponding * CRTC in `crtc_icc_keys` */ -static char** crtc_icc_values = NULL; +static char **crtc_icc_values = NULL; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " - "(-x | [-p priority] [-d] [file])\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " + "(-x | [-p priority] [-d] [file])\n", + argv0); + exit(1); } @@ -127,28 +128,29 @@ void usage(void) * @param ret The value to return * @return `ret` is returned as is */ -static int cleanup(int ret) +static int +cleanup(int ret) { - int saved_errno = errno; - size_t i; - libcoopgamma_ramps_destroy(&uniramps); - if (confdirfd >= 0) - close(confdirfd); - if (rampses != NULL) - for (i = 0; i < crtcs_n; i++) - libcoopgamma_ramps_destroy(rampses + i); - free(rampses); - free(depths); - if (crtc_icc_keys != NULL) - for (i = 0; crtc_icc_keys[i] != NULL; i++) - free(crtc_icc_keys[i]); - free(crtc_icc_keys); - if (crtc_icc_values != NULL) - for (i = 0; crtc_icc_values[i] != NULL; i++) - free(crtc_icc_values[i]); - free(crtc_icc_values); - errno = saved_errno; - return ret; + int saved_errno = errno; + size_t i; + libcoopgamma_ramps_destroy(&uniramps); + if (confdirfd >= 0) + close(confdirfd); + if (rampses) + for (i = 0; i < crtcs_n; i++) + libcoopgamma_ramps_destroy(rampses + i); + free(rampses); + free(depths); + if (crtc_icc_keys) + for (i = 0; crtc_icc_keys[i]; i++) + free(crtc_icc_keys[i]); + free(crtc_icc_keys); + if (crtc_icc_values) + for (i = 0; crtc_icc_values[i]; i++) + free(crtc_icc_values[i]); + free(crtc_icc_values); + errno = saved_errno; + return ret; } @@ -166,26 +168,27 @@ static int cleanup(int ret) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - default: - usage(); - } - return 0; - (void) arg; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + default: + usage(); + } + } + return 0; + (void) arg; } @@ -196,95 +199,93 @@ int handle_opt(char* opt, char* arg) * @path dirname The dirname of the ICC profile table * @return Zero on success, -1 on error */ -static int load_icc_table(int fd, const char *dirname) +static int +load_icc_table(int fd, const char *dirname) { - FILE *fp; - ssize_t len; - size_t lineno = 1, size = 0; - char *p, *q, *line = NULL; - int saved_errno; - size_t ptr = 0, siz = 0; - void *new; - size_t dirname_len = strlen(dirname); - fp = fdopen(fd, "rb"); - if (fp == NULL) - return -1; - for (; len = getline(&line, &size, fp), len >= 0; lineno++) - { - if (len && line[len - 1] == '\n') - line[--len] = '\0'; - p = line + strspn(line, " \t"); - if (!*p || (*p == '#')) - continue; - q = p + strspn(p, "0123456789abcdefABCDEF"); - if ((*q != ' ' && *q != '\t')) - { - fprintf(stderr, "%s: warning: line %zu is malformated in %s/%s\n", - argv0, lineno, dirname, ICCTAB); - continue; - } - *q = '\0'; - if ((size_t)(q - p) != 256) - fprintf(stderr, "%s: warning: EDID on line %zu in %s/%s looks to be of wrong length: %s\n", - argv0, lineno, dirname, ICCTAB, p); - q++; - q += strspn(p, " \t"); - if (!*q) - { - fprintf(stderr, "%s: warning: line %zu is malformated in %s/%s\n", - argv0, lineno, dirname, ICCTAB); - continue; - } - if (strchr(" \t", strchr(q, '\0')[-1])) - fprintf(stderr, "%s: warning: filename on line %zu in %s/%s ends with white space: %s\n", - argv0, lineno, dirname, ICCTAB, q); - if (ptr == siz) - { - new = realloc(crtc_icc_keys, (siz + 5) * sizeof(*crtc_icc_keys)); - if (new == NULL) - goto fail; - crtc_icc_keys = new; - new = realloc(crtc_icc_values, (siz + 5) * sizeof(*crtc_icc_values)); - if (new == NULL) - goto fail; - crtc_icc_values = new; - siz += 4; - } - crtc_icc_values[ptr] = malloc((*q == '/' ? 1 : dirname_len + sizeof("/")) + strlen(q)); - if (crtc_icc_values[ptr] == NULL) - goto fail; - if (*q == '/') - strcpy(crtc_icc_values[ptr], q); - else - stpcpy(stpcpy(stpcpy(crtc_icc_values[ptr], dirname), "/"), q); - crtc_icc_keys[ptr] = malloc(strlen(p) + 1); - if (crtc_icc_keys[ptr] == NULL) - { - ptr++; - goto fail; + FILE *fp; + ssize_t len; + size_t lineno = 1, size = 0; + char *p, *q, *line = NULL; + int saved_errno; + size_t ptr = 0, siz = 0; + void *new; + size_t dirname_len = strlen(dirname); + fp = fdopen(fd, "rb"); + if (!fp) + return -1; + for (; (len = getline(&line, &size, fp)) >= 0; lineno++) { + if (len && line[len - 1] == '\n') + line[--len] = '\0'; + p = line + strspn(line, " \t"); + if (!*p || *p == '#') + continue; + q = p + strspn(p, "0123456789abcdefABCDEF"); + if (*q != ' ' && *q != '\t') { + fprintf(stderr, "%s: warning: line %zu is malformated in %s/%s\n", + argv0, lineno, dirname, ICCTAB); + continue; + } + *q = '\0'; + if ((size_t)(q - p) != 256) { + fprintf(stderr, "%s: warning: EDID on line %zu in %s/%s looks to be of wrong length: %s\n", + argv0, lineno, dirname, ICCTAB, p); + } + q++; + q += strspn(p, " \t"); + if (!*q) { + fprintf(stderr, "%s: warning: line %zu is malformated in %s/%s\n", + argv0, lineno, dirname, ICCTAB); + continue; + } + if (strchr(" \t", strchr(q, '\0')[-1])) { + fprintf(stderr, "%s: warning: filename on line %zu in %s/%s ends with white space: %s\n", + argv0, lineno, dirname, ICCTAB, q); + } + if (ptr == siz) { + new = realloc(crtc_icc_keys, (siz + 5) * sizeof(*crtc_icc_keys)); + if (!new) + goto fail; + crtc_icc_keys = new; + new = realloc(crtc_icc_values, (siz + 5) * sizeof(*crtc_icc_values)); + if (!new) + goto fail; + crtc_icc_values = new; + siz += 4; + } + crtc_icc_values[ptr] = malloc((*q == '/' ? 1 : dirname_len + sizeof("/")) + strlen(q)); + if (!crtc_icc_values[ptr]) + goto fail; + if (*q == '/') + strcpy(crtc_icc_values[ptr], q); + else + stpcpy(stpcpy(stpcpy(crtc_icc_values[ptr], dirname), "/"), q); + crtc_icc_keys[ptr] = malloc(strlen(p) + 1); + if (!crtc_icc_keys[ptr]) { + ptr++; + goto fail; + } + strcpy(crtc_icc_keys[ptr], p); + ptr++; } - strcpy(crtc_icc_keys[ptr], p); - ptr++; - } - if (ferror(fp)) - goto fail; - if (crtc_icc_keys != NULL) - crtc_icc_keys[ptr] = NULL; - if (crtc_icc_values != NULL) - crtc_icc_values[ptr] = NULL; - fclose(fp); - free(line); - return 0; - fail: - saved_errno = errno; - if (crtc_icc_keys != NULL) - crtc_icc_keys[ptr] = NULL; - if (crtc_icc_values != NULL) - crtc_icc_values[ptr] = NULL; - fclose(fp); - free(line); - errno = saved_errno; - return -1; + if (ferror(fp)) + goto fail; + if (crtc_icc_keys) + crtc_icc_keys[ptr] = NULL; + if (crtc_icc_values) + crtc_icc_values[ptr] = NULL; + fclose(fp); + free(line); + return 0; +fail: + saved_errno = errno; + if (crtc_icc_keys) + crtc_icc_keys[ptr] = NULL; + if (crtc_icc_values) + crtc_icc_values[ptr] = NULL; + fclose(fp); + free(line); + errno = saved_errno; + return -1; } @@ -297,53 +298,55 @@ static int load_icc_table(int fd, const char *dirname) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - struct passwd* pw; - char* path = NULL; - int saved_errno; - int fd = -1, q = xflag + dflag; - if ((q > 1) || (xflag && ((argc > 0) || (prio != NULL))) || (argc > 1)) - usage(); - icc_pathname = *argv; - memset(&uniramps, 0, sizeof(uniramps)); - if (!xflag && (icc_pathname == NULL)) - { - pw = getpwuid(getuid()); - if ((pw == NULL) || (pw->pw_dir == NULL)) - goto fail; - - path = malloc(strlen(pw->pw_dir) + sizeof("/.config")); - if (path == NULL) - goto fail; - - sprintf(path, "%s/.config", pw->pw_dir); - - if (access(path, F_OK) < 0) - sprintf(path, "/etc"); - - confdirfd = open(path, O_DIRECTORY); - if (confdirfd < 0) - goto fail; - - fd = openat(confdirfd, ICCTAB, O_RDONLY); - if (fd < 0) - goto fail; - - if (load_icc_table(fd, path) < 0) - goto fail; - - free(path), path = NULL; - close(fd), fd = -1; - } - return 0; - fail: - saved_errno = errno; - free(path), path = NULL; - if (fd >= 0) - close(fd); - errno = saved_errno; - return cleanup(-1); + struct passwd *pw; + char *path = NULL; + int saved_errno; + int fd = -1, q = xflag + dflag; + if ((q > 1) || (xflag && (argc > 0 || prio)) || argc > 1) + usage(); + icc_pathname = *argv; + memset(&uniramps, 0, sizeof(uniramps)); + if (!xflag && !icc_pathname) { + pw = getpwuid(getuid()); + if (!pw || !pw->pw_dir) + goto fail; + + path = malloc(strlen(pw->pw_dir) + sizeof("/.config")); + if (!path) + goto fail; + + sprintf(path, "%s/.config", pw->pw_dir); + + if (access(path, F_OK) < 0) + sprintf(path, "/etc"); + + confdirfd = open(path, O_DIRECTORY); + if (confdirfd < 0) + goto fail; + + fd = openat(confdirfd, ICCTAB, O_RDONLY); + if (fd < 0) + goto fail; + + if (load_icc_table(fd, path) < 0) + goto fail; + + free(path); + path = NULL; + close(fd), fd = -1; + } + return 0; +fail: + saved_errno = errno; + free(path); + path = NULL; + if (fd >= 0) + close(fd); + errno = saved_errno; + return cleanup(-1); } @@ -353,18 +356,19 @@ int handle_args(int argc, char* argv[], char* prio) * @param content The beginning of the encoded integer * @return The integer, decoded */ -static uint64_t icc_uint64(const char* restrict content) +static uint64_t +icc_uint64(const char *restrict content) { - uint64_t rc; - rc = (uint64_t)(unsigned char)(content[0]), rc <<= 8; - rc |= (uint64_t)(unsigned char)(content[1]), rc <<= 8; - rc |= (uint64_t)(unsigned char)(content[2]), rc <<= 8; - rc |= (uint64_t)(unsigned char)(content[3]), rc <<= 8; - rc |= (uint64_t)(unsigned char)(content[4]), rc <<= 8; - rc |= (uint64_t)(unsigned char)(content[5]), rc <<= 8; - rc |= (uint64_t)(unsigned char)(content[6]), rc <<= 8; - rc |= (uint64_t)(unsigned char)(content[7]); - return rc; + uint64_t rc; + rc = (uint64_t)(unsigned char)(content[0]), rc = (uint64_t)(rc << 8); + rc = (uint64_t)(rc | (uint64_t)(unsigned char)(content[1])), rc = (uint64_t)(rc << 8); + rc = (uint64_t)(rc | (uint64_t)(unsigned char)(content[2])), rc = (uint64_t)(rc << 8); + rc = (uint64_t)(rc | (uint64_t)(unsigned char)(content[3])), rc = (uint64_t)(rc << 8); + rc = (uint64_t)(rc | (uint64_t)(unsigned char)(content[4])), rc = (uint64_t)(rc << 8); + rc = (uint64_t)(rc | (uint64_t)(unsigned char)(content[5])), rc = (uint64_t)(rc << 8); + rc = (uint64_t)(rc | (uint64_t)(unsigned char)(content[6])), rc = (uint64_t)(rc << 8); + rc = (uint64_t)(rc | (uint64_t)(unsigned char)(content[7])); + return rc; } @@ -374,14 +378,15 @@ static uint64_t icc_uint64(const char* restrict content) * @param content The beginning of the encoded integer * @return The integer, decoded */ -static uint32_t icc_uint32(const char* restrict content) +static uint32_t +icc_uint32(const char *restrict content) { - uint32_t rc; - rc = (uint32_t)(unsigned char)(content[0]), rc <<= 8; - rc |= (uint32_t)(unsigned char)(content[1]), rc <<= 8; - rc |= (uint32_t)(unsigned char)(content[2]), rc <<= 8; - rc |= (uint32_t)(unsigned char)(content[3]); - return rc; + uint32_t rc; + rc = (uint32_t)(unsigned char)(content[0]), rc = (uint32_t)(rc << 8); + rc = (uint32_t)(rc | (uint32_t)(unsigned char)(content[1])), rc = (uint32_t)(rc << 8); + rc = (uint32_t)(rc | (uint32_t)(unsigned char)(content[2])), rc = (uint32_t)(rc << 8); + rc = (uint32_t)(rc | (uint32_t)(unsigned char)(content[3])); + return rc; } @@ -391,12 +396,13 @@ static uint32_t icc_uint32(const char* restrict content) * @param content The beginning of the encoded integer * @return The integer, decoded */ -static uint16_t icc_uint16(const char* restrict content) +static uint16_t +icc_uint16(const char *restrict content) { - uint16_t rc; - rc = (uint16_t)(unsigned char)(content[0]), rc <<= 8; - rc |= (uint16_t)(unsigned char)(content[1]); - return rc; + uint16_t rc; + rc = (uint16_t)(unsigned char)(content[0]), rc = (uint16_t)(rc << 8); + rc = (uint16_t)(rc | (uint16_t)(unsigned char)(content[1])); + return rc; } @@ -406,9 +412,10 @@ static uint16_t icc_uint16(const char* restrict content) * @param content The beginning of the encoded integer * @return The integer, decoded */ -static uint16_t icc_uint8(const char* restrict content) +static uint8_t +icc_uint8(const char *restrict content) { - return (uint8_t)(content[0]); + return (uint8_t)(content[0]); } @@ -419,17 +426,16 @@ static uint16_t icc_uint8(const char* restrict content) * @param width The number of bytes with which the value is encoded * @return The value, decoded */ -static double icc_double(const char* restrict content, size_t width) +static double icc_double(const char *restrict content, size_t width) { - double ret = 0; - size_t i; - for (i = 0; i < width; i++) - { - ret /= 256; - ret += (double)(unsigned char)(content[width - 1 - i]); - } - ret /= 255; - return ret; + double ret = 0; + size_t i; + for (i = 0; i < width; i++) { + ret /= 256; + ret += (double)(unsigned char)(content[width - 1 - i]); + } + ret /= 255; + return ret; } @@ -445,226 +451,217 @@ static double icc_double(const char* restrict content, size_t width) * @return Zero on success, -1 on error, -2 if no usable data is * available in the profile. */ -static int parse_icc(const char* restrict content, size_t n, libcoopgamma_ramps_t* ramps, - libcoopgamma_depth_t* depth) +static int parse_icc(const char *restrict content, size_t n, libcoopgamma_ramps_t *ramps, libcoopgamma_depth_t *depth) { - uint32_t i_tag, n_tags; - size_t i, ptr = 0, xptr; - - /* Skip header */ - if (n - ptr < 128) - return -2; - ptr += 128; - - /* Get the number of tags */ - if (n - ptr < 4) - return -2; - n_tags = icc_uint32(content + ptr), ptr += 4; + uint32_t tag_name, tag_offset, tag_size, gamma_type; + size_t n_channels, n_entries, entry_size; + double r_gamma, r_min, r_max, g_gamma, g_min, g_max, b_gamma, b_min, b_max; + uint32_t i_tag, n_tags; + size_t i, ptr = 0, xptr; - for (i_tag = 0, xptr = ptr; i_tag < n_tags; i_tag++, ptr = xptr) - { - uint32_t tag_name, tag_offset, tag_size, gamma_type; - - /* Get profile encoding type, offset to the profile and the encoding size of its data */ - if (n - ptr < 12) - return -2; - tag_name = icc_uint32(content + ptr), ptr += 4; - tag_offset = icc_uint32(content + ptr), ptr += 4; - tag_size = icc_uint32(content + ptr), ptr += 4; - xptr = ptr; - - /* Jump to the profile data */ - if (tag_offset > INT32_MAX - tag_size) - return -2; - if (tag_offset + tag_size > n) - return -2; - ptr = tag_offset; - - if (tag_name == MLUT_TAG) - { - /* The profile is encododed as an dual-byte precision lookup table */ - - /* Initialise ramps */ - *depth = LIBCOOPGAMMA_UINT16; - ramps->u16.red_size = 256; - ramps->u16.green_size = 256; - ramps->u16.blue_size = 256; - if (libcoopgamma_ramps_initialise(&(ramps->u16)) < 0) - return -1; - - /* Get the lookup table */ - if (n - ptr < 3 * 256 * 2) - continue; - for (i = 0; i < 256; i++) - ramps->u16.red[i] = icc_uint16(content + ptr), ptr += 2; - for (i = 0; i < 256; i++) - ramps->u16.green[i] = icc_uint16(content + ptr), ptr += 2; - for (i = 0; i < 256; i++) - ramps->u16.blue[i] = icc_uint16(content + ptr), ptr += 2; - - return 0; - } - else if (tag_name == VCGT_TAG) - { - /* The profile is encoded as with gamma, brightness and contrast values - * or as a variable precision lookup table profile */ - - /* VCGT profiles starts where their magic number */ - if (n - ptr < 4) - continue; - tag_name = icc_uint32(content + ptr), ptr += 4; - if (tag_name != VCGT_TAG) - continue; - - /* Skip four bytes */ - if (n - ptr < 4) - continue; - ptr += 4; - - /* Get the actual encoding type */ - if (n - ptr < 4) - continue; - gamma_type = icc_uint32(content + ptr), ptr += 4; - - if (gamma_type == 0) - { - /* The profile is encoded as a variable precision lookup table */ - uint16_t n_channels, n_entries, entry_size; - - /* Get metadata */ - if (n - ptr < 3 * 4) - continue; - n_channels = icc_uint16(content + ptr), ptr += 2; - n_entries = icc_uint16(content + ptr), ptr += 2; - entry_size = icc_uint16(content + ptr), ptr += 2; - if (tag_size == 1584) - n_channels = 3, n_entries = 256, entry_size = 2; - if (n_channels != 3) - /* Assuming sRGB, can only be an correct assumption if there are exactly three channels */ - continue; - - /* Check data availability */ - if (n_channels > SIZE_MAX / n_entries) - continue; - if (entry_size > SIZE_MAX / (n_entries * n_channels)) - continue; - if (n - ptr < (size_t)n_channels * (size_t)n_entries * (size_t)entry_size) - continue; - - /* Initialise ramps */ - ramps->u8.red_size = (size_t)n_entries; - ramps->u8.green_size = (size_t)n_entries; - ramps->u8.blue_size = (size_t)n_entries; - switch (entry_size) - { - case 1: - *depth = LIBCOOPGAMMA_UINT8; - if (libcoopgamma_ramps_initialise(&(ramps->u8)) < 0) - return -1; - break; - case 2: - *depth = LIBCOOPGAMMA_UINT16; - if (libcoopgamma_ramps_initialise(&(ramps->u16)) < 0) - return -1; - break; - case 4: - *depth = LIBCOOPGAMMA_UINT32; - if (libcoopgamma_ramps_initialise(&(ramps->u32)) < 0) - return -1; - break; - case 8: - *depth = LIBCOOPGAMMA_UINT64; - if (libcoopgamma_ramps_initialise(&(ramps->u64)) < 0) - return -1; - break; - default: - *depth = LIBCOOPGAMMA_DOUBLE; - if (libcoopgamma_ramps_initialise(&(ramps->d)) < 0) - return -1; - break; + /* Skip header */ + if (n - ptr < 128) + return -2; + ptr += 128; + + /* Get the number of tags */ + if (n - ptr < 4) + return -2; + n_tags = icc_uint32(content + ptr), ptr += 4; + + for (i_tag = 0, xptr = ptr; i_tag < n_tags; i_tag++, ptr = xptr) { + /* Get profile encoding type, offset to the profile and the encoding size of its data */ + if (n - ptr < 12) + return -2; + tag_name = icc_uint32(content + ptr), ptr += 4; + tag_offset = icc_uint32(content + ptr), ptr += 4; + tag_size = icc_uint32(content + ptr), ptr += 4; + xptr = ptr; + + /* Jump to the profile data */ + if (tag_offset > INT32_MAX - tag_size) + return -2; + if (tag_offset + tag_size > n) + return -2; + ptr = tag_offset; + + if (tag_name == MLUT_TAG) { + /* The profile is encododed as an dual-byte precision lookup table */ + + /* Initialise ramps */ + *depth = LIBCOOPGAMMA_UINT16; + ramps->u16.red_size = 256; + ramps->u16.green_size = 256; + ramps->u16.blue_size = 256; + if (libcoopgamma_ramps_initialise(&ramps->u16) < 0) + return -1; + + /* Get the lookup table */ + if (n - ptr < 3 * 256 * 2) + continue; + for (i = 0; i < 256; i++, ptr += 2) + ramps->u16.red[i] = icc_uint16(content + ptr); + for (i = 0; i < 256; i++, ptr += 2) + ramps->u16.green[i] = icc_uint16(content + ptr); + for (i = 0; i < 256; i++, ptr += 2) + ramps->u16.blue[i] = icc_uint16(content + ptr); + + return 0; + } else if (tag_name == VCGT_TAG) { + /* The profile is encoded as with gamma, brightness and contrast values + * or as a variable precision lookup table profile */ + + /* VCGT profiles starts where their magic number */ + if (n - ptr < 4) + continue; + tag_name = icc_uint32(content + ptr), ptr += 4; + if (tag_name != VCGT_TAG) + continue; + + /* Skip four bytes */ + if (n - ptr < 4) + continue; + ptr += 4; + + /* Get the actual encoding type */ + if (n - ptr < 4) + continue; + gamma_type = icc_uint32(content + ptr), ptr += 4; + + if (!gamma_type) { + /* The profile is encoded as a variable precision lookup table */ + + /* Get metadata */ + if (n - ptr < 3 * 4) + continue; + n_channels = (size_t)icc_uint16(content + ptr), ptr += 2; + n_entries = (size_t)icc_uint16(content + ptr), ptr += 2; + entry_size = (size_t)icc_uint16(content + ptr), ptr += 2; + if (tag_size == 1584) + n_channels = 3, n_entries = 256, entry_size = 2; + if (n_channels != 3) + /* Assuming sRGB, can only be an correct assumption if there are exactly three channels */ + continue; + + /* Check data availability */ + if (n_channels > SIZE_MAX / n_entries) + continue; + if (entry_size > SIZE_MAX / (n_entries * n_channels)) + continue; + if (n - ptr < n_channels * n_entries * entry_size) + continue; + + /* Initialise ramps */ + ramps->u8.red_size = n_entries; + ramps->u8.green_size = n_entries; + ramps->u8.blue_size = n_entries; + switch (entry_size) { + case 1: + *depth = LIBCOOPGAMMA_UINT8; + if (libcoopgamma_ramps_initialise(&ramps->u8) < 0) + return -1; + break; + case 2: + *depth = LIBCOOPGAMMA_UINT16; + if (libcoopgamma_ramps_initialise(&ramps->u16) < 0) + return -1; + break; + case 4: + *depth = LIBCOOPGAMMA_UINT32; + if (libcoopgamma_ramps_initialise(&ramps->u32) < 0) + return -1; + break; + case 8: + *depth = LIBCOOPGAMMA_UINT64; + if (libcoopgamma_ramps_initialise(&ramps->u64) < 0) + return -1; + break; + default: + *depth = LIBCOOPGAMMA_DOUBLE; + if (libcoopgamma_ramps_initialise(&ramps->d) < 0) + return -1; + break; + } + + /* Get the lookup table */ + switch (*depth) { + case LIBCOOPGAMMA_UINT8: + for (i = 0; i < ramps->u8.red_size; i++, ptr += 1) + ramps->u8.red[i] = icc_uint8(content + ptr); + for (i = 0; i < ramps->u8.green_size; i++, ptr += 1) + ramps->u8.green[i] = icc_uint8(content + ptr); + for (i = 0; i < ramps->u8.blue_size; i++, ptr += 1) + ramps->u8.blue[i] = icc_uint8(content + ptr); + break; + case LIBCOOPGAMMA_UINT16: + for (i = 0; i < ramps->u16.red_size; i++, ptr += 2) + ramps->u16.red[i] = icc_uint16(content + ptr); + for (i = 0; i < ramps->u16.green_size; i++, ptr += 2) + ramps->u16.green[i] = icc_uint16(content + ptr); + for (i = 0; i < ramps->u16.blue_size; i++, ptr += 2) + ramps->u16.blue[i] = icc_uint16(content + ptr); + break; + case LIBCOOPGAMMA_UINT32: + for (i = 0; i < ramps->u32.red_size; i++, ptr += 4) + ramps->u32.red[i] = icc_uint32(content + ptr); + for (i = 0; i < ramps->u32.green_size; i++, ptr += 4) + ramps->u32.green[i] = icc_uint32(content + ptr); + for (i = 0; i < ramps->u32.blue_size; i++, ptr += 4) + ramps->u32.blue[i] = icc_uint32(content + ptr); + break; + case LIBCOOPGAMMA_UINT64: + for (i = 0; i < ramps->u64.red_size; i++, ptr += 8) + ramps->u64.red[i] = icc_uint64(content + ptr); + for (i = 0; i < ramps->u64.green_size; i++, ptr += 8) + ramps->u64.green[i] = icc_uint64(content + ptr); + for (i = 0; i < ramps->u64.blue_size; i++, ptr += 8) + ramps->u64.blue[i] = icc_uint64(content + ptr); + break; + case LIBCOOPGAMMA_FLOAT: + case LIBCOOPGAMMA_DOUBLE: + default: + for (i = 0; i < ramps->d.red_size; i++, ptr += entry_size) + ramps->d.red[i] = icc_double(content + ptr, entry_size); + for (i = 0; i < ramps->d.green_size; i++, ptr += entry_size) + ramps->d.green[i] = icc_double(content + ptr, entry_size); + for (i = 0; i < ramps->d.blue_size; i++, ptr += entry_size) + ramps->d.blue[i] = icc_double(content + ptr, entry_size); + break; + } + + return 0; + } else if (gamma_type == 1) { + /* The profile is encoded with gamma, brightness and contrast values */ + + /* Get the gamma, brightness and contrast */ + if (n - ptr < 9 * 4) + continue; + r_gamma = icc_uint32(content + ptr), r_gamma /= 65536L, ptr += 4; + r_min = icc_uint32(content + ptr), r_min /= 65536L, ptr += 4; + r_max = icc_uint32(content + ptr), r_max /= 65536L, ptr += 4; + g_gamma = icc_uint32(content + ptr), g_gamma /= 65536L, ptr += 4; + g_min = icc_uint32(content + ptr), g_min /= 65536L, ptr += 4; + g_max = icc_uint32(content + ptr), g_max /= 65536L, ptr += 4; + b_gamma = icc_uint32(content + ptr), b_gamma /= 65536L, ptr += 4; + b_min = icc_uint32(content + ptr), b_min /= 65536L, ptr += 4; + b_max = icc_uint32(content + ptr), b_max /= 65536L, ptr += 4; + + /* Initialise ramps */ + *depth = LIBCOOPGAMMA_DOUBLE; + if (libcoopgamma_ramps_initialise(&ramps->d) < 0) + return -1; + + /* Set ramps */ + libclut_start_over(&ramps->d, (double)1, double, 1, 1, 1); + libclut_gamma(&ramps->d, (double)1, double, r_gamma, g_gamma, b_gamma); + libclut_rgb_limits(&ramps->d, (double)1, double, r_min, r_max, g_min, g_max, b_min, b_max); + + return 0; + } } - - /* Get the lookup table */ - switch (*depth) - { - case LIBCOOPGAMMA_UINT8: - for (i = 0; i < ramps->u8.red_size; i++) - ramps->u8.red[i] = icc_uint8(content + ptr), ptr += 1; - for (i = 0; i < ramps->u8.green_size; i++) - ramps->u8.green[i] = icc_uint8(content + ptr), ptr += 1; - for (i = 0; i < ramps->u8.blue_size; i++) - ramps->u8.blue[i] = icc_uint8(content + ptr), ptr += 1; - break; - case LIBCOOPGAMMA_UINT16: - for (i = 0; i < ramps->u16.red_size; i++) - ramps->u16.red[i] = icc_uint16(content + ptr), ptr += 2; - for (i = 0; i < ramps->u16.green_size; i++) - ramps->u16.green[i] = icc_uint16(content + ptr), ptr += 2; - for (i = 0; i < ramps->u16.blue_size; i++) - ramps->u16.blue[i] = icc_uint16(content + ptr), ptr += 2; - break; - case LIBCOOPGAMMA_UINT32: - for (i = 0; i < ramps->u32.red_size; i++) - ramps->u32.red[i] = icc_uint32(content + ptr), ptr += 4; - for (i = 0; i < ramps->u32.green_size; i++) - ramps->u32.green[i] = icc_uint32(content + ptr), ptr += 4; - for (i = 0; i < ramps->u32.blue_size; i++) - ramps->u32.blue[i] = icc_uint32(content + ptr), ptr += 4; - break; - case LIBCOOPGAMMA_UINT64: - for (i = 0; i < ramps->u64.red_size; i++) - ramps->u64.red[i] = icc_uint64(content + ptr), ptr += 8; - for (i = 0; i < ramps->u64.green_size; i++) - ramps->u64.green[i] = icc_uint64(content + ptr), ptr += 8; - for (i = 0; i < ramps->u64.blue_size; i++) - ramps->u64.blue[i] = icc_uint64(content + ptr), ptr += 8; - break; - default: - for (i = 0; i < ramps->d.red_size; i++) - ramps->d.red[i] = icc_double(content + ptr, entry_size), ptr += entry_size; - for (i = 0; i < ramps->d.green_size; i++) - ramps->d.green[i] = icc_double(content + ptr, entry_size), ptr += entry_size; - for (i = 0; i < ramps->d.blue_size; i++) - ramps->d.blue[i] = icc_double(content + ptr, entry_size), ptr += entry_size; - break; - } - - return 0; - } - else if (gamma_type == 1) - { - /* The profile is encoded with gamma, brightness and contrast values */ - double r_gamma, r_min, r_max, g_gamma, g_min, g_max, b_gamma, b_min, b_max; - - /* Get the gamma, brightness and contrast */ - if (n - ptr < 9 * 4) - continue; - r_gamma = icc_uint32(content + ptr), r_gamma /= 65536L, ptr += 4; - r_min = icc_uint32(content + ptr), r_min /= 65536L, ptr += 4; - r_max = icc_uint32(content + ptr), r_max /= 65536L, ptr += 4; - g_gamma = icc_uint32(content + ptr), g_gamma /= 65536L, ptr += 4; - g_min = icc_uint32(content + ptr), g_min /= 65536L, ptr += 4; - g_max = icc_uint32(content + ptr), g_max /= 65536L, ptr += 4; - b_gamma = icc_uint32(content + ptr), b_gamma /= 65536L, ptr += 4; - b_min = icc_uint32(content + ptr), b_min /= 65536L, ptr += 4; - b_max = icc_uint32(content + ptr), b_max /= 65536L, ptr += 4; - - /* Initialise ramps */ - *depth = LIBCOOPGAMMA_DOUBLE; - if (libcoopgamma_ramps_initialise(&(ramps->d)) < 0) - return -1; - - /* Set ramps */ - libclut_start_over(&(ramps->d), (double)1, double, 1, 1, 1); - libclut_gamma(&(ramps->d), (double)1, double, r_gamma, g_gamma, b_gamma); - libclut_rgb_limits(&(ramps->d), (double)1, double, r_min, r_max, g_min, g_max, b_min, b_max); - - return 0; - } } - } - - return -2; + + return -2; } @@ -679,57 +676,55 @@ static int parse_icc(const char* restrict content, size_t n, libcoopgamma_ramps_ * @return Zero on success, -1 on error, -2 if no usable data is * available in the profile. */ -static int load_icc(const char* file, libcoopgamma_ramps_t* ramps, libcoopgamma_depth_t* depth) +static int +load_icc(const char *file, libcoopgamma_ramps_t *ramps, libcoopgamma_depth_t *depth) { - char* content = NULL; - size_t ptr = 0, size = 0; - ssize_t got; - int fd = -1, r = -1, saved_errno; - - fd = open(file, O_RDONLY); - if (fd < 0) - { - if (errno == ENOENT) - { - fprintf(stderr, "%s: %s: %s\n", argv0, strerror(ENOENT), file); - errno = 0; - } - goto fail; - } - - for (;;) - { - if (ptr == size) - { - size_t new_size = size ? (size << 1) : 4098; - void* new = realloc(content, new_size); - if (new == NULL) - goto fail; - content = new; - size = new_size; + char *content = NULL; + size_t ptr = 0, size = 0; + ssize_t got; + int fd = -1, r = -1, saved_errno; + size_t new_size; + void *new; + + fd = open(file, O_RDONLY); + if (fd < 0) { + if (errno == ENOENT) { + fprintf(stderr, "%s: %s: %s\n", argv0, strerror(ENOENT), file); + errno = 0; + } + goto fail; } - got = read(fd, content + ptr, size - ptr); - if (got < 0) - { - if (errno == EINTR) - continue; - goto fail; + + for (;;) { + if (ptr == size) { + new_size = size ? (size << 1) : 4098; + new = realloc(content, new_size); + if (!new) + goto fail; + content = new; + size = new_size; + } + got = read(fd, content + ptr, size - ptr); + if (got < 0) { + if (errno == EINTR) + continue; + goto fail; + } + if (!got) + break; + ptr += (size_t)got; } - if (got == 0) - break; - ptr += (size_t)got; - } - - close(fd), fd = -1; - - r = parse_icc(content, ptr, ramps, depth); - fail: - saved_errno = errno; - if (fd >= 0) - close(fd); - free(content); - errno = saved_errno; - return r; + + close(fd), fd = -1; + + r = parse_icc(content, ptr, ramps, depth); +fail: + saved_errno = errno; + if (fd >= 0) + close(fd); + free(content); + errno = saved_errno; + return r; } @@ -739,14 +734,15 @@ static int load_icc(const char* file, libcoopgamma_ramps_t* ramps, libcoopgamma_ * @param crtc The CRTC name * @return The ICC profile file */ -static const char* get_icc(const char* crtc) +static const char * +get_icc(const char *crtc) { - size_t i; - if (crtc_icc_keys != NULL) - for (i = 0; crtc_icc_keys[i] != NULL; i++) - if (!strcasecmp(crtc, crtc_icc_keys[i])) - return crtc_icc_values[i]; - return NULL; + size_t i; + if (crtc_icc_keys) + for (i = 0; crtc_icc_keys[i]; i++) + if (!strcasecmp(crtc, crtc_icc_keys[i])) + return crtc_icc_values[i]; + return NULL; } @@ -757,40 +753,38 @@ static const char* get_icc(const char* crtc) * @param ramps The prototype filter * @param depth The prototype filter's stop datatype */ -static void fill_filter(libcoopgamma_filter_t* filter, const libcoopgamma_ramps_t* ramps, - libcoopgamma_depth_t depth) +static void +fill_filter(libcoopgamma_filter_t *filter, const libcoopgamma_ramps_t *ramps, libcoopgamma_depth_t depth) { - switch (filter->depth) - { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - switch (depth)\ - {\ - case LIBCOOPGAMMA_UINT8:\ - libclut_translate(&(filter->ramps.MEMBER), MAX, TYPE, &(ramps->u8), UINT8_MAX, uint8_t);\ - break;\ - case LIBCOOPGAMMA_UINT16:\ - libclut_translate(&(filter->ramps.MEMBER), MAX, TYPE, &(ramps->u16), UINT16_MAX, uint16_t);\ - break;\ - case LIBCOOPGAMMA_UINT32:\ - libclut_translate(&(filter->ramps.MEMBER), MAX, TYPE, &(ramps->u32), UINT32_MAX, uint32_t);\ - break;\ - case LIBCOOPGAMMA_UINT64:\ - libclut_translate(&(filter->ramps.MEMBER), MAX, TYPE, &(ramps->u64), UINT64_MAX, uint64_t);\ - break;\ - case LIBCOOPGAMMA_FLOAT:\ - libclut_translate(&(filter->ramps.MEMBER), MAX, TYPE, &(ramps->f), (float)1, float);\ - break;\ - case LIBCOOPGAMMA_DOUBLE:\ - libclut_translate(&(filter->ramps.MEMBER), MAX, TYPE, &(ramps->d), (double)1, double);\ - break;\ - }\ - break; -LIST_DEPTHS + case CONST:\ + switch (depth) {\ + case LIBCOOPGAMMA_UINT8:\ + libclut_translate(&filter->ramps.MEMBER, MAX, TYPE, &ramps->u8, UINT8_MAX, uint8_t);\ + break;\ + case LIBCOOPGAMMA_UINT16:\ + libclut_translate(&filter->ramps.MEMBER, MAX, TYPE, &ramps->u16, UINT16_MAX, uint16_t);\ + break;\ + case LIBCOOPGAMMA_UINT32:\ + libclut_translate(&filter->ramps.MEMBER, MAX, TYPE, &ramps->u32, UINT32_MAX, uint32_t);\ + break;\ + case LIBCOOPGAMMA_UINT64:\ + libclut_translate(&filter->ramps.MEMBER, MAX, TYPE, &ramps->u64, UINT64_MAX, uint64_t);\ + break;\ + case LIBCOOPGAMMA_FLOAT:\ + libclut_translate(&filter->ramps.MEMBER, MAX, TYPE, &ramps->f, (float)1, float);\ + break;\ + case LIBCOOPGAMMA_DOUBLE:\ + libclut_translate(&filter->ramps.MEMBER, MAX, TYPE, &ramps->d, (double)1, double);\ + break;\ + }\ + break; + LIST_DEPTHS #undef X - default: - abort(); - } + default: + abort(); + } } @@ -802,127 +796,118 @@ LIST_DEPTHS * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - const char* path; - - if (xflag) - for (i = 0; i < crtcs_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < crtcs_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < crtcs_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - if (!xflag && (icc_pathname == NULL)) - if ((r = make_slaves()) < 0) - return cleanup(r); - - if (icc_pathname != NULL) - { - uniramps.u8.red_size = uniramps.u8.green_size = uniramps.u8.blue_size = 1; - for (i = 0; i < crtcs_n; i++) - { - if (uniramps.u8.red_size < crtc_updates[i].filter.ramps.u8.red_size) - uniramps. u8.red_size = crtc_updates[i].filter.ramps.u8.red_size; - if (uniramps.u8.green_size < crtc_updates[i].filter.ramps.u8.green_size) - uniramps. u8.green_size = crtc_updates[i].filter.ramps.u8.green_size; - if (uniramps.u8.blue_size < crtc_updates[i].filter.ramps.u8.blue_size) - uniramps. u8.blue_size = crtc_updates[i].filter.ramps.u8.blue_size; + int r; + size_t i, j; + const char *path; + + if (xflag) + for (i = 0; i < crtcs_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < crtcs_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < crtcs_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + if (!xflag && !icc_pathname) + if ((r = make_slaves()) < 0) + return cleanup(r); + + if (icc_pathname) { + uniramps.u8.red_size = uniramps.u8.green_size = uniramps.u8.blue_size = 1; + for (i = 0; i < crtcs_n; i++) { + if (uniramps.u8.red_size < crtc_updates[i].filter.ramps.u8.red_size) + uniramps. u8.red_size = crtc_updates[i].filter.ramps.u8.red_size; + if (uniramps.u8.green_size < crtc_updates[i].filter.ramps.u8.green_size) + uniramps. u8.green_size = crtc_updates[i].filter.ramps.u8.green_size; + if (uniramps.u8.blue_size < crtc_updates[i].filter.ramps.u8.blue_size) + uniramps. u8.blue_size = crtc_updates[i].filter.ramps.u8.blue_size; + } + switch (load_icc(icc_pathname, &uniramps, &unidepth)) { + case 0: + break; + case -1: + return cleanup(-1); + case -2: + fprintf(stderr, "%s: unusable ICC profile: %s\n", argv0, icc_pathname); + return cleanup(-3); + } + } else { + rampses = calloc(crtcs_n, sizeof(*rampses)); + if (!rampses) + return cleanup(-1); + depths = malloc(crtcs_n * sizeof(*depths)); + if (!depths) + return cleanup(-1); + for (i = 0; i < crtcs_n; i++) { + rampses[i].u8.red_size = crtc_updates[i].filter.ramps.u8.red_size; + rampses[i].u8.green_size = crtc_updates[i].filter.ramps.u8.green_size; + rampses[i].u8.blue_size = crtc_updates[i].filter.ramps.u8.blue_size; + path = get_icc(crtc_updates[i].filter.crtc); + if (!path) { + /* TODO remove CRTC */ + } else { + switch (load_icc(path, rampses + i, depths + i)) { + case 0: + break; + case -1: + return cleanup(-1); + case -2: + fprintf(stderr, "%s: unusable ICC profile: %s\n", argv0, path); + return cleanup(-3); + } + } + } } - switch (load_icc(icc_pathname, &uniramps, &unidepth)) - { - case 0: - break; - case -1: - return cleanup(-1); - case -2: - fprintf(stderr, "%s: unusable ICC profile: %s\n", argv0, icc_pathname); - return cleanup(-3); + + for (i = 0, r = 1; i < crtcs_n; i++) { + if (!crtc_updates[i].master || !crtc_info[i].supported) + continue; + if (!xflag) { + if (icc_pathname) + fill_filter(&crtc_updates[i].filter, &uniramps, unidepth); + else + fill_filter(&crtc_updates[i].filter, rampses + i, depths[i]); + } + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + } + } } - } - else - { - rampses = calloc(crtcs_n, sizeof(*rampses)); - if (rampses == NULL) - return cleanup(-1); - depths = malloc(crtcs_n * sizeof(*depths)); - if (depths == NULL) - return cleanup(-1); - for (i = 0; i < crtcs_n; i++) - { - rampses[i].u8.red_size = crtc_updates[i].filter.ramps.u8.red_size; - rampses[i].u8.green_size = crtc_updates[i].filter.ramps.u8.green_size; - rampses[i].u8.blue_size = crtc_updates[i].filter.ramps.u8.blue_size; - path = get_icc(crtc_updates[i].filter.crtc); - if (path == NULL) - { - /* TODO remove CRTC */ - } - else - switch (load_icc(path, rampses + i, depths + i)) - { - case 0: - break; - case -1: + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return cleanup(r); + + if (!dflag) + return cleanup(0); + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) return cleanup(-1); - case -2: - fprintf(stderr, "%s: unusable ICC profile: %s\n", argv0, path); - return cleanup(-3); - } - } - } - - for (i = 0, r = 1; i < crtcs_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[i].supported)) - continue; - if (!xflag) - { - if (icc_pathname != NULL) - fill_filter(&(crtc_updates[i].filter), &uniramps, unidepth); - else - fill_filter(&(crtc_updates[i].filter), rampses + i, depths[i]); - } - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return cleanup(r); - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return cleanup(r); - } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return cleanup(r); - - if (!dflag) - return cleanup(0); - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return cleanup(-1); - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return cleanup(-1); + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return cleanup(-1); + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return cleanup(-1); + +enotrecoverable: + pause(); + return cleanup(-1); } diff --git a/cg-limits.c b/cg-limits.c index 896505f..242d46b 100644 --- a/cg-limits.c +++ b/cg-limits.c @@ -27,7 +27,7 @@ char default_class[] = PKGNAME "::cg-limits::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; @@ -44,12 +44,12 @@ static int xflag = 0; /** * -B: brightness listing file */ -static char* Bflag = NULL; +static char *Bflag = NULL; /** * -C: constrat listing file */ -static char* Cflag = NULL; +static char *Cflag = NULL; /** * The brightness of the red channel @@ -85,63 +85,64 @@ static double bcontrast = 1; * `NULL`-terminated list of output names * listed in the brightness configuration file */ -static char** brightness_names = NULL; +static char **brightness_names = NULL; /** * The brightness of the red channel on monitor * with same index in `brightness_names` */ -static double* rbrightnesses = NULL; +static double *rbrightnesses = NULL; /** * The brightness of the green channel on monitor * with same index in `brightness_names` */ -static double* gbrightnesses = NULL; +static double *gbrightnesses = NULL; /** * The brightness of the blue channel on monitor * with same index in `brightness_names` */ -static double* bbrightnesses = NULL; +static double *bbrightnesses = NULL; /** * `NULL`-terminated list of output names * listed in the contrast configuration file */ -static char** contrast_names = NULL; +static char **contrast_names = NULL; /** * The contrast of the red channel on monitor * with same index in `contrast_names` */ -static double* rcontrasts = NULL; +static double *rcontrasts = NULL; /** * The contrast of the green channel on monitor * with same index in `contrast_names` */ -static double* gcontrasts = NULL; +static double *gcontrasts = NULL; /** * The contrast of the blue channel on monitor * with same index in `contrast_names` */ -static double* bcontrasts = NULL; +static double *bcontrasts = NULL; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] (-x | [-p priority] [-d] " - "([-B brightness-file] [-C contrast-file] | brightness-all:contrast-all | " - "brightness-red:contrast-red brightness-green:contrast-green brightness-blue:contrast-blue))\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] (-x | [-p priority] [-d] " + "([-B brightness-file] [-C contrast-file] | brightness-all:contrast-all | " + "brightness-red:contrast-red brightness-green:contrast-green brightness-blue:contrast-blue))\n", + argv0); + exit(1); } @@ -151,31 +152,27 @@ void usage(void) * @param ret The value to return * @return `ret` is returned as is */ -static int cleanup(int ret) +static int +cleanup(int ret) { - int saved_errno = errno; - if (brightness_names != NULL) - { - char** p = brightness_names; - while (*p) - free(*p++); - } - free(brightness_names); - free(rbrightnesses); - free(gbrightnesses); - free(bbrightnesses); - if (contrast_names != NULL) - { - char** p = contrast_names; - while (*p) - free(*p++); - } - free(contrast_names); - free(rcontrasts); - free(gcontrasts); - free(bcontrasts); - errno = saved_errno; - return ret; + int saved_errno = errno; + char **p; + if (brightness_names) + for (p = brightness_names; *p; p++) + free(*p); + free(brightness_names); + free(rbrightnesses); + free(gbrightnesses); + free(bbrightnesses); + if (contrast_names) + for (p = contrast_names; *p; p++) + free(*p); + free(contrast_names); + free(rcontrasts); + free(gcontrasts); + free(bcontrasts); + errno = saved_errno; + return ret; } @@ -193,33 +190,34 @@ static int cleanup(int ret) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - case 'B': - if (Bflag || !(Bflag = arg)) - usage(); - return 1; - case 'C': - if (Cflag || !(Cflag = arg)) - usage(); - return 1; - default: - usage(); - } - return 0; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + case 'B': + if (Bflag || !(Bflag = arg)) + usage(); + return 1; + case 'C': + if (Cflag || !(Cflag = arg)) + usage(); + return 1; + default: + usage(); + } + } + return 0; } @@ -229,25 +227,26 @@ int handle_opt(char* opt, char* arg) * @param confname The filename (excluding directory) of the configuration file * @return The full pathname of the configuration file, `NULL` on error */ -static char* get_conf_file(const char* restrict confname) +static char * +get_conf_file(const char *restrict confname) { - struct passwd* pw; - char* path; - - pw = getpwuid(getuid()); - if ((pw == NULL) || (pw->pw_dir == NULL)) - return NULL; - - path = malloc(strlen(pw->pw_dir) + strlen(confname) + sizeof("/.config/")); - if (path == NULL) - return NULL; - - sprintf(path, "%s/.config/%s", pw->pw_dir, confname); - - if (access(path, F_OK) < 0) - sprintf(path, "/etc/%s", confname); - - return path; + struct passwd *pw; + char *path; + + pw = getpwuid(getuid()); + if (!pw || !pw->pw_dir) + return NULL; + + path = malloc(strlen(pw->pw_dir) + strlen(confname) + sizeof("/.config/")); + if (!path) + return NULL; + + sprintf(path, "%s/.config/%s", pw->pw_dir, confname); + + if (access(path, F_OK) < 0) + sprintf(path, "/etc/%s", confname); + + return path; } @@ -258,16 +257,17 @@ static char* get_conf_file(const char* restrict confname) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_double(double* restrict out, const char* restrict str) +static int +parse_double(double *restrict out, const char *restrict str) { - char* end; - errno = 0; - *out = strtod(str, &end); - if (errno || isinf(*out) || isnan(*out) || *end) - return -1; - if (!*str || !strchr("-0123456789.", *str)) - return -1; - return 0; + char *end; + errno = 0; + *out = strtod(str, &end); + if (errno || isinf(*out) || isnan(*out) || *end) + return -1; + if (!*str || !strchr("-0123456789.", *str)) + return -1; + return 0; } @@ -279,16 +279,17 @@ static int parse_double(double* restrict out, const char* restrict str) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_twidouble(double* restrict left, double* restrict right, const char* restrict str) +static int +parse_twidouble(double *restrict left, double *restrict right, const char *restrict str) { - char* p = strchr(str, ':'); - int r; - if (p == NULL) - return -1; - *p = '\0'; - r = -((parse_double(left, str) < 0) || (parse_double(right, p + 1) < 0)); - *p = ':'; - return r; + char *p = strchr(str, ':'); + int r; + if (!p) + return -1; + *p = '\0'; + r = -(parse_double(left, str) < 0 || parse_double(right, &p[1]) < 0); + *p = ':'; + return r; } @@ -302,143 +303,137 @@ static int parse_twidouble(double* restrict left, double* restrict right, const * @param bs Reference to the list of blue values * @return Zero on success, -1 on error */ -static int parse_conf_file(const char* restrict pathname, char*** restrict names, - double** restrict rs, double** restrict gs, double** restrict bs) +static int +parse_conf_file(const char *restrict pathname, char ***restrict names, + double **restrict rs, double **restrict gs, double **restrict bs) { - int fd, saved_errno; - char* line = NULL; - size_t size = 0, lineno = 0, ptr = 0, alloc = 0; - ssize_t n; - FILE* f = NULL; - char* p; - char* q; - char* r; - char* g; - char* b; - - fd = open(pathname, O_RDONLY); - if (fd == -1) - return -1; - - f = fdopen(fd, "rb"); - if (f == NULL) - goto fail; - - while (n = getline(&line, &size, f), n >= 0) - { - lineno += 1; - - if ((n > 0) && (line[n - 1] == '\n')) - line[n - 1] = '\0'; - p = line; - while ((*p == ' ') || (*p == '\t')) p++; - if ((!*p) || (*p == '#')) - continue; - - r = strpbrk(line, " \t"); - if (r == NULL) - goto bad; - while (r[1] == ' ' || r[1] == '\t') r++; - g = strpbrk(r + 1, " \t"); - if (g == NULL) - goto bad; - while (g[1] == ' ' || g[1] == '\t') g++; - b = strpbrk(g + 1, " \t"); - if (b == NULL) - goto bad; - while (b[1] == ' ' || b[1] == '\t') b++; - - for (;;) - { - q = strpbrk(b + 1, " \t"); - if (q == NULL) - break; - while (q[1] == ' ' || q[1] == '\t') q++; - if (!*q) - break; - r = g, g = b, b = q; + int fd, saved_errno; + char *line = NULL; + size_t size = 0, lineno = 0, ptr = 0, alloc = 0; + ssize_t n; + FILE *f = NULL; + char *p, *q; + char *r, *g, *b; + void *new; + size_t new_size; + + fd = open(pathname, O_RDONLY); + if (fd < 0) + return -1; + + f = fdopen(fd, "rb"); + if (!f) + goto fail; + + while ((n = getline(&line, &size, f)) >= 0) { + lineno += 1; + + if (n > 0 && line[n - 1] == '\n') + line[n - 1] = '\0'; + for (p = line; *p == ' ' || *p == '\t'; p++); + if (!*p || *p == '#') + continue; + + r = strpbrk(line, " \t"); + if (!r) + goto bad; + for (; r[1] == ' ' || r[1] == '\t'; r++); + g = strpbrk(r + 1, " \t"); + if (!g) + goto bad; + for (; g[1] == ' ' || g[1] == '\t'; g++); + b = strpbrk(g + 1, " \t"); + if (!b) + goto bad; + for (; b[1] == ' ' || b[1] == '\t'; b++); + + for (;;) { + q = strpbrk(b + 1, " \t"); + if (!q) + break; + for (;q[1] == ' ' || q[1] == '\t'; q++); + if (!*q) + break; + r = g, g = b, b = q; + } + + *r++ = '\0'; + *g++ = '\0'; + *b++ = '\0'; + + q = strpbrk(r, " \t"); + if (!q) + *q = '\0'; + q = strpbrk(g, " \t"); + if (!q) + *q = '\0'; + q = strpbrk(b, " \t"); + if (!q) + *q = '\0'; + + q = strchr(p, '\0'); + while (q != p && (q[-1] == ' ' || q[-1] == '\t')) + q--; + *q = '\0'; + + if (ptr == alloc) { + new_size= alloc ? (alloc << 1) : 4; + + new = realloc(*rs, new_size * sizeof(**rs)); + if (!new) + goto fail; + *rs = new; + + new = realloc(*gs, new_size * sizeof(**gs)); + if (!new) + goto fail; + *gs = new; + + new = realloc(*bs, new_size * sizeof(**bs)); + if (!new) + goto fail; + *bs = new; + + new = realloc(*names, (new_size + 1) * sizeof(**names)); + if (!new) + goto fail; + *names = new; + memset(*names + alloc, 0, (new_size + 1 - alloc) * sizeof(**names)); + + alloc = new_size; + } + + if (parse_double((*rs) + ptr, r) < 0 || + parse_double((*gs) + ptr, g) < 0 || + parse_double((*bs) + ptr, b) < 0) + goto bad; + (*names)[ptr] = malloc(strlen(p) + 1); + if (!(*names)[ptr]) + goto fail; + strcpy((*names)[ptr], p); + ptr++; + + continue; + bad: + fprintf(stderr, "%s: ignoring malformatted line in %s: %zu\n", argv0, pathname, lineno); } - - *r++ = '\0'; - *g++ = '\0'; - *b++ = '\0'; - - q = strpbrk(r, " \t"); - if (q != NULL) - *q = '\0'; - q = strpbrk(g, " \t"); - if (q != NULL) - *q = '\0'; - q = strpbrk(b, " \t"); - if (q != NULL) - *q = '\0'; - - q = strchr(p, '\0'); - while ((q != p) && ((q[-1] == ' ') || (q[-1] == '\t'))) - q--; - *q = '\0'; - - if (ptr == alloc) - { - void* new; - size_t new_size = alloc ? (alloc << 1) : 4; - - new = realloc(*rs, new_size * sizeof(**rs)); - if (new == NULL) - goto fail; - *rs = new; - - new = realloc(*gs, new_size * sizeof(**gs)); - if (new == NULL) - goto fail; - *gs = new; - - new = realloc(*bs, new_size * sizeof(**bs)); - if (new == NULL) - goto fail; - *bs = new; - - new = realloc(*names, (new_size + 1) * sizeof(**names)); - if (new == NULL) - goto fail; - *names = new; - memset(*names + alloc, 0, (new_size + 1 - alloc) * sizeof(**names)); - - alloc = new_size; + + if (fclose(f) < 0) { + f = NULL; + goto fail; } - - if ((parse_double((*rs) + ptr, r) < 0) || - (parse_double((*gs) + ptr, g) < 0) || - (parse_double((*bs) + ptr, b) < 0)) - goto bad; - (*names)[ptr] = malloc(strlen(p) + 1); - if ((*names)[ptr] == NULL) - goto fail; - strcpy((*names)[ptr], p); - ptr++; - - continue; - bad: - fprintf(stderr, "%s: ignoring malformatted line in %s: %zu\n", argv0, pathname, lineno); - } - - if (fclose(f) < 0) - { - f = NULL; - goto fail; - } - close(fd); - free(line); - return 0; - fail: - saved_errno = errno; - free(line); - if (f != NULL) - fclose(f); - if (fd >= 0) - close(fd); - errno = saved_errno; - return -1; + close(fd); + free(line); + return 0; +fail: + saved_errno = errno; + free(line); + if (f) + fclose(f); + if (fd >= 0) + close(fd); + errno = saved_errno; + return -1; } @@ -448,9 +443,10 @@ static int parse_conf_file(const char* restrict pathname, char*** restrict names * @param pathname The pathname of the file * @return Zero on success, -1 on error */ -static int parse_brightness_file(const char* restrict pathname) +static int +parse_brightness_file(const char *restrict pathname) { - return parse_conf_file(pathname, &brightness_names, &rbrightnesses, &gbrightnesses, &bbrightnesses); + return parse_conf_file(pathname, &brightness_names, &rbrightnesses, &gbrightnesses, &bbrightnesses); } @@ -460,9 +456,10 @@ static int parse_brightness_file(const char* restrict pathname) * @param pathname The pathname of the file * @return Zero on success, -1 on error */ -static int parse_contrast_file(const char* restrict pathname) +static int +parse_contrast_file(const char *restrict pathname) { - return parse_conf_file(pathname, &contrast_names, &rcontrasts, &gcontrasts, &bcontrasts); + return parse_conf_file(pathname, &contrast_names, &rcontrasts, &gcontrasts, &bcontrasts); } @@ -475,69 +472,71 @@ static int parse_contrast_file(const char* restrict pathname) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - int free_Bflag = 0, free_Cflag = 0, saved_errno; - int q = xflag + dflag; - if ((q > 1) || (xflag && ((Bflag != NULL) || (Cflag != NULL) || (argc > 0) || (prio != NULL)))) - usage(); - if ((Bflag || Cflag) && argc) - usage(); - - if (argc == 1) - { - if (parse_twidouble(&rbrightness, &rcontrast, argv[0]) < 0) - usage(); - bbrightness = gbrightness = rbrightness; - bcontrast = gcontrast = rcontrast; - } - else if (argc == 3) - { - if (parse_twidouble(&rbrightness, &rcontrast, argv[0]) < 0) - usage(); - if (parse_twidouble(&gbrightness, &gcontrast, argv[1]) < 0) - usage(); - if (parse_twidouble(&bbrightness, &bcontrast, argv[2]) < 0) - usage(); - } - else if (argc) - usage(); - - if (!argc && !Bflag && !xflag) - { - Bflag = get_conf_file("brightness"); - if (Bflag == NULL) - return -1; - free_Bflag = 1; - } - if (Bflag) - if (parse_brightness_file(Bflag) < 0) - goto fail; - if (free_Bflag) - free(Bflag), Bflag = NULL; - - if (!argc && !Cflag && !xflag) - { - Cflag = get_conf_file("contrast"); - if (Cflag == NULL) - return -1; - free_Cflag = 1; - } - if (Cflag) - if (parse_contrast_file(Cflag) < 0) - goto fail; - if (free_Cflag) - free(Cflag), Cflag = NULL; - - return 0; - fail: - saved_errno = errno; - if (free_Bflag) - free(Bflag), Bflag = NULL; - if (free_Cflag) - free(Cflag), Cflag = NULL; - errno = saved_errno; - return cleanup(-1); + int free_Bflag = 0, free_Cflag = 0, saved_errno; + int q = xflag + dflag; + if (q > 1 || (xflag && (Bflag || Cflag || argc > 0 || prio))) + usage(); + if ((Bflag || Cflag) && argc) + usage(); + + if (argc == 1) { + if (parse_twidouble(&rbrightness, &rcontrast, argv[0]) < 0) + usage(); + bbrightness = gbrightness = rbrightness; + bcontrast = gcontrast = rcontrast; + } else if (argc == 3) { + if (parse_twidouble(&rbrightness, &rcontrast, argv[0]) < 0) + usage(); + if (parse_twidouble(&gbrightness, &gcontrast, argv[1]) < 0) + usage(); + if (parse_twidouble(&bbrightness, &bcontrast, argv[2]) < 0) + usage(); + } else if (argc) { + usage(); + } + + if (!argc && !Bflag && !xflag) { + Bflag = get_conf_file("brightness"); + if (!Bflag) + return -1; + free_Bflag = 1; + } + if (Bflag && parse_brightness_file(Bflag) < 0) + goto fail; + if (free_Bflag) { + free(Bflag); + Bflag = NULL; + } + + if (!argc && !Cflag && !xflag) { + Cflag = get_conf_file("contrast"); + if (!Cflag) + return -1; + free_Cflag = 1; + } + if (Cflag && parse_contrast_file(Cflag) < 0) + goto fail; + if (free_Cflag) { + free(Cflag); + Cflag = NULL; + } + + return 0; +fail: + saved_errno = errno; + if (free_Bflag) { + free(Bflag); + Bflag = NULL; + } + if (free_Cflag) { + free(Cflag); + Cflag = NULL; + } + errno = saved_errno; + return cleanup(-1); } @@ -553,58 +552,57 @@ int handle_args(int argc, char* argv[], char* prio) * @param bc The blue contrast * @return Zero on success, -1 on error */ -static int fill_filter(libcoopgamma_filter_t* restrict filter, - double rb, double rc, double gb, double gc, double bb, double bc) +static int +fill_filter(libcoopgamma_filter_t *restrict filter, double rb, double rc, double gb, double gc, double bb, double bc) { - union libcoopgamma_ramps dramps; - size_t size; - - if (filter->depth == LIBCOOPGAMMA_DOUBLE) - { - libclut_rgb_limits(&(filter->ramps.d), (double)1, double, rb, rc, gb, gc, bb, bc); - libclut_clip(&(filter->ramps.d), (double)1, double, 1, 1, 1); - return 0; - } - if (filter->depth == LIBCOOPGAMMA_FLOAT) - { - libclut_rgb_limits(&(filter->ramps.f), (float)1, float, rb, rc, gb, gc, bb, bc); - libclut_clip(&(filter->ramps.f), (float)1, float, 1, 1, 1); - return 0; - } + union libcoopgamma_ramps dramps; + size_t size; - size = dramps.d.red_size = filter->ramps.d.red_size; - size += dramps.d.green_size = filter->ramps.d.green_size; - size += dramps.d.blue_size = filter->ramps.d.blue_size; - dramps.d.red = calloc(size, sizeof(double)); - if (dramps.d.red == NULL) - return -1; - dramps.d.green = dramps.d.red + dramps.d.red_size; - dramps.d.blue = dramps.d.green + dramps.d.green_size; - - libclut_start_over(&(dramps.d), (double)1, double, 1, 1, 1); - libclut_rgb_limits(&(dramps.d), (double)1, double, rb, rc, gb, gc, bb, bc); - libclut_clip(&(dramps.d), (double)1, double, 1, 1, 1); - - switch (filter->depth) - { - case LIBCOOPGAMMA_UINT8: - libclut_translate(&(filter->ramps.u8), UINT8_MAX, uint8_t, &(dramps.d), (double)1, double); - break; - case LIBCOOPGAMMA_UINT16: - libclut_translate(&(filter->ramps.u16), UINT16_MAX, uint16_t, &(dramps.d), (double)1, double); - break; - case LIBCOOPGAMMA_UINT32: - libclut_translate(&(filter->ramps.u32), UINT32_MAX, uint32_t, &(dramps.d), (double)1, double); - break; - case LIBCOOPGAMMA_UINT64: - libclut_translate(&(filter->ramps.u64), UINT64_MAX, uint64_t, &(dramps.d), (double)1, double); - break; - default: - abort(); - } - - free(dramps.d.red); - return 0; + if (filter->depth == LIBCOOPGAMMA_DOUBLE) { + libclut_rgb_limits(&filter->ramps.d, (double)1, double, rb, rc, gb, gc, bb, bc); + libclut_clip(&filter->ramps.d, (double)1, double, 1, 1, 1); + return 0; + } + if (filter->depth == LIBCOOPGAMMA_FLOAT) { + libclut_rgb_limits(&filter->ramps.f, (float)1, float, rb, rc, gb, gc, bb, bc); + libclut_clip(&filter->ramps.f, (float)1, float, 1, 1, 1); + return 0; + } + + size = dramps.d.red_size = filter->ramps.d.red_size; + size += dramps.d.green_size = filter->ramps.d.green_size; + size += dramps.d.blue_size = filter->ramps.d.blue_size; + dramps.d.red = calloc(size, sizeof(double)); + if (!dramps.d.red) + return -1; + dramps.d.green = dramps.d.red + dramps.d.red_size; + dramps.d.blue = dramps.d.green + dramps.d.green_size; + + libclut_start_over(&dramps.d, (double)1, double, 1, 1, 1); + libclut_rgb_limits(&dramps.d, (double)1, double, rb, rc, gb, gc, bb, bc); + libclut_clip(&dramps.d, (double)1, double, 1, 1, 1); + + switch (filter->depth) { + case LIBCOOPGAMMA_UINT8: + libclut_translate(&filter->ramps.u8, UINT8_MAX, uint8_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_UINT16: + libclut_translate(&filter->ramps.u16, UINT16_MAX, uint16_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_UINT32: + libclut_translate(&filter->ramps.u32, UINT32_MAX, uint32_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_UINT64: + libclut_translate(&filter->ramps.u64, UINT64_MAX, uint64_t, &dramps.d, (double)1, double); + break; + case LIBCOOPGAMMA_FLOAT: + case LIBCOOPGAMMA_DOUBLE: + default: + abort(); + } + + free(dramps.d.red); + return 0; } @@ -616,99 +614,105 @@ static int fill_filter(libcoopgamma_filter_t* restrict filter, * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j, k; - - if (xflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - if (!xflag && ((brightness_names != NULL) || (contrast_names != NULL))) - if ((r = make_slaves()) < 0) - return cleanup(r); - - if ((brightness_names == NULL) && (contrast_names == NULL)) - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - if (!xflag) - if ((r = fill_filter(&(crtc_updates[i].filter), rbrightness, rcontrast, - gbrightness, gcontrast, bbrightness, bcontrast)) < 0) - return cleanup(r); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return cleanup(r); - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return cleanup(r); - } - } - else - { - char* empty = NULL; - char** bnames = brightness_names ? brightness_names : ∅ - char** cnames = contrast_names ? contrast_names : ∅ - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_info[crtc_updates[i].crtc].supported)) - continue; - for (j = 0; bnames[j] != NULL; j++) - if (!strcasecmp(crtc_updates[i].filter.crtc, bnames[j])) - break; - for (k = 0; cnames[k] != NULL; k++) - if (!strcasecmp(crtc_updates[i].filter.crtc, cnames[k])) - break; - if ((bnames[j] != NULL) || (cnames[k] != NULL)) - { - double rb = 0, gb = 0, bb = 0, rc = 1, bc = 1, gc = 1; - if (bnames[j] != NULL) - rb = rbrightnesses[j], gb = gbrightnesses[j], bb = bbrightnesses[j]; - if (cnames[j] != NULL) - rc = rcontrasts[j], gc = gcontrasts[j], bc = bcontrasts[j]; - if ((r = fill_filter(&(crtc_updates[i].filter), rb, rc, gb, gc, bb, bc)) < 0) - return cleanup(r); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return cleanup(r); - } + int r; + size_t i, j, k; + char *empty = NULL; + char **bnames, **cnames; + double rb, gb, bb, rc, bc, gc; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + if (!xflag && (brightness_names || contrast_names)) + if ((r = make_slaves()) < 0) + return cleanup(r); + + if (!brightness_names && !contrast_names) { + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + if (!xflag) + if ((r = fill_filter(&crtc_updates[i].filter, rbrightness, rcontrast, + gbrightness, gcontrast, bbrightness, bcontrast)) < 0) + return cleanup(r); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + if (!crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + } + } + } + } else { + bnames = brightness_names ? brightness_names : ∅ + cnames = contrast_names ? contrast_names : ∅ + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_info[crtc_updates[i].crtc].supported) + continue; + for (j = 0; bnames[j]; j++) + if (!strcasecmp(crtc_updates[i].filter.crtc, bnames[j])) + break; + for (k = 0; cnames[k]; k++) + if (!strcasecmp(crtc_updates[i].filter.crtc, cnames[k])) + break; + if (bnames[j] || cnames[k]) { + rb = gb = bb = 0; + rc = bc = gc = 1; + if (bnames[j]) { + rb = rbrightnesses[j]; + gb = gbrightnesses[j]; + bb = bbrightnesses[j]; + } + if (cnames[j]) { + rc = rcontrasts[j]; + gc = gcontrasts[j]; + bc = bcontrasts[j]; + } + if ((r = fill_filter(&crtc_updates[i].filter, rb, rc, gb, gc, bb, bc)) < 0) + return cleanup(r); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return cleanup(r); + } + } } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return cleanup(r); - - if (!dflag) - return cleanup(0); - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return cleanup(-1); - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return cleanup(-1); + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return cleanup(r); + + if (!dflag) + return cleanup(0); + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return cleanup(-1); + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return cleanup(-1); + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return cleanup(-1); + +enotrecoverable: + pause(); + return cleanup(-1); } diff --git a/cg-linear.c b/cg-linear.c index 554478e..872b1e5 100644 --- a/cg-linear.c +++ b/cg-linear.c @@ -24,7 +24,7 @@ char default_class[] = PKGNAME "::cg-linear::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){":start", ":stop", NULL}; +const char *const *class_suffixes = (const char *const[]){":start", ":stop", NULL}; @@ -68,13 +68,14 @@ static int64_t stop_priority; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule-base] " - "(-x | -p start-priority:stop-priority [-d] [+rgb])\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule-base] " + "(-x | -p start-priority:stop-priority [-d] [+rgb])\n", + argv0); + exit(1); } @@ -92,47 +93,47 @@ void usage(void) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - default: - usage(); - } - else - switch (opt[1]) - { - case 'r': - if (rplus) - usage(); - rplus = 1; - break; - case 'g': - if (gplus) - usage(); - gplus = 1; - break; - case 'b': - if (bplus) - usage(); - bplus = 1; - break; - default: - usage(); - } - return 0; - (void) arg; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + default: + usage(); + } + } else { + switch (opt[1]) { + case 'r': + if (rplus) + usage(); + rplus = 1; + break; + case 'g': + if (gplus) + usage(); + gplus = 1; + break; + case 'b': + if (bplus) + usage(); + bplus = 1; + break; + default: + usage(); + } + } + return 0; + (void) arg; } @@ -145,31 +146,31 @@ int handle_opt(char* opt, char* arg) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - int q = xflag + (dflag | rplus | gplus | bplus); - char *p, *end; - if (argc || (q > 1) || (xflag && (prio != NULL))) - usage(); - if (!xflag && (prio == NULL)) - usage(); - if (prio != NULL) - { - p = strchr(prio, ':'); - if (!p) - usage(); - *p++ = '\0'; - errno = 0; - start_priority = (size_t)strtoul(prio, &end, 10); - if (errno || *end || !*prio) - usage(); - stop_priority = (size_t)strtoul(p, &end, 10); - if (errno || *end || !*prio) - usage(); - p[-1] = ':'; - } - return 0; - (void) argv; + int q = xflag + (dflag | rplus | gplus | bplus); + char *p, *end; + if (argc || q > 1 || (xflag && prio)) + usage(); + if (!xflag && !prio) + usage(); + if (prio) { + p = strchr(prio, ':'); + if (!p) + usage(); + *p++ = '\0'; + errno = 0; + start_priority = (int64_t)strtoll(prio, &end, 10); + if (errno || *end || !*prio) + usage(); + stop_priority = (int64_t)strtoll(p, &end, 10); + if (errno || *end || !*prio) + usage(); + p[-1] = ':'; + } + return 0; + (void) argv; } @@ -179,22 +180,22 @@ int handle_args(int argc, char* argv[], char* prio) * @param filter The filter to fill * @param is_start If the fitler is a linearisation filter */ -static void fill_filter(libcoopgamma_filter_t* restrict filter, int is_start) +static void +fill_filter(libcoopgamma_filter_t *restrict filter, int is_start) { - switch (filter->depth) - { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - if (is_start)\ - libclut_linearise(&(filter->ramps.MEMBER), MAX, TYPE, !rplus, !gplus, !bplus);\ - else\ - libclut_standardise(&(filter->ramps.MEMBER), MAX, TYPE, !rplus, !gplus, !bplus);\ - break; -LIST_DEPTHS + case CONST:\ + if (is_start)\ + libclut_linearise(&filter->ramps.MEMBER, MAX, TYPE, !rplus, !gplus, !bplus);\ + else\ + libclut_standardise(&filter->ramps.MEMBER, MAX, TYPE, !rplus, !gplus, !bplus);\ + break; + LIST_DEPTHS #undef X - default: - abort(); - } + default: + abort(); + } } @@ -206,58 +207,58 @@ LIST_DEPTHS * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - - if (xflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_info[crtc_updates[i].crtc].supported)) - continue; - if (!xflag) { - int is_start = strchr(crtc_updates[i].filter.class, '\0')[-1] == 't'; - fill_filter(&(crtc_updates[i].filter), is_start); - crtc_updates[i].filter.priority = is_start ? start_priority : stop_priority; - } - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - if (!dflag) - return 0; - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return -1; - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return -1; + int r, is_start; + size_t i, j; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_info[crtc_updates[i].crtc].supported) + continue; + if (!xflag) { + is_start = strchr(crtc_updates[i].filter.class, '\0')[-1] == 't'; + fill_filter(&crtc_updates[i].filter, is_start); + crtc_updates[i].filter.priority = is_start ? start_priority : stop_priority; + } + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + if (!dflag) + return 0; + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return -1; + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return -1; + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return -1; + +enotrecoverable: + pause(); + return -1; } diff --git a/cg-negative.c b/cg-negative.c index be5f2a0..32fad71 100644 --- a/cg-negative.c +++ b/cg-negative.c @@ -24,7 +24,7 @@ char default_class[] = PKGNAME "::cg-negative::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; @@ -58,12 +58,13 @@ static int bplus = 0; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] (-x | [-p priority] [-d] [+rgb])\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] (-x | [-p priority] [-d] [+rgb])\n", + argv0); + exit(1); } @@ -81,47 +82,47 @@ void usage(void) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - default: - usage(); - } - else - switch (opt[1]) - { - case 'r': - if (rplus) - usage(); - rplus = 1; - break; - case 'g': - if (gplus) - usage(); - gplus = 1; - break; - case 'b': - if (bplus) - usage(); - bplus = 1; - break; - default: - usage(); - } - return 0; - (void) arg; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + default: + usage(); + } + } else { + switch (opt[1]) { + case 'r': + if (rplus) + usage(); + rplus = 1; + break; + case 'g': + if (gplus) + usage(); + gplus = 1; + break; + case 'b': + if (bplus) + usage(); + bplus = 1; + break; + default: + usage(); + } + } + return 0; + (void) arg; } @@ -134,13 +135,14 @@ int handle_opt(char* opt, char* arg) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - int q = xflag + (dflag | rplus | gplus | bplus); - if (argc || (q > 1) || (xflag && (prio != NULL))) - usage(); - return 0; - (void) argv; + int q = xflag + (dflag | rplus | gplus | bplus); + if (argc || q > 1 || (xflag && prio)) + usage(); + return 0; + (void) argv; } @@ -149,19 +151,19 @@ int handle_args(int argc, char* argv[], char* prio) * * @param filter The filter to fill */ -static void fill_filter(libcoopgamma_filter_t* restrict filter) +static void +fill_filter(libcoopgamma_filter_t *restrict filter) { - switch (filter->depth) - { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - libclut_negative(&(filter->ramps.MEMBER), MAX, TYPE, !rplus, !gplus, !bplus);\ - break; -LIST_DEPTHS + case CONST:\ + libclut_negative(&filter->ramps.MEMBER, MAX, TYPE, !rplus, !gplus, !bplus);\ + break; + LIST_DEPTHS #undef X - default: - abort(); - } + default: + abort(); + } } @@ -173,66 +175,65 @@ LIST_DEPTHS * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - - if (xflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - if (!xflag) - if ((r = make_slaves()) < 0) - return r; - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - if (!xflag) - fill_filter(&(crtc_updates[i].filter)); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - if (!dflag) - return 0; - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return -1; - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return -1; + int r; + size_t i, j; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + if (!xflag && (r = make_slaves()) < 0) + return r; + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + if (!xflag) + fill_filter(&crtc_updates[i].filter); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return -1; + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + if (!dflag) + return 0; + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return -1; + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return -1; + } + } + } + +enotrecoverable: + pause(); + return -1; } @@ -25,19 +25,20 @@ static libcoopgamma_filter_query_t query; /** * The class of the filter to print */ -static char* class = NULL; +static char *class = NULL; /** * Print usage information and exit */ -static void usage(void) +static void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-h high] [-l low] [-f class] -c crtc\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-h high] [-l low] [-f class] -c crtc\n", + argv0); + exit(1); } @@ -47,22 +48,23 @@ static void usage(void) * * @return Zero on success, -1 on error */ -static int initialise_proc(void) +static int +initialise_proc(void) { - sigset_t sigmask; - int sig; - - for (sig = 1; sig < _NSIG; sig++) - if (signal(sig, SIG_DFL) == SIG_ERR) - if (sig == SIGCHLD) - return -1; - - if (sigemptyset(&sigmask) < 0) - return -1; - if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0) - return -1; - - return 0; + sigset_t sigmask; + int sig; + + for (sig = 1; sig < _NSIG; sig++) + if (signal(sig, SIG_DFL) == SIG_ERR) + if (sig == SIGCHLD) + return -1; + + if (sigemptyset(&sigmask) < 0) + return -1; + if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0) + return -1; + + return 0; } @@ -72,21 +74,22 @@ static int initialise_proc(void) * * @return Zero on success, -1 on error */ -static int list_methods(void) +static int +list_methods(void) { - char** list; - size_t i; - - list = libcoopgamma_get_methods(); - if (list == NULL) - return -1; - for (i = 0; list[i]; i++) - printf("%s\n", list[i]); - free(list); - if (fflush(stdout) < 0) - return -1; - - return 0; + char **list; + size_t i; + + list = libcoopgamma_get_methods(); + if (!list) + return -1; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; } @@ -99,21 +102,22 @@ static int list_methods(void) * @return Zero on success, -1 on error, -2 * on libcoopgamma error */ -static int list_crtcs(void) +static int +list_crtcs(void) { - char** list; - size_t i; - - list = libcoopgamma_get_crtcs_sync(&cg); - if (list == NULL) - return -2; - for (i = 0; list[i]; i++) - printf("%s\n", list[i]); - free(list); - if (fflush(stdout) < 0) - return -1; - - return 0; + char **list; + size_t i; + + list = libcoopgamma_get_crtcs_sync(&cg); + if (!list) + return -2; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; } @@ -125,113 +129,108 @@ static int list_crtcs(void) * on libcoopgamma error, -3 on error * with error message already printed */ -static int print_info(void) +static int +print_info(void) { - libcoopgamma_crtc_info_t info; - libcoopgamma_filter_table_t table; - const char* str; - int saved_errno, ret = 0; - size_t i; - - if (libcoopgamma_crtc_info_initialise(&info) < 0) - return -1; - if (libcoopgamma_filter_table_initialise(&table) < 0) - { - saved_errno = errno; - libcoopgamma_crtc_info_destroy(&info); - errno = saved_errno; - return -1; - } - - if (libcoopgamma_get_gamma_info_sync(query.crtc, &info, &cg) < 0) - goto cg_fail; - - printf("Cooperative gamma server running: %s\n", - info.cooperative ? "yes" : "no"); - - printf("Gamma adjustments supported: %s\n", - info.supported == LIBCOOPGAMMA_MAYBE ? "maybe" : info.supported ? "yes" : "no"); - - printf("Gamma ramps stops (red green blue): %zu %zu %zu\n", - info.red_size, info.green_size, info.blue_size); - - switch (info.depth) - { - case LIBCOOPGAMMA_DOUBLE: str = "double-precision floating-point"; break; - case LIBCOOPGAMMA_FLOAT: str = "single-precision floating-point"; break; - case LIBCOOPGAMMA_UINT8: str = "unsigned 8-bit integer"; break; - case LIBCOOPGAMMA_UINT16: str = "unsigned 16-bit integer"; break; - case LIBCOOPGAMMA_UINT32: str = "unsigned 32-bit integer"; break; - case LIBCOOPGAMMA_UINT64: str = "unsigned 64-bit integer"; break; - default: - errno = EPROTO; - goto fail; - } - printf("Gamma ramps stops value type: %s\n", str); - - if (info.colourspace != LIBCOOPGAMMA_UNKNOWN) - { - switch (info.colourspace) - { - case LIBCOOPGAMMA_SRGB: str = "sRGB"; break; - case LIBCOOPGAMMA_RGB: str = "non-standard RGB"; break; - case LIBCOOPGAMMA_NON_RGB: str = "non-RGB multicolour"; break; - case LIBCOOPGAMMA_GREY: str = "monochrome or singlecolour scale"; break; + libcoopgamma_crtc_info_t info; + libcoopgamma_filter_table_t table; + const char *str; + int saved_errno, ret = 0; + size_t i; + + if (libcoopgamma_crtc_info_initialise(&info) < 0) + return -1; + if (libcoopgamma_filter_table_initialise(&table) < 0) { + saved_errno = errno; + libcoopgamma_crtc_info_destroy(&info); + errno = saved_errno; + return -1; + } + + if (libcoopgamma_get_gamma_info_sync(query.crtc, &info, &cg) < 0) + goto cg_fail; + + printf("Cooperative gamma server running: %s\n", + info.cooperative ? "yes" : "no"); + + printf("Gamma adjustments supported: %s\n", + info.supported == LIBCOOPGAMMA_MAYBE ? "maybe" : info.supported ? "yes" : "no"); + + printf("Gamma ramps stops (red green blue): %zu %zu %zu\n", + info.red_size, info.green_size, info.blue_size); + + switch (info.depth) { + case LIBCOOPGAMMA_DOUBLE: str = "double-precision floating-point"; break; + case LIBCOOPGAMMA_FLOAT: str = "single-precision floating-point"; break; + case LIBCOOPGAMMA_UINT8: str = "unsigned 8-bit integer"; break; + case LIBCOOPGAMMA_UINT16: str = "unsigned 16-bit integer"; break; + case LIBCOOPGAMMA_UINT32: str = "unsigned 32-bit integer"; break; + case LIBCOOPGAMMA_UINT64: str = "unsigned 64-bit integer"; break; default: - errno = EPROTO; - goto fail; + errno = EPROTO; + goto fail; + } + printf("Gamma ramps stops value type: %s\n", str); + + if (info.colourspace != LIBCOOPGAMMA_UNKNOWN) { + switch (info.colourspace) { + case LIBCOOPGAMMA_SRGB: str = "sRGB"; break; + case LIBCOOPGAMMA_RGB: str = "non-standard RGB"; break; + case LIBCOOPGAMMA_NON_RGB: str = "non-RGB multicolour"; break; + case LIBCOOPGAMMA_GREY: str = "monochrome or singlecolour scale"; break; + case LIBCOOPGAMMA_UNKNOWN: + default: + errno = EPROTO; + goto fail; + } + printf("Monitor's colourspace: %s\n", str); + } + + if (info.have_gamut) { + printf("Monitor's red colour (x, y): %lf, %lf\n", + info.red_x / (double)1024, info.red_y / (double)1024); + + printf("Monitor's green colour (x, y): %lf, %lf\n", + info.green_x / (double)1024, info.green_y / (double)1024); + + printf("Monitor's blue colour (x, y): %lf, %lf\n", + info.blue_x / (double)1024, info.blue_y / (double)1024); + + printf("Monitor's white point (x, y): %lf, %lf\n", + info.white_x / (double)1024, info.white_y / (double)1024); + } + + if (libcoopgamma_get_gamma_sync(&query, &table, &cg) < 0) + goto cg_fail; + + if (table.red_size != info.red_size || table.green_size != info.green_size || + table.blue_size != info.blue_size || table.depth != info.depth) { + fprintf(stderr, "%s: gamma ramp structure changed between queries\n", argv0); + goto custom_fail; + } + + printf("Filters: %zu\n", table.filter_count); + for (i = 0; i < table.filter_count; i++) { + printf(" Filter %zu:\n", i); + printf(" Priority: %" PRIi64 "\n", table.filters[i].priority); + printf(" Class: %s\n", table.filters[i].class); } - printf("Monitor's colourspace: %s\n", str); - } - - if (info.have_gamut) - { - printf("Monitor's red colour (x, y): %lf, %lf\n", - info.red_x / (double)1024, info.red_y / (double)1024); - - printf("Monitor's green colour (x, y): %lf, %lf\n", - info.green_x / (double)1024, info.green_y / (double)1024); - - printf("Monitor's blue colour (x, y): %lf, %lf\n", - info.blue_x / (double)1024, info.blue_y / (double)1024); - - printf("Monitor's white point (x, y): %lf, %lf\n", - info.white_x / (double)1024, info.white_y / (double)1024); - } - - if (libcoopgamma_get_gamma_sync(&query, &table, &cg) < 0) - goto cg_fail; - - if ((table.red_size != info.red_size) || (table.green_size != info.green_size) || - (table.blue_size != info.blue_size) || (table.depth != info.depth)) - { - fprintf(stderr, "%s: gamma ramp structure changed between queries\n", argv0); - goto custom_fail; - } - - printf("Filters: %zu\n", table.filter_count); - for (i = 0; i < table.filter_count; i++) - { - printf(" Filter %zu:\n", i); - printf(" Priority: %" PRIi64 "\n", table.filters[i].priority); - printf(" Class: %s\n", table.filters[i].class); - } - - done: - saved_errno = errno; - libcoopgamma_crtc_info_destroy(&info); - libcoopgamma_filter_table_destroy(&table); - errno = saved_errno; - return ret; - fail: - ret = -1; - goto done; - cg_fail: - ret = -2; - goto done; - custom_fail: - ret = -3; - goto done; + +done: + saved_errno = errno; + libcoopgamma_crtc_info_destroy(&info); + libcoopgamma_filter_table_destroy(&table); + errno = saved_errno; + return ret; +fail: + ret = -1; + goto done; +cg_fail: + ret = -2; + goto done; +custom_fail: + ret = -3; + goto done; } @@ -244,84 +243,85 @@ static int print_info(void) * on libcoopgamma error, -3 on error * with error message already printed */ -static int print_filter(void) +static int +print_filter(void) { - libcoopgamma_filter_table_t table; - libcoopgamma_ramps_t* restrict ramps; - int saved_errno, ret = 0; - size_t i, n; - - if (libcoopgamma_filter_table_initialise(&table) < 0) - return -1; - - if (libcoopgamma_get_gamma_sync(&query, &table, &cg) < 0) - goto cg_fail; - - if (query.coalesce) - i = 0; - else - for (i = 0; i < table.filter_count; i++) - if (!strcmp(table.filters[i].class, class)) - break; - if (i == table.filter_count) - { - fprintf(stderr, "%s: selected filter does not exist on selected CRTC\n", argv0); - goto custom_fail; - } - ramps = &(table.filters[i].ramps); - - n = table.red_size; - if (n < table.green_size) - n = table.green_size; - if (n < table.blue_size) - n = table.blue_size; - - switch (table.depth) - { -#define X(CONST, MEMBER, TYPE, FORMAT, DASH) \ - case CONST: \ - for (i = 0; i < n; i++) \ - { \ - if (i < ramps->MEMBER.red_size) \ - printf("%" FORMAT " ", (TYPE)(ramps->MEMBER.red[i])); \ - else \ - printf(DASH " "); \ - if (i < ramps->MEMBER.green_size) \ - printf("%" FORMAT " ", (TYPE)(ramps->MEMBER.green[i])); \ - else \ - printf(DASH " "); \ - if (i < ramps->MEMBER.blue_size) \ - printf("%" FORMAT "\n", (TYPE)(ramps->MEMBER.blue[i])); \ - else \ - printf(DASH "\n"); \ - } \ - break - X(LIBCOOPGAMMA_DOUBLE, d, double, "lf", "----"); - X(LIBCOOPGAMMA_FLOAT, f, double, "lf", "----"); - X(LIBCOOPGAMMA_UINT8, u8, uint8_t, "02" PRIx8, "--"); - X(LIBCOOPGAMMA_UINT16, u16, uint16_t, "04" PRIx16, "----"); - X(LIBCOOPGAMMA_UINT32, u32, uint32_t, "08" PRIx32, "--------"); - X(LIBCOOPGAMMA_UINT64, u64, uint64_t, "016" PRIx64, "----------------"); + libcoopgamma_filter_table_t table; + libcoopgamma_ramps_t *restrict ramps; + int saved_errno, ret = 0; + size_t i, n; + + if (libcoopgamma_filter_table_initialise(&table) < 0) + return -1; + + if (libcoopgamma_get_gamma_sync(&query, &table, &cg) < 0) + goto cg_fail; + + if (query.coalesce) { + i = 0; + } else { + for (i = 0; i < table.filter_count; i++) + if (!strcmp(table.filters[i].class, class)) + break; + } + if (i == table.filter_count) { + fprintf(stderr, "%s: selected filter does not exist on selected CRTC\n", argv0); + goto custom_fail; + } + ramps = &table.filters[i].ramps; + + n = table.red_size; + if (n < table.green_size) + n = table.green_size; + if (n < table.blue_size) + n = table.blue_size; + + switch (table.depth) { +#define X(CONST, MEMBER, TYPE, FORMAT, DASH)\ + case CONST:\ + for (i = 0; i < n; i++) {\ + if (i < ramps->MEMBER.red_size)\ + printf("%" FORMAT " ", (TYPE)(ramps->MEMBER.red[i]));\ + else\ + printf(DASH " ");\ + \ + if (i < ramps->MEMBER.green_size)\ + printf("%" FORMAT " ", (TYPE)(ramps->MEMBER.green[i]));\ + else\ + printf(DASH " ");\ + \ + if (i < ramps->MEMBER.blue_size)\ + printf("%" FORMAT "\n", (TYPE)(ramps->MEMBER.blue[i]));\ + else\ + printf(DASH "\n");\ + } \ + break + X(LIBCOOPGAMMA_DOUBLE, d, double, "lf", "----"); + X(LIBCOOPGAMMA_FLOAT, f, double, "lf", "----"); + X(LIBCOOPGAMMA_UINT8, u8, uint8_t, "02" PRIx8, "--"); + X(LIBCOOPGAMMA_UINT16, u16, uint16_t, "04" PRIx16, "----"); + X(LIBCOOPGAMMA_UINT32, u32, uint32_t, "08" PRIx32, "--------"); + X(LIBCOOPGAMMA_UINT64, u64, uint64_t, "016" PRIx64, "----------------"); #undef X - default: - errno = EPROTO; - goto fail; - } - - done: - saved_errno = errno; - libcoopgamma_filter_table_destroy(&table); - errno = saved_errno; - return ret; - fail: - ret = -1; - goto done; - cg_fail: - ret = -2; - goto done; - custom_fail: - ret = -3; - goto done; + default: + errno = EPROTO; + goto fail; + } + +done: + saved_errno = errno; + libcoopgamma_filter_table_destroy(&table); + errno = saved_errno; + return ret; +fail: + ret = -1; + goto done; +cg_fail: + ret = -2; + goto done; +custom_fail: + ret = -3; + goto done; } @@ -352,146 +352,142 @@ static int print_filter(void) * @param argv The command line arguments * @return 0 on success, 1 on error */ -int main(int argc, char* argv[]) +int +main(int argc, char *argv[]) { - int stage = 0, haveh = 0, havel = 0; - int rc = 0; - char* method = NULL; - char* site = NULL; - - query.high_priority = INT64_MAX; - query.low_priority = INT64_MIN; - query.crtc = NULL; - query.coalesce = 0; - - ARGBEGIN - { - case 'M': - if (method != NULL) - usage(); - method = EARGF(usage()); - break; - case 'S': - if (site != NULL) - usage(); - site = EARGF(usage()); - break; - case 'c': - if (query.crtc != NULL) - usage(); - query.crtc = EARGF(usage()); - break; - case 'h': - if (haveh++) - usage(); - query.high_priority = (int64_t)atoll(EARGF(usage())); - break; - case 'l': - if (havel++) - usage(); - query.low_priority = (int64_t)atoll(EARGF(usage())); - break; - case 'f': - if (class != NULL) - usage(); - class = EARGF(usage()); - if ((class[0] == '*') && (class[1] == '\0')) - query.coalesce = 1; - break; - default: - usage(); - } - ARGEND; - - if (argc) - usage(); - - if (initialise_proc() < 0) - goto fail; - - if ((method != NULL) && !strcmp(method, "?")) - { - if ((site != NULL) || (query.crtc != NULL)) - usage(); - if (list_methods() < 0) - goto fail; - return 0; - } - - if (libcoopgamma_context_initialise(&cg) < 0) - goto fail; - stage++; - if (libcoopgamma_connect(method, site, &cg) < 0) - { - fprintf(stderr, "%s: server failed to initialise\n", argv0); - goto custom_fail; - } - stage++; - - if (!(query.crtc)) - usage(); - - if (!strcmp(query.crtc, "?")) - switch (list_crtcs()) - { - case 0: + int stage = 0, haveh = 0, havel = 0; + int rc = 0; + char *method = NULL; + char *site = NULL; + const char *side; + + query.high_priority = INT64_MAX; + query.low_priority = INT64_MIN; + query.crtc = NULL; + query.coalesce = 0; + + ARGBEGIN { + case 'M': + if (method) + usage(); + method = EARGF(usage()); + break; + case 'S': + if (site) + usage(); + site = EARGF(usage()); + break; + case 'c': + if (query.crtc) + usage(); + query.crtc = EARGF(usage()); + break; + case 'h': + if (haveh++) + usage(); + query.high_priority = (int64_t)atoll(EARGF(usage())); + break; + case 'l': + if (havel++) + usage(); + query.low_priority = (int64_t)atoll(EARGF(usage())); + break; + case 'f': + if (class) + usage(); + class = EARGF(usage()); + if (class[0] == '*' && class[1] == '\0') + query.coalesce = 1; + break; + default: + usage(); + } + ARGEND; + + if (argc) + usage(); + + if (initialise_proc() < 0) + goto fail; + + if (method && !strcmp(method, "?")) { + if (site || query.crtc) + usage(); + if (list_methods() < 0) + goto fail; + return 0; + } + + if (libcoopgamma_context_initialise(&cg) < 0) + goto fail; + stage++; + if (libcoopgamma_connect(method, site, &cg) < 0) { + fprintf(stderr, "%s: server failed to initialise\n", argv0); + goto custom_fail; + } + stage++; + + if (!query.crtc) + usage(); + + if (!strcmp(query.crtc, "?")) { + switch (list_crtcs()) { + case 0: + goto done; + case -1: + goto fail; + default: + goto cg_fail; + } + } + + switch (class ? print_filter() : print_info()) { + case 0: + goto done; + case -1: + goto fail; + case -2: + goto cg_fail; + default: + goto custom_fail; + } + + fflush(stdout); + if (ferror(stdout)) + goto fail; + if (fclose(stdout) < 0) + goto fail; + +done: + if (stage >= 1) + libcoopgamma_context_destroy(&cg, stage >= 2); + return rc; + +custom_fail: + rc = 1; + goto done; + +fail: + rc = 1; + perror(argv0); + goto done; + +cg_fail: + rc = 1; + side = cg.error.server_side ? "server" : "client"; + if (cg.error.custom) { + if (cg.error.number || cg.error.description) { + fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n", + argv0, side, cg.error.number, cg.error.description); + } else if (cg.error.number) { + fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number); + } else if (cg.error.description) { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + } + } else if (cg.error.description) { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + } else { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror((int)cg.error.number)); + } goto done; - case -1: - goto fail; - default: - goto cg_fail; - } - - switch (class ? print_filter() : print_info()) - { - case 0: - goto done; - case -1: - goto fail; - case -2: - goto cg_fail; - default: - goto custom_fail; - } - - fflush(stdout); - if (ferror(stdout)) - goto fail; - if (fclose(stdout) < 0) - goto fail; - - done: - if (stage >= 1) - libcoopgamma_context_destroy(&cg, stage >= 2); - return rc; - - custom_fail: - rc = 1; - goto done; - - fail: - rc = 1; - perror(argv0); - goto done; - - cg_fail: - rc = 1; - { - const char* side = cg.error.server_side ? "server" : "client"; - if (cg.error.custom) - { - if ((cg.error.number != 0) || (cg.error.description != NULL)) - fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n", - argv0, side, cg.error.number, cg.error.description); - else if (cg.error.number != 0) - fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number); - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); - } - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); - else - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror(cg.error.number)); - } - goto done; } diff --git a/cg-rainbow.c b/cg-rainbow.c index 4cf4797..07ee4a5 100644 --- a/cg-rainbow.c +++ b/cg-rainbow.c @@ -27,19 +27,19 @@ char default_class[] = PKGNAME "::cg-rainbow::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; /** * -s: rainbow-frequency in Hz */ -static char* sflag = NULL; +static char *sflag = NULL; /** * -l: base luminosity */ -static char* lflag = NULL; +static char *lflag = NULL; /** * The rainbow-frequency multiplied by 3 @@ -56,13 +56,14 @@ double luminosity = (double)1 / 3; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] [-p priority]" - " [-l luminosity] [-s rainbowhz]\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] [-p priority]" + " [-l luminosity] [-s rainbowhz]\n", + argv0); + exit(1); } @@ -80,25 +81,26 @@ void usage(void) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'l': - if (lflag || !(lflag = arg)) - usage(); - return 1; - case 's': - if (sflag || !(sflag = arg)) - usage(); - return 1; - default: - usage(); - } - else - usage(); - return 0; + if (opt[0] == '-') { + switch (opt[1]) { + case 'l': + if (lflag || !(lflag = arg)) + usage(); + return 1; + case 's': + if (sflag || !(sflag = arg)) + usage(); + return 1; + default: + usage(); + } + } else { + usage(); + } + return 0; } @@ -109,16 +111,17 @@ int handle_opt(char* opt, char* arg) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_double(double* restrict out, const char* restrict str) +static int +parse_double(double *restrict out, const char *restrict str) { - char* end; - errno = 0; - *out = strtod(str, &end); - if (errno || (*out < 0) || isinf(*out) || isnan(*out) || *end) - return -1; - if (!*str || !strchr("0123456789.", *str)) - return -1; - return 0; + char *end; + errno = 0; + *out = strtod(str, &end); + if (errno || *out < 0 || isinf(*out) || isnan(*out) || *end) + return -1; + if (!*str || !strchr("0123456789.", *str)) + return -1; + return 0; } @@ -131,25 +134,24 @@ static int parse_double(double* restrict out, const char* restrict str) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - int q = (lflag || sflag); - if ((q > 1) || argc) - usage(); - if (sflag != NULL) - { - if (parse_double(&rainbows_per_third_second, sflag) < 0) - usage(); - rainbows_per_third_second *= 3; - } - if (lflag != NULL) - { - if (parse_double(&luminosity, lflag) < 0) - usage(); - } - return 0; - (void) argv; - (void) prio; + int q = (lflag || sflag); + if (q > 1 || argc) + usage(); + if (sflag) { + if (parse_double(&rainbows_per_third_second, sflag) < 0) + usage(); + rainbows_per_third_second *= 3; + } + if (lflag) { + if (parse_double(&luminosity, lflag) < 0) + usage(); + } + return 0; + (void) argv; + (void) prio; } @@ -161,20 +163,20 @@ int handle_args(int argc, char* argv[], char* prio) * @param green The green brightness * @param blue The blue brightness */ -static void fill_filter(libcoopgamma_filter_t* restrict filter, double red, double green, double blue) +static void +fill_filter(libcoopgamma_filter_t *restrict filter, double red, double green, double blue) { - switch (filter->depth) - { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - libclut_start_over(&(filter->ramps.MEMBER), MAX, TYPE, 1, 1, 1);\ - libclut_rgb_brightness(&(filter->ramps.MEMBER), MAX, TYPE, red, green, blue);\ - break; -LIST_DEPTHS + case CONST:\ + libclut_start_over(&filter->ramps.MEMBER, MAX, TYPE, 1, 1, 1);\ + libclut_rgb_brightness(&filter->ramps.MEMBER, MAX, TYPE, red, green, blue);\ + break; + LIST_DEPTHS #undef X - default: - abort(); - } + default: + abort(); + } } @@ -184,18 +186,19 @@ LIST_DEPTHS * @param now Output parameter for the current time (monotonic) * @return Zero on success, -1 on error */ -static int double_time(double* restrict now) +static int +double_time(double *restrict now) { #ifndef CLOCK_MONOTONIC_RAW # define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC #endif - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) < 0) - return -1; - *now = (double)(ts.tv_nsec); - *now /= 1000000000L; - *now += (double)(ts.tv_sec); - return 0; + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) < 0) + return -1; + *now = (double)(ts.tv_nsec); + *now /= 1000000000L; + *now += (double)(ts.tv_sec); + return 0; } @@ -207,56 +210,58 @@ static int double_time(double* restrict now) * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - double pal[3]; - double t, starttime; - - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - - if ((r = make_slaves()) < 0) - return r; - - if ((r = double_time(&starttime)) < 0) - return r; - - for (;;) - { - if ((r = double_time(&t)) < 0) - return r; - t -= starttime; - t *= rainbows_per_third_second; - pal[0] = pal[1] = pal[2] = luminosity; - pal[((long)t) % 3] += 1 - fmod(t, 1); - pal[((long)t + 1) % 3] += fmod(t, 1); - if (pal[0] > 1) pal[0] = 1; - if (pal[1] > 1) pal[1] = 1; - if (pal[2] > 1) pal[2] = 1; - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - fill_filter(&(crtc_updates[i].filter), pal[0], pal[1], pal[2]); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } + int r; + size_t i, j; + double pal[3]; + double t, starttime; + + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + + if ((r = make_slaves()) < 0) + return r; + + if ((r = double_time(&starttime)) < 0) + return r; + + for (;;) { + if ((r = double_time(&t)) < 0) + return r; + t -= starttime; + t *= rainbows_per_third_second; + pal[0] = pal[1] = pal[2] = luminosity; + pal[((long)t) % 3] += 1 - fmod(t, 1); + pal[((long)t + 1) % 3] += fmod(t, 1); + if (pal[0] > 1) + pal[0] = 1; + if (pal[1] > 1) + pal[1] = 1; + if (pal[2] > 1) + pal[2] = 1; + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + fill_filter(&crtc_updates[i].filter, pal[0], pal[1], pal[2]); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } + } + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + sched_yield(); } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - sched_yield(); - } } diff --git a/cg-remove.c b/cg-remove.c index 0e81df0..20ca51e 100644 --- a/cg-remove.c +++ b/cg-remove.c @@ -23,12 +23,13 @@ static libcoopgamma_context_t cg; /** * Print usage information and exit */ -static void usage(void) +static void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... class...\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... class...\n", + argv0); + exit(1); } @@ -38,22 +39,23 @@ static void usage(void) * * @return Zero on success, -1 on error */ -static int initialise_proc(void) +static int +initialise_proc(void) { - sigset_t sigmask; - int sig; - - for (sig = 1; sig < _NSIG; sig++) - if (signal(sig, SIG_DFL) == SIG_ERR) - if (sig == SIGCHLD) - return -1; - - if (sigemptyset(&sigmask) < 0) - return -1; - if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0) - return -1; - - return 0; + sigset_t sigmask; + int sig; + + for (sig = 1; sig < _NSIG; sig++) + if (signal(sig, SIG_DFL) == SIG_ERR) + if (sig == SIGCHLD) + return -1; + + if (sigemptyset(&sigmask) < 0) + return -1; + if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0) + return -1; + + return 0; } @@ -63,21 +65,22 @@ static int initialise_proc(void) * * @return Zero on success, -1 on error */ -static int list_methods(void) +static int +list_methods(void) { - char** list; - size_t i; - - list = libcoopgamma_get_methods(); - if (list == NULL) - return -1; - for (i = 0; list[i]; i++) - printf("%s\n", list[i]); - free(list); - if (fflush(stdout) < 0) - return -1; - - return 0; + char **list; + size_t i; + + list = libcoopgamma_get_methods(); + if (!list) + return -1; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; } @@ -90,21 +93,22 @@ static int list_methods(void) * @return Zero on success, -1 on error, -2 * on libcoopgamma error */ -static int list_crtcs(void) +static int +list_crtcs(void) { - char** list; - size_t i; - - list = libcoopgamma_get_crtcs_sync(&cg); - if (list == NULL) - return -2; - for (i = 0; list[i]; i++) - printf("%s\n", list[i]); - free(list); - if (fflush(stdout) < 0) - return -1; - - return 0; + char **list; + size_t i; + + list = libcoopgamma_get_crtcs_sync(&cg); + if (!list) + return -2; + for (i = 0; list[i]; i++) + printf("%s\n", list[i]); + free(list); + if (fflush(stdout) < 0) + return -1; + + return 0; } @@ -116,123 +120,119 @@ static int list_crtcs(void) * @return Zero on success, -1 on error, -2 on * libcoopgamma error */ -static int remove_filters(char* const* restrict crtcs, char* const* restrict classes) +static int +remove_filters(char *const *restrict crtcs, char *const *restrict classes) { - size_t n = 0, unsynced = 0, selected, i, j; - char* synced = NULL; - libcoopgamma_async_context_t* asyncs = NULL; - int saved_errno, need_flush = 0, ret = 0; - struct pollfd pollfd; - libcoopgamma_filter_t command; - - for (i = 0; crtcs[i] != NULL; i++); - for (j = 0; classes[j] != NULL; j++); - synced = calloc(i, j * sizeof(*synced)); - if (synced == NULL) - goto fail; - asyncs = calloc(i, j * sizeof(*asyncs)); - if (asyncs == NULL) - goto fail; - - i = j = 0; - command.lifespan = LIBCOOPGAMMA_REMOVE; - pollfd.fd = cg.fd; - pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; - - while ((unsynced > 0) || (crtcs[i] != NULL)) - { - wait: - if (crtcs[i] != NULL) - pollfd.events |= POLLOUT; - else - pollfd.events &= ~POLLOUT; - - pollfd.revents = 0; - if (poll(&pollfd, (nfds_t)1, -1) < 0) - goto fail; - - if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) - { - if (need_flush && (libcoopgamma_flush(&cg) < 0)) - goto send_fail; - need_flush = 0; - for (; crtcs[i] != NULL; i++, j = 0) - { - command.crtc = crtcs[i]; - while (classes[j] != NULL) - { - command.class = classes[j++]; - if (unsynced++, libcoopgamma_set_gamma_send(&command, &cg, asyncs + n++) < 0) - goto send_fail; - } - } - goto send_done; - send_fail: - switch (errno) - { - case EINTR: - case EAGAIN: + size_t n = 0, unsynced = 0, selected, i, j; + char *synced = NULL; + libcoopgamma_async_context_t *asyncs = NULL; + int saved_errno, need_flush = 0, ret = 0; + struct pollfd pollfd; + libcoopgamma_filter_t command; + + for (i = 0; crtcs[i]; i++); + for (j = 0; classes[j]; j++); + synced = calloc(i, j * sizeof(*synced)); + if (!synced) + goto fail; + asyncs = calloc(i, j * sizeof(*asyncs)); + if (!asyncs) + goto fail; + + i = j = 0; + command.lifespan = LIBCOOPGAMMA_REMOVE; + pollfd.fd = cg.fd; + pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; + + while (unsynced > 0 || crtcs[i]) { + wait: + if (crtcs[i]) + pollfd.events |= POLLOUT; + else + pollfd.events &= ~POLLOUT; + + pollfd.revents = 0; + if (poll(&pollfd, (nfds_t)1, -1) < 0) + goto fail; + + if (pollfd.revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL)) { + if (need_flush && (libcoopgamma_flush(&cg) < 0)) + goto send_fail; + need_flush = 0; + for (; crtcs[i]; i++, j = 0) { + command.crtc = crtcs[i]; + while (classes[j]) { + command.class = classes[j++]; + unsynced++; + if (libcoopgamma_set_gamma_send(&command, &cg, asyncs + n++) < 0) + goto send_fail; + } + } + goto send_done; + send_fail: + switch (errno) { + case EINTR: + case EAGAIN: #if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif - need_flush = 1; - if (classes[j] == NULL) - i++, j = 0; - break; - default: - goto fail; - } - } - send_done: - - if ((unsynced == 0) && (crtcs[i] == NULL)) - break; - - if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) - while (unsynced > 0) - switch (libcoopgamma_synchronise(&cg, asyncs, n, &selected)) - { - case 0: - if (synced[selected]) - { - libcoopgamma_skip_message(&cg); - break; + need_flush = 1; + if (!classes[j]) + i++, j = 0; + break; + default: + goto fail; + } } - synced[selected] = 1; - unsynced -= 1; - if (libcoopgamma_set_gamma_recv(&cg, asyncs + selected) < 0) - goto cg_fail; - break; - default: - switch (errno) - { - case 0: - break; - case EINTR: - case EAGAIN: + send_done: + + if (!unsynced && !crtcs[i]) + break; + + if (pollfd.revents & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { + while (unsynced > 0) { + switch (libcoopgamma_synchronise(&cg, asyncs, n, &selected)) { + case 0: + if (synced[selected]) { + libcoopgamma_skip_message(&cg); + break; + } + synced[selected] = 1; + unsynced -= 1; + if (libcoopgamma_set_gamma_recv(&cg, asyncs + selected) < 0) + goto cg_fail; + break; + default: + switch (errno) { + case 0: + break; + case EINTR: + case EAGAIN: #if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif - goto wait; - default: - goto fail; + goto wait; + default: + goto fail; + } + break; + } + } } - break; - } - } - - done: - saved_errno = errno; - free(synced); - free(asyncs); - errno = saved_errno; - return ret; - fail: - ret = -1; - goto done; - cg_fail: - ret = -2; - goto done; + } + +done: + saved_errno = errno; + free(synced); + free(asyncs); + errno = saved_errno; + return ret; +fail: + ret = -1; + goto done; +cg_fail: + ret = -2; + goto done; } @@ -255,132 +255,128 @@ static int remove_filters(char* const* restrict crtcs, char* const* restrict cla * @param argv The command line arguments * @return 0 on success, 1 on error */ -int main(int argc, char* argv[]) +int +main(int argc, char *argv[]) { - int stage = 0; - int rc = 0; - char* method = NULL; - char* site = NULL; - char** crtcs_ = NULL; - char** crtcs = alloca(argc * sizeof(char*)); - size_t i, crtcs_n = 0; - - ARGBEGIN - { - case 'M': - if (method != NULL) - usage(); - method = EARGF(usage()); - break; - case 'S': - if (site != NULL) - usage(); - site = EARGF(usage()); - break; - case 'c': - crtcs[crtcs_n++] = EARGF(usage()); - break; - default: - usage(); - } - ARGEND; - - if (initialise_proc() < 0) - goto fail; - - if ((method != NULL) && !strcmp(method, "?")) - { - if ((site != NULL) || (crtcs_n > 0) || (argc > 0)) - usage(); - if (list_methods() < 0) - goto fail; - return 0; - } - - if (libcoopgamma_context_initialise(&cg) < 0) - goto fail; - stage++; - if (libcoopgamma_connect(method, site, &cg) < 0) - { - fprintf(stderr, "%s: server failed to initialise\n", argv0); - goto custom_fail; - } - stage++; - - for (i = 0; i < crtcs_n; i++) - if (!strcmp(crtcs[i], "?")) - { - if (argc > 0) - usage(); - switch (list_crtcs()) - { - case 0: - goto done; - case -1: - goto fail; - default: - goto cg_fail; - } - } - - if (argc == 0) - usage(); - - if (crtcs_n == 0) - { - crtcs = crtcs_ = libcoopgamma_get_crtcs_sync(&cg); - if (crtcs == NULL) - goto cg_fail; - } - else - crtcs[crtcs_n] = NULL; - - if (libcoopgamma_set_nonblocking(&cg, 1) < 0) - goto fail; - - switch (remove_filters(crtcs, argv)) - { - case 0: - break; - case -1: - goto fail; - default: - goto cg_fail; - } - - done: - if (stage >= 1) - libcoopgamma_context_destroy(&cg, stage >= 2); - free(crtcs_); - return rc; - - custom_fail: - rc = 1; - goto done; - - fail: - rc = 1; - perror(argv0); - goto done; + int stage = 0; + int rc = 0; + char *method = NULL; + char *site = NULL; + char **crtcs_ = NULL; + char **crtcs; + size_t i, crtcs_n = 0; + const char *side; + + crtcs = alloca((size_t)argc * sizeof(char *)); + + ARGBEGIN { + case 'M': + if (method) + usage(); + method = EARGF(usage()); + break; + case 'S': + if (site) + usage(); + site = EARGF(usage()); + break; + case 'c': + crtcs[crtcs_n++] = EARGF(usage()); + break; + default: + usage(); + } + ARGEND; + + if (initialise_proc() < 0) + goto fail; + + if (method && !strcmp(method, "?")) { + if (site || crtcs_n > 0 || argc > 0) + usage(); + if (list_methods() < 0) + goto fail; + return 0; + } + + if (libcoopgamma_context_initialise(&cg) < 0) + goto fail; + stage++; + if (libcoopgamma_connect(method, site, &cg) < 0) { + fprintf(stderr, "%s: server failed to initialise\n", argv0); + goto custom_fail; + } + stage++; + + for (i = 0; i < crtcs_n; i++) { + if (!strcmp(crtcs[i], "?")) { + if (argc > 0) + usage(); + switch (list_crtcs()) + { + case 0: + goto done; + case -1: + goto fail; + default: + goto cg_fail; + } + } + } + + if (!argc) + usage(); - cg_fail: - rc = 1; - { - const char* side = cg.error.server_side ? "server" : "client"; - if (cg.error.custom) - { - if ((cg.error.number != 0) || (cg.error.description != NULL)) - fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n", - argv0, side, cg.error.number, cg.error.description); - else if (cg.error.number != 0) - fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number); - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); - } - else if (cg.error.description != NULL) - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); - else - fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror(cg.error.number)); - } - goto done; + if (!crtcs_n) { + crtcs = crtcs_ = libcoopgamma_get_crtcs_sync(&cg); + if (!crtcs) + goto cg_fail; + } else { + crtcs[crtcs_n] = NULL; + } + + if (libcoopgamma_set_nonblocking(&cg, 1) < 0) + goto fail; + + switch (remove_filters(crtcs, argv)) { + case 0: + break; + case -1: + goto fail; + default: + goto cg_fail; + } + +done: + if (stage >= 1) + libcoopgamma_context_destroy(&cg, stage >= 2); + free(crtcs_); + return rc; + +custom_fail: + rc = 1; + goto done; + +fail: + rc = 1; + perror(argv0); + goto done; + +cg_fail: + rc = 1; + side = cg.error.server_side ? "server" : "client"; + if (cg.error.custom) { + if (cg.error.number || cg.error.description) + fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n", + argv0, side, cg.error.number, cg.error.description); + else if (cg.error.number) + fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number); + else if (cg.error.description) + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + } else if (cg.error.description) { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description); + } else { + fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror((int)cg.error.number)); + } + goto done; } diff --git a/cg-shallow.c b/cg-shallow.c index 5b65364..2ddd45d 100644 --- a/cg-shallow.c +++ b/cg-shallow.c @@ -25,7 +25,7 @@ char default_class[] = PKGNAME "::cg-shallow::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; @@ -58,13 +58,14 @@ static size_t bres = 2; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " - "(-x | [-p priority] [-d] [all | red green blue])\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] " + "(-x | [-p priority] [-d] [all | red green blue])\n", + argv0); + exit(1); } @@ -82,28 +83,29 @@ void usage(void) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'd': - if (dflag || xflag) - usage(); - dflag = 1; - break; - case 'x': - if (xflag || dflag) - usage(); - xflag = 1; - break; - default: - usage(); - } - else - usage(); - return 0; - (void) arg; + if (opt[0] == '-') { + switch (opt[1]) { + case 'd': + if (dflag || xflag) + usage(); + dflag = 1; + break; + case 'x': + if (xflag || dflag) + usage(); + xflag = 1; + break; + default: + usage(); + } + } else { + usage(); + } + return 0; + (void) arg; } @@ -114,16 +116,17 @@ int handle_opt(char* opt, char* arg) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_int(size_t* restrict out, const char* restrict str) +static int +parse_int(size_t *restrict out, const char *restrict str) { - char* end; - errno = 0; - if (!isdigit(*str)) - return -1; - *out = strtoul(str, &end, 10); - if (errno || *end) - return -1; - return 0; + char *end; + errno = 0; + if (!isdigit(*str)) + return -1; + *out = strtoul(str, &end, 10); + if (errno || *end) + return -1; + return 0; } @@ -136,35 +139,34 @@ static int parse_int(size_t* restrict out, const char* restrict str) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - char* red = NULL; - char* green = NULL; - char* blue = NULL; - int q = xflag + (dflag | (argc > 0)); - if ((q > 1) || (xflag && (prio != NULL))) - usage(); - if (argc == 1) - red = green = blue = argv[0]; - else if (argc == 3) - { - red = argv[0]; - green = argv[1]; - blue = argv[2]; - } - else if (argc && !xflag) - usage(); - if (argc) - { - if (parse_int(&rres, red) < 0) - usage(); - if (parse_int(&gres, blue) < 0) - usage(); - if (parse_int(&bres, green) < 0) - usage(); - } - return 0; - (void) argv; + char *red = NULL; + char *green = NULL; + char *blue = NULL; + int q = xflag + (dflag | (argc > 0)); + if (q > 1 || (xflag && prio)) + usage(); + if (argc == 1) { + red = green = blue = argv[0]; + } else if (argc == 3) { + red = argv[0]; + green = argv[1]; + blue = argv[2]; + } else if (argc && !xflag) { + usage(); + } + if (argc) { + if (parse_int(&rres, red) < 0) + usage(); + if (parse_int(&gres, blue) < 0) + usage(); + if (parse_int(&bres, green) < 0) + usage(); + } + return 0; + (void) argv; } @@ -173,19 +175,19 @@ int handle_args(int argc, char* argv[], char* prio) * * @param filter The filter to fill */ -static void fill_filter(libcoopgamma_filter_t* restrict filter) +static void +fill_filter(libcoopgamma_filter_t *restrict filter) { - switch (filter->depth) - { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - libclut_lower_resolution(&(filter->ramps.MEMBER), MAX, TYPE, 0, rres, 0, gres, 0, bres);\ - break; -LIST_DEPTHS + case CONST:\ + libclut_lower_resolution(&filter->ramps.MEMBER, MAX, TYPE, 0, rres, 0, gres, 0, bres);\ + break; + LIST_DEPTHS #undef X - default: - abort(); - } + default: + abort(); + } } @@ -197,66 +199,65 @@ LIST_DEPTHS * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r; - size_t i, j; - - if (xflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; - else if (dflag) - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - else - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; - - if (!xflag) - if ((r = make_slaves()) < 0) - return r; - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - if (!xflag) - fill_filter(&(crtc_updates[i].filter)); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - if (!dflag) - return 0; - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return -1; - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return -1; + int r; + size_t i, j; + + if (xflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_REMOVE; + else if (dflag) + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + else + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_REMOVAL; + + if (!xflag && (r = make_slaves()) < 0) + return r; + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + if (!xflag) + fill_filter(&crtc_updates[i].filter); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } } - - enotrecoverable: - for (;;) - if (pause() < 0) - return -1; + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + if (!dflag) + return 0; + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return -1; + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return -1; + } + } + } + +enotrecoverable: + pause(); + return -1; } diff --git a/cg-sleepmode.c b/cg-sleepmode.c index 67b5570..df50ffc 100644 --- a/cg-sleepmode.c +++ b/cg-sleepmode.c @@ -30,24 +30,24 @@ char default_class[] = PKGNAME "::cg-sleepmode::standard"; /** * Class suffixes */ -const char* const* class_suffixes = (const char* const[]){NULL}; +const char *const *class_suffixes = (const char *const[]){NULL}; /** * -r: fade-out time for the red channel */ -static char* rflag; +static char *rflag; /** * -g: fade-out time for the green channel */ -static char* gflag; +static char *gflag; /** * -b: fade-out time for the blue channel */ -static char* bflag; +static char *bflag; /** * The duration, in seconds, of the red channel's fade out @@ -89,14 +89,15 @@ static volatile sig_atomic_t received_int = 0; /** * Print usage information and exit */ -void usage(void) +void +usage(void) { - fprintf(stderr, - "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] [-p priority] " - "[-r red-fadeout-time] [-g green-fadeout-time] [-b blue-fadeout-time] " - "[red-luminosity [green-luminosity [blue-luminosity]]]\n", - argv0); - exit(1); + fprintf(stderr, + "usage: %s [-M method] [-S site] [-c crtc]... [-R rule] [-p priority] " + "[-r red-fadeout-time] [-g green-fadeout-time] [-b blue-fadeout-time] " + "[red-luminosity [green-luminosity [blue-luminosity]]]\n", + argv0); + exit(1); } @@ -106,10 +107,11 @@ void usage(void) * * @param signo The received signal */ -static void sig_int(int signo) +static void +sig_int(int signo) { - received_int = 1; - (void) signo; + received_int = 1; + (void) signo; } @@ -127,29 +129,30 @@ static void sig_int(int signo) * 1 if `arg` was used, * -1 on error */ -int handle_opt(char* opt, char* arg) +int +handle_opt(char *opt, char *arg) { - if (opt[0] == '-') - switch (opt[1]) - { - case 'r': - if (rflag || !(rflag = arg)) - usage(); - return 1; - case 'g': - if (gflag || !(gflag = arg)) - usage(); - return 1; - case 'b': - if (bflag || !(bflag = arg)) - usage(); - return 1; - default: - usage(); - } - else - usage(); - return 0; + if (opt[0] == '-') { + switch (opt[1]) { + case 'r': + if (rflag || !(rflag = arg)) + usage(); + return 1; + case 'g': + if (gflag || !(gflag = arg)) + usage(); + return 1; + case 'b': + if (bflag || !(bflag = arg)) + usage(); + return 1; + default: + usage(); + } + } else { + usage(); + } + return 0; } @@ -160,16 +163,17 @@ int handle_opt(char* opt, char* arg) * @param str The string * @return Zero on success, -1 if the string is invalid */ -static int parse_double(double* restrict out, const char* restrict str) +static int +parse_double(double *restrict out, const char *restrict str) { - char* end; - errno = 0; - *out = strtod(str, &end); - if (errno || (*out < 0) || isinf(*out) || isnan(*out) || *end) - return -1; - if (!*str || !strchr("0123456789.", *str)) - return -1; - return 0; + char *end; + errno = 0; + *out = strtod(str, &end); + if (errno || *out < 0 || isinf(*out) || isnan(*out) || *end) + return -1; + if (!*str || !strchr("0123456789.", *str)) + return -1; + return 0; } @@ -182,37 +186,32 @@ static int parse_double(double* restrict out, const char* restrict str) * @param prio The argument associated with the "-p" option * @return Zero on success, -1 on error */ -int handle_args(int argc, char* argv[], char* prio) +int +handle_args(int argc, char *argv[], char *prio) { - int q = (rflag || gflag || bflag || argc); - if ((q > 1) || (argc > 3)) - usage(); - if (rflag != NULL) - if (parse_double(&red_time, rflag) < 0) - usage(); - if (gflag != NULL) - if (parse_double(&green_time, gflag) < 0) - usage(); - if (bflag != NULL) - if (parse_double(&blue_time, bflag) < 0) - usage(); - if (argc >= 1) - if (parse_double(&red_target, argv[0]) < 0) - usage(); - if (argc >= 2) - if (parse_double(&green_target, argv[1]) < 0) - usage(); - if (argc >= 3) - if (parse_double(&blue_target, argv[2]) < 0) - usage(); - if (red_target >= 1) - red_time = 0; - if (green_target >= 1) - green_time = 0; - if (blue_target >= 1) - blue_time = 0; - return 0; - (void) prio; + int q = (rflag || gflag || bflag || argc); + if (q > 1 || argc > 3) + usage(); + if (rflag && parse_double(&red_time, rflag) < 0) + usage(); + if (gflag && parse_double(&green_time, gflag) < 0) + usage(); + if (bflag && parse_double(&blue_time, bflag) < 0) + usage(); + if (argc >= 1 && parse_double(&red_target, argv[0]) < 0) + usage(); + if (argc >= 2 && parse_double(&green_target, argv[1]) < 0) + usage(); + if (argc >= 3 && parse_double(&blue_target, argv[2]) < 0) + usage(); + if (red_target >= 1) + red_time = 0; + if (green_target >= 1) + green_time = 0; + if (blue_target >= 1) + blue_time = 0; + return 0; + (void) prio; } @@ -224,20 +223,20 @@ int handle_args(int argc, char* argv[], char* prio) * @param green The green brightness * @param blue The blue brightness */ -static void fill_filter(libcoopgamma_filter_t* restrict filter, double red, double green, double blue) +static void +fill_filter(libcoopgamma_filter_t *restrict filter, double red, double green, double blue) { - switch (filter->depth) - { + switch (filter->depth) { #define X(CONST, MEMBER, MAX, TYPE)\ - case CONST:\ - libclut_start_over(&(filter->ramps.MEMBER), MAX, TYPE, 1, 1, 1);\ - libclut_rgb_brightness(&(filter->ramps.MEMBER), MAX, TYPE, red, green, blue);\ - break; -LIST_DEPTHS + case CONST:\ + libclut_start_over(&filter->ramps.MEMBER, MAX, TYPE, 1, 1, 1);\ + libclut_rgb_brightness(&filter->ramps.MEMBER, MAX, TYPE, red, green, blue);\ + break; + LIST_DEPTHS #undef X - default: - abort(); - } + default: + abort(); + } } @@ -247,18 +246,19 @@ LIST_DEPTHS * @param now Output parameter for the current time (monotonic) * @return Zero on success, -1 on error */ -static int double_time(double* restrict now) +static int +double_time(double *restrict now) { #ifndef CLOCK_MONOTONIC_RAW # define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC #endif - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) < 0) - return -1; - *now = (double)(ts.tv_nsec); - *now /= 1000000000L; - *now += (double)(ts.tv_sec); - return 0; + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) < 0) + return -1; + *now = (double)(ts.tv_nsec); + *now /= 1000000000L; + *now += (double)(ts.tv_sec); + return 0; } @@ -270,174 +270,188 @@ static int double_time(double* restrict now) * -2: Error, `cg.error` set * -3: Error, message already printed */ -int start(void) +int +start(void) { - int r, fade_red, fade_green, fade_blue; - size_t i, j; - double t, starttime, red, green, blue, redt, greent, bluet; - - redt = (red_target - 1) / red_time; - greent = (green_target - 1) / green_time; - bluet = (blue_target - 1) / blue_time; - fade_red = !isinf(redt) && !isnan(redt); - fade_green = !isinf(greent) && !isnan(greent); - fade_blue = !isinf(bluet) && !isnan(bluet); - - for (i = 0; i < filters_n; i++) - crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; - - if ((r = make_slaves()) < 0) - return r; - - if ((r = double_time(&starttime)) < 0) - return r; - - red = red_target < 0 ? 0 : red_target > 1 ? 1 : red_target; - green = green_target < 0 ? 0 : green_target > 1 ? 1 : green_target; - blue = blue_target < 0 ? 0 : blue_target > 1 ? 1 : blue_target; - - for (;;) - { - if ((r = double_time(&t)) < 0) - return r; - t -= starttime; - if (fade_red) - {if (red = 1 + t * redt, red > 1) red = 1; else if (red < 0) red = 0;} - if (fade_green) - {if (green = 1 + t * greent, green > 1) green = 1; else if (green < 0) green = 0;} - if (fade_blue) - {if (blue = 1 + t * bluet, blue > 1) blue = 1; else if (blue < 0) blue = 0;} - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - fill_filter(&(crtc_updates[i].filter), red, green, blue); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } - } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - sched_yield(); - - if ((t >= red_time) && (t >= green_time) && (t >= blue_time)) - break; - } - - if ((signal(SIGINT, sig_int) == SIG_ERR) || - (signal(SIGTERM, sig_int) == SIG_ERR) || - (signal(SIGHUP, sig_int) == SIG_ERR)) - return -1; - - if (libcoopgamma_set_nonblocking(&cg, 0) < 0) - return -1; - for (;;) - if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) - { - if (received_int) - goto fade_in; - switch (errno) - { - case 0: - break; - case ENOTRECOVERABLE: - goto enotrecoverable; - default: - return 1; - } - } - - fade_in: - if (libcoopgamma_set_nonblocking(&cg, 1) < 0) - return -1; - - t = red_time; - t = t > green_time ? t : green_time; - t = t > blue_time ? t : blue_time; - redt = t - red_time; - greent = t - green_time; - bluet = t - blue_time; - t = red_time + green_time + blue_time; - if (red_time > 0) - t = t < red_time ? t : red_time; - if (green_time > 0) - t = t < green_time ? t : green_time; - if (blue_time > 0) - t = t < blue_time ? t : blue_time; - red_time = t + redt; - green_time = t + greent; - blue_time = t + bluet; - - red = green = blue = 1; - - if ((r = double_time(&starttime)) < 0) - return r; - - for (;;) - { - if ((r = double_time(&t)) < 0) - return r; - t -= starttime; - redt = t / red_time; - greent = t / green_time; - bluet = t / blue_time; - if (!isinf(redt) && !isnan(redt)) - { - red = red_target * (1 - redt) + redt; - if (red > 1) red = 1; else if (red < 0) red = 0; - } - if (!isinf(greent) && !isnan(greent)) - { - green = green_target * (1 - greent) + greent; - if (green > 1) green = 1; else if (green < 0) green = 0; + int r, fade_red, fade_green, fade_blue; + size_t i, j; + double t, starttime, red, green, blue, redt, greent, bluet; + + redt = (red_target - 1) / red_time; + greent = (green_target - 1) / green_time; + bluet = (blue_target - 1) / blue_time; + fade_red = !isinf(redt) && !isnan(redt); + fade_green = !isinf(greent) && !isnan(greent); + fade_blue = !isinf(bluet) && !isnan(bluet); + + for (i = 0; i < filters_n; i++) + crtc_updates[i].filter.lifespan = LIBCOOPGAMMA_UNTIL_DEATH; + + if ((r = make_slaves()) < 0) + return r; + + if ((r = double_time(&starttime)) < 0) + return r; + + red = red_target < 0 ? 0 : red_target > 1 ? 1 : red_target; + green = green_target < 0 ? 0 : green_target > 1 ? 1 : green_target; + blue = blue_target < 0 ? 0 : blue_target > 1 ? 1 : blue_target; + + for (;;) { + if ((r = double_time(&t)) < 0) + return r; + t -= starttime; + if (fade_red) { + red = 1 + t * redt; + if (red > 1) + red = 1; + else if (red < 0) + red = 0; + } + if (fade_green) { + green = 1 + t * greent; + if (green > 1) + green = 1; + else if (green < 0) + green = 0; + } + if (fade_blue) { + blue = 1 + t * bluet; + if (blue > 1) + blue = 1; + else if (blue < 0) + blue = 0; + } + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + fill_filter(&crtc_updates[i].filter, red, green, blue); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } + + while (r != 1) + if ((r = synchronise(-1)) < 0) + return r; + + sched_yield(); + + if (t >= red_time && t >= green_time && t >= blue_time) + break; } - if (!isinf(bluet) && !isnan(bluet)) - { - blue = blue_target * (1 - bluet) + bluet; - if (blue > 1) blue = 1; else if (blue < 0) blue = 0; + + if (signal(SIGINT, sig_int) == SIG_ERR || + signal(SIGTERM, sig_int) == SIG_ERR || + signal(SIGHUP, sig_int) == SIG_ERR) + return -1; + + if (libcoopgamma_set_nonblocking(&cg, 0) < 0) + return -1; + for (;;) { + if (libcoopgamma_synchronise(&cg, NULL, 0, &j) < 0) { + if (received_int) + goto fade_in; + switch (errno) { + case 0: + break; + case ENOTRECOVERABLE: + goto enotrecoverable; + default: + return 1; + } + } } - - for (i = 0, r = 1; i < filters_n; i++) - { - if (!(crtc_updates[i].master) || !(crtc_info[crtc_updates[i].crtc].supported)) - continue; - fill_filter(&(crtc_updates[i].filter), red, green, blue); - r = update_filter(i, 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - if (crtc_updates[i].slaves != NULL) - for (j = 0; crtc_updates[i].slaves[j] != 0; j++) - { - r = update_filter(crtc_updates[i].slaves[j], 0); - if ((r == -2) || ((r == -1) && (errno != EAGAIN))) - return r; - } + +fade_in: + if (libcoopgamma_set_nonblocking(&cg, 1) < 0) + return -1; + + t = red_time; + t = t > green_time ? t : green_time; + t = t > blue_time ? t : blue_time; + redt = t - red_time; + greent = t - green_time; + bluet = t - blue_time; + t = red_time + green_time + blue_time; + if (red_time > 0) + t = t < red_time ? t : red_time; + if (green_time > 0) + t = t < green_time ? t : green_time; + if (blue_time > 0) + t = t < blue_time ? t : blue_time; + red_time = t + redt; + green_time = t + greent; + blue_time = t + bluet; + + red = green = blue = 1; + + if ((r = double_time(&starttime)) < 0) + return r; + + for (;;) { + if ((r = double_time(&t)) < 0) + return r; + t -= starttime; + redt = t / red_time; + greent = t / green_time; + bluet = t / blue_time; + if (!isinf(redt) && !isnan(redt)) { + red = red_target * (1 - redt) + redt; + if (red > 1) + red = 1; + else if (red < 0) + red = 0; + } + if (!isinf(greent) && !isnan(greent)) { + green = green_target * (1 - greent) + greent; + if (green > 1) + green = 1; + else if (green < 0) + green = 0; + } + if (!isinf(bluet) && !isnan(bluet)) { + blue = blue_target * (1 - bluet) + bluet; + if (blue > 1) + blue = 1; + else if (blue < 0) + blue = 0; + } + + for (i = 0, r = 1; i < filters_n; i++) { + if (!crtc_updates[i].master || !crtc_info[crtc_updates[i].crtc].supported) + continue; + fill_filter(&crtc_updates[i].filter, red, green, blue); + r = update_filter(i, 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + if (crtc_updates[i].slaves) { + for (j = 0; crtc_updates[i].slaves[j]; j++) { + r = update_filter(crtc_updates[i].slaves[j], 0); + if (r == -2 || (r == -1 && errno != EAGAIN)) + return r; + } + } + } + + while (r != 1 && (r = synchronise(-1)) < 0) + return r; + + sched_yield(); + + if (t >= red_time && t >= green_time && t >= blue_time) + break; } - - while (r != 1) - if ((r = synchronise(-1)) < 0) - return r; - - sched_yield(); - - if ((t >= red_time) && (t >= green_time) && (t >= blue_time)) - break; - } - - return 0; - enotrecoverable: - for (;;) - if (pause() < 0) - return -1; + + return 0; +enotrecoverable: + pause(); + return -1; } @@ -4,5 +4,5 @@ MANPREFIX = $(PREFIX)/share/man PKGNAME = cg-tools CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D'PKGNAME="$(PKGNAME)"' -CFLAGS = -std=c99 -Wall -O2 -LDFLAGS = -lcoopgamma -lm -s +CFLAGS = -std=c99 -Wall -Og -g +LDFLAGS = -lcoopgamma -lm |