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 <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
27 #include "defaults.h"
28 #include "fs.h"
29 #include "minibuffer.h"
30 #include "session.h"
31 #include "ui.h"
33 struct history history;
35 static struct event autosaveev;
37 void
38 switch_to_tab(struct tab *tab)
39 {
40 current_tab = tab;
41 tab->flags &= ~TAB_URGENT;
43 if (operating && tab->flags & TAB_LAZY)
44 load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
45 }
47 unsigned int
48 tab_new_id(void)
49 {
50 static uint32_t tab_counter;
52 return tab_counter++;
53 }
55 struct tab *
56 new_tab(const char *url, const char *base, struct tab *after)
57 {
58 struct tab *tab;
60 ui_schedule_redraw();
61 autosave_hook();
63 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
64 event_loopbreak();
65 return NULL;
66 }
68 TAILQ_INIT(&tab->hist.head);
69 TAILQ_INIT(&tab->buffer.head);
70 TAILQ_INIT(&tab->buffer.page.head);
71 evtimer_set(&tab->loadingev, NULL, NULL);
73 tab->id = tab_new_id();
75 if (after != NULL)
76 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
77 else
78 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
80 if (!operating)
81 tab->flags |= TAB_LAZY;
82 load_url_in_tab(tab, url, base, 0);
83 switch_to_tab(tab);
84 return tab;
85 }
87 /*
88 * Move a tab from the tablist to the killed tab list and erase its
89 * contents. Append should always be 0 to prepend tabs so unkill_tab
90 * can work correctly; appending is only useful during startup when
91 * receiving the list of killed tabs to keep the correct order.
92 * NB: doesn't update the current_tab.
93 */
94 void
95 kill_tab(struct tab *tab, int append)
96 {
97 int count;
99 stop_tab(tab);
100 erase_buffer(&tab->buffer);
101 TAILQ_REMOVE(&tabshead, tab, tabs);
102 ui_schedule_redraw();
103 autosave_hook();
105 if (evtimer_pending(&tab->loadingev, NULL))
106 evtimer_del(&tab->loadingev);
108 if (append)
109 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
110 else
111 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
113 /* gc closed tabs */
114 count = 0;
115 TAILQ_FOREACH(tab, &ktabshead, tabs)
116 count++;
117 while (count > max_killed_tabs) {
118 count--;
119 free_tab(TAILQ_LAST(&ktabshead, tabshead));
123 /*
124 * Resurrects the lastest killed tab and returns it. The tab is already
125 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
126 * update current_tab.
127 */
128 struct tab *
129 unkill_tab(void)
131 struct tab *t;
133 if (TAILQ_EMPTY(&ktabshead))
134 return NULL;
136 ui_schedule_redraw();
137 autosave_hook();
139 t = TAILQ_FIRST(&ktabshead);
140 TAILQ_REMOVE(&ktabshead, t, tabs);
141 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
142 t->flags |= TAB_LAZY;
143 return t;
146 /*
147 * Free every resource linked to the tab, including the tab itself, and
148 * removes it from the *killed* tablist.
149 */
150 void
151 free_tab(struct tab *tab)
153 TAILQ_REMOVE(&ktabshead, tab, tabs);
154 hist_clear(&tab->hist);
155 free(tab);
158 void
159 stop_tab(struct tab *tab)
161 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
164 static inline void
165 savetab(FILE *fp, struct tab *tab, int killed)
167 struct hist *h;
168 size_t top_line, current_line;
169 int future;
171 get_scroll_position(tab, &top_line, &current_line);
173 fprintf(fp, "%s ", tab->hist_cur->h);
174 if (tab == current_tab)
175 fprintf(fp, "current,");
176 if (killed)
177 fprintf(fp, "killed,");
179 fprintf(fp, "top=%zu,cur=%zu %s\n", top_line, current_line,
180 tab->buffer.page.title);
182 future = 0;
183 TAILQ_FOREACH(h, &tab->hist.head, entries) {
184 if (h == tab->hist_cur) {
185 future = 1;
186 continue;
189 fprintf(fp, "%s %s\n", future ? ">" : "<", h->h);
193 void
194 save_session(void)
196 FILE *tmp;
197 struct tab *tab;
198 size_t i;
199 int fd, err= 0;
200 char sfn[PATH_MAX];
202 if (safe_mode)
203 return;
205 strlcpy(sfn, session_file_tmp, sizeof(sfn));
206 if ((fd = mkstemp(sfn)) == -1 ||
207 (tmp = fdopen(fd, "w")) == NULL) {
208 if (fd != -1) {
209 unlink(sfn);
210 close(fd);
212 return;
215 TAILQ_FOREACH(tab, &tabshead, tabs)
216 savetab(tmp, tab, 0);
217 TAILQ_FOREACH(tab, &ktabshead, tabs)
218 savetab(tmp, tab, 1);
220 err = ferror(tmp);
221 fclose(tmp);
223 if (err) {
224 unlink(sfn);
225 return;
228 if (rename(sfn, session_file))
229 return;
231 strlcpy(sfn, history_file_tmp, sizeof(sfn));
232 if ((fd = mkstemp(sfn)) == -1 ||
233 (tmp = fdopen(fd, "w")) == NULL) {
234 if (fd != -1) {
235 unlink(sfn);
236 close(fd);
238 return;
241 if (history.dirty) {
242 for (i = 0; i < history.len && history.dirty > 0; ++i) {
243 if (!history.items[i].dirty)
244 continue;
245 history.dirty--;
246 history.items[i].dirty = 0;
248 fprintf(tmp, "%lld %s\n",
249 (long long)history.items[i].ts,
250 history.items[i].uri);
252 history.dirty = 0;
255 err = ferror(tmp);
256 fclose(tmp);
258 if (err) {
259 unlink(sfn);
260 return;
263 rename(sfn, history_file);
266 void
267 history_push(struct histitem *hi)
269 size_t i, oldest = 0;
270 char *uri;
272 for (i = 0; i < history.len; ++i) {
273 if (history.items[i].ts < history.items[oldest].ts)
274 oldest = i;
276 /* remove duplicates */
277 if (!strcmp(history.items[i].uri, hi->uri))
278 return;
281 if ((uri = strdup(hi->uri)) == NULL)
282 abort();
284 /* don't grow too much; replace the oldest */
285 if (history.len == HISTORY_CAP) {
286 history.items[oldest].ts = hi->ts;
287 free(history.items[oldest].uri);
288 history.items[oldest].uri = uri;
289 return;
292 history.items[history.len].ts = hi->ts;
293 history.items[history.len].uri = uri;
294 history.len++;
297 static int
298 history_cmp(const void *a, const void *b)
300 const struct history_item *i = a, *j = b;
301 return strcmp(i->uri, j->uri);
304 void
305 history_sort(void)
307 qsort(history.items, history.len, sizeof(history.items[0]),
308 history_cmp);
311 void
312 history_add(const char *uri)
314 size_t i, j, insert = 0, oldest = 0;
315 char *u;
316 int c;
318 for (i = 0; i < history.len; ++i) {
319 if (history.items[i].ts < history.items[oldest].ts)
320 oldest = i;
322 if (insert != 0 && insert < i)
323 continue;
325 c = strcmp(uri, history.items[i].uri);
326 if (c == 0) {
327 history.items[i].ts = time(NULL);
328 history.items[i].dirty = 1;
329 history.dirty++;
330 autosave_hook();
331 return;
334 if (c > 0)
335 insert = i;
338 if ((u = strdup(uri)) == NULL)
339 return;
341 /* if history is full, replace the oldest one */
342 if (history.len == HISTORY_CAP) {
343 free(history.items[oldest].uri);
344 history.items[oldest].uri = u;
345 history.items[oldest].ts = time(NULL);
346 history.items[oldest].dirty = 1;
347 history.dirty++;
348 history_sort();
349 autosave_hook();
350 return;
353 /* otherwise just insert in the right spot */
355 for (j = history.len; j > insert; --j)
356 memcpy(&history.items[j], &history.items[j-1],
357 sizeof(history.items[j]));
359 history.items[insert].ts = time(NULL);
360 history.items[insert].uri = u;
361 history.items[insert].dirty = 1;
362 history.dirty++;
363 history.len++;
364 autosave_hook();
367 void
368 autosave_init(void)
370 evtimer_set(&autosaveev, autosave_timer, NULL);
373 void
374 autosave_timer(int fd, short event, void *data)
376 save_session();
379 /*
380 * Function to be called in "interesting" places where we may want to
381 * schedule an autosave (like on new tab or before loading an url.)
382 */
383 void
384 autosave_hook(void)
386 struct timeval tv;
388 if (autosave <= 0)
389 return;
391 if (!evtimer_pending(&autosaveev, NULL)) {
392 tv.tv_sec = autosave;
393 tv.tv_usec = 0;
395 evtimer_add(&autosaveev, &tv);