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 *session, *hist;
197 struct tab *tab;
198 size_t i;
200 if (safe_mode)
201 return;
203 if ((session = fopen(session_file, "w")) == NULL)
204 return;
206 TAILQ_FOREACH(tab, &tabshead, tabs)
207 savetab(session, tab, 0);
208 TAILQ_FOREACH(tab, &ktabshead, tabs)
209 savetab(session, tab, 1);
211 fclose(session);
213 if ((hist = fopen(history_file, "a")) == NULL)
214 return;
216 if (history.dirty) {
217 for (i = 0; i < history.len && history.dirty > 0; ++i) {
218 if (!history.items[i].dirty)
219 continue;
220 history.dirty--;
221 history.items[i].dirty = 0;
223 fprintf(hist, "%lld %s\n",
224 (long long)history.items[i].ts,
225 history.items[i].uri);
227 history.dirty = 0;
230 fclose(hist);
233 void
234 history_push(struct histitem *hi)
236 size_t i, oldest = 0;
237 char *uri;
239 for (i = 0; i < history.len; ++i) {
240 if (history.items[i].ts < history.items[oldest].ts)
241 oldest = i;
243 /* remove duplicates */
244 if (!strcmp(history.items[i].uri, hi->uri))
245 return;
248 if ((uri = strdup(hi->uri)) == NULL)
249 abort();
251 /* don't grow too much; replace the oldest */
252 if (history.len == HISTORY_CAP) {
253 history.items[oldest].ts = hi->ts;
254 free(history.items[oldest].uri);
255 history.items[oldest].uri = uri;
256 return;
259 history.items[history.len].ts = hi->ts;
260 history.items[history.len].uri = uri;
261 history.len++;
264 static int
265 history_cmp(const void *a, const void *b)
267 const struct history_item *i = a, *j = b;
268 return strcmp(i->uri, j->uri);
271 void
272 history_sort(void)
274 qsort(history.items, history.len, sizeof(history.items[0]),
275 history_cmp);
278 void
279 history_add(const char *uri)
281 size_t i, j, insert = 0, oldest = 0;
282 char *u;
283 int c;
285 for (i = 0; i < history.len; ++i) {
286 if (history.items[i].ts < history.items[oldest].ts)
287 oldest = i;
289 if (insert != 0 && insert < i)
290 continue;
292 c = strcmp(uri, history.items[i].uri);
293 if (c == 0) {
294 history.items[i].ts = time(NULL);
295 history.items[i].dirty = 1;
296 history.dirty++;
297 autosave_hook();
298 return;
301 if (c > 0)
302 insert = i;
305 if ((u = strdup(uri)) == NULL)
306 return;
308 /* if history is full, replace the oldest one */
309 if (history.len == HISTORY_CAP) {
310 free(history.items[oldest].uri);
311 history.items[oldest].uri = u;
312 history.items[oldest].ts = time(NULL);
313 history.items[oldest].dirty = 1;
314 history.dirty++;
315 history_sort();
316 autosave_hook();
317 return;
320 /* otherwise just insert in the right spot */
322 for (j = history.len; j > insert; --j)
323 memcpy(&history.items[j], &history.items[j-1],
324 sizeof(history.items[j]));
326 history.items[insert].ts = time(NULL);
327 history.items[insert].uri = u;
328 history.items[insert].dirty = 1;
329 history.dirty++;
330 history.len++;
331 autosave_hook();
334 void
335 autosave_init(void)
337 evtimer_set(&autosaveev, autosave_timer, NULL);
340 void
341 autosave_timer(int fd, short event, void *data)
343 save_session();
346 /*
347 * Function to be called in "interesting" places where we may want to
348 * schedule an autosave (like on new tab or before loading an url.)
349 */
350 void
351 autosave_hook(void)
353 struct timeval tv;
355 if (autosave <= 0)
356 return;
358 if (!evtimer_pending(&autosaveev, NULL)) {
359 tv.tv_sec = autosave;
360 tv.tv_usec = 0;
362 evtimer_add(&autosaveev, &tv);