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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <unistd.h>
26 #include "defaults.h"
27 #include "fs.h"
28 #include "minibuffer.h"
29 #include "session.h"
30 #include "ui.h"
32 struct history history;
34 static struct event autosaveev;
36 void
37 switch_to_tab(struct tab *tab)
38 {
39 current_tab = tab;
40 tab->flags &= ~TAB_URGENT;
42 if (operating && tab->flags & TAB_LAZY)
43 load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
44 }
46 unsigned int
47 tab_new_id(void)
48 {
49 static uint32_t tab_counter;
51 return tab_counter++;
52 }
54 struct tab *
55 new_tab(const char *url, const char *base, struct tab *after)
56 {
57 struct tab *tab;
59 ui_schedule_redraw();
60 autosave_hook();
62 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
63 event_loopbreak();
64 return NULL;
65 }
67 TAILQ_INIT(&tab->hist.head);
68 TAILQ_INIT(&tab->buffer.head);
69 TAILQ_INIT(&tab->buffer.page.head);
70 evtimer_set(&tab->loadingev, NULL, NULL);
72 tab->id = tab_new_id();
74 if (after != NULL)
75 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
76 else
77 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
79 if (!operating)
80 tab->flags |= TAB_LAZY;
81 load_url_in_tab(tab, url, base, 0);
82 switch_to_tab(tab);
83 return tab;
84 }
86 /*
87 * Move a tab from the tablist to the killed tab list and erase its
88 * contents. Append should always be 0 to prepend tabs so unkill_tab
89 * can work correctly; appending is only useful during startup when
90 * receiving the list of killed tabs to keep the correct order.
91 * NB: doesn't update the current_tab.
92 */
93 void
94 kill_tab(struct tab *tab, int append)
95 {
96 int count;
98 stop_tab(tab);
99 erase_buffer(&tab->buffer);
100 TAILQ_REMOVE(&tabshead, tab, tabs);
101 ui_schedule_redraw();
102 autosave_hook();
104 if (evtimer_pending(&tab->loadingev, NULL))
105 evtimer_del(&tab->loadingev);
107 if (append)
108 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
109 else
110 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
112 /* gc closed tabs */
113 count = 0;
114 TAILQ_FOREACH(tab, &ktabshead, tabs)
115 count++;
116 while (count > max_killed_tabs) {
117 count--;
118 free_tab(TAILQ_LAST(&ktabshead, tabshead));
122 /*
123 * Resurrects the lastest killed tab and returns it. The tab is already
124 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
125 * update current_tab.
126 */
127 struct tab *
128 unkill_tab(void)
130 struct tab *t;
132 if (TAILQ_EMPTY(&ktabshead))
133 return NULL;
135 ui_schedule_redraw();
136 autosave_hook();
138 t = TAILQ_FIRST(&ktabshead);
139 TAILQ_REMOVE(&ktabshead, t, tabs);
140 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
141 t->flags |= TAB_LAZY;
142 return t;
145 /*
146 * Free every resource linked to the tab, including the tab itself, and
147 * removes it from the *killed* tablist.
148 */
149 void
150 free_tab(struct tab *tab)
152 TAILQ_REMOVE(&ktabshead, tab, tabs);
153 hist_clear(&tab->hist);
154 free(tab);
157 void
158 stop_tab(struct tab *tab)
160 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
163 static inline void
164 sendtab(struct tab *tab, int killed)
166 struct session_tab st;
167 struct session_tab_hist sth;
168 struct hist *h;
169 int future;
171 memset(&st, 0, sizeof(st));
173 if (tab == current_tab)
174 st.flags |= TAB_CURRENT;
175 if (killed)
176 st.flags |= TAB_KILLED;
178 get_scroll_position(tab, &st.top_line, &st.current_line);
180 strlcpy(st.uri, tab->hist_cur->h, sizeof(st.uri));
181 strlcpy(st.title, tab->buffer.page.title, sizeof(st.title));
182 ui_send_fs(IMSG_SESSION_TAB, 0, &st, sizeof(st));
184 future = 0;
185 TAILQ_FOREACH(h, &tab->hist.head, entries) {
186 if (h == tab->hist_cur) {
187 future = 1;
188 continue;
191 memset(&sth, 0, sizeof(sth));
192 strlcpy(sth.uri, h->h, sizeof(sth.uri));
193 sth.future = future;
194 ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
199 void
200 save_session(void)
202 struct tab *tab;
203 struct histitem hi;
204 size_t i;
206 if (safe_mode)
207 return;
209 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
211 TAILQ_FOREACH(tab, &tabshead, tabs)
212 sendtab(tab, 0);
213 TAILQ_FOREACH(tab, &ktabshead, tabs)
214 sendtab(tab, 1);
216 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
218 if (history.dirty) {
219 for (i = 0; i < history.len && history.dirty > 0; ++i) {
220 if (!history.items[i].dirty)
221 continue;
222 history.dirty--;
223 history.items[i].dirty = 0;
225 memset(&hi, 0, sizeof(hi));
226 hi.ts = history.items[i].ts;
227 strlcpy(hi.uri, history.items[i].uri, sizeof(hi.uri));
228 ui_send_fs(IMSG_HIST_ITEM, 0, &hi, sizeof(hi));
230 ui_send_fs(IMSG_HIST_END, 0, NULL, 0);
231 history.dirty = 0;
235 void
236 history_push(struct histitem *hi)
238 size_t i, oldest = 0;
239 char *uri;
241 for (i = 0; i < history.len; ++i) {
242 if (history.items[i].ts < history.items[oldest].ts)
243 oldest = i;
245 /* remove duplicates */
246 if (!strcmp(history.items[i].uri, hi->uri))
247 return;
250 if ((uri = strdup(hi->uri)) == NULL)
251 abort();
253 /* don't grow too much; replace the oldest */
254 if (history.len == HISTORY_CAP) {
255 history.items[oldest].ts = hi->ts;
256 free(history.items[oldest].uri);
257 history.items[oldest].uri = uri;
258 return;
261 history.items[history.len].ts = hi->ts;
262 history.items[history.len].uri = uri;
263 history.len++;
266 static int
267 history_cmp(const void *a, const void *b)
269 const struct history_item *i = a, *j = b;
270 return strcmp(i->uri, j->uri);
273 void
274 history_sort(void)
276 qsort(history.items, history.len, sizeof(history.items[0]),
277 history_cmp);
280 void
281 history_add(const char *uri)
283 size_t i, j, insert = 0, oldest = 0;
284 char *u;
285 int c;
287 for (i = 0; i < history.len; ++i) {
288 if (history.items[i].ts < history.items[oldest].ts)
289 oldest = i;
291 if (insert != 0 && insert < i)
292 continue;
294 c = strcmp(uri, history.items[i].uri);
295 if (c == 0) {
296 history.items[i].ts = time(NULL);
297 history.items[i].dirty = 1;
298 history.dirty++;
299 autosave_hook();
300 return;
303 if (c > 0)
304 insert = i;
307 if ((u = strdup(uri)) == NULL)
308 return;
310 /* if history is full, replace the oldest one */
311 if (history.len == HISTORY_CAP) {
312 free(history.items[oldest].uri);
313 history.items[oldest].uri = u;
314 history.items[oldest].ts = time(NULL);
315 history.items[oldest].dirty = 1;
316 history.dirty++;
317 history_sort();
318 autosave_hook();
319 return;
322 /* otherwise just insert in the right spot */
324 for (j = history.len; j > insert; --j)
325 memcpy(&history.items[j], &history.items[j-1],
326 sizeof(history.items[j]));
328 history.items[insert].ts = time(NULL);
329 history.items[insert].uri = u;
330 history.items[insert].dirty = 1;
331 history.dirty++;
332 history.len++;
333 autosave_hook();
336 void
337 autosave_init(void)
339 evtimer_set(&autosaveev, autosave_timer, NULL);
342 void
343 autosave_timer(int fd, short event, void *data)
345 save_session();
348 /*
349 * Function to be called in "interesting" places where we may want to
350 * schedule an autosave (like on new tab or before loading an url.)
351 */
352 void
353 autosave_hook(void)
355 struct timeval tv;
357 if (autosave <= 0)
358 return;
360 if (!evtimer_pending(&autosaveev, NULL)) {
361 tv.tv_sec = autosave;
362 tv.tv_usec = 0;
364 evtimer_add(&autosaveev, &tv);