blob: b4800d5fe5951ee73de6376048c0d24ec04053ec [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
Kristian Høgsberg12308a42009-09-28 13:08:50 -040038#include "wayland-util.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "wayland-client.h"
40#include "wayland-glib.h"
41
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042#include "window.h"
43
Kristian Høgsberg0395f302008-12-22 12:14:50 -050044static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050045static const char gem_device[] = "/dev/dri/card0";
46static const char socket_name[] = "\0wayland";
47
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050048#define MOD_SHIFT 0x01
49#define MOD_ALT 0x02
50#define MOD_CTRL 0x04
51
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052struct terminal {
53 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050054 struct display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050055 struct wl_compositor *compositor;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -050056 int redraw_scheduled, redraw_pending;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050057 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050058 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050059 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050060 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050061 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050062 char escape[64];
63 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050064 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050065 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050066 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050067 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -040068 struct color_scheme *color_scheme;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069};
70
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050071static char *
72terminal_get_row(struct terminal *terminal, int row)
73{
74 int index;
75
76 index = (row + terminal->start) % terminal->height;
77
78 return &terminal->data[index * (terminal->width + 1)];
79}
80
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050081static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050082terminal_resize(struct terminal *terminal, int width, int height)
83{
84 size_t size;
85 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050086 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050087
88 if (terminal->width == width && terminal->height == height)
89 return;
90
91 size = (width + 1) * height;
92 data = malloc(size);
93 memset(data, 0, size);
94 if (terminal->data) {
95 if (width > terminal->width)
96 l = terminal->width;
97 else
98 l = width;
99
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500100 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500101 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500102 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500103 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500104 total_rows = terminal->height;
105 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500106 }
107
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500108 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500109 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500110 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500111
112 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500113 }
114
115 terminal->width = width;
116 terminal->height = height;
117 terminal->data = data;
118
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500119 if (terminal->row >= terminal->height)
120 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500121 if (terminal->column >= terminal->width)
122 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500123 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500124}
125
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400126struct color_scheme { struct { double r, g, b, a; } fg, bg; }
127 matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
128 jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
129
Kristian Høgsberg22106762008-12-08 13:50:07 -0500130static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500131terminal_draw_contents(struct terminal *terminal)
132{
133 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500134 cairo_t *cr;
135 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500136 int i, top_margin, side_margin;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500137 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500138 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500139
140 window_get_child_rectangle(terminal->window, &rectangle);
141
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500142 surface = window_create_surface(terminal->window, &rectangle);
143 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500144 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400145 cairo_set_source_rgba(cr,
146 terminal->color_scheme->bg.r,
147 terminal->color_scheme->bg.g,
148 terminal->color_scheme->bg.b,
149 terminal->color_scheme->bg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500150 cairo_paint(cr);
151 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400152 cairo_set_source_rgba(cr,
153 terminal->color_scheme->fg.r,
154 terminal->color_scheme->fg.g,
155 terminal->color_scheme->fg.b,
156 terminal->color_scheme->fg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500157
158 cairo_select_font_face (cr, "mono",
159 CAIRO_FONT_SLANT_NORMAL,
160 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500161 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500162
163 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500164 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
165 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
166
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500167 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500168 cairo_move_to(cr, side_margin,
169 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500170 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500171 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500172
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500173 d = terminal->focused ? 0 : 0.5;
174
175 cairo_set_line_width(cr, 1);
176 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
177 top_margin + terminal->row * extents.height + d);
178 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
179 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
180 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500181 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500182
183 if (terminal->focused)
184 cairo_fill(cr);
185 else
186 cairo_stroke(cr);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500187
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500188 cairo_destroy(cr);
189
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500190 window_copy_surface(terminal->window,
191 &rectangle,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500192 surface);
193
194 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500195}
196
197static void
198terminal_draw(struct terminal *terminal)
199{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500200 struct rectangle rectangle;
201 cairo_surface_t *surface;
202 cairo_font_extents_t extents;
203 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500204 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500205
206 window_get_child_rectangle(terminal->window, &rectangle);
207
208 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
209 cr = cairo_create(surface);
210 cairo_select_font_face (cr, "mono",
211 CAIRO_FONT_SLANT_NORMAL,
212 CAIRO_FONT_WEIGHT_NORMAL);
213 cairo_set_font_size(cr, 14);
214 cairo_font_extents(cr, &extents);
215 cairo_destroy(cr);
216 cairo_surface_destroy(surface);
217
218 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
219 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
220 terminal_resize(terminal, width, height);
221
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500222 if (!terminal->fullscreen) {
223 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
224 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
225 window_set_child_size(terminal->window, &rectangle);
226 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500227
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500228 window_draw(terminal->window);
229 terminal_draw_contents(terminal);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500230 window_commit(terminal->window, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500231}
232
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500233static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500234idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500235{
236 struct terminal *terminal = data;
237
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500238 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500239
240 return FALSE;
241}
242
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500243#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500244#define STATE_ESCAPE 1
245
246static void
247terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500248
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500249static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500250terminal_schedule_redraw(struct terminal *terminal)
251{
252 if (!terminal->redraw_scheduled) {
253 g_idle_add(idle_redraw, terminal);
254 terminal->redraw_scheduled = 1;
255 } else {
256 terminal->redraw_pending = 1;
257 }
258}
259
260static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500261handle_escape(struct terminal *terminal)
262{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500263 char *row, *p;
264 int i, count;
265 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500266
267 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500268 i = 0;
269 p = &terminal->escape[2];
270 while ((isdigit(*p) || *p == ';') && i < 10) {
271 if (*p == ';') {
272 p++;
273 i++;
274 } else {
275 args[i] = strtol(p, &p, 10);
276 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500277 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500278 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500279
280 switch (*p) {
281 case 'A':
282 count = set[0] ? args[0] : 1;
283 if (terminal->row - count >= 0)
284 terminal->row -= count;
285 else
286 terminal->row = 0;
287 break;
288 case 'B':
289 count = set[0] ? args[0] : 1;
290 if (terminal->row + count < terminal->height)
291 terminal->row += count;
292 else
293 terminal->row = terminal->height;
294 break;
295 case 'C':
296 count = set[0] ? args[0] : 1;
297 if (terminal->column + count < terminal->width)
298 terminal->column += count;
299 else
300 terminal->column = terminal->width;
301 break;
302 case 'D':
303 count = set[0] ? args[0] : 1;
304 if (terminal->column - count >= 0)
305 terminal->column -= count;
306 else
307 terminal->column = 0;
308 break;
309 case 'J':
310 row = terminal_get_row(terminal, terminal->row);
311 memset(&row[terminal->column], 0, terminal->width - terminal->column);
312 for (i = terminal->row + 1; i < terminal->height; i++)
313 memset(terminal_get_row(terminal, i), 0, terminal->width);
314 break;
315 case 'G':
316 if (set[0])
317 terminal->column = args[0] - 1;
318 break;
319 case 'H':
320 case 'f':
321 terminal->row = set[0] ? args[0] - 1 : 0;
322 terminal->column = set[1] ? args[1] - 1 : 0;
323 break;
324 case 'K':
325 row = terminal_get_row(terminal, terminal->row);
326 memset(&row[terminal->column], 0, terminal->width - terminal->column);
327 break;
328 case 'm':
329 /* color, blink, bold etc*/
330 break;
331 case '?':
332 if (strcmp(p, "?25l") == 0) {
333 /* hide cursor */
334 } else if (strcmp(p, "?25h") == 0) {
335 /* show cursor */
336 }
337 break;
338 default:
339 terminal_data(terminal,
340 terminal->escape + 1,
341 terminal->escape_length - 2);
342 break;
343 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500344}
345
346static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500347terminal_data(struct terminal *terminal, const char *data, size_t length)
348{
349 int i;
350 char *row;
351
352 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500353 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500354
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500355 if (terminal->state == STATE_ESCAPE) {
356 terminal->escape[terminal->escape_length++] = data[i];
357 if (terminal->escape_length == 2 && data[i] != '[') {
358 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500359 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500360 goto cancel_escape;
361 }
362
363 if (isalpha(data[i])) {
364 terminal->state = STATE_NORMAL;
365 handle_escape(terminal);
366 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500367 continue;
368 }
369
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500370 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500371 switch (data[i]) {
372 case '\r':
373 terminal->column = 0;
374 break;
375 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500376 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500377 if (terminal->row + 1 < terminal->height) {
378 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500379 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500380 terminal->start++;
381 if (terminal->start == terminal->height)
382 terminal->start = 0;
383 memset(terminal_get_row(terminal, terminal->row),
384 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500385 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500386
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500387 break;
388 case '\t':
389 memset(&row[terminal->column], ' ', -terminal->column & 7);
390 terminal->column = (terminal->column + 7) & ~7;
391 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500392 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500393 terminal->state = STATE_ESCAPE;
394 terminal->escape[0] = '\e';
395 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500396 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500397 case '\b':
398 if (terminal->column > 0)
399 terminal->column--;
400 break;
401 case '\a':
402 /* Bell */
403 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500404 default:
405 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500406 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500407 break;
408 }
409 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500410
411 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500412}
413
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500414static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500415resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500416{
417 struct terminal *terminal = data;
418
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500419 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500420}
421
422static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500423handle_acknowledge(void *data,
424 struct wl_compositor *compositor,
425 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500426{
427 struct terminal *terminal = data;
428
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500429 terminal->redraw_scheduled = 0;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500430 if (terminal->redraw_pending) {
431 terminal->redraw_pending = 0;
432 terminal_schedule_redraw(terminal);
433 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500434}
435
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500436static void
437handle_frame(void *data,
438 struct wl_compositor *compositor,
439 uint32_t frame, uint32_t timestamp)
440{
441}
442
443static const struct wl_compositor_listener compositor_listener = {
444 handle_acknowledge,
445 handle_frame,
446};
447
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500448static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500449key_handler(struct window *window, uint32_t key, uint32_t unicode,
450 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500451{
452 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500453 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500454
455 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500456 case KEY_F11:
457 if (!state)
458 break;
459 terminal->fullscreen ^= 1;
460 window_set_fullscreen(window, terminal->fullscreen);
461 terminal_schedule_redraw(terminal);
462 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500463 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500464 if (state && unicode)
465 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500466 break;
467 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500468}
469
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500470static void
471keyboard_focus_handler(struct window *window,
472 struct wl_input_device *device, void *data)
473{
474 struct terminal *terminal = data;
475
476 terminal->focused = (device != NULL);
477 terminal_schedule_redraw(terminal);
478}
479
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500480static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500481terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500482{
483 struct terminal *terminal;
484
485 terminal = malloc(sizeof *terminal);
486 if (terminal == NULL)
487 return terminal;
488
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500489 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500490 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400491 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500492 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500493 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500494 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500495 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500496 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500497
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500498 terminal->compositor = display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500499 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500500 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500501
502 wl_compositor_add_listener(terminal->compositor,
503 &compositor_listener, terminal);
504
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500505 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500506 window_set_keyboard_focus_handler(terminal->window,
507 keyboard_focus_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500508
Kristian Høgsberg22106762008-12-08 13:50:07 -0500509 terminal_draw(terminal);
510
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500511 return terminal;
512}
513
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500514static gboolean
515io_handler(GIOChannel *source,
516 GIOCondition condition,
517 gpointer data)
518{
519 struct terminal *terminal = data;
520 gchar buffer[256];
521 gsize bytes_read;
522 GError *error = NULL;
523
524 g_io_channel_read_chars(source, buffer, sizeof buffer,
525 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500526
527 terminal_data(terminal, buffer, bytes_read);
528
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500529 return TRUE;
530}
531
532static int
533terminal_run(struct terminal *terminal, const char *path)
534{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500535 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500536 pid_t pid;
537
538 pid = forkpty(&master, NULL, NULL, NULL);
539 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500540 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500541 if (execl(path, path, NULL)) {
542 printf("exec failed: %m\n");
543 exit(EXIT_FAILURE);
544 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500545 } else if (pid < 0) {
546 fprintf(stderr, "failed to fork and create pty (%m).\n");
547 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500548 }
549
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500550 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500551 terminal->channel = g_io_channel_unix_new(master);
552 fcntl(master, F_SETFL, O_NONBLOCK);
553 g_io_add_watch(terminal->channel, G_IO_IN,
554 io_handler, terminal);
555
556 return 0;
557}
558
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500559static const GOptionEntry option_entries[] = {
560 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
561 &option_fullscreen, "Run in fullscreen mode" },
562 { NULL }
563};
564
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500565int main(int argc, char *argv[])
566{
567 struct wl_display *display;
568 int fd;
569 GMainLoop *loop;
570 GSource *source;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500571 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500572 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500573 GOptionContext *context;
574 GError *error;
575
576 context = g_option_context_new(NULL);
577 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
578 if (!g_option_context_parse(context, &argc, &argv, &error)) {
579 fprintf(stderr, "option parsing failed: %s\n", error->message);
580 exit(EXIT_FAILURE);
581 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500582
583 fd = open(gem_device, O_RDWR);
584 if (fd < 0) {
585 fprintf(stderr, "drm open failed: %m\n");
586 return -1;
587 }
588
589 display = wl_display_create(socket_name, sizeof socket_name);
590 if (display == NULL) {
591 fprintf(stderr, "failed to create display: %m\n");
592 return -1;
593 }
594
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500595 d = display_create(display, fd);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500596 loop = g_main_loop_new(NULL, FALSE);
597 source = wl_glib_source_new(display);
598 g_source_attach(source, NULL);
599
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500600 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500601 if (terminal_run(terminal, "/bin/bash"))
602 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500603
604 g_main_loop_run(loop);
605
606 return 0;
607}