You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
weston/clients/terminal.c

714 lines
17 KiB

/*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <pty.h>
#include <ctype.h>
#include <cairo.h>
#include <glib.h>
#include <X11/keysym.h>
#include "wayland-util.h"
#include "wayland-client.h"
#include "wayland-glib.h"
#include "window.h"
static int option_fullscreen;
#define MOD_SHIFT 0x01
#define MOD_ALT 0x02
#define MOD_CTRL 0x04
union utf8_char {
unsigned char byte[4];
uint32_t ch;
};
enum utf8_state {
utf8state_start,
utf8state_accept,
utf8state_reject,
utf8state_expect3,
utf8state_expect2,
utf8state_expect1
};
struct utf8_state_machine {
enum utf8_state state;
int len;
union utf8_char s;
};
static void
init_state_machine(struct utf8_state_machine *machine)
{
machine->state = utf8state_start;
machine->len = 0;
machine->s.ch = 0;
}
static enum utf8_state
utf8_next_char(struct utf8_state_machine *machine, char c)
{
switch(machine->state) {
case utf8state_start:
case utf8state_accept:
case utf8state_reject:
machine->s.ch = 0;
machine->len = 0;
if(c == 0xC0 || c == 0xC1) {
/* overlong encoding, reject */
machine->state = utf8state_reject;
} else if((c & 0x80) == 0) {
/* single byte, accept */
machine->s.byte[machine->len++] = c;
machine->state = utf8state_accept;
} else if((c & 0xC0) == 0x80) {
/* parser out of sync, ignore byte */
machine->state = utf8state_start;
} else if((c & 0xE0) == 0xC0) {
/* start of two byte sequence */
machine->s.byte[machine->len++] = c;
machine->state = utf8state_expect1;
} else if((c & 0xF0) == 0xE0) {
/* start of three byte sequence */
machine->s.byte[machine->len++] = c;
machine->state = utf8state_expect2;
} else if((c & 0xF8) == 0xF0) {
/* start of four byte sequence */
machine->s.byte[machine->len++] = c;
machine->state = utf8state_expect3;
} else {
/* overlong encoding, reject */
machine->state = utf8state_reject;
}
break;
case utf8state_expect3:
machine->s.byte[machine->len++] = c;
if((c & 0xC0) == 0x80) {
/* all good, continue */
machine->state = utf8state_expect2;
} else {
/* missing extra byte, reject */
machine->state = utf8state_reject;
}
break;
case utf8state_expect2:
machine->s.byte[machine->len++] = c;
if((c & 0xC0) == 0x80) {
/* all good, continue */
machine->state = utf8state_expect1;
} else {
/* missing extra byte, reject */
machine->state = utf8state_reject;
}
break;
case utf8state_expect1:
machine->s.byte[machine->len++] = c;
if((c & 0xC0) == 0x80) {
/* all good, accept */
machine->state = utf8state_accept;
} else {
/* missing extra byte, reject */
machine->state = utf8state_reject;
}
break;
default:
machine->state = utf8state_reject;
break;
}
return machine->state;
}
struct terminal {
struct window *window;
struct display *display;
union utf8_char *data;
union utf8_char last_char;
int data_pitch; /* The width in bytes of a line */
int width, height, start, row, column;
int fd, master;
GIOChannel *channel;
uint32_t modifiers;
char escape[64];
int escape_length;
int state;
struct utf8_state_machine state_machine;
int margin;
int fullscreen;
int focused;
struct color_scheme *color_scheme;
14 years ago
cairo_font_extents_t extents;
};
static union utf8_char *
terminal_get_row(struct terminal *terminal, int row)
{
int index;
index = (row + terminal->start) % terminal->height;
return &terminal->data[index * terminal->width];
}
static void
terminal_resize(struct terminal *terminal, int width, int height)
{
size_t size;
union utf8_char *data;
int data_pitch;
int i, l, total_rows, start;
if (terminal->width == width && terminal->height == height)
return;
data_pitch = width * sizeof(union utf8_char);
size = data_pitch * height;
data = malloc(size);
memset(data, 0, size);
if (terminal->data) {
if (width > terminal->width)
l = terminal->width;
else
l = width;
if (terminal->height > height) {
total_rows = height;
start = terminal->height - height;
} else {
total_rows = terminal->height;
start = 0;
}
for (i = 0; i < total_rows; i++) {
memcpy(&data[width * i],
terminal_get_row(terminal, i),
l * sizeof(union utf8_char));
}
free(terminal->data);
}
terminal->data_pitch = data_pitch;
terminal->width = width;
terminal->height = height;
terminal->data = data;
if (terminal->row >= terminal->height)
terminal->row = terminal->height - 1;
if (terminal->column >= terminal->width)
terminal->column = terminal->width - 1;
terminal->start = 0;
}
struct color_scheme { struct { double r, g, b, a; } fg, bg; }
matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
static void
terminal_draw_contents(struct terminal *terminal)
{
struct rectangle rectangle;
cairo_t *cr;
cairo_font_extents_t extents;
int top_margin, side_margin;
int row, col;
union utf8_char *p_row;
struct utf8_chars {
union utf8_char c;
char null;
} toShow;
int text_x, text_y;
cairo_surface_t *surface;
double d;
toShow.null = 0;
window_get_child_rectangle(terminal->window, &rectangle);
surface = display_create_surface(terminal->display, &rectangle);
cr = cairo_create(surface);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr,
terminal->color_scheme->bg.r,
terminal->color_scheme->bg.g,
terminal->color_scheme->bg.b,
terminal->color_scheme->bg.a);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(cr,
terminal->color_scheme->fg.r,
terminal->color_scheme->fg.g,
terminal->color_scheme->fg.b,
terminal->color_scheme->fg.a);
cairo_select_font_face (cr, "mono",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 14);
cairo_font_extents(cr, &extents);
side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
top_margin = (rectangle.height - terminal->height * extents.height) / 2;
for (row = 0; row < terminal->height; row++) {
p_row = terminal_get_row(terminal, row);
for (col = 0; col < terminal->width; col++) {
/* paint the foreground */
text_x = side_margin + col * extents.max_x_advance;
text_y = top_margin + extents.ascent + row * extents.height;
cairo_move_to(cr, text_x, text_y);
toShow.c = p_row[col];
cairo_show_text(cr, (char *) toShow.c.byte);
}
}
d = terminal->focused ? 0 : 0.5;
cairo_set_line_width(cr, 1);
cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
top_margin + terminal->row * extents.height + d);
cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
cairo_rel_line_to(cr, 0, extents.height - 2 * d);
cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
cairo_close_path(cr);
if (terminal->focused)
cairo_fill(cr);
else
cairo_stroke(cr);
cairo_destroy(cr);
window_copy_surface(terminal->window,
&rectangle,
surface);
cairo_surface_destroy(surface);
}
static void
terminal_draw(struct terminal *terminal)
{
struct rectangle rectangle;
int32_t width, height;
window_get_child_rectangle(terminal->window, &rectangle);
14 years ago
width = (rectangle.width - 2 * terminal->margin) /
(int32_t) terminal->extents.max_x_advance;
height = (rectangle.height - 2 * terminal->margin) /
(int32_t) terminal->extents.height;
terminal_resize(terminal, width, height);
if (!terminal->fullscreen) {
14 years ago
rectangle.width = terminal->width *
terminal->extents.max_x_advance + 2 * terminal->margin;
rectangle.height = terminal->height *
terminal->extents.height + 2 * terminal->margin;
window_set_child_size(terminal->window, &rectangle);
}
window_draw(terminal->window);
terminal_draw_contents(terminal);
window_flush(terminal->window);
}
static void
redraw_handler(struct window *window, void *data)
{
struct terminal *terminal = data;
terminal_draw(terminal);
}
#define STATE_NORMAL 0
#define STATE_ESCAPE 1
static void
terminal_data(struct terminal *terminal, const char *data, size_t length);
static void
handle_escape(struct terminal *terminal)
{
union utf8_char *row;
char *p;
int i, count;
int args[10], set[10] = { 0, };
terminal->escape[terminal->escape_length++] = '\0';
i = 0;
p = &terminal->escape[2];
while ((isdigit(*p) || *p == ';') && i < 10) {
if (*p == ';') {
p++;
i++;
} else {
args[i] = strtol(p, &p, 10);
set[i] = 1;
}
}
switch (*p) {
case 'A':
count = set[0] ? args[0] : 1;
if (terminal->row - count >= 0)
terminal->row -= count;
else
terminal->row = 0;
break;
case 'B':
count = set[0] ? args[0] : 1;
if (terminal->row + count < terminal->height)
terminal->row += count;
else
terminal->row = terminal->height;
break;
case 'C':
count = set[0] ? args[0] : 1;
if (terminal->column + count < terminal->width)
terminal->column += count;
else
terminal->column = terminal->width;
break;
case 'D':
count = set[0] ? args[0] : 1;
if (terminal->column - count >= 0)
terminal->column -= count;
else
terminal->column = 0;
break;
case 'J':
row = terminal_get_row(terminal, terminal->row);
memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
for (i = terminal->row + 1; i < terminal->height; i++)
memset(terminal_get_row(terminal, i), 0, terminal->width * sizeof(union utf8_char));
break;
case 'G':
if (set[0])
terminal->column = args[0] - 1;
break;
case 'H':
case 'f':
terminal->row = set[0] ? args[0] - 1 : 0;
terminal->column = set[1] ? args[1] - 1 : 0;
break;
case 'K':
row = terminal_get_row(terminal, terminal->row);
memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
break;
case 'm':
/* color, blink, bold etc*/
break;
case '?':
if (strcmp(p, "?25l") == 0) {
/* hide cursor */
} else if (strcmp(p, "?25h") == 0) {
/* show cursor */
}
break;
default:
terminal_data(terminal,
terminal->escape + 1,
terminal->escape_length - 2);
break;
}
}
static void
terminal_data(struct terminal *terminal, const char *data, size_t length)
{
int i;
union utf8_char utf8;
enum utf8_state parser_state;
union utf8_char *row;
for (i = 0; i < length; i++) {
parser_state =
utf8_next_char(&terminal->state_machine, data[i]);
switch(parser_state) {
case utf8state_accept:
utf8.ch = terminal->state_machine.s.ch;
break;
case utf8state_reject:
/* the unicode replacement character */
utf8.byte[0] = 0xEF;
utf8.byte[1] = 0xBF;
utf8.byte[2] = 0xBD;
utf8.byte[3] = 0x00;
break;
default:
continue;
}
row = terminal_get_row(terminal, terminal->row);
if (terminal->state == STATE_ESCAPE) {
terminal->escape[terminal->escape_length++] = utf8.byte[0];
if (terminal->escape_length == 2 && utf8.byte[0] != '[') {
/* Bad escape sequence. */
terminal->state = STATE_NORMAL;
goto cancel_escape;
}
if (isalpha(utf8.byte[0])) {
terminal->state = STATE_NORMAL;
handle_escape(terminal);
}
continue;
}
cancel_escape:
switch (utf8.byte[0]) {
case '\r':
terminal->column = 0;
break;
case '\n':
terminal->column = 0;
if (terminal->row + 1 < terminal->height) {
terminal->row++;
} else {
terminal->start++;
if (terminal->start == terminal->height)
terminal->start = 0;
memset(terminal_get_row(terminal, terminal->row),
0, terminal->width * sizeof(union utf8_char));
}
break;
case '\t':
memset(&row[terminal->column], ' ', (-terminal->column & 7) * sizeof(union utf8_char));
terminal->column = (terminal->column + 7) & ~7;
break;
case '\e':
terminal->state = STATE_ESCAPE;
terminal->escape[0] = '\e';
terminal->escape_length = 1;
break;
case '\b':
if (terminal->column > 0)
terminal->column--;
break;
case '\a':
/* Bell */
break;
default:
if (terminal->column < terminal->width)
if (utf8.byte[0] < 32) utf8.byte[0] += 64;
row[terminal->column++] = utf8;
break;
}
}
window_schedule_redraw(terminal->window);
}
static void
key_handler(struct window *window, uint32_t key, uint32_t sym,
uint32_t state, uint32_t modifiers, void *data)
{
struct terminal *terminal = data;
char ch[2];
int len = 0;
switch (sym) {
case XK_F11:
if (!state)
break;
terminal->fullscreen ^= 1;
window_set_fullscreen(window, terminal->fullscreen);
window_schedule_redraw(terminal->window);
break;
case XK_Delete:
sym = 0x04;
case XK_BackSpace:
case XK_Tab:
case XK_Linefeed:
case XK_Clear:
case XK_Return:
case XK_Pause:
case XK_Scroll_Lock:
case XK_Sys_Req:
case XK_Escape:
ch[len++] = sym & 0x7f;
break;
case XK_Shift_L:
case XK_Shift_R:
case XK_Control_L:
case XK_Control_R:
case XK_Alt_L:
case XK_Alt_R:
break;
default:
if (modifiers & WINDOW_MODIFIER_CONTROL)
sym = sym & 0x1f;
else if (modifiers & WINDOW_MODIFIER_ALT)
ch[len++] = 0x1b;
if (sym < 256)
ch[len++] = sym;
break;
}
if (state && len > 0)
write(terminal->master, ch, len);
}
static void
keyboard_focus_handler(struct window *window,
struct input *device, void *data)
{
struct terminal *terminal = data;
terminal->focused = (device != NULL);
window_schedule_redraw(terminal->window);
}
static struct terminal *
terminal_create(struct display *display, int fullscreen)
{
struct terminal *terminal;
14 years ago
cairo_surface_t *surface;
cairo_t *cr;
terminal = malloc(sizeof *terminal);
if (terminal == NULL)
return terminal;
memset(terminal, 0, sizeof *terminal);
terminal->fullscreen = fullscreen;
terminal->color_scheme = &jbarnes_colors;
terminal->window = window_create(display, "Wayland Terminal",
500, 400);
init_state_machine(&terminal->state_machine);
terminal->display = display;
terminal->margin = 5;
window_set_fullscreen(terminal->window, terminal->fullscreen);
window_set_user_data(terminal->window, terminal);
window_set_redraw_handler(terminal->window, redraw_handler);
window_set_key_handler(terminal->window, key_handler);
window_set_keyboard_focus_handler(terminal->window,
keyboard_focus_handler);
14 years ago
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
cr = cairo_create(surface);
cairo_select_font_face (cr, "mono",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 14);
cairo_font_extents(cr, &terminal->extents);
cairo_destroy(cr);
cairo_surface_destroy(surface);
terminal_draw(terminal);
return terminal;
}
static gboolean
io_handler(GIOChannel *source,
GIOCondition condition,
gpointer data)
{
struct terminal *terminal = data;
gchar buffer[256];
gsize bytes_read;
GError *error = NULL;
g_io_channel_read_chars(source, buffer, sizeof buffer,
&bytes_read, &error);
terminal_data(terminal, buffer, bytes_read);
return TRUE;
}
static int
terminal_run(struct terminal *terminal, const char *path)
{
int master;
pid_t pid;
pid = forkpty(&master, NULL, NULL, NULL);
if (pid == 0) {
setenv("TERM", "vt100", 1);
if (execl(path, path, NULL)) {
printf("exec failed: %m\n");
exit(EXIT_FAILURE);
}
} else if (pid < 0) {
fprintf(stderr, "failed to fork and create pty (%m).\n");
return -1;
}
terminal->master = master;
terminal->channel = g_io_channel_unix_new(master);
fcntl(master, F_SETFL, O_NONBLOCK);
g_io_add_watch(terminal->channel, G_IO_IN,
io_handler, terminal);
return 0;
}
static const GOptionEntry option_entries[] = {
{ "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
&option_fullscreen, "Run in fullscreen mode" },
{ NULL }
};
int main(int argc, char *argv[])
{
struct display *d;
struct terminal *terminal;
d = display_create(&argc, &argv, option_entries);
if (d == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
terminal = terminal_create(d, option_fullscreen);
if (terminal_run(terminal, "/bin/bash"))
exit(EXIT_FAILURE);
display_run(d);
return 0;
}