2 1fce2e75 2021-08-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 1fce2e75 2021-08-14 op #include "compat.h"
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 9e97090d 2022-02-26 op #include <time.h>
24 1fce2e75 2021-08-14 op #include <unistd.h>
26 1fce2e75 2021-08-14 op #include "defaults.h"
27 b9fcc0e9 2021-10-08 op #include "fs.h"
28 1fce2e75 2021-08-14 op #include "minibuffer.h"
29 1fce2e75 2021-08-14 op #include "session.h"
30 1fce2e75 2021-08-14 op #include "ui.h"
32 9e97090d 2022-02-26 op struct history history;
34 1fce2e75 2021-08-14 op static struct event autosaveev;
37 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
39 1fce2e75 2021-08-14 op current_tab = tab;
40 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
42 fb8dcd1c 2022-01-18 op if (operating && tab->flags & TAB_LAZY)
43 ed21a9a1 2022-01-11 op load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
47 1fce2e75 2021-08-14 op tab_new_id(void)
49 1fce2e75 2021-08-14 op static uint32_t tab_counter;
51 1fce2e75 2021-08-14 op return tab_counter++;
55 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
57 1fce2e75 2021-08-14 op struct tab *tab;
59 87aeb47b 2021-08-18 op ui_schedule_redraw();
60 1fce2e75 2021-08-14 op autosave_hook();
62 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
63 1fce2e75 2021-08-14 op event_loopbreak();
67 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->hist.head);
68 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
69 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.page.head);
70 3b30597e 2022-02-11 op evtimer_set(&tab->loadingev, NULL, NULL);
72 1fce2e75 2021-08-14 op tab->id = tab_new_id();
74 1fce2e75 2021-08-14 op if (after != NULL)
75 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
77 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
79 ea639250 2021-01-02 op if (!operating)
80 ea639250 2021-01-02 op tab->flags |= TAB_LAZY;
81 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
82 ea639250 2021-01-02 op switch_to_tab(tab);
87 6c74799d 2022-01-05 op * Move a tab from the tablist to the killed tab list and erase its
88 05de8ac3 2022-01-06 op * contents. Append should always be 0 to prepend tabs so unkill_tab
89 05de8ac3 2022-01-06 op * can work correctly; appending is only useful during startup when
90 05de8ac3 2022-01-06 op * receiving the list of killed tabs to keep the correct order.
91 05de8ac3 2022-01-06 op * NB: doesn't update the current_tab.
94 05de8ac3 2022-01-06 op kill_tab(struct tab *tab, int append)
98 1fce2e75 2021-08-14 op stop_tab(tab);
99 6c74799d 2022-01-05 op erase_buffer(&tab->buffer);
100 6c74799d 2022-01-05 op TAILQ_REMOVE(&tabshead, tab, tabs);
101 87aeb47b 2021-08-18 op ui_schedule_redraw();
102 1fce2e75 2021-08-14 op autosave_hook();
104 1fce2e75 2021-08-14 op if (evtimer_pending(&tab->loadingev, NULL))
105 1fce2e75 2021-08-14 op evtimer_del(&tab->loadingev);
108 05de8ac3 2022-01-06 op TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
110 05de8ac3 2022-01-06 op TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
112 6c74799d 2022-01-05 op /* gc closed tabs */
114 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
116 6c74799d 2022-01-05 op while (count > max_killed_tabs) {
118 6c74799d 2022-01-05 op free_tab(TAILQ_LAST(&ktabshead, tabshead));
123 265508d0 2022-01-05 op * Resurrects the lastest killed tab and returns it. The tab is already
124 6c74799d 2022-01-05 op * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
125 6c74799d 2022-01-05 op * update current_tab.
128 6c74799d 2022-01-05 op unkill_tab(void)
130 6c74799d 2022-01-05 op struct tab *t;
132 6c74799d 2022-01-05 op if (TAILQ_EMPTY(&ktabshead))
135 b7286684 2022-01-13 op ui_schedule_redraw();
136 6c74799d 2022-01-05 op autosave_hook();
138 6c74799d 2022-01-05 op t = TAILQ_FIRST(&ktabshead);
139 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, t, tabs);
140 6c74799d 2022-01-05 op TAILQ_INSERT_TAIL(&tabshead, t, tabs);
141 6c74799d 2022-01-05 op t->flags |= TAB_LAZY;
146 1b45e5df 2022-01-05 op * Free every resource linked to the tab, including the tab itself, and
147 1b45e5df 2022-01-05 op * removes it from the *killed* tablist.
150 6c74799d 2022-01-05 op free_tab(struct tab *tab)
152 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, tab, tabs);
153 bf935370 2022-01-05 op hist_clear(&tab->hist);
158 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
160 1fce2e75 2021-08-14 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
163 6c74799d 2022-01-05 op static inline void
164 6c74799d 2022-01-05 op sendtab(struct tab *tab, int killed)
166 f8c6e753 2021-12-30 op struct session_tab st;
167 1040cc7f 2021-01-02 op struct session_tab_hist sth;
168 1040cc7f 2021-01-02 op struct hist *h;
171 6c74799d 2022-01-05 op memset(&st, 0, sizeof(st));
173 6c74799d 2022-01-05 op if (tab == current_tab)
174 6c74799d 2022-01-05 op st.flags |= TAB_CURRENT;
176 6c74799d 2022-01-05 op st.flags |= TAB_KILLED;
178 e795e935 2022-01-18 op get_scroll_position(tab, &st.top_line, &st.current_line);
180 6c74799d 2022-01-05 op strlcpy(st.uri, tab->hist_cur->h, sizeof(st.uri));
181 6c74799d 2022-01-05 op strlcpy(st.title, tab->buffer.page.title, sizeof(st.title));
182 6c74799d 2022-01-05 op ui_send_fs(IMSG_SESSION_TAB, 0, &st, sizeof(st));
185 6c74799d 2022-01-05 op TAILQ_FOREACH(h, &tab->hist.head, entries) {
186 6c74799d 2022-01-05 op if (h == tab->hist_cur) {
191 6c74799d 2022-01-05 op memset(&sth, 0, sizeof(sth));
192 6c74799d 2022-01-05 op strlcpy(sth.uri, h->h, sizeof(sth.uri));
193 6c74799d 2022-01-05 op sth.future = future;
194 6c74799d 2022-01-05 op ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
200 6c74799d 2022-01-05 op save_session(void)
202 6c74799d 2022-01-05 op struct tab *tab;
203 9e97090d 2022-02-26 op struct histitem hi;
206 6c74799d 2022-01-05 op if (safe_mode)
209 6c74799d 2022-01-05 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
211 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &tabshead, tabs)
212 6c74799d 2022-01-05 op sendtab(tab, 0);
213 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
214 6c74799d 2022-01-05 op sendtab(tab, 1);
216 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
218 9e97090d 2022-02-26 op if (history.dirty) {
219 9e97090d 2022-02-26 op for (i = 0; i < history.len && history.dirty > 0; ++i) {
220 9e97090d 2022-02-26 op if (!history.items[i].dirty)
222 9e97090d 2022-02-26 op history.dirty--;
223 9e97090d 2022-02-26 op history.items[i].dirty = 0;
225 9e97090d 2022-02-26 op memset(&hi, 0, sizeof(hi));
226 9e97090d 2022-02-26 op hi.ts = history.items[i].ts;
227 9e97090d 2022-02-26 op strlcpy(hi.uri, history.items[i].uri, sizeof(hi.uri));
228 9e97090d 2022-02-26 op ui_send_fs(IMSG_HIST_ITEM, 0, &hi, sizeof(hi));
230 9e97090d 2022-02-26 op ui_send_fs(IMSG_HIST_END, 0, NULL, 0);
231 9e97090d 2022-02-26 op history.dirty = 0;
236 9e97090d 2022-02-26 op history_push(struct histitem *hi)
238 9e97090d 2022-02-26 op size_t i, oldest = 0;
241 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
242 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
245 9e97090d 2022-02-26 op /* remove duplicates */
246 9e97090d 2022-02-26 op if (!strcmp(history.items[i].uri, hi->uri))
250 9e97090d 2022-02-26 op if ((uri = strdup(hi->uri)) == NULL)
253 9e97090d 2022-02-26 op /* don't grow too much; replace the oldest */
254 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
255 9e97090d 2022-02-26 op history.items[oldest].ts = hi->ts;
256 9e97090d 2022-02-26 op free(history.items[oldest].uri);
257 9e97090d 2022-02-26 op history.items[oldest].uri = uri;
261 9e97090d 2022-02-26 op history.items[history.len].ts = hi->ts;
262 9e97090d 2022-02-26 op history.items[history.len].uri = uri;
263 9e97090d 2022-02-26 op history.len++;
267 9e97090d 2022-02-26 op history_cmp(const void *a, const void *b)
269 9e97090d 2022-02-26 op const struct history_item *i = a, *j = b;
270 9e97090d 2022-02-26 op return strcmp(i->uri, j->uri);
274 9e97090d 2022-02-26 op history_sort(void)
276 9e97090d 2022-02-26 op qsort(history.items, history.len, sizeof(history.items[0]),
277 9e97090d 2022-02-26 op history_cmp);
281 9e97090d 2022-02-26 op history_add(const char *uri)
283 9e97090d 2022-02-26 op size_t i, j, insert = 0, oldest = 0;
287 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
288 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
291 9e97090d 2022-02-26 op if (insert != 0 && insert < i)
294 9e97090d 2022-02-26 op c = strcmp(uri, history.items[i].uri);
295 9e97090d 2022-02-26 op if (c == 0) {
296 9e97090d 2022-02-26 op history.items[i].ts = time(NULL);
297 9e97090d 2022-02-26 op history.items[i].dirty = 1;
298 9e97090d 2022-02-26 op history.dirty++;
299 9e97090d 2022-02-26 op autosave_hook();
307 9e97090d 2022-02-26 op if ((u = strdup(uri)) == NULL)
310 9e97090d 2022-02-26 op /* if history is full, replace the oldest one */
311 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
312 9e97090d 2022-02-26 op free(history.items[oldest].uri);
313 9e97090d 2022-02-26 op history.items[oldest].uri = u;
314 9e97090d 2022-02-26 op history.items[oldest].ts = time(NULL);
315 9e97090d 2022-02-26 op history.items[oldest].dirty = 1;
316 9e97090d 2022-02-26 op history.dirty++;
317 9e97090d 2022-02-26 op history_sort();
318 9e97090d 2022-02-26 op autosave_hook();
322 9e97090d 2022-02-26 op /* otherwise just insert in the right spot */
324 9e97090d 2022-02-26 op for (j = history.len; j > insert; --j)
325 9e97090d 2022-02-26 op memcpy(&history.items[j], &history.items[j-1],
326 9e97090d 2022-02-26 op sizeof(history.items[j]));
328 9e97090d 2022-02-26 op history.items[insert].ts = time(NULL);
329 9e97090d 2022-02-26 op history.items[insert].uri = u;
330 9e97090d 2022-02-26 op history.items[insert].dirty = 1;
331 9e97090d 2022-02-26 op history.dirty++;
332 9e97090d 2022-02-26 op history.len++;
333 9e97090d 2022-02-26 op autosave_hook();
337 1fce2e75 2021-08-14 op autosave_init(void)
339 1fce2e75 2021-08-14 op evtimer_set(&autosaveev, autosave_timer, NULL);
343 1fce2e75 2021-08-14 op autosave_timer(int fd, short event, void *data)
345 1fce2e75 2021-08-14 op save_session();
349 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
350 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
353 1fce2e75 2021-08-14 op autosave_hook(void)
355 1fce2e75 2021-08-14 op struct timeval tv;
357 1fce2e75 2021-08-14 op if (autosave <= 0)
360 1fce2e75 2021-08-14 op if (!evtimer_pending(&autosaveev, NULL)) {
361 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
362 1fce2e75 2021-08-14 op tv.tv_usec = 0;
364 1fce2e75 2021-08-14 op evtimer_add(&autosaveev, &tv);