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 "session.h"
29 #include "ui.h"
31 static struct event autosaveev;
33 void
34 switch_to_tab(struct tab *tab)
35 {
36 current_tab = tab;
37 tab->flags &= ~TAB_URGENT;
39 if (operating && tab->flags & TAB_LAZY)
40 load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
41 }
43 unsigned int
44 tab_new_id(void)
45 {
46 static uint32_t tab_counter;
48 return tab_counter++;
49 }
51 struct tab *
52 new_tab(const char *url, const char *base, struct tab *after)
53 {
54 struct tab *tab;
56 ui_schedule_redraw();
57 autosave_hook();
59 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
60 event_loopbreak();
61 return NULL;
62 }
64 TAILQ_INIT(&tab->hist.head);
65 TAILQ_INIT(&tab->buffer.head);
66 TAILQ_INIT(&tab->buffer.page.head);
67 evtimer_set(&tab->loadingev, NULL, NULL);
69 tab->id = tab_new_id();
71 if (after != NULL)
72 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
73 else
74 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
76 if (!operating)
77 tab->flags |= TAB_LAZY;
78 load_url_in_tab(tab, url, base, 0);
79 switch_to_tab(tab);
80 return tab;
81 }
83 /*
84 * Move a tab from the tablist to the killed tab list and erase its
85 * contents. Append should always be 0 to prepend tabs so unkill_tab
86 * can work correctly; appending is only useful during startup when
87 * receiving the list of killed tabs to keep the correct order.
88 * NB: doesn't update the current_tab.
89 */
90 void
91 kill_tab(struct tab *tab, int append)
92 {
93 int count;
95 stop_tab(tab);
96 erase_buffer(&tab->buffer);
97 TAILQ_REMOVE(&tabshead, tab, tabs);
98 ui_schedule_redraw();
99 autosave_hook();
101 if (evtimer_pending(&tab->loadingev, NULL))
102 evtimer_del(&tab->loadingev);
104 if (append)
105 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
106 else
107 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
109 /* gc closed tabs */
110 count = 0;
111 TAILQ_FOREACH(tab, &ktabshead, tabs)
112 count++;
113 while (count > max_killed_tabs) {
114 count--;
115 free_tab(TAILQ_LAST(&ktabshead, tabshead));
119 /*
120 * Resurrects the lastest killed tab and returns it. The tab is already
121 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
122 * update current_tab.
123 */
124 struct tab *
125 unkill_tab(void)
127 struct tab *t;
129 if (TAILQ_EMPTY(&ktabshead))
130 return NULL;
132 ui_schedule_redraw();
133 autosave_hook();
135 t = TAILQ_FIRST(&ktabshead);
136 TAILQ_REMOVE(&ktabshead, t, tabs);
137 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
138 t->flags |= TAB_LAZY;
139 return t;
142 /*
143 * Free every resource linked to the tab, including the tab itself, and
144 * removes it from the *killed* tablist.
145 */
146 void
147 free_tab(struct tab *tab)
149 TAILQ_REMOVE(&ktabshead, tab, tabs);
150 hist_clear(&tab->hist);
151 free(tab);
154 void
155 stop_tab(struct tab *tab)
157 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
160 static inline void
161 sendtab(struct tab *tab, int killed)
163 struct session_tab st;
164 struct session_tab_hist sth;
165 struct hist *h;
166 int future;
168 memset(&st, 0, sizeof(st));
170 if (tab == current_tab)
171 st.flags |= TAB_CURRENT;
172 if (killed)
173 st.flags |= TAB_KILLED;
175 get_scroll_position(tab, &st.top_line, &st.current_line);
177 strlcpy(st.uri, tab->hist_cur->h, sizeof(st.uri));
178 strlcpy(st.title, tab->buffer.page.title, sizeof(st.title));
179 ui_send_fs(IMSG_SESSION_TAB, 0, &st, sizeof(st));
181 future = 0;
182 TAILQ_FOREACH(h, &tab->hist.head, entries) {
183 if (h == tab->hist_cur) {
184 future = 1;
185 continue;
188 memset(&sth, 0, sizeof(sth));
189 strlcpy(sth.uri, h->h, sizeof(sth.uri));
190 sth.future = future;
191 ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
196 void
197 save_session(void)
199 struct tab *tab;
201 if (safe_mode)
202 return;
204 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
206 TAILQ_FOREACH(tab, &tabshead, tabs)
207 sendtab(tab, 0);
208 TAILQ_FOREACH(tab, &ktabshead, tabs)
209 sendtab(tab, 1);
211 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
214 void
215 autosave_init(void)
217 evtimer_set(&autosaveev, autosave_timer, NULL);
220 void
221 autosave_timer(int fd, short event, void *data)
223 save_session();
226 /*
227 * Function to be called in "interesting" places where we may want to
228 * schedule an autosave (like on new tab or before loading an url.)
229 */
230 void
231 autosave_hook(void)
233 struct timeval tv;
235 if (autosave <= 0)
236 return;
238 if (!evtimer_pending(&autosaveev, NULL)) {
239 tv.tv_sec = autosave;
240 tv.tv_usec = 0;
242 evtimer_add(&autosaveev, &tv);