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 autosave_hook();
58 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
59 event_loopbreak();
60 return NULL;
61 }
62 tab->fd = -1;
64 TAILQ_INIT(&tab->hist.head);
66 TAILQ_INIT(&tab->buffer.head);
67 TAILQ_INIT(&tab->buffer.page.head);
69 tab->id = tab_new_id();
70 if (!operating)
71 tab->flags |= TAB_LAZY;
72 switch_to_tab(tab);
74 if (after != NULL)
75 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
76 else
77 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
79 load_url_in_tab(tab, url, base, 0);
80 return tab;
81 }
83 /*
84 * Free every resource linked to the tab, including the tab itself.
85 * Removes the tab from the tablist, but doesn't update the
86 * current_tab though.
87 */
88 void
89 free_tab(struct tab *tab)
90 {
91 stop_tab(tab);
93 autosave_hook();
95 if (evtimer_pending(&tab->loadingev, NULL))
96 evtimer_del(&tab->loadingev);
98 TAILQ_REMOVE(&tabshead, tab, tabs);
99 free(tab);
102 void
103 stop_tab(struct tab *tab)
105 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
107 if (tab->fd != -1) {
108 close(tab->fd);
109 tab->fd = -1;
110 free(tab->path);
111 tab->path = NULL;
112 load_page_from_str(tab, "Stopped.\n");
117 void
118 save_session(void)
120 struct tab *tab;
121 char *t;
122 int flags;
124 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
126 TAILQ_FOREACH(tab, &tabshead, tabs) {
127 flags = tab->flags;
128 if (tab == current_tab)
129 flags |= TAB_CURRENT;
131 t = tab->hist_cur->h;
132 ui_send_fs(IMSG_SESSION_TAB, flags, t, strlen(t)+1);
134 t = tab->buffer.page.title;
135 ui_send_fs(IMSG_SESSION_TAB_TITLE, 0, t, strlen(t)+1);
138 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
141 /*
142 * Parse a line of the session file. The format is:
144 * URL [flags,...] [title]\n
145 */
146 static void
147 parse_session_line(char *line, const char **title, uint32_t *flags)
149 char *s, *t, *ap;
151 *title = "";
152 *flags = 0;
153 if ((s = strchr(line, ' ')) == NULL)
154 return;
156 *s++ = '\0';
158 if ((t = strchr(s, ' ')) != NULL) {
159 *t++ = '\0';
160 *title = t;
163 while ((ap = strsep(&s, ",")) != NULL) {
164 if (*ap == '\0')
166 else if (!strcmp(ap, "current"))
167 *flags |= TAB_CURRENT;
168 else
169 message("unknown tab flag: %s", ap);
173 void
174 load_last_session(void)
176 const char *title;
177 char *nl, *line = NULL;
178 uint32_t flags;
179 size_t linesize = 0;
180 ssize_t linelen;
181 FILE *session;
182 struct tab *tab, *curr = NULL;
184 if ((session = fopen(session_file, "r")) == NULL) {
185 /* first time? */
186 new_tab("about:new", NULL, NULL);
187 switch_to_tab(new_tab("about:help", NULL, NULL));
188 return;
191 while ((linelen = getline(&line, &linesize, session)) != -1) {
192 if ((nl = strchr(line, '\n')) != NULL)
193 *nl = '\0';
194 parse_session_line(line, &title, &flags);
195 if ((tab = new_tab(line, NULL, NULL)) == NULL)
196 err(1, "new_tab");
197 strlcpy(tab->buffer.page.title, title,
198 sizeof(tab->buffer.page.title));
199 if (flags & TAB_CURRENT)
200 curr = tab;
203 if (ferror(session))
204 message("error reading %s: %s",
205 session_file, strerror(errno));
206 fclose(session);
207 free(line);
209 if (curr != NULL)
210 switch_to_tab(curr);
212 if (last_time_crashed())
213 switch_to_tab(new_tab("about:crash", NULL, NULL));
215 return;
218 void
219 autosave_init(void)
221 evtimer_set(&autosaveev, autosave_timer, NULL);
224 void
225 autosave_timer(int fd, short event, void *data)
227 save_session();
230 /*
231 * Function to be called in "interesting" places where we may want to
232 * schedule an autosave (like on new tab or before loading an url.)
233 */
234 void
235 autosave_hook(void)
237 struct timeval tv;
239 if (autosave <= 0)
240 return;
242 if (!evtimer_pending(&autosaveev, NULL)) {
243 tv.tv_sec = autosave;
244 tv.tv_usec = 0;
246 evtimer_add(&autosaveev, &tv);