2 1fce2e75 2021-08-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 1fce2e75 2021-08-14 op * Permission to use, copy, modify, and distribute this software for any
5 1fce2e75 2021-08-14 op * purpose with or without fee is hereby granted, provided that the above
6 1fce2e75 2021-08-14 op * copyright notice and this permission notice appear in all copies.
8 1fce2e75 2021-08-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1fce2e75 2021-08-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1fce2e75 2021-08-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1fce2e75 2021-08-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1fce2e75 2021-08-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1fce2e75 2021-08-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1fce2e75 2021-08-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1fce2e75 2021-08-14 op #include "compat.h"
19 1fce2e75 2021-08-14 op #include <errno.h>
20 1fce2e75 2021-08-14 op #include <stdio.h>
21 1fce2e75 2021-08-14 op #include <stdlib.h>
22 1fce2e75 2021-08-14 op #include <string.h>
23 1fce2e75 2021-08-14 op #include <unistd.h>
25 1fce2e75 2021-08-14 op #include "defaults.h"
26 b9fcc0e9 2021-10-08 op #include "fs.h"
27 1fce2e75 2021-08-14 op #include "minibuffer.h"
28 1fce2e75 2021-08-14 op #include "parser.h"
29 1fce2e75 2021-08-14 op #include "session.h"
30 1fce2e75 2021-08-14 op #include "ui.h"
32 1fce2e75 2021-08-14 op static struct event autosaveev;
35 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
37 1fce2e75 2021-08-14 op current_tab = tab;
38 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
40 1fce2e75 2021-08-14 op if (operating && tab->flags & TAB_LAZY)
41 1fce2e75 2021-08-14 op load_url_in_tab(tab, tab->hist_cur->h, NULL, 0);
45 1fce2e75 2021-08-14 op tab_new_id(void)
47 1fce2e75 2021-08-14 op static uint32_t tab_counter;
49 1fce2e75 2021-08-14 op return tab_counter++;
53 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
55 1fce2e75 2021-08-14 op struct tab *tab;
57 87aeb47b 2021-08-18 op ui_schedule_redraw();
58 1fce2e75 2021-08-14 op autosave_hook();
60 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
61 1fce2e75 2021-08-14 op event_loopbreak();
66 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->hist.head);
68 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
69 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.page.head);
71 1fce2e75 2021-08-14 op tab->id = tab_new_id();
72 1fce2e75 2021-08-14 op if (!operating)
73 1fce2e75 2021-08-14 op tab->flags |= TAB_LAZY;
74 1fce2e75 2021-08-14 op switch_to_tab(tab);
76 1fce2e75 2021-08-14 op if (after != NULL)
77 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
79 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
81 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
86 1fce2e75 2021-08-14 op * Free every resource linked to the tab, including the tab itself.
87 1fce2e75 2021-08-14 op * Removes the tab from the tablist, but doesn't update the
88 1fce2e75 2021-08-14 op * current_tab though.
91 1fce2e75 2021-08-14 op free_tab(struct tab *tab)
93 1fce2e75 2021-08-14 op stop_tab(tab);
94 87aeb47b 2021-08-18 op ui_schedule_redraw();
95 1fce2e75 2021-08-14 op autosave_hook();
97 1fce2e75 2021-08-14 op if (evtimer_pending(&tab->loadingev, NULL))
98 1fce2e75 2021-08-14 op evtimer_del(&tab->loadingev);
100 1fce2e75 2021-08-14 op TAILQ_REMOVE(&tabshead, tab, tabs);
105 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
107 1fce2e75 2021-08-14 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
109 1fce2e75 2021-08-14 op if (tab->fd != -1) {
110 1fce2e75 2021-08-14 op close(tab->fd);
111 1fce2e75 2021-08-14 op tab->fd = -1;
112 1fce2e75 2021-08-14 op free(tab->path);
113 1fce2e75 2021-08-14 op tab->path = NULL;
114 1fce2e75 2021-08-14 op load_page_from_str(tab, "Stopped.\n");
119 1fce2e75 2021-08-14 op save_session(void)
121 1fce2e75 2021-08-14 op struct tab *tab;
125 d0971653 2021-09-15 op if (safe_mode)
128 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
130 1fce2e75 2021-08-14 op TAILQ_FOREACH(tab, &tabshead, tabs) {
131 1fce2e75 2021-08-14 op flags = tab->flags;
132 1fce2e75 2021-08-14 op if (tab == current_tab)
133 1fce2e75 2021-08-14 op flags |= TAB_CURRENT;
135 1fce2e75 2021-08-14 op t = tab->hist_cur->h;
136 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_TAB, flags, t, strlen(t)+1);
138 1fce2e75 2021-08-14 op t = tab->buffer.page.title;
139 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_TAB_TITLE, 0, t, strlen(t)+1);
142 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
146 1fce2e75 2021-08-14 op * Parse a line of the session file. The format is:
148 1fce2e75 2021-08-14 op * URL [flags,...] [title]\n
151 1fce2e75 2021-08-14 op parse_session_line(char *line, const char **title, uint32_t *flags)
153 1fce2e75 2021-08-14 op char *s, *t, *ap;
157 1fce2e75 2021-08-14 op if ((s = strchr(line, ' ')) == NULL)
162 95a8c791 2021-08-26 op if ((t = strchr(s, ' ')) != NULL) {
167 1fce2e75 2021-08-14 op while ((ap = strsep(&s, ",")) != NULL) {
168 1fce2e75 2021-08-14 op if (*ap == '\0')
170 1fce2e75 2021-08-14 op else if (!strcmp(ap, "current"))
171 1fce2e75 2021-08-14 op *flags |= TAB_CURRENT;
173 1fce2e75 2021-08-14 op message("unknown tab flag: %s", ap);
178 1fce2e75 2021-08-14 op load_last_session(void)
180 1fce2e75 2021-08-14 op const char *title;
181 1fce2e75 2021-08-14 op char *nl, *line = NULL;
182 1fce2e75 2021-08-14 op uint32_t flags;
183 1fce2e75 2021-08-14 op size_t linesize = 0;
184 1fce2e75 2021-08-14 op ssize_t linelen;
185 1fce2e75 2021-08-14 op FILE *session;
186 1fce2e75 2021-08-14 op struct tab *tab, *curr = NULL;
188 1fce2e75 2021-08-14 op if ((session = fopen(session_file, "r")) == NULL) {
189 1fce2e75 2021-08-14 op /* first time? */
190 1fce2e75 2021-08-14 op new_tab("about:new", NULL, NULL);
191 1fce2e75 2021-08-14 op switch_to_tab(new_tab("about:help", NULL, NULL));
195 1fce2e75 2021-08-14 op while ((linelen = getline(&line, &linesize, session)) != -1) {
196 95a8c791 2021-08-26 op if ((nl = strchr(line, '\n')) != NULL)
198 1fce2e75 2021-08-14 op parse_session_line(line, &title, &flags);
199 1fce2e75 2021-08-14 op if ((tab = new_tab(line, NULL, NULL)) == NULL)
200 1fce2e75 2021-08-14 op err(1, "new_tab");
201 95a8c791 2021-08-26 op strlcpy(tab->buffer.page.title, title,
202 1fce2e75 2021-08-14 op sizeof(tab->buffer.page.title));
203 1fce2e75 2021-08-14 op if (flags & TAB_CURRENT)
207 1fce2e75 2021-08-14 op if (ferror(session))
208 1fce2e75 2021-08-14 op message("error reading %s: %s",
209 1fce2e75 2021-08-14 op session_file, strerror(errno));
210 1fce2e75 2021-08-14 op fclose(session);
213 1fce2e75 2021-08-14 op if (curr != NULL)
214 1fce2e75 2021-08-14 op switch_to_tab(curr);
216 1fce2e75 2021-08-14 op if (last_time_crashed())
217 1fce2e75 2021-08-14 op switch_to_tab(new_tab("about:crash", NULL, NULL));
223 1fce2e75 2021-08-14 op autosave_init(void)
225 1fce2e75 2021-08-14 op evtimer_set(&autosaveev, autosave_timer, NULL);
229 1fce2e75 2021-08-14 op autosave_timer(int fd, short event, void *data)
231 1fce2e75 2021-08-14 op save_session();
235 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
236 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
239 1fce2e75 2021-08-14 op autosave_hook(void)
241 1fce2e75 2021-08-14 op struct timeval tv;
243 1fce2e75 2021-08-14 op if (autosave <= 0)
246 1fce2e75 2021-08-14 op if (!evtimer_pending(&autosaveev, NULL)) {
247 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
248 1fce2e75 2021-08-14 op tv.tv_usec = 0;
250 1fce2e75 2021-08-14 op evtimer_add(&autosaveev, &tv);