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 "minibuffer.h"
27 #include "parser.h"
28 #include "session.h"
29 #include "ui.h"
31 static struct event autosaveev;
33 void
34 switch_to_tab(struct tab *tab)
35 {
36 current_tab = tab;
37 tab->flags &= ~TAB_URGENT;
39 if (operating && tab->flags & TAB_LAZY)
40 load_url_in_tab(tab, tab->hist_cur->h, NULL, 0);
41 }
43 unsigned int
44 tab_new_id(void)
45 {
46 static uint32_t tab_counter;
48 return tab_counter++;
49 }
51 struct tab *
52 new_tab(const char *url, const char *base, struct tab *after)
53 {
54 struct tab *tab;
56 ui_schedule_redraw();
57 autosave_hook();
59 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
60 event_loopbreak();
61 return NULL;
62 }
63 tab->fd = -1;
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);
108 if (tab->fd != -1) {
109 close(tab->fd);
110 tab->fd = -1;
111 free(tab->path);
112 tab->path = NULL;
113 load_page_from_str(tab, "Stopped.\n");
117 void
118 save_session(void)
120 struct tab *tab;
121 char *t;
122 int flags;
124 if (safe_mode)
125 return;
127 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
129 TAILQ_FOREACH(tab, &tabshead, tabs) {
130 flags = tab->flags;
131 if (tab == current_tab)
132 flags |= TAB_CURRENT;
134 t = tab->hist_cur->h;
135 ui_send_fs(IMSG_SESSION_TAB, flags, t, strlen(t)+1);
137 t = tab->buffer.page.title;
138 ui_send_fs(IMSG_SESSION_TAB_TITLE, 0, t, strlen(t)+1);
141 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
144 /*
145 * Parse a line of the session file. The format is:
147 * URL [flags,...] [title]\n
148 */
149 static void
150 parse_session_line(char *line, const char **title, uint32_t *flags)
152 char *s, *t, *ap;
154 *title = "";
155 *flags = 0;
156 if ((s = strchr(line, ' ')) == NULL)
157 return;
159 *s++ = '\0';
161 if ((t = strchr(s, ' ')) != NULL) {
162 *t++ = '\0';
163 *title = t;
166 while ((ap = strsep(&s, ",")) != NULL) {
167 if (*ap == '\0')
169 else if (!strcmp(ap, "current"))
170 *flags |= TAB_CURRENT;
171 else
172 message("unknown tab flag: %s", ap);
176 void
177 load_last_session(void)
179 const char *title;
180 char *nl, *line = NULL;
181 uint32_t flags;
182 size_t linesize = 0;
183 ssize_t linelen;
184 FILE *session;
185 struct tab *tab, *curr = NULL;
187 if ((session = fopen(session_file, "r")) == NULL) {
188 /* first time? */
189 new_tab("about:new", NULL, NULL);
190 switch_to_tab(new_tab("about:help", NULL, NULL));
191 return;
194 while ((linelen = getline(&line, &linesize, session)) != -1) {
195 if ((nl = strchr(line, '\n')) != NULL)
196 *nl = '\0';
197 parse_session_line(line, &title, &flags);
198 if ((tab = new_tab(line, NULL, NULL)) == NULL)
199 err(1, "new_tab");
200 strlcpy(tab->buffer.page.title, title,
201 sizeof(tab->buffer.page.title));
202 if (flags & TAB_CURRENT)
203 curr = tab;
206 if (ferror(session))
207 message("error reading %s: %s",
208 session_file, strerror(errno));
209 fclose(session);
210 free(line);
212 if (curr != NULL)
213 switch_to_tab(curr);
215 if (last_time_crashed())
216 switch_to_tab(new_tab("about:crash", NULL, NULL));
218 return;
221 void
222 autosave_init(void)
224 evtimer_set(&autosaveev, autosave_timer, NULL);
227 void
228 autosave_timer(int fd, short event, void *data)
230 save_session();
233 /*
234 * Function to be called in "interesting" places where we may want to
235 * schedule an autosave (like on new tab or before loading an url.)
236 */
237 void
238 autosave_hook(void)
240 struct timeval tv;
242 if (autosave <= 0)
243 return;
245 if (!evtimer_pending(&autosaveev, NULL)) {
246 tv.tv_sec = autosave;
247 tv.tv_usec = 0;
249 evtimer_add(&autosaveev, &tv);