Blob


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