From 08a374cf2a6521a045e148af86d5493ce456a15b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 18 Jul 2016 15:08:11 +0200 Subject: Implement reconnect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/coopgammad.c | 26 ++++++------------ src/servers/crtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/servers/crtc.h | 7 +++++ src/state.c | 5 ++++ src/state.h | 5 ++++ 5 files changed, 104 insertions(+), 19 deletions(-) diff --git a/src/coopgammad.c b/src/coopgammad.c index 47efb42..67dc0e4 100644 --- a/src/coopgammad.c +++ b/src/coopgammad.c @@ -277,19 +277,17 @@ static enum init_status daemonise(int keep_stderr) * * @param full Perform a full initialisation, shall be done * iff the state is not going to be unmarshalled - * @param preserve Preserve current gamma ramps at priority 0 * @param foreground Keep process in the foreground * @param keep_stderr Keep stderr open * @param query Was -q used, see `main` for description * @return An `enum init_status` value or an exit value */ -static enum init_status initialise(int full, int preserve, int foreground, int keep_stderr, int query) +static enum init_status initialise(int full, int foreground, int keep_stderr, int query) { struct rlimit rlimit; size_t i, n; sigset_t mask; - char* restrict sitename_dup = NULL; - int s, gerror; + int s; enum init_status r; /* Zero out some memory so it can be destoried safely. */ @@ -324,10 +322,8 @@ static enum init_status initialise(int full, int preserve, int foreground, int k return INIT_SUCCESS; /* Get site */ - if ((sitename != NULL) && !(sitename_dup = memdup(sitename, strlen(sitename) + 1))) + if (initialise_site() < 0) goto fail; - if ((gerror = libgamma_site_initialise(&site, method, sitename_dup))) - goto fail_libgamma; if (full) { @@ -403,9 +399,6 @@ static enum init_status initialise(int full, int preserve, int foreground, int k } return INIT_SUCCESS; - fail_libgamma: - libgamma_perror(argv0, gerror); - errno = 0; fail: return INIT_FAILURE; } @@ -653,11 +646,10 @@ static int print_method_and_site(int query) * * Returns only on failure * - * @param preserve Did -p appear on the comment line? - * @return Pathname of file where the state is stored, - * `NULL` if the state is in tact + * @return Pathname of file where the state is stored, + * `NULL` if the state is in tact */ -static char* reexecute(int preserve) +static char* reexecute(void) { char* statefile = NULL; char* statebuffer = NULL; @@ -773,7 +765,7 @@ static void usage(void) */ int main(int argc, char** argv) { - int rc = 1, preserve = 0, foreground = 0, keep_stderr = 0, query = 0, r; + int rc = 1, foreground = 0, keep_stderr = 0, query = 0, r; char* statefile = NULL; ARGBEGIN @@ -806,7 +798,7 @@ int main(int argc, char** argv) restart: - switch ((r = initialise(statefile == NULL, preserve, foreground, keep_stderr, query))) + switch ((r = initialise(statefile == NULL, foreground, keep_stderr, query))) { case INIT_SUCCESS: break; case INIT_RUNNING: rc = 2; /* fall through */ @@ -837,7 +829,7 @@ int main(int argc, char** argv) if (reexec && !terminate) { - if ((statefile = reexecute(preserve))) + if ((statefile = reexecute())) { perror(argv0); fprintf(stderr, "%s: restoring state without re-executing\n", argv0); diff --git a/src/servers/crtc.c b/src/servers/crtc.c index 96e3a84..5facade 100644 --- a/src/servers/crtc.c +++ b/src/servers/crtc.c @@ -17,8 +17,10 @@ */ #include "crtc.h" #include "gamma.h" +#include "coopgamma.h" #include "../state.h" #include "../communication.h" +#include "../util.h" #include #include @@ -89,6 +91,33 @@ char* get_crtc_name(const libgamma_crtc_information_t* restrict info, } +/** + * Initialise the site + * + * @return Zero on success, -1 on error + */ +int initialise_site(void) +{ + char* restrict sitename_dup = NULL; + int gerror, saved_errno; + + if ((sitename != NULL) && !(sitename_dup = memdup(sitename, strlen(sitename) + 1))) + goto fail; + if ((gerror = libgamma_site_initialise(&site, method, sitename_dup))) + goto fail_libgamma; + + return 0; + fail_libgamma: + libgamma_perror(argv0, gerror); + errno = 0; + fail: + saved_errno = errno; + free(sitename_dup); + errno = saved_errno; + return -1; +} + + /** * Get partitions and CRTC:s * @@ -233,11 +262,58 @@ int disconnect(void) */ int reconnect(void) { + struct output* restrict old_outputs = NULL; + size_t i, old_outputs_n = 0; + int saved_errno; + if (connected) return 0; - connected = 1; + + /* Get site */ + if (initialise_site() < 0) + goto fail; + + /* Get partitions and CRTC:s */ + if (initialise_crtcs() < 0) + goto fail; + + /* Get CRTC information */ + if (outputs_n && !(outputs = calloc(outputs_n, sizeof(*outputs)))) + goto fail; + if (initialise_gamma_info() < 0) + goto fail; + + /* Sort outputs */ + qsort(outputs, outputs_n, sizeof(*outputs), output_cmp_by_name); + + /* Load current gamma ramps */ + store_gamma(); + + /* Preserve current gamma ramps at priority=0 if -p */ + if (preserve && (preserve_gamma() < 0)) + goto fail; + + /* Merge state */ + old_outputs = outputs, outputs = NULL; + old_outputs_n = outputs_n, outputs_n = 0; + if (merge_state(old_outputs, old_outputs_n) < 0) + goto fail; + for (i = 0; i < old_outputs_n; i++) + output_destroy(old_outputs + i); + free(old_outputs), old_outputs = NULL, old_outputs_n = 0; + + /* Reapply gamma ramps */ reapply_gamma(); - return 0; /* TODO reconnect() */ + + return 0; + + fail: + saved_errno = errno; + for (i = 0; i < old_outputs_n; i++) + output_destroy(old_outputs + i); + free(old_outputs); + errno = saved_errno; + return -1; } diff --git a/src/servers/crtc.h b/src/servers/crtc.h index 3bf9eed..68239d4 100644 --- a/src/servers/crtc.h +++ b/src/servers/crtc.h @@ -57,6 +57,13 @@ GCC_ONLY(__attribute__((nonnull))) char* get_crtc_name(const libgamma_crtc_information_t* restrict info, const libgamma_crtc_state_t* restrict crtc); +/** + * Initialise the site + * + * @return Zero on success, -1 on error + */ +int initialise_site(void); + /** * Get partitions and CRTC:s * diff --git a/src/state.c b/src/state.c index 0a0a369..d140789 100644 --- a/src/state.c +++ b/src/state.c @@ -134,6 +134,11 @@ libgamma_partition_state_t* restrict partitions = NULL; /* do not marshal */ */ libgamma_crtc_state_t* restrict crtcs = NULL; /* do not marshal */ +/** + * Preserve gamma ramps at priority 0? + */ +int preserve = 0; /* do not marshal, pass on via command line arguments */ + /** diff --git a/src/state.h b/src/state.h index 0a09023..32a6cb1 100644 --- a/src/state.h +++ b/src/state.h @@ -151,6 +151,11 @@ extern libgamma_partition_state_t* restrict partitions; */ extern libgamma_crtc_state_t* restrict crtcs; +/** + * Preserve gamma ramps at priority 0? + */ +extern int preserve; + /** -- cgit v1.2.3-70-g09d2