Skip to content

Commit

Permalink
Added options to restore screen backlight and temperature upon clight…
Browse files Browse the repository at this point in the history
… exit. Fixes #213.
  • Loading branch information
FedeDP committed Jul 14, 2021
1 parent 47cb17d commit ee220be
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 31 deletions.
6 changes: 6 additions & 0 deletions Extra/clight.conf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ backlight:
{
## Uncomment to disable
# disabled = true;

## Uncomment to restore screen backlight on exit
# restore_on_exit = true;

## Uncomment to disable smooth transitions
# no_smooth_transition = true;
Expand Down Expand Up @@ -187,6 +190,9 @@ gamma:
{
## Uncomment to disable gamma tool
# disabled = true;

## Uncomment to restore screen temperature on exit
# restore_on_exit = true;

## Uncomment to disable gamma smooth transitions
# no_smooth_transition = true;
Expand Down
8 changes: 7 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
- [x] Implement StoreCurveConf
- [x] Fixed StoreCurveConf

- [x] Fix bug in lid state initialization
- [x] Allow to automatically restore screen backlight upon clight exit

### Inhibit
- [x] Add back inhibit_bl conf value
- [x] Add a conf opt to disable inhibit module

### Upower
- [x] Fix bug in lid state initialization

### Conf
- [x] Properly dump "disabled" bool in StoreConf for each module

### Gamma
- [x] Allow to automatically restore screen temperature upon clight exit

## 5.x
- [ ] Port to libmodule 6.0.0 (?)
- [ ] Add a Dump dbus method (and a DUMP_REQ request) to allow any module to dump their state (module_dump()) to a txt file
2 changes: 2 additions & 0 deletions src/commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct {
double shutter_threshold; // capture values below this threshold will be considered "shuttered"
int pause_on_lid_closed; // whether clight should inhibit autocalibration on lid closed
int capture_on_lid_opened; // whether to trigger a new capture whenever lid gets opened
int restore; // whether backlight should be restored on Clight exit
} bl_conf_t;

typedef struct {
Expand All @@ -71,6 +72,7 @@ typedef struct {
int trans_timeout; // every gamma transition timeout value, used when smooth GAMMA transitions are enabled
int long_transition; // flag to enable a very long smooth transition for gamma (redshift-like)
int ambient_gamma; // enable gamma adjustments based on ambient backlight
int restore; // whether gamma should be restored on Clight exit
} gamma_conf_t;

typedef struct {
Expand Down
8 changes: 8 additions & 0 deletions src/conf/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static void load_backlight_settings(config_t *cfg, bl_conf_t *bl_conf) {
if (bl) {
const char *screendev;
config_setting_lookup_bool(bl, "disabled", &bl_conf->disabled);
config_setting_lookup_bool(bl, "restore_on_exit", &bl_conf->restore);
config_setting_lookup_bool(bl, "no_smooth_transition", &bl_conf->no_smooth);
config_setting_lookup_float(bl, "trans_step", &bl_conf->trans_step);
config_setting_lookup_int(bl, "trans_timeout", &bl_conf->trans_timeout);
Expand Down Expand Up @@ -225,6 +226,7 @@ static void load_gamma_settings(config_t *cfg, gamma_conf_t *gamma_conf) {
config_setting_t *gamma = config_lookup(cfg, "gamma");
if (gamma) {
config_setting_lookup_bool(gamma, "disabled", &gamma_conf->disabled);
config_setting_lookup_bool(gamma, "restore_on_exit", &gamma_conf->restore);
config_setting_lookup_bool(gamma, "no_smooth_transition", &gamma_conf->no_smooth);
config_setting_lookup_int(gamma, "trans_step", &gamma_conf->trans_step);
config_setting_lookup_int(gamma, "trans_timeout", &gamma_conf->trans_timeout);
Expand Down Expand Up @@ -407,6 +409,9 @@ static void store_backlight_settings(config_t *cfg, bl_conf_t *bl_conf) {
config_setting_t *setting = config_setting_add(bl, "disabled", CONFIG_TYPE_BOOL);
config_setting_set_bool(setting, bl_conf->disabled);

setting = config_setting_add(bl, "restore_on_exit", CONFIG_TYPE_BOOL);
config_setting_set_bool(setting, bl_conf->restore);

setting = config_setting_add(bl, "no_smooth_transition", CONFIG_TYPE_BOOL);
config_setting_set_bool(setting, bl_conf->no_smooth);

Expand Down Expand Up @@ -521,6 +526,9 @@ static void store_gamma_settings(config_t *cfg, gamma_conf_t *gamma_conf) {
config_setting_t *setting = config_setting_add(gamma, "disabled", CONFIG_TYPE_BOOL);
config_setting_set_bool(setting, gamma_conf->disabled);

setting = config_setting_add(gamma, "restore_on_exit", CONFIG_TYPE_BOOL);
config_setting_set_bool(setting, gamma_conf->restore);

setting = config_setting_add(gamma, "no_smooth_transition", CONFIG_TYPE_BOOL);
config_setting_set_bool(setting, gamma_conf->no_smooth);

Expand Down
88 changes: 59 additions & 29 deletions src/modules/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static double get_value_from_curve(const double perc, curve_t *curve);
static double set_new_backlight(const double perc);
static void publish_bl_upd(const double pct, const bool is_smooth, const double step, const int timeout);
static int get_and_set_each_brightness(const double pct, const bool is_smooth, const double step, const int timeout);
static void set_each_brightness(sd_bus_message *m, double pct, const bool is_smooth, const double step, const int timeout);
static void set_backlight_level(const double pct, const bool is_smooth, const double step, const int timeout);
static int capture_frames_brightness(void);
static void upower_callback(void);
Expand All @@ -30,6 +31,7 @@ static void on_lid_update(void);
static void pause_mod(enum mod_pause type);
static void resume_mod(enum mod_pause type);

static sd_bus_message *restore_m = NULL;
static int bl_fd = -1;
static sd_bus_slot *sens_slot, *bl_slot;
static char *backlight_interface;
Expand Down Expand Up @@ -62,6 +64,10 @@ static void init(void) {
M_SUB(BL_REQ);
M_SUB(INHIBIT_UPD);
m_become(waiting_init);

// Store current backlight to later restore them if requested
SYSBUS_ARG_REPLY(args, parse_bus_reply, &restore_m, CLIGHTD_SERVICE, "/org/clightd/clightd/Backlight", "org.clightd.clightd.Backlight", "GetAll");
call(&args, "s", conf.bl_conf.screen_path);
}

static bool check(void) {
Expand All @@ -82,6 +88,9 @@ static void destroy(void) {
if (bl_fd >= 0) {
close(bl_fd);
}
if (restore_m) {
sd_bus_message_unrefp(&restore_m);
}
free(backlight_interface);
free(conf.bl_conf.screen_path);
free(conf.sens_conf.dev_name);
Expand Down Expand Up @@ -221,6 +230,12 @@ static void receive(const msg_t *const msg, UNUSED const void* userdata) {
}
break;
}
case SYSTEM_UPD:
if (msg->ps_msg->type == LOOP_STOPPED && restore_m && conf.bl_conf.restore) {
set_each_brightness(restore_m, 0, false, 0, 0);
restore_m = NULL;
}
break;
default:
break;
}
Expand Down Expand Up @@ -286,6 +301,12 @@ static void receive_paused(const msg_t *const msg, UNUSED const void* userdata)
}
break;
}
case SYSTEM_UPD:
if (msg->ps_msg->type == LOOP_STOPPED && restore_m && conf.bl_conf.restore) {
set_each_brightness(restore_m, 0, false, 0, 0);
restore_m = NULL;
}
break;
default:
break;
}
Expand Down Expand Up @@ -413,47 +434,56 @@ static void publish_bl_upd(const double pct, const bool is_smooth, const double
* otherwise just sets requested backlight level (just like a SetAll)
*/
static int get_and_set_each_brightness(const double pct, const bool is_smooth, const double step, const int timeout) {
sensor_conf_t *sens_conf = &conf.sens_conf;
enum ac_states st = state.ac_state;

/* Get all monitors backlight; we just need to map each monitor to its specific curve indeed */
// TODO: when clightd will use different object paths for each monitor, just enumerate paths here!
sd_bus_message *m = NULL;
SYSBUS_ARG_REPLY(args, parse_bus_reply, &m, CLIGHTD_SERVICE, "/org/clightd/clightd/Backlight", "org.clightd.clightd.Backlight", "GetAll");
int r = call(&args, "s", conf.bl_conf.screen_path);
if (r >= 0) {
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sd)");
while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sd") >= 0) && r >= 0) {
const char *mon_id = NULL;
set_each_brightness(m, pct, is_smooth, step, timeout);
}
return r;
}

static void set_each_brightness(sd_bus_message *m, double pct, const bool is_smooth, const double step, const int timeout) {
const bool restoring = pct == 0;
sensor_conf_t *sens_conf = &conf.sens_conf;
enum ac_states st = state.ac_state;

int r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sd)");
while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sd") >= 0) && r >= 0) {
const char *mon_id = NULL;
if (!restoring) {
r = sd_bus_message_read(m, "sd", &mon_id, NULL);
sd_bus_message_exit_container(m);
} else {
r = sd_bus_message_read(m, "sd", &mon_id, &pct);
}
sd_bus_message_exit_container(m);

if (r >= 0 && mon_id) {
int ok = true;
if (r >= 0 && mon_id) {
int ok = true;

/* Set backlight on monitor id */
SYSBUS_ARG_REPLY(args, parse_bus_reply, &ok, CLIGHTD_SERVICE, "/org/clightd/clightd/Backlight", "org.clightd.clightd.Backlight", "Set");
curve_t *c = map_get(sens_conf->specific_curves, mon_id);
if (c) {
/* Use monitor specific adjustment, properly scaling bl pct */
int num_points = c[st].num_points;
const double real_pct = get_value_from_curve(pct * (num_points - 1), &c[st]);
DEBUG("Using specific curve for '%s': setting %.3lf pct.\n", mon_id, real_pct);
r = call(&args, "d(bdu)s", real_pct, is_smooth, step, timeout, mon_id);
} else {
DEBUG("Using default curve for '%s'\n", mon_id);
/* Use non-adjusted (default) curve value */
r = call(&args, "d(bdu)s", pct, is_smooth, step, timeout, mon_id);
}
if (!ok) {
r = -1;
}
/* Set backlight on monitor id */
SYSBUS_ARG_REPLY(args, parse_bus_reply, &ok, CLIGHTD_SERVICE, "/org/clightd/clightd/Backlight", "org.clightd.clightd.Backlight", "Set");
curve_t *c = map_get(sens_conf->specific_curves, mon_id);
if (c && !restoring) {
/* Use monitor specific adjustment, properly scaling bl pct */
int num_points = c[st].num_points;
const double real_pct = get_value_from_curve(pct * (num_points - 1), &c[st]);
DEBUG("Using specific curve for '%s': setting %.3lf pct.\n", mon_id, real_pct);
r = call(&args, "d(bdu)s", real_pct, is_smooth, step, timeout, mon_id);
} else {
DEBUG("Using default curve for '%s'\n", mon_id);
/* Use non-adjusted (default) curve value */
r = call(&args, "d(bdu)s", pct, is_smooth, step, timeout, mon_id);
}
if (!ok) {
r = -1;
}
}
sd_bus_message_exit_container(m);
sd_bus_message_unref(m);
}
return r;
sd_bus_message_exit_container(m);
sd_bus_message_unref(m);
}

static void set_backlight_level(const double pct, const bool is_smooth, const double step, const int timeout) {
Expand Down
15 changes: 14 additions & 1 deletion src/modules/gamma.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static void interface_callback(temp_upd *req);
static int on_temp_changed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
static void pause_mod(bool pause, enum mod_pause reason);

static int initial_temp;
static sd_bus_slot *slot;
static bool long_transitioning, should_sync_temp;
static const self_t *daytime_ref;
Expand All @@ -31,6 +32,10 @@ static void init(void) {
M_SUB(NEXT_DAYEVT_UPD);
M_SUB(SUSPEND_UPD);
m_become(waiting_daytime);

// Store current temperature to later restore it if requested
SYSBUS_ARG_REPLY(args, parse_bus_reply, &initial_temp, CLIGHTD_SERVICE, "/org/clightd/clightd/Gamma", "org.clightd.clightd.Gamma", "Get");
call(&args, "ss", fetch_display(), fetch_env());
}

static bool check(void) {
Expand Down Expand Up @@ -102,6 +107,11 @@ static void receive(const msg_t *const msg, UNUSED const void* userdata) {
case SUSPEND_UPD:
pause_mod(state.suspended, SUSPEND);
break;
case SYSTEM_UPD:
if (msg->ps_msg->type == LOOP_STOPPED && initial_temp && conf.gamma_conf.restore) {
set_temp(initial_temp, NULL, false, 0, 0);
}
break;
default:
break;
}
Expand Down Expand Up @@ -145,7 +155,10 @@ static void publish_temp_upd(int temp, int smooth, int step, int timeout) {
}

static int parse_bus_reply(sd_bus_message *reply, const char *member, void *userdata) {
return sd_bus_message_read(reply, "b", userdata);
if (!strcmp(member, "Get")) {
return sd_bus_message_read(reply, "i", userdata);
}
return sd_bus_message_read(reply, "b", userdata);
}

static void set_temp(int temp, const time_t *now, int smooth, int step, int timeout) {
Expand Down
2 changes: 2 additions & 0 deletions src/modules/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ static const sd_bus_vtable conf_bl_vtable[] = {
SD_BUS_WRITABLE_PROPERTY("BattDayTimeout", "i", NULL, set_timeouts, offsetof(bl_conf_t, timeout[ON_BATTERY][DAY]), 0),
SD_BUS_WRITABLE_PROPERTY("BattNightTimeout", "i", NULL, set_timeouts, offsetof(bl_conf_t, timeout[ON_BATTERY][NIGHT]), 0),
SD_BUS_WRITABLE_PROPERTY("BattEventTimeout", "i", NULL, set_timeouts, offsetof(bl_conf_t, timeout[ON_BATTERY][IN_EVENT]), 0),
SD_BUS_WRITABLE_PROPERTY("RestoreOnExit", "b", NULL, NULL, offsetof(bl_conf_t, restore), 0),
SD_BUS_VTABLE_END
};

Expand Down Expand Up @@ -155,6 +156,7 @@ static const sd_bus_vtable conf_gamma_vtable[] = {
SD_BUS_WRITABLE_PROPERTY("DayTemp", "i", NULL, set_gamma, offsetof(gamma_conf_t, temp[DAY]), 0),
SD_BUS_WRITABLE_PROPERTY("NightTemp", "i", NULL, set_gamma, offsetof(gamma_conf_t, temp[NIGHT]), 0),
SD_BUS_WRITABLE_PROPERTY("LongTransition", "b", NULL, NULL, offsetof(gamma_conf_t, long_transition), 0),
SD_BUS_WRITABLE_PROPERTY("RestoreOnExit", "b", NULL, NULL, offsetof(gamma_conf_t, restore), 0),
SD_BUS_VTABLE_END
};

Expand Down
2 changes: 2 additions & 0 deletions src/utils/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static void log_bl_conf(bl_conf_t *bl_conf) {
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");
fprintf(log_file, "* Capture on lid opened:\t\t%s\n", bl_conf->capture_on_lid_opened ? "Enabled" : "Disabled");
fprintf(log_file, "* Restore On Exit:\t\t%s\n", bl_conf->restore ? "Enabled" : "Disabled");
}

static void log_sens_conf(sensor_conf_t *sens_conf) {
Expand All @@ -79,6 +80,7 @@ static void log_gamma_conf(gamma_conf_t *gamma_conf) {
fprintf(log_file, "* Nightly screen temp:\t\t%d\n", gamma_conf->temp[NIGHT]);
fprintf(log_file, "* Long transition:\t\t%s\n", gamma_conf->long_transition ? "Enabled" : "Disabled");
fprintf(log_file, "* Ambient gamma:\t\t%s\n", gamma_conf->ambient_gamma ? "Enabled" : "Disabled");
fprintf(log_file, "* Restore On Exit:\t\t%s\n", gamma_conf->restore ? "Enabled" : "Disabled");
}

static void log_daytime_conf(daytime_conf_t *day_conf) {
Expand Down

0 comments on commit ee220be

Please sign in to comment.