2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
27 #include "minibuffer.h"
32 static struct event autosaveev;
35 switch_to_tab(struct 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, LU_MODE_NOHIST);
49 static uint32_t tab_counter;
55 new_tab(const char *url, const char *base, struct tab *after)
62 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
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();
75 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
77 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
80 tab->flags |= TAB_LAZY;
81 load_url_in_tab(tab, url, base, 0);
87 * Move a tab from the tablist to the killed tab list and erase its
88 * contents. Append should always be 0 to prepend tabs so unkill_tab
89 * can work correctly; appending is only useful during startup when
90 * receiving the list of killed tabs to keep the correct order.
91 * NB: doesn't update the current_tab.
94 kill_tab(struct tab *tab, int append)
99 erase_buffer(&tab->buffer);
100 TAILQ_REMOVE(&tabshead, tab, tabs);
101 ui_schedule_redraw();
104 if (evtimer_pending(&tab->loadingev, NULL))
105 evtimer_del(&tab->loadingev);
108 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
110 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
114 TAILQ_FOREACH(tab, &ktabshead, tabs)
116 while (count > max_killed_tabs) {
118 free_tab(TAILQ_LAST(&ktabshead, tabshead));
123 * Resurrects the lastest killed tab and returns it. The tab is already
124 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
125 * update current_tab.
132 if (TAILQ_EMPTY(&ktabshead))
135 ui_schedule_redraw();
138 t = TAILQ_FIRST(&ktabshead);
139 TAILQ_REMOVE(&ktabshead, t, tabs);
140 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
141 t->flags |= TAB_LAZY;
146 * Free every resource linked to the tab, including the tab itself, and
147 * removes it from the *killed* tablist.
150 free_tab(struct tab *tab)
152 TAILQ_REMOVE(&ktabshead, tab, tabs);
153 hist_clear(&tab->hist);
158 stop_tab(struct tab *tab)
160 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
164 sendtab(struct tab *tab, int killed)
166 struct session_tab st;
167 struct session_tab_hist sth;
171 memset(&st, 0, sizeof(st));
173 if (tab == current_tab)
174 st.flags |= TAB_CURRENT;
176 st.flags |= TAB_KILLED;
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));
183 TAILQ_FOREACH(h, &tab->hist.head, entries) {
184 if (h == tab->hist_cur) {
189 memset(&sth, 0, sizeof(sth));
190 strlcpy(sth.uri, h->h, sizeof(sth.uri));
192 ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
205 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
207 TAILQ_FOREACH(tab, &tabshead, tabs)
209 TAILQ_FOREACH(tab, &ktabshead, tabs)
212 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
218 evtimer_set(&autosaveev, autosave_timer, NULL);
222 autosave_timer(int fd, short event, void *data)
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.)
239 if (!evtimer_pending(&autosaveev, NULL)) {
240 tv.tv_sec = autosave;
243 evtimer_add(&autosaveev, &tv);