commit ecfbb1d42fbd289d9c48e25806b7b377bf93acd2 from: Omar Polo date: Sat Aug 14 19:34:10 2021 UTC add autosave timer for the session This is achieved by calling `autosave_hook' in interesting places, like new_tab or free_tab. It'll set up a timer to later persist the session. This is particularly useful to avoid loosing tabs on the event of a crash or unexpected system halt, or other similar event. commit - 5ffaef7f71a51fd298f3f4138e382f06b05e6543 commit + ecfbb1d42fbd289d9c48e25806b7b377bf93acd2 blob - ab2dca1d89b75270959d211cc8b908fce094c895 blob + d9482f85fce0082d15897fadc13ceef873eefc08 --- defaults.c +++ defaults.c @@ -28,6 +28,7 @@ char *new_tab_url = NULL; +int autosave = 20; int dont_wrap_pre = 0; int emojify_link = 1; int enable_colors = 1; @@ -490,6 +491,8 @@ config_setvari(const char *var, int val) emojify_link = !!val; } else if (!strcmp(var, "set-title")) { set_title = !!val; + } else if (!strcmp(var, "autosave")) { + autosave = val; } else { return 0; } blob - 8f9809f8b6c30ab5eac20e52cd6961288a4af83c blob + 3bb7bee9fa10b99fdc77684477d6c89353acc874 --- defaults.h +++ defaults.h @@ -19,6 +19,7 @@ extern char *new_tab_url; +extern int autosave; extern int dont_wrap_pre; extern int emojify_link; extern int enable_colors; blob - 0d715ad096cce9e10ddbd2e2eaab3528974e72ea blob + 9aae5b14987ea75fa46c4e7429b368fad0180a0d --- telescope.c +++ telescope.c @@ -43,6 +43,8 @@ static struct option longopts[] = { static const char *opts = "Cc:hnT:v"; static struct imsgev *iev_fs, *iev_net; + +static struct event autosaveev; struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead); struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies); @@ -113,6 +115,7 @@ static int make_request(struct tab *, struct get_req const char *); static int make_fs_request(struct tab *, int, const char *); static int do_load_url(struct tab*, const char *, const char *); +static void autosave_timer(int, short, void *); static void parse_session_line(char *, const char **, uint32_t *); static void load_last_session(void); static pid_t start_child(enum telescope_process, const char *, int); @@ -927,6 +930,8 @@ void free_tab(struct tab *tab) { stop_tab(tab); + + autosave_hook(); if (evtimer_pending(&tab->loadingev, NULL)) evtimer_del(&tab->loadingev); @@ -978,6 +983,32 @@ save_session(void) } ui_send_fs(IMSG_SESSION_END, 0, NULL, 0); +} + +static void +autosave_timer(int fd, short event, void *data) +{ + save_session(); +} + +/* + * Function to be called in "interesting" places where we may want to + * schedule an autosave (like on new tab or before loading an url.) + */ +void +autosave_hook(void) +{ + struct timeval tv; + + if (autosave <= 0) + return; + + if (!evtimer_pending(&autosaveev, NULL)) { + tv.tv_sec = autosave; + tv.tv_usec = 0; + + evtimer_add(&autosaveev, &tv); + } } /* @@ -1236,6 +1267,9 @@ main(int argc, char * const *argv) event_init(); + /* Setup event handler for the autosave */ + evtimer_set(&autosaveev, autosave_timer, NULL); + /* Setup event handlers for pipes to fs/net */ iev_fs->events = EV_READ; event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events, blob - 7a2377bbac51378de25faa9e84af9711308db2e4 blob + 756a7092d2927d064b0bb4d658654e5f5159672b --- telescope.h +++ telescope.h @@ -328,6 +328,7 @@ void free_tab(struct tab *); void stop_tab(struct tab*); void add_to_bookmarks(const char*); void save_session(void); +void autosave_hook(void); /* tofu.c */ void tofu_init(struct ohash*, unsigned int, ptrdiff_t); blob - 5b7da6d1130b55dc57f37640fa9daa167fbbef5a blob + 8276391fde6c4cacf478669e857d4cfd80ffbc29 --- ui.c +++ ui.c @@ -956,6 +956,8 @@ load_url_in_tab(struct tab *tab, const char *url, cons load_url(tab, url, base, nohist); return; } + + autosave_hook(); message("Loading %s...", url); start_loading_anim(tab); @@ -984,6 +986,8 @@ struct tab * new_tab(const char *url, const char *base, struct tab *after) { struct tab *tab; + + autosave_hook(); if ((tab = calloc(1, sizeof(*tab))) == NULL) { event_loopbreak();