aboutsummaryrefslogtreecommitdiff
path: root/session.c
blob: c651b61a18d5a34635568da54d7c17aec9f5ad7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "compat.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "defaults.h"
#include "fs.h"
#include "minibuffer.h"
#include "parser.h"
#include "session.h"
#include "ui.h"

static struct event	 autosaveev;

void
switch_to_tab(struct tab *tab)
{
	current_tab = tab;
	tab->flags &= ~TAB_URGENT;

	if (operating && tab->flags & TAB_LAZY)
		load_url_in_tab(tab, tab->hist_cur->h, NULL, 0);
}

unsigned int
tab_new_id(void)
{
	static uint32_t tab_counter;

	return tab_counter++;
}

struct tab *
new_tab(const char *url, const char *base, struct tab *after)
{
	struct tab	*tab;

	ui_schedule_redraw();
	autosave_hook();

	if ((tab = calloc(1, sizeof(*tab))) == NULL) {
		event_loopbreak();
		return NULL;
	}
	tab->fd = -1;

	TAILQ_INIT(&tab->hist.head);

	TAILQ_INIT(&tab->buffer.head);
	TAILQ_INIT(&tab->buffer.page.head);

	tab->id = tab_new_id();
	if (!operating)
		tab->flags |= TAB_LAZY;
	switch_to_tab(tab);

	if (after != NULL)
		TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
	else
		TAILQ_INSERT_TAIL(&tabshead, tab, tabs);

	load_url_in_tab(tab, url, base, 0);
	return tab;
}

/*
 * Free every resource linked to the tab, including the tab itself.
 * Removes the tab from the tablist, but doesn't update the
 * current_tab though.
 */
void
free_tab(struct tab *tab)
{
	stop_tab(tab);
	ui_schedule_redraw();
	autosave_hook();

	if (evtimer_pending(&tab->loadingev, NULL))
		evtimer_del(&tab->loadingev);

	TAILQ_REMOVE(&tabshead, tab, tabs);
	free(tab);
}

void
stop_tab(struct tab *tab)
{
	ui_send_net(IMSG_STOP, tab->id, NULL, 0);

	if (tab->fd != -1) {
		close(tab->fd);
		tab->fd = -1;
		free(tab->path);
		tab->path = NULL;
		load_page_from_str(tab, "Stopped.\n");
	}
}

void
save_session(void)
{
	struct tab	*tab;
	char		*t;
	int		 flags;

	if (safe_mode)
		return;

	ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);

	TAILQ_FOREACH(tab, &tabshead, tabs) {
		flags = tab->flags;
		if (tab == current_tab)
			flags |= TAB_CURRENT;

		t = tab->hist_cur->h;
		ui_send_fs(IMSG_SESSION_TAB, flags, t, strlen(t)+1);

		t = tab->buffer.page.title;
		ui_send_fs(IMSG_SESSION_TAB_TITLE, 0, t, strlen(t)+1);
	}

	ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
}

/*
 * Parse a line of the session file.  The format is:
 *
 *	URL [flags,...] [title]\n
 */
static void
parse_session_line(char *line, const char **title, uint32_t *flags)
{
	char *s, *t, *ap;

	*title = "";
	*flags = 0;
	if ((s = strchr(line, ' ')) == NULL)
		return;

	*s++ = '\0';

	if ((t = strchr(s, ' ')) != NULL) {
		*t++ = '\0';
		*title = t;
	}

	while ((ap = strsep(&s, ",")) != NULL) {
		if (*ap == '\0')
			;
		else if (!strcmp(ap, "current"))
			*flags |= TAB_CURRENT;
		else
			message("unknown tab flag: %s", ap);
	}
}

void
load_last_session(void)
{
	const char	*title;
	char		*nl, *line = NULL;
	uint32_t	 flags;
	size_t		 linesize = 0;
	ssize_t		 linelen;
	FILE		*session;
	struct tab	*tab, *curr = NULL;

	if ((session = fopen(session_file, "r")) == NULL) {
		/* first time? */
		new_tab("about:new", NULL, NULL);
		switch_to_tab(new_tab("about:help", NULL, NULL));
		return;
	}

	while ((linelen = getline(&line, &linesize, session)) != -1) {
		if ((nl = strchr(line, '\n')) != NULL)
			*nl = '\0';
		parse_session_line(line, &title, &flags);
		if ((tab = new_tab(line, NULL, NULL)) == NULL)
			err(1, "new_tab");
		strlcpy(tab->buffer.page.title, title,
		    sizeof(tab->buffer.page.title));
		if (flags & TAB_CURRENT)
			curr = tab;
	}

	if (ferror(session))
		message("error reading %s: %s",
		    session_file, strerror(errno));
	fclose(session);
	free(line);

	if (curr != NULL)
		switch_to_tab(curr);

	if (last_time_crashed())
		switch_to_tab(new_tab("about:crash", NULL, NULL));

	return;
}

void
autosave_init(void)
{
	evtimer_set(&autosaveev, autosave_timer, NULL);
}

void
autosave_timer(int fd, short event, void *data)
{
	save_session();
}

/*
 * Function to be called in "interesting" places where we may want to
 * schedule an autosave (like on new tab or before loading an url.)
 */
void
autosave_hook(void)
{
	struct timeval tv;

	if (autosave <= 0)
		return;

	if (!evtimer_pending(&autosaveev, NULL)) {
		tv.tv_sec = autosave;
		tv.tv_usec = 0;

		evtimer_add(&autosaveev, &tv);
	}
}