Blame


1 1fce2e75 2021-08-14 op /*
2 1fce2e75 2021-08-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 1fce2e75 2021-08-14 op *
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.
7 1fce2e75 2021-08-14 op *
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.
15 1fce2e75 2021-08-14 op */
16 1fce2e75 2021-08-14 op
17 1fce2e75 2021-08-14 op #include "compat.h"
18 1fce2e75 2021-08-14 op
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>
24 1fce2e75 2021-08-14 op
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"
31 1fce2e75 2021-08-14 op
32 1fce2e75 2021-08-14 op static struct event autosaveev;
33 1fce2e75 2021-08-14 op
34 1fce2e75 2021-08-14 op void
35 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
36 1fce2e75 2021-08-14 op {
37 1fce2e75 2021-08-14 op current_tab = tab;
38 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
39 1fce2e75 2021-08-14 op
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);
42 1fce2e75 2021-08-14 op }
43 1fce2e75 2021-08-14 op
44 1fce2e75 2021-08-14 op unsigned int
45 1fce2e75 2021-08-14 op tab_new_id(void)
46 1fce2e75 2021-08-14 op {
47 1fce2e75 2021-08-14 op static uint32_t tab_counter;
48 1fce2e75 2021-08-14 op
49 1fce2e75 2021-08-14 op return tab_counter++;
50 1fce2e75 2021-08-14 op }
51 1fce2e75 2021-08-14 op
52 1fce2e75 2021-08-14 op struct tab *
53 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
54 1fce2e75 2021-08-14 op {
55 1fce2e75 2021-08-14 op struct tab *tab;
56 1fce2e75 2021-08-14 op
57 87aeb47b 2021-08-18 op ui_schedule_redraw();
58 1fce2e75 2021-08-14 op autosave_hook();
59 1fce2e75 2021-08-14 op
60 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
61 1fce2e75 2021-08-14 op event_loopbreak();
62 1fce2e75 2021-08-14 op return NULL;
63 1fce2e75 2021-08-14 op }
64 1fce2e75 2021-08-14 op tab->fd = -1;
65 1fce2e75 2021-08-14 op
66 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->hist.head);
67 1fce2e75 2021-08-14 op
68 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
69 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.page.head);
70 1fce2e75 2021-08-14 op
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);
75 1fce2e75 2021-08-14 op
76 1fce2e75 2021-08-14 op if (after != NULL)
77 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
78 1fce2e75 2021-08-14 op else
79 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
80 1fce2e75 2021-08-14 op
81 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
82 1fce2e75 2021-08-14 op return tab;
83 1fce2e75 2021-08-14 op }
84 1fce2e75 2021-08-14 op
85 1fce2e75 2021-08-14 op /*
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.
89 1fce2e75 2021-08-14 op */
90 1fce2e75 2021-08-14 op void
91 1fce2e75 2021-08-14 op free_tab(struct tab *tab)
92 1fce2e75 2021-08-14 op {
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();
96 1fce2e75 2021-08-14 op
97 1fce2e75 2021-08-14 op if (evtimer_pending(&tab->loadingev, NULL))
98 1fce2e75 2021-08-14 op evtimer_del(&tab->loadingev);
99 1fce2e75 2021-08-14 op
100 1fce2e75 2021-08-14 op TAILQ_REMOVE(&tabshead, tab, tabs);
101 1fce2e75 2021-08-14 op free(tab);
102 1fce2e75 2021-08-14 op }
103 1fce2e75 2021-08-14 op
104 1fce2e75 2021-08-14 op void
105 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
106 1fce2e75 2021-08-14 op {
107 1fce2e75 2021-08-14 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
108 1fce2e75 2021-08-14 op
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");
115 1fce2e75 2021-08-14 op }
116 1fce2e75 2021-08-14 op }
117 1fce2e75 2021-08-14 op
118 1fce2e75 2021-08-14 op void
119 1fce2e75 2021-08-14 op save_session(void)
120 1fce2e75 2021-08-14 op {
121 1fce2e75 2021-08-14 op struct tab *tab;
122 1fce2e75 2021-08-14 op char *t;
123 1fce2e75 2021-08-14 op int flags;
124 1fce2e75 2021-08-14 op
125 d0971653 2021-09-15 op if (safe_mode)
126 d0971653 2021-09-15 op return;
127 d0971653 2021-09-15 op
128 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
129 1fce2e75 2021-08-14 op
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;
134 1fce2e75 2021-08-14 op
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);
137 1fce2e75 2021-08-14 op
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);
140 1fce2e75 2021-08-14 op }
141 1fce2e75 2021-08-14 op
142 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
143 1fce2e75 2021-08-14 op }
144 1fce2e75 2021-08-14 op
145 1fce2e75 2021-08-14 op /*
146 1fce2e75 2021-08-14 op * Parse a line of the session file. The format is:
147 1fce2e75 2021-08-14 op *
148 1fce2e75 2021-08-14 op * URL [flags,...] [title]\n
149 1fce2e75 2021-08-14 op */
150 1fce2e75 2021-08-14 op static void
151 1fce2e75 2021-08-14 op parse_session_line(char *line, const char **title, uint32_t *flags)
152 1fce2e75 2021-08-14 op {
153 1fce2e75 2021-08-14 op char *s, *t, *ap;
154 1fce2e75 2021-08-14 op
155 1fce2e75 2021-08-14 op *title = "";
156 1fce2e75 2021-08-14 op *flags = 0;
157 1fce2e75 2021-08-14 op if ((s = strchr(line, ' ')) == NULL)
158 1fce2e75 2021-08-14 op return;
159 1fce2e75 2021-08-14 op
160 1fce2e75 2021-08-14 op *s++ = '\0';
161 1fce2e75 2021-08-14 op
162 95a8c791 2021-08-26 op if ((t = strchr(s, ' ')) != NULL) {
163 1fce2e75 2021-08-14 op *t++ = '\0';
164 1fce2e75 2021-08-14 op *title = t;
165 1fce2e75 2021-08-14 op }
166 1fce2e75 2021-08-14 op
167 1fce2e75 2021-08-14 op while ((ap = strsep(&s, ",")) != NULL) {
168 1fce2e75 2021-08-14 op if (*ap == '\0')
169 1fce2e75 2021-08-14 op ;
170 1fce2e75 2021-08-14 op else if (!strcmp(ap, "current"))
171 1fce2e75 2021-08-14 op *flags |= TAB_CURRENT;
172 1fce2e75 2021-08-14 op else
173 1fce2e75 2021-08-14 op message("unknown tab flag: %s", ap);
174 1fce2e75 2021-08-14 op }
175 1fce2e75 2021-08-14 op }
176 1fce2e75 2021-08-14 op
177 1fce2e75 2021-08-14 op void
178 1fce2e75 2021-08-14 op load_last_session(void)
179 1fce2e75 2021-08-14 op {
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;
187 1fce2e75 2021-08-14 op
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));
192 1fce2e75 2021-08-14 op return;
193 1fce2e75 2021-08-14 op }
194 1fce2e75 2021-08-14 op
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)
197 1fce2e75 2021-08-14 op *nl = '\0';
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)
204 1fce2e75 2021-08-14 op curr = tab;
205 1fce2e75 2021-08-14 op }
206 1fce2e75 2021-08-14 op
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);
211 1fce2e75 2021-08-14 op free(line);
212 1fce2e75 2021-08-14 op
213 1fce2e75 2021-08-14 op if (curr != NULL)
214 1fce2e75 2021-08-14 op switch_to_tab(curr);
215 1fce2e75 2021-08-14 op
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));
218 1fce2e75 2021-08-14 op
219 1fce2e75 2021-08-14 op return;
220 1fce2e75 2021-08-14 op }
221 1fce2e75 2021-08-14 op
222 1fce2e75 2021-08-14 op void
223 1fce2e75 2021-08-14 op autosave_init(void)
224 1fce2e75 2021-08-14 op {
225 1fce2e75 2021-08-14 op evtimer_set(&autosaveev, autosave_timer, NULL);
226 1fce2e75 2021-08-14 op }
227 1fce2e75 2021-08-14 op
228 1fce2e75 2021-08-14 op void
229 1fce2e75 2021-08-14 op autosave_timer(int fd, short event, void *data)
230 1fce2e75 2021-08-14 op {
231 1fce2e75 2021-08-14 op save_session();
232 1fce2e75 2021-08-14 op }
233 1fce2e75 2021-08-14 op
234 1fce2e75 2021-08-14 op /*
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.)
237 1fce2e75 2021-08-14 op */
238 1fce2e75 2021-08-14 op void
239 1fce2e75 2021-08-14 op autosave_hook(void)
240 1fce2e75 2021-08-14 op {
241 1fce2e75 2021-08-14 op struct timeval tv;
242 1fce2e75 2021-08-14 op
243 1fce2e75 2021-08-14 op if (autosave <= 0)
244 1fce2e75 2021-08-14 op return;
245 1fce2e75 2021-08-14 op
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;
249 1fce2e75 2021-08-14 op
250 1fce2e75 2021-08-14 op evtimer_add(&autosaveev, &tv);
251 1fce2e75 2021-08-14 op }
252 1fce2e75 2021-08-14 op }