blob: a16fdcbb028bea5f2f22d2e9db095a2ed84d6a9d [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 -050045
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050046#define MOD_SHIFT 0x01
47#define MOD_ALT 0x02
48#define MOD_CTRL 0x04
49
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050050struct terminal {
51 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050052 struct display *display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -050053 int redraw_scheduled, redraw_pending;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050054 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050055 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050056 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050057 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050058 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050059 char escape[64];
60 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050061 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050062 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050063 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050064 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -040065 struct color_scheme *color_scheme;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066};
67
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050068static char *
69terminal_get_row(struct terminal *terminal, int row)
70{
71 int index;
72
73 index = (row + terminal->start) % terminal->height;
74
75 return &terminal->data[index * (terminal->width + 1)];
76}
77
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050078static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050079terminal_resize(struct terminal *terminal, int width, int height)
80{
81 size_t size;
82 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050083 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050084
85 if (terminal->width == width && terminal->height == height)
86 return;
87
88 size = (width + 1) * height;
89 data = malloc(size);
90 memset(data, 0, size);
91 if (terminal->data) {
92 if (width > terminal->width)
93 l = terminal->width;
94 else
95 l = width;
96
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050097 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -050098 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050099 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500100 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500101 total_rows = terminal->height;
102 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500103 }
104
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500105 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500106 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500107 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500108
109 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500110 }
111
112 terminal->width = width;
113 terminal->height = height;
114 terminal->data = data;
115
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500116 if (terminal->row >= terminal->height)
117 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500118 if (terminal->column >= terminal->width)
119 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500120 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500121}
122
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400123struct color_scheme { struct { double r, g, b, a; } fg, bg; }
124 matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
125 jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
126
Kristian Høgsberg22106762008-12-08 13:50:07 -0500127static 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øgsberg2aac3022009-12-21 10:04:53 -0500134 cairo_surface_t *surface;
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øgsberg2aac3022009-12-21 10:04:53 -0500139 surface = window_create_surface(terminal->window, &rectangle);
140 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500141 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400142 cairo_set_source_rgba(cr,
143 terminal->color_scheme->bg.r,
144 terminal->color_scheme->bg.g,
145 terminal->color_scheme->bg.b,
146 terminal->color_scheme->bg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500147 cairo_paint(cr);
148 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400149 cairo_set_source_rgba(cr,
150 terminal->color_scheme->fg.r,
151 terminal->color_scheme->fg.g,
152 terminal->color_scheme->fg.b,
153 terminal->color_scheme->fg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500154
155 cairo_select_font_face (cr, "mono",
156 CAIRO_FONT_SLANT_NORMAL,
157 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500158 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500159
160 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500161 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
162 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
163
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500164 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500165 cairo_move_to(cr, side_margin,
166 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500167 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500168 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500169
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500170 d = terminal->focused ? 0 : 0.5;
171
172 cairo_set_line_width(cr, 1);
173 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
174 top_margin + terminal->row * extents.height + d);
175 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
176 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
177 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500178 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500179
180 if (terminal->focused)
181 cairo_fill(cr);
182 else
183 cairo_stroke(cr);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500184
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500185 cairo_destroy(cr);
186
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500187 window_copy_surface(terminal->window,
188 &rectangle,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500189 surface);
190
191 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500192}
193
194static void
195terminal_draw(struct terminal *terminal)
196{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500197 struct rectangle rectangle;
198 cairo_surface_t *surface;
199 cairo_font_extents_t extents;
200 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500201 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500202
203 window_get_child_rectangle(terminal->window, &rectangle);
204
205 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
206 cr = cairo_create(surface);
207 cairo_select_font_face (cr, "mono",
208 CAIRO_FONT_SLANT_NORMAL,
209 CAIRO_FONT_WEIGHT_NORMAL);
210 cairo_set_font_size(cr, 14);
211 cairo_font_extents(cr, &extents);
212 cairo_destroy(cr);
213 cairo_surface_destroy(surface);
214
215 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
216 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
217 terminal_resize(terminal, width, height);
218
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500219 if (!terminal->fullscreen) {
220 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
221 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
222 window_set_child_size(terminal->window, &rectangle);
223 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500224
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500225 window_draw(terminal->window);
226 terminal_draw_contents(terminal);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500227 window_commit(terminal->window, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500228}
229
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500230static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500231idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500232{
233 struct terminal *terminal = data;
234
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500235 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500236
237 return FALSE;
238}
239
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500240#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500241#define STATE_ESCAPE 1
242
243static void
244terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500245
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500246static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500247terminal_schedule_redraw(struct terminal *terminal)
248{
249 if (!terminal->redraw_scheduled) {
250 g_idle_add(idle_redraw, terminal);
251 terminal->redraw_scheduled = 1;
252 } else {
253 terminal->redraw_pending = 1;
254 }
255}
256
257static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500258handle_escape(struct terminal *terminal)
259{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500260 char *row, *p;
261 int i, count;
262 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500263
264 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500265 i = 0;
266 p = &terminal->escape[2];
267 while ((isdigit(*p) || *p == ';') && i < 10) {
268 if (*p == ';') {
269 p++;
270 i++;
271 } else {
272 args[i] = strtol(p, &p, 10);
273 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500274 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500275 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500276
277 switch (*p) {
278 case 'A':
279 count = set[0] ? args[0] : 1;
280 if (terminal->row - count >= 0)
281 terminal->row -= count;
282 else
283 terminal->row = 0;
284 break;
285 case 'B':
286 count = set[0] ? args[0] : 1;
287 if (terminal->row + count < terminal->height)
288 terminal->row += count;
289 else
290 terminal->row = terminal->height;
291 break;
292 case 'C':
293 count = set[0] ? args[0] : 1;
294 if (terminal->column + count < terminal->width)
295 terminal->column += count;
296 else
297 terminal->column = terminal->width;
298 break;
299 case 'D':
300 count = set[0] ? args[0] : 1;
301 if (terminal->column - count >= 0)
302 terminal->column -= count;
303 else
304 terminal->column = 0;
305 break;
306 case 'J':
307 row = terminal_get_row(terminal, terminal->row);
308 memset(&row[terminal->column], 0, terminal->width - terminal->column);
309 for (i = terminal->row + 1; i < terminal->height; i++)
310 memset(terminal_get_row(terminal, i), 0, terminal->width);
311 break;
312 case 'G':
313 if (set[0])
314 terminal->column = args[0] - 1;
315 break;
316 case 'H':
317 case 'f':
318 terminal->row = set[0] ? args[0] - 1 : 0;
319 terminal->column = set[1] ? args[1] - 1 : 0;
320 break;
321 case 'K':
322 row = terminal_get_row(terminal, terminal->row);
323 memset(&row[terminal->column], 0, terminal->width - terminal->column);
324 break;
325 case 'm':
326 /* color, blink, bold etc*/
327 break;
328 case '?':
329 if (strcmp(p, "?25l") == 0) {
330 /* hide cursor */
331 } else if (strcmp(p, "?25h") == 0) {
332 /* show cursor */
333 }
334 break;
335 default:
336 terminal_data(terminal,
337 terminal->escape + 1,
338 terminal->escape_length - 2);
339 break;
340 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500341}
342
343static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500344terminal_data(struct terminal *terminal, const char *data, size_t length)
345{
346 int i;
347 char *row;
348
349 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500350 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500351
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500352 if (terminal->state == STATE_ESCAPE) {
353 terminal->escape[terminal->escape_length++] = data[i];
354 if (terminal->escape_length == 2 && data[i] != '[') {
355 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500356 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500357 goto cancel_escape;
358 }
359
360 if (isalpha(data[i])) {
361 terminal->state = STATE_NORMAL;
362 handle_escape(terminal);
363 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500364 continue;
365 }
366
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500367 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500368 switch (data[i]) {
369 case '\r':
370 terminal->column = 0;
371 break;
372 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500373 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500374 if (terminal->row + 1 < terminal->height) {
375 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500376 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500377 terminal->start++;
378 if (terminal->start == terminal->height)
379 terminal->start = 0;
380 memset(terminal_get_row(terminal, terminal->row),
381 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500382 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500383
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500384 break;
385 case '\t':
386 memset(&row[terminal->column], ' ', -terminal->column & 7);
387 terminal->column = (terminal->column + 7) & ~7;
388 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500389 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500390 terminal->state = STATE_ESCAPE;
391 terminal->escape[0] = '\e';
392 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500393 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500394 case '\b':
395 if (terminal->column > 0)
396 terminal->column--;
397 break;
398 case '\a':
399 /* Bell */
400 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500401 default:
402 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500403 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500404 break;
405 }
406 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500407
408 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500409}
410
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500411static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500412resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500413{
414 struct terminal *terminal = data;
415
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500416 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500417}
418
419static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400420acknowledge_handler(struct window *window,
421 uint32_t key, uint32_t frame, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500422{
423 struct terminal *terminal = data;
424
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500425 terminal->redraw_scheduled = 0;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500426 if (terminal->redraw_pending) {
427 terminal->redraw_pending = 0;
428 terminal_schedule_redraw(terminal);
429 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500430}
431
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500432static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500433key_handler(struct window *window, uint32_t key, uint32_t unicode,
434 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500435{
436 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500437 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500438
439 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500440 case KEY_F11:
441 if (!state)
442 break;
443 terminal->fullscreen ^= 1;
444 window_set_fullscreen(window, terminal->fullscreen);
445 terminal_schedule_redraw(terminal);
446 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500447 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500448 if (state && unicode)
449 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500450 break;
451 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500452}
453
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500454static void
455keyboard_focus_handler(struct window *window,
456 struct wl_input_device *device, void *data)
457{
458 struct terminal *terminal = data;
459
460 terminal->focused = (device != NULL);
461 terminal_schedule_redraw(terminal);
462}
463
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500464static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500465terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500466{
467 struct terminal *terminal;
468
469 terminal = malloc(sizeof *terminal);
470 if (terminal == NULL)
471 return terminal;
472
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500473 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500474 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400475 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500476 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500477 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500478 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500479 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500480 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500481
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500482 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500483 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400484 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500485
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500486 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500487 window_set_keyboard_focus_handler(terminal->window,
488 keyboard_focus_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500489
Kristian Høgsberg22106762008-12-08 13:50:07 -0500490 terminal_draw(terminal);
491
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500492 return terminal;
493}
494
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500495static gboolean
496io_handler(GIOChannel *source,
497 GIOCondition condition,
498 gpointer data)
499{
500 struct terminal *terminal = data;
501 gchar buffer[256];
502 gsize bytes_read;
503 GError *error = NULL;
504
505 g_io_channel_read_chars(source, buffer, sizeof buffer,
506 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500507
508 terminal_data(terminal, buffer, bytes_read);
509
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500510 return TRUE;
511}
512
513static int
514terminal_run(struct terminal *terminal, const char *path)
515{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500516 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500517 pid_t pid;
518
519 pid = forkpty(&master, NULL, NULL, NULL);
520 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500521 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500522 if (execl(path, path, NULL)) {
523 printf("exec failed: %m\n");
524 exit(EXIT_FAILURE);
525 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500526 } else if (pid < 0) {
527 fprintf(stderr, "failed to fork and create pty (%m).\n");
528 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500529 }
530
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500531 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500532 terminal->channel = g_io_channel_unix_new(master);
533 fcntl(master, F_SETFL, O_NONBLOCK);
534 g_io_add_watch(terminal->channel, G_IO_IN,
535 io_handler, terminal);
536
537 return 0;
538}
539
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500540static const GOptionEntry option_entries[] = {
541 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
542 &option_fullscreen, "Run in fullscreen mode" },
543 { NULL }
544};
545
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500546int main(int argc, char *argv[])
547{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500548 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500549 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500550
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400551 d = display_create(&argc, &argv, option_entries);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500552
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500553 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500554 if (terminal_run(terminal, "/bin/bash"))
555 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500556
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400557 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500558
559 return 0;
560}