diff --git a/CHANGELOG.md b/CHANGELOG.md index 41fd0ca..4cff215 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - feat: Add config option to customise bullet character (#17) - fix: Use actual screen DPI value to compute sizes and spaces +- feat: Allow shutting down the device on inactivity (defaults to 5 minutes) ## 0.2.0 (2022-05-27) diff --git a/config.c b/config.c index 11c9bbc..6d5a188 100644 --- a/config.c +++ b/config.c @@ -22,7 +22,10 @@ #include "log.h" +#include "lvgl/lvgl.h" + #include +#include #include "squeek2lvgl/sq2lv.h" @@ -74,6 +77,7 @@ static bool parse_bool(const char *value, bool *result); static void init_opts(ul_config_opts *opts) { opts->general.animations = false; opts->general.backend = ul_backends_backends[0] == NULL ? UL_BACKENDS_BACKEND_NONE : 0; + opts->general.timeout = 0; opts->keyboard.autohide = true; opts->keyboard.layout_id = SQ2LV_LAYOUT_US; opts->keyboard.popovers = false; @@ -103,6 +107,10 @@ static int parsing_handler(void* user_data, const char* section, const char* key opts->general.backend = id; return 1; } + } else if (strcmp(key, "timeout") == 0) { + /* Use a max ceiling of 60 minutes (3600 secs) */ + opts->general.timeout = (uint16_t)LV_MIN(strtoul(value, (char **)NULL, 10), 3600); + return 1; } } else if (strcmp(section, "keyboard") == 0) { if (strcmp(key, "autohide") == 0) { diff --git a/config.h b/config.h index 689cbf3..d7bb336 100644 --- a/config.h +++ b/config.h @@ -28,6 +28,7 @@ #include "sq2lv_layouts.h" #include +#include /** * General options @@ -37,6 +38,8 @@ typedef struct { ul_backends_backend_id_t backend; /* If true, use animations */ bool animations; + /* Timeout (in seconds) - once elapsed, the device will shutdown. 0 (default) to disable */ + uint16_t timeout; } ul_config_opts_general; /** diff --git a/main.c b/main.c index f6f1679..886676f 100644 --- a/main.c +++ b/main.c @@ -182,6 +182,11 @@ static void textarea_ready_cb(lv_event_t *event); */ static void print_password_and_exit(lv_obj_t *textarea); +/** + * Shuts down the device. + */ +static void shutdown(void); + /** * Handle termination signals sent to the process. * @@ -278,8 +283,7 @@ static void shutdown_btn_clicked_cb(lv_event_t *event) { static void shutdown_mbox_value_changed_cb(lv_event_t *event) { lv_obj_t *mbox = lv_event_get_current_target(event); if (lv_msgbox_get_active_btn(mbox) == 0) { - sync(); - reboot(RB_POWER_OFF); + shutdown(); } lv_msgbox_close(mbox); } @@ -313,6 +317,11 @@ static void print_password_and_exit(lv_obj_t *textarea) { sigaction_handler(SIGTERM); } +static void shutdown(void) { + sync(); + reboot(RB_POWER_OFF); +} + static void sigaction_handler(int signum) { LV_UNUSED(signum); ul_terminal_reset_current_terminal(); @@ -558,8 +567,13 @@ int main(int argc, char *argv[]) { } /* Run lvgl in "tickless" mode */ + uint32_t timeout = conf_opts.general.timeout * 1000; /* ms */ while(1) { - lv_task_handler(); + if (!timeout || lv_disp_get_inactive_time(NULL) < timeout) { + lv_task_handler(); + } else if (timeout) { + shutdown(); + } usleep(5000); }