commit c7107cec96c1cb1a0bc0a0fcb5bc341e5464e57e from: Omar Polo date: Thu Apr 01 09:41:10 2021 UTC save session and reload it on startup commit - e49ce157fae23fdb5dac3e093f743b85af2d952f commit + c7107cec96c1cb1a0bc0a0fcb5bc341e5464e57e blob - a2bbcc59aef4fc9f0250f5c2f6635843250074fb blob + ddba9d64c485f3f5ea1b1fcd4dc321eea2624c41 --- fs.c +++ fs.c @@ -37,19 +37,28 @@ static void handle_get(struct imsg*, size_t); static void handle_quit(struct imsg*, size_t); static void handle_bookmark_page(struct imsg*, size_t); static void handle_save_cert(struct imsg*, size_t); +static void handle_session_start(struct imsg*, size_t); +static void handle_session_tab(struct imsg*, size_t); +static void handle_session_end(struct imsg*, size_t); static void handle_dispatch_imsg(int, short, void*); static struct event imsgev; static struct imsgbuf *ibuf; +static FILE *session; + static char bookmark_file[PATH_MAX]; static char known_hosts_file[PATH_MAX]; +static char session_file[PATH_MAX]; static imsg_handlerfn *handlers[] = { [IMSG_GET] = handle_get, [IMSG_QUIT] = handle_quit, [IMSG_BOOKMARK_PAGE] = handle_bookmark_page, [IMSG_SAVE_CERT] = handle_save_cert, + [IMSG_SESSION_START] = handle_session_start, + [IMSG_SESSION_TAB] = handle_session_tab, + [IMSG_SESSION_END] = handle_session_end, }; static void __attribute__((__noreturn__)) @@ -174,6 +183,40 @@ end: } static void +handle_session_start(struct imsg *imsg, size_t datalen) +{ + if (datalen != 0) + die(); + + if ((session = fopen(session_file, "w")) == NULL) + die(); +} + +static void +handle_session_tab(struct imsg *imsg, size_t datalen) +{ + if (session == NULL) + die(); + + if (datalen == 0) + die(); + + /* skip the NUL-terminator */ + fwrite(imsg->data, 1, datalen-1, session); + + fprintf(session, "\n"); +} + +static void +handle_session_end(struct imsg *imsg, size_t datalen) +{ + if (session == NULL) + die(); + fclose(session); + session = NULL; +} + +static void handle_dispatch_imsg(int fd, short ev, void *d) { struct imsgbuf *ibuf = d; @@ -195,6 +238,9 @@ fs_init(void) strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file)); strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file)); + strlcpy(session_file, getenv("HOME"), sizeof(session_file)); + strlcat(session_file, "/.telescope/session", sizeof(session_file)); + return 1; } @@ -276,3 +322,28 @@ load_certs(struct ohash *h) free(line); return ferror(f); } + +int +load_last_session(void (*cb)(const char*)) +{ + char *nl, *line = NULL; + int e; + size_t linesize = 0; + ssize_t linelen; + FILE *session; + + if ((session = fopen(session_file, "r")) == NULL) + return 0; + + while ((linelen = getline(&line, &linesize, session)) != -1) { + if ((nl = strchr(line, '\n')) != NULL) + *nl = '\0'; + cb(line); + } + + free(line); + e = ferror(session); + fclose(session); + + return !e; +} blob - bdbb0df70436547074fb284a20ded7414f1f21bb blob + 4df53b9b15288038037f8c6d5a78591df9712e09 --- telescope.c +++ telescope.c @@ -424,6 +424,24 @@ void add_to_bookmarks(const char *str) { imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1); + imsg_flush(fsibuf); +} + +void +save_session(void) +{ + struct tab *tab; + + imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0); + imsg_flush(fsibuf); + + TAILQ_FOREACH(tab, &tabshead, tabs) { + imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1, + tab->hist_cur->h, strlen(tab->hist_cur->h)+1); + imsg_flush(fsibuf); + } + + imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0); imsg_flush(fsibuf); } blob - 50a7cc2dee9be23766864d7cf244a65e06b7363f blob + f655701a0218a729095cb36427b2c787a3dda7b6 --- telescope.h +++ telescope.h @@ -47,6 +47,10 @@ enum imsg_type { IMSG_BOOKMARK_OK, IMSG_SAVE_CERT, IMSG_SAVE_CERT_OK, + + IMSG_SESSION_START, + IMSG_SESSION_TAB, + IMSG_SESSION_END, }; enum line_type { @@ -190,6 +194,7 @@ struct keymap { int fs_init(void); int fs_main(struct imsgbuf*); int load_certs(struct ohash*); +int load_last_session(void(*)(const char*)); /* gemini.c */ int client_main(struct imsgbuf*); @@ -241,6 +246,7 @@ int load_previous_page(struct tab*); int load_next_page(struct tab*); void stop_tab(struct tab*); void add_to_bookmarks(const char*); +void save_session(void); /* textplain.c */ void textplain_initparser(struct parser*); blob - 05ffdd75e9e4b6cd6c5c3808ca18c7f721fcc6b4 blob + c0554c6ea45fbf875d2f309a526aec8be7868885 --- ui.c +++ ui.c @@ -147,6 +147,7 @@ static void enter_minibuffer(void(*)(void), void(*)( static void exit_minibuffer(void); static void switch_to_tab(struct tab*); static struct tab *new_tab(const char*); +static void session_new_tab_cb(const char*); static void usage(void); static struct { short meta; int key; uint32_t cp; } thiskey; @@ -590,6 +591,7 @@ cmd_end_of_buffer(struct window *window) static void cmd_kill_telescope(struct window *window) { + save_session(); event_loopbreak(); } @@ -1892,6 +1894,12 @@ new_tab(const char *url) load_url_in_tab(tab, url); return tab; +} + +static void +session_new_tab_cb(const char *url) +{ + new_tab(url); } static void @@ -1976,7 +1984,9 @@ ui_init(int argc, char * const *argv) signal_set(&winchev, SIGWINCH, handle_resize, NULL); signal_add(&winchev, NULL); - new_tab(url); + load_last_session(session_new_tab_cb); + if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead)) + new_tab(url); return 1; }