blob: 2b1d06ba9d5c5ac3fc07371d4e6b58f7f645202b [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øgsberg3c248cc2009-02-22 23:01:35 -050070 int focused;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050071};
72
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050073static char *
74terminal_get_row(struct terminal *terminal, int row)
75{
76 int index;
77
78 index = (row + terminal->start) % terminal->height;
79
80 return &terminal->data[index * (terminal->width + 1)];
81}
82
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050083static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050084terminal_resize(struct terminal *terminal, int width, int height)
85{
86 size_t size;
87 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050088 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050089
90 if (terminal->width == width && terminal->height == height)
91 return;
92
93 size = (width + 1) * height;
94 data = malloc(size);
95 memset(data, 0, size);
96 if (terminal->data) {
97 if (width > terminal->width)
98 l = terminal->width;
99 else
100 l = width;
101
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500102 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500103 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500104 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500105 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500106 total_rows = terminal->height;
107 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500108 }
109
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500110 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500111 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500112 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500113
114 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500115 }
116
117 terminal->width = width;
118 terminal->height = height;
119 terminal->data = data;
120
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500121 if (terminal->row >= terminal->height)
122 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500123 if (terminal->column >= terminal->width)
124 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500125 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500126}
127
128static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500129terminal_draw_contents(struct terminal *terminal)
130{
131 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500132 cairo_t *cr;
133 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500134 int i, top_margin, side_margin;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500135 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500136
137 window_get_child_rectangle(terminal->window, &rectangle);
138
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500139 terminal->surface =
140 window_create_surface(terminal->window, &rectangle);
141 cr = cairo_create(terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500142 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
143 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
144 cairo_paint(cr);
145 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg6e0a2f82008-12-08 14:06:56 -0500146 cairo_set_source_rgba(cr, 0, 0.7, 0, 1);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500147
148 cairo_select_font_face (cr, "mono",
149 CAIRO_FONT_SLANT_NORMAL,
150 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500151 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500152
153 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500154 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
155 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
156
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500157 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500158 cairo_move_to(cr, side_margin,
159 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500160 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500161 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500162
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500163 d = terminal->focused ? 0 : 0.5;
164
165 cairo_set_line_width(cr, 1);
166 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
167 top_margin + terminal->row * extents.height + d);
168 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
169 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
170 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500171 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500172
173 if (terminal->focused)
174 cairo_fill(cr);
175 else
176 cairo_stroke(cr);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500177
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500178 cairo_destroy(cr);
179
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500180 window_copy_surface(terminal->window,
181 &rectangle,
182 terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500183}
184
185static void
186terminal_draw(struct terminal *terminal)
187{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500188 struct rectangle rectangle;
189 cairo_surface_t *surface;
190 cairo_font_extents_t extents;
191 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500192 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500193
194 window_get_child_rectangle(terminal->window, &rectangle);
195
196 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
197 cr = cairo_create(surface);
198 cairo_select_font_face (cr, "mono",
199 CAIRO_FONT_SLANT_NORMAL,
200 CAIRO_FONT_WEIGHT_NORMAL);
201 cairo_set_font_size(cr, 14);
202 cairo_font_extents(cr, &extents);
203 cairo_destroy(cr);
204 cairo_surface_destroy(surface);
205
206 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
207 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
208 terminal_resize(terminal, width, height);
209
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500210 if (!terminal->fullscreen) {
211 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
212 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
213 window_set_child_size(terminal->window, &rectangle);
214 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500215
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500216 window_draw(terminal->window);
217 terminal_draw_contents(terminal);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500218 wl_compositor_commit(terminal->compositor, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500219}
220
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500221static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500222idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500223{
224 struct terminal *terminal = data;
225
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500226 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500227
228 return FALSE;
229}
230
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500231#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500232#define STATE_ESCAPE 1
233
234static void
235terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500236
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500237static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500238terminal_schedule_redraw(struct terminal *terminal)
239{
240 if (!terminal->redraw_scheduled) {
241 g_idle_add(idle_redraw, terminal);
242 terminal->redraw_scheduled = 1;
243 } else {
244 terminal->redraw_pending = 1;
245 }
246}
247
248static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500249handle_escape(struct terminal *terminal)
250{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500251 char *row, *p;
252 int i, count;
253 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500254
255 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500256 i = 0;
257 p = &terminal->escape[2];
258 while ((isdigit(*p) || *p == ';') && i < 10) {
259 if (*p == ';') {
260 p++;
261 i++;
262 } else {
263 args[i] = strtol(p, &p, 10);
264 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500265 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500266 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500267
268 switch (*p) {
269 case 'A':
270 count = set[0] ? args[0] : 1;
271 if (terminal->row - count >= 0)
272 terminal->row -= count;
273 else
274 terminal->row = 0;
275 break;
276 case 'B':
277 count = set[0] ? args[0] : 1;
278 if (terminal->row + count < terminal->height)
279 terminal->row += count;
280 else
281 terminal->row = terminal->height;
282 break;
283 case 'C':
284 count = set[0] ? args[0] : 1;
285 if (terminal->column + count < terminal->width)
286 terminal->column += count;
287 else
288 terminal->column = terminal->width;
289 break;
290 case 'D':
291 count = set[0] ? args[0] : 1;
292 if (terminal->column - count >= 0)
293 terminal->column -= count;
294 else
295 terminal->column = 0;
296 break;
297 case 'J':
298 row = terminal_get_row(terminal, terminal->row);
299 memset(&row[terminal->column], 0, terminal->width - terminal->column);
300 for (i = terminal->row + 1; i < terminal->height; i++)
301 memset(terminal_get_row(terminal, i), 0, terminal->width);
302 break;
303 case 'G':
304 if (set[0])
305 terminal->column = args[0] - 1;
306 break;
307 case 'H':
308 case 'f':
309 terminal->row = set[0] ? args[0] - 1 : 0;
310 terminal->column = set[1] ? args[1] - 1 : 0;
311 break;
312 case 'K':
313 row = terminal_get_row(terminal, terminal->row);
314 memset(&row[terminal->column], 0, terminal->width - terminal->column);
315 break;
316 case 'm':
317 /* color, blink, bold etc*/
318 break;
319 case '?':
320 if (strcmp(p, "?25l") == 0) {
321 /* hide cursor */
322 } else if (strcmp(p, "?25h") == 0) {
323 /* show cursor */
324 }
325 break;
326 default:
327 terminal_data(terminal,
328 terminal->escape + 1,
329 terminal->escape_length - 2);
330 break;
331 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500332}
333
334static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500335terminal_data(struct terminal *terminal, const char *data, size_t length)
336{
337 int i;
338 char *row;
339
340 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500341 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500342
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500343 if (terminal->state == STATE_ESCAPE) {
344 terminal->escape[terminal->escape_length++] = data[i];
345 if (terminal->escape_length == 2 && data[i] != '[') {
346 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500347 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500348 goto cancel_escape;
349 }
350
351 if (isalpha(data[i])) {
352 terminal->state = STATE_NORMAL;
353 handle_escape(terminal);
354 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500355 continue;
356 }
357
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500358 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500359 switch (data[i]) {
360 case '\r':
361 terminal->column = 0;
362 break;
363 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500364 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500365 if (terminal->row + 1 < terminal->height) {
366 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500367 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500368 terminal->start++;
369 if (terminal->start == terminal->height)
370 terminal->start = 0;
371 memset(terminal_get_row(terminal, terminal->row),
372 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500373 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500374
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500375 break;
376 case '\t':
377 memset(&row[terminal->column], ' ', -terminal->column & 7);
378 terminal->column = (terminal->column + 7) & ~7;
379 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500380 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500381 terminal->state = STATE_ESCAPE;
382 terminal->escape[0] = '\e';
383 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500384 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500385 case '\b':
386 if (terminal->column > 0)
387 terminal->column--;
388 break;
389 case '\a':
390 /* Bell */
391 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500392 default:
393 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500394 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500395 break;
396 }
397 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500398
399 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500400}
401
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500402static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500403resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500404{
405 struct terminal *terminal = data;
406
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500407 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500408}
409
410static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500411handle_acknowledge(void *data,
412 struct wl_compositor *compositor,
413 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500414{
415 struct terminal *terminal = data;
416
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500417 terminal->redraw_scheduled = 0;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500418 if (key == 0)
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500419 cairo_surface_destroy(terminal->surface);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500420
421 if (terminal->redraw_pending) {
422 terminal->redraw_pending = 0;
423 terminal_schedule_redraw(terminal);
424 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500425}
426
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500427static void
428handle_frame(void *data,
429 struct wl_compositor *compositor,
430 uint32_t frame, uint32_t timestamp)
431{
432}
433
434static const struct wl_compositor_listener compositor_listener = {
435 handle_acknowledge,
436 handle_frame,
437};
438
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500439static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500440key_handler(struct window *window, uint32_t key, uint32_t unicode,
441 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500442{
443 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500444 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500445
446 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500447 case KEY_F11:
448 if (!state)
449 break;
450 terminal->fullscreen ^= 1;
451 window_set_fullscreen(window, terminal->fullscreen);
452 terminal_schedule_redraw(terminal);
453 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500454 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500455 if (state && unicode)
456 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500457 break;
458 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500459}
460
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500461static void
462keyboard_focus_handler(struct window *window,
463 struct wl_input_device *device, void *data)
464{
465 struct terminal *terminal = data;
466
467 terminal->focused = (device != NULL);
468 terminal_schedule_redraw(terminal);
469}
470
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500471static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500472terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500473{
474 struct terminal *terminal;
475
476 terminal = malloc(sizeof *terminal);
477 if (terminal == NULL)
478 return terminal;
479
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500480 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500481 terminal->fullscreen = fullscreen;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500482 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500483 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500484 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500485 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500486 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500487
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500488 terminal->compositor = display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500489 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500490 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500491
492 wl_compositor_add_listener(terminal->compositor,
493 &compositor_listener, terminal);
494
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500495 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500496 window_set_keyboard_focus_handler(terminal->window,
497 keyboard_focus_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500498
Kristian Høgsberg22106762008-12-08 13:50:07 -0500499 terminal_draw(terminal);
500
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500501 return terminal;
502}
503
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500504static gboolean
505io_handler(GIOChannel *source,
506 GIOCondition condition,
507 gpointer data)
508{
509 struct terminal *terminal = data;
510 gchar buffer[256];
511 gsize bytes_read;
512 GError *error = NULL;
513
514 g_io_channel_read_chars(source, buffer, sizeof buffer,
515 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500516
517 terminal_data(terminal, buffer, bytes_read);
518
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500519 return TRUE;
520}
521
522static int
523terminal_run(struct terminal *terminal, const char *path)
524{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500525 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500526 pid_t pid;
527
528 pid = forkpty(&master, NULL, NULL, NULL);
529 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500530 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500531 if (execl(path, path, NULL)) {
532 printf("exec failed: %m\n");
533 exit(EXIT_FAILURE);
534 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500535 } else if (pid < 0) {
536 fprintf(stderr, "failed to fork and create pty (%m).\n");
537 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500538 }
539
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500540 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500541 terminal->channel = g_io_channel_unix_new(master);
542 fcntl(master, F_SETFL, O_NONBLOCK);
543 g_io_add_watch(terminal->channel, G_IO_IN,
544 io_handler, terminal);
545
546 return 0;
547}
548
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500549static const GOptionEntry option_entries[] = {
550 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
551 &option_fullscreen, "Run in fullscreen mode" },
552 { NULL }
553};
554
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500555int main(int argc, char *argv[])
556{
557 struct wl_display *display;
558 int fd;
559 GMainLoop *loop;
560 GSource *source;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500561 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500562 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500563 GOptionContext *context;
564 GError *error;
565
566 context = g_option_context_new(NULL);
567 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
568 if (!g_option_context_parse(context, &argc, &argv, &error)) {
569 fprintf(stderr, "option parsing failed: %s\n", error->message);
570 exit(EXIT_FAILURE);
571 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500572
573 fd = open(gem_device, O_RDWR);
574 if (fd < 0) {
575 fprintf(stderr, "drm open failed: %m\n");
576 return -1;
577 }
578
579 display = wl_display_create(socket_name, sizeof socket_name);
580 if (display == NULL) {
581 fprintf(stderr, "failed to create display: %m\n");
582 return -1;
583 }
584
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500585 d = display_create(display, fd);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500586 loop = g_main_loop_new(NULL, FALSE);
587 source = wl_glib_source_new(display);
588 g_source_attach(source, NULL);
589
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500590 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500591 if (terminal_run(terminal, "/bin/bash"))
592 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500593
594 g_main_loop_run(loop);
595
596 return 0;
597}