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 ea639250 2021-01-02 op if (operating && tab->flags & TAB_LAZY) {
41 ea639250 2021-01-02 op tab->flags ^= TAB_LAZY;
42 ea639250 2021-01-02 op load_url_in_tab(tab, tab->hist_cur->h, NULL, 1);
43 ea639250 2021-01-02 op }
44 1fce2e75 2021-08-14 op }
45 1fce2e75 2021-08-14 op
46 1fce2e75 2021-08-14 op unsigned int
47 1fce2e75 2021-08-14 op tab_new_id(void)
48 1fce2e75 2021-08-14 op {
49 1fce2e75 2021-08-14 op static uint32_t tab_counter;
50 1fce2e75 2021-08-14 op
51 1fce2e75 2021-08-14 op return tab_counter++;
52 1fce2e75 2021-08-14 op }
53 1fce2e75 2021-08-14 op
54 1fce2e75 2021-08-14 op struct tab *
55 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
56 1fce2e75 2021-08-14 op {
57 1fce2e75 2021-08-14 op struct tab *tab;
58 1fce2e75 2021-08-14 op
59 87aeb47b 2021-08-18 op ui_schedule_redraw();
60 1fce2e75 2021-08-14 op autosave_hook();
61 1fce2e75 2021-08-14 op
62 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
63 1fce2e75 2021-08-14 op event_loopbreak();
64 1fce2e75 2021-08-14 op return NULL;
65 1fce2e75 2021-08-14 op }
66 1fce2e75 2021-08-14 op
67 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->hist.head);
68 1fce2e75 2021-08-14 op
69 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
70 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.page.head);
71 1fce2e75 2021-08-14 op
72 1fce2e75 2021-08-14 op tab->id = tab_new_id();
73 1fce2e75 2021-08-14 op
74 1fce2e75 2021-08-14 op if (after != NULL)
75 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
76 1fce2e75 2021-08-14 op else
77 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
78 1fce2e75 2021-08-14 op
79 ea639250 2021-01-02 op if (!operating)
80 ea639250 2021-01-02 op tab->flags |= TAB_LAZY;
81 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
82 ea639250 2021-01-02 op switch_to_tab(tab);
83 1fce2e75 2021-08-14 op return tab;
84 1fce2e75 2021-08-14 op }
85 1fce2e75 2021-08-14 op
86 1fce2e75 2021-08-14 op /*
87 1fce2e75 2021-08-14 op * Free every resource linked to the tab, including the tab itself.
88 1fce2e75 2021-08-14 op * Removes the tab from the tablist, but doesn't update the
89 1fce2e75 2021-08-14 op * current_tab though.
90 1fce2e75 2021-08-14 op */
91 1fce2e75 2021-08-14 op void
92 1fce2e75 2021-08-14 op free_tab(struct tab *tab)
93 1fce2e75 2021-08-14 op {
94 1fce2e75 2021-08-14 op stop_tab(tab);
95 87aeb47b 2021-08-18 op ui_schedule_redraw();
96 1fce2e75 2021-08-14 op autosave_hook();
97 1fce2e75 2021-08-14 op
98 1fce2e75 2021-08-14 op if (evtimer_pending(&tab->loadingev, NULL))
99 1fce2e75 2021-08-14 op evtimer_del(&tab->loadingev);
100 1fce2e75 2021-08-14 op
101 1fce2e75 2021-08-14 op TAILQ_REMOVE(&tabshead, tab, tabs);
102 1fce2e75 2021-08-14 op free(tab);
103 1fce2e75 2021-08-14 op }
104 1fce2e75 2021-08-14 op
105 1fce2e75 2021-08-14 op void
106 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
107 1fce2e75 2021-08-14 op {
108 1fce2e75 2021-08-14 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
109 1fce2e75 2021-08-14 op }
110 1fce2e75 2021-08-14 op
111 1fce2e75 2021-08-14 op void
112 1fce2e75 2021-08-14 op save_session(void)
113 1fce2e75 2021-08-14 op {
114 f8c6e753 2021-12-30 op struct session_tab st;
115 1040cc7f 2021-01-02 op struct session_tab_hist sth;
116 f8c6e753 2021-12-30 op struct tab *tab;
117 1040cc7f 2021-01-02 op struct hist *h;
118 1040cc7f 2021-01-02 op int future;
119 1fce2e75 2021-08-14 op
120 d0971653 2021-09-15 op if (safe_mode)
121 d0971653 2021-09-15 op return;
122 d0971653 2021-09-15 op
123 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
124 1fce2e75 2021-08-14 op
125 1fce2e75 2021-08-14 op TAILQ_FOREACH(tab, &tabshead, tabs) {
126 f8c6e753 2021-12-30 op memset(&st, 0, sizeof(st));
127 f8c6e753 2021-12-30 op
128 1fce2e75 2021-08-14 op if (tab == current_tab)
129 f8c6e753 2021-12-30 op st.flags = TAB_CURRENT;
130 1fce2e75 2021-08-14 op
131 f8c6e753 2021-12-30 op strlcpy(st.uri, tab->hist_cur->h, sizeof(st.uri));
132 f8c6e753 2021-12-30 op strlcpy(st.title, tab->buffer.page.title, sizeof(st.title));
133 f8c6e753 2021-12-30 op ui_send_fs(IMSG_SESSION_TAB, 0, &st, sizeof(st));
134 1040cc7f 2021-01-02 op
135 1040cc7f 2021-01-02 op future = 0;
136 1040cc7f 2021-01-02 op TAILQ_FOREACH(h, &tab->hist.head, entries) {
137 1040cc7f 2021-01-02 op if (h == tab->hist_cur) {
138 1040cc7f 2021-01-02 op future = 1;
139 1040cc7f 2021-01-02 op continue;
140 1040cc7f 2021-01-02 op }
141 1040cc7f 2021-01-02 op
142 1040cc7f 2021-01-02 op memset(&sth, 0, sizeof(sth));
143 1040cc7f 2021-01-02 op strlcpy(sth.uri, h->h, sizeof(sth.uri));
144 1040cc7f 2021-01-02 op sth.future = future;
145 1040cc7f 2021-01-02 op ui_send_fs(IMSG_SESSION_TAB_HIST, 0, &sth, sizeof(sth));
146 1040cc7f 2021-01-02 op }
147 1fce2e75 2021-08-14 op }
148 1fce2e75 2021-08-14 op
149 1fce2e75 2021-08-14 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
150 1fce2e75 2021-08-14 op }
151 1fce2e75 2021-08-14 op
152 1fce2e75 2021-08-14 op void
153 1fce2e75 2021-08-14 op autosave_init(void)
154 1fce2e75 2021-08-14 op {
155 1fce2e75 2021-08-14 op evtimer_set(&autosaveev, autosave_timer, NULL);
156 1fce2e75 2021-08-14 op }
157 1fce2e75 2021-08-14 op
158 1fce2e75 2021-08-14 op void
159 1fce2e75 2021-08-14 op autosave_timer(int fd, short event, void *data)
160 1fce2e75 2021-08-14 op {
161 1fce2e75 2021-08-14 op save_session();
162 1fce2e75 2021-08-14 op }
163 1fce2e75 2021-08-14 op
164 1fce2e75 2021-08-14 op /*
165 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
166 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
167 1fce2e75 2021-08-14 op */
168 1fce2e75 2021-08-14 op void
169 1fce2e75 2021-08-14 op autosave_hook(void)
170 1fce2e75 2021-08-14 op {
171 1fce2e75 2021-08-14 op struct timeval tv;
172 1fce2e75 2021-08-14 op
173 1fce2e75 2021-08-14 op if (autosave <= 0)
174 1fce2e75 2021-08-14 op return;
175 1fce2e75 2021-08-14 op
176 1fce2e75 2021-08-14 op if (!evtimer_pending(&autosaveev, NULL)) {
177 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
178 1fce2e75 2021-08-14 op tv.tv_usec = 0;
179 1fce2e75 2021-08-14 op
180 1fce2e75 2021-08-14 op evtimer_add(&autosaveev, &tv);
181 1fce2e75 2021-08-14 op }
182 1fce2e75 2021-08-14 op }