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 tab->flags ^= TAB_LAZY;
42 load_url_in_tab(tab, tab->hist_cur->h, NULL, 1);
43 }
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);
69 TAILQ_INIT(&tab->buffer.head);
70 TAILQ_INIT(&tab->buffer.page.head);
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. NB: doesn't update the current_tab.
89 */
90 void
91 kill_tab(struct tab *tab)
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 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
106 /* gc closed tabs */
107 count = 0;
108 TAILQ_FOREACH(tab, &ktabshead, tabs)
109 count++;
110 while (count > max_killed_tabs) {
111 count--;
112 free_tab(TAILQ_LAST(&ktabshead, tabshead));
116 /*
117 * Resurrects the lastest killed tab and returns it. The tab is already
118 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
119 * update current_tab.
120 */
121 struct tab *
122 unkill_tab(void)
124 struct tab *t;
126 if (TAILQ_EMPTY(&ktabshead))
127 return NULL;
129 autosave_hook();
131 t = TAILQ_FIRST(&ktabshead);
132 TAILQ_REMOVE(&ktabshead, t, tabs);
133 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
134 t->flags |= TAB_LAZY;
135 return t;
138 /*
139 * Free every resource linked to the tab, including the tab itself.
140 * Removes the tab from the *killed* tablist, but doesn't update the
141 * current_tab though.
142 */
143 void
144 free_tab(struct tab *tab)
146 /* TODO: free the history */
147 TAILQ_REMOVE(&ktabshead, tab, tabs);
148 free(tab);
151 void
152 stop_tab(struct tab *tab)
154 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
157 static inline void
158 sendtab(struct tab *tab, int killed)
160 struct session_tab st;
161 struct session_tab_hist sth;
162 struct hist *h;
163 int future;
165 memset(&st, 0, sizeof(st));
167 if (tab == current_tab)
168 st.flags |= TAB_CURRENT;
169 if (killed)
170 st.flags |= TAB_KILLED;
172 strlcpy(st.uri, tab->hist_cur->h, sizeof(st.uri));
173 strlcpy(st.title, tab->buffer.page.title, sizeof(st.title));
174 ui_send_fs(IMSG_SESSION_TAB, 0, &st, sizeof(st));
176 future = 0;
177 TAILQ_FOREACH(h, &tab->hist.head, entries) {
178 if (h == tab->hist_cur) {
179 future = 1;
180 continue;
183 memset(&sth, 0, sizeof(sth));
184 strlcpy(sth.uri, h->h, sizeof(sth.uri));
185 sth.future = future;
186 ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
191 void
192 save_session(void)
194 struct tab *tab;
196 if (safe_mode)
197 return;
199 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
201 TAILQ_FOREACH(tab, &tabshead, tabs)
202 sendtab(tab, 0);
203 TAILQ_FOREACH(tab, &ktabshead, tabs)
204 sendtab(tab, 1);
206 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
209 void
210 autosave_init(void)
212 evtimer_set(&autosaveev, autosave_timer, NULL);
215 void
216 autosave_timer(int fd, short event, void *data)
218 save_session();
221 /*
222 * Function to be called in "interesting" places where we may want to
223 * schedule an autosave (like on new tab or before loading an url.)
224 */
225 void
226 autosave_hook(void)
228 struct timeval tv;
230 if (autosave <= 0)
231 return;
233 if (!evtimer_pending(&autosaveev, NULL)) {
234 tv.tv_sec = autosave;
235 tv.tv_usec = 0;
237 evtimer_add(&autosaveev, &tv);