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 }
65 TAILQ_INIT(&tab->hist.head);
67 TAILQ_INIT(&tab->buffer.head);
68 TAILQ_INIT(&tab->buffer.page.head);
70 tab->id = tab_new_id();
71 if (!operating)
72 tab->flags |= TAB_LAZY;
73 switch_to_tab(tab);
75 if (after != NULL)
76 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
77 else
78 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
80 load_url_in_tab(tab, url, base, 0);
81 return tab;
82 }
84 /*
85 * Free every resource linked to the tab, including the tab itself.
86 * Removes the tab from the tablist, but doesn't update the
87 * current_tab though.
88 */
89 void
90 free_tab(struct tab *tab)
91 {
92 stop_tab(tab);
93 ui_schedule_redraw();
94 autosave_hook();
96 if (evtimer_pending(&tab->loadingev, NULL))
97 evtimer_del(&tab->loadingev);
99 TAILQ_REMOVE(&tabshead, tab, tabs);
100 free(tab);
103 void
104 stop_tab(struct tab *tab)
106 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
109 void
110 save_session(void)
112 struct tab *tab;
113 char *t;
114 int flags;
116 if (safe_mode)
117 return;
119 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
121 TAILQ_FOREACH(tab, &tabshead, tabs) {
122 flags = tab->flags;
123 if (tab == current_tab)
124 flags |= TAB_CURRENT;
126 t = tab->hist_cur->h;
127 ui_send_fs(IMSG_SESSION_TAB, flags, t, strlen(t)+1);
129 t = tab->buffer.page.title;
130 ui_send_fs(IMSG_SESSION_TAB_TITLE, 0, t, strlen(t)+1);
133 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
136 /*
137 * Parse a line of the session file. The format is:
139 * URL [flags,...] [title]\n
140 */
141 static void
142 parse_session_line(char *line, const char **title, uint32_t *flags)
144 char *s, *t, *ap;
146 *title = "";
147 *flags = 0;
148 if ((s = strchr(line, ' ')) == NULL)
149 return;
151 *s++ = '\0';
153 if ((t = strchr(s, ' ')) != NULL) {
154 *t++ = '\0';
155 *title = t;
158 while ((ap = strsep(&s, ",")) != NULL) {
159 if (*ap == '\0')
161 else if (!strcmp(ap, "current"))
162 *flags |= TAB_CURRENT;
163 else
164 message("unknown tab flag: %s", ap);
168 void
169 load_last_session(void)
171 const char *title;
172 char *nl, *line = NULL;
173 uint32_t flags;
174 size_t linesize = 0;
175 ssize_t linelen;
176 FILE *session;
177 struct tab *tab, *curr = NULL;
179 if ((session = fopen(session_file, "r")) == NULL) {
180 /* first time? */
181 new_tab("about:new", NULL, NULL);
182 switch_to_tab(new_tab("about:help", NULL, NULL));
183 return;
186 while ((linelen = getline(&line, &linesize, session)) != -1) {
187 if ((nl = strchr(line, '\n')) != NULL)
188 *nl = '\0';
189 parse_session_line(line, &title, &flags);
190 if ((tab = new_tab(line, NULL, NULL)) == NULL)
191 err(1, "new_tab");
192 strlcpy(tab->buffer.page.title, title,
193 sizeof(tab->buffer.page.title));
194 if (flags & TAB_CURRENT)
195 curr = tab;
198 if (ferror(session))
199 message("error reading %s: %s",
200 session_file, strerror(errno));
201 fclose(session);
202 free(line);
204 if (curr != NULL)
205 switch_to_tab(curr);
207 if (last_time_crashed())
208 switch_to_tab(new_tab("about:crash", NULL, NULL));
210 return;
213 void
214 autosave_init(void)
216 evtimer_set(&autosaveev, autosave_timer, NULL);
219 void
220 autosave_timer(int fd, short event, void *data)
222 save_session();
225 /*
226 * Function to be called in "interesting" places where we may want to
227 * schedule an autosave (like on new tab or before loading an url.)
228 */
229 void
230 autosave_hook(void)
232 struct timeval tv;
234 if (autosave <= 0)
235 return;
237 if (!evtimer_pending(&autosaveev, NULL)) {
238 tv.tv_sec = autosave;
239 tv.tv_usec = 0;
241 evtimer_add(&autosaveev, &tv);