blob: 71c88033f43d397858f2034840d93da17bb591b5 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
34#include <glib.h>
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050035#include <linux/input.h>
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050036#include <cairo-drm.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037
38#include <GL/gl.h>
39#include <eagle.h>
40
41#include "wayland-client.h"
42#include "wayland-glib.h"
43
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044#include "window.h"
45
Kristian Høgsberg0395f302008-12-22 12:14:50 -050046static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047static const char gem_device[] = "/dev/dri/card0";
48static const char socket_name[] = "\0wayland";
49
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050050#define MOD_SHIFT 0x01
51#define MOD_ALT 0x02
52#define MOD_CTRL 0x04
53
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054struct terminal {
55 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050056 struct display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050057 struct wl_compositor *compositor;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -050058 int redraw_scheduled, redraw_pending;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050059 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050060 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050061 int fd, master;
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050062 cairo_surface_t *surface;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050063 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050064 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050065 char escape[64];
66 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050067 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050068 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050069 int fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050070};
71
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050072static char *
73terminal_get_row(struct terminal *terminal, int row)
74{
75 int index;
76
77 index = (row + terminal->start) % terminal->height;
78
79 return &terminal->data[index * (terminal->width + 1)];
80}
81
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050082static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050083terminal_resize(struct terminal *terminal, int width, int height)
84{
85 size_t size;
86 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050087 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050088
89 if (terminal->width == width && terminal->height == height)
90 return;
91
92 size = (width + 1) * height;
93 data = malloc(size);
94 memset(data, 0, size);
95 if (terminal->data) {
96 if (width > terminal->width)
97 l = terminal->width;
98 else
99 l = width;
100
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500101 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500102 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500103 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500104 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500105 total_rows = terminal->height;
106 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500107 }
108
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500109 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500110 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500111 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500112
113 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500114 }
115
116 terminal->width = width;
117 terminal->height = height;
118 terminal->data = data;
119
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500120 if (terminal->row >= terminal->height)
121 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500122 if (terminal->column >= terminal->width)
123 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500124 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500125}
126
127static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500128terminal_draw_contents(struct terminal *terminal)
129{
130 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500131 cairo_t *cr;
132 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500133 int i, top_margin, side_margin;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500134
135 window_get_child_rectangle(terminal->window, &rectangle);
136
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500137 terminal->surface =
138 window_create_surface(terminal->window, &rectangle);
139 cr = cairo_create(terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500140 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
141 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
142 cairo_paint(cr);
143 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg6e0a2f82008-12-08 14:06:56 -0500144 cairo_set_source_rgba(cr, 0, 0.7, 0, 1);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500145
146 cairo_select_font_face (cr, "mono",
147 CAIRO_FONT_SLANT_NORMAL,
148 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500149 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500150
151 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500152 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
153 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
154
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500155 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500156 cairo_move_to(cr, side_margin,
157 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500158 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500159 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500160
161 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance,
162 top_margin + terminal->row * extents.height);
163 cairo_rel_line_to(cr, extents.max_x_advance, 0);
164 cairo_rel_line_to(cr, 0, extents.height);
165 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
166 cairo_close_path(cr);
167 cairo_fill(cr);
168
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500169 cairo_destroy(cr);
170
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500171 window_copy_surface(terminal->window,
172 &rectangle,
173 terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500174}
175
176static void
177terminal_draw(struct terminal *terminal)
178{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500179 struct rectangle rectangle;
180 cairo_surface_t *surface;
181 cairo_font_extents_t extents;
182 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500183 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500184
185 window_get_child_rectangle(terminal->window, &rectangle);
186
187 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
188 cr = cairo_create(surface);
189 cairo_select_font_face (cr, "mono",
190 CAIRO_FONT_SLANT_NORMAL,
191 CAIRO_FONT_WEIGHT_NORMAL);
192 cairo_set_font_size(cr, 14);
193 cairo_font_extents(cr, &extents);
194 cairo_destroy(cr);
195 cairo_surface_destroy(surface);
196
197 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
198 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
199 terminal_resize(terminal, width, height);
200
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500201 if (!terminal->fullscreen) {
202 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
203 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
204 window_set_child_size(terminal->window, &rectangle);
205 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500206
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500207 window_draw(terminal->window);
208 terminal_draw_contents(terminal);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500209 wl_compositor_commit(terminal->compositor, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500210}
211
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500212static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500213idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500214{
215 struct terminal *terminal = data;
216
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500217 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500218
219 return FALSE;
220}
221
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500222#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500223#define STATE_ESCAPE 1
224
225static void
226terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500227
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500228static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500229terminal_schedule_redraw(struct terminal *terminal)
230{
231 if (!terminal->redraw_scheduled) {
232 g_idle_add(idle_redraw, terminal);
233 terminal->redraw_scheduled = 1;
234 } else {
235 terminal->redraw_pending = 1;
236 }
237}
238
239static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500240handle_escape(struct terminal *terminal)
241{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500242 char *row, *p;
243 int i, count;
244 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500245
246 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500247 i = 0;
248 p = &terminal->escape[2];
249 while ((isdigit(*p) || *p == ';') && i < 10) {
250 if (*p == ';') {
251 p++;
252 i++;
253 } else {
254 args[i] = strtol(p, &p, 10);
255 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500256 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500257 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500258
259 switch (*p) {
260 case 'A':
261 count = set[0] ? args[0] : 1;
262 if (terminal->row - count >= 0)
263 terminal->row -= count;
264 else
265 terminal->row = 0;
266 break;
267 case 'B':
268 count = set[0] ? args[0] : 1;
269 if (terminal->row + count < terminal->height)
270 terminal->row += count;
271 else
272 terminal->row = terminal->height;
273 break;
274 case 'C':
275 count = set[0] ? args[0] : 1;
276 if (terminal->column + count < terminal->width)
277 terminal->column += count;
278 else
279 terminal->column = terminal->width;
280 break;
281 case 'D':
282 count = set[0] ? args[0] : 1;
283 if (terminal->column - count >= 0)
284 terminal->column -= count;
285 else
286 terminal->column = 0;
287 break;
288 case 'J':
289 row = terminal_get_row(terminal, terminal->row);
290 memset(&row[terminal->column], 0, terminal->width - terminal->column);
291 for (i = terminal->row + 1; i < terminal->height; i++)
292 memset(terminal_get_row(terminal, i), 0, terminal->width);
293 break;
294 case 'G':
295 if (set[0])
296 terminal->column = args[0] - 1;
297 break;
298 case 'H':
299 case 'f':
300 terminal->row = set[0] ? args[0] - 1 : 0;
301 terminal->column = set[1] ? args[1] - 1 : 0;
302 break;
303 case 'K':
304 row = terminal_get_row(terminal, terminal->row);
305 memset(&row[terminal->column], 0, terminal->width - terminal->column);
306 break;
307 case 'm':
308 /* color, blink, bold etc*/
309 break;
310 case '?':
311 if (strcmp(p, "?25l") == 0) {
312 /* hide cursor */
313 } else if (strcmp(p, "?25h") == 0) {
314 /* show cursor */
315 }
316 break;
317 default:
318 terminal_data(terminal,
319 terminal->escape + 1,
320 terminal->escape_length - 2);
321 break;
322 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500323}
324
325static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500326terminal_data(struct terminal *terminal, const char *data, size_t length)
327{
328 int i;
329 char *row;
330
331 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500332 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500333
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500334 if (terminal->state == STATE_ESCAPE) {
335 terminal->escape[terminal->escape_length++] = data[i];
336 if (terminal->escape_length == 2 && data[i] != '[') {
337 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500338 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500339 goto cancel_escape;
340 }
341
342 if (isalpha(data[i])) {
343 terminal->state = STATE_NORMAL;
344 handle_escape(terminal);
345 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500346 continue;
347 }
348
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500349 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500350 switch (data[i]) {
351 case '\r':
352 terminal->column = 0;
353 break;
354 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500355 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500356 if (terminal->row + 1 < terminal->height) {
357 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500358 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500359 terminal->start++;
360 if (terminal->start == terminal->height)
361 terminal->start = 0;
362 memset(terminal_get_row(terminal, terminal->row),
363 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500364 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500365
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500366 break;
367 case '\t':
368 memset(&row[terminal->column], ' ', -terminal->column & 7);
369 terminal->column = (terminal->column + 7) & ~7;
370 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500371 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500372 terminal->state = STATE_ESCAPE;
373 terminal->escape[0] = '\e';
374 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500375 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500376 case '\b':
377 if (terminal->column > 0)
378 terminal->column--;
379 break;
380 case '\a':
381 /* Bell */
382 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500383 default:
384 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500385 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500386 break;
387 }
388 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500389
390 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500391}
392
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500393static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500394resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500395{
396 struct terminal *terminal = data;
397
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500398 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500399}
400
401static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500402handle_acknowledge(void *data,
403 struct wl_compositor *compositor,
404 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500405{
406 struct terminal *terminal = data;
407
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500408 terminal->redraw_scheduled = 0;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500409 if (key == 0)
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500410 cairo_surface_destroy(terminal->surface);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500411
412 if (terminal->redraw_pending) {
413 terminal->redraw_pending = 0;
414 terminal_schedule_redraw(terminal);
415 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500416}
417
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500418static void
419handle_frame(void *data,
420 struct wl_compositor *compositor,
421 uint32_t frame, uint32_t timestamp)
422{
423}
424
425static const struct wl_compositor_listener compositor_listener = {
426 handle_acknowledge,
427 handle_frame,
428};
429
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500430static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500431key_handler(struct window *window, uint32_t key, uint32_t unicode,
432 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500433{
434 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500435 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500436
437 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500438 case KEY_F11:
439 if (!state)
440 break;
441 terminal->fullscreen ^= 1;
442 window_set_fullscreen(window, terminal->fullscreen);
443 terminal_schedule_redraw(terminal);
444 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500445 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500446 if (state && unicode)
447 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500448 break;
449 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500450}
451
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500452static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500453terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500454{
455 struct terminal *terminal;
456
457 terminal = malloc(sizeof *terminal);
458 if (terminal == NULL)
459 return terminal;
460
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500461 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500462 terminal->fullscreen = fullscreen;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500463 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500464 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500465 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500466 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500467 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500468
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500469 terminal->compositor = display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500470 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500471 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500472
473 wl_compositor_add_listener(terminal->compositor,
474 &compositor_listener, terminal);
475
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500476 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500477
Kristian Høgsberg22106762008-12-08 13:50:07 -0500478 terminal_draw(terminal);
479
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500480 return terminal;
481}
482
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500483static gboolean
484io_handler(GIOChannel *source,
485 GIOCondition condition,
486 gpointer data)
487{
488 struct terminal *terminal = data;
489 gchar buffer[256];
490 gsize bytes_read;
491 GError *error = NULL;
492
493 g_io_channel_read_chars(source, buffer, sizeof buffer,
494 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500495
496 terminal_data(terminal, buffer, bytes_read);
497
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500498 return TRUE;
499}
500
501static int
502terminal_run(struct terminal *terminal, const char *path)
503{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500504 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500505 pid_t pid;
506
507 pid = forkpty(&master, NULL, NULL, NULL);
508 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500509 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500510 if (execl(path, path, NULL)) {
511 printf("exec failed: %m\n");
512 exit(EXIT_FAILURE);
513 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500514 } else if (pid < 0) {
515 fprintf(stderr, "failed to fork and create pty (%m).\n");
516 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500517 }
518
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500519 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500520 terminal->channel = g_io_channel_unix_new(master);
521 fcntl(master, F_SETFL, O_NONBLOCK);
522 g_io_add_watch(terminal->channel, G_IO_IN,
523 io_handler, terminal);
524
525 return 0;
526}
527
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500528static const GOptionEntry option_entries[] = {
529 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
530 &option_fullscreen, "Run in fullscreen mode" },
531 { NULL }
532};
533
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500534int main(int argc, char *argv[])
535{
536 struct wl_display *display;
537 int fd;
538 GMainLoop *loop;
539 GSource *source;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500540 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500541 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500542 GOptionContext *context;
543 GError *error;
544
545 context = g_option_context_new(NULL);
546 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
547 if (!g_option_context_parse(context, &argc, &argv, &error)) {
548 fprintf(stderr, "option parsing failed: %s\n", error->message);
549 exit(EXIT_FAILURE);
550 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500551
552 fd = open(gem_device, O_RDWR);
553 if (fd < 0) {
554 fprintf(stderr, "drm open failed: %m\n");
555 return -1;
556 }
557
558 display = wl_display_create(socket_name, sizeof socket_name);
559 if (display == NULL) {
560 fprintf(stderr, "failed to create display: %m\n");
561 return -1;
562 }
563
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500564 d = display_create(display, fd);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500565 loop = g_main_loop_new(NULL, FALSE);
566 source = wl_glib_source_new(display);
567 g_source_attach(source, NULL);
568
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500569 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500570 if (terminal_run(terminal, "/bin/bash"))
571 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500572
573 g_main_loop_run(loop);
574
575 return 0;
576}