From 0c0d0ca0da8060484ab5f41a6ee73087755a69e3 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 16 Jul 2016 19:28:39 +0200 Subject: Restructure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 18 +- src/arg.h | 2 - src/communication.c | 171 +++++++++ src/communication.h | 137 +++++++ src/coopgamma-server/chaining.c | 257 +++++++++++++ src/coopgamma-server/chaining.h | 75 ++++ src/coopgamma-server/server.c | 335 +++++++++++++++++ src/coopgamma-server/server.h | 93 +++++ src/coopgammad.c | 265 +++---------- src/crtc-server/server.c | 59 +++ src/crtc-server/server.h | 49 +++ src/filter.c | 144 ------- src/filter.h | 131 ------- src/gamma-server/server.c | 144 +++++++ src/gamma-server/server.h | 75 ++++ src/message.c | 572 ---------------------------- src/message.h | 153 -------- src/output.c | 482 ------------------------ src/output.h | 230 ------------ src/ramps.c | 165 -------- src/ramps.h | 111 ------ src/ring.c | 224 ----------- src/ring.h | 145 ------- src/server.c | 810 ++-------------------------------------- src/server.h | 79 +--- src/state.c | 363 ++++++++++++++++++ src/state.h | 182 +++++++++ src/types/filter.c | 144 +++++++ src/types/filter.h | 138 +++++++ src/types/message.c | 572 ++++++++++++++++++++++++++++ src/types/message.h | 160 ++++++++ src/types/output.c | 264 +++++++++++++ src/types/output.h | 206 ++++++++++ src/types/ramps.c | 98 +++++ src/types/ramps.h | 102 +++++ src/types/ring.c | 224 +++++++++++ src/types/ring.h | 152 ++++++++ src/util.h | 7 + 38 files changed, 4100 insertions(+), 3438 deletions(-) create mode 100644 src/communication.c create mode 100644 src/communication.h create mode 100644 src/coopgamma-server/chaining.c create mode 100644 src/coopgamma-server/chaining.h create mode 100644 src/coopgamma-server/server.c create mode 100644 src/coopgamma-server/server.h create mode 100644 src/crtc-server/server.c create mode 100644 src/crtc-server/server.h delete mode 100644 src/filter.c delete mode 100644 src/filter.h create mode 100644 src/gamma-server/server.c create mode 100644 src/gamma-server/server.h delete mode 100644 src/message.c delete mode 100644 src/message.h delete mode 100644 src/output.c delete mode 100644 src/output.h delete mode 100644 src/ramps.c delete mode 100644 src/ramps.h delete mode 100644 src/ring.c delete mode 100644 src/ring.h create mode 100644 src/state.c create mode 100644 src/state.h create mode 100644 src/types/filter.c create mode 100644 src/types/filter.h create mode 100644 src/types/message.c create mode 100644 src/types/message.h create mode 100644 src/types/output.c create mode 100644 src/types/output.h create mode 100644 src/types/ramps.c create mode 100644 src/types/ramps.h create mode 100644 src/types/ring.c create mode 100644 src/types/ring.h diff --git a/Makefile b/Makefile index 4e2139c..921121a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,21 @@ COMMAND = coopgammad KERNEL = $(shell uname | tr '[A-Z]_' '[a-z]-') -SRC = filter coopgammad output ramps util message server ring +SRC = \ + coopgammad \ + server \ + util \ + communication \ + state \ + crtc-server/server \ + gamma-server/server \ + coopgamma-server/server \ + coopgamma-server/chaining \ + types/filter \ + types/output \ + types/ramps \ + types/message \ + types/ring OPTIMISE = -Og -g @@ -34,7 +48,7 @@ bin/coopgammad: $(foreach S,$(SRC),obj/$(S).o) @mkdir -p -- "$$(dirname -- "$@")" $(CC) $(LDFLAGS) -o $@ $^ -obj/%.o: src/%.c src/*.h +obj/%.o: src/%.c src/*.h src/*/*.h @mkdir -p -- "$$(dirname -- "$@")" $(CC) $(CCFLAGS) $(CPPFLAGS) -c -o $@ $< diff --git a/src/arg.h b/src/arg.h index 97e5980..8418b38 100644 --- a/src/arg.h +++ b/src/arg.h @@ -6,8 +6,6 @@ #ifndef ARG_H__ #define ARG_H__ -extern char *restrict argv0; - /* use main(int argc, char *argv[]) */ #define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ argv[0] && argv[0][0] == '-'\ diff --git a/src/communication.c b/src/communication.c new file mode 100644 index 0000000..c508992 --- /dev/null +++ b/src/communication.c @@ -0,0 +1,171 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "communication.h" +#include "state.h" +#include "coopgamma-server/server.h" + +#include +#include +#include + + + +/** + * Send a message + * + * @param conn The index of the connection + * @param buf The data to send + * @param n The size of `buf` + * @return Zero on success, -1 on error, 1 if disconncted + * EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count + * as success (ECONNRESET cause 1 to be returned), + * and are handled appropriately. + */ +int send_message(size_t conn, char* restrict buf, size_t n) +{ + struct ring* restrict ring = outbound + conn; + int fd = connections[conn]; + int saved_errno; + size_t ptr = 0; + ssize_t sent; + size_t chunksize = n; + size_t sendsize; + size_t old_n; + char* old_buf; + + while ((old_buf = ring_peek(ring, &old_n))) + { + size_t old_ptr = 0; + while (old_ptr < n) + { + sendsize = old_n - old_ptr < chunksize ? old_n - old_ptr : chunksize; + sent = send(fd, old_buf + old_ptr, sendsize, 0); + if (sent < 0) + { + if (errno != EMSGSIZE) + goto fail; + chunksize >>= 1; + if (chunksize == 0) + goto fail; + continue; + } + old_ptr += (size_t)sent; + ring_pop(ring, (size_t)sent); + } + } + + while (ptr < n) + { + sendsize = n - ptr < chunksize ? n - ptr : chunksize; + sent = send(fd, buf + ptr, sendsize, 0); + if (sent < 0) + { + if (errno != EMSGSIZE) + goto fail; + chunksize >>= 1; + if (chunksize == 0) + goto fail; + continue; + } + ptr += (size_t)sent; + } + + free(buf); + return 0; + + fail: + switch (errno) + { + case EINTR: + case EAGAIN: +#if EAGAIN != EWOULDBLOCK + case EWOULDBLOCK: +#endif + if (ring_push(ring, buf + ptr, n - ptr) < 0) + goto proper_fail; + free(buf); + return 0; + case ECONNRESET: + free(buf); + if (connection_closed(fd) < 0) + return -1; + return 1; + default: + break; + } + proper_fail: + saved_errno = errno; + free(buf); + errno = saved_errno; + return -1; +} + + +/** + * Send a custom error without an error number + * + * @param conn The index of the connection + * @param message_id The ID of the message to which this message is a response + * @param desc The error description to send + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +int (send_error)(size_t conn, const char* restrict message_id, const char* restrict desc) +{ + char* restrict buf; + size_t n; + + MAKE_MESSAGE(&buf, &n, 0, + "Command: error\n" + "In response to: %s\n" + "Error: custom\n" + "Length: %zu\n" + "\n" + "%s\n", + message_id, strlen(desc) + 1, desc); + + return send_message(conn, buf, n); +} + + +/** + * Send a standard error + * + * @param conn The index of the connection + * @param message_id The ID of the message to which this message is a response + * @param number The value of `errno`, 0 to indicate success + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +int (send_errno)(size_t conn, const char* restrict message_id, int number) +{ + char* restrict buf; + size_t n; + + MAKE_MESSAGE(&buf, &n, 0, + "Command: error\n" + "In response to: %s\n" + "Error: %i\n" + "\n", + message_id, number); + + return send_message(conn, buf, n); +} + diff --git a/src/communication.h b/src/communication.h new file mode 100644 index 0000000..43af314 --- /dev/null +++ b/src/communication.h @@ -0,0 +1,137 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef COMMUNICATION_H +#define COMMUNICATION_H + + +#include +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Construct a message + * + * @param bufp:char** Output parameter for the buffer, must not have side-effects + * @param np:size_t* Output parameter for the size of the buffer sans `extra` + * @param extra:size_t The extra number for bytes to allocate to the buffer + * @param format:string-literal Message format string + * @param ... Message formatting arguments + */ +#define MAKE_MESSAGE(bufp, np, extra, format, ...) \ + do \ + { \ + ssize_t m__; \ + snprintf(NULL, 0, format "%zn", __VA_ARGS__, &m__); \ + *(bufp) = malloc((size_t)(extra) + (size_t)m__); \ + if (*(bufp) == NULL) \ + return -1; \ + sprintf(*(bufp), format, __VA_ARGS__); \ + *(np) = (size_t)m__; \ + } \ + while (0) + +/** + * Send a custom error without an error number + * + * @param ... The error description to send + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +#define send_error(...) ((send_error)(conn, message_id, __VA_ARGS__)) + +/** + * Send a standard error + * + * @param ... The value of `errno`, 0 to indicate success + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +#define send_errno(...) ((send_errno)(conn, message_id, __VA_ARGS__)) + + + +/** + * Send a message + * + * @param conn The index of the connection + * @param buf The data to send + * @param n The size of `buf` + * @return Zero on success, -1 on error, 1 if disconncted + * EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count + * as success (ECONNRESET cause 1 to be returned), + * and are handled appropriately. + */ +int send_message(size_t conn, char* restrict buf, size_t n); + +/** + * Send a custom error without an error number + * + * @param conn The index of the connection + * @param message_id The ID of the message to which this message is a response + * @param desc The error description to send + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +GCC_ONLY(__attribute__((nonnull))) +int (send_error)(size_t conn, const char* restrict message_id, const char* restrict desc); + +/** + * Send a standard error + * + * @param conn The index of the connection + * @param message_id The ID of the message to which this message is a response + * @param number The value of `errno`, 0 to indicate success + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +GCC_ONLY(__attribute__((nonnull))) +int (send_errno)(size_t conn, const char* restrict message_id, int number); + +/** + * Continue sending the queued messages + * + * @param conn The index of the connection + * @return Zero on success, -1 on error, 1 if disconncted + * EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count + * as success (ECONNRESET cause 1 to be returned), + * and are handled appropriately. + */ +static inline int continue_send(size_t conn) +{ + return send_message(conn, NULL, 0); +} + + + +#endif + diff --git a/src/coopgamma-server/chaining.c b/src/coopgamma-server/chaining.c new file mode 100644 index 0000000..b2739c2 --- /dev/null +++ b/src/coopgamma-server/chaining.c @@ -0,0 +1,257 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "chaining.h" + +#include + +#include +#include + + + +/** + * The name of the process + */ +extern char* restrict argv0; + + + +/** + * Apply a filter on top of another filter + * + * @param dest The output for the resulting ramp-trio, must be initialised + * @param application The red, green and blue ramps, as one single raw array, + * of the filter that should be applied + * @param depth -1: `float` stops + * -2: `double` stops + * Other: the number of bits of each (integral) stop + * @param base The CLUT on top of which the new filter should be applied, + * this can be the same pointer as `dest` + */ +void apply_filter(union gamma_ramps* restrict dest, void* restrict application, + int depth, union gamma_ramps* restrict base) +{ + union gamma_ramps app; + size_t bytedepth; + size_t red_width, green_width, blue_width; + + if (depth == -1) + bytedepth = sizeof(float); + else if (depth == -2) + bytedepth = sizeof(double); + else + bytedepth = (size_t)depth / 8; + + red_width = (app.u8.red_size = base->u8.red_size) * bytedepth; + green_width = (app.u8.green_size = base->u8.green_size) * bytedepth; + blue_width = (app.u8.blue_size = base->u8.blue_size) * bytedepth; + + app.u8.red = application; + app.u8.green = app.u8.red + red_width; + app.u8.blue = app.u8.green + green_width; + + if (dest != base) + { + memcpy(dest->u8.red, base->u8.red, red_width); + memcpy(dest->u8.green, base->u8.green, green_width); + memcpy(dest->u8.blue, base->u8.blue, blue_width); + } + + switch (depth) + { + case 8: + libclut_apply(&(dest->u8), UINT8_MAX, uint8_t, &(app.u8), UINT8_MAX, uint8_t, 1, 1, 1); + break; + case 16: + libclut_apply(&(dest->u16), UINT16_MAX, uint16_t, &(app.u16), UINT16_MAX, uint16_t, 1, 1, 1); + break; + case 32: + libclut_apply(&(dest->u32), UINT32_MAX, uint32_t, &(app.u32), UINT32_MAX, uint32_t, 1, 1, 1); + break; + case 64: + libclut_apply(&(dest->u64), UINT64_MAX, uint64_t, &(app.u64), UINT64_MAX, uint64_t, 1, 1, 1); + break; + case -1: + libclut_apply(&(dest->f), 1.0f, float, &(app.d), 1.0f, float, 1, 1, 1); + break; + case -2: + libclut_apply(&(dest->d), (double)1, double, &(app.f), (double)1, double, 1, 1, 1); + break; + default: + abort(); + } +} + + + +/** + * Remove a filter from an output + * + * @param out The output + * @param filter The filter + * @return The index of the filter, `out->table_size` if not found + */ +static ssize_t remove_filter(struct output* restrict out, struct filter* restrict filter) +{ + size_t i, n = out->table_size; + + for (i = 0; i < n; i++) + if (!strcmp(filter->class, out->table_filters[i].class)) + break; + + if (i == out->table_size) + { + fprintf(stderr, "%s: ignoring attempt to removing non-existing filter on CRTC %s: %s", + argv0, out->name, filter->class); + return (ssize_t)(out->table_size); + } + + filter_destroy(out->table_filters + i); + libgamma_gamma_ramps8_destroy(&(out->table_sums[i].u8)); + + n = n - i - 1; + memmove(out->table_filters + i, out->table_filters + i + 1, n * sizeof(*(out->table_filters))); + memmove(out->table_sums + i, out->table_sums + i + 1, n * sizeof(*(out->table_sums))); + out->table_size--; + + return (ssize_t)i; +} + + +/** + * Add a filter to an output + * + * @param out The output + * @param filter The filter + * @return The index given to the filter, -1 on error + */ +ssize_t add_filter(struct output* restrict out, struct filter* restrict filter) +{ + size_t i, n = out->table_size; + int r = -1; + + /* Remove? */ + if (filter->lifespan == LIFESPAN_REMOVE) + return remove_filter(out, filter); + + /* Update? */ + for (i = 0; i < n; i++) + if (!strcmp(filter->class, out->table_filters[i].class)) + break; + if (i != n) + { + filter_destroy(out->table_filters + i); + out->table_filters[i] = *filter; + return (ssize_t)i; + } + + /* Add! */ + for (i = 0; i < n; i++) + if (filter->priority > out->table_filters[i].priority) + break; + + if (n == out->table_alloc) + { + void* new; + + new = realloc(out->table_filters, (n + 10) * sizeof(*(out->table_filters))); + if (new == NULL) + return -1; + out->table_filters = new; + + new = realloc(out->table_sums, (n + 10) * sizeof(*(out->table_sums))); + if (new == NULL) + return -1; + out->table_sums = new; + + out->table_alloc += 10; + } + + memmove(out->table_filters + i + 1, out->table_filters + i, (n - i) * sizeof(*(out->table_filters))); + memmove(out->table_sums + i + 1, out->table_sums + i, (n - i) * sizeof(*(out->table_sums))); + out->table_size++; + + COPY_RAMP_SIZES(&(out->table_sums[i].u8), out); + switch (out->depth) + { + case 8: r = libgamma_gamma_ramps8_initialise(&(out->table_sums[i].u8)); break; + case 16: r = libgamma_gamma_ramps16_initialise(&(out->table_sums[i].u16)); break; + case 32: r = libgamma_gamma_ramps32_initialise(&(out->table_sums[i].u32)); break; + case 64: r = libgamma_gamma_ramps64_initialise(&(out->table_sums[i].u64)); break; + case -1: r = libgamma_gamma_rampsf_initialise(&(out->table_sums[i].f)); break; + case -2: r = libgamma_gamma_rampsd_initialise(&(out->table_sums[i].d)); break; + default: + abort(); + } + if (r < 0) + return -1; + + out->table_filters[i] = *filter; + + return (ssize_t)i; +} + + +/** + * Make identity mapping ramps + * + * @param ramps Output parameter for the ramps + * @param output The output for which the ramps shall be configured + * @return Zero on success, -1 on error + */ +int make_plain_ramps(union gamma_ramps* restrict ramps, struct output* restrict output) +{ + COPY_RAMP_SIZES(&(ramps->u8), output); + switch (output->depth) + { + case 8: + if (libgamma_gamma_ramps8_initialise(&(ramps->u8))) + return -1; + libclut_start_over(&(ramps->u8), UINT8_MAX, uint8_t, 1, 1, 1); + break; + case 16: + if (libgamma_gamma_ramps16_initialise(&(ramps->u16))) + return -1; + libclut_start_over(&(ramps->u16), UINT16_MAX, uint16_t, 1, 1, 1); + break; + case 32: + if (libgamma_gamma_ramps32_initialise(&(ramps->u32))) + return -1; + libclut_start_over(&(ramps->u32), UINT32_MAX, uint32_t, 1, 1, 1); + break; + case 64: + if (libgamma_gamma_ramps64_initialise(&(ramps->u64))) + return -1; + libclut_start_over(&(ramps->u64), UINT64_MAX, uint64_t, 1, 1, 1); + break; + case -1: + if (libgamma_gamma_rampsf_initialise(&(ramps->f))) + return -1; + libclut_start_over(&(ramps->f), 1.0f, float, 1, 1, 1); + break; + case -2: + if (libgamma_gamma_rampsd_initialise(&(ramps->d))) + return -1; + libclut_start_over(&(ramps->d), (double)1, double, 1, 1, 1); + break; + default: + abort(); + } + return 0; +} + diff --git a/src/coopgamma-server/chaining.h b/src/coopgamma-server/chaining.h new file mode 100644 index 0000000..cd31e5a --- /dev/null +++ b/src/coopgamma-server/chaining.h @@ -0,0 +1,75 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef COOPGAMMA_SERVER_CHAINING +#define COOPGAMMA_SERVER_CHAINING + + +#include "../types/output.h" + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Apply a filter on top of another filter + * + * @param dest The output for the resulting ramp-trio, must be initialised + * @param application The red, green and blue ramps, as one single raw array, + * of the filter that should be applied + * @param depth -1: `float` stops + * -2: `double` stops + * Other: the number of bits of each (integral) stop + * @param base The CLUT on top of which the new filter should be applied, + * this can be the same pointer as `dest` + */ +GCC_ONLY(__attribute__((nonnull))) +void apply_filter(union gamma_ramps* restrict dest, void* restrict application, + int depth, union gamma_ramps* restrict base); + + +/** + * Add a filter to an output + * + * @param output The output + * @param filter The filter + * @return The index given to the filter, -1 on error + */ +GCC_ONLY(__attribute__((nonnull))) +ssize_t add_filter(struct output* restrict output, struct filter* restrict filter); + +/** + * Make identity mapping ramps + * + * @param ramps Output parameter for the ramps + * @param output The output for which the ramps shall be configured + * @return Zero on success, -1 on error + */ +GCC_ONLY(__attribute__((nonnull))) +int make_plain_ramps(union gamma_ramps* restrict ramps, struct output* restrict output); + + +#endif + diff --git a/src/coopgamma-server/server.c b/src/coopgamma-server/server.c new file mode 100644 index 0000000..8d56119 --- /dev/null +++ b/src/coopgamma-server/server.c @@ -0,0 +1,335 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "server.h" +#include "chaining.h" +#include "../state.h" +#include "../communication.h" +#include "../util.h" +#include "../gamma-server/server.h" + +#include + +#include +#include +#include + + + +/** + * Handle a closed connection + * + * @param client The file descriptor for the client + * @return Zero on success, -1 on error + */ +int connection_closed(int client) +{ + size_t i, j, k; + int remove; + + for (i = 0; i < outputs_n; i++) + { + struct output* output = outputs + i; + ssize_t updated = -1; + for (j = k = 0; j < output->table_size; j += !remove, k++) + { + remove = output->table_filters[j].client == client; + remove = remove && (output->table_filters[j].lifespan == LIFESPAN_UNTIL_DEATH); + if (remove) + { + filter_destroy(output->table_filters + j); + libgamma_gamma_ramps8_destroy(&(output->table_sums[j].u8)); + output->table_size -= 1; + if (updated == -1) + updated = (ssize_t)j; + } + output->table_filters[j] = output->table_filters[k]; + output->table_sums[j] = output->table_sums[k]; + } + if (updated >= 0) + if (flush_filters(output, (size_t)updated) < 0) + return -1; + } + + return 0; +} + + +/** + * Handle a ‘Command: get-gamma’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @param crtc The value of the ‘CRTC’ header + * @param coalesce The value of the ‘Coalesce’ header + * @param high_priority The value of the ‘High priority’ header + * @param low_priority The value of the ‘Low priority’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +int handle_get_gamma(size_t conn, const char* restrict message_id, const char* restrict crtc, + const char* restrict coalesce, const char* restrict high_priority, + const char* restrict low_priority) +{ + struct output* restrict output; + int64_t high, low; + int coal; + char* restrict buf; + size_t start, end, len, n, i; + char depth[3]; + char tables[sizeof("Tables: \n") + 3 * sizeof(size_t)]; + + if (crtc == NULL) return send_error("protocol error: 'CRTC' header omitted"); + if (coalesce == NULL) return send_error("protocol error: 'Coalesce' header omitted"); + if (high_priority == NULL) return send_error("protocol error: 'High priority' header omitted"); + if (low_priority == NULL) return send_error("protocol error: 'Low priority' header omitted"); + + high = (int64_t)atoll(high_priority); + low = (int64_t)atoll(low_priority); + + if (!strcmp(coalesce, "yes")) + coal = 1; + else if (!strcmp(coalesce, "no")) + coal = 0; + else + return send_error("protocol error: recognised value for 'Coalesce' header"); + + output = output_find_by_name(crtc, outputs, outputs_n); + if (output == NULL) + return send_error("selected CRTC does not exist"); + else if (output->supported == LIBGAMMA_NO) + return send_error("selected CRTC does not support gamma adjustments"); + + for (start = 0; start < output->table_size; start++) + if (output->table_filters[start].priority <= high) + break; + + for (end = output->table_size; end > 0; end--) + if (output->table_filters[end - 1].priority >= low) + break; + + switch (output->depth) + { + case -2: strcpy(depth, "d"); break; + case -1: strcpy(depth, "f"); break; + default: + sprintf(depth, "%i", output->depth); + break; + } + + if (coal) + { + *tables = '\0'; + n = output->ramps_size; + } + else + { + sprintf(tables, "Tables: %zu\n", end - start); + n = (sizeof(int64_t) + output->ramps_size) * (end - start); + for (i = start; i < end; i++) + n += strlen(output->table_filters[i].class) + 1; + } + + MAKE_MESSAGE(&buf, &n, 0, + "In response to: %s\n" + "Depth: %s\n" + "Red size: %zu\n" + "Green size: %zu\n" + "Blue size: %zu\n" + "%s" + "Length: %zu\n" + "\n", + message_id, depth, output->red_size, output->green_size, + output->blue_size, tables, n); + + if (coal) + { + if (start == 0) + memcpy(buf + n, output->table_sums[end].u8.red, output->ramps_size); + else + { + union gamma_ramps ramps; + if (make_plain_ramps(&ramps, output)) + { + int saved_errno = errno; + free(buf); + errno = saved_errno; + return -1; + } + for (i = start; i < end; i++) + apply_filter(&ramps, output->table_filters[i].ramps, output->depth, &ramps); + memcpy(buf + n, ramps.u8.red, output->ramps_size); + libgamma_gamma_ramps8_destroy(&(ramps.u8)); + } + n += output->ramps_size; + } + else + for (i = start; i < end; i++) + { +#if defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wcast-align" +#endif + *(int64_t*)(buf + n) = output->table_filters[i].priority; +#if defined(__clang__) +# pragma GCC diagnostic pop +#endif + n += sizeof(int64_t); + len = strlen(output->table_filters[i].class) + 1; + memcpy(buf + n, output->table_filters[i].class, len); + n += len; + memcpy(buf + n, output->table_filters[i].ramps, output->ramps_size); + n += output->ramps_size; + } + + return send_message(conn, buf, n); +} + + +/** + * Handle a ‘Command: set-gamma’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @param crtc The value of the ‘CRTC’ header + * @param priority The value of the ‘Priority’ header + * @param class The value of the ‘Class’ header + * @param lifespan The value of the ‘Lifespan’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +int handle_set_gamma(size_t conn, const char* restrict message_id, const char* restrict crtc, + const char* restrict priority, const char* restrict class, const char* restrict lifespan) +{ + struct message* restrict msg = inbound + conn; + struct output* restrict output = NULL; + struct filter filter; + char* restrict p; + char* restrict q; + int saved_errno; + ssize_t r; + + if (crtc == NULL) return send_error("protocol error: 'CRTC' header omitted"); + if (class == NULL) return send_error("protocol error: 'Class' header omitted"); + if (lifespan == NULL) return send_error("protocol error: 'Lifespan' header omitted"); + + filter.client = connections[conn]; + filter.priority = priority == NULL ? 0 : (int64_t)atoll(priority); + filter.ramps = NULL; + + output = output_find_by_name(crtc, outputs, outputs_n); + if (output == NULL) + return send_error("CRTC does not exists"); + + p = strstr(class, "::"); + if ((p == NULL) || (p == class)) + return send_error("protocol error: malformatted value for 'Class' header"); + q = strstr(p + 2, "::"); + if ((q == NULL) || (q == p)) + return send_error("protocol error: malformatted value for 'Class' header"); + + if (!strcmp(lifespan, "until-removal")) + filter.lifespan = LIFESPAN_UNTIL_REMOVAL; + else if (!strcmp(lifespan, "until-death")) + filter.lifespan = LIFESPAN_UNTIL_DEATH; + else if (!strcmp(lifespan, "remove")) + filter.lifespan = LIFESPAN_REMOVE; + else + return send_error("protocol error: recognised value for 'Lifespan' header"); + + if (filter.lifespan == LIFESPAN_REMOVE) + { + if (msg->payload_size) + fprintf(stderr, "%s: ignoring superfluous payload on Command: set-gamma message with " + "Lifespan: remove\n", argv0); + if (priority != NULL) + fprintf(stderr, "%s: ignoring superfluous Priority header on Command: set-gamma message with " + "Lifespan: remove\n", argv0); + } + else if (msg->payload_size != output->ramps_size) + return send_error("invalid payload: size of message payload does matched the expectancy"); + else if (priority == NULL) + return send_error("protocol error: 'Priority' header omitted"); + + filter.class = memdup(class, strlen(class) + 1); + if (filter.class == NULL) + goto fail; + + if (filter.lifespan != LIFESPAN_REMOVE) + { + filter.ramps = memdup(msg->payload, msg->payload_size); + if (filter.ramps == NULL) + goto fail; + } + + if ((r = add_filter(output, &filter)) < 0) + goto fail; + filter.class = NULL; + filter.ramps = NULL; + if (flush_filters(output, (size_t)r)) + goto fail; + + return send_errno(0); + + fail: + saved_errno = errno; + send_errno(saved_errno); + free(filter.class); + free(filter.ramps); + errno = saved_errno; + return -1; +} + + + +/** + * Recalculate the resulting gamma and + * update push the new gamma ramps to the CRTC + * + * @param output The output + * @param first_updated The index of the first added or removed filter + * @return Zero on success, -1 on error + */ +int flush_filters(struct output* restrict output, size_t first_updated) +{ + union gamma_ramps plain; + union gamma_ramps* last; + size_t i; + + if (first_updated == 0) + { + if (make_plain_ramps(&plain, output) < 0) + return -1; + last = &plain; + } + else + last = output->table_sums + (first_updated - 1); + + for (i = first_updated; i < output->table_size; i++) + { + apply_filter(output->table_sums + i, output->table_filters[i].ramps, output->depth, last); + last = output->table_sums + i; + } + + set_gamma(output, last); + + if (first_updated == 0) + libgamma_gamma_ramps8_destroy(&(plain.u8)); + + return 0; +} + diff --git a/src/coopgamma-server/server.h b/src/coopgamma-server/server.h new file mode 100644 index 0000000..971f7df --- /dev/null +++ b/src/coopgamma-server/server.h @@ -0,0 +1,93 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef COOPGAMMA_SERVER_SERVER_H +#define COOPGAMMA_SERVER_SERVER_H + + +#include "../types/output.h" + +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Handle a closed connection + * + * @param client The file descriptor for the client + * @return Zero on success, -1 on error + */ +int connection_closed(int client); + +/** + * Handle a ‘Command: get-gamma’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @param crtc The value of the ‘CRTC’ header + * @param coalesce The value of the ‘Coalesce’ header + * @param high_priority The value of the ‘High priority’ header + * @param low_priority The value of the ‘Low priority’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +GCC_ONLY(__attribute__((nonnull(2)))) +int handle_get_gamma(size_t conn, const char* restrict message_id, const char* restrict crtc, + const char* restrict coalesce, const char* restrict high_priority, + const char* restrict low_priority); + +/** + * Handle a ‘Command: set-gamma’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @param crtc The value of the ‘CRTC’ header + * @param priority The value of the ‘Priority’ header + * @param class The value of the ‘Class’ header + * @param lifespan The value of the ‘Lifespan’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +GCC_ONLY(__attribute__((nonnull(2)))) +int handle_set_gamma(size_t conn, const char* restrict message_id, const char* restrict crtc, + const char* restrict priority, const char* restrict class, const char* restrict lifespan); + + +/** + * Recalculate the resulting gamma and + * update push the new gamma ramps to the CRTC + * + * @param output The output + * @param first_updated The index of the first added or removed filter + * @return Zero on success, -1 on error + */ +GCC_ONLY(__attribute__((nonnull))) +int flush_filters(struct output* restrict output, size_t first_updated); + + +#endif + diff --git a/src/coopgammad.c b/src/coopgammad.c index 7e6422e..df8f95d 100644 --- a/src/coopgammad.c +++ b/src/coopgammad.c @@ -15,7 +15,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include +#include "arg.h" +#include "util.h" +#include "server.h" +#include "state.h" #include #include @@ -30,11 +33,6 @@ #include #include -#include "arg.h" -#include "output.h" -#include "util.h" -#include "server.h" - /** @@ -55,50 +53,12 @@ -extern char* restrict argv0_real; -extern struct output* restrict outputs; -extern size_t outputs_n; -extern int socketfd; extern char* restrict pidpath; extern char* restrict socketpath; extern int gerror; -extern int method; -extern char* restrict sitename; -extern libgamma_site_state_t site; -extern libgamma_partition_state_t* restrict partitions; -extern libgamma_crtc_state_t* restrict crtcs; -extern volatile sig_atomic_t reexec; -extern volatile sig_atomic_t terminate; -extern volatile sig_atomic_t connection; -/** - * The name of the process - */ -char* restrict argv0; - -/** - * The real pathname of the process's binary, - * `NULL` if `argv0` is satisfactory - */ -char* restrict argv0_real = NULL; - -/** - * Array of all outputs - */ -struct output* restrict outputs = NULL; - -/** - * The nubmer of elements in `outputs` - */ -size_t outputs_n = 0; - -/** - * The server socket's file descriptor - */ -int socketfd = -1; - /** * The pathname of the PID file */ @@ -112,52 +72,7 @@ char* restrict socketpath = NULL; /** * Error code returned by libgamma */ -int gerror; - -/** - * The adjustment method, -1 for automatic - */ -int method = -1; - -/** - * The site's name, may be `NULL` - */ -char* restrict sitename = NULL; - -/** - * The libgamma site state - */ -libgamma_site_state_t site; - -/** - * The libgamma partition states - */ -libgamma_partition_state_t* restrict partitions = NULL; - -/** - * The libgamma CRTC states - */ -libgamma_crtc_state_t* restrict crtcs = NULL; - -/** - * Has the process receive a signal - * telling it to re-execute? - */ -volatile sig_atomic_t reexec = 0; - -/** - * Has the process receive a signal - * telling it to terminate? - */ -volatile sig_atomic_t terminate = 0; - -/** - * Has the process receive a to - * disconnect from or reconnect to - * the site? 1 if disconnct, 2 if - * reconnect, 0 otherwise. - */ -volatile sig_atomic_t connection = 0; +int gerror; /* do not marshal */ @@ -905,12 +820,16 @@ static void destroy(int full) { size_t i; + if (full) + disconnect_all(); + if (full && (socketfd >= 0)) { shutdown(socketfd, SHUT_RDWR); close(socketfd); unlink(socketpath); } + #define RESTORE_RAMPS(SUFFIX, MEMBER) \ do \ if (outputs[i].saved_ramps.MEMBER.red != NULL) \ @@ -922,51 +841,36 @@ static void destroy(int full) while (0) if (outputs != NULL) for (i = 0; i < outputs_n; i++) - { - if (full && (outputs[i].supported != LIBGAMMA_NO)) - switch (outputs[i].depth) - { - case 8: - RESTORE_RAMPS(8, u8); - break; - case 16: - RESTORE_RAMPS(16, u16); - break; - case 32: - RESTORE_RAMPS(32, u32); - break; - case 64: - RESTORE_RAMPS(64, u64); - break; - case -1: - RESTORE_RAMPS(f, f); - break; - case -2: - RESTORE_RAMPS(d, d); - break; - default: - break; /* impossible */ - } - if (crtcs == NULL) - libgamma_crtc_destroy(outputs[i].crtc + i); - output_destroy(outputs + i); - } - free(outputs); - if (crtcs != NULL) - for (i = 0; i < outputs_n; i++) - libgamma_crtc_destroy(crtcs + i); - free(crtcs); - if (partitions != NULL) - for (i = 0; i < site.partitions_available; i++) - libgamma_partition_destroy(partitions + i); - free(partitions); - libgamma_site_destroy(&site); + if (full && (outputs[i].supported != LIBGAMMA_NO)) + switch (outputs[i].depth) + { + case 8: + RESTORE_RAMPS(8, u8); + break; + case 16: + RESTORE_RAMPS(16, u16); + break; + case 32: + RESTORE_RAMPS(32, u32); + break; + case 64: + RESTORE_RAMPS(64, u64); + break; + case -1: + RESTORE_RAMPS(f, f); + break; + case -2: + RESTORE_RAMPS(d, d); + break; + default: + break; /* impossible */ + } + free(socketpath); if (full && (pidpath != NULL)) unlink(pidpath); free(pidpath); - free(argv0_real); - free(sitename); + state_destroy(); } @@ -987,38 +891,13 @@ static void destroy(int full) */ static size_t marshal(void* restrict buf) { - size_t off = 0, i, n; + size_t off = 0, n; char* restrict bs = buf; if (bs != NULL) *(int*)(bs + off) = MARSHAL_VERSION; off += sizeof(int); - if (argv0_real == NULL) - { - if (bs != NULL) - *(bs + off) = '\0'; - off += 1; - } - else - { - n = strlen(argv0_real) + 1; - if (bs != NULL) - memcpy(bs + off, argv0_real, n); - off += n; - } - - if (bs != NULL) - *(size_t*)(bs + off) = outputs_n; - off += sizeof(size_t); - - for (i = 0; i < outputs_n; i++) - off += output_marshal(outputs + i, bs ? bs + off : NULL); - - if (bs != NULL) - *(int*)(bs + off) = socketfd; - off += sizeof(int); - n = strlen(pidpath) + 1; if (bs != NULL) memcpy(bs + off, pidpath, n); @@ -1029,26 +908,7 @@ static size_t marshal(void* restrict buf) memcpy(bs + off, socketpath, n); off += n; - off += server_marshal(bs ? bs + off : NULL); - - if (bs != NULL) - *(int*)(bs + off) = method; - off += sizeof(int); - - if (bs != NULL) - *(int*)(bs + off) = sitename != NULL; - off += sizeof(int); - if (sitename != NULL) - { - n = strlen(sitename) + 1; - if (bs != NULL) - memcpy(bs + off, sitename, n); - off += n; - } - - if (bs != NULL) - *(sig_atomic_t*)(bs + off) = connection; - off += sizeof(sig_atomic_t); + off += state_marshal(bs ? bs + off : NULL); return off; } @@ -1061,12 +921,12 @@ static size_t marshal(void* restrict buf) * @return The number of marshalled bytes, 0 on error */ GCC_ONLY(__attribute__((nonnull))) -static size_t unmarshal(void* restrict buf) +static size_t unmarshal(const void* restrict buf) { - size_t off = 0, i, n; - char* restrict bs = buf; + size_t off = 0, n; + const char* restrict bs = buf; - if (*(int*)(bs + off) != MARSHAL_VERSION) + if (*(const int*)(bs + off) != MARSHAL_VERSION) { fprintf(stderr, "%s: re-executing to incompatible version, sorry about that\n", argv0); errno = 0; @@ -1074,30 +934,6 @@ static size_t unmarshal(void* restrict buf) } off += sizeof(int); - if (*(bs + off)) - { - off += 1; - n = strlen(bs + off) + 1; - if (!(argv0_real = memdup(bs + off, n))) - return 0; - off += n; - } - else - off += 1; - - outputs_n = *(size_t*)(bs + off); - off += sizeof(size_t); - - for (i = 0; i < outputs_n; i++) - { - off += n = output_unmarshal(outputs + i, bs + off); - if (n == 0) - return 0; - } - - socketfd = *(int*)(bs + off); - off += sizeof(int); - n = strlen(bs + off) + 1; if (!(pidpath = memdup(bs + off, n))) return 0; @@ -1108,27 +944,10 @@ static size_t unmarshal(void* restrict buf) return 0; off += n; - off += n = server_unmarshal(bs + off); + off += n = state_unmarshal(bs + off); if (n == 0) return 0; - method = *(int*)(bs + off); - off += sizeof(int); - - if (*(int*)(bs + off)) - { - off += sizeof(int); - n = strlen(bs + off) + 1; - if (!(sitename = memdup(bs + off, n))) - return 0; - off += n; - } - else - off += sizeof(int); - - connection = *(sig_atomic_t*)(bs + off); - off += sizeof(sig_atomic_t); - return off; } diff --git a/src/crtc-server/server.c b/src/crtc-server/server.c new file mode 100644 index 0000000..6c16cee --- /dev/null +++ b/src/crtc-server/server.c @@ -0,0 +1,59 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "server.h" +#include "../state.h" +#include "../communication.h" + +#include + + + +/** + * Handle a ‘Command: enumerate-crtcs’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +int handle_enumerate_crtcs(size_t conn, const char* restrict message_id) +{ + size_t i, n = 0, len; + char* restrict buf; + + for (i = 0; i < outputs_n; i++) + n += strlen(outputs[i].name) + 1; + + MAKE_MESSAGE(&buf, &n, 0, + "Command: crtc-enumeration\n" + "In response to: %s\n" + "Length: %zu\n" + "\n", + message_id, n); + + for (i = 0; i < outputs_n; i++) + { + len = strlen(outputs[i].name); + memcpy(buf + n, outputs[i].name, len); + buf[n + len] = '\n'; + n += len + 1; + } + + return send_message(conn, buf, n); +} + diff --git a/src/crtc-server/server.h b/src/crtc-server/server.h new file mode 100644 index 0000000..ffe4f64 --- /dev/null +++ b/src/crtc-server/server.h @@ -0,0 +1,49 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef CRTC_SERVER_SERVER_H +#define CRTC_SERVER_SERVER_H + + +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Handle a ‘Command: enumerate-crtcs’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +GCC_ONLY(__attribute__((nonnull))) +int handle_enumerate_crtcs(size_t conn, const char* restrict message_id); + + +#endif + diff --git a/src/filter.c b/src/filter.c deleted file mode 100644 index c335e0b..0000000 --- a/src/filter.c +++ /dev/null @@ -1,144 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include "filter.h" -#include "util.h" - -#include -#include - - - -/** - * Free all resources allocated to a filter. - * The allocation of `filter` itself is not freed. - * - * @param this The filter - */ -void filter_destroy(struct filter* restrict this) -{ - free(this->class); - free(this->ramps); -} - - - -#if defined(__clang__) -# pragma GCC diagnostic ignored "-Wcast-align" -#endif - - -/** - * Marshal a filter - * - * @param this The filter - * @param buf Output buffer for the marshalled filter, - * `NULL` just measure how large the buffers - * needs to be - * @param ramps_size The byte-size of `this->ramps` - * @return The number of marshalled byte - */ -size_t filter_marshal(const struct filter* restrict this, void* restrict buf, size_t ramps_size) -{ - size_t off = 0, n; - char nonnulls = 0; - char* restrict bs = buf; - - if (bs != NULL) - { - if (this->class != NULL) nonnulls |= 1; - if (this->ramps != NULL) nonnulls |= 2; - *(bs + off) = nonnulls; - } - off += 1; - - if (bs != NULL) - *(int64_t*)(bs + off) = this->priority; - off += sizeof(int64_t); - - if (bs != NULL) - *(enum lifespan*)(bs + off) = this->lifespan; - off += sizeof(enum lifespan); - - if (this->class != NULL) - { - n = strlen(this->class) + 1; - if (bs != NULL) - memcpy(bs + off, this->class, n); - off += n; - } - - if (this->ramps != NULL) - { - if (bs != NULL) - memcpy(bs + off, this->ramps, ramps_size); - off += ramps_size; - } - - return off; -} - - -/** - * Unmarshal a filter - * - * @param this Output for the filter - * @param buf Buffer with the marshalled filter - * @param ramps_size The byte-size of `this->ramps` - * @return The number of unmarshalled bytes, 0 on error - */ -size_t filter_unmarshal(struct filter* restrict this, const void* restrict buf, size_t ramps_size) -{ - size_t off = 0, n; - char nonnulls = 0; - const char* restrict bs = buf; - - nonnulls = *(bs + off); - off += 1; - - this->class = NULL; - this->ramps = NULL; - - this->priority = *(const int64_t*)(bs + off); - off += sizeof(int64_t); - - this->lifespan = *(const enum lifespan*)(bs + off); - off += sizeof(enum lifespan); - - if (nonnulls & 1) - { - n = strlen(bs + off) + 1; - if (!(this->class = memdup(bs + off, n))) - goto fail; - off += n; - } - - if (nonnulls & 2) - { - if (!(this->ramps = memdup(bs + off, ramps_size))) - goto fail; - off += ramps_size; - } - - return off; - - fail: - free(this->class); - free(this->ramps); - return 0; -} - diff --git a/src/filter.h b/src/filter.h deleted file mode 100644 index 5691f6c..0000000 --- a/src/filter.h +++ /dev/null @@ -1,131 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -#ifndef GCC_ONLY -# if defined(__GNUC__) && !defined(__clang__) -# define GCC_ONLY(...) __VA_ARGS__ -# else -# define GCC_ONLY(...) /* nothing */ -# endif -#endif - - - -/** - * The lifespan of a filter - */ -enum lifespan -{ - /** - * The filter should be applied - * until it is explicitly removed - */ - LIFESPAN_UNTIL_REMOVAL, - - /** - * The filter should be applied - * until the client exists - */ - LIFESPAN_UNTIL_DEATH, - - /** - * The filter should be removed now - */ - LIFESPAN_REMOVE - -}; - - -/** - * Information about a filter - */ -struct filter -{ - /** - * The client that applied it. This need not be - * set unless `.lifespan == LIFESPAN_UNTIL_DEATH` - * and unless the process itself added this. - * This is the file descriptor of the client's - * connection. - */ - int client; - - /** - * The lifespan of the filter - */ - enum lifespan lifespan; - - /** - * The priority of the filter - */ - int64_t priority; - - /** - * Identifier for the filter - */ - char* class; - - /** - * The gamma ramp adjustments for the filter. - * This is raw binary data. `NULL` iff - * `lifespan == LIFESPAN_REMOVE`. - */ - void* ramps; - -}; - - - -/** - * Free all resources allocated to a filter. - * The allocation of `filter` itself is not freed. - * - * @param this The filter - */ -GCC_ONLY(__attribute__((nonnull))) -void filter_destroy(struct filter* restrict this); - -/** - * Marshal a filter - * - * @param this The filter - * @param buf Output buffer for the marshalled filter, - * `NULL` just measure how large the buffers - * needs to be - * @param ramps_size The byte-size of `filter->ramps` - * @return The number of marshalled byte - */ -GCC_ONLY(__attribute__((nonnull(1)))) -size_t filter_marshal(const struct filter* restrict this, void* restrict buf, size_t ramps_size); - -/** - * Unmarshal a filter - * - * @param this Output for the filter, `.red_size`, `.green_size`, - * and `.blue_size` must already be set - * @param buf Buffer with the marshalled filter - * @param ramps_size The byte-size of `filter->ramps` - * @return The number of unmarshalled bytes, 0 on error - */ -GCC_ONLY(__attribute__((nonnull))) -size_t filter_unmarshal(struct filter* restrict this, const void* restrict buf, size_t ramps_size); - diff --git a/src/gamma-server/server.c b/src/gamma-server/server.c new file mode 100644 index 0000000..b6ec852 --- /dev/null +++ b/src/gamma-server/server.c @@ -0,0 +1,144 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "server.h" +#include "../state.h" +#include "../communication.h" + +#include + + + +#if defined(__clang__) +# pragma GCC diagnostic ignored "-Wswitch-enum" +#endif + + + +/** + * Handle a ‘Command: set-gamma’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @param crtc The value of the ‘CRTC’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +int handle_get_gamma_info(size_t conn, const char* restrict message_id, const char* restrict crtc) +{ + struct output* restrict output; + char* restrict buf; + char depth[3]; + const char* supported; + size_t n; + + if (crtc == NULL) return send_error("protocol error: 'CRTC' header omitted"); + + output = output_find_by_name(crtc, outputs, outputs_n); + if (output == NULL) + return send_error("selected CRTC does not exist"); + + switch (output->depth) + { + case -2: strcpy(depth, "d"); break; + case -1: strcpy(depth, "f"); break; + default: + sprintf(depth, "%i", output->depth); + break; + } + + switch (output->supported) + { + case LIBGAMMA_YES: supported = "yes"; break; + case LIBGAMMA_NO: supported = "no"; break; + default: supported = "maybe"; break; + } + + MAKE_MESSAGE(&buf, &n, 0, + "In response to: %s\n" + "Cooperative: yes\n" /* In mds: say ‘no’, mds-coopgamma changes to ‘yes’.” */ + "Depth: %s\n" + "Red size: %zu\n" + "Green size: %zu\n" + "Blue size: %zu\n" + "Gamma support: %s\n" + "\n", + message_id, depth, output->red_size, output->green_size, + output->blue_size, supported); + + return send_message(conn, buf, n); +} + + +/** + * Set the gamma ramps on an output + * + * @param output The output + * @param ramps The gamma ramps + */ +void set_gamma(const struct output* restrict output, const union gamma_ramps* restrict ramps) +{ + int r = 0; + + if (connected) + { + switch (output->depth) + { + case 8: r = libgamma_crtc_set_gamma_ramps8(output->crtc, ramps->u8); break; + case 16: r = libgamma_crtc_set_gamma_ramps16(output->crtc, ramps->u16); break; + case 32: r = libgamma_crtc_set_gamma_ramps32(output->crtc, ramps->u32); break; + case 64: r = libgamma_crtc_set_gamma_ramps64(output->crtc, ramps->u64); break; + case -1: r = libgamma_crtc_set_gamma_rampsf(output->crtc, ramps->f); break; + case -2: r = libgamma_crtc_set_gamma_rampsd(output->crtc, ramps->d); break; + default: + abort(); + } + if (r) + libgamma_perror(argv0, r); /* Not fatal */ + } +} + + +/** + * Disconnect from the site + * + * @return Zero on success, -1 on error + */ +int disconnect(void) +{ + if (!connected) + return 0; + + connected = 0; + return 0; /* TODO disconnect() */ +} + + +/** + * Reconnect to the site + * + * @return Zero on success, -1 on error + */ +int reconnect(void) +{ + if (connected) + return 0; + + connected = 1; + return 0; /* TODO reconnect() */ +} + diff --git a/src/gamma-server/server.h b/src/gamma-server/server.h new file mode 100644 index 0000000..b84a836 --- /dev/null +++ b/src/gamma-server/server.h @@ -0,0 +1,75 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef GAMMA_SERVER_SERVER_H +#define GAMMA_SERVER_SERVER_H + + +#include "../types/output.h" + +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Handle a ‘Command: set-gamma’ message + * + * @param conn The index of the connection + * @param message_id The value of the ‘Message ID’ header + * @param crtc The value of the ‘CRTC’ header + * @return Zero on success (even if ignored), -1 on error, + * 1 if connection closed + */ +GCC_ONLY(__attribute__((nonnull(2)))) +int handle_get_gamma_info(size_t conn, const char* restrict message_id, const char* restrict crtc); + +/** + * Set the gamma ramps on an output + * + * @param output The output + * @param ramps The gamma ramps + */ +GCC_ONLY(__attribute__((nonnull))) +void set_gamma(const struct output* restrict output, const union gamma_ramps* restrict ramps); + +/** + * Disconnect from the site + * + * @return Zero on success, -1 on error + */ +int disconnect(void); + +/** + * Reconnect to the site + * + * @return Zero on success, -1 on error + */ +int reconnect(void); + + +#endif + diff --git a/src/message.c b/src/message.c deleted file mode 100644 index 48bdbff..0000000 --- a/src/message.c +++ /dev/null @@ -1,572 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include "message.h" -#include "util.h" - -#include -#include -#include -#include -#include - - - -/** - * Initialise a message slot so that it can - * be used by to read messages - * - * @param this Memory slot in which to store the new message - * @return Non-zero on error, `errno` will be set accordingly - */ -int message_initialise(struct message* restrict this) -{ - this->headers = NULL; - this->header_count = 0; - this->payload = NULL; - this->payload_size = 0; - this->payload_ptr = 0; - this->buffer_size = 128; - this->buffer_ptr = 0; - this->stage = 0; - this->buffer = malloc(this->buffer_size); - if (this->buffer == NULL) - return -1; - return 0; -} - - -/** - * Release all resources in a message, should - * be done even if initialisation fails - * - * @param this The message - */ -void message_destroy(struct message* restrict this) -{ - size_t i; - if (this->headers != NULL) - for (i = 0; i < this->header_count; i++) - free(this->headers[i]); - - free(this->headers); - free(this->payload); - free(this->buffer); -} - - - -#if defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wcast-align" -#endif - - -/** - * Marshal a message for state serialisation - * - * @param this The message - * @param buf Output buffer for the marshalled data, - * `NULL` just measure how large the buffers - * needs to be - * @return The number of marshalled byte - */ -size_t message_marshal(const struct message* restrict this, void* restrict buf) -{ - size_t i, n, off = 0; - char* bs = buf; - - if (bs != NULL) - *(size_t*)(bs + off) = this->header_count; - off += sizeof(size_t); - - if (bs != NULL) - *(size_t*)(bs + off) = this->payload_size; - off += sizeof(size_t); - - if (bs != NULL) - *(size_t*)(bs + off) = this->payload_ptr; - off += sizeof(size_t); - - if (bs != NULL) - *(size_t*)(bs + off) = this->buffer_ptr; - off += sizeof(size_t); - - if (bs != NULL) - *(int*)(bs + off) = this->stage; - off += sizeof(int); - - for (i = 0; i < this->header_count; i++) - { - n = strlen(this->headers[i]) + 1; - if (bs != NULL) - memcpy(bs + off, this->headers[i], n); - off += n; - } - - if (bs != NULL) - memcpy(bs + off, this->payload, this->payload_ptr); - off += this->payload_ptr; - - if (bs != NULL) - memcpy(bs + off, this->buffer, this->buffer_ptr); - off += this->buffer_ptr; - - return off; -} - - -/** - * Unmarshal a message for state deserialisation - * - * @param this Memory slot in which to store the new message - * @param buf In buffer with the marshalled data - * @return The number of unmarshalled bytes, 0 on error - */ -size_t message_unmarshal(struct message* restrict this, const void* restrict buf) -{ - size_t i, n, off = 0, header_count; - const char* bs = buf; - - this->header_count = 0; - - header_count = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->payload_size = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->payload_ptr = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->buffer_size = this->buffer_ptr = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->stage = *(const int*)(bs + off); - off += sizeof(int); - - /* Make sure that the pointers are NULL so that they are - not freed without being allocated when the message is - destroyed if this function fails. */ - this->headers = NULL; - this->payload = NULL; - this->buffer = NULL; - - /* To 2-power-multiple of 128 bytes. */ - this->buffer_size >>= 7; - if (this->buffer_size == 0) - this->buffer_size = 1; - else - { - this->buffer_size -= 1; - this->buffer_size |= this->buffer_size >> 1; - this->buffer_size |= this->buffer_size >> 2; - this->buffer_size |= this->buffer_size >> 4; - this->buffer_size |= this->buffer_size >> 8; - this->buffer_size |= this->buffer_size >> 16; -#if SIZE_MAX == UINT64_MAX - this->buffer_size |= this->buffer_size >> 32; -#endif - this->buffer_size += 1; - } - this->buffer_size <<= 7; - - /* Allocate header list, payload and read buffer. */ - - if (header_count > 0) - if (!(this->headers = malloc(header_count * sizeof(char*)))) - goto fail; - - if (this->payload_size > 0) - if (!(this->payload = malloc(this->payload_size))) - goto fail; - - if (!(this->buffer = malloc(this->buffer_size))) - goto fail; - - /* Fill the header list, payload and read buffer. */ - - for (i = 0; i < header_count; i++) - { - n = strlen(bs + off) + 1; - this->headers[i] = memdup(bs + off, n); - if (this->headers[i] == NULL) - goto fail; - off += n; - this->header_count++; - } - - memcpy(this->payload, bs + off, this->payload_ptr); - off += this->payload_ptr; - - memcpy(this->buffer, bs + off, this->buffer_ptr); - off += this->buffer_ptr; - - return off; - - fail: - return 0; -} - - -#if defined(__clang__) -# pragma GCC diagnostic pop -#endif - - - -/** - * Extend the header list's allocation - * - * @param this The message - * @param extent The number of additional entries - * @return Zero on success, -1 on error - */ -GCC_ONLY(__attribute__((nonnull))) -static int extend_headers(struct message* restrict this, size_t extent) -{ - char** new; - if (!(new = realloc(this->headers, (this->header_count + extent) * sizeof(char*)))) - return -1; - this->headers = new; - return 0; -} - - -/** - * Extend the read buffer by way of doubling - * - * @param this The message - * @return Zero on success, -1 on error - */ -GCC_ONLY(__attribute__((nonnull))) -static int extend_buffer(struct message* restrict this) -{ - char* restrict new; - if (!(new = realloc(this->buffer, (this->buffer_size << 1) * sizeof(char)))) - return -1; - this->buffer = new; - this->buffer_size <<= 1; - return 0; -} - - -/** - * Reset the header list and the payload - * - * @param this The message - */ -GCC_ONLY(__attribute__((nonnull))) -static void reset_message(struct message* restrict this) -{ - size_t i; - if (this->headers != NULL) - for (i = 0; i < this->header_count; i++) - free(this->headers[i]); - free(this->headers); - this->headers = NULL; - this->header_count = 0; - - free(this->payload); - this->payload = NULL; - this->payload_size = 0; - this->payload_ptr = 0; -} - - -/** - * Read the headers the message and determine, and store, its payload's length - * - * @param this The message - * @return Zero on success, negative on error (malformated message: unrecoverable state) - */ -GCC_ONLY(__attribute__((pure, nonnull))) -static int get_payload_length(struct message* restrict this) -{ - char* header; - size_t i; - - for (i = 0; i < this->header_count; i++) - if (strstr(this->headers[i], "Length: ") == this->headers[i]) - { - /* Store the message length. */ - header = this->headers[i] + strlen("Length: "); - this->payload_size = (size_t)atol(header); - - /* Do not except a length that is not correctly formated. */ - for (; *header; header++) - if ((*header < '0') || ('9' < *header)) - return -2; /* Malformated value, enters unrecoverable state. */ - - /* Stop searching for the ‘Length’ header, we have found and parsed it. */ - break; - } - - return 0; -} - - -/** - * Verify that a header is correctly formatted - * - * @param header The header, must be NUL-terminated - * @param length The length of the header - * @return Zero if valid, negative if invalid (malformated message: unrecoverable state) - */ -GCC_ONLY(__attribute__((pure, nonnull))) -static int validate_header(const char* restrict header, size_t length) -{ - char* restrict p = memchr(header, ':', length * sizeof(char)); - - if (verify_utf8(header, 0) < 0) - /* Either the string is not UTF-8, or your are under an UTF-8 attack, - let's just call this unrecoverable because the client will not correct. */ - return -2; - - if ((p == NULL) || /* Buck you, rawmemchr should not segfault the program. */ - (p[1] != ' ')) /* Also an invalid format. ' ' is mandated after the ':'. */ - return -2; - - return 0; -} - - -/** - * Remove the beginning of the read buffer - * - * @param this The message - * @param length The number of characters to remove - * @param update_ptr Whether to update the buffer pointer - */ -GCC_ONLY(__attribute__((nonnull))) -static void unbuffer_beginning(struct message* restrict this, size_t length, int update_ptr) -{ - memmove(this->buffer, this->buffer + length, (this->buffer_ptr - length) * sizeof(char)); - if (update_ptr) - this->buffer_ptr -= length; -} - - -/** - * Remove the header–payload delimiter from the buffer, - * get the payload's size and allocate the payload - * - * @param this The message - * @return The return value follows the rules of `message_read` - */ -GCC_ONLY(__attribute__((nonnull))) -static int initialise_payload(struct message* restrict this) -{ - /* Remove the \n (end of empty line) we found from the buffer. */ - unbuffer_beginning(this, 1, 1); - - /* Get the length of the payload. */ - if (get_payload_length(this) < 0) - return -2; /* Malformated value, enters unrecoverable state. */ - - /* Allocate the payload buffer. */ - if (this->payload_size > 0) - if (!(this->payload = malloc(this->payload_size))) - return -1; - - return 0; -} - - -/** - * Create a header from the buffer and store it - * - * @param this The message - * @param length The length of the header, including LF-termination - * @return The return value follows the rules of `message_read` - */ -GCC_ONLY(__attribute__((nonnull))) -static int store_header(struct message* restrict this, size_t length) -{ - char* restrict header; - - /* Allocate the header. */ - if (!(header = malloc(length))) /* Last char is a LF, which is substituted with NUL. */ - return -1; - /* Copy the header data into the allocated header, */ - memcpy(header, this->buffer, length * sizeof(char)); - /* and NUL-terminate it. */ - header[length - 1] = '\0'; - - /* Remove the header data from the read buffer. */ - unbuffer_beginning(this, length, 1); - - /* Make sure the the header syntax is correct so that - the program does not need to care about it. */ - if (validate_header(header, length)) - { - free(header); - return -2; - } - - /* Store the header in the header list. */ - this->headers[this->header_count++] = header; - - return 0; -} - - -/** - * Continue reading from the socket into the buffer - * - * @param this The message - * @param fd The file descriptor of the socket - * @return The return value follows the rules of `message_read` - */ -GCC_ONLY(__attribute__((nonnull))) -static int continue_read(struct message* restrict this, int fd) -{ - size_t n; - ssize_t got; - int r; - - /* Figure out how much space we have left in the read buffer. */ - n = this->buffer_size - this->buffer_ptr; - - /* If we do not have too much left, */ - if (n < 128) - { - /* grow the buffer, */ - if ((r = extend_buffer(this)) < 0) - return r; - - /* and recalculate how much space we have left. */ - n = this->buffer_size - this->buffer_ptr; - } - - /* Then read from the socket. */ - errno = 0; - got = recv(fd, this->buffer + this->buffer_ptr, n, 0); - this->buffer_ptr += (size_t)(got < 0 ? 0 : got); - if (errno) - return -1; - if (got == 0) - { - errno = ECONNRESET; - return -1; - } - - return 0; -} - - -/** - * Read the next message from a file descriptor - * - * @param this Memory slot in which to store the new message - * @param fd The file descriptor - * @return 0: At least one message is available - * -1: Exceptional connection: - * EINTR: System call interrupted - * EAGAIN: No message is available - * EWOULDBLOCK: No message is available - * ECONNRESET: Connection closed - * Other: Failure - * -2: Corrupt message (unrecoverable) - */ -GCC_ONLY(__attribute__((nonnull))) -int message_read(struct message* restrict this, int fd) -{ - size_t header_commit_buffer = 0; - int r; - - /* If we are at stage 2, we are done and it is time to start over. - This is important because the function could have been interrupted. */ - if (this->stage == 2) - { - reset_message(this); - this->stage = 0; - } - - /* Read from file descriptor until we have a full message. */ - for (;;) - { - char* p; - size_t length; - - /* Stage 0: headers. */ - /* Read all headers that we have stored into the read buffer. */ - while ((this->stage == 0) && - ((p = memchr(this->buffer, '\n', this->buffer_ptr * sizeof(char))) != NULL)) - if ((length = (size_t)(p - this->buffer))) - { - /* We have found a header. */ - - /* On every eighth header found with this function call, - we prepare the header list for eight more headers so - that it does not need to be reallocated again and again. */ - if (header_commit_buffer == 0) - if ((r = extend_headers(this, header_commit_buffer = 8)) < 0) - return r; - - /* Create and store header. */ - if ((r = store_header(this, length + 1)) < 0) - return r; - header_commit_buffer -= 1; - } - else - { - /* We have found an empty line, i.e. the end of the headers. */ - - /* Remove the header–payload delimiter from the buffer, - get the payload's size and allocate the payload. */ - if ((r = initialise_payload(this)) < 0) - return r; - - /* Mark end of stage, next stage is getting the payload. */ - this->stage = 1; - } - - - /* Stage 1: payload. */ - if ((this->stage == 1) && (this->payload_size > 0)) - { - /* How much of the payload that has not yet been filled. */ - size_t need = this->payload_size - this->payload_ptr; - /* How much we have of that what is needed. */ - size_t move = this->buffer_ptr < need ? this->buffer_ptr : need; - - /* Copy what we have, and remove it from the the read buffer. */ - memcpy(this->payload + this->payload_ptr, this->buffer, move * sizeof(char)); - unbuffer_beginning(this, move, 1); - - /* Keep track of how much we have read. */ - this->payload_ptr += move; - } - if ((this->stage == 1) && (this->payload_ptr == this->payload_size)) - { - /* If we have filled the payload (or there was no payload), - mark the end of this stage, i.e. that the message is - complete, and return with success. */ - this->stage = 2; - return 0; - } - - - /* If stage 1 was not completed. */ - - /* Continue reading from the socket into the buffer. */ - if ((r = continue_read(this, fd)) < 0) - return r; - } -} - diff --git a/src/message.h b/src/message.h deleted file mode 100644 index 15a9330..0000000 --- a/src/message.h +++ /dev/null @@ -1,153 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -#ifndef GCC_ONLY -# if defined(__GNUC__) && !defined(__clang__) -# define GCC_ONLY(...) __VA_ARGS__ -# else -# define GCC_ONLY(...) /* nothing */ -# endif -#endif - - - -/** - * Message passed between a server and a client - */ -struct message -{ - /** - * The headers in the message, each element in this list - * as an unparsed header, it consists of both the header - * name and its associated value, joined by ": ". A header - * cannot be `NULL` (unless its memory allocation failed,) - * but `headers` itself is `NULL` if there are no headers. - * The "Length" header should be included in this list. - */ - char** restrict headers; - - /** - * The number of headers in the message - */ - size_t header_count; - - /** - * The payload of the message, `NULL` if none (of zero-length) - */ - char* restrict payload; - - /** - * The size of the payload - */ - size_t payload_size; - - /** - * How much of the payload that has been stored (internal data) - */ - size_t payload_ptr; - - /** - * Internal buffer for the reading function (internal data) - */ - char* restrict buffer; - - /** - * The size allocated to `buffer` (internal data) - */ - size_t buffer_size; - - /** - * The number of bytes used in `buffer` (internal data) - */ - size_t buffer_ptr; - - /** - * 0 while reading headers, 1 while reading payload, and 2 when done (internal data) - */ - int stage; - -#if INT_MAX != LONG_MAX - int padding__; -#endif - -}; - - - -/** - * Initialise a message slot so that it can - * be used by to read messages - * - * @param this Memory slot in which to store the new message - * @return Non-zero on error, `errno` will be set accordingly - */ -GCC_ONLY(__attribute__((nonnull))) -int message_initialise(struct message* restrict this); - -/** - * Release all resources in a message, should - * be done even if initialisation fails - * - * @param this The message - */ -GCC_ONLY(__attribute__((nonnull))) -void message_destroy(struct message* restrict this); - -/** - * Marshal a message for state serialisation - * - * @param this The message - * @param buf Output buffer for the marshalled data, - * `NULL` just measure how large the buffers - * needs to be - * @return The number of marshalled byte - */ -GCC_ONLY(__attribute__((nonnull(1)))) -size_t message_marshal(const struct message* restrict this, void* restrict buf); - -/** - * Unmarshal a message for state deserialisation - * - * @param this Memory slot in which to store the new message - * @param buf In buffer with the marshalled data - * @return The number of unmarshalled bytes, 0 on error - */ -GCC_ONLY(__attribute__((nonnull))) -size_t message_unmarshal(struct message* restrict this, const void* restrict buf); - -/** - * Read the next message from a file descriptor - * - * @param this Memory slot in which to store the new message - * @param fd The file descriptor - * @return 0: At least one message is available - * -1: Exceptional connection: - * EINTR: System call interrupted - * EAGAIN: No message is available - * EWOULDBLOCK: No message is available - * ECONNRESET: Connection closed - * Other: Failure - * -2: Corrupt message (unrecoverable) - */ -GCC_ONLY(__attribute__((nonnull))) -int message_read(struct message* restrict this, int fd); - diff --git a/src/output.c b/src/output.c deleted file mode 100644 index e8913dd..0000000 --- a/src/output.c +++ /dev/null @@ -1,482 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include "output.h" -#include "util.h" - -#include - -#include -#include -#include - - - -/** - * The name of the process - */ -extern char* restrict argv0; - - - -/** - * Free all resources allocated to an output. - * The allocation of `output` itself is not freed, - * nor is its the libgamma destroyed. - * - * @param this The output - */ -void output_destroy(struct output* restrict this) -{ - size_t i; - - if (this->supported != LIBGAMMA_NO) - switch (this->depth) - { - case 8: - libgamma_gamma_ramps8_destroy(&(this->saved_ramps.u8)); - for (i = 0; i < this->table_size; i++) - libgamma_gamma_ramps8_destroy(&(this->table_sums[i].u8)); - break; - case 16: - libgamma_gamma_ramps16_destroy(&(this->saved_ramps.u16)); - for (i = 0; i < this->table_size; i++) - libgamma_gamma_ramps16_destroy(&(this->table_sums[i].u16)); - break; - case 32: - libgamma_gamma_ramps32_destroy(&(this->saved_ramps.u32)); - for (i = 0; i < this->table_size; i++) - libgamma_gamma_ramps32_destroy(&(this->table_sums[i].u32)); - break; - case 64: - libgamma_gamma_ramps64_destroy(&(this->saved_ramps.u64)); - for (i = 0; i < this->table_size; i++) - libgamma_gamma_ramps64_destroy(&(this->table_sums[i].u64)); - break; - case -1: - libgamma_gamma_rampsf_destroy(&(this->saved_ramps.f)); - for (i = 0; i < this->table_size; i++) - libgamma_gamma_rampsf_destroy(&(this->table_sums[i].f)); - break; - case -2: - libgamma_gamma_rampsd_destroy(&(this->saved_ramps.d)); - for (i = 0; i < this->table_size; i++) - libgamma_gamma_rampsd_destroy(&(this->table_sums[i].d)); - break; - default: - break; /* impossible */ - } - - for (i = 0; i < this->table_size; i++) - filter_destroy(this->table_filters + i); - - free(this->table_filters); - free(this->table_sums); - free(this->name); -} - - - -#if defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wcast-align" -#endif - - -/** - * Marshal an output - * - * @param this The output - * @param buf Output buffer for the marshalled output, - * `NULL` just measure how large the buffers - * needs to be - * @return The number of marshalled byte - */ -size_t output_marshal(const struct output* restrict this, void* restrict buf) -{ - size_t off = 0, i, n; - char* bs = buf; - - if (bs != NULL) - *(signed*)(bs + off) = this->depth; - off += sizeof(signed); - - if (bs != NULL) - *(size_t*)(bs + off) = this->red_size; - off += sizeof(size_t); - - if (bs != NULL) - *(size_t*)(bs + off) = this->green_size; - off += sizeof(size_t); - - if (bs != NULL) - *(size_t*)(bs + off) = this->blue_size; - off += sizeof(size_t); - - if (bs != NULL) - *(size_t*)(bs + off) = this->ramps_size; - off += sizeof(size_t); - - if (bs != NULL) - *(enum libgamma_decision*)(bs + off) = this->supported; - off += sizeof(enum libgamma_decision); - - n = strlen(this->name) + 1; - if (bs != NULL) - memcpy(bs + off, this->name, n); - off += n; - - off += gamma_ramps_marshal(&(this->saved_ramps), bs ? bs + off : NULL, this->ramps_size); - - if (bs != NULL) - *(size_t*)(bs + off) = this->table_size; - off += sizeof(size_t); - - for (i = 0; i < this->table_size; i++) - { - off += filter_marshal(this->table_filters + i, bs ? bs + off : NULL, this->ramps_size); - off += gamma_ramps_marshal(this->table_sums + i, bs ? bs + off : NULL, this->ramps_size); - } - - return off; -} - - -/** - * Unmarshal an output - * - * @param this Output for the output - * @param buf Buffer with the marshalled output - * @return The number of unmarshalled bytes, 0 on error - */ -size_t output_unmarshal(struct output* restrict this, const void* restrict buf) -{ - size_t off = 0, i, n; - const char* bs = buf; - - this->crtc = NULL; - this->name = NULL; - - this->depth = *(const signed*)(bs + off); - off += sizeof(signed); - - this->red_size = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->green_size = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->blue_size = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->ramps_size = *(const size_t*)(bs + off); - off += sizeof(size_t); - - this->supported = *(const enum libgamma_decision*)(bs + off); - off += sizeof(enum libgamma_decision); - - n = strlen(bs + off) + 1; - this->name = memdup(bs + off, n); - if (this->name == NULL) - return 0; - - off += n = gamma_ramps_unmarshal(&(this->saved_ramps), bs, this->ramps_size); - COPY_RAMP_SIZES(&(this->saved_ramps.u8), this); - if (n == 0) - return 0; - - this->table_size = this->table_alloc = *(const size_t*)(bs + off); - off += sizeof(size_t); - if (this->table_size > 0) - { - this->table_filters = calloc(this->table_size, sizeof(*(this->table_filters))); - if (this->table_filters == NULL) - return 0; - this->table_sums = calloc(this->table_size, sizeof(*(this->table_sums))); - if (this->table_sums == NULL) - return 0; - } - - for (i = 0; i < this->table_size; i++) - { - off += n = filter_unmarshal(this->table_filters + i, bs + off, this->ramps_size); - if (n == 0) - return 0; - COPY_RAMP_SIZES(&(this->table_sums[i].u8), this); - off += n = gamma_ramps_unmarshal(this->table_sums + i, bs + off, this->ramps_size); - if (n == 0) - return 0; - } - - return off; -} - - -#if defined(__clang__) -# pragma GCC diagnostic pop -#endif - - - -/** - * Compare to outputs by the names of their respective CRTC:s - * - * @param a Return -1 if this one is lower - * @param b Return +1 if this one is higher - * @return See description of `a` and `b`, - * 0 if returned if they are the same - */ -int output_cmp_by_name(const void* restrict a, const void* restrict b) -{ - const char* an = ((const struct output*)a)->name; - const char* bn = ((const struct output*)b)->name; - return strcmp(an, bn); -} - - -/** - * Find an output by its name - * - * @param key The name of the output - * @param base The array of outputs - * @param n The number of elements in `base` - * @return Output find in `base`, `NULL` if not found - */ -struct output* output_find_by_name(const char* restrict key, struct output* restrict base, size_t n) -{ - struct output k; - -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wcast-qual" -#endif - k.name = (char*)key; -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#endif - - return bsearch(&k, base, n, sizeof(*base), output_cmp_by_name); -} - - -/** - * Remove a filter from an output - * - * @param out The output - * @param filter The filter - * @return The index of the filter, `out->table_size` if not found - */ -static ssize_t remove_filter(struct output* restrict out, struct filter* restrict filter) -{ - size_t i, n = out->table_size; - - for (i = 0; i < n; i++) - if (!strcmp(filter->class, out->table_filters[i].class)) - break; - - if (i == out->table_size) - { - fprintf(stderr, "%s: ignoring attempt to removing non-existing filter on CRTC %s: %s", - argv0, out->name, filter->class); - return (ssize_t)(out->table_size); - } - - filter_destroy(out->table_filters + i); - libgamma_gamma_ramps8_destroy(&(out->table_sums[i].u8)); - - n = n - i - 1; - memmove(out->table_filters + i, out->table_filters + i + 1, n * sizeof(*(out->table_filters))); - memmove(out->table_sums + i, out->table_sums + i + 1, n * sizeof(*(out->table_sums))); - out->table_size--; - - return (ssize_t)i; -} - - -/** - * Add a filter to an output - * - * @param out The output - * @param filter The filter - * @return The index given to the filter, -1 on error - */ -ssize_t add_filter(struct output* restrict out, struct filter* restrict filter) -{ - size_t i, n = out->table_size; - int r = -1; - - /* Remove? */ - if (filter->lifespan == LIFESPAN_REMOVE) - return remove_filter(out, filter); - - /* Update? */ - for (i = 0; i < n; i++) - if (!strcmp(filter->class, out->table_filters[i].class)) - break; - if (i != n) - { - filter_destroy(out->table_filters + i); - out->table_filters[i] = *filter; - return (ssize_t)i; - } - - /* Add! */ - for (i = 0; i < n; i++) - if (filter->priority > out->table_filters[i].priority) - break; - - if (n == out->table_alloc) - { - void* new; - - new = realloc(out->table_filters, (n + 10) * sizeof(*(out->table_filters))); - if (new == NULL) - return -1; - out->table_filters = new; - - new = realloc(out->table_sums, (n + 10) * sizeof(*(out->table_sums))); - if (new == NULL) - return -1; - out->table_sums = new; - - out->table_alloc += 10; - } - - memmove(out->table_filters + i + 1, out->table_filters + i, (n - i) * sizeof(*(out->table_filters))); - memmove(out->table_sums + i + 1, out->table_sums + i, (n - i) * sizeof(*(out->table_sums))); - out->table_size++; - - COPY_RAMP_SIZES(&(out->table_sums[i].u8), out); - switch (out->depth) - { - case 8: r = libgamma_gamma_ramps8_initialise(&(out->table_sums[i].u8)); break; - case 16: r = libgamma_gamma_ramps16_initialise(&(out->table_sums[i].u16)); break; - case 32: r = libgamma_gamma_ramps32_initialise(&(out->table_sums[i].u32)); break; - case 64: r = libgamma_gamma_ramps64_initialise(&(out->table_sums[i].u64)); break; - case -1: r = libgamma_gamma_rampsf_initialise(&(out->table_sums[i].f)); break; - case -2: r = libgamma_gamma_rampsd_initialise(&(out->table_sums[i].d)); break; - default: - abort(); - } - if (r < 0) - return -1; - - out->table_filters[i] = *filter; - - return (ssize_t)i; -} - - -/** - * Recalculate the resulting gamma and - * update push the new gamma ramps to the CRTC - * - * @param output The output - * @param first_updated The index of the first added or removed filter - * @return Zero on success, -1 on error - */ -int flush_filters(struct output* restrict output, size_t first_updated) -{ - union gamma_ramps plain; - union gamma_ramps* last; - size_t i; - int r = 0; - - if (first_updated == 0) - { - if (make_plain_ramps(&plain, output) < 0) - return -1; - last = &plain; - } - else - last = output->table_sums + (first_updated - 1); - - for (i = first_updated; i < output->table_size; i++) - { - apply(output->table_sums + i, output->table_filters[i].ramps, output->depth, last); - last = output->table_sums + i; - } - - switch (output->depth) - { - case 8: r = libgamma_crtc_set_gamma_ramps8(output->crtc, last->u8); break; - case 16: r = libgamma_crtc_set_gamma_ramps16(output->crtc, last->u16); break; - case 32: r = libgamma_crtc_set_gamma_ramps32(output->crtc, last->u32); break; - case 64: r = libgamma_crtc_set_gamma_ramps64(output->crtc, last->u64); break; - case -1: r = libgamma_crtc_set_gamma_rampsf(output->crtc, last->f); break; - case -2: r = libgamma_crtc_set_gamma_rampsd(output->crtc, last->d); break; - default: - abort(); - } - if (r) - libgamma_perror(argv0, r); /* Not fatal */ - - if (first_updated == 0) - libgamma_gamma_ramps8_destroy(&(plain.u8)); - - return 0; -} - - -/** - * Make identity mapping ramps - * - * @param ramps Output parameter for the ramps - * @param output The output for which the ramps shall be configured - * @return Zero on success, -1 on error - */ -int make_plain_ramps(union gamma_ramps* restrict ramps, struct output* restrict output) -{ - COPY_RAMP_SIZES(&(ramps->u8), output); - switch (output->depth) - { - case 8: - if (libgamma_gamma_ramps8_initialise(&(ramps->u8))) - return -1; - libclut_start_over(&(ramps->u8), UINT8_MAX, uint8_t, 1, 1, 1); - break; - case 16: - if (libgamma_gamma_ramps16_initialise(&(ramps->u16))) - return -1; - libclut_start_over(&(ramps->u16), UINT16_MAX, uint16_t, 1, 1, 1); - break; - case 32: - if (libgamma_gamma_ramps32_initialise(&(ramps->u32))) - return -1; - libclut_start_over(&(ramps->u32), UINT32_MAX, uint32_t, 1, 1, 1); - break; - case 64: - if (libgamma_gamma_ramps64_initialise(&(ramps->u64))) - return -1; - libclut_start_over(&(ramps->u64), UINT64_MAX, uint64_t, 1, 1, 1); - break; - case -1: - if (libgamma_gamma_rampsf_initialise(&(ramps->f))) - return -1; - libclut_start_over(&(ramps->f), 1.0f, float, 1, 1, 1); - break; - case -2: - if (libgamma_gamma_rampsd_initialise(&(ramps->d))) - return -1; - libclut_start_over(&(ramps->d), (double)1, double, 1, 1, 1); - break; - default: - abort(); - } - return 0; -} - diff --git a/src/output.h b/src/output.h deleted file mode 100644 index 08978b5..0000000 --- a/src/output.h +++ /dev/null @@ -1,230 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - -#include - -#include "ramps.h" -#include "filter.h" - - - -#ifndef GCC_ONLY -# if defined(__GNUC__) && !defined(__clang__) -# define GCC_ONLY(...) __VA_ARGS__ -# else -# define GCC_ONLY(...) /* nothing */ -# endif -#endif - - - -/** - * Copy the ramp sizes - * - * This macro supports both `struct output` - * and `struct gamma_ramps` - * - * @param dest The destination - * @param src The source - */ -#define COPY_RAMP_SIZES(dest, src) \ - ((dest)->red_size = (src)->red_size, \ - (dest)->green_size = (src)->green_size, \ - (dest)->blue_size = (src)->blue_size) - - - -/** - * Information about an output - */ -struct output -{ - /** - * -2: double - * -1: float - * 8: uint8_t - * 16: uint16_t - * 32: uint32_t - * 64: uint64_t - */ - signed depth; - - /** - * Whether gamma ramps are supported - */ - enum libgamma_decision supported; - - /** - * The number of stops in the red gamma ramp - */ - size_t red_size; - - /** - * The number of stops in the green gamma ramp - */ - size_t green_size; - - /** - * The number of stops in the blue gamma ramp - */ - size_t blue_size; - - /** - * `.red_size + .green_size + .blue_size` - * multiplied by the byte-size of each stop - */ - size_t ramps_size; - - /** - * The name of the output, will be its EDID - * if available, otherwise it will be the - * index of the partition, followed by a dot - * and the index of the CRTC within the - * partition, or if a name for the connector - * is available: the index of the partition - * followed by a dot and the name of the - * connector - */ - char* restrict name; - - /** - * The libgamma state for the output - */ - libgamma_crtc_state_t* restrict crtc; - - /** - * Saved gamma ramps - */ - union gamma_ramps saved_ramps; - - /** - * The table of all applied filters - */ - struct filter* restrict table_filters; - - /** - * `.table_sums[i]` is the resulting - * adjustment made when all filter - * from `.table_filters[0]` up to and - * including `.table_filters[i]` has - * been applied - */ - union gamma_ramps* restrict table_sums; - - /** - * The number of elements allocated - * for `.table_filters` and for `.table_sums` - */ - size_t table_alloc; - - /** - * The number of elements stored in - * `.table_filters` and in `.table_sums` - */ - size_t table_size; - -}; - - - -/** - * Free all resources allocated to an output. - * The allocation of `output` itself is not freed, - * nor is its the libgamma destroyed. - * - * @param this The output - */ -GCC_ONLY(__attribute__((nonnull))) -void output_destroy(struct output* restrict this); - -/** - * Marshal an output - * - * @param this The output - * @param buf Output buffer for the marshalled output, - * `NULL` just measure how large the buffers - * needs to be - * @return The number of marshalled byte - */ -GCC_ONLY(__attribute__((nonnull(1)))) -size_t output_marshal(const struct output* restrict this, void* restrict buf); - -/** - * Unmarshal an output - * - * @param this Output for the output - * @param buf Buffer with the marshalled output - * @return The number of unmarshalled bytes, 0 on error - */ -GCC_ONLY(__attribute__((nonnull))) -size_t output_unmarshal(struct output* restrict this, const void* restrict buf); - -/** - * Compare to outputs by the names of their respective CRTC:s - * - * @param a Return -1 if this one is lower - * @param b Return +1 if this one is higher - * @return See description of `a` and `b`, - * 0 if returned if they are the same - */ -GCC_ONLY(__attribute__((pure, nonnull))) -int output_cmp_by_name(const void* restrict a, const void* restrict b); - -/** - * Find an output by its name - * - * @param key The name of the output - * @param base The array of outputs - * @param n The number of elements in `base` - * @return Output find in `base`, `NULL` if not found - */ -GCC_ONLY(__attribute__((pure, nonnull))) -struct output* output_find_by_name(const char* restrict key, struct output* restrict base, size_t n); - -/** - * Add a filter to an output - * - * @param output The output - * @param filter The filter - * @return The index given to the filter, -1 on error - */ -GCC_ONLY(__attribute__((nonnull))) -ssize_t add_filter(struct output* restrict output, struct filter* restrict filter); - -/** - * Recalculate the resulting gamma and - * update push the new gamma ramps to the CRTC - * - * @param output The output - * @param first_updated The index of the first added or removed filter - * @return Zero on success, -1 on error - */ -GCC_ONLY(__attribute__((nonnull))) -int flush_filters(struct output* restrict output, size_t first_updated); - -/** - * Make identity mapping ramps - * - * @param ramps Output parameter for the ramps - * @param output The output for which the ramps shall be configured - * @return Zero on success, -1 on error - */ -GCC_ONLY(__attribute__((nonnull))) -int make_plain_ramps(union gamma_ramps* restrict ramps, struct output* restrict output); - diff --git a/src/ramps.c b/src/ramps.c deleted file mode 100644 index 2b855a4..0000000 --- a/src/ramps.c +++ /dev/null @@ -1,165 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include "ramps.h" - -#include - -#include -#include -#include - - - -/** - * The name of the process - */ -extern char* restrict argv0; - - - -/** - * Marshal a ramp trio - * - * @param this The ramps - * @param buf Output buffer for the marshalled ramps, - * `NULL` just measure how large the buffers - * needs to be - * @param ramps_size The byte-size of ramps - * @return The number of marshalled byte - */ -size_t gamma_ramps_marshal(const union gamma_ramps* restrict this, void* restrict buf, size_t ramps_size) -{ - if (buf != NULL) - memcpy(buf, this->u8.red, ramps_size); - return ramps_size; -} - - -/** - * Unmarshal a ramp trio - * - * @param this Output for the ramps, `.red_size`, `.green_size`, - * and `.blue_size` must already be set - * @param buf Buffer with the marshalled ramps - * @param ramps_size The byte-size of ramps - * @return The number of unmarshalled bytes, 0 on error - */ -size_t gamma_ramps_unmarshal(union gamma_ramps* restrict this, const void* restrict buf, size_t ramps_size) -{ - size_t depth = ramps_size / (this->u8.red_size + this->u8.green_size + this->u8.blue_size); - int r = 0; - switch (depth) - { - case 1: - r = libgamma_gamma_ramps8_initialise(&(this->u8)); - break; - case 2: - r = libgamma_gamma_ramps16_initialise(&(this->u16)); - break; - case 4: - r = libgamma_gamma_ramps32_initialise(&(this->u32)); - break; - case 8: - r = libgamma_gamma_ramps64_initialise(&(this->u64)); - break; - default: - if (depth == sizeof(float)) - r = libgamma_gamma_rampsf_initialise(&(this->f)); - else if (depth == sizeof(double)) - r = libgamma_gamma_rampsd_initialise(&(this->d)); - else - abort(); - break; - } - if (r) - { - libgamma_perror(argv0, r); - errno = 0; - return 0; - } - memcpy(this->u8.red, buf, ramps_size); - return ramps_size; -} - - -/** - * Apply a ramp-trio on top of another ramp-trio - * - * @param dest The output for the resulting ramp-trio, must be initialised - * @param application The red, green and blue ramps, as one single raw array, - * of the filter that should be applied - * @param depth -1: `float` stops - * -2: `double` stops - * Other: the number of bits of each (integral) stop - * @param base The CLUT on top of which the new filter should be applied, - * this can be the same pointer as `dest` - */ -void apply(union gamma_ramps* restrict dest, void* restrict application, - int depth, union gamma_ramps* restrict base) -{ - union gamma_ramps app; - size_t bytedepth; - size_t red_width, green_width, blue_width; - - if (depth == -1) - bytedepth = sizeof(float); - else if (depth == -2) - bytedepth = sizeof(double); - else - bytedepth = (size_t)depth / 8; - - red_width = (app.u8.red_size = base->u8.red_size) * bytedepth; - green_width = (app.u8.green_size = base->u8.green_size) * bytedepth; - blue_width = (app.u8.blue_size = base->u8.blue_size) * bytedepth; - - app.u8.red = application; - app.u8.green = app.u8.red + red_width; - app.u8.blue = app.u8.green + green_width; - - if (dest != base) - { - memcpy(dest->u8.red, base->u8.red, red_width); - memcpy(dest->u8.green, base->u8.green, green_width); - memcpy(dest->u8.blue, base->u8.blue, blue_width); - } - - switch (depth) - { - case 8: - libclut_apply(&(dest->u8), UINT8_MAX, uint8_t, &(app.u8), UINT8_MAX, uint8_t, 1, 1, 1); - break; - case 16: - libclut_apply(&(dest->u16), UINT16_MAX, uint16_t, &(app.u16), UINT16_MAX, uint16_t, 1, 1, 1); - break; - case 32: - libclut_apply(&(dest->u32), UINT32_MAX, uint32_t, &(app.u32), UINT32_MAX, uint32_t, 1, 1, 1); - break; - case 64: - libclut_apply(&(dest->u64), UINT64_MAX, uint64_t, &(app.u64), UINT64_MAX, uint64_t, 1, 1, 1); - break; - case -1: - libclut_apply(&(dest->f), 1.0f, float, &(app.d), 1.0f, float, 1, 1, 1); - break; - case -2: - libclut_apply(&(dest->d), (double)1, double, &(app.f), (double)1, double, 1, 1, 1); - break; - default: - abort(); - } -} - diff --git a/src/ramps.h b/src/ramps.h deleted file mode 100644 index 0ba8579..0000000 --- a/src/ramps.h +++ /dev/null @@ -1,111 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -#ifndef GCC_ONLY -# if defined(__GNUC__) && !defined(__clang__) -# define GCC_ONLY(...) __VA_ARGS__ -# else -# define GCC_ONLY(...) /* nothing */ -# endif -#endif - - - -/** - * Gamma ramps union for all - * lbigamma gamma ramps types - */ -union gamma_ramps -{ - /** - * Ramps with 8-bit value - */ - libgamma_gamma_ramps8_t u8; - - /** - * Ramps with 16-bit value - */ - libgamma_gamma_ramps16_t u16; - - /** - * Ramps with 32-bit value - */ - libgamma_gamma_ramps32_t u32; - - /** - * Ramps with 64-bit value - */ - libgamma_gamma_ramps64_t u64; - - /** - * Ramps with `float` value - */ - libgamma_gamma_rampsf_t f; - - /** - * Ramps with `double` value - */ - libgamma_gamma_rampsd_t d; - -}; - - - -/** - * Marshal a ramp trio - * - * @param this The ramps - * @param buf Output buffer for the marshalled ramps, - * `NULL` just measure how large the buffers - * needs to be - * @param ramps_size The byte-size of ramps - * @return The number of marshalled byte - */ -GCC_ONLY(__attribute__((nonnull(1)))) -size_t gamma_ramps_marshal(const union gamma_ramps* restrict this, void* restrict buf, size_t ramps_size); - -/** - * Unmarshal a ramp trio - * - * @param this Output for the ramps - * @param buf Buffer with the marshalled ramps - * @param ramps_size The byte-size of ramps - * @return The number of unmarshalled bytes, 0 on error - */ -GCC_ONLY(__attribute__((nonnull))) -size_t gamma_ramps_unmarshal(union gamma_ramps* restrict this, const void* restrict buf, size_t ramps_size); - -/** - * Apply a ramp-trio on top of another ramp-trio - * - * @param dest The output for the resulting ramp-trio, must be initialised - * @param application The red, green and blue ramps, as one single raw array, - * of the filter that should be applied - * @param depth -1: `float` stops - * -2: `double` stops - * Other: the number of bits of each (integral) stop - * @param base The CLUT on top of which the new filter should be applied, - * this can be the same pointer as `dest` - */ -GCC_ONLY(__attribute__((nonnull))) -void apply(union gamma_ramps* restrict dest, void* restrict application, - int depth, union gamma_ramps* restrict base); - diff --git a/src/ring.c b/src/ring.c deleted file mode 100644 index 13cf8c9..0000000 --- a/src/ring.c +++ /dev/null @@ -1,224 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include "ring.h" - -#include -#include - - - -/** - * Initialise a ring buffer - * - * @param this The ring buffer - */ -void ring_initialise(struct ring* restrict this) -{ - this->start = 0; - this->end = 0; - this->size = 0; - this->buffer = NULL; -} - - -/** - * Release resource allocated to a ring buffer - * - * @param this The ring buffer - */ -void ring_destroy(struct ring* restrict this) -{ - free(this->buffer); -} - - - -#if defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wcast-align" -#endif - - -/** - * Marshal a ring buffer - * - * @param this The ring buffer - * @param buf Output buffer for the marshalled data, - * `NULL` to only measure how large buffer - * is needed - * @return The number of marshalled bytes - */ -size_t ring_marshal(const struct ring* restrict this, void* restrict buf) -{ - size_t off = 0, n = this->end - this->start; - char* bs = buf; - - if (bs != NULL) - *(size_t*)(bs + off) = n; - off += sizeof(size_t); - - if (bs != NULL) - memcpy(bs + off, this->buffer + this->start, n); - off += n; - - return off; -} - - -/** - * Unmarshal a ring buffer - * - * @param this Output parameter for the ring buffer - * @param buf Buffer with the marshalled data - * @return The number of unmarshalled bytes, 0 on error - */ -size_t ring_unmarshal(struct ring* restrict this, const void* restrict buf) -{ - size_t off = 0; - const char* bs = buf; - - ring_initialise(this); - - this->size = this->end = *(const size_t*)(bs + off); - off += sizeof(size_t); - - if (this->end > 0) - { - if (!(this->buffer = malloc(this->end))) - return 0; - - memcpy(this->buffer, bs + off, this->end); - off += this->end; - } - - return off; -} - - -#if defined(__clang__) -# pragma GCC diagnostic pop -#endif - - - -/** - * Append data to a ring buffer - * - * @param this The ring buffer - * @param data The new data - * @param n The number of bytes in `data` - * @return Zero on success, -1 on error - */ -int ring_push(struct ring* restrict this, void* restrict data, size_t n) -{ - size_t used = 0; - - if (this->start == this->end) - { - if (this->buffer != NULL) - used = this->size; - } - else if (this->start > this->end) - used = this->size - this->start + this->end; - else - used = this->start - this->end; - - if (used + n > this->size) - { - char* restrict new = malloc(used + n); - if (new == NULL) - return -1; - if (this->buffer) - { - if (this->start < this->end) - memcpy(new, this->buffer + this->start, this->end - this->start); - else - { - memcpy(new, this->buffer + this->start, this->size - this->start); - memcpy(new + this->size - this->start, this->buffer, this->end); - } - } - memcpy(new + used, data, n); - this->buffer = new; - this->start = 0; - this->end = used + n; - this->size = used + n; - } - else if ((this->start >= this->end) || (this->end + n <= this->size)) - { - memcpy(this->buffer + this->end, data, n); - this->end += n; - } - else - { - memcpy(this->buffer + this->end, data, this->size - this->end); - data = (char*)data + (this->size - this->end); - n -= this->size - this->end; - memcpy(this->buffer, data, n); - this->end = n; - } - - return 0; -} - - -/** - * Get queued data from a ring buffer - * - * It can take up to two calls (with `ring_pop` between) - * to get all queued data - * - * @param this The ring buffer - * @param n Output parameter for the length - * of the returned segment - * @return The beginning of the queued data, - * `NULL` if there is nothing more - */ -void* ring_peek(struct ring* restrict this, size_t* restrict n) -{ - if (this->buffer == NULL) - return *n = 0, NULL; - - if (this->start < this->end) - *n = this->end - this->start; - else - *n = this->size - this->start; - return this->buffer + this->start; -} - - -/** - * Dequeue data from a ring bubber - * - * @param this The ring buffer - * @param n The number of bytes to dequeue - */ -void ring_pop(struct ring* restrict this, size_t n) -{ - this->start += n; - this->start %= this->size; - if (this->start == this->end) - { - free(this->buffer); - this->start = 0; - this->end = 0; - this->size = 0; - this->buffer = NULL; - } -} - diff --git a/src/ring.h b/src/ring.h deleted file mode 100644 index 5825810..0000000 --- a/src/ring.h +++ /dev/null @@ -1,145 +0,0 @@ -/** - * coopgammad -- Cooperative gamma server - * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -#ifndef GCC_ONLY -# if defined(__GNUC__) && !defined(__clang__) -# define GCC_ONLY(...) __VA_ARGS__ -# else -# define GCC_ONLY(...) /* nothing */ -# endif -#endif - - - -/** - * Ring buffer - */ -struct ring -{ - /** - * Buffer for the data - */ - char* restrict buffer; - - /** - * The first set byte in `.buffer` - */ - size_t start; - - /** - * The last set byte in `.buffer`, plus 1 - */ - size_t end; - - /** - * The size of `.buffer` - */ - size_t size; -}; - - - -/** - * Initialise a ring buffer - * - * @param this The ring buffer - */ -GCC_ONLY(__attribute__((nonnull))) -void ring_initialise(struct ring* restrict this); - -/** - * Release resource allocated to a ring buffer - * - * @param this The ring buffer - */ -GCC_ONLY(__attribute__((nonnull))) -void ring_destroy(struct ring* restrict this); - -/** - * Marshal a ring buffer - * - * @param this The ring buffer - * @param buf Output buffer for the marshalled data, - * `NULL` to only measure how large buffer - * is needed - * @return The number of marshalled bytes - */ -GCC_ONLY(__attribute__((nonnull(1)))) -size_t ring_marshal(const struct ring* restrict this, void* restrict buf); - -/** - * Unmarshal a ring buffer - * - * @param this Output parameter for the ring buffer - * @param buf Buffer with the marshalled data - * @return The number of unmarshalled bytes, 0 on error - */ -GCC_ONLY(__attribute__((nonnull))) -size_t ring_unmarshal(struct ring* restrict this, const void* restrict buf); - -/** - * Append data to a ring buffer - * - * @param this The ring buffer - * @param data The new data - * @param n The number of bytes in `data` - * @return Zero on success, -1 on error - */ -GCC_ONLY(__attribute__((nonnull(1)))) -int ring_push(struct ring* restrict this, void* restrict data, size_t n); - -/** - * Get queued data from a ring buffer - * - * It can take up to two calls (with `ring_pop` between) - * to get all queued data - * - * @param this The ring buffer - * @param n Output parameter for the length - * of the returned segment - * @return The beginning of the queued data, - * `NULL` if there is nothing more - */ -GCC_ONLY(__attribute__((nonnull))) -void* ring_peek(struct ring* restrict this, size_t* restrict n); - -/** - * Dequeue data from a ring bubber - * - * @param this The ring buffer - * @param n The number of bytes to dequeue - */ -GCC_ONLY(__attribute__((nonnull))) -void ring_pop(struct ring* restrict this, size_t n); - -/** - * Check whether there is more data waiting - * in a ring buffer - * - * @param this The ring buffer - * @return 1 if there is more data, 0 otherwise - */ -GCC_ONLY(__attribute__((nonnull))) -static inline int ring_have_more(struct ring* restrict this) -{ - return this->buffer != NULL; -} - diff --git a/src/server.c b/src/server.c index 2705f4e..4adcc5e 100644 --- a/src/server.c +++ b/src/server.c @@ -16,14 +16,17 @@ * along with this program. If not, see . */ #include "server.h" -#include "output.h" #include "util.h" +#include "communication.h" +#include "state.h" +#include "crtc-server/server.h" +#include "gamma-server/server.h" +#include "coopgamma-server/server.h" #include #include #include #include -#include #include #include #include @@ -31,233 +34,6 @@ -#if defined(__clang__) -# pragma GCC diagnostic ignored "-Wswitch-enum" -#endif - - - -/** - * List of all client's file descriptors - * - * Unused slots, with index less than `connections_used`, - * should have the value -1 (negative) - */ -int* restrict connections = NULL; - -/** - * The number of elements allocated for `connections` - */ -size_t connections_alloc = 0; - -/** - * The index of the first unused slot in `connections` - */ -size_t connections_ptr = 0; - -/** - * The index of the last used slot in `connections`, plus 1 - */ -size_t connections_used = 0; - -/** - * The clients' connections' inbound-message buffers - */ -struct message* restrict inbound = NULL; - -/** - * The clients' connections' outbound-message buffers - */ -struct ring* restrict outbound = NULL; - - -/** - * The name of the process - */ -extern char* restrict argv0; - -/** - * The server socket's file descriptor - */ -extern int socketfd; - -/** - * Has the process receive a signal - * telling it to re-execute? - */ -extern volatile sig_atomic_t reexec; - -/** - * Has the process receive a signal - * telling it to terminate? - */ -extern volatile sig_atomic_t terminate; - -/** - * Has the process receive a to - * disconnect from or reconnect to - * the site? 1 if disconnct, 2 if - * reconnect, 0 otherwise. - */ -extern volatile sig_atomic_t connection; - -/** - * Array of all outputs - */ -extern struct output* restrict outputs; - -/** - * The nubmer of elements in `outputs` - */ -extern size_t outputs_n; - - - -/** - * Construct a message - * - * @param bufp:char** Output parameter for the buffer, must not have side-effects - * @param np:size_t* Output parameter for the size of the buffer sans `extra` - * @param extra:size_t The extra number for bytes to allocate to the buffer - * @param format:string-literal Message format string - * @param ... Message formatting arguments - */ -#define MAKE_MESSAGE(bufp, np, extra, format, ...) \ - do \ - { \ - ssize_t m__; \ - snprintf(NULL, 0, format "%zn", __VA_ARGS__, &m__); \ - *(bufp) = malloc((size_t)(extra) + (size_t)m__); \ - if (*(bufp) == NULL) \ - return -1; \ - sprintf(*(bufp), format, __VA_ARGS__); \ - *(np) = (size_t)m__; \ - } \ - while (0) - - - -/** - * Destroy the state of the connections - * - * @param disconnect Disconnect all connections? - */ -void server_destroy(int disconnect) -{ - size_t i; - for (i = 0; i < connections_used; i++) - if (connections[i] >= 0) - { - if (disconnect) - { - shutdown(connections[i], SHUT_RDWR); - close(connections[i]); - } - message_destroy(inbound + i); - ring_destroy(outbound + i); - } - free(inbound); - free(outbound); - free(connections); -} - - - -#if defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wcast-align" -#endif - - -/** - * Marshal the state of the connections - * - * @param buf Output buffer for the marshalled data, - * `NULL` to only measure how many bytes - * this buffer needs - * @return The number of marshalled bytes - */ -size_t server_marshal(void* restrict buf) -{ - size_t i, off = 0; - char* restrict bs = buf; - - if (bs != NULL) - *(size_t*)(bs + off) = connections_ptr; - off += sizeof(size_t); - - if (bs != NULL) - *(size_t*)(bs + off) = connections_used; - off += sizeof(size_t); - - if (bs != NULL) - memcpy(bs + off, connections, connections_used * sizeof(*connections)); - off += connections_used * sizeof(*connections); - - for (i = 0; i < connections_used; i++) - if (connections[i] >= 0) - { - off += message_marshal(inbound + i, bs ? bs + off : NULL); - off += ring_marshal(outbound + i, bs ? bs + off : NULL); - } - - return off; -} - - -/** - * Unmarshal the state of the connections - * - * @param buf Buffer for the marshalled data - * @return The number of unmarshalled bytes, 0 on error - */ -size_t server_unmarshal(const void* restrict buf) -{ - size_t off = 0, i, n; - const char* restrict bs = buf; - - connections = NULL; - inbound = NULL; - - connections_ptr = *(const size_t*)(bs + off); - off += sizeof(size_t); - - connections_alloc = connections_used = *(const size_t*)(bs + off); - off += sizeof(size_t); - - if (connections_alloc > 0) - { - connections = memdup(bs + off, connections_alloc * sizeof(*connections)); - if (connections == NULL) - return 0; - off += connections_used * sizeof(*connections); - - inbound = malloc(connections_alloc * sizeof(*inbound)); - if (inbound == NULL) - return 0; - } - - for (i = 0; i < connections_used; i++) - if (connections[i] >= 0) - { - off += n = message_unmarshal(inbound + i, bs + off); - if (n == 0) - return 0; - off += n = ring_unmarshal(outbound + i, bs + off); - if (n == 0) - return 0; - } - - return off; -} - - -#if defined(__clang__) -# pragma GCC diagnostic pop -#endif - - - /** * Sets the file descriptor set that includes * the server socket and all connections @@ -354,548 +130,6 @@ static int handle_server(void) } -/** - * Handle a closed connection - * - * @param client The file descriptor for the client - * @return Zero on success, -1 on error - */ -static int connection_closed(int client) -{ - size_t i, j, k; - int remove; - - for (i = 0; i < outputs_n; i++) - { - struct output* output = outputs + i; - ssize_t updated = -1; - for (j = k = 0; j < output->table_size; j += !remove, k++) - { - remove = output->table_filters[j].client == client; - remove = remove && (output->table_filters[j].lifespan == LIFESPAN_UNTIL_DEATH); - if (remove) - { - filter_destroy(output->table_filters + j); - libgamma_gamma_ramps8_destroy(&(output->table_sums[j].u8)); - output->table_size -= 1; - if (updated == -1) - updated = (ssize_t)j; - } - output->table_filters[j] = output->table_filters[k]; - output->table_sums[j] = output->table_sums[k]; - } - if (updated >= 0) - if (flush_filters(output, (size_t)updated) < 0) - return -1; - } - - return 0; -} - - -/** - * Send a message - * - * @param conn The index of the connection - * @param buf The data to send - * @param n The size of `buf` - * @return Zero on success, -1 on error, 1 if disconncted - * EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count - * as success (ECONNRESET cause 1 to be returned), - * and are handled appropriately. - */ -static int send_message(size_t conn, char* restrict buf, size_t n) -{ - struct ring* restrict ring = outbound + conn; - int fd = connections[conn]; - int saved_errno; - size_t ptr = 0; - ssize_t sent; - size_t chunksize = n; - size_t sendsize; - size_t old_n; - char* old_buf; - - while ((old_buf = ring_peek(ring, &old_n))) - { - size_t old_ptr = 0; - while (old_ptr < n) - { - sendsize = old_n - old_ptr < chunksize ? old_n - old_ptr : chunksize; - sent = send(fd, old_buf + old_ptr, sendsize, 0); - if (sent < 0) - { - if (errno != EMSGSIZE) - goto fail; - chunksize >>= 1; - if (chunksize == 0) - goto fail; - continue; - } - old_ptr += (size_t)sent; - ring_pop(ring, (size_t)sent); - } - } - - while (ptr < n) - { - sendsize = n - ptr < chunksize ? n - ptr : chunksize; - sent = send(fd, buf + ptr, sendsize, 0); - if (sent < 0) - { - if (errno != EMSGSIZE) - goto fail; - chunksize >>= 1; - if (chunksize == 0) - goto fail; - continue; - } - ptr += (size_t)sent; - } - - free(buf); - return 0; - - fail: - switch (errno) - { - case EINTR: - case EAGAIN: -#if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: -#endif - if (ring_push(ring, buf + ptr, n - ptr) < 0) - goto proper_fail; - free(buf); - return 0; - case ECONNRESET: - free(buf); - if (connection_closed(fd) < 0) - return -1; - return 1; - default: - break; - } - proper_fail: - saved_errno = errno; - free(buf); - errno = saved_errno; - return -1; -} - - -/** - * Continue sending the queued messages - * - * @param conn The index of the connection - * @return Zero on success, -1 on error, 1 if disconncted - * EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count - * as success (ECONNRESET cause 1 to be returned), - * and are handled appropriately. - */ -static inline int continue_send(size_t conn) -{ - return send_message(conn, NULL, 0); -} - - -/** - * Send a custom error without an error number - * - * @param ... The error description to send - * @return 1: Client disconnected - * 0: Success (possibily delayed) - * -1: An error occurred - */ -#define send_error(...) ((send_error)(conn, message_id, __VA_ARGS__)) - - -/** - * Send a custom error without an error number - * - * @param conn The index of the connection - * @param message_id The ID of the message to which this message is a response - * @param desc The error description to send - * @return 1: Client disconnected - * 0: Success (possibily delayed) - * -1: An error occurred - */ -GCC_ONLY(__attribute__((nonnull))) -static int (send_error)(size_t conn, const char* restrict message_id, const char* restrict desc) -{ - char* restrict buf; - size_t n; - - MAKE_MESSAGE(&buf, &n, 0, - "Command: error\n" - "In response to: %s\n" - "Error: custom\n" - "Length: %zu\n" - "\n" - "%s\n", - message_id, strlen(desc) + 1, desc); - - return send_message(conn, buf, n); -} - - -/** - * Send a standard error - * - * @param ... The value of `errno`, 0 to indicate success - * @return 1: Client disconnected - * 0: Success (possibily delayed) - * -1: An error occurred - */ -#define send_errno(...) ((send_errno)(conn, message_id, __VA_ARGS__)) - - -/** - * Send a standard error - * - * @param conn The index of the connection - * @param message_id The ID of the message to which this message is a response - * @param number The value of `errno`, 0 to indicate success - * @return 1: Client disconnected - * 0: Success (possibily delayed) - * -1: An error occurred - */ -GCC_ONLY(__attribute__((nonnull))) -static int (send_errno)(size_t conn, const char* restrict message_id, int number) -{ - char* restrict buf; - size_t n; - - MAKE_MESSAGE(&buf, &n, 0, - "Command: error\n" - "In response to: %s\n" - "Error: %i\n" - "\n", - message_id, number); - - return send_message(conn, buf, n); -} - - -/** - * Handle a ‘Command: enumerate-crtcs’ message - * - * @param conn The index of the connection - * @param message_id The value of the ‘Message ID’ header - * @return Zero on success (even if ignored), -1 on error, - * 1 if connection closed - */ -GCC_ONLY(__attribute__((nonnull))) -static int enumerate_crtcs(size_t conn, const char* restrict message_id) -{ - size_t i, n = 0, len; - char* restrict buf; - - for (i = 0; i < outputs_n; i++) - n += strlen(outputs[i].name) + 1; - - MAKE_MESSAGE(&buf, &n, 0, - "Command: crtc-enumeration\n" - "In response to: %s\n" - "Length: %zu\n" - "\n", - message_id, n); - - for (i = 0; i < outputs_n; i++) - { - len = strlen(outputs[i].name); - memcpy(buf + n, outputs[i].name, len); - buf[n + len] = '\n'; - n += len + 1; - } - - return send_message(conn, buf, n); -} - - -/** - * Handle a ‘Command: set-gamma’ message - * - * @param conn The index of the connection - * @param message_id The value of the ‘Message ID’ header - * @param crtc The value of the ‘CRTC’ header - * @return Zero on success (even if ignored), -1 on error, - * 1 if connection closed - */ -GCC_ONLY(__attribute__((nonnull(2)))) -static int get_gamma_info(size_t conn, const char* restrict message_id, const char* restrict crtc) -{ - struct output* restrict output; - char* restrict buf; - char depth[3]; - const char* supported; - size_t n; - - if (crtc == NULL) return send_error("protocol error: 'CRTC' header omitted"); - - output = output_find_by_name(crtc, outputs, outputs_n); - if (output == NULL) - return send_error("selected CRTC does not exist"); - - switch (output->depth) - { - case -2: strcpy(depth, "d"); break; - case -1: strcpy(depth, "f"); break; - default: - sprintf(depth, "%i", output->depth); - break; - } - - switch (output->supported) - { - case LIBGAMMA_YES: supported = "yes"; break; - case LIBGAMMA_NO: supported = "no"; break; - default: supported = "maybe"; break; - } - - MAKE_MESSAGE(&buf, &n, 0, - "In response to: %s\n" - "Cooperative: yes\n" - "Depth: %s\n" - "Red size: %zu\n" - "Green size: %zu\n" - "Blue size: %zu\n" - "Gamma support: %s\n" - "\n", - message_id, depth, output->red_size, output->green_size, - output->blue_size, supported); - - return send_message(conn, buf, n); -} - - -/** - * Handle a ‘Command: get-gamma’ message - * - * @param conn The index of the connection - * @param message_id The value of the ‘Message ID’ header - * @param crtc The value of the ‘CRTC’ header - * @param coalesce The value of the ‘Coalesce’ header - * @param high_priority The value of the ‘High priority’ header - * @param low_priority The value of the ‘Low priority’ header - * @return Zero on success (even if ignored), -1 on error, - * 1 if connection closed - */ -GCC_ONLY(__attribute__((nonnull(2)))) -static int get_gamma(size_t conn, const char* restrict message_id, const char* restrict crtc, - const char* restrict coalesce, const char* restrict high_priority, - const char* restrict low_priority) -{ - struct output* restrict output; - int64_t high, low; - int coal; - char* restrict buf; - size_t start, end, len, n, i; - char depth[3]; - char tables[sizeof("Tables: \n") + 3 * sizeof(size_t)]; - - if (crtc == NULL) return send_error("protocol error: 'CRTC' header omitted"); - if (coalesce == NULL) return send_error("protocol error: 'Coalesce' header omitted"); - if (high_priority == NULL) return send_error("protocol error: 'High priority' header omitted"); - if (low_priority == NULL) return send_error("protocol error: 'Low priority' header omitted"); - - high = (int64_t)atoll(high_priority); - low = (int64_t)atoll(low_priority); - - if (!strcmp(coalesce, "yes")) - coal = 1; - else if (!strcmp(coalesce, "no")) - coal = 0; - else - return send_error("protocol error: recognised value for 'Coalesce' header"); - - output = output_find_by_name(crtc, outputs, outputs_n); - if (output == NULL) - return send_error("selected CRTC does not exist"); - else if (output->supported == LIBGAMMA_NO) - return send_error("selected CRTC does not support gamma adjustments"); - - for (start = 0; start < output->table_size; start++) - if (output->table_filters[start].priority <= high) - break; - - for (end = output->table_size; end > 0; end--) - if (output->table_filters[end - 1].priority >= low) - break; - - switch (output->depth) - { - case -2: strcpy(depth, "d"); break; - case -1: strcpy(depth, "f"); break; - default: - sprintf(depth, "%i", output->depth); - break; - } - - if (coal) - { - *tables = '\0'; - n = output->ramps_size; - } - else - { - sprintf(tables, "Tables: %zu\n", end - start); - n = (sizeof(int64_t) + output->ramps_size) * (end - start); - for (i = start; i < end; i++) - n += strlen(output->table_filters[i].class) + 1; - } - - MAKE_MESSAGE(&buf, &n, 0, - "In response to: %s\n" - "Depth: %s\n" - "Red size: %zu\n" - "Green size: %zu\n" - "Blue size: %zu\n" - "%s" - "Length: %zu\n" - "\n", - message_id, depth, output->red_size, output->green_size, - output->blue_size, tables, n); - - if (coal) - { - if (start == 0) - memcpy(buf + n, output->table_sums[end].u8.red, output->ramps_size); - else - { - union gamma_ramps ramps; - if (make_plain_ramps(&ramps, output)) - { - int saved_errno = errno; - free(buf); - errno = saved_errno; - return -1; - } - for (i = start; i < end; i++) - apply(&ramps, output->table_filters[i].ramps, output->depth, &ramps); - memcpy(buf + n, ramps.u8.red, output->ramps_size); - libgamma_gamma_ramps8_destroy(&(ramps.u8)); - } - n += output->ramps_size; - } - else - for (i = start; i < end; i++) - { -#if defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wcast-align" -#endif - *(int64_t*)(buf + n) = output->table_filters[i].priority; -#if defined(__clang__) -# pragma GCC diagnostic pop -#endif - n += sizeof(int64_t); - len = strlen(output->table_filters[i].class) + 1; - memcpy(buf + n, output->table_filters[i].class, len); - n += len; - memcpy(buf + n, output->table_filters[i].ramps, output->ramps_size); - n += output->ramps_size; - } - - return send_message(conn, buf, n); -} - - -/** - * Handle a ‘Command: set-gamma’ message - * - * @param conn The index of the connection - * @param message_id The value of the ‘Message ID’ header - * @param crtc The value of the ‘CRTC’ header - * @param priority The value of the ‘Priority’ header - * @param class The value of the ‘Class’ header - * @param lifespan The value of the ‘Lifespan’ header - * @return Zero on success (even if ignored), -1 on error, - * 1 if connection closed - */ -GCC_ONLY(__attribute__((nonnull(2)))) -static int set_gamma(size_t conn, const char* restrict message_id, const char* restrict crtc, - const char* restrict priority, const char* restrict class, const char* restrict lifespan) -{ - struct message* restrict msg = inbound + conn; - struct output* restrict output = NULL; - struct filter filter; - char* restrict p; - char* restrict q; - int saved_errno; - ssize_t r; - - if (crtc == NULL) return send_error("protocol error: 'CRTC' header omitted"); - if (class == NULL) return send_error("protocol error: 'Class' header omitted"); - if (lifespan == NULL) return send_error("protocol error: 'Lifespan' header omitted"); - - filter.client = connections[conn]; - filter.priority = priority == NULL ? 0 : (int64_t)atoll(priority); - filter.ramps = NULL; - - output = output_find_by_name(crtc, outputs, outputs_n); - if (output == NULL) - return send_error("CRTC does not exists"); - - p = strstr(class, "::"); - if ((p == NULL) || (p == class)) - return send_error("protocol error: malformatted value for 'Class' header"); - q = strstr(p + 2, "::"); - if ((q == NULL) || (q == p)) - return send_error("protocol error: malformatted value for 'Class' header"); - - if (!strcmp(lifespan, "until-removal")) - filter.lifespan = LIFESPAN_UNTIL_REMOVAL; - else if (!strcmp(lifespan, "until-death")) - filter.lifespan = LIFESPAN_UNTIL_DEATH; - else if (!strcmp(lifespan, "remove")) - filter.lifespan = LIFESPAN_REMOVE; - else - return send_error("protocol error: recognised value for 'Lifespan' header"); - - if (filter.lifespan == LIFESPAN_REMOVE) - { - if (msg->payload_size) - fprintf(stderr, "%s: ignoring superfluous payload on Command: set-gamma message with " - "Lifespan: remove\n", argv0); - if (priority != NULL) - fprintf(stderr, "%s: ignoring superfluous Priority header on Command: set-gamma message with " - "Lifespan: remove\n", argv0); - } - else if (msg->payload_size != output->ramps_size) - return send_error("invalid payload: size of message payload does matched the expectancy"); - else if (priority == NULL) - return send_error("protocol error: 'Priority' header omitted"); - - filter.class = memdup(class, strlen(class) + 1); - if (filter.class == NULL) - goto fail; - - if (filter.lifespan != LIFESPAN_REMOVE) - { - filter.ramps = memdup(msg->payload, msg->payload_size); - if (filter.ramps == NULL) - goto fail; - } - - if ((r = add_filter(output, &filter)) < 0) - goto fail; - filter.class = NULL; - filter.ramps = NULL; - if (flush_filters(output, (size_t)r)) - goto fail; - - return send_errno(0); - - fail: - saved_errno = errno; - send_errno(saved_errno); - free(filter.class); - free(filter.ramps); - errno = saved_errno; - return -1; -} - /** * Handle event on a connection to a client @@ -978,25 +212,25 @@ static int handle_connection(size_t conn) { if (crtc || coalesce || high_priority || low_priority || priority || class || lifespan) fprintf(stderr, "%s: ignoring superfluous headers in Command: enumerate-crtcs message\n", argv0); - r = enumerate_crtcs(conn, message_id); + r = handle_enumerate_crtcs(conn, message_id); } else if (!strcmp(command, "get-gamma-info")) { if (coalesce || high_priority || low_priority || priority || class || lifespan) fprintf(stderr, "%s: ignoring superfluous headers in Command: get-gamma-info message\n", argv0); - r = get_gamma_info(conn, message_id, crtc); + r = handle_get_gamma_info(conn, message_id, crtc); } else if (!strcmp(command, "get-gamma")) { if (priority || class || lifespan) fprintf(stderr, "%s: ignoring superfluous headers in Command: get-gamma message\n", argv0); - r = get_gamma(conn, message_id, crtc, coalesce, high_priority, low_priority); + r = handle_get_gamma(conn, message_id, crtc, coalesce, high_priority, low_priority); } else if (!strcmp(command, "set-gamma")) { if (coalesce || high_priority || low_priority) fprintf(stderr, "%s: ignoring superfluous headers in Command: set-gamma message\n", argv0); - r = set_gamma(conn, message_id, crtc, priority, class, lifespan); + r = handle_set_gamma(conn, message_id, crtc, priority, class, lifespan); } else fprintf(stderr, "%s: ignoring unrecognised command: Command: %s\n", argv0, command); @@ -1007,6 +241,22 @@ static int handle_connection(size_t conn) } + +/** + * Disconnect all clients + */ +void disconnect_all(void) +{ + size_t i; + for (i = 0; i < connections_used; i++) + if (connections[i] >= 0) + { + shutdown(connections[i], SHUT_RDWR); + close(connections[i]); + } +} + + /** * The program's main loop * @@ -1020,15 +270,11 @@ int main_loop(void) while (!reexec && !terminate) { - if (connection == 1) /* disconnect */ - { - connection = 0; - /* TODO */ - } - else if (connection >= 2) /* reconnect */ + if (connection) { connection = 0; - /* TODO */ + if ((connection == 1 ? disconnect() : reconnect()) < 0) + return -1; } memcpy(&fds_rd, &fds_orig, sizeof(fd_set)); diff --git a/src/server.h b/src/server.h index adf4c36..d3cf88c 100644 --- a/src/server.h +++ b/src/server.h @@ -15,82 +15,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "message.h" -#include "ring.h" +#ifndef SERVER_H +#define SERVER_H -#include - - - -#ifndef GCC_ONLY -# if defined(__GNUC__) && !defined(__clang__) -# define GCC_ONLY(...) __VA_ARGS__ -# else -# define GCC_ONLY(...) /* nothing */ -# endif -#endif - - - -/** - * List of all client's file descriptors - * - * Unused slots, with index less than `connections_used`, - * should have the value -1 (negative) - */ -extern int* restrict connections; /** - * The number of elements allocated for `connections` + * Disconnect all clients */ -extern size_t connections_alloc; - -/** - * The index of the first unused slot in `connections` - */ -extern size_t connections_ptr; - -/** - * The index of the last used slot in `connections`, plus 1 - */ -extern size_t connections_used; - -/** - * The clients' connections' inbound-message buffers - */ -extern struct message* restrict inbound; - -/** - * The clients' connections' outbound-message buffers - */ -extern struct ring* restrict outbound; - - - -/** - * Destroy the state of the connections - * - * @param disconnect Disconnect all connections? - */ -void server_destroy(int disconnect); - -/** - * Marshal the state of the connections - * - * @param buf Output buffer for the marshalled data, - * `NULL` to only measure how many bytes - * this buffer needs - * @return The number of marshalled bytes - */ -size_t server_marshal(void* restrict buf); - -/** - * Unmarshal the state of the connections - * - * @param buf Buffer for the marshalled data - * @return The number of unmarshalled bytes, 0 on error - */ -size_t server_unmarshal(const void* restrict buf); +void disconnect_all(void); /** * The program's main loop @@ -99,3 +31,6 @@ size_t server_unmarshal(const void* restrict buf); */ int main_loop(void); + +#endif + diff --git a/src/state.c b/src/state.c new file mode 100644 index 0000000..a7fd57f --- /dev/null +++ b/src/state.c @@ -0,0 +1,363 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "state.h" +#include "util.h" + +#include +#include + + + +/** + * The name of the process + */ +char* restrict argv0; /* do not marshal */ + +/** + * The real pathname of the process's binary, + * `NULL` if `argv0` is satisfactory + */ +char* restrict argv0_real = NULL; + +/** + * Array of all outputs + */ +struct output* restrict outputs = NULL; + +/** + * The nubmer of elements in `outputs` + */ +size_t outputs_n = 0; + +/** + * The server socket's file descriptor + */ +int socketfd = -1; + +/** + * Has the process receive a signal + * telling it to re-execute? + */ +volatile sig_atomic_t reexec = 0; /* do not marshal */ + +/** + * Has the process receive a signal + * telling it to terminate? + */ +volatile sig_atomic_t terminate = 0; /* do not marshal */ + +/** + * Has the process receive a to + * disconnect from or reconnect to + * the site? 1 if disconnct, 2 if + * reconnect, 0 otherwise. + */ +volatile sig_atomic_t connection = 0; + +/** + * List of all client's file descriptors + * + * Unused slots, with index less than `connections_used`, + * should have the value -1 (negative) + */ +int* restrict connections = NULL; + +/** + * The number of elements allocated for `connections` + */ +size_t connections_alloc = 0; + +/** + * The index of the first unused slot in `connections` + */ +size_t connections_ptr = 0; + +/** + * The index of the last used slot in `connections`, plus 1 + */ +size_t connections_used = 0; + +/** + * The clients' connections' inbound-message buffers + */ +struct message* restrict inbound = NULL; + +/** + * The clients' connections' outbound-message buffers + */ +struct ring* restrict outbound = NULL; + +/** + * Is the server connect to the display? + * + * Set to true before the initial connection + */ +int connected = 1; + +/** + * The adjustment method, -1 for automatic + */ +int method = -1; + +/** + * The site's name, may be `NULL` + */ +char* restrict sitename = NULL; + +/** + * The libgamma site state + */ +libgamma_site_state_t site; /* do not marshal */ + +/** + * The libgamma partition states + */ +libgamma_partition_state_t* restrict partitions = NULL; /* do not marshal */ + +/** + * The libgamma CRTC states + */ +libgamma_crtc_state_t* restrict crtcs = NULL; /* do not marshal */ + + + +/** + * Destroy the state + */ +void state_destroy(void) +{ + size_t i; + + for (i = 0; i < connections_used; i++) + if (connections[i] >= 0) + { + message_destroy(inbound + i); + ring_destroy(outbound + i); + } + free(inbound); + free(outbound); + free(connections); + + if (outputs != NULL) + for (i = 0; i < outputs_n; i++) + { + if (crtcs == NULL) + libgamma_crtc_destroy(outputs[i].crtc + i); + output_destroy(outputs + i); + } + free(outputs); + if (crtcs != NULL) + for (i = 0; i < outputs_n; i++) + libgamma_crtc_destroy(crtcs + i); + free(crtcs); + if (partitions != NULL) + for (i = 0; i < site.partitions_available; i++) + libgamma_partition_destroy(partitions + i); + free(partitions); + libgamma_site_destroy(&site); + + free(argv0_real); + free(sitename); +} + + +#if defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wcast-align" +#endif + + +/** + * Marshal the state + * + * @param buf Output buffer for the marshalled data, + * `NULL` to only measure how many bytes + * this buffer needs + * @return The number of marshalled bytes + */ +size_t state_marshal(void* restrict buf) +{ + size_t off = 0, i, n; + char* restrict bs = buf; + + if (argv0_real == NULL) + { + if (bs != NULL) + *(bs + off) = '\0'; + off += 1; + } + else + { + n = strlen(argv0_real) + 1; + if (bs != NULL) + memcpy(bs + off, argv0_real, n); + off += n; + } + + if (bs != NULL) + *(size_t*)(bs + off) = outputs_n; + off += sizeof(size_t); + + for (i = 0; i < outputs_n; i++) + off += output_marshal(outputs + i, bs ? bs + off : NULL); + + if (bs != NULL) + *(int*)(bs + off) = socketfd; + off += sizeof(int); + + if (bs != NULL) + *(sig_atomic_t*)(bs + off) = connection; + off += sizeof(sig_atomic_t); + + if (bs != NULL) + *(int*)(bs + off) = connected; + off += sizeof(int); + + if (bs != NULL) + *(size_t*)(bs + off) = connections_ptr; + off += sizeof(size_t); + + if (bs != NULL) + *(size_t*)(bs + off) = connections_used; + off += sizeof(size_t); + + if (bs != NULL) + memcpy(bs + off, connections, connections_used * sizeof(*connections)); + off += connections_used * sizeof(*connections); + + for (i = 0; i < connections_used; i++) + if (connections[i] >= 0) + { + off += message_marshal(inbound + i, bs ? bs + off : NULL); + off += ring_marshal(outbound + i, bs ? bs + off : NULL); + } + + if (bs != NULL) + *(int*)(bs + off) = method; + off += sizeof(int); + + if (bs != NULL) + *(int*)(bs + off) = sitename != NULL; + off += sizeof(int); + if (sitename != NULL) + { + n = strlen(sitename) + 1; + if (bs != NULL) + memcpy(bs + off, sitename, n); + off += n; + } + + return off; +} + + +/** + * Unmarshal the state + * + * @param buf Buffer for the marshalled data + * @return The number of unmarshalled bytes, 0 on error + */ +size_t state_unmarshal(const void* restrict buf) +{ + size_t off = 0, i, n; + const char* restrict bs = buf; + + connections = NULL; + inbound = NULL; + + if (*(bs + off)) + { + off += 1; + n = strlen(bs + off) + 1; + if (!(argv0_real = memdup(bs + off, n))) + return 0; + off += n; + } + else + off += 1; + + outputs_n = *(const size_t*)(bs + off); + off += sizeof(size_t); + + for (i = 0; i < outputs_n; i++) + { + off += n = output_unmarshal(outputs + i, bs + off); + if (n == 0) + return 0; + } + + socketfd = *(const int*)(bs + off); + off += sizeof(int); + + connection = *(const sig_atomic_t*)(bs + off); + off += sizeof(sig_atomic_t); + + connected = *(const int*)(bs + off); + off += sizeof(int); + + connections_ptr = *(const size_t*)(bs + off); + off += sizeof(size_t); + + connections_alloc = connections_used = *(const size_t*)(bs + off); + off += sizeof(size_t); + + if (connections_alloc > 0) + { + connections = memdup(bs + off, connections_alloc * sizeof(*connections)); + if (connections == NULL) + return 0; + off += connections_used * sizeof(*connections); + + inbound = malloc(connections_alloc * sizeof(*inbound)); + if (inbound == NULL) + return 0; + } + + for (i = 0; i < connections_used; i++) + if (connections[i] >= 0) + { + off += n = message_unmarshal(inbound + i, bs + off); + if (n == 0) + return 0; + off += n = ring_unmarshal(outbound + i, bs + off); + if (n == 0) + return 0; + } + + method = *(const int*)(bs + off); + off += sizeof(int); + + if (*(const int*)(bs + off)) + { + off += sizeof(int); + n = strlen(bs + off) + 1; + if (!(sitename = memdup(bs + off, n))) + return 0; + off += n; + } + else + off += sizeof(int); + + return off; +} + + +#if defined(__clang__) +# pragma GCC diagnostic pop +#endif + diff --git a/src/state.h b/src/state.h new file mode 100644 index 0000000..0a09023 --- /dev/null +++ b/src/state.h @@ -0,0 +1,182 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef STATE_H +#define STATE_H + + +#include "types/message.h" +#include "types/ring.h" +#include "types/output.h" + +#include + +#include +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * The name of the process + */ +extern char* restrict argv0; + +/** + * The real pathname of the process's binary, + * `NULL` if `argv0` is satisfactory + */ +extern char* restrict argv0_real; + +/** + * Array of all outputs + */ +extern struct output* restrict outputs; + +/** + * The nubmer of elements in `outputs` + */ +extern size_t outputs_n; + +/** + * The server socket's file descriptor + */ +extern int socketfd; + +/** + * Has the process receive a signal + * telling it to re-execute? + */ +extern volatile sig_atomic_t reexec; + +/** + * Has the process receive a signal + * telling it to terminate? + */ +extern volatile sig_atomic_t terminate; + +/** + * Has the process receive a to + * disconnect from or reconnect to + * the site? 1 if disconnct, 2 if + * reconnect, 0 otherwise. + */ +extern volatile sig_atomic_t connection; + +/** + * List of all client's file descriptors + * + * Unused slots, with index less than `connections_used`, + * should have the value -1 (negative) + */ +extern int* restrict connections; + +/** + * The number of elements allocated for `connections` + */ +extern size_t connections_alloc; + +/** + * The index of the first unused slot in `connections` + */ +extern size_t connections_ptr; + +/** + * The index of the last used slot in `connections`, plus 1 + */ +extern size_t connections_used; + +/** + * The clients' connections' inbound-message buffers + */ +extern struct message* restrict inbound; + +/** + * The clients' connections' outbound-message buffers + */ +extern struct ring* restrict outbound; + +/** + * Is the server connect to the display? + * + * Set to true before the initial connection + */ +extern int connected; + +/** + * The adjustment method, -1 for automatic + */ +extern int method; + +/** + * The site's name, may be `NULL` + */ +extern char* restrict sitename; + +/** + * The libgamma site state + */ +extern libgamma_site_state_t site; + +/** + * The libgamma partition states + */ +extern libgamma_partition_state_t* restrict partitions; + +/** + * The libgamma CRTC states + */ +extern libgamma_crtc_state_t* restrict crtcs; + + + +/** + * Destroy the state + */ +void state_destroy(void); + +/** + * Marshal the state + * + * @param buf Output buffer for the marshalled data, + * `NULL` to only measure how many bytes + * this buffer needs + * @return The number of marshalled bytes + */ +size_t state_marshal(void* restrict buf); + +/** + * Unmarshal the state + * + * @param buf Buffer for the marshalled data + * @return The number of unmarshalled bytes, 0 on error + */ +GCC_ONLY(__attribute__((nonnull))) +size_t state_unmarshal(const void* restrict buf); + + +#endif + diff --git a/src/types/filter.c b/src/types/filter.c new file mode 100644 index 0000000..e6facc9 --- /dev/null +++ b/src/types/filter.c @@ -0,0 +1,144 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "filter.h" +#include "../util.h" + +#include +#include + + + +/** + * Free all resources allocated to a filter. + * The allocation of `filter` itself is not freed. + * + * @param this The filter + */ +void filter_destroy(struct filter* restrict this) +{ + free(this->class); + free(this->ramps); +} + + + +#if defined(__clang__) +# pragma GCC diagnostic ignored "-Wcast-align" +#endif + + +/** + * Marshal a filter + * + * @param this The filter + * @param buf Output buffer for the marshalled filter, + * `NULL` just measure how large the buffers + * needs to be + * @param ramps_size The byte-size of `this->ramps` + * @return The number of marshalled byte + */ +size_t filter_marshal(const struct filter* restrict this, void* restrict buf, size_t ramps_size) +{ + size_t off = 0, n; + char nonnulls = 0; + char* restrict bs = buf; + + if (bs != NULL) + { + if (this->class != NULL) nonnulls |= 1; + if (this->ramps != NULL) nonnulls |= 2; + *(bs + off) = nonnulls; + } + off += 1; + + if (bs != NULL) + *(int64_t*)(bs + off) = this->priority; + off += sizeof(int64_t); + + if (bs != NULL) + *(enum lifespan*)(bs + off) = this->lifespan; + off += sizeof(enum lifespan); + + if (this->class != NULL) + { + n = strlen(this->class) + 1; + if (bs != NULL) + memcpy(bs + off, this->class, n); + off += n; + } + + if (this->ramps != NULL) + { + if (bs != NULL) + memcpy(bs + off, this->ramps, ramps_size); + off += ramps_size; + } + + return off; +} + + +/** + * Unmarshal a filter + * + * @param this Output for the filter + * @param buf Buffer with the marshalled filter + * @param ramps_size The byte-size of `this->ramps` + * @return The number of unmarshalled bytes, 0 on error + */ +size_t filter_unmarshal(struct filter* restrict this, const void* restrict buf, size_t ramps_size) +{ + size_t off = 0, n; + char nonnulls = 0; + const char* restrict bs = buf; + + nonnulls = *(bs + off); + off += 1; + + this->class = NULL; + this->ramps = NULL; + + this->priority = *(const int64_t*)(bs + off); + off += sizeof(int64_t); + + this->lifespan = *(const enum lifespan*)(bs + off); + off += sizeof(enum lifespan); + + if (nonnulls & 1) + { + n = strlen(bs + off) + 1; + if (!(this->class = memdup(bs + off, n))) + goto fail; + off += n; + } + + if (nonnulls & 2) + { + if (!(this->ramps = memdup(bs + off, ramps_size))) + goto fail; + off += ramps_size; + } + + return off; + + fail: + free(this->class); + free(this->ramps); + return 0; +} + diff --git a/src/types/filter.h b/src/types/filter.h new file mode 100644 index 0000000..c9e01bb --- /dev/null +++ b/src/types/filter.h @@ -0,0 +1,138 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef TYPES_FILTER_H +#define TYPES_FILTER_H + + +#include +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * The lifespan of a filter + */ +enum lifespan +{ + /** + * The filter should be applied + * until it is explicitly removed + */ + LIFESPAN_UNTIL_REMOVAL, + + /** + * The filter should be applied + * until the client exists + */ + LIFESPAN_UNTIL_DEATH, + + /** + * The filter should be removed now + */ + LIFESPAN_REMOVE + +}; + + +/** + * Information about a filter + */ +struct filter +{ + /** + * The client that applied it. This need not be + * set unless `.lifespan == LIFESPAN_UNTIL_DEATH` + * and unless the process itself added this. + * This is the file descriptor of the client's + * connection. + */ + int client; + + /** + * The lifespan of the filter + */ + enum lifespan lifespan; + + /** + * The priority of the filter + */ + int64_t priority; + + /** + * Identifier for the filter + */ + char* class; + + /** + * The gamma ramp adjustments for the filter. + * This is raw binary data. `NULL` iff + * `lifespan == LIFESPAN_REMOVE`. + */ + void* ramps; + +}; + + + +/** + * Free all resources allocated to a filter. + * The allocation of `filter` itself is not freed. + * + * @param this The filter + */ +GCC_ONLY(__attribute__((nonnull))) +void filter_destroy(struct filter* restrict this); + +/** + * Marshal a filter + * + * @param this The filter + * @param buf Output buffer for the marshalled filter, + * `NULL` just measure how large the buffers + * needs to be + * @param ramps_size The byte-size of `filter->ramps` + * @return The number of marshalled byte + */ +GCC_ONLY(__attribute__((nonnull(1)))) +size_t filter_marshal(const struct filter* restrict this, void* restrict buf, size_t ramps_size); + +/** + * Unmarshal a filter + * + * @param this Output for the filter, `.red_size`, `.green_size`, + * and `.blue_size` must already be set + * @param buf Buffer with the marshalled filter + * @param ramps_size The byte-size of `filter->ramps` + * @return The number of unmarshalled bytes, 0 on error + */ +GCC_ONLY(__attribute__((nonnull))) +size_t filter_unmarshal(struct filter* restrict this, const void* restrict buf, size_t ramps_size); + + +#endif + diff --git a/src/types/message.c b/src/types/message.c new file mode 100644 index 0000000..e7c844d --- /dev/null +++ b/src/types/message.c @@ -0,0 +1,572 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "message.h" +#include "../util.h" + +#include +#include +#include +#include +#include + + + +/** + * Initialise a message slot so that it can + * be used by to read messages + * + * @param this Memory slot in which to store the new message + * @return Non-zero on error, `errno` will be set accordingly + */ +int message_initialise(struct message* restrict this) +{ + this->headers = NULL; + this->header_count = 0; + this->payload = NULL; + this->payload_size = 0; + this->payload_ptr = 0; + this->buffer_size = 128; + this->buffer_ptr = 0; + this->stage = 0; + this->buffer = malloc(this->buffer_size); + if (this->buffer == NULL) + return -1; + return 0; +} + + +/** + * Release all resources in a message, should + * be done even if initialisation fails + * + * @param this The message + */ +void message_destroy(struct message* restrict this) +{ + size_t i; + if (this->headers != NULL) + for (i = 0; i < this->header_count; i++) + free(this->headers[i]); + + free(this->headers); + free(this->payload); + free(this->buffer); +} + + + +#if defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wcast-align" +#endif + + +/** + * Marshal a message for state serialisation + * + * @param this The message + * @param buf Output buffer for the marshalled data, + * `NULL` just measure how large the buffers + * needs to be + * @return The number of marshalled byte + */ +size_t message_marshal(const struct message* restrict this, void* restrict buf) +{ + size_t i, n, off = 0; + char* bs = buf; + + if (bs != NULL) + *(size_t*)(bs + off) = this->header_count; + off += sizeof(size_t); + + if (bs != NULL) + *(size_t*)(bs + off) = this->payload_size; + off += sizeof(size_t); + + if (bs != NULL) + *(size_t*)(bs + off) = this->payload_ptr; + off += sizeof(size_t); + + if (bs != NULL) + *(size_t*)(bs + off) = this->buffer_ptr; + off += sizeof(size_t); + + if (bs != NULL) + *(int*)(bs + off) = this->stage; + off += sizeof(int); + + for (i = 0; i < this->header_count; i++) + { + n = strlen(this->headers[i]) + 1; + if (bs != NULL) + memcpy(bs + off, this->headers[i], n); + off += n; + } + + if (bs != NULL) + memcpy(bs + off, this->payload, this->payload_ptr); + off += this->payload_ptr; + + if (bs != NULL) + memcpy(bs + off, this->buffer, this->buffer_ptr); + off += this->buffer_ptr; + + return off; +} + + +/** + * Unmarshal a message for state deserialisation + * + * @param this Memory slot in which to store the new message + * @param buf In buffer with the marshalled data + * @return The number of unmarshalled bytes, 0 on error + */ +size_t message_unmarshal(struct message* restrict this, const void* restrict buf) +{ + size_t i, n, off = 0, header_count; + const char* bs = buf; + + this->header_count = 0; + + header_count = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->payload_size = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->payload_ptr = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->buffer_size = this->buffer_ptr = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->stage = *(const int*)(bs + off); + off += sizeof(int); + + /* Make sure that the pointers are NULL so that they are + not freed without being allocated when the message is + destroyed if this function fails. */ + this->headers = NULL; + this->payload = NULL; + this->buffer = NULL; + + /* To 2-power-multiple of 128 bytes. */ + this->buffer_size >>= 7; + if (this->buffer_size == 0) + this->buffer_size = 1; + else + { + this->buffer_size -= 1; + this->buffer_size |= this->buffer_size >> 1; + this->buffer_size |= this->buffer_size >> 2; + this->buffer_size |= this->buffer_size >> 4; + this->buffer_size |= this->buffer_size >> 8; + this->buffer_size |= this->buffer_size >> 16; +#if SIZE_MAX == UINT64_MAX + this->buffer_size |= this->buffer_size >> 32; +#endif + this->buffer_size += 1; + } + this->buffer_size <<= 7; + + /* Allocate header list, payload and read buffer. */ + + if (header_count > 0) + if (!(this->headers = malloc(header_count * sizeof(char*)))) + goto fail; + + if (this->payload_size > 0) + if (!(this->payload = malloc(this->payload_size))) + goto fail; + + if (!(this->buffer = malloc(this->buffer_size))) + goto fail; + + /* Fill the header list, payload and read buffer. */ + + for (i = 0; i < header_count; i++) + { + n = strlen(bs + off) + 1; + this->headers[i] = memdup(bs + off, n); + if (this->headers[i] == NULL) + goto fail; + off += n; + this->header_count++; + } + + memcpy(this->payload, bs + off, this->payload_ptr); + off += this->payload_ptr; + + memcpy(this->buffer, bs + off, this->buffer_ptr); + off += this->buffer_ptr; + + return off; + + fail: + return 0; +} + + +#if defined(__clang__) +# pragma GCC diagnostic pop +#endif + + + +/** + * Extend the header list's allocation + * + * @param this The message + * @param extent The number of additional entries + * @return Zero on success, -1 on error + */ +GCC_ONLY(__attribute__((nonnull))) +static int extend_headers(struct message* restrict this, size_t extent) +{ + char** new; + if (!(new = realloc(this->headers, (this->header_count + extent) * sizeof(char*)))) + return -1; + this->headers = new; + return 0; +} + + +/** + * Extend the read buffer by way of doubling + * + * @param this The message + * @return Zero on success, -1 on error + */ +GCC_ONLY(__attribute__((nonnull))) +static int extend_buffer(struct message* restrict this) +{ + char* restrict new; + if (!(new = realloc(this->buffer, (this->buffer_size << 1) * sizeof(char)))) + return -1; + this->buffer = new; + this->buffer_size <<= 1; + return 0; +} + + +/** + * Reset the header list and the payload + * + * @param this The message + */ +GCC_ONLY(__attribute__((nonnull))) +static void reset_message(struct message* restrict this) +{ + size_t i; + if (this->headers != NULL) + for (i = 0; i < this->header_count; i++) + free(this->headers[i]); + free(this->headers); + this->headers = NULL; + this->header_count = 0; + + free(this->payload); + this->payload = NULL; + this->payload_size = 0; + this->payload_ptr = 0; +} + + +/** + * Read the headers the message and determine, and store, its payload's length + * + * @param this The message + * @return Zero on success, negative on error (malformated message: unrecoverable state) + */ +GCC_ONLY(__attribute__((pure, nonnull))) +static int get_payload_length(struct message* restrict this) +{ + char* header; + size_t i; + + for (i = 0; i < this->header_count; i++) + if (strstr(this->headers[i], "Length: ") == this->headers[i]) + { + /* Store the message length. */ + header = this->headers[i] + strlen("Length: "); + this->payload_size = (size_t)atol(header); + + /* Do not except a length that is not correctly formated. */ + for (; *header; header++) + if ((*header < '0') || ('9' < *header)) + return -2; /* Malformated value, enters unrecoverable state. */ + + /* Stop searching for the ‘Length’ header, we have found and parsed it. */ + break; + } + + return 0; +} + + +/** + * Verify that a header is correctly formatted + * + * @param header The header, must be NUL-terminated + * @param length The length of the header + * @return Zero if valid, negative if invalid (malformated message: unrecoverable state) + */ +GCC_ONLY(__attribute__((pure, nonnull))) +static int validate_header(const char* restrict header, size_t length) +{ + char* restrict p = memchr(header, ':', length * sizeof(char)); + + if (verify_utf8(header, 0) < 0) + /* Either the string is not UTF-8, or your are under an UTF-8 attack, + let's just call this unrecoverable because the client will not correct. */ + return -2; + + if ((p == NULL) || /* Buck you, rawmemchr should not segfault the program. */ + (p[1] != ' ')) /* Also an invalid format. ' ' is mandated after the ':'. */ + return -2; + + return 0; +} + + +/** + * Remove the beginning of the read buffer + * + * @param this The message + * @param length The number of characters to remove + * @param update_ptr Whether to update the buffer pointer + */ +GCC_ONLY(__attribute__((nonnull))) +static void unbuffer_beginning(struct message* restrict this, size_t length, int update_ptr) +{ + memmove(this->buffer, this->buffer + length, (this->buffer_ptr - length) * sizeof(char)); + if (update_ptr) + this->buffer_ptr -= length; +} + + +/** + * Remove the header–payload delimiter from the buffer, + * get the payload's size and allocate the payload + * + * @param this The message + * @return The return value follows the rules of `message_read` + */ +GCC_ONLY(__attribute__((nonnull))) +static int initialise_payload(struct message* restrict this) +{ + /* Remove the \n (end of empty line) we found from the buffer. */ + unbuffer_beginning(this, 1, 1); + + /* Get the length of the payload. */ + if (get_payload_length(this) < 0) + return -2; /* Malformated value, enters unrecoverable state. */ + + /* Allocate the payload buffer. */ + if (this->payload_size > 0) + if (!(this->payload = malloc(this->payload_size))) + return -1; + + return 0; +} + + +/** + * Create a header from the buffer and store it + * + * @param this The message + * @param length The length of the header, including LF-termination + * @return The return value follows the rules of `message_read` + */ +GCC_ONLY(__attribute__((nonnull))) +static int store_header(struct message* restrict this, size_t length) +{ + char* restrict header; + + /* Allocate the header. */ + if (!(header = malloc(length))) /* Last char is a LF, which is substituted with NUL. */ + return -1; + /* Copy the header data into the allocated header, */ + memcpy(header, this->buffer, length * sizeof(char)); + /* and NUL-terminate it. */ + header[length - 1] = '\0'; + + /* Remove the header data from the read buffer. */ + unbuffer_beginning(this, length, 1); + + /* Make sure the the header syntax is correct so that + the program does not need to care about it. */ + if (validate_header(header, length)) + { + free(header); + return -2; + } + + /* Store the header in the header list. */ + this->headers[this->header_count++] = header; + + return 0; +} + + +/** + * Continue reading from the socket into the buffer + * + * @param this The message + * @param fd The file descriptor of the socket + * @return The return value follows the rules of `message_read` + */ +GCC_ONLY(__attribute__((nonnull))) +static int continue_read(struct message* restrict this, int fd) +{ + size_t n; + ssize_t got; + int r; + + /* Figure out how much space we have left in the read buffer. */ + n = this->buffer_size - this->buffer_ptr; + + /* If we do not have too much left, */ + if (n < 128) + { + /* grow the buffer, */ + if ((r = extend_buffer(this)) < 0) + return r; + + /* and recalculate how much space we have left. */ + n = this->buffer_size - this->buffer_ptr; + } + + /* Then read from the socket. */ + errno = 0; + got = recv(fd, this->buffer + this->buffer_ptr, n, 0); + this->buffer_ptr += (size_t)(got < 0 ? 0 : got); + if (errno) + return -1; + if (got == 0) + { + errno = ECONNRESET; + return -1; + } + + return 0; +} + + +/** + * Read the next message from a file descriptor + * + * @param this Memory slot in which to store the new message + * @param fd The file descriptor + * @return 0: At least one message is available + * -1: Exceptional connection: + * EINTR: System call interrupted + * EAGAIN: No message is available + * EWOULDBLOCK: No message is available + * ECONNRESET: Connection closed + * Other: Failure + * -2: Corrupt message (unrecoverable) + */ +GCC_ONLY(__attribute__((nonnull))) +int message_read(struct message* restrict this, int fd) +{ + size_t header_commit_buffer = 0; + int r; + + /* If we are at stage 2, we are done and it is time to start over. + This is important because the function could have been interrupted. */ + if (this->stage == 2) + { + reset_message(this); + this->stage = 0; + } + + /* Read from file descriptor until we have a full message. */ + for (;;) + { + char* p; + size_t length; + + /* Stage 0: headers. */ + /* Read all headers that we have stored into the read buffer. */ + while ((this->stage == 0) && + ((p = memchr(this->buffer, '\n', this->buffer_ptr * sizeof(char))) != NULL)) + if ((length = (size_t)(p - this->buffer))) + { + /* We have found a header. */ + + /* On every eighth header found with this function call, + we prepare the header list for eight more headers so + that it does not need to be reallocated again and again. */ + if (header_commit_buffer == 0) + if ((r = extend_headers(this, header_commit_buffer = 8)) < 0) + return r; + + /* Create and store header. */ + if ((r = store_header(this, length + 1)) < 0) + return r; + header_commit_buffer -= 1; + } + else + { + /* We have found an empty line, i.e. the end of the headers. */ + + /* Remove the header–payload delimiter from the buffer, + get the payload's size and allocate the payload. */ + if ((r = initialise_payload(this)) < 0) + return r; + + /* Mark end of stage, next stage is getting the payload. */ + this->stage = 1; + } + + + /* Stage 1: payload. */ + if ((this->stage == 1) && (this->payload_size > 0)) + { + /* How much of the payload that has not yet been filled. */ + size_t need = this->payload_size - this->payload_ptr; + /* How much we have of that what is needed. */ + size_t move = this->buffer_ptr < need ? this->buffer_ptr : need; + + /* Copy what we have, and remove it from the the read buffer. */ + memcpy(this->payload + this->payload_ptr, this->buffer, move * sizeof(char)); + unbuffer_beginning(this, move, 1); + + /* Keep track of how much we have read. */ + this->payload_ptr += move; + } + if ((this->stage == 1) && (this->payload_ptr == this->payload_size)) + { + /* If we have filled the payload (or there was no payload), + mark the end of this stage, i.e. that the message is + complete, and return with success. */ + this->stage = 2; + return 0; + } + + + /* If stage 1 was not completed. */ + + /* Continue reading from the socket into the buffer. */ + if ((r = continue_read(this, fd)) < 0) + return r; + } +} + diff --git a/src/types/message.h b/src/types/message.h new file mode 100644 index 0000000..00a2a50 --- /dev/null +++ b/src/types/message.h @@ -0,0 +1,160 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef TYPES_MESSAGE_H +#define TYPES_MESSAGE_H + + +#include +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Message passed between a server and a client + */ +struct message +{ + /** + * The headers in the message, each element in this list + * as an unparsed header, it consists of both the header + * name and its associated value, joined by ": ". A header + * cannot be `NULL` (unless its memory allocation failed,) + * but `headers` itself is `NULL` if there are no headers. + * The "Length" header should be included in this list. + */ + char** restrict headers; + + /** + * The number of headers in the message + */ + size_t header_count; + + /** + * The payload of the message, `NULL` if none (of zero-length) + */ + char* restrict payload; + + /** + * The size of the payload + */ + size_t payload_size; + + /** + * How much of the payload that has been stored (internal data) + */ + size_t payload_ptr; + + /** + * Internal buffer for the reading function (internal data) + */ + char* restrict buffer; + + /** + * The size allocated to `buffer` (internal data) + */ + size_t buffer_size; + + /** + * The number of bytes used in `buffer` (internal data) + */ + size_t buffer_ptr; + + /** + * 0 while reading headers, 1 while reading payload, and 2 when done (internal data) + */ + int stage; + +#if INT_MAX != LONG_MAX + int padding__; +#endif + +}; + + + +/** + * Initialise a message slot so that it can + * be used by to read messages + * + * @param this Memory slot in which to store the new message + * @return Non-zero on error, `errno` will be set accordingly + */ +GCC_ONLY(__attribute__((nonnull))) +int message_initialise(struct message* restrict this); + +/** + * Release all resources in a message, should + * be done even if initialisation fails + * + * @param this The message + */ +GCC_ONLY(__attribute__((nonnull))) +void message_destroy(struct message* restrict this); + +/** + * Marshal a message for state serialisation + * + * @param this The message + * @param buf Output buffer for the marshalled data, + * `NULL` just measure how large the buffers + * needs to be + * @return The number of marshalled byte + */ +GCC_ONLY(__attribute__((nonnull(1)))) +size_t message_marshal(const struct message* restrict this, void* restrict buf); + +/** + * Unmarshal a message for state deserialisation + * + * @param this Memory slot in which to store the new message + * @param buf In buffer with the marshalled data + * @return The number of unmarshalled bytes, 0 on error + */ +GCC_ONLY(__attribute__((nonnull))) +size_t message_unmarshal(struct message* restrict this, const void* restrict buf); + +/** + * Read the next message from a file descriptor + * + * @param this Memory slot in which to store the new message + * @param fd The file descriptor + * @return 0: At least one message is available + * -1: Exceptional connection: + * EINTR: System call interrupted + * EAGAIN: No message is available + * EWOULDBLOCK: No message is available + * ECONNRESET: Connection closed + * Other: Failure + * -2: Corrupt message (unrecoverable) + */ +GCC_ONLY(__attribute__((nonnull))) +int message_read(struct message* restrict this, int fd); + + +#endif + diff --git a/src/types/output.c b/src/types/output.c new file mode 100644 index 0000000..8576baa --- /dev/null +++ b/src/types/output.c @@ -0,0 +1,264 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "output.h" +#include "../util.h" + +#include +#include + + + +/** + * Free all resources allocated to an output. + * The allocation of `output` itself is not freed, + * nor is its the libgamma destroyed. + * + * @param this The output + */ +void output_destroy(struct output* restrict this) +{ + size_t i; + + if (this->supported != LIBGAMMA_NO) + switch (this->depth) + { + case 8: + libgamma_gamma_ramps8_destroy(&(this->saved_ramps.u8)); + for (i = 0; i < this->table_size; i++) + libgamma_gamma_ramps8_destroy(&(this->table_sums[i].u8)); + break; + case 16: + libgamma_gamma_ramps16_destroy(&(this->saved_ramps.u16)); + for (i = 0; i < this->table_size; i++) + libgamma_gamma_ramps16_destroy(&(this->table_sums[i].u16)); + break; + case 32: + libgamma_gamma_ramps32_destroy(&(this->saved_ramps.u32)); + for (i = 0; i < this->table_size; i++) + libgamma_gamma_ramps32_destroy(&(this->table_sums[i].u32)); + break; + case 64: + libgamma_gamma_ramps64_destroy(&(this->saved_ramps.u64)); + for (i = 0; i < this->table_size; i++) + libgamma_gamma_ramps64_destroy(&(this->table_sums[i].u64)); + break; + case -1: + libgamma_gamma_rampsf_destroy(&(this->saved_ramps.f)); + for (i = 0; i < this->table_size; i++) + libgamma_gamma_rampsf_destroy(&(this->table_sums[i].f)); + break; + case -2: + libgamma_gamma_rampsd_destroy(&(this->saved_ramps.d)); + for (i = 0; i < this->table_size; i++) + libgamma_gamma_rampsd_destroy(&(this->table_sums[i].d)); + break; + default: + break; /* impossible */ + } + + for (i = 0; i < this->table_size; i++) + filter_destroy(this->table_filters + i); + + free(this->table_filters); + free(this->table_sums); + free(this->name); +} + + + +#if defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wcast-align" +#endif + + +/** + * Marshal an output + * + * @param this The output + * @param buf Output buffer for the marshalled output, + * `NULL` just measure how large the buffers + * needs to be + * @return The number of marshalled byte + */ +size_t output_marshal(const struct output* restrict this, void* restrict buf) +{ + size_t off = 0, i, n; + char* bs = buf; + + if (bs != NULL) + *(signed*)(bs + off) = this->depth; + off += sizeof(signed); + + if (bs != NULL) + *(size_t*)(bs + off) = this->red_size; + off += sizeof(size_t); + + if (bs != NULL) + *(size_t*)(bs + off) = this->green_size; + off += sizeof(size_t); + + if (bs != NULL) + *(size_t*)(bs + off) = this->blue_size; + off += sizeof(size_t); + + if (bs != NULL) + *(size_t*)(bs + off) = this->ramps_size; + off += sizeof(size_t); + + if (bs != NULL) + *(enum libgamma_decision*)(bs + off) = this->supported; + off += sizeof(enum libgamma_decision); + + n = strlen(this->name) + 1; + if (bs != NULL) + memcpy(bs + off, this->name, n); + off += n; + + off += gamma_ramps_marshal(&(this->saved_ramps), bs ? bs + off : NULL, this->ramps_size); + + if (bs != NULL) + *(size_t*)(bs + off) = this->table_size; + off += sizeof(size_t); + + for (i = 0; i < this->table_size; i++) + { + off += filter_marshal(this->table_filters + i, bs ? bs + off : NULL, this->ramps_size); + off += gamma_ramps_marshal(this->table_sums + i, bs ? bs + off : NULL, this->ramps_size); + } + + return off; +} + + +/** + * Unmarshal an output + * + * @param this Output for the output + * @param buf Buffer with the marshalled output + * @return The number of unmarshalled bytes, 0 on error + */ +size_t output_unmarshal(struct output* restrict this, const void* restrict buf) +{ + size_t off = 0, i, n; + const char* bs = buf; + + this->crtc = NULL; + this->name = NULL; + + this->depth = *(const signed*)(bs + off); + off += sizeof(signed); + + this->red_size = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->green_size = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->blue_size = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->ramps_size = *(const size_t*)(bs + off); + off += sizeof(size_t); + + this->supported = *(const enum libgamma_decision*)(bs + off); + off += sizeof(enum libgamma_decision); + + n = strlen(bs + off) + 1; + this->name = memdup(bs + off, n); + if (this->name == NULL) + return 0; + + off += n = gamma_ramps_unmarshal(&(this->saved_ramps), bs, this->ramps_size); + COPY_RAMP_SIZES(&(this->saved_ramps.u8), this); + if (n == 0) + return 0; + + this->table_size = this->table_alloc = *(const size_t*)(bs + off); + off += sizeof(size_t); + if (this->table_size > 0) + { + this->table_filters = calloc(this->table_size, sizeof(*(this->table_filters))); + if (this->table_filters == NULL) + return 0; + this->table_sums = calloc(this->table_size, sizeof(*(this->table_sums))); + if (this->table_sums == NULL) + return 0; + } + + for (i = 0; i < this->table_size; i++) + { + off += n = filter_unmarshal(this->table_filters + i, bs + off, this->ramps_size); + if (n == 0) + return 0; + COPY_RAMP_SIZES(&(this->table_sums[i].u8), this); + off += n = gamma_ramps_unmarshal(this->table_sums + i, bs + off, this->ramps_size); + if (n == 0) + return 0; + } + + return off; +} + + +#if defined(__clang__) +# pragma GCC diagnostic pop +#endif + + + +/** + * Compare to outputs by the names of their respective CRTC:s + * + * @param a Return -1 if this one is lower + * @param b Return +1 if this one is higher + * @return See description of `a` and `b`, + * 0 if returned if they are the same + */ +int output_cmp_by_name(const void* restrict a, const void* restrict b) +{ + const char* an = ((const struct output*)a)->name; + const char* bn = ((const struct output*)b)->name; + return strcmp(an, bn); +} + + +/** + * Find an output by its name + * + * @param key The name of the output + * @param base The array of outputs + * @param n The number of elements in `base` + * @return Output find in `base`, `NULL` if not found + */ +struct output* output_find_by_name(const char* restrict key, struct output* restrict base, size_t n) +{ + struct output k; + +#if defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wcast-qual" +#endif + k.name = (char*)key; +#if defined(__GNUC__) +# pragma GCC diagnostic pop +#endif + + return bsearch(&k, base, n, sizeof(*base), output_cmp_by_name); +} + diff --git a/src/types/output.h b/src/types/output.h new file mode 100644 index 0000000..f8a4c3e --- /dev/null +++ b/src/types/output.h @@ -0,0 +1,206 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef TYPES_OUTPUT_H +#define TYPES_OUTPUT_H + + +#include + +#include + +#include "ramps.h" +#include "filter.h" + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Copy the ramp sizes + * + * This macro supports both `struct output` + * and `struct gamma_ramps` + * + * @param dest The destination + * @param src The source + */ +#define COPY_RAMP_SIZES(dest, src) \ + ((dest)->red_size = (src)->red_size, \ + (dest)->green_size = (src)->green_size, \ + (dest)->blue_size = (src)->blue_size) + + + +/** + * Information about an output + */ +struct output +{ + /** + * -2: double + * -1: float + * 8: uint8_t + * 16: uint16_t + * 32: uint32_t + * 64: uint64_t + */ + signed depth; + + /** + * Whether gamma ramps are supported + */ + enum libgamma_decision supported; + + /** + * The number of stops in the red gamma ramp + */ + size_t red_size; + + /** + * The number of stops in the green gamma ramp + */ + size_t green_size; + + /** + * The number of stops in the blue gamma ramp + */ + size_t blue_size; + + /** + * `.red_size + .green_size + .blue_size` + * multiplied by the byte-size of each stop + */ + size_t ramps_size; + + /** + * The name of the output, will be its EDID + * if available, otherwise it will be the + * index of the partition, followed by a dot + * and the index of the CRTC within the + * partition, or if a name for the connector + * is available: the index of the partition + * followed by a dot and the name of the + * connector + */ + char* restrict name; + + /** + * The libgamma state for the output + */ + libgamma_crtc_state_t* restrict crtc; + + /** + * Saved gamma ramps + */ + union gamma_ramps saved_ramps; + + /** + * The table of all applied filters + */ + struct filter* restrict table_filters; + + /** + * `.table_sums[i]` is the resulting + * adjustment made when all filter + * from `.table_filters[0]` up to and + * including `.table_filters[i]` has + * been applied + */ + union gamma_ramps* restrict table_sums; + + /** + * The number of elements allocated + * for `.table_filters` and for `.table_sums` + */ + size_t table_alloc; + + /** + * The number of elements stored in + * `.table_filters` and in `.table_sums` + */ + size_t table_size; + +}; + + + +/** + * Free all resources allocated to an output. + * The allocation of `output` itself is not freed, + * nor is its the libgamma destroyed. + * + * @param this The output + */ +GCC_ONLY(__attribute__((nonnull))) +void output_destroy(struct output* restrict this); + +/** + * Marshal an output + * + * @param this The output + * @param buf Output buffer for the marshalled output, + * `NULL` just measure how large the buffers + * needs to be + * @return The number of marshalled byte + */ +GCC_ONLY(__attribute__((nonnull(1)))) +size_t output_marshal(const struct output* restrict this, void* restrict buf); + +/** + * Unmarshal an output + * + * @param this Output for the output + * @param buf Buffer with the marshalled output + * @return The number of unmarshalled bytes, 0 on error + */ +GCC_ONLY(__attribute__((nonnull))) +size_t output_unmarshal(struct output* restrict this, const void* restrict buf); + +/** + * Compare to outputs by the names of their respective CRTC:s + * + * @param a Return -1 if this one is lower + * @param b Return +1 if this one is higher + * @return See description of `a` and `b`, + * 0 if returned if they are the same + */ +GCC_ONLY(__attribute__((pure, nonnull))) +int output_cmp_by_name(const void* restrict a, const void* restrict b); + +/** + * Find an output by its name + * + * @param key The name of the output + * @param base The array of outputs + * @param n The number of elements in `base` + * @return Output find in `base`, `NULL` if not found + */ +GCC_ONLY(__attribute__((pure, nonnull))) +struct output* output_find_by_name(const char* restrict key, struct output* restrict base, size_t n); + + +#endif + diff --git a/src/types/ramps.c b/src/types/ramps.c new file mode 100644 index 0000000..30bed3e --- /dev/null +++ b/src/types/ramps.c @@ -0,0 +1,98 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "ramps.h" + +#include + +#include +#include +#include + + + +/** + * The name of the process + */ +extern char* restrict argv0; + + + +/** + * Marshal a ramp trio + * + * @param this The ramps + * @param buf Output buffer for the marshalled ramps, + * `NULL` just measure how large the buffers + * needs to be + * @param ramps_size The byte-size of ramps + * @return The number of marshalled byte + */ +size_t gamma_ramps_marshal(const union gamma_ramps* restrict this, void* restrict buf, size_t ramps_size) +{ + if (buf != NULL) + memcpy(buf, this->u8.red, ramps_size); + return ramps_size; +} + + +/** + * Unmarshal a ramp trio + * + * @param this Output for the ramps, `.red_size`, `.green_size`, + * and `.blue_size` must already be set + * @param buf Buffer with the marshalled ramps + * @param ramps_size The byte-size of ramps + * @return The number of unmarshalled bytes, 0 on error + */ +size_t gamma_ramps_unmarshal(union gamma_ramps* restrict this, const void* restrict buf, size_t ramps_size) +{ + size_t depth = ramps_size / (this->u8.red_size + this->u8.green_size + this->u8.blue_size); + int r = 0; + switch (depth) + { + case 1: + r = libgamma_gamma_ramps8_initialise(&(this->u8)); + break; + case 2: + r = libgamma_gamma_ramps16_initialise(&(this->u16)); + break; + case 4: + r = libgamma_gamma_ramps32_initialise(&(this->u32)); + break; + case 8: + r = libgamma_gamma_ramps64_initialise(&(this->u64)); + break; + default: + if (depth == sizeof(float)) + r = libgamma_gamma_rampsf_initialise(&(this->f)); + else if (depth == sizeof(double)) + r = libgamma_gamma_rampsd_initialise(&(this->d)); + else + abort(); + break; + } + if (r) + { + libgamma_perror(argv0, r); + errno = 0; + return 0; + } + memcpy(this->u8.red, buf, ramps_size); + return ramps_size; +} + diff --git a/src/types/ramps.h b/src/types/ramps.h new file mode 100644 index 0000000..001d504 --- /dev/null +++ b/src/types/ramps.h @@ -0,0 +1,102 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef TYPES_RAMPS_H +#define TYPES_RAMPS_H + + +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Gamma ramps union for all + * lbigamma gamma ramps types + */ +union gamma_ramps +{ + /** + * Ramps with 8-bit value + */ + libgamma_gamma_ramps8_t u8; + + /** + * Ramps with 16-bit value + */ + libgamma_gamma_ramps16_t u16; + + /** + * Ramps with 32-bit value + */ + libgamma_gamma_ramps32_t u32; + + /** + * Ramps with 64-bit value + */ + libgamma_gamma_ramps64_t u64; + + /** + * Ramps with `float` value + */ + libgamma_gamma_rampsf_t f; + + /** + * Ramps with `double` value + */ + libgamma_gamma_rampsd_t d; + +}; + + + +/** + * Marshal a ramp trio + * + * @param this The ramps + * @param buf Output buffer for the marshalled ramps, + * `NULL` just measure how large the buffers + * needs to be + * @param ramps_size The byte-size of ramps + * @return The number of marshalled byte + */ +GCC_ONLY(__attribute__((nonnull(1)))) +size_t gamma_ramps_marshal(const union gamma_ramps* restrict this, void* restrict buf, size_t ramps_size); + +/** + * Unmarshal a ramp trio + * + * @param this Output for the ramps + * @param buf Buffer with the marshalled ramps + * @param ramps_size The byte-size of ramps + * @return The number of unmarshalled bytes, 0 on error + */ +GCC_ONLY(__attribute__((nonnull))) +size_t gamma_ramps_unmarshal(union gamma_ramps* restrict this, const void* restrict buf, size_t ramps_size); + + +#endif + diff --git a/src/types/ring.c b/src/types/ring.c new file mode 100644 index 0000000..13cf8c9 --- /dev/null +++ b/src/types/ring.c @@ -0,0 +1,224 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "ring.h" + +#include +#include + + + +/** + * Initialise a ring buffer + * + * @param this The ring buffer + */ +void ring_initialise(struct ring* restrict this) +{ + this->start = 0; + this->end = 0; + this->size = 0; + this->buffer = NULL; +} + + +/** + * Release resource allocated to a ring buffer + * + * @param this The ring buffer + */ +void ring_destroy(struct ring* restrict this) +{ + free(this->buffer); +} + + + +#if defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wcast-align" +#endif + + +/** + * Marshal a ring buffer + * + * @param this The ring buffer + * @param buf Output buffer for the marshalled data, + * `NULL` to only measure how large buffer + * is needed + * @return The number of marshalled bytes + */ +size_t ring_marshal(const struct ring* restrict this, void* restrict buf) +{ + size_t off = 0, n = this->end - this->start; + char* bs = buf; + + if (bs != NULL) + *(size_t*)(bs + off) = n; + off += sizeof(size_t); + + if (bs != NULL) + memcpy(bs + off, this->buffer + this->start, n); + off += n; + + return off; +} + + +/** + * Unmarshal a ring buffer + * + * @param this Output parameter for the ring buffer + * @param buf Buffer with the marshalled data + * @return The number of unmarshalled bytes, 0 on error + */ +size_t ring_unmarshal(struct ring* restrict this, const void* restrict buf) +{ + size_t off = 0; + const char* bs = buf; + + ring_initialise(this); + + this->size = this->end = *(const size_t*)(bs + off); + off += sizeof(size_t); + + if (this->end > 0) + { + if (!(this->buffer = malloc(this->end))) + return 0; + + memcpy(this->buffer, bs + off, this->end); + off += this->end; + } + + return off; +} + + +#if defined(__clang__) +# pragma GCC diagnostic pop +#endif + + + +/** + * Append data to a ring buffer + * + * @param this The ring buffer + * @param data The new data + * @param n The number of bytes in `data` + * @return Zero on success, -1 on error + */ +int ring_push(struct ring* restrict this, void* restrict data, size_t n) +{ + size_t used = 0; + + if (this->start == this->end) + { + if (this->buffer != NULL) + used = this->size; + } + else if (this->start > this->end) + used = this->size - this->start + this->end; + else + used = this->start - this->end; + + if (used + n > this->size) + { + char* restrict new = malloc(used + n); + if (new == NULL) + return -1; + if (this->buffer) + { + if (this->start < this->end) + memcpy(new, this->buffer + this->start, this->end - this->start); + else + { + memcpy(new, this->buffer + this->start, this->size - this->start); + memcpy(new + this->size - this->start, this->buffer, this->end); + } + } + memcpy(new + used, data, n); + this->buffer = new; + this->start = 0; + this->end = used + n; + this->size = used + n; + } + else if ((this->start >= this->end) || (this->end + n <= this->size)) + { + memcpy(this->buffer + this->end, data, n); + this->end += n; + } + else + { + memcpy(this->buffer + this->end, data, this->size - this->end); + data = (char*)data + (this->size - this->end); + n -= this->size - this->end; + memcpy(this->buffer, data, n); + this->end = n; + } + + return 0; +} + + +/** + * Get queued data from a ring buffer + * + * It can take up to two calls (with `ring_pop` between) + * to get all queued data + * + * @param this The ring buffer + * @param n Output parameter for the length + * of the returned segment + * @return The beginning of the queued data, + * `NULL` if there is nothing more + */ +void* ring_peek(struct ring* restrict this, size_t* restrict n) +{ + if (this->buffer == NULL) + return *n = 0, NULL; + + if (this->start < this->end) + *n = this->end - this->start; + else + *n = this->size - this->start; + return this->buffer + this->start; +} + + +/** + * Dequeue data from a ring bubber + * + * @param this The ring buffer + * @param n The number of bytes to dequeue + */ +void ring_pop(struct ring* restrict this, size_t n) +{ + this->start += n; + this->start %= this->size; + if (this->start == this->end) + { + free(this->buffer); + this->start = 0; + this->end = 0; + this->size = 0; + this->buffer = NULL; + } +} + diff --git a/src/types/ring.h b/src/types/ring.h new file mode 100644 index 0000000..0474f39 --- /dev/null +++ b/src/types/ring.h @@ -0,0 +1,152 @@ +/** + * coopgammad -- Cooperative gamma server + * Copyright (C) 2016 Mattias Andrée (maandree@kth.se) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef TYPES_RING_H +#define TYPES_RING_H + + +#include + + + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + + + +/** + * Ring buffer + */ +struct ring +{ + /** + * Buffer for the data + */ + char* restrict buffer; + + /** + * The first set byte in `.buffer` + */ + size_t start; + + /** + * The last set byte in `.buffer`, plus 1 + */ + size_t end; + + /** + * The size of `.buffer` + */ + size_t size; +}; + + + +/** + * Initialise a ring buffer + * + * @param this The ring buffer + */ +GCC_ONLY(__attribute__((nonnull))) +void ring_initialise(struct ring* restrict this); + +/** + * Release resource allocated to a ring buffer + * + * @param this The ring buffer + */ +GCC_ONLY(__attribute__((nonnull))) +void ring_destroy(struct ring* restrict this); + +/** + * Marshal a ring buffer + * + * @param this The ring buffer + * @param buf Output buffer for the marshalled data, + * `NULL` to only measure how large buffer + * is needed + * @return The number of marshalled bytes + */ +GCC_ONLY(__attribute__((nonnull(1)))) +size_t ring_marshal(const struct ring* restrict this, void* restrict buf); + +/** + * Unmarshal a ring buffer + * + * @param this Output parameter for the ring buffer + * @param buf Buffer with the marshalled data + * @return The number of unmarshalled bytes, 0 on error + */ +GCC_ONLY(__attribute__((nonnull))) +size_t ring_unmarshal(struct ring* restrict this, const void* restrict buf); + +/** + * Append data to a ring buffer + * + * @param this The ring buffer + * @param data The new data + * @param n The number of bytes in `data` + * @return Zero on success, -1 on error + */ +GCC_ONLY(__attribute__((nonnull(1)))) +int ring_push(struct ring* restrict this, void* restrict data, size_t n); + +/** + * Get queued data from a ring buffer + * + * It can take up to two calls (with `ring_pop` between) + * to get all queued data + * + * @param this The ring buffer + * @param n Output parameter for the length + * of the returned segment + * @return The beginning of the queued data, + * `NULL` if there is nothing more + */ +GCC_ONLY(__attribute__((nonnull))) +void* ring_peek(struct ring* restrict this, size_t* restrict n); + +/** + * Dequeue data from a ring bubber + * + * @param this The ring buffer + * @param n The number of bytes to dequeue + */ +GCC_ONLY(__attribute__((nonnull))) +void ring_pop(struct ring* restrict this, size_t n); + +/** + * Check whether there is more data waiting + * in a ring buffer + * + * @param this The ring buffer + * @return 1 if there is more data, 0 otherwise + */ +GCC_ONLY(__attribute__((nonnull))) +static inline int ring_have_more(struct ring* restrict this) +{ + return this->buffer != NULL; +} + + +#endif + diff --git a/src/util.h b/src/util.h index c1f8934..7429f5f 100644 --- a/src/util.h +++ b/src/util.h @@ -15,6 +15,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#ifndef UTIL_H +#define UTIL_H + + #include @@ -97,3 +101,6 @@ void msleep(int ms); GCC_ONLY(__attribute__((pure, nonnull))) int verify_utf8(const char* restrict string, int allow_modified_nul); + +#endif + -- cgit v1.2.3-70-g09d2