From 60b04df6fb50f67e3b2f9796f645de0942c4281e Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Sun, 28 Feb 2021 10:54:31 +0100 Subject: [PATCH] Fix for #202. To be tested. --- TODO.md | 2 +- src/commons.h | 10 +++++----- src/conf/config.c | 35 ++++++++++++++++++++--------------- src/conf/opts.c | 34 +++++++++++++++------------------- src/modules/backlight.c | 3 +++ src/modules/wizard.c | 2 +- src/utils/log.c | 6 +++--- 7 files changed, 48 insertions(+), 44 deletions(-) diff --git a/TODO.md b/TODO.md index 2260f652..fc3b5bcd 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,4 @@ -## 4.4 +## 4.5 ### Generic - [ ] Port to libmodule 6.0.0 (?) diff --git a/src/commons.h b/src/commons.h index ed657098..8840e93e 100644 --- a/src/commons.h +++ b/src/commons.h @@ -30,16 +30,16 @@ typedef struct { int num_captures[SIZE_AC]; - char dev_name[PATH_MAX + 1]; - char dev_opts[NAME_MAX + 1]; - double regression_points[SIZE_AC][MAX_SIZE_POINTS]; // points used for regression through libgsl - int num_points[SIZE_AC]; // number of points currently used for polynomial regression + char *dev_name; + char *dev_opts; + double regression_points[SIZE_AC][MAX_SIZE_POINTS]; // points used for regression through libgsl + int num_points[SIZE_AC]; // number of points currently used for polynomial regression } sensor_conf_t; typedef struct { int disabled; int timeout[SIZE_AC][SIZE_STATES + 1]; - char screen_path[PATH_MAX + 1]; // screen syspath (eg: /sys/class/backlight/intel_backlight) + char *screen_path; // screen syspath (eg: /sys/class/backlight/intel_backlight) int no_smooth; // disable smooth backlight changes for BACKLIGHT module double trans_step; // every backlight transition step value (in pct), used when smooth BACKLIGHT transitions are enabled int trans_timeout; // every backlight transition timeout value, used when smooth BACKLIGHT transitions are enabled diff --git a/src/conf/config.c b/src/conf/config.c index f7b99299..297e3a3d 100644 --- a/src/conf/config.c +++ b/src/conf/config.c @@ -51,8 +51,8 @@ static void load_backlight_settings(config_t *cfg, bl_conf_t *bl_conf) { config_setting_lookup_int(bl, "trans_timeout", &bl_conf->trans_timeout); config_setting_lookup_float(bl, "shutter_threshold", &bl_conf->shutter_threshold); config_setting_lookup_bool(bl, "no_auto_calibration", &bl_conf->no_auto_calib); - if (config_setting_lookup_string(bl, "screen_sysname", &screendev) == CONFIG_TRUE) { - strncpy(bl_conf->screen_path, screendev, sizeof(bl_conf->screen_path) - 1); + if (config_setting_lookup_string(bl, "screen_sysname", &screendev) == CONFIG_TRUE && strlen(screendev)) { + bl_conf->screen_path = strdup(screendev); } config_setting_lookup_bool(bl, "pause_on_lid_closed", &bl_conf->pause_on_lid_closed); config_setting_lookup_bool(bl, "capture_on_lid_opened", &bl_conf->capture_on_lid_opened); @@ -88,12 +88,12 @@ static void load_sensor_settings(config_t *cfg, sensor_conf_t *sens_conf) { if (sens_group) { const char *sensor_dev, *sensor_settings; - if (config_setting_lookup_string(sens_group, "devname", &sensor_dev) == CONFIG_TRUE) { - strncpy(sens_conf->dev_name, sensor_dev, sizeof(sens_conf->dev_name) - 1); + if (config_setting_lookup_string(sens_group, "devname", &sensor_dev) == CONFIG_TRUE && strlen(sensor_dev)) { + sens_conf->dev_name = strdup(sensor_dev); } - if (config_setting_lookup_string(sens_group, "settings", &sensor_settings) == CONFIG_TRUE) { - strncpy(sens_conf->dev_opts, sensor_settings, sizeof(sens_conf->dev_opts) - 1); + if (config_setting_lookup_string(sens_group, "settings", &sensor_settings) == CONFIG_TRUE && strlen(sensor_settings)) { + sens_conf->dev_opts = strdup(sensor_settings); } config_setting_t *captures, *points; @@ -347,7 +347,6 @@ static void store_backlight_settings(config_t *cfg, bl_conf_t *bl_conf) { setting = config_setting_add(bl, "trans_timeout", CONFIG_TYPE_INT); config_setting_set_int(setting, bl_conf->trans_timeout); - setting = config_setting_add(bl, "no_auto_calibration", CONFIG_TYPE_BOOL); config_setting_set_bool(setting, bl_conf->no_auto_calib); @@ -357,8 +356,10 @@ static void store_backlight_settings(config_t *cfg, bl_conf_t *bl_conf) { setting = config_setting_add(bl, "capture_on_lid_opened", CONFIG_TYPE_BOOL); config_setting_set_bool(setting, bl_conf->capture_on_lid_opened); - setting = config_setting_add(bl, "screen_sysname", CONFIG_TYPE_STRING); - config_setting_set_string(setting, bl_conf->screen_path); + if (bl_conf->screen_path) { + setting = config_setting_add(bl, "screen_sysname", CONFIG_TYPE_STRING); + config_setting_set_string(setting, bl_conf->screen_path); + } setting = config_setting_add(bl, "shutter_threshold", CONFIG_TYPE_FLOAT); config_setting_set_float(setting, bl_conf->shutter_threshold); @@ -380,12 +381,16 @@ static void store_sensors_settings(config_t *cfg, sensor_conf_t *sens_conf) { for (int i = 0; i < SIZE_AC; i++) { config_setting_set_int_elem(setting, -1, sens_conf->num_captures[i]); } - - setting = config_setting_add(sensor, "devname", CONFIG_TYPE_STRING); - config_setting_set_string(setting, sens_conf->dev_name); - - setting = config_setting_add(sensor, "settings", CONFIG_TYPE_STRING); - config_setting_set_string(setting, sens_conf->dev_opts); + + if (sens_conf->dev_name) { + setting = config_setting_add(sensor, "devname", CONFIG_TYPE_STRING); + config_setting_set_string(setting, sens_conf->dev_name); + } + + if (sens_conf->dev_opts) { + setting = config_setting_add(sensor, "settings", CONFIG_TYPE_STRING); + config_setting_set_string(setting, sens_conf->dev_opts); + } /* -1 here below means append to end of array */ setting = config_setting_add(sensor, "ac_regression_points", CONFIG_TYPE_ARRAY); diff --git a/src/conf/opts.c b/src/conf/opts.c index c332f365..ba110e8c 100644 --- a/src/conf/opts.c +++ b/src/conf/opts.c @@ -143,9 +143,9 @@ void init_opts(int argc, char *argv[]) { static void parse_cmd(int argc, char *const argv[], char *conf_file, size_t size) { poptContext pc; const struct poptOption po[] = { - {"frames", 'f', POPT_ARG_INT, NULL, 7, "Frames taken for each capture, Between 1 and 20", NULL}, - {"device", 'd', POPT_ARG_STRING, NULL, 1, "Path to sensor device. If empty, first matching device is used", "video0"}, - {"backlight", 'b', POPT_ARG_STRING, NULL, 2, "Path to internal backlight syspath. If empty, first matching device is used", "intel_backlight"}, + {"frames", 'f', POPT_ARG_INT, NULL, 5, "Frames taken for each capture, Between 1 and 20", NULL}, + {"device", 'd', POPT_ARG_STRING, &conf.sens_conf.dev_name, 100, "Path to sensor device. If empty, first matching device is used", "video0"}, + {"backlight", 'b', POPT_ARG_STRING, &conf.bl_conf.screen_path, 100, "Path to internal backlight syspath. If empty, first matching device is used", "intel_backlight"}, {"no-backlight-smooth", 0, POPT_ARG_NONE, &conf.bl_conf.no_smooth, 100, "Disable smooth backlight transitions", NULL}, {"no-gamma-smooth", 0, POPT_ARG_NONE, &conf.gamma_conf.no_smooth, 100, "Disable smooth gamma transitions", NULL}, {"no-dimmer-smooth-enter", 0, POPT_ARG_NONE, &conf.dim_conf.no_smooth[ENTER], 100, "Disable smooth dimmer transitions while entering dimmed state", NULL}, @@ -154,8 +154,8 @@ static void parse_cmd(int argc, char *const argv[], char *conf_file, size_t size {"night-temp", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &conf.gamma_conf.temp[NIGHT], 100, "Nightly gamma temperature, between 1000 and 10000", NULL}, {"lat", 0, POPT_ARG_DOUBLE, &conf.day_conf.loc.lat, 100, "Your desired latitude", NULL}, {"lon", 0, POPT_ARG_DOUBLE, &conf.day_conf.loc.lon, 100, "Your desired longitude", NULL}, - {"sunrise", 0, POPT_ARG_STRING, NULL, 3, "Force sunrise time for gamma correction", "07:00"}, - {"sunset", 0, POPT_ARG_STRING, NULL, 4, "Force sunset time for gamma correction", "19:00"}, + {"sunrise", 0, POPT_ARG_STRING, NULL, 1, "Force sunrise time for gamma correction", "07:00"}, + {"sunset", 0, POPT_ARG_STRING, NULL, 2, "Force sunset time for gamma correction", "19:00"}, {"no-gamma", 0, POPT_ARG_NONE, &conf.gamma_conf.disabled, 100, "Disable gamma correction tool", NULL}, {"no-dimmer", 0, POPT_ARG_NONE, &conf.dim_conf.disabled, 100, "Disable dimmer tool", NULL}, {"no-dpms", 0, POPT_ARG_NONE, &conf.dpms_conf.disabled, 100, "Disable dpms tool", NULL}, @@ -166,8 +166,8 @@ static void parse_cmd(int argc, char *const argv[], char *conf_file, size_t size {"verbose", 0, POPT_ARG_NONE, &conf.verbose, 100, "Enable verbose mode", NULL}, {"no-auto-calib", 0, POPT_ARG_NONE, &conf.bl_conf.no_auto_calib, 100, "Disable screen backlight automatic calibration", NULL}, {"shutter-thres", 0, POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &conf.bl_conf.shutter_threshold, 100, "Threshold to consider a capture as clogged", NULL}, - {"version", 'v', POPT_ARG_NONE, NULL, 5, "Show version info", NULL}, - {"conf-file", 'c', POPT_ARG_STRING, NULL, 6, "Specify a conf file to be parsed", NULL}, + {"version", 'v', POPT_ARG_NONE, NULL, 3, "Show version info", NULL}, + {"conf-file", 'c', POPT_ARG_STRING, NULL, 4, "Specify a conf file to be parsed", NULL}, {"gamma-long-transition", 0, POPT_ARG_NONE, &conf.gamma_conf.long_transition, 100, "Enable a very long smooth transition for gamma (redshift-like)", NULL }, {"ambient-gamma", 0, POPT_ARG_NONE, &conf.gamma_conf.ambient_gamma, 100, "Enable screen temperature matching ambient brightness instead of time based.", NULL }, {"wizard", 'w', POPT_ARG_NONE, &conf.wizard, 100, "Enable wizard mode.", NULL}, @@ -179,36 +179,32 @@ static void parse_cmd(int argc, char *const argv[], char *conf_file, size_t size int rc; while ((rc = poptGetNextOpt(pc)) > 0) { char *str = poptGetOptArg(pc); + bool must_free = true; switch (rc) { case 1: - strncpy(conf.sens_conf.dev_name, str, sizeof(conf.sens_conf.dev_name) - 1); - break; - case 2: - strncpy(conf.bl_conf.screen_path, str, sizeof(conf.bl_conf.screen_path) - 1); - break; - case 3: strncpy(conf.day_conf.day_events[SUNRISE], str, sizeof(conf.day_conf.day_events[SUNRISE]) - 1); break; - case 4: + case 2: strncpy(conf.day_conf.day_events[SUNSET], str, sizeof(conf.day_conf.day_events[SUNSET]) - 1); break; - case 5: + case 3: printf("%s: C daemon utility to automagically adjust screen backlight to match ambient brightness.\n" "* Current version: %s\n" "* https://github.com/FedeDP/Clight\n" - "* Copyright (C) 2020 Federico Di Pierro \n", argv[0], VERSION); + "* Copyright (C) 2021 Federico Di Pierro \n" + "* For more info, see man clight.1.\n", argv[0], VERSION); exit(EXIT_SUCCESS); - case 6: + case 4: strncpy(conf_file, str, size); break; - case 7: + case 5: conf.sens_conf.num_captures[ON_AC] = atoi(str); conf.sens_conf.num_captures[ON_BATTERY] = atoi(str); break; default: break; } - if (str) { + if (str && must_free) { free(str); } } diff --git a/src/modules/backlight.c b/src/modules/backlight.c index ca7a577d..744102a2 100644 --- a/src/modules/backlight.c +++ b/src/modules/backlight.c @@ -79,6 +79,9 @@ static void destroy(void) { close(bl_fd); } free(backlight_interface); + free(conf.bl_conf.screen_path); + free(conf.sens_conf.dev_name); + free(conf.sens_conf.dev_opts); } static void receive_waiting_init(const msg_t *const msg, UNUSED const void* userdata) { diff --git a/src/modules/wizard.c b/src/modules/wizard.c index c42cc5a7..c895fb4b 100644 --- a/src/modules/wizard.c +++ b/src/modules/wizard.c @@ -135,7 +135,7 @@ static void receive_calibrating(const msg_t *const msg, UNUSED const void* userd switch (c) { case 10: if (get_backlight() != 0) { - WARN("Failed to get '%s' backlight pct.\n", conf.bl_conf.screen_path); + WARN("Failed to get '%s' backlight pct.\n", conf.bl_conf.screen_path ? conf.bl_conf.screen_path : ""); modules_quit(-1); } else { INFO("Backlight level is: %.3lf\n\n", bls[capture_idx - 1]); diff --git a/src/utils/log.c b/src/utils/log.c index d5448bed..4a7e00ad 100644 --- a/src/utils/log.c +++ b/src/utils/log.c @@ -49,7 +49,7 @@ static void log_bl_conf(bl_conf_t *bl_conf) { fprintf(log_file, "* Daily timeouts:\t\tAC %d\tBATT %d\n", bl_conf->timeout[ON_AC][DAY], bl_conf->timeout[ON_BATTERY][DAY]); fprintf(log_file, "* Nightly timeout:\t\tAC %d\tBATT %d\n", bl_conf->timeout[ON_AC][NIGHT], bl_conf->timeout[ON_BATTERY][NIGHT]); fprintf(log_file, "* Event timeouts:\t\tAC %d\tBATT %d\n", bl_conf->timeout[ON_AC][SIZE_STATES], bl_conf->timeout[ON_BATTERY][SIZE_STATES]); - fprintf(log_file, "* Backlight path:\t\t%s\n", strlen(bl_conf->screen_path) ? bl_conf->screen_path : "Unset"); + fprintf(log_file, "* Backlight path:\t\t%s\n", bl_conf->screen_path ? bl_conf->screen_path : "Unset"); fprintf(log_file, "* Shutter threshold:\t\t%.2lf\n", bl_conf->shutter_threshold); fprintf(log_file, "* Autocalibration:\t\t%s\n", bl_conf->no_auto_calib ? "Disabled" : "Enabled"); fprintf(log_file, "* Pause on lid closed:\t\t%s\n", bl_conf->pause_on_lid_closed ? "Enabled" : "Disabled"); @@ -59,8 +59,8 @@ static void log_bl_conf(bl_conf_t *bl_conf) { static void log_sens_conf(sensor_conf_t *sens_conf) { fprintf(log_file, "\n### SENSOR ###\n"); fprintf(log_file, "* Captures:\t\tAC %d\tBATT %d\n", sens_conf->num_captures[ON_AC], sens_conf->num_captures[ON_BATTERY]); - fprintf(log_file, "* Device:\t\t%s\n", strlen(sens_conf->dev_name) ? sens_conf->dev_name : "Unset"); - fprintf(log_file, "* Settings:\t\t%s\n", strlen(sens_conf->dev_opts) ? sens_conf->dev_opts : "Unset"); + fprintf(log_file, "* Device:\t\t%s\n", sens_conf->dev_name ? sens_conf->dev_name : "Unset"); + fprintf(log_file, "* Settings:\t\t%s\n", sens_conf->dev_opts ? sens_conf->dev_opts : "Unset"); } static void log_kbd_conf(kbd_conf_t *kbd_conf) {