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