blob: 515063248a30e2dd5ba23b79ab3d8c102f64bc64 [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 }
160 cairo_destroy(cr);
161
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500162 window_copy_surface(terminal->window,
163 &rectangle,
164 terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500165}
166
167static void
168terminal_draw(struct terminal *terminal)
169{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500170 struct rectangle rectangle;
171 cairo_surface_t *surface;
172 cairo_font_extents_t extents;
173 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500174 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500175
176 window_get_child_rectangle(terminal->window, &rectangle);
177
178 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
179 cr = cairo_create(surface);
180 cairo_select_font_face (cr, "mono",
181 CAIRO_FONT_SLANT_NORMAL,
182 CAIRO_FONT_WEIGHT_NORMAL);
183 cairo_set_font_size(cr, 14);
184 cairo_font_extents(cr, &extents);
185 cairo_destroy(cr);
186 cairo_surface_destroy(surface);
187
188 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
189 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
190 terminal_resize(terminal, width, height);
191
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500192 if (!terminal->fullscreen) {
193 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
194 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
195 window_set_child_size(terminal->window, &rectangle);
196 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500197
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500198 window_draw(terminal->window);
199 terminal_draw_contents(terminal);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500200 wl_compositor_commit(terminal->compositor, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500201}
202
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500203static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500204idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500205{
206 struct terminal *terminal = data;
207
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500208 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500209
210 return FALSE;
211}
212
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500213#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500214#define STATE_ESCAPE 1
215
216static void
217terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500218
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500219static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500220terminal_schedule_redraw(struct terminal *terminal)
221{
222 if (!terminal->redraw_scheduled) {
223 g_idle_add(idle_redraw, terminal);
224 terminal->redraw_scheduled = 1;
225 } else {
226 terminal->redraw_pending = 1;
227 }
228}
229
230static void
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500231terminal_data(struct terminal *terminal, const char *data, size_t length);
232
233static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500234handle_escape(struct terminal *terminal)
235{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500236 char *row, *p;
237 int i, count;
238 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500239
240 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500241 i = 0;
242 p = &terminal->escape[2];
243 while ((isdigit(*p) || *p == ';') && i < 10) {
244 if (*p == ';') {
245 p++;
246 i++;
247 } else {
248 args[i] = strtol(p, &p, 10);
249 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500250 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500251 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500252
253 switch (*p) {
254 case 'A':
255 count = set[0] ? args[0] : 1;
256 if (terminal->row - count >= 0)
257 terminal->row -= count;
258 else
259 terminal->row = 0;
260 break;
261 case 'B':
262 count = set[0] ? args[0] : 1;
263 if (terminal->row + count < terminal->height)
264 terminal->row += count;
265 else
266 terminal->row = terminal->height;
267 break;
268 case 'C':
269 count = set[0] ? args[0] : 1;
270 if (terminal->column + count < terminal->width)
271 terminal->column += count;
272 else
273 terminal->column = terminal->width;
274 break;
275 case 'D':
276 count = set[0] ? args[0] : 1;
277 if (terminal->column - count >= 0)
278 terminal->column -= count;
279 else
280 terminal->column = 0;
281 break;
282 case 'J':
283 row = terminal_get_row(terminal, terminal->row);
284 memset(&row[terminal->column], 0, terminal->width - terminal->column);
285 for (i = terminal->row + 1; i < terminal->height; i++)
286 memset(terminal_get_row(terminal, i), 0, terminal->width);
287 break;
288 case 'G':
289 if (set[0])
290 terminal->column = args[0] - 1;
291 break;
292 case 'H':
293 case 'f':
294 terminal->row = set[0] ? args[0] - 1 : 0;
295 terminal->column = set[1] ? args[1] - 1 : 0;
296 break;
297 case 'K':
298 row = terminal_get_row(terminal, terminal->row);
299 memset(&row[terminal->column], 0, terminal->width - terminal->column);
300 break;
301 case 'm':
302 /* color, blink, bold etc*/
303 break;
304 case '?':
305 if (strcmp(p, "?25l") == 0) {
306 /* hide cursor */
307 } else if (strcmp(p, "?25h") == 0) {
308 /* show cursor */
309 }
310 break;
311 default:
312 terminal_data(terminal,
313 terminal->escape + 1,
314 terminal->escape_length - 2);
315 break;
316 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500317}
318
319static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500320terminal_data(struct terminal *terminal, const char *data, size_t length)
321{
322 int i;
323 char *row;
324
325 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500326 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500327
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500328 if (terminal->state == STATE_ESCAPE) {
329 terminal->escape[terminal->escape_length++] = data[i];
330 if (terminal->escape_length == 2 && data[i] != '[') {
331 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500332 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500333 goto cancel_escape;
334 }
335
336 if (isalpha(data[i])) {
337 terminal->state = STATE_NORMAL;
338 handle_escape(terminal);
339 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500340 continue;
341 }
342
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500343 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500344 switch (data[i]) {
345 case '\r':
346 terminal->column = 0;
347 break;
348 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500349 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500350 if (terminal->row + 1 < terminal->height) {
351 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500352 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500353 terminal->start++;
354 if (terminal->start == terminal->height)
355 terminal->start = 0;
356 memset(terminal_get_row(terminal, terminal->row),
357 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500358 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500359
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500360 break;
361 case '\t':
362 memset(&row[terminal->column], ' ', -terminal->column & 7);
363 terminal->column = (terminal->column + 7) & ~7;
364 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500365 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500366 terminal->state = STATE_ESCAPE;
367 terminal->escape[0] = '\e';
368 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500369 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500370 case '\b':
371 if (terminal->column > 0)
372 terminal->column--;
373 break;
374 case '\a':
375 /* Bell */
376 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500377 default:
378 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500379 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500380 break;
381 }
382 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500383
384 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500385}
386
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500387static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500388resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500389{
390 struct terminal *terminal = data;
391
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500392 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500393}
394
395static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500396handle_acknowledge(void *data,
397 struct wl_compositor *compositor,
398 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500399{
400 struct terminal *terminal = data;
401
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500402 terminal->redraw_scheduled = 0;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500403 if (key == 0)
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500404 cairo_surface_destroy(terminal->surface);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500405
406 if (terminal->redraw_pending) {
407 terminal->redraw_pending = 0;
408 terminal_schedule_redraw(terminal);
409 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500410}
411
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500412static void
413handle_frame(void *data,
414 struct wl_compositor *compositor,
415 uint32_t frame, uint32_t timestamp)
416{
417}
418
419static const struct wl_compositor_listener compositor_listener = {
420 handle_acknowledge,
421 handle_frame,
422};
423
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500424static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500425key_handler(struct window *window, uint32_t key, uint32_t unicode,
426 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500427{
428 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500429 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500430
431 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500432 case KEY_F11:
433 if (!state)
434 break;
435 terminal->fullscreen ^= 1;
436 window_set_fullscreen(window, terminal->fullscreen);
437 terminal_schedule_redraw(terminal);
438 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500439 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500440 if (state && unicode)
441 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500442 break;
443 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500444}
445
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500446static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500447terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500448{
449 struct terminal *terminal;
450
451 terminal = malloc(sizeof *terminal);
452 if (terminal == NULL)
453 return terminal;
454
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500455 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500456 terminal->fullscreen = fullscreen;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500457 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500458 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500459 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500460 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500461 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500462
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500463 terminal->compositor = display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500464 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500465 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500466
467 wl_compositor_add_listener(terminal->compositor,
468 &compositor_listener, terminal);
469
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500470 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500471
Kristian Høgsberg22106762008-12-08 13:50:07 -0500472 terminal_draw(terminal);
473
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500474 return terminal;
475}
476
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500477static gboolean
478io_handler(GIOChannel *source,
479 GIOCondition condition,
480 gpointer data)
481{
482 struct terminal *terminal = data;
483 gchar buffer[256];
484 gsize bytes_read;
485 GError *error = NULL;
486
487 g_io_channel_read_chars(source, buffer, sizeof buffer,
488 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500489
490 terminal_data(terminal, buffer, bytes_read);
491
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500492 return TRUE;
493}
494
495static int
496terminal_run(struct terminal *terminal, const char *path)
497{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500498 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500499 pid_t pid;
500
501 pid = forkpty(&master, NULL, NULL, NULL);
502 if (pid == 0) {
503 close(master);
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500504 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500505 if (execl(path, path, NULL)) {
506 printf("exec failed: %m\n");
507 exit(EXIT_FAILURE);
508 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500509 } else if (pid < 0) {
510 fprintf(stderr, "failed to fork and create pty (%m).\n");
511 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500512 }
513
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500514 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500515 terminal->channel = g_io_channel_unix_new(master);
516 fcntl(master, F_SETFL, O_NONBLOCK);
517 g_io_add_watch(terminal->channel, G_IO_IN,
518 io_handler, terminal);
519
520 return 0;
521}
522
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500523static const GOptionEntry option_entries[] = {
524 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
525 &option_fullscreen, "Run in fullscreen mode" },
526 { NULL }
527};
528
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500529int main(int argc, char *argv[])
530{
531 struct wl_display *display;
532 int fd;
533 GMainLoop *loop;
534 GSource *source;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500535 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500536 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500537 GOptionContext *context;
538 GError *error;
539
540 context = g_option_context_new(NULL);
541 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
542 if (!g_option_context_parse(context, &argc, &argv, &error)) {
543 fprintf(stderr, "option parsing failed: %s\n", error->message);
544 exit(EXIT_FAILURE);
545 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500546
547 fd = open(gem_device, O_RDWR);
548 if (fd < 0) {
549 fprintf(stderr, "drm open failed: %m\n");
550 return -1;
551 }
552
553 display = wl_display_create(socket_name, sizeof socket_name);
554 if (display == NULL) {
555 fprintf(stderr, "failed to create display: %m\n");
556 return -1;
557 }
558
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500559 d = display_create(display, fd);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500560 loop = g_main_loop_new(NULL, FALSE);
561 source = wl_glib_source_new(display);
562 g_source_attach(source, NULL);
563
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500564 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500565 if (terminal_run(terminal, "/bin/bash"))
566 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500567
568 g_main_loop_run(loop);
569
570 return 0;
571}