Blame


1 1fce2e75 2021-08-14 op /*
2 1fce2e75 2021-08-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 1fce2e75 2021-08-14 op *
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.
7 1fce2e75 2021-08-14 op *
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.
15 1fce2e75 2021-08-14 op */
16 1fce2e75 2021-08-14 op
17 1fce2e75 2021-08-14 op #include "compat.h"
18 1fce2e75 2021-08-14 op
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 1fce2e75 2021-08-14 op #include <unistd.h>
24 1fce2e75 2021-08-14 op
25 1fce2e75 2021-08-14 op #include "defaults.h"
26 b9fcc0e9 2021-10-08 op #include "fs.h"
27 1fce2e75 2021-08-14 op #include "minibuffer.h"
28 1fce2e75 2021-08-14 op #include "parser.h"
29 1fce2e75 2021-08-14 op #include "session.h"
30 1fce2e75 2021-08-14 op #include "ui.h"
31 1fce2e75 2021-08-14 op
32 1fce2e75 2021-08-14 op static struct event autosaveev;
33 1fce2e75 2021-08-14 op
34 1fce2e75 2021-08-14 op void
35 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
36 1fce2e75 2021-08-14 op {
37 1fce2e75 2021-08-14 op current_tab = tab;
38 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
39 1fce2e75 2021-08-14 op
40 fb8dcd1c 2022-01-18 op if (operating && tab->flags & TAB_LAZY)
41 ed21a9a1 2022-01-11 op load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
42 1fce2e75 2021-08-14 op }
43 1fce2e75 2021-08-14 op
44 1fce2e75 2021-08-14 op unsigned int
45 1fce2e75 2021-08-14 op tab_new_id(void)
46 1fce2e75 2021-08-14 op {
47 1fce2e75 2021-08-14 op static uint32_t tab_counter;
48 1fce2e75 2021-08-14 op
49 1fce2e75 2021-08-14 op return tab_counter++;
50 1fce2e75 2021-08-14 op }
51 1fce2e75 2021-08-14 op
52 1fce2e75 2021-08-14 op struct tab *
53 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
54 1fce2e75 2021-08-14 op {
55 1fce2e75 2021-08-14 op struct tab *tab;
56 1fce2e75 2021-08-14 op
57 87aeb47b 2021-08-18 op ui_schedule_redraw();
58 1fce2e75 2021-08-14 op autosave_hook();
59 1fce2e75 2021-08-14 op
60 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
61 1fce2e75 2021-08-14 op event_loopbreak();
62 1fce2e75 2021-08-14 op return NULL;
63 1fce2e75 2021-08-14 op }
64 1fce2e75 2021-08-14 op
65 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->hist.head);
66 1fce2e75 2021-08-14 op
67 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
68 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.page.head);
69 1fce2e75 2021-08-14 op
70 1fce2e75 2021-08-14 op tab->id = tab_new_id();
71 1fce2e75 2021-08-14 op
72 1fce2e75 2021-08-14 op if (after != NULL)
73 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
74 1fce2e75 2021-08-14 op else
75 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
76 1fce2e75 2021-08-14 op
77 ea639250 2021-01-02 op if (!operating)
78 ea639250 2021-01-02 op tab->flags |= TAB_LAZY;
79 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
80 ea639250 2021-01-02 op switch_to_tab(tab);
81 1fce2e75 2021-08-14 op return tab;
82 1fce2e75 2021-08-14 op }
83 1fce2e75 2021-08-14 op
84 1fce2e75 2021-08-14 op /*
85 6c74799d 2022-01-05 op * Move a tab from the tablist to the killed tab list and erase its
86 05de8ac3 2022-01-06 op * contents. Append should always be 0 to prepend tabs so unkill_tab
87 05de8ac3 2022-01-06 op * can work correctly; appending is only useful during startup when
88 05de8ac3 2022-01-06 op * receiving the list of killed tabs to keep the correct order.
89 05de8ac3 2022-01-06 op * NB: doesn't update the current_tab.
90 1fce2e75 2021-08-14 op */
91 1fce2e75 2021-08-14 op void
92 05de8ac3 2022-01-06 op kill_tab(struct tab *tab, int append)
93 1fce2e75 2021-08-14 op {
94 6c74799d 2022-01-05 op int count;
95 6c74799d 2022-01-05 op
96 1fce2e75 2021-08-14 op stop_tab(tab);
97 6c74799d 2022-01-05 op erase_buffer(&tab->buffer);
98 6c74799d 2022-01-05 op TAILQ_REMOVE(&tabshead, tab, tabs);
99 87aeb47b 2021-08-18 op ui_schedule_redraw();
100 1fce2e75 2021-08-14 op autosave_hook();
101 1fce2e75 2021-08-14 op
102 1fce2e75 2021-08-14 op if (evtimer_pending(&tab->loadingev, NULL))
103 1fce2e75 2021-08-14 op evtimer_del(&tab->loadingev);
104 1fce2e75 2021-08-14 op
105 05de8ac3 2022-01-06 op if (append)
106 05de8ac3 2022-01-06 op TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
107 05de8ac3 2022-01-06 op else
108 05de8ac3 2022-01-06 op TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
109 6c74799d 2022-01-05 op
110 6c74799d 2022-01-05 op /* gc closed tabs */
111 6c74799d 2022-01-05 op count = 0;
112 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
113 6c74799d 2022-01-05 op count++;
114 6c74799d 2022-01-05 op while (count > max_killed_tabs) {
115 6c74799d 2022-01-05 op count--;
116 6c74799d 2022-01-05 op free_tab(TAILQ_LAST(&ktabshead, tabshead));
117 6c74799d 2022-01-05 op }
118 6c74799d 2022-01-05 op }
119 6c74799d 2022-01-05 op
120 6c74799d 2022-01-05 op /*
121 265508d0 2022-01-05 op * Resurrects the lastest killed tab and returns it. The tab is already
122 6c74799d 2022-01-05 op * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
123 6c74799d 2022-01-05 op * update current_tab.
124 6c74799d 2022-01-05 op */
125 6c74799d 2022-01-05 op struct tab *
126 6c74799d 2022-01-05 op unkill_tab(void)
127 6c74799d 2022-01-05 op {
128 6c74799d 2022-01-05 op struct tab *t;
129 6c74799d 2022-01-05 op
130 6c74799d 2022-01-05 op if (TAILQ_EMPTY(&ktabshead))
131 6c74799d 2022-01-05 op return NULL;
132 6c74799d 2022-01-05 op
133 b7286684 2022-01-13 op ui_schedule_redraw();
134 6c74799d 2022-01-05 op autosave_hook();
135 6c74799d 2022-01-05 op
136 6c74799d 2022-01-05 op t = TAILQ_FIRST(&ktabshead);
137 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, t, tabs);
138 6c74799d 2022-01-05 op TAILQ_INSERT_TAIL(&tabshead, t, tabs);
139 6c74799d 2022-01-05 op t->flags |= TAB_LAZY;
140 6c74799d 2022-01-05 op return t;
141 6c74799d 2022-01-05 op }
142 6c74799d 2022-01-05 op
143 6c74799d 2022-01-05 op /*
144 1b45e5df 2022-01-05 op * Free every resource linked to the tab, including the tab itself, and
145 1b45e5df 2022-01-05 op * removes it from the *killed* tablist.
146 6c74799d 2022-01-05 op */
147 6c74799d 2022-01-05 op void
148 6c74799d 2022-01-05 op free_tab(struct tab *tab)
149 6c74799d 2022-01-05 op {
150 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, tab, tabs);
151 bf935370 2022-01-05 op hist_clear(&tab->hist);
152 1fce2e75 2021-08-14 op free(tab);
153 1fce2e75 2021-08-14 op }
154 1fce2e75 2021-08-14 op
155 1fce2e75 2021-08-14 op void
156 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
157 1fce2e75 2021-08-14 op {
158 1fce2e75 2021-08-14 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
159 1fce2e75 2021-08-14 op }
160 1fce2e75 2021-08-14 op
161 6c74799d 2022-01-05 op static inline void
162 6c74799d 2022-01-05 op sendtab(struct tab *tab, int killed)
163 1fce2e75 2021-08-14 op {
164 f8c6e753 2021-12-30 op struct session_tab st;
165 1040cc7f 2021-01-02 op struct session_tab_hist sth;
166 1040cc7f 2021-01-02 op struct hist *h;
167 1040cc7f 2021-01-02 op int future;
168 1fce2e75 2021-08-14 op
169 6c74799d 2022-01-05 op memset(&st, 0, sizeof(st));
170 d0971653 2021-09-15 op
171 6c74799d 2022-01-05 op if (tab == current_tab)
172 6c74799d 2022-01-05 op st.flags |= TAB_CURRENT;
173 6c74799d 2022-01-05 op if (killed)
174 6c74799d 2022-01-05 op st.flags |= TAB_KILLED;
175 1fce2e75 2021-08-14 op
176 e795e935 2022-01-18 op get_scroll_position(tab, &st.top_line, &st.current_line);
177 e795e935 2022-01-18 op
178 6c74799d 2022-01-05 op strlcpy(st.uri, tab->hist_cur->h, sizeof(st.uri));
179 6c74799d 2022-01-05 op strlcpy(st.title, tab->buffer.page.title, sizeof(st.title));
180 6c74799d 2022-01-05 op ui_send_fs(IMSG_SESSION_TAB, 0, &st, sizeof(st));
181 f8c6e753 2021-12-30 op
182 6c74799d 2022-01-05 op future = 0;
183 6c74799d 2022-01-05 op TAILQ_FOREACH(h, &tab->hist.head, entries) {
184 6c74799d 2022-01-05 op if (h == tab->hist_cur) {
185 6c74799d 2022-01-05 op future = 1;
186 6c74799d 2022-01-05 op continue;
187 6c74799d 2022-01-05 op }
188 1fce2e75 2021-08-14 op
189 6c74799d 2022-01-05 op memset(&sth, 0, sizeof(sth));
190 6c74799d 2022-01-05 op strlcpy(sth.uri, h->h, sizeof(sth.uri));
191 6c74799d 2022-01-05 op sth.future = future;
192 6c74799d 2022-01-05 op ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
193 6c74799d 2022-01-05 op }
194 1040cc7f 2021-01-02 op
195 6c74799d 2022-01-05 op }
196 1040cc7f 2021-01-02 op
197 6c74799d 2022-01-05 op void
198 6c74799d 2022-01-05 op save_session(void)
199 6c74799d 2022-01-05 op {
200 6c74799d 2022-01-05 op struct tab *tab;
201 1fce2e75 2021-08-14 op
202 6c74799d 2022-01-05 op if (safe_mode)
203 6c74799d 2022-01-05 op return;
204 6c74799d 2022-01-05 op
205 6c74799d 2022-01-05 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
206 6c74799d 2022-01-05 op
207 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &tabshead, tabs)
208 6c74799d 2022-01-05 op sendtab(tab, 0);
209 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
210 6c74799d 2022-01-05 op sendtab(tab, 1);
211 6c74799d 2022-01-05 op
212 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
213 1fce2e75 2021-08-14 op }
214 1fce2e75 2021-08-14 op
215 1fce2e75 2021-08-14 op void
216 1fce2e75 2021-08-14 op autosave_init(void)
217 1fce2e75 2021-08-14 op {
218 1fce2e75 2021-08-14 op evtimer_set(&autosaveev, autosave_timer, NULL);
219 1fce2e75 2021-08-14 op }
220 1fce2e75 2021-08-14 op
221 1fce2e75 2021-08-14 op void
222 1fce2e75 2021-08-14 op autosave_timer(int fd, short event, void *data)
223 1fce2e75 2021-08-14 op {
224 1fce2e75 2021-08-14 op save_session();
225 1fce2e75 2021-08-14 op }
226 1fce2e75 2021-08-14 op
227 1fce2e75 2021-08-14 op /*
228 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
229 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
230 1fce2e75 2021-08-14 op */
231 1fce2e75 2021-08-14 op void
232 1fce2e75 2021-08-14 op autosave_hook(void)
233 1fce2e75 2021-08-14 op {
234 1fce2e75 2021-08-14 op struct timeval tv;
235 1fce2e75 2021-08-14 op
236 1fce2e75 2021-08-14 op if (autosave <= 0)
237 1fce2e75 2021-08-14 op return;
238 1fce2e75 2021-08-14 op
239 1fce2e75 2021-08-14 op if (!evtimer_pending(&autosaveev, NULL)) {
240 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
241 1fce2e75 2021-08-14 op tv.tv_usec = 0;
242 1fce2e75 2021-08-14 op
243 1fce2e75 2021-08-14 op evtimer_add(&autosaveev, &tv);
244 1fce2e75 2021-08-14 op }
245 1fce2e75 2021-08-14 op }