summaryrefslogtreecommitdiff
path: root/tests/test_terminal.c
blob: b6bab905dd50af6dd9590ccb30c25dd9edc5aaeb (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * test_terminal - Test Terminal
 *
 * Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
 * Copyright (c) 2011 University of Tuebingen
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Test Terminal
 * This runs a terminal emulator with default settings on all connected outputs.
 * This is supposed to be a fully functional VT. It's only missing
 * configurability and extended features.
 */

#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

#include "eloop.h"
#include "input.h"
#include "log.h"
#include "output.h"
#include "terminal.h"
#include "vt.h"

struct app {
	struct ev_eloop *eloop;
	struct ev_signal *sig_term;
	struct ev_signal *sig_int;
	struct ev_signal *sig_chld;
	struct kmscon_symbol_table *st;
	struct kmscon_font_factory *ff;
	struct kmscon_compositor *comp;
	struct kmscon_input *input;
	struct kmscon_vt *vt;
	struct kmscon_terminal *term;
};

static volatile sig_atomic_t terminate;

static void sig_term(struct ev_signal *sig, int signum, void *data)
{
	terminate = 1;
}

static void sig_chld(struct ev_signal *sig, int signum, void *data)
{
	pid_t pid;
	int status;

	/*
	 * If multiple children exit at the same time, signalfd would put them
	 * all in one event. So we reap in a loop.
	 */
	while (1) {
		pid = waitpid(-1, &status, WNOHANG);
		if (pid == -1) {
			if (errno != ECHILD)
				log_warn("test: cannot wait on child: %m\n");
			break;
		} else if (pid == 0) {
			break;
		} else if (WIFEXITED(status)) {
			if (WEXITSTATUS(status) != 0)
				log_info("test: child %d exited with status "
					"%d\n", pid, WEXITSTATUS(status));
			else
				log_debug("test: child %d exited "
						"successfully\n", pid);
		} else if (WIFSIGNALED(status)) {
			log_debug("test: child %d exited by signal %d\n", pid,
					WTERMSIG(status));
		}
	}
}

static void terminal_closed(struct kmscon_terminal *term, void *data)
{
	kmscon_terminal_close(term);
	terminate = 1;
}

static void read_input(struct kmscon_input *input,
				struct kmscon_input_event *ev, void *data)
{
	struct app *app = data;
	int ret;

	ret = kmscon_terminal_input(app->term, ev);
	if (ret) {
		kmscon_terminal_close(app->term);
		terminate = 1;
	}
}

static void activate_outputs(struct app *app)
{
	struct kmscon_output *iter;
	int ret;

	iter = kmscon_compositor_get_outputs(app->comp);

	for ( ; iter; iter = kmscon_output_next(iter)) {
		if (!kmscon_output_is_active(iter)) {
			ret = kmscon_output_activate(iter, NULL);
			if (ret) {
				log_err("test: cannot activate output: %d\n",
									ret);
				continue;
			}
		}

		ret = kmscon_terminal_add_output(app->term, iter);
		if (ret) {
			log_err("test: cannot assign output: %d\n", ret);
			continue;
		}
	}
}

static bool vt_switch(struct kmscon_vt *vt, int action, void *data)
{
	struct app *app = data;
	int ret;

	if (action == KMSCON_VT_ENTER) {
		ret = kmscon_compositor_wake_up(app->comp);
		if (ret == 0)
			log_info("test: running without active outputs\n");
		else if (ret > 0)
			activate_outputs(app);

		kmscon_input_wake_up(app->input);
	} else if (action == KMSCON_VT_LEAVE) {
		kmscon_input_sleep(app->input);
		kmscon_terminal_rm_all_outputs(app->term);
		kmscon_compositor_sleep(app->comp);
	}

	return true;
}

static void destroy_app(struct app *app)
{
	kmscon_terminal_unref(app->term);
	kmscon_vt_unref(app->vt);
	kmscon_input_unref(app->input);
	kmscon_compositor_unref(app->comp);
	kmscon_font_factory_unref(app->ff);
	kmscon_symbol_table_unref(app->st);
	ev_eloop_rm_signal(app->sig_chld);
	ev_eloop_rm_signal(app->sig_int);
	ev_eloop_rm_signal(app->sig_term);
	ev_eloop_unref(app->eloop);
}

static int setup_app(struct app *app)
{
	int ret;

	ret = ev_eloop_new(&app->eloop);
	if (ret)
		goto err_loop;

	ret = ev_eloop_new_signal(app->eloop, &app->sig_term, SIGTERM,
							sig_term, NULL);
	if (ret)
		goto err_loop;

	ret = ev_eloop_new_signal(app->eloop, &app->sig_int, SIGINT,
							sig_term, NULL);
	if (ret)
		goto err_loop;

	ret = ev_eloop_new_signal(app->eloop, &app->sig_chld, SIGCHLD,
							sig_chld, NULL);
	if (ret)
		goto err_loop;

	ret = kmscon_symbol_table_new(&app->st);
	if (ret)
		goto err_loop;

	ret = kmscon_compositor_new(&app->comp);
	if (ret)
		goto err_loop;

	ret = kmscon_compositor_use(app->comp);
	if (ret)
		goto err_loop;

	ret = kmscon_font_factory_new(&app->ff, app->st, app->comp);
	if (ret)
		goto err_loop;

	ret = kmscon_input_new(&app->input);
	if (ret)
		goto err_loop;

	ret = kmscon_vt_new(&app->vt, vt_switch, app);
	if (ret)
		goto err_loop;

	ret = kmscon_vt_open(app->vt, KMSCON_VT_NEW, app->eloop);
	if (ret)
		goto err_loop;

	ret = kmscon_terminal_new(&app->term, app->eloop, app->ff,
							app->comp, app->st);
	if (ret)
		goto err_loop;

	ret = kmscon_terminal_open(app->term, terminal_closed, app);
	if (ret)
		goto err_loop;

	ret = kmscon_input_connect_eloop(app->input, app->eloop, read_input,
									app);
	if (ret)
		goto err_loop;

	return 0;

err_loop:
	destroy_app(app);
	return ret;
}

int main(int argc, char **argv)
{
	struct app app;
	int ret;

	log_info("test: starting\n");
	memset(&app, 0, sizeof(app));

	ret = setup_app(&app);
	if (ret)
		goto err_out;

	log_info("test: starting main-loop\n");

	while (!terminate) {
		ret = ev_eloop_dispatch(app.eloop, -1);
		if (ret)
			break;
	}

	log_info("test: stopping main-loop\n");

err_out:
	destroy_app(&app);

	if (ret) {
		log_err("test: failed with: %d\n", ret);
		return EXIT_FAILURE;
	} else {
		log_info("test: terminating\n");
		return EXIT_SUCCESS;
	}
}