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 <unistd.h>
25 #include "defaults.h"
26 #include "fs.h"
27 #include "minibuffer.h"
28 #include "parser.h"
29 #include "session.h"
30 #include "ui.h"
32 static struct event autosaveev;
34 void
35 switch_to_tab(struct tab *tab)
36 {
37 current_tab = tab;
38 tab->flags &= ~TAB_URGENT;
40 if (operating && tab->flags & TAB_LAZY)
41 load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
42 }
44 unsigned int
45 tab_new_id(void)
46 {
47 static uint32_t tab_counter;
49 return tab_counter++;
50 }
52 struct tab *
53 new_tab(const char *url, const char *base, struct tab *after)
54 {
55 struct tab *tab;
57 ui_schedule_redraw();
58 autosave_hook();
60 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
61 event_loopbreak();
62 return NULL;
63 }
65 TAILQ_INIT(&tab->hist.head);
67 TAILQ_INIT(&tab->buffer.head);
68 TAILQ_INIT(&tab->buffer.page.head);
70 tab->id = tab_new_id();
72 if (after != NULL)
73 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
74 else
75 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
77 if (!operating)
78 tab->flags |= TAB_LAZY;
79 load_url_in_tab(tab, url, base, 0);
80 switch_to_tab(tab);
81 return tab;
82 }
84 /*
85 * Move a tab from the tablist to the killed tab list and erase its
86 * contents. Append should always be 0 to prepend tabs so unkill_tab
87 * can work correctly; appending is only useful during startup when
88 * receiving the list of killed tabs to keep the correct order.
89 * NB: doesn't update the current_tab.
90 */
91 void
92 kill_tab(struct tab *tab, int append)
93 {
94 int count;
96 stop_tab(tab);
97 erase_buffer(&tab->buffer);
98 TAILQ_REMOVE(&tabshead, tab, tabs);
99 ui_schedule_redraw();
100 autosave_hook();
102 if (evtimer_pending(&tab->loadingev, NULL))
103 evtimer_del(&tab->loadingev);
105 if (append)
106 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
107 else
108 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
110 /* gc closed tabs */
111 count = 0;
112 TAILQ_FOREACH(tab, &ktabshead, tabs)
113 count++;
114 while (count > max_killed_tabs) {
115 count--;
116 free_tab(TAILQ_LAST(&ktabshead, tabshead));
120 /*
121 * Resurrects the lastest killed tab and returns it. The tab is already
122 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
123 * update current_tab.
124 */
125 struct tab *
126 unkill_tab(void)
128 struct tab *t;
130 if (TAILQ_EMPTY(&ktabshead))
131 return NULL;
133 ui_schedule_redraw();
134 autosave_hook();
136 t = TAILQ_FIRST(&ktabshead);
137 TAILQ_REMOVE(&ktabshead, t, tabs);
138 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
139 t->flags |= TAB_LAZY;
140 return t;
143 /*
144 * Free every resource linked to the tab, including the tab itself, and
145 * removes it from the *killed* tablist.
146 */
147 void
148 free_tab(struct tab *tab)
150 TAILQ_REMOVE(&ktabshead, tab, tabs);
151 hist_clear(&tab->hist);
152 free(tab);
155 void
156 stop_tab(struct tab *tab)
158 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
161 static inline void
162 sendtab(struct tab *tab, int killed)
164 struct session_tab st;
165 struct session_tab_hist sth;
166 struct hist *h;
167 int future;
169 memset(&st, 0, sizeof(st));
171 if (tab == current_tab)
172 st.flags |= TAB_CURRENT;
173 if (killed)
174 st.flags |= TAB_KILLED;
176 get_scroll_position(tab, &st.top_line, &st.current_line);
178 strlcpy(st.uri, tab->hist_cur->h, sizeof(st.uri));
179 strlcpy(st.title, tab->buffer.page.title, sizeof(st.title));
180 ui_send_fs(IMSG_SESSION_TAB, 0, &st, sizeof(st));
182 future = 0;
183 TAILQ_FOREACH(h, &tab->hist.head, entries) {
184 if (h == tab->hist_cur) {
185 future = 1;
186 continue;
189 memset(&sth, 0, sizeof(sth));
190 strlcpy(sth.uri, h->h, sizeof(sth.uri));
191 sth.future = future;
192 ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
197 void
198 save_session(void)
200 struct tab *tab;
202 if (safe_mode)
203 return;
205 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
207 TAILQ_FOREACH(tab, &tabshead, tabs)
208 sendtab(tab, 0);
209 TAILQ_FOREACH(tab, &ktabshead, tabs)
210 sendtab(tab, 1);
212 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
215 void
216 autosave_init(void)
218 evtimer_set(&autosaveev, autosave_timer, NULL);
221 void
222 autosave_timer(int fd, short event, void *data)
224 save_session();
227 /*
228 * Function to be called in "interesting" places where we may want to
229 * schedule an autosave (like on new tab or before loading an url.)
230 */
231 void
232 autosave_hook(void)
234 struct timeval tv;
236 if (autosave <= 0)
237 return;
239 if (!evtimer_pending(&autosaveev, NULL)) {
240 tv.tv_sec = autosave;
241 tv.tv_usec = 0;
243 evtimer_add(&autosaveev, &tv);