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