2008-12-08 03:59:11 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-08-22 09:13:17 +04:00
|
|
|
#include <config.h>
|
2013-08-19 06:59:21 +04:00
|
|
|
|
2012-08-18 15:38:38 +04:00
|
|
|
#include <stdbool.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
2008-12-08 07:17:31 +03:00
|
|
|
#include <pty.h>
|
2008-12-08 08:07:49 +03:00
|
|
|
#include <ctype.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
#include <cairo.h>
|
2011-09-15 01:33:48 +04:00
|
|
|
#include <sys/epoll.h>
|
2013-08-19 06:59:21 +04:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <locale.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2013-10-24 03:52:05 +04:00
|
|
|
#include <linux/input.h>
|
|
|
|
|
2011-11-22 16:18:50 +04:00
|
|
|
#include <wayland-client.h>
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2012-06-20 23:29:07 +04:00
|
|
|
#include "../shared/config-parser.h"
|
2008-12-08 03:59:11 +03:00
|
|
|
#include "window.h"
|
|
|
|
|
2008-12-22 20:14:50 +03:00
|
|
|
static int option_fullscreen;
|
2013-09-22 09:23:08 +04:00
|
|
|
static char *option_font;
|
|
|
|
static int option_font_size;
|
|
|
|
static char *option_term;
|
2012-06-21 06:44:03 +04:00
|
|
|
static char *option_shell;
|
|
|
|
|
|
|
|
static struct wl_list terminal_list;
|
|
|
|
|
|
|
|
static struct terminal *
|
2012-10-10 19:37:46 +04:00
|
|
|
terminal_create(struct display *display);
|
2012-06-21 06:44:03 +04:00
|
|
|
static void
|
|
|
|
terminal_destroy(struct terminal *terminal);
|
|
|
|
static int
|
|
|
|
terminal_run(struct terminal *terminal, const char *path);
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2013-06-06 11:32:41 +04:00
|
|
|
#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
|
|
|
|
" !\"#$%&'()*+,-./" \
|
|
|
|
"0123456789" \
|
|
|
|
":;<=>?@" \
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|
|
|
"[\\]^_`" \
|
|
|
|
"abcdefghijklmnopqrstuvwxyz" \
|
|
|
|
"{|}~" \
|
|
|
|
""
|
|
|
|
|
2008-12-08 08:01:36 +03:00
|
|
|
#define MOD_SHIFT 0x01
|
|
|
|
#define MOD_ALT 0x02
|
|
|
|
#define MOD_CTRL 0x04
|
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
#define ATTRMASK_BOLD 0x01
|
|
|
|
#define ATTRMASK_UNDERLINE 0x02
|
|
|
|
#define ATTRMASK_BLINK 0x04
|
|
|
|
#define ATTRMASK_INVERSE 0x08
|
2011-01-10 02:14:01 +03:00
|
|
|
#define ATTRMASK_CONCEALED 0x10
|
2011-01-07 22:46:55 +03:00
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
/* Buffer sizes */
|
2011-01-14 10:46:23 +03:00
|
|
|
#define MAX_RESPONSE 256
|
|
|
|
#define MAX_ESCAPE 255
|
2011-01-07 22:46:57 +03:00
|
|
|
|
2011-01-07 22:46:59 +03:00
|
|
|
/* Terminal modes */
|
|
|
|
#define MODE_SHOW_CURSOR 0x00000001
|
|
|
|
#define MODE_INVERSE 0x00000002
|
|
|
|
#define MODE_AUTOWRAP 0x00000004
|
|
|
|
#define MODE_AUTOREPEAT 0x00000008
|
|
|
|
#define MODE_LF_NEWLINE 0x00000010
|
2011-01-07 22:47:00 +03:00
|
|
|
#define MODE_IRM 0x00000020
|
2011-01-07 22:47:02 +03:00
|
|
|
#define MODE_DELETE_SENDS_DEL 0x00000040
|
|
|
|
#define MODE_ALT_SENDS_ESC 0x00000080
|
2011-01-07 22:46:59 +03:00
|
|
|
|
2011-01-07 22:46:54 +03:00
|
|
|
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;
|
2013-08-14 04:23:54 +04:00
|
|
|
uint32_t unicode;
|
2011-01-07 22:46:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2012-03-30 19:52:39 +04:00
|
|
|
utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
|
2011-01-07 22:46:54 +03:00
|
|
|
{
|
|
|
|
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;
|
2013-08-14 04:23:54 +04:00
|
|
|
machine->unicode = c;
|
2011-01-07 22:46:54 +03:00
|
|
|
} 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;
|
2013-08-14 04:23:54 +04:00
|
|
|
machine->unicode = c & 0x1f;
|
2011-01-07 22:46:54 +03:00
|
|
|
} else if((c & 0xF0) == 0xE0) {
|
|
|
|
/* start of three byte sequence */
|
|
|
|
machine->s.byte[machine->len++] = c;
|
|
|
|
machine->state = utf8state_expect2;
|
2013-08-14 04:23:54 +04:00
|
|
|
machine->unicode = c & 0x0f;
|
2011-01-07 22:46:54 +03:00
|
|
|
} else if((c & 0xF8) == 0xF0) {
|
|
|
|
/* start of four byte sequence */
|
|
|
|
machine->s.byte[machine->len++] = c;
|
|
|
|
machine->state = utf8state_expect3;
|
2013-08-14 04:23:54 +04:00
|
|
|
machine->unicode = c & 0x07;
|
2011-01-07 22:46:54 +03:00
|
|
|
} else {
|
|
|
|
/* overlong encoding, reject */
|
|
|
|
machine->state = utf8state_reject;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case utf8state_expect3:
|
|
|
|
machine->s.byte[machine->len++] = c;
|
2013-08-14 04:23:54 +04:00
|
|
|
machine->unicode = (machine->unicode << 6) | (c & 0x3f);
|
2011-01-07 22:46:54 +03:00
|
|
|
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;
|
2013-08-14 04:23:54 +04:00
|
|
|
machine->unicode = (machine->unicode << 6) | (c & 0x3f);
|
2011-01-07 22:46:54 +03:00
|
|
|
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;
|
2013-08-14 04:23:54 +04:00
|
|
|
machine->unicode = (machine->unicode << 6) | (c & 0x3f);
|
2011-01-07 22:46:54 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-14 04:23:54 +04:00
|
|
|
static uint32_t
|
|
|
|
get_unicode(union utf8_char utf8)
|
|
|
|
{
|
|
|
|
struct utf8_state_machine machine;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
init_state_machine(&machine);
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
utf8_next_char(&machine, utf8.byte[i]);
|
|
|
|
if (machine.state == utf8state_accept ||
|
|
|
|
machine.state == utf8state_reject)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (machine.state == utf8state_reject)
|
|
|
|
return 0xfffd;
|
|
|
|
|
|
|
|
return machine.unicode;
|
|
|
|
}
|
|
|
|
|
2013-08-19 06:59:21 +04:00
|
|
|
static bool
|
|
|
|
is_wide(union utf8_char utf8)
|
|
|
|
{
|
|
|
|
uint32_t unichar = get_unicode(utf8);
|
|
|
|
return wcwidth(unichar) > 1;
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:47:01 +03:00
|
|
|
struct char_sub {
|
|
|
|
union utf8_char match;
|
|
|
|
union utf8_char replace;
|
|
|
|
};
|
|
|
|
/* Set last char_sub match to NULL char */
|
|
|
|
typedef struct char_sub *character_set;
|
|
|
|
|
|
|
|
struct char_sub CS_US[] = {
|
|
|
|
{{{0, }}, {{0, }}}
|
|
|
|
};
|
|
|
|
static struct char_sub CS_UK[] = {
|
2012-05-29 11:37:02 +04:00
|
|
|
{{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
|
2011-01-07 22:47:01 +03:00
|
|
|
{{{0, }}, {{0, }}}
|
|
|
|
};
|
|
|
|
static struct char_sub CS_SPECIAL[] = {
|
2012-05-29 11:37:02 +04:00
|
|
|
{{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
|
|
|
|
{{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
|
|
|
|
{{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
|
|
|
|
{{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
|
|
|
|
{{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
|
|
|
|
{{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
|
|
|
|
{{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
|
|
|
|
{{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
|
|
|
|
{{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL:  */
|
|
|
|
{{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
|
|
|
|
{{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
|
|
|
|
{{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
|
|
|
|
{{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
|
|
|
|
{{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
|
|
|
|
{{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
|
|
|
|
{{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
|
|
|
|
{{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
|
|
|
|
{{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
|
|
|
|
{{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
|
|
|
|
{{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
|
|
|
|
{{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
|
|
|
|
{{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
|
|
|
|
{{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
|
|
|
|
{{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
|
|
|
|
{{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
|
|
|
|
{{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
|
|
|
|
{{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
|
|
|
|
{{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
|
|
|
|
{{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
|
|
|
|
{{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
|
|
|
|
{{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
|
2011-01-07 22:47:01 +03:00
|
|
|
{{{0, }}, {{0, }}}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
apply_char_set(character_set cs, union utf8_char *utf8)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (cs[i].match.byte[0]) {
|
|
|
|
if ((*utf8).ch == cs[i].match.ch) {
|
|
|
|
*utf8 = cs[i].replace;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:47:02 +03:00
|
|
|
struct key_map {
|
|
|
|
int sym;
|
|
|
|
int num;
|
|
|
|
char escape;
|
|
|
|
char code;
|
|
|
|
};
|
|
|
|
/* Set last key_sub sym to NULL */
|
|
|
|
typedef struct key_map *keyboard_mode;
|
|
|
|
|
|
|
|
static struct key_map KM_NORMAL[] = {
|
2012-05-11 19:24:29 +04:00
|
|
|
{ XKB_KEY_Left, 1, '[', 'D' },
|
|
|
|
{ XKB_KEY_Right, 1, '[', 'C' },
|
|
|
|
{ XKB_KEY_Up, 1, '[', 'A' },
|
|
|
|
{ XKB_KEY_Down, 1, '[', 'B' },
|
|
|
|
{ XKB_KEY_Home, 1, '[', 'H' },
|
|
|
|
{ XKB_KEY_End, 1, '[', 'F' },
|
|
|
|
{ 0, 0, 0, 0 }
|
2011-01-07 22:47:02 +03:00
|
|
|
};
|
|
|
|
static struct key_map KM_APPLICATION[] = {
|
2012-05-11 19:24:29 +04:00
|
|
|
{ XKB_KEY_Left, 1, 'O', 'D' },
|
|
|
|
{ XKB_KEY_Right, 1, 'O', 'C' },
|
|
|
|
{ XKB_KEY_Up, 1, 'O', 'A' },
|
|
|
|
{ XKB_KEY_Down, 1, 'O', 'B' },
|
|
|
|
{ XKB_KEY_Home, 1, 'O', 'H' },
|
|
|
|
{ XKB_KEY_End, 1, 'O', 'F' },
|
|
|
|
{ XKB_KEY_KP_Enter, 1, 'O', 'M' },
|
|
|
|
{ XKB_KEY_KP_Multiply, 1, 'O', 'j' },
|
|
|
|
{ XKB_KEY_KP_Add, 1, 'O', 'k' },
|
|
|
|
{ XKB_KEY_KP_Separator, 1, 'O', 'l' },
|
|
|
|
{ XKB_KEY_KP_Subtract, 1, 'O', 'm' },
|
|
|
|
{ XKB_KEY_KP_Divide, 1, 'O', 'o' },
|
|
|
|
{ 0, 0, 0, 0 }
|
2011-01-07 22:47:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
function_key_response(char escape, int num, uint32_t modifiers,
|
|
|
|
char code, char *response)
|
|
|
|
{
|
|
|
|
int mod_num = 0;
|
|
|
|
int len;
|
|
|
|
|
2012-05-08 23:55:39 +04:00
|
|
|
if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
|
|
|
|
if (modifiers & MOD_ALT_MASK) mod_num |= 2;
|
|
|
|
if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
|
2011-01-07 22:47:02 +03:00
|
|
|
|
|
|
|
if (mod_num != 0)
|
|
|
|
len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
|
|
|
|
num, mod_num + 1, code);
|
|
|
|
else if (code != '~')
|
|
|
|
len = snprintf(response, MAX_RESPONSE, "\e%c%c",
|
|
|
|
escape, code);
|
|
|
|
else
|
|
|
|
len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
|
|
|
|
escape, num, code);
|
|
|
|
|
|
|
|
if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
|
|
|
|
else return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the number of bytes written into response,
|
|
|
|
* which must have room for MAX_RESPONSE bytes */
|
|
|
|
static int
|
|
|
|
apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
|
|
|
|
{
|
|
|
|
struct key_map map;
|
|
|
|
int len = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (mode[i].sym) {
|
|
|
|
map = mode[i++];
|
|
|
|
if (sym == map.sym) {
|
|
|
|
len = function_key_response(map.escape, map.num,
|
|
|
|
modifiers, map.code,
|
|
|
|
response);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
struct terminal_color { double r, g, b, a; };
|
|
|
|
struct attr {
|
|
|
|
unsigned char fg, bg;
|
|
|
|
char a; /* attributes format:
|
|
|
|
* 76543210
|
2011-01-10 02:14:01 +03:00
|
|
|
* cilub */
|
2011-01-21 23:18:55 +03:00
|
|
|
char s; /* in selection */
|
2011-01-07 22:46:55 +03:00
|
|
|
};
|
|
|
|
struct color_scheme {
|
|
|
|
struct terminal_color palette[16];
|
2011-01-11 18:13:00 +03:00
|
|
|
char border;
|
2011-01-07 22:46:55 +03:00
|
|
|
struct attr default_attr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
attr_init(struct attr *data_attr, struct attr attr, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
data_attr[i] = attr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 09:23:41 +03:00
|
|
|
enum escape_state {
|
|
|
|
escape_state_normal = 0,
|
|
|
|
escape_state_escape,
|
|
|
|
escape_state_dcs,
|
|
|
|
escape_state_csi,
|
|
|
|
escape_state_osc,
|
|
|
|
escape_state_inner_escape,
|
|
|
|
escape_state_ignore,
|
|
|
|
escape_state_special
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ESC_FLAG_WHAT 0x01
|
|
|
|
#define ESC_FLAG_GT 0x02
|
|
|
|
#define ESC_FLAG_BANG 0x04
|
|
|
|
#define ESC_FLAG_CASH 0x08
|
|
|
|
#define ESC_FLAG_SQUOTE 0x10
|
|
|
|
#define ESC_FLAG_DQUOTE 0x20
|
|
|
|
#define ESC_FLAG_SPACE 0x40
|
|
|
|
|
2012-06-28 01:43:10 +04:00
|
|
|
enum {
|
|
|
|
SELECT_NONE,
|
|
|
|
SELECT_CHAR,
|
|
|
|
SELECT_WORD,
|
|
|
|
SELECT_LINE
|
|
|
|
};
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
struct terminal {
|
|
|
|
struct window *window;
|
2012-01-10 18:43:58 +04:00
|
|
|
struct widget *widget;
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *display;
|
2013-10-24 07:47:35 +04:00
|
|
|
char *title;
|
2011-01-07 22:46:54 +03:00
|
|
|
union utf8_char *data;
|
2011-09-15 01:33:48 +04:00
|
|
|
struct task io_task;
|
2011-01-07 22:46:59 +03:00
|
|
|
char *tab_ruler;
|
2011-01-07 22:46:55 +03:00
|
|
|
struct attr *data_attr;
|
|
|
|
struct attr curr_attr;
|
2011-01-07 22:46:59 +03:00
|
|
|
uint32_t mode;
|
2011-01-07 22:46:58 +03:00
|
|
|
char origin_mode;
|
2011-01-07 22:46:59 +03:00
|
|
|
char saved_origin_mode;
|
|
|
|
struct attr saved_attr;
|
2011-01-07 22:46:54 +03:00
|
|
|
union utf8_char last_char;
|
2011-01-07 22:46:58 +03:00
|
|
|
int margin_top, margin_bottom;
|
2011-01-07 22:47:01 +03:00
|
|
|
character_set cs, g0, g1;
|
|
|
|
character_set saved_cs, saved_g0, saved_g1;
|
2011-01-07 22:47:02 +03:00
|
|
|
keyboard_mode key_mode;
|
2011-01-07 22:46:55 +03:00
|
|
|
int data_pitch, attr_pitch; /* The width in bytes of a line */
|
2013-10-24 03:29:14 +04:00
|
|
|
int width, height, row, column, max_width;
|
|
|
|
uint32_t buffer_height;
|
|
|
|
uint32_t start, end, saved_start, log_size;
|
2011-01-07 22:46:59 +03:00
|
|
|
int saved_row, saved_column;
|
2013-10-24 03:29:14 +04:00
|
|
|
int scrolling;
|
2012-05-28 00:25:02 +04:00
|
|
|
int send_cursor_position;
|
2008-12-08 08:01:36 +03:00
|
|
|
int fd, master;
|
|
|
|
uint32_t modifiers;
|
2011-01-14 10:46:23 +03:00
|
|
|
char escape[MAX_ESCAPE+1];
|
2008-12-08 20:20:40 +03:00
|
|
|
int escape_length;
|
2011-01-12 09:23:41 +03:00
|
|
|
enum escape_state state;
|
|
|
|
enum escape_state outer_state;
|
|
|
|
int escape_flags;
|
2011-01-07 22:46:54 +03:00
|
|
|
struct utf8_state_machine state_machine;
|
2008-12-08 20:59:37 +03:00
|
|
|
int margin;
|
2009-09-28 21:08:50 +04:00
|
|
|
struct color_scheme *color_scheme;
|
2011-01-07 22:46:55 +03:00
|
|
|
struct terminal_color color_table[256];
|
2010-06-15 07:22:15 +04:00
|
|
|
cairo_font_extents_t extents;
|
2013-06-06 11:32:41 +04:00
|
|
|
double average_width;
|
2010-12-26 00:58:31 +03:00
|
|
|
cairo_scaled_font_t *font_normal, *font_bold;
|
2012-06-21 23:55:03 +04:00
|
|
|
uint32_t hide_cursor_serial;
|
2011-01-19 22:27:42 +03:00
|
|
|
|
2011-10-28 20:26:06 +04:00
|
|
|
struct wl_data_source *selection;
|
2012-06-28 01:43:10 +04:00
|
|
|
uint32_t button_time;
|
|
|
|
int dragging, click_count;
|
2011-01-20 19:56:57 +03:00
|
|
|
int selection_start_x, selection_start_y;
|
|
|
|
int selection_end_x, selection_end_y;
|
2012-06-28 01:43:10 +04:00
|
|
|
int selection_start_row, selection_start_col;
|
|
|
|
int selection_end_row, selection_end_col;
|
2012-06-21 06:44:03 +04:00
|
|
|
struct wl_list link;
|
2008-12-08 03:59:11 +03:00
|
|
|
};
|
|
|
|
|
2011-01-07 22:46:59 +03:00
|
|
|
/* Create default tab stops, every 8 characters */
|
|
|
|
static void
|
|
|
|
terminal_init_tabs(struct terminal *terminal)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (i < terminal->width) {
|
|
|
|
if (i % 8 == 0)
|
|
|
|
terminal->tab_ruler[i] = 1;
|
|
|
|
else
|
|
|
|
terminal->tab_ruler[i] = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
static void
|
|
|
|
terminal_init(struct terminal *terminal)
|
|
|
|
{
|
|
|
|
terminal->curr_attr = terminal->color_scheme->default_attr;
|
2011-01-07 22:46:58 +03:00
|
|
|
terminal->origin_mode = 0;
|
2011-01-07 22:46:59 +03:00
|
|
|
terminal->mode = MODE_SHOW_CURSOR |
|
|
|
|
MODE_AUTOREPEAT |
|
2011-01-07 22:47:02 +03:00
|
|
|
MODE_ALT_SENDS_ESC |
|
2011-01-07 22:46:59 +03:00
|
|
|
MODE_AUTOWRAP;
|
2011-01-07 22:46:55 +03:00
|
|
|
|
|
|
|
terminal->row = 0;
|
|
|
|
terminal->column = 0;
|
2011-01-07 22:46:59 +03:00
|
|
|
|
2011-01-07 22:47:01 +03:00
|
|
|
terminal->g0 = CS_US;
|
|
|
|
terminal->g1 = CS_US;
|
|
|
|
terminal->cs = terminal->g0;
|
2011-01-07 22:47:02 +03:00
|
|
|
terminal->key_mode = KM_NORMAL;
|
2011-01-07 22:47:01 +03:00
|
|
|
|
|
|
|
terminal->saved_g0 = terminal->g0;
|
|
|
|
terminal->saved_g1 = terminal->g1;
|
|
|
|
terminal->saved_cs = terminal->cs;
|
|
|
|
|
2011-01-07 22:46:59 +03:00
|
|
|
terminal->saved_attr = terminal->curr_attr;
|
|
|
|
terminal->saved_origin_mode = terminal->origin_mode;
|
|
|
|
terminal->saved_row = terminal->row;
|
|
|
|
terminal->saved_column = terminal->column;
|
|
|
|
|
|
|
|
if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
|
2011-01-07 22:46:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_color_table(struct terminal *terminal)
|
|
|
|
{
|
|
|
|
int c, r;
|
|
|
|
struct terminal_color *color_table = terminal->color_table;
|
|
|
|
|
|
|
|
for (c = 0; c < 256; c ++) {
|
|
|
|
if (c < 16) {
|
|
|
|
color_table[c] = terminal->color_scheme->palette[c];
|
|
|
|
} else if (c < 232) {
|
|
|
|
r = c - 16;
|
|
|
|
color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
|
|
|
|
color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
|
|
|
|
color_table[c].r = ((double)(r % 6) / 6.0);
|
|
|
|
color_table[c].a = 1.0;
|
|
|
|
} else {
|
|
|
|
r = (c - 232) * 10 + 8;
|
|
|
|
color_table[c].r = ((double) r) / 256.0;
|
|
|
|
color_table[c].g = color_table[c].r;
|
|
|
|
color_table[c].b = color_table[c].r;
|
|
|
|
color_table[c].a = 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:54 +03:00
|
|
|
static union utf8_char *
|
2008-12-09 06:22:25 +03:00
|
|
|
terminal_get_row(struct terminal *terminal, int row)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
index = (row + terminal->start) & (terminal->buffer_height - 1);
|
2008-12-09 06:22:25 +03:00
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
return (void *) terminal->data + index * terminal->data_pitch;
|
2008-12-09 06:22:25 +03:00
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
static struct attr*
|
2010-12-25 16:58:46 +03:00
|
|
|
terminal_get_attr_row(struct terminal *terminal, int row)
|
|
|
|
{
|
2011-01-07 22:46:55 +03:00
|
|
|
int index;
|
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
index = (row + terminal->start) & (terminal->buffer_height - 1);
|
2011-01-07 22:46:55 +03:00
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
return (void *) terminal->data_attr + index * terminal->attr_pitch;
|
2011-01-07 22:46:55 +03:00
|
|
|
}
|
|
|
|
|
2011-01-11 19:06:49 +03:00
|
|
|
union decoded_attr {
|
2011-01-12 10:06:17 +03:00
|
|
|
struct attr attr;
|
2011-01-11 19:06:49 +03:00
|
|
|
uint32_t key;
|
2011-01-11 18:26:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
terminal_decode_attr(struct terminal *terminal, int row, int col,
|
2011-01-11 19:06:49 +03:00
|
|
|
union decoded_attr *decoded)
|
2010-12-25 16:58:46 +03:00
|
|
|
{
|
2011-01-11 18:26:04 +03:00
|
|
|
struct attr attr;
|
|
|
|
int foreground, background, tmp;
|
2012-06-28 01:43:10 +04:00
|
|
|
|
2011-01-21 23:18:55 +03:00
|
|
|
decoded->attr.s = 0;
|
2012-06-28 01:43:10 +04:00
|
|
|
if (((row == terminal->selection_start_row &&
|
|
|
|
col >= terminal->selection_start_col) ||
|
|
|
|
row > terminal->selection_start_row) &&
|
|
|
|
((row == terminal->selection_end_row &&
|
|
|
|
col < terminal->selection_end_col) ||
|
|
|
|
row < terminal->selection_end_row))
|
2011-01-21 23:18:55 +03:00
|
|
|
decoded->attr.s = 1;
|
2011-01-11 18:26:04 +03:00
|
|
|
|
|
|
|
/* get the attributes for this character cell */
|
|
|
|
attr = terminal_get_attr_row(terminal, row)[col];
|
|
|
|
if ((attr.a & ATTRMASK_INVERSE) ||
|
2011-01-21 23:18:55 +03:00
|
|
|
decoded->attr.s ||
|
2011-01-11 18:26:04 +03:00
|
|
|
((terminal->mode & MODE_SHOW_CURSOR) &&
|
2012-08-14 06:25:53 +04:00
|
|
|
window_has_focus(terminal->window) && terminal->row == row &&
|
2011-01-11 18:26:04 +03:00
|
|
|
terminal->column == col)) {
|
|
|
|
foreground = attr.bg;
|
|
|
|
background = attr.fg;
|
|
|
|
if (attr.a & ATTRMASK_BOLD) {
|
|
|
|
if (foreground <= 16) foreground |= 0x08;
|
|
|
|
if (background <= 16) background &= 0x07;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreground = attr.fg;
|
|
|
|
background = attr.bg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (terminal->mode & MODE_INVERSE) {
|
|
|
|
tmp = foreground;
|
|
|
|
foreground = background;
|
|
|
|
background = tmp;
|
|
|
|
if (attr.a & ATTRMASK_BOLD) {
|
|
|
|
if (foreground <= 16) foreground |= 0x08;
|
|
|
|
if (background <= 16) background &= 0x07;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 10:06:17 +03:00
|
|
|
decoded->attr.fg = foreground;
|
|
|
|
decoded->attr.bg = background;
|
|
|
|
decoded->attr.a = attr.a;
|
2011-01-07 22:46:55 +03:00
|
|
|
}
|
|
|
|
|
2011-01-21 23:18:55 +03:00
|
|
|
|
2011-01-07 22:46:58 +03:00
|
|
|
static void
|
|
|
|
terminal_scroll_buffer(struct terminal *terminal, int d)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
terminal->start += d;
|
|
|
|
if (d < 0) {
|
2011-01-07 22:46:58 +03:00
|
|
|
d = 0 - d;
|
2013-10-24 03:29:14 +04:00
|
|
|
for (i = 0; i < d; i++) {
|
2011-01-07 22:46:58 +03:00
|
|
|
memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
|
|
|
|
attr_init(terminal_get_attr_row(terminal, i),
|
|
|
|
terminal->curr_attr, terminal->width);
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-24 03:29:14 +04:00
|
|
|
for (i = terminal->height - d; i < terminal->height; i++) {
|
2011-01-07 22:46:58 +03:00
|
|
|
memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
|
|
|
|
attr_init(terminal_get_attr_row(terminal, i),
|
|
|
|
terminal->curr_attr, terminal->width);
|
|
|
|
}
|
|
|
|
}
|
2012-06-28 03:29:41 +04:00
|
|
|
|
|
|
|
terminal->selection_start_row -= d;
|
|
|
|
terminal->selection_end_row -= d;
|
2011-01-07 22:46:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
terminal_scroll_window(struct terminal *terminal, int d)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int window_height;
|
|
|
|
int from_row, to_row;
|
|
|
|
|
|
|
|
// scrolling range is inclusive
|
|
|
|
window_height = terminal->margin_bottom - terminal->margin_top + 1;
|
|
|
|
d = d % (window_height + 1);
|
|
|
|
if(d < 0) {
|
|
|
|
d = 0 - d;
|
|
|
|
to_row = terminal->margin_bottom;
|
|
|
|
from_row = terminal->margin_bottom - d;
|
|
|
|
|
|
|
|
for (i = 0; i < (window_height - d); i++) {
|
|
|
|
memcpy(terminal_get_row(terminal, to_row - i),
|
|
|
|
terminal_get_row(terminal, from_row - i),
|
|
|
|
terminal->data_pitch);
|
|
|
|
memcpy(terminal_get_attr_row(terminal, to_row - i),
|
|
|
|
terminal_get_attr_row(terminal, from_row - i),
|
|
|
|
terminal->attr_pitch);
|
|
|
|
}
|
|
|
|
for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
|
|
|
|
memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
|
2011-01-07 22:47:03 +03:00
|
|
|
attr_init(terminal_get_attr_row(terminal, i),
|
|
|
|
terminal->curr_attr, terminal->width);
|
2011-01-07 22:46:58 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
to_row = terminal->margin_top;
|
|
|
|
from_row = terminal->margin_top + d;
|
|
|
|
|
|
|
|
for (i = 0; i < (window_height - d); i++) {
|
|
|
|
memcpy(terminal_get_row(terminal, to_row + i),
|
|
|
|
terminal_get_row(terminal, from_row + i),
|
|
|
|
terminal->data_pitch);
|
|
|
|
memcpy(terminal_get_attr_row(terminal, to_row + i),
|
|
|
|
terminal_get_attr_row(terminal, from_row + i),
|
|
|
|
terminal->attr_pitch);
|
|
|
|
}
|
|
|
|
for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
|
|
|
|
memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
|
2011-01-07 22:47:03 +03:00
|
|
|
attr_init(terminal_get_attr_row(terminal, i),
|
|
|
|
terminal->curr_attr, terminal->width);
|
2011-01-07 22:46:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
terminal_scroll(struct terminal *terminal, int d)
|
|
|
|
{
|
|
|
|
if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
|
|
|
|
terminal_scroll_buffer(terminal, d);
|
|
|
|
else
|
|
|
|
terminal_scroll_window(terminal, d);
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:47:00 +03:00
|
|
|
static void
|
|
|
|
terminal_shift_line(struct terminal *terminal, int d)
|
|
|
|
{
|
|
|
|
union utf8_char *row;
|
2011-01-25 23:16:01 +03:00
|
|
|
struct attr *attr_row;
|
2011-01-07 22:47:00 +03:00
|
|
|
|
|
|
|
row = terminal_get_row(terminal, terminal->row);
|
|
|
|
attr_row = terminal_get_attr_row(terminal, terminal->row);
|
|
|
|
|
|
|
|
if ((terminal->width + d) <= terminal->column)
|
|
|
|
d = terminal->column + 1 - terminal->width;
|
|
|
|
if ((terminal->column + d) >= terminal->width)
|
|
|
|
d = terminal->width - terminal->column - 1;
|
|
|
|
|
|
|
|
if (d < 0) {
|
|
|
|
d = 0 - d;
|
|
|
|
memmove(&row[terminal->column],
|
|
|
|
&row[terminal->column + d],
|
|
|
|
(terminal->width - terminal->column - d) * sizeof(union utf8_char));
|
|
|
|
memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
|
|
|
|
(terminal->width - terminal->column - d) * sizeof(struct attr));
|
|
|
|
memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
|
|
|
|
attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
|
|
|
|
} else {
|
|
|
|
memmove(&row[terminal->column + d], &row[terminal->column],
|
|
|
|
(terminal->width - terminal->column - d) * sizeof(union utf8_char));
|
|
|
|
memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
|
|
|
|
(terminal->width - terminal->column - d) * sizeof(struct attr));
|
|
|
|
memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
|
|
|
|
attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-08 21:50:07 +03:00
|
|
|
static void
|
2013-10-24 03:29:14 +04:00
|
|
|
terminal_resize_cells(struct terminal *terminal,
|
|
|
|
int width, int height)
|
2008-12-08 21:50:07 +03:00
|
|
|
{
|
2011-01-07 22:46:54 +03:00
|
|
|
union utf8_char *data;
|
2011-01-07 22:46:55 +03:00
|
|
|
struct attr *data_attr;
|
2011-01-07 22:46:59 +03:00
|
|
|
char *tab_ruler;
|
2011-01-07 22:46:55 +03:00
|
|
|
int data_pitch, attr_pitch;
|
2011-01-25 23:16:01 +03:00
|
|
|
int i, l, total_rows;
|
2013-10-24 03:29:14 +04:00
|
|
|
uint32_t d, uheight = height;
|
2011-01-11 18:00:52 +03:00
|
|
|
struct rectangle allocation;
|
2011-01-07 22:46:56 +03:00
|
|
|
struct winsize ws;
|
2008-12-08 21:50:07 +03:00
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
if (uheight > terminal->buffer_height)
|
|
|
|
height = terminal->buffer_height;
|
|
|
|
|
2008-12-08 21:50:07 +03:00
|
|
|
if (terminal->width == width && terminal->height == height)
|
|
|
|
return;
|
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
if (terminal->data && width <= terminal->max_width) {
|
|
|
|
d = 0;
|
|
|
|
if (height < terminal->height && height <= terminal->row)
|
|
|
|
d = terminal->height - height;
|
|
|
|
else if (height > terminal->height &&
|
|
|
|
terminal->height - 1 == terminal->row) {
|
|
|
|
d = terminal->height - height;
|
|
|
|
if (terminal->log_size < uheight)
|
|
|
|
d = -terminal->start;
|
2008-12-08 21:50:07 +03:00
|
|
|
}
|
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
terminal->start += d;
|
|
|
|
terminal->row -= d;
|
|
|
|
} else {
|
|
|
|
terminal->max_width = width;
|
|
|
|
data_pitch = width * sizeof(union utf8_char);
|
|
|
|
data = zalloc(data_pitch * terminal->buffer_height);
|
|
|
|
attr_pitch = width * sizeof(struct attr);
|
|
|
|
data_attr = malloc(attr_pitch * terminal->buffer_height);
|
|
|
|
tab_ruler = zalloc(width);
|
|
|
|
attr_init(data_attr, terminal->curr_attr,
|
|
|
|
width * terminal->buffer_height);
|
|
|
|
|
|
|
|
if (terminal->data && terminal->data_attr) {
|
|
|
|
if (width > terminal->width)
|
|
|
|
l = terminal->width;
|
|
|
|
else
|
|
|
|
l = width;
|
|
|
|
|
|
|
|
if (terminal->height > height) {
|
|
|
|
total_rows = height;
|
|
|
|
i = 1 + terminal->row - height;
|
|
|
|
if (i > 0) {
|
|
|
|
terminal->start += i;
|
|
|
|
terminal->row = terminal->row - i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
total_rows = terminal->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < total_rows; i++) {
|
|
|
|
memcpy(&data[width * i],
|
|
|
|
terminal_get_row(terminal, i),
|
|
|
|
l * sizeof(union utf8_char));
|
|
|
|
memcpy(&data_attr[width * i],
|
|
|
|
terminal_get_attr_row(terminal, i),
|
|
|
|
l * sizeof(struct attr));
|
|
|
|
}
|
|
|
|
|
|
|
|
free(terminal->data);
|
|
|
|
free(terminal->data_attr);
|
|
|
|
free(terminal->tab_ruler);
|
2011-01-07 22:46:54 +03:00
|
|
|
}
|
2008-12-08 21:50:07 +03:00
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
terminal->data_pitch = data_pitch;
|
|
|
|
terminal->attr_pitch = attr_pitch;
|
|
|
|
terminal->data = data;
|
|
|
|
terminal->data_attr = data_attr;
|
|
|
|
terminal->tab_ruler = tab_ruler;
|
|
|
|
terminal_init_tabs(terminal);
|
|
|
|
terminal->start = 0;
|
2008-12-08 21:50:07 +03:00
|
|
|
}
|
|
|
|
|
2011-01-07 22:47:03 +03:00
|
|
|
terminal->margin_bottom =
|
|
|
|
height - (terminal->height - terminal->margin_bottom);
|
2008-12-08 21:50:07 +03:00
|
|
|
terminal->width = width;
|
|
|
|
terminal->height = height;
|
|
|
|
|
2011-01-07 22:46:56 +03:00
|
|
|
/* Update the window size */
|
|
|
|
ws.ws_row = terminal->height;
|
|
|
|
ws.ws_col = terminal->width;
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(terminal->widget, &allocation);
|
2011-01-11 18:00:52 +03:00
|
|
|
ws.ws_xpixel = allocation.width;
|
|
|
|
ws.ws_ypixel = allocation.height;
|
2011-01-07 22:46:56 +03:00
|
|
|
ioctl(terminal->master, TIOCSWINSZ, &ws);
|
2008-12-08 21:50:07 +03:00
|
|
|
}
|
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
static void
|
|
|
|
resize_handler(struct widget *widget,
|
|
|
|
int32_t width, int32_t height, void *data)
|
|
|
|
{
|
|
|
|
struct terminal *terminal = data;
|
|
|
|
int32_t columns, rows, m;
|
2013-10-24 07:47:35 +04:00
|
|
|
char *p;
|
2012-01-11 04:11:42 +04:00
|
|
|
m = 2 * terminal->margin;
|
2013-06-06 11:32:41 +04:00
|
|
|
columns = (width - m) / (int32_t) terminal->average_width;
|
2012-01-11 04:11:42 +04:00
|
|
|
rows = (height - m) / (int32_t) terminal->extents.height;
|
|
|
|
|
2012-10-10 19:41:21 +04:00
|
|
|
if (!window_is_fullscreen(terminal->window) &&
|
|
|
|
!window_is_maximized(terminal->window)) {
|
2013-06-06 11:32:41 +04:00
|
|
|
width = columns * terminal->average_width + m;
|
2012-01-11 04:11:42 +04:00
|
|
|
height = rows * terminal->extents.height + m;
|
|
|
|
widget_set_size(terminal->widget, width, height);
|
2013-10-24 07:47:35 +04:00
|
|
|
asprintf(&p, "%s — [%dx%d]", terminal->title, columns, rows);
|
|
|
|
window_set_title(terminal->window, p);
|
|
|
|
free(p);
|
2012-01-11 04:11:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
terminal_resize_cells(terminal, columns, rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
terminal_resize(struct terminal *terminal, int columns, int rows)
|
|
|
|
{
|
|
|
|
int32_t width, height, m;
|
|
|
|
|
2012-10-10 19:41:21 +04:00
|
|
|
if (window_is_fullscreen(terminal->window) ||
|
|
|
|
window_is_maximized(terminal->window))
|
2012-01-11 04:11:42 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
m = 2 * terminal->margin;
|
2013-06-06 11:32:41 +04:00
|
|
|
width = columns * terminal->average_width + m;
|
2012-01-11 04:11:42 +04:00
|
|
|
height = rows * terminal->extents.height + m;
|
2012-06-21 01:30:03 +04:00
|
|
|
|
2013-10-14 04:08:38 +04:00
|
|
|
window_frame_set_child_size(terminal->widget, width, height);
|
2012-01-11 04:11:42 +04:00
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
struct color_scheme DEFAULT_COLORS = {
|
|
|
|
{
|
|
|
|
{0, 0, 0, 1}, /* black */
|
|
|
|
{0.66, 0, 0, 1}, /* red */
|
|
|
|
{0 , 0.66, 0, 1}, /* green */
|
|
|
|
{0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
|
|
|
|
{0 , 0 , 0.66, 1}, /* blue */
|
|
|
|
{0.66, 0 , 0.66, 1}, /* magenta */
|
|
|
|
{0, 0.66, 0.66, 1}, /* cyan */
|
|
|
|
{0.66, 0.66, 0.66, 1}, /* light grey */
|
|
|
|
{0.22, 0.33, 0.33, 1}, /* dark grey */
|
|
|
|
{1, 0.33, 0.33, 1}, /* high red */
|
|
|
|
{0.33, 1, 0.33, 1}, /* high green */
|
|
|
|
{1, 1, 0.33, 1}, /* high yellow */
|
|
|
|
{0.33, 0.33, 1, 1}, /* high blue */
|
|
|
|
{1, 0.33, 1, 1}, /* high magenta */
|
|
|
|
{0.33, 1, 1, 1}, /* high cyan */
|
|
|
|
{1, 1, 1, 1} /* white */
|
|
|
|
},
|
2011-01-11 18:13:00 +03:00
|
|
|
0, /* black border */
|
2011-01-07 22:46:55 +03:00
|
|
|
{7, 0, 0, } /* bg:black (0), fg:light gray (7) */
|
|
|
|
};
|
2009-09-28 21:08:50 +04:00
|
|
|
|
2011-01-11 18:11:39 +03:00
|
|
|
static void
|
|
|
|
terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
|
|
|
|
{
|
|
|
|
cairo_set_source_rgba(cr,
|
|
|
|
terminal->color_table[index].r,
|
|
|
|
terminal->color_table[index].g,
|
|
|
|
terminal->color_table[index].b,
|
|
|
|
terminal->color_table[index].a);
|
|
|
|
}
|
|
|
|
|
2011-01-21 23:18:55 +03:00
|
|
|
static void
|
|
|
|
terminal_send_selection(struct terminal *terminal, int fd)
|
|
|
|
{
|
|
|
|
int row, col;
|
|
|
|
union utf8_char *p_row;
|
|
|
|
union decoded_attr attr;
|
|
|
|
FILE *fp;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
fp = fdopen(fd, "w");
|
2013-08-09 03:12:55 +04:00
|
|
|
if (fp == NULL){
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2011-01-21 23:18:55 +03:00
|
|
|
for (row = 0; row < terminal->height; row++) {
|
|
|
|
p_row = terminal_get_row(terminal, row);
|
|
|
|
for (col = 0; col < terminal->width; col++) {
|
2013-08-19 06:59:21 +04:00
|
|
|
if (p_row[col].ch == 0x200B) /* space glyph */
|
|
|
|
continue;
|
2011-01-21 23:18:55 +03:00
|
|
|
/* get the attributes for this character cell */
|
|
|
|
terminal_decode_attr(terminal, row, col, &attr);
|
|
|
|
if (!attr.attr.s)
|
|
|
|
continue;
|
|
|
|
len = strnlen((char *) p_row[col].byte, 4);
|
2012-07-02 05:25:41 +04:00
|
|
|
if (len > 0)
|
|
|
|
fwrite(p_row[col].byte, 1, len, fp);
|
|
|
|
if (len == 0 || col == terminal->width - 1) {
|
|
|
|
fwrite("\n", 1, 1, fp);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-21 23:18:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2011-01-11 19:06:49 +03:00
|
|
|
struct glyph_run {
|
|
|
|
struct terminal *terminal;
|
|
|
|
cairo_t *cr;
|
2012-03-30 19:52:39 +04:00
|
|
|
unsigned int count;
|
2011-01-11 19:06:49 +03:00
|
|
|
union decoded_attr attr;
|
|
|
|
cairo_glyph_t glyphs[256], *g;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
|
|
|
|
{
|
|
|
|
run->terminal = terminal;
|
|
|
|
run->cr = cr;
|
|
|
|
run->g = run->glyphs;
|
|
|
|
run->count = 0;
|
|
|
|
run->attr.key = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
|
|
|
|
{
|
|
|
|
cairo_scaled_font_t *font;
|
|
|
|
|
|
|
|
if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
|
|
|
|
(attr.key != run->attr.key)) {
|
2011-01-12 10:06:17 +03:00
|
|
|
if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
|
2011-01-11 19:06:49 +03:00
|
|
|
font = run->terminal->font_bold;
|
|
|
|
else
|
|
|
|
font = run->terminal->font_normal;
|
|
|
|
cairo_set_scaled_font(run->cr, font);
|
|
|
|
terminal_set_color(run->terminal, run->cr,
|
2011-01-12 10:06:17 +03:00
|
|
|
run->attr.attr.fg);
|
2011-01-11 19:06:49 +03:00
|
|
|
|
2011-01-12 10:06:17 +03:00
|
|
|
if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
|
|
|
|
cairo_show_glyphs (run->cr, run->glyphs, run->count);
|
2011-01-11 19:06:49 +03:00
|
|
|
run->g = run->glyphs;
|
|
|
|
run->count = 0;
|
|
|
|
}
|
2011-01-12 10:06:17 +03:00
|
|
|
run->attr = attr;
|
2011-01-11 19:06:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
|
|
|
|
{
|
|
|
|
int num_glyphs;
|
|
|
|
cairo_scaled_font_t *font;
|
|
|
|
|
|
|
|
num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
|
|
|
|
|
2011-01-12 10:06:17 +03:00
|
|
|
if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
|
2011-01-11 19:06:49 +03:00
|
|
|
font = run->terminal->font_bold;
|
|
|
|
else
|
|
|
|
font = run->terminal->font_normal;
|
|
|
|
|
|
|
|
cairo_move_to(run->cr, x, y);
|
|
|
|
cairo_scaled_font_text_to_glyphs (font, x, y,
|
|
|
|
(char *) c->byte, 4,
|
|
|
|
&run->g, &num_glyphs,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
run->g += num_glyphs;
|
|
|
|
run->count += num_glyphs;
|
|
|
|
}
|
|
|
|
|
2012-01-10 21:23:19 +04:00
|
|
|
|
2008-12-08 05:51:58 +03:00
|
|
|
static void
|
2012-01-10 21:23:19 +04:00
|
|
|
redraw_handler(struct widget *widget, void *data)
|
2008-12-08 03:59:11 +03:00
|
|
|
{
|
2012-01-10 21:23:19 +04:00
|
|
|
struct terminal *terminal = data;
|
2011-01-11 18:00:52 +03:00
|
|
|
struct rectangle allocation;
|
2008-12-08 05:51:58 +03:00
|
|
|
cairo_t *cr;
|
2011-01-07 22:46:54 +03:00
|
|
|
int top_margin, side_margin;
|
2012-05-28 00:25:02 +04:00
|
|
|
int row, col, cursor_x, cursor_y;
|
2011-01-07 22:46:54 +03:00
|
|
|
union utf8_char *p_row;
|
2011-01-11 19:06:49 +03:00
|
|
|
union decoded_attr attr;
|
2011-01-07 22:46:54 +03:00
|
|
|
int text_x, text_y;
|
2009-12-21 18:04:53 +03:00
|
|
|
cairo_surface_t *surface;
|
2009-02-23 07:01:35 +03:00
|
|
|
double d;
|
2011-01-11 19:06:49 +03:00
|
|
|
struct glyph_run run;
|
2011-01-20 19:56:57 +03:00
|
|
|
cairo_font_extents_t extents;
|
2013-06-06 11:32:41 +04:00
|
|
|
double average_width;
|
2013-08-19 06:59:21 +04:00
|
|
|
double unichar_width;
|
2008-12-08 05:51:58 +03:00
|
|
|
|
2011-01-20 20:37:33 +03:00
|
|
|
surface = window_get_surface(terminal->window);
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(terminal->widget, &allocation);
|
2013-05-22 16:41:34 +04:00
|
|
|
cr = widget_cairo_create(terminal->widget);
|
2011-01-20 20:37:33 +03:00
|
|
|
cairo_rectangle(cr, allocation.x, allocation.y,
|
|
|
|
allocation.width, allocation.height);
|
|
|
|
cairo_clip(cr);
|
|
|
|
cairo_push_group(cr);
|
|
|
|
|
2008-12-08 05:51:58 +03:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
2011-01-11 18:13:00 +03:00
|
|
|
terminal_set_color(terminal, cr, terminal->color_scheme->border);
|
2008-12-08 05:51:58 +03:00
|
|
|
cairo_paint(cr);
|
2011-01-07 22:46:55 +03:00
|
|
|
|
2010-12-26 00:58:31 +03:00
|
|
|
cairo_set_scaled_font(cr, terminal->font_normal);
|
2008-12-08 05:51:58 +03:00
|
|
|
|
2011-01-20 19:56:57 +03:00
|
|
|
extents = terminal->extents;
|
2013-06-06 11:32:41 +04:00
|
|
|
average_width = terminal->average_width;
|
|
|
|
side_margin = (allocation.width - terminal->width * average_width) / 2;
|
2011-01-11 18:00:52 +03:00
|
|
|
top_margin = (allocation.height - terminal->height * extents.height) / 2;
|
2008-12-22 20:14:50 +03:00
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
cairo_set_line_width(cr, 1.0);
|
2011-01-20 20:37:33 +03:00
|
|
|
cairo_translate(cr, allocation.x + side_margin,
|
|
|
|
allocation.y + top_margin);
|
2010-12-25 16:58:46 +03:00
|
|
|
/* paint the background */
|
2011-01-07 22:46:54 +03:00
|
|
|
for (row = 0; row < terminal->height; row++) {
|
2013-08-19 06:59:21 +04:00
|
|
|
p_row = terminal_get_row(terminal, row);
|
2011-01-07 22:46:54 +03:00
|
|
|
for (col = 0; col < terminal->width; col++) {
|
2011-01-07 22:46:55 +03:00
|
|
|
/* get the attributes for this character cell */
|
2011-01-11 18:26:04 +03:00
|
|
|
terminal_decode_attr(terminal, row, col, &attr);
|
2011-01-07 22:46:55 +03:00
|
|
|
|
2011-01-12 10:06:17 +03:00
|
|
|
if (attr.attr.bg == terminal->color_scheme->border)
|
2011-01-11 18:15:21 +03:00
|
|
|
continue;
|
|
|
|
|
2013-08-19 06:59:21 +04:00
|
|
|
if (is_wide(p_row[col]))
|
|
|
|
unichar_width = 2 * average_width;
|
|
|
|
else
|
|
|
|
unichar_width = average_width;
|
|
|
|
|
2011-01-12 10:06:17 +03:00
|
|
|
terminal_set_color(terminal, cr, attr.attr.bg);
|
2013-06-06 11:32:41 +04:00
|
|
|
cairo_move_to(cr, col * average_width,
|
2011-01-20 20:37:33 +03:00
|
|
|
row * extents.height);
|
2013-08-19 06:59:21 +04:00
|
|
|
cairo_rel_line_to(cr, unichar_width, 0);
|
2011-01-07 22:46:55 +03:00
|
|
|
cairo_rel_line_to(cr, 0, extents.height);
|
2013-08-19 06:59:21 +04:00
|
|
|
cairo_rel_line_to(cr, -unichar_width, 0);
|
2011-01-07 22:46:55 +03:00
|
|
|
cairo_close_path(cr);
|
|
|
|
cairo_fill(cr);
|
2010-12-25 16:58:46 +03:00
|
|
|
}
|
|
|
|
}
|
2011-01-07 22:46:55 +03:00
|
|
|
|
2010-12-25 16:58:46 +03:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
|
|
|
|
|
|
|
/* paint the foreground */
|
2011-01-11 19:06:49 +03:00
|
|
|
glyph_run_init(&run, terminal, cr);
|
2010-12-25 16:58:46 +03:00
|
|
|
for (row = 0; row < terminal->height; row++) {
|
|
|
|
p_row = terminal_get_row(terminal, row);
|
|
|
|
for (col = 0; col < terminal->width; col++) {
|
|
|
|
/* get the attributes for this character cell */
|
2011-01-11 18:26:04 +03:00
|
|
|
terminal_decode_attr(terminal, row, col, &attr);
|
2011-01-07 22:46:55 +03:00
|
|
|
|
2011-01-11 19:06:49 +03:00
|
|
|
glyph_run_flush(&run, attr);
|
2011-01-07 22:46:55 +03:00
|
|
|
|
2013-06-06 11:32:41 +04:00
|
|
|
text_x = col * average_width;
|
2011-01-20 20:37:33 +03:00
|
|
|
text_y = extents.ascent + row * extents.height;
|
2011-01-12 10:06:17 +03:00
|
|
|
if (attr.attr.a & ATTRMASK_UNDERLINE) {
|
|
|
|
terminal_set_color(terminal, cr, attr.attr.fg);
|
2011-01-07 22:47:03 +03:00
|
|
|
cairo_move_to(cr, text_x, (double)text_y + 1.5);
|
2013-06-06 11:32:41 +04:00
|
|
|
cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
|
2011-01-07 22:46:55 +03:00
|
|
|
cairo_stroke(cr);
|
|
|
|
}
|
2011-01-11 19:06:49 +03:00
|
|
|
|
|
|
|
glyph_run_add(&run, text_x, text_y, &p_row[col]);
|
2011-01-07 22:46:54 +03:00
|
|
|
}
|
2008-12-08 05:51:58 +03:00
|
|
|
}
|
2009-02-21 23:42:25 +03:00
|
|
|
|
2011-01-11 19:06:49 +03:00
|
|
|
attr.key = ~0;
|
|
|
|
glyph_run_flush(&run, attr);
|
|
|
|
|
2012-08-14 06:25:53 +04:00
|
|
|
if ((terminal->mode & MODE_SHOW_CURSOR) &&
|
|
|
|
!window_has_focus(terminal->window)) {
|
2011-01-07 22:46:55 +03:00
|
|
|
d = 0.5;
|
2009-02-23 07:01:35 +03:00
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
cairo_set_line_width(cr, 1);
|
2013-06-06 11:32:41 +04:00
|
|
|
cairo_move_to(cr, terminal->column * average_width + d,
|
2011-01-20 20:37:33 +03:00
|
|
|
terminal->row * extents.height + d);
|
2013-06-06 11:32:41 +04:00
|
|
|
cairo_rel_line_to(cr, average_width - 2 * d, 0);
|
2011-01-07 22:46:55 +03:00
|
|
|
cairo_rel_line_to(cr, 0, extents.height - 2 * d);
|
2013-06-06 11:32:41 +04:00
|
|
|
cairo_rel_line_to(cr, -average_width + 2 * d, 0);
|
2011-01-07 22:46:55 +03:00
|
|
|
cairo_close_path(cr);
|
2009-02-23 07:01:35 +03:00
|
|
|
|
|
|
|
cairo_stroke(cr);
|
2011-01-07 22:46:55 +03:00
|
|
|
}
|
2009-02-21 23:42:25 +03:00
|
|
|
|
2011-01-20 20:37:33 +03:00
|
|
|
cairo_pop_group_to_source(cr);
|
|
|
|
cairo_paint(cr);
|
2008-12-08 05:51:58 +03:00
|
|
|
cairo_destroy(cr);
|
2009-12-21 18:04:53 +03:00
|
|
|
cairo_surface_destroy(surface);
|
2012-05-28 00:25:02 +04:00
|
|
|
|
|
|
|
if (terminal->send_cursor_position) {
|
|
|
|
cursor_x = side_margin + allocation.x +
|
2013-06-06 11:32:41 +04:00
|
|
|
terminal->column * average_width;
|
2012-05-28 00:25:02 +04:00
|
|
|
cursor_y = top_margin + allocation.y +
|
|
|
|
terminal->row * extents.height;
|
|
|
|
window_set_text_cursor_position(terminal->window,
|
|
|
|
cursor_x, cursor_y);
|
|
|
|
terminal->send_cursor_position = 0;
|
|
|
|
}
|
2008-12-08 05:51:58 +03:00
|
|
|
}
|
|
|
|
|
2011-08-24 19:30:21 +04:00
|
|
|
static void
|
|
|
|
terminal_write(struct terminal *terminal, const char *data, size_t length)
|
|
|
|
{
|
|
|
|
if (write(terminal->master, data, length) < 0)
|
|
|
|
abort();
|
2012-05-28 00:25:02 +04:00
|
|
|
terminal->send_cursor_position = 1;
|
2011-08-24 19:30:21 +04:00
|
|
|
}
|
|
|
|
|
2008-12-08 20:20:40 +03:00
|
|
|
static void
|
|
|
|
terminal_data(struct terminal *terminal, const char *data, size_t length);
|
2008-12-08 08:07:49 +03:00
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
static void
|
|
|
|
handle_char(struct terminal *terminal, union utf8_char utf8);
|
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
static void
|
|
|
|
handle_sgr(struct terminal *terminal, int code);
|
|
|
|
|
2011-01-07 22:46:58 +03:00
|
|
|
static void
|
|
|
|
handle_term_parameter(struct terminal *terminal, int code, int sr)
|
|
|
|
{
|
2011-01-07 22:46:59 +03:00
|
|
|
int i;
|
|
|
|
|
2011-01-12 09:23:41 +03:00
|
|
|
if (terminal->escape_flags & ESC_FLAG_WHAT) {
|
2011-01-07 22:46:58 +03:00
|
|
|
switch(code) {
|
2011-01-07 22:47:02 +03:00
|
|
|
case 1: /* DECCKM */
|
|
|
|
if (sr) terminal->key_mode = KM_APPLICATION;
|
|
|
|
else terminal->key_mode = KM_NORMAL;
|
|
|
|
break;
|
2011-01-07 22:47:01 +03:00
|
|
|
case 2: /* DECANM */
|
|
|
|
/* No VT52 support yet */
|
|
|
|
terminal->g0 = CS_US;
|
|
|
|
terminal->g1 = CS_US;
|
|
|
|
terminal->cs = terminal->g0;
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 3: /* DECCOLM */
|
|
|
|
if (sr)
|
|
|
|
terminal_resize(terminal, 132, 24);
|
|
|
|
else
|
|
|
|
terminal_resize(terminal, 80, 24);
|
|
|
|
|
|
|
|
/* set columns, but also home cursor and clear screen */
|
|
|
|
terminal->row = 0; terminal->column = 0;
|
|
|
|
for (i = 0; i < terminal->height; i++) {
|
|
|
|
memset(terminal_get_row(terminal, i),
|
|
|
|
0, terminal->data_pitch);
|
|
|
|
attr_init(terminal_get_attr_row(terminal, i),
|
|
|
|
terminal->curr_attr, terminal->width);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5: /* DECSCNM */
|
|
|
|
if (sr) terminal->mode |= MODE_INVERSE;
|
|
|
|
else terminal->mode &= ~MODE_INVERSE;
|
|
|
|
break;
|
2011-01-07 22:46:58 +03:00
|
|
|
case 6: /* DECOM */
|
|
|
|
terminal->origin_mode = sr;
|
|
|
|
if (terminal->origin_mode)
|
|
|
|
terminal->row = terminal->margin_top;
|
|
|
|
else
|
|
|
|
terminal->row = 0;
|
|
|
|
terminal->column = 0;
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 7: /* DECAWM */
|
|
|
|
if (sr) terminal->mode |= MODE_AUTOWRAP;
|
|
|
|
else terminal->mode &= ~MODE_AUTOWRAP;
|
|
|
|
break;
|
|
|
|
case 8: /* DECARM */
|
|
|
|
if (sr) terminal->mode |= MODE_AUTOREPEAT;
|
|
|
|
else terminal->mode &= ~MODE_AUTOREPEAT;
|
|
|
|
break;
|
2012-06-21 00:59:17 +04:00
|
|
|
case 12: /* Very visible cursor (CVVIS) */
|
|
|
|
/* FIXME: What do we do here. */
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 25:
|
|
|
|
if (sr) terminal->mode |= MODE_SHOW_CURSOR;
|
|
|
|
else terminal->mode &= ~MODE_SHOW_CURSOR;
|
|
|
|
break;
|
2012-06-21 00:59:17 +04:00
|
|
|
case 1034: /* smm/rmm, meta mode on/off */
|
|
|
|
/* ignore */
|
|
|
|
break;
|
2011-01-07 22:47:02 +03:00
|
|
|
case 1037: /* deleteSendsDel */
|
|
|
|
if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
|
|
|
|
else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
|
|
|
|
break;
|
|
|
|
case 1039: /* altSendsEscape */
|
|
|
|
if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
|
|
|
|
else terminal->mode &= ~MODE_ALT_SENDS_ESC;
|
|
|
|
break;
|
2012-06-21 00:59:17 +04:00
|
|
|
case 1049: /* rmcup/smcup, alternate screen */
|
|
|
|
/* Ignore. Should be possible to implement,
|
|
|
|
* but it's kind of annoying. */
|
|
|
|
break;
|
2011-01-07 22:46:58 +03:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown parameter: ?%d\n", code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(code) {
|
2011-01-07 22:47:00 +03:00
|
|
|
case 4: /* IRM */
|
|
|
|
if (sr) terminal->mode |= MODE_IRM;
|
|
|
|
else terminal->mode &= ~MODE_IRM;
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 20: /* LNM */
|
|
|
|
if (sr) terminal->mode |= MODE_LF_NEWLINE;
|
|
|
|
else terminal->mode &= ~MODE_LF_NEWLINE;
|
|
|
|
break;
|
2011-01-07 22:46:58 +03:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown parameter: %d\n", code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 09:23:41 +03:00
|
|
|
static void
|
|
|
|
handle_dcs(struct terminal *terminal)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_osc(struct terminal *terminal)
|
|
|
|
{
|
2011-01-14 10:46:23 +03:00
|
|
|
char *p;
|
|
|
|
int code;
|
|
|
|
|
|
|
|
terminal->escape[terminal->escape_length++] = '\0';
|
|
|
|
p = &terminal->escape[2];
|
|
|
|
code = strtol(p, &p, 10);
|
|
|
|
if (*p == ';') p++;
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case 0: /* Icon name and window title */
|
|
|
|
case 1: /* Icon label */
|
|
|
|
case 2: /* Window title*/
|
2013-10-24 07:47:35 +04:00
|
|
|
free(terminal->title);
|
|
|
|
terminal->title = strdup(p);
|
2011-01-14 10:46:23 +03:00
|
|
|
window_set_title(terminal->window, p);
|
|
|
|
break;
|
2013-09-11 22:52:56 +04:00
|
|
|
case 7: /* shell cwd as uri */
|
|
|
|
break;
|
2011-01-14 10:46:23 +03:00
|
|
|
default:
|
2013-09-11 22:52:56 +04:00
|
|
|
fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
|
|
|
|
code, p);
|
2011-01-14 10:46:23 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-01-12 09:23:41 +03:00
|
|
|
}
|
|
|
|
|
2008-12-08 20:20:40 +03:00
|
|
|
static void
|
|
|
|
handle_escape(struct terminal *terminal)
|
|
|
|
{
|
2011-01-07 22:46:54 +03:00
|
|
|
union utf8_char *row;
|
2011-01-07 22:46:55 +03:00
|
|
|
struct attr *attr_row;
|
2011-01-07 22:46:54 +03:00
|
|
|
char *p;
|
2011-01-07 22:46:59 +03:00
|
|
|
int i, count, x, y, top, bottom;
|
2008-12-09 06:22:25 +03:00
|
|
|
int args[10], set[10] = { 0, };
|
2011-01-07 22:46:59 +03:00
|
|
|
char response[MAX_RESPONSE] = {0, };
|
2011-01-14 10:46:23 +03:00
|
|
|
struct rectangle allocation;
|
2008-12-08 20:20:40 +03:00
|
|
|
|
|
|
|
terminal->escape[terminal->escape_length++] = '\0';
|
2008-12-09 06:22:25 +03:00
|
|
|
i = 0;
|
|
|
|
p = &terminal->escape[2];
|
|
|
|
while ((isdigit(*p) || *p == ';') && i < 10) {
|
|
|
|
if (*p == ';') {
|
2011-01-07 22:46:55 +03:00
|
|
|
if (!set[i]) {
|
|
|
|
args[i] = 0;
|
|
|
|
set[i] = 1;
|
|
|
|
}
|
2008-12-09 06:22:25 +03:00
|
|
|
p++;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
args[i] = strtol(p, &p, 10);
|
|
|
|
set[i] = 1;
|
2008-12-08 20:20:40 +03:00
|
|
|
}
|
|
|
|
}
|
2008-12-09 06:22:25 +03:00
|
|
|
|
|
|
|
switch (*p) {
|
2011-01-07 22:47:00 +03:00
|
|
|
case '@': /* ICH */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
terminal_shift_line(terminal, count);
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'A': /* CUU */
|
2008-12-09 06:22:25 +03:00
|
|
|
count = set[0] ? args[0] : 1;
|
2011-01-07 22:46:59 +03:00
|
|
|
if (count == 0) count = 1;
|
|
|
|
if (terminal->row - count >= terminal->margin_top)
|
2008-12-09 06:22:25 +03:00
|
|
|
terminal->row -= count;
|
|
|
|
else
|
2011-01-07 22:46:59 +03:00
|
|
|
terminal->row = terminal->margin_top;
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'B': /* CUD */
|
2008-12-09 06:22:25 +03:00
|
|
|
count = set[0] ? args[0] : 1;
|
2011-01-07 22:46:59 +03:00
|
|
|
if (count == 0) count = 1;
|
|
|
|
if (terminal->row + count <= terminal->margin_bottom)
|
2008-12-09 06:22:25 +03:00
|
|
|
terminal->row += count;
|
|
|
|
else
|
2011-01-07 22:46:59 +03:00
|
|
|
terminal->row = terminal->margin_bottom;
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'C': /* CUF */
|
2008-12-09 06:22:25 +03:00
|
|
|
count = set[0] ? args[0] : 1;
|
2011-01-07 22:46:59 +03:00
|
|
|
if (count == 0) count = 1;
|
|
|
|
if ((terminal->column + count) < terminal->width)
|
2008-12-09 06:22:25 +03:00
|
|
|
terminal->column += count;
|
|
|
|
else
|
2011-01-07 22:46:59 +03:00
|
|
|
terminal->column = terminal->width - 1;
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'D': /* CUB */
|
2008-12-09 06:22:25 +03:00
|
|
|
count = set[0] ? args[0] : 1;
|
2011-01-07 22:46:59 +03:00
|
|
|
if (count == 0) count = 1;
|
|
|
|
if ((terminal->column - count) >= 0)
|
2008-12-09 06:22:25 +03:00
|
|
|
terminal->column -= count;
|
|
|
|
else
|
|
|
|
terminal->column = 0;
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'E': /* CNL */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (terminal->row + count <= terminal->margin_bottom)
|
|
|
|
terminal->row += count;
|
|
|
|
else
|
|
|
|
terminal->row = terminal->margin_bottom;
|
|
|
|
terminal->column = 0;
|
|
|
|
break;
|
|
|
|
case 'F': /* CPL */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (terminal->row - count >= terminal->margin_top)
|
|
|
|
terminal->row -= count;
|
|
|
|
else
|
|
|
|
terminal->row = terminal->margin_top;
|
|
|
|
terminal->column = 0;
|
|
|
|
break;
|
|
|
|
case 'G': /* CHA */
|
|
|
|
y = set[0] ? args[0] : 1;
|
|
|
|
y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
|
|
|
|
|
|
|
|
terminal->column = y - 1;
|
|
|
|
break;
|
|
|
|
case 'f': /* HVP */
|
|
|
|
case 'H': /* CUP */
|
|
|
|
x = (set[1] ? args[1] : 1) - 1;
|
|
|
|
x = x < 0 ? 0 :
|
|
|
|
(x >= terminal->width ? terminal->width - 1 : x);
|
|
|
|
|
|
|
|
y = (set[0] ? args[0] : 1) - 1;
|
|
|
|
if (terminal->origin_mode) {
|
|
|
|
y += terminal->margin_top;
|
|
|
|
y = y < terminal->margin_top ? terminal->margin_top :
|
|
|
|
(y > terminal->margin_bottom ? terminal->margin_bottom : y);
|
|
|
|
} else {
|
|
|
|
y = y < 0 ? 0 :
|
|
|
|
(y >= terminal->height ? terminal->height - 1 : y);
|
2011-01-07 22:46:55 +03:00
|
|
|
}
|
2011-01-07 22:46:59 +03:00
|
|
|
|
|
|
|
terminal->row = y;
|
|
|
|
terminal->column = x;
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'I': /* CHT */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
while (count > 0 && terminal->column < terminal->width) {
|
|
|
|
if (terminal->tab_ruler[terminal->column]) count--;
|
|
|
|
terminal->column++;
|
|
|
|
}
|
|
|
|
terminal->column--;
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'J': /* ED */
|
|
|
|
row = terminal_get_row(terminal, terminal->row);
|
|
|
|
attr_row = terminal_get_attr_row(terminal, terminal->row);
|
|
|
|
if (!set[0] || args[0] == 0 || args[0] > 2) {
|
|
|
|
memset(&row[terminal->column],
|
|
|
|
0, (terminal->width - terminal->column) * sizeof(union utf8_char));
|
|
|
|
attr_init(&attr_row[terminal->column],
|
|
|
|
terminal->curr_attr, terminal->width - terminal->column);
|
|
|
|
for (i = terminal->row + 1; i < terminal->height; i++) {
|
|
|
|
memset(terminal_get_row(terminal, i),
|
|
|
|
0, terminal->data_pitch);
|
|
|
|
attr_init(terminal_get_attr_row(terminal, i),
|
|
|
|
terminal->curr_attr, terminal->width);
|
|
|
|
}
|
|
|
|
} else if (args[0] == 1) {
|
|
|
|
memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
|
|
|
|
attr_init(attr_row, terminal->curr_attr, terminal->column+1);
|
|
|
|
for (i = 0; i < terminal->row; i++) {
|
|
|
|
memset(terminal_get_row(terminal, i),
|
|
|
|
0, terminal->data_pitch);
|
|
|
|
attr_init(terminal_get_attr_row(terminal, i),
|
|
|
|
terminal->curr_attr, terminal->width);
|
|
|
|
}
|
|
|
|
} else if (args[0] == 2) {
|
2013-10-24 03:29:14 +04:00
|
|
|
/* Clear screen by scrolling contents out */
|
|
|
|
terminal_scroll_buffer(terminal,
|
|
|
|
terminal->end - terminal->start);
|
2011-01-07 22:46:59 +03:00
|
|
|
}
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'K': /* EL */
|
2008-12-09 06:22:25 +03:00
|
|
|
row = terminal_get_row(terminal, terminal->row);
|
2011-01-07 22:46:55 +03:00
|
|
|
attr_row = terminal_get_attr_row(terminal, terminal->row);
|
2011-01-07 22:46:59 +03:00
|
|
|
if (!set[0] || args[0] == 0 || args[0] > 2) {
|
|
|
|
memset(&row[terminal->column], 0,
|
|
|
|
(terminal->width - terminal->column) * sizeof(union utf8_char));
|
|
|
|
attr_init(&attr_row[terminal->column], terminal->curr_attr,
|
|
|
|
terminal->width - terminal->column);
|
|
|
|
} else if (args[0] == 1) {
|
|
|
|
memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
|
|
|
|
attr_init(attr_row, terminal->curr_attr, terminal->column+1);
|
|
|
|
} else if (args[0] == 2) {
|
|
|
|
memset(row, 0, terminal->data_pitch);
|
|
|
|
attr_init(attr_row, terminal->curr_attr, terminal->width);
|
|
|
|
}
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:47:00 +03:00
|
|
|
case 'L': /* IL */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
if (terminal->row >= terminal->margin_top &&
|
|
|
|
terminal->row < terminal->margin_bottom)
|
|
|
|
{
|
|
|
|
top = terminal->margin_top;
|
|
|
|
terminal->margin_top = terminal->row;
|
|
|
|
terminal_scroll(terminal, 0 - count);
|
|
|
|
terminal->margin_top = top;
|
|
|
|
} else if (terminal->row == terminal->margin_bottom) {
|
|
|
|
memset(terminal_get_row(terminal, terminal->row),
|
|
|
|
0, terminal->data_pitch);
|
|
|
|
attr_init(terminal_get_attr_row(terminal, terminal->row),
|
|
|
|
terminal->curr_attr, terminal->width);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'M': /* DL */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
if (terminal->row >= terminal->margin_top &&
|
|
|
|
terminal->row < terminal->margin_bottom)
|
|
|
|
{
|
|
|
|
top = terminal->margin_top;
|
|
|
|
terminal->margin_top = terminal->row;
|
|
|
|
terminal_scroll(terminal, count);
|
|
|
|
terminal->margin_top = top;
|
|
|
|
} else if (terminal->row == terminal->margin_bottom) {
|
|
|
|
memset(terminal_get_row(terminal, terminal->row),
|
|
|
|
0, terminal->data_pitch);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'P': /* DCH */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
terminal_shift_line(terminal, 0 - count);
|
|
|
|
break;
|
2011-01-07 22:46:58 +03:00
|
|
|
case 'S': /* SU */
|
|
|
|
terminal_scroll(terminal, set[0] ? args[0] : 1);
|
|
|
|
break;
|
|
|
|
case 'T': /* SD */
|
|
|
|
terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
|
|
|
|
break;
|
2011-01-07 22:47:00 +03:00
|
|
|
case 'X': /* ECH */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
if ((terminal->column + count) > terminal->width)
|
|
|
|
count = terminal->width - terminal->column;
|
|
|
|
row = terminal_get_row(terminal, terminal->row);
|
|
|
|
attr_row = terminal_get_attr_row(terminal, terminal->row);
|
|
|
|
memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
|
|
|
|
attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'Z': /* CBT */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
while (count > 0 && terminal->column >= 0) {
|
|
|
|
if (terminal->tab_ruler[terminal->column]) count--;
|
|
|
|
terminal->column--;
|
|
|
|
}
|
|
|
|
terminal->column++;
|
|
|
|
break;
|
|
|
|
case '`': /* HPA */
|
|
|
|
y = set[0] ? args[0] : 1;
|
|
|
|
y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
|
|
|
|
|
|
|
|
terminal->column = y - 1;
|
|
|
|
break;
|
|
|
|
case 'b': /* REP */
|
|
|
|
count = set[0] ? args[0] : 1;
|
|
|
|
if (count == 0) count = 1;
|
|
|
|
if (terminal->last_char.byte[0])
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
handle_char(terminal, terminal->last_char);
|
|
|
|
terminal->last_char.byte[0] = 0;
|
|
|
|
break;
|
|
|
|
case 'c': /* Primary DA */
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, "\e[?6c", 5);
|
2011-01-07 22:46:59 +03:00
|
|
|
break;
|
|
|
|
case 'd': /* VPA */
|
|
|
|
x = set[0] ? args[0] : 1;
|
|
|
|
x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
|
|
|
|
|
|
|
|
terminal->row = x - 1;
|
|
|
|
break;
|
|
|
|
case 'g': /* TBC */
|
|
|
|
if (!set[0] || args[0] == 0) {
|
|
|
|
terminal->tab_ruler[terminal->column] = 0;
|
|
|
|
} else if (args[0] == 3) {
|
|
|
|
memset(terminal->tab_ruler, 0, terminal->width);
|
|
|
|
}
|
|
|
|
break;
|
2011-01-07 22:46:58 +03:00
|
|
|
case 'h': /* SM */
|
|
|
|
for(i = 0; i < 10 && set[i]; i++) {
|
|
|
|
handle_term_parameter(terminal, args[i], 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l': /* RM */
|
|
|
|
for(i = 0; i < 10 && set[i]; i++) {
|
|
|
|
handle_term_parameter(terminal, args[i], 0);
|
|
|
|
}
|
|
|
|
break;
|
2011-01-07 22:46:55 +03:00
|
|
|
case 'm': /* SGR */
|
|
|
|
for(i = 0; i < 10; i++) {
|
2011-01-10 02:14:01 +03:00
|
|
|
if (i <= 7 && set[i] && set[i + 1] &&
|
|
|
|
set[i + 2] && args[i + 1] == 5)
|
|
|
|
{
|
|
|
|
if (args[i] == 38) {
|
|
|
|
handle_sgr(terminal, args[i + 2] + 256);
|
|
|
|
break;
|
|
|
|
} else if (args[i] == 48) {
|
|
|
|
handle_sgr(terminal, args[i + 2] + 512);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-07 22:46:55 +03:00
|
|
|
if(set[i]) {
|
|
|
|
handle_sgr(terminal, args[i]);
|
|
|
|
} else if(i == 0) {
|
|
|
|
handle_sgr(terminal, 0);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'n': /* DSR */
|
|
|
|
i = set[0] ? args[0] : 0;
|
|
|
|
if (i == 0 || i == 5) {
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, "\e[0n", 4);
|
2011-01-07 22:46:59 +03:00
|
|
|
} else if (i == 6) {
|
|
|
|
snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
|
|
|
|
terminal->origin_mode ?
|
|
|
|
terminal->row+terminal->margin_top : terminal->row+1,
|
|
|
|
terminal->column+1);
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, response, strlen(response));
|
2011-01-07 22:46:59 +03:00
|
|
|
}
|
|
|
|
break;
|
2011-01-07 22:46:58 +03:00
|
|
|
case 'r':
|
|
|
|
if(!set[0]) {
|
|
|
|
terminal->margin_top = 0;
|
|
|
|
terminal->margin_bottom = terminal->height-1;
|
|
|
|
terminal->row = 0;
|
|
|
|
terminal->column = 0;
|
|
|
|
} else {
|
|
|
|
top = (set[0] ? args[0] : 1) - 1;
|
|
|
|
top = top < 0 ? 0 :
|
|
|
|
(top >= terminal->height ? terminal->height - 1 : top);
|
|
|
|
bottom = (set[1] ? args[1] : 1) - 1;
|
|
|
|
bottom = bottom < 0 ? 0 :
|
|
|
|
(bottom >= terminal->height ? terminal->height - 1 : bottom);
|
|
|
|
if(bottom > top) {
|
|
|
|
terminal->margin_top = top;
|
|
|
|
terminal->margin_bottom = bottom;
|
|
|
|
} else {
|
|
|
|
terminal->margin_top = 0;
|
|
|
|
terminal->margin_bottom = terminal->height-1;
|
|
|
|
}
|
|
|
|
if(terminal->origin_mode)
|
|
|
|
terminal->row = terminal->margin_top;
|
|
|
|
else
|
|
|
|
terminal->row = 0;
|
|
|
|
terminal->column = 0;
|
2008-12-09 06:22:25 +03:00
|
|
|
}
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 's':
|
|
|
|
terminal->saved_row = terminal->row;
|
|
|
|
terminal->saved_column = terminal->column;
|
|
|
|
break;
|
2011-01-14 10:46:23 +03:00
|
|
|
case 't': /* windowOps */
|
|
|
|
if (!set[0]) break;
|
|
|
|
switch (args[0]) {
|
|
|
|
case 4: /* resize px */
|
|
|
|
if (set[1] && set[2]) {
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_schedule_resize(terminal->widget,
|
|
|
|
args[2], args[1]);
|
2011-01-14 10:46:23 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8: /* resize ch */
|
|
|
|
if (set[1] && set[2]) {
|
|
|
|
terminal_resize(terminal, args[2], args[1]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 13: /* report position */
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(terminal->widget, &allocation);
|
2011-01-14 10:46:23 +03:00
|
|
|
snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
|
|
|
|
allocation.x, allocation.y);
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, response, strlen(response));
|
2011-01-14 10:46:23 +03:00
|
|
|
break;
|
|
|
|
case 14: /* report px */
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(terminal->widget, &allocation);
|
2011-01-14 10:46:23 +03:00
|
|
|
snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
|
|
|
|
allocation.height, allocation.width);
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, response, strlen(response));
|
2011-01-14 10:46:23 +03:00
|
|
|
break;
|
|
|
|
case 18: /* report ch */
|
|
|
|
snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
|
|
|
|
terminal->height, terminal->width);
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, response, strlen(response));
|
2011-01-14 10:46:23 +03:00
|
|
|
break;
|
|
|
|
case 21: /* report title */
|
|
|
|
snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
|
|
|
|
window_get_title(terminal->window));
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, response, strlen(response));
|
2011-01-14 10:46:23 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (args[0] >= 24)
|
|
|
|
terminal_resize(terminal, terminal->width, args[0]);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'u':
|
|
|
|
terminal->row = terminal->saved_row;
|
|
|
|
terminal->column = terminal->saved_column;
|
|
|
|
break;
|
2008-12-09 06:22:25 +03:00
|
|
|
default:
|
2011-01-07 22:46:57 +03:00
|
|
|
fprintf(stderr, "Unknown CSI escape: %c\n", *p);
|
2008-12-09 06:22:25 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-12-08 20:20:40 +03:00
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
static void
|
|
|
|
handle_non_csi_escape(struct terminal *terminal, char code)
|
|
|
|
{
|
2011-01-07 22:46:58 +03:00
|
|
|
switch(code) {
|
|
|
|
case 'M': /* RI */
|
|
|
|
terminal->row -= 1;
|
|
|
|
if(terminal->row < terminal->margin_top) {
|
|
|
|
terminal->row = terminal->margin_top;
|
|
|
|
terminal_scroll(terminal, -1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'E': /* NEL */
|
|
|
|
terminal->column = 0;
|
|
|
|
// fallthrough
|
|
|
|
case 'D': /* IND */
|
|
|
|
terminal->row += 1;
|
|
|
|
if(terminal->row > terminal->margin_bottom) {
|
|
|
|
terminal->row = terminal->margin_bottom;
|
|
|
|
terminal_scroll(terminal, +1);
|
|
|
|
}
|
|
|
|
break;
|
2011-01-07 22:46:59 +03:00
|
|
|
case 'c': /* RIS */
|
|
|
|
terminal_init(terminal);
|
|
|
|
break;
|
|
|
|
case 'H': /* HTS */
|
|
|
|
terminal->tab_ruler[terminal->column] = 1;
|
|
|
|
break;
|
|
|
|
case '7': /* DECSC */
|
|
|
|
terminal->saved_row = terminal->row;
|
|
|
|
terminal->saved_column = terminal->column;
|
|
|
|
terminal->saved_attr = terminal->curr_attr;
|
|
|
|
terminal->saved_origin_mode = terminal->origin_mode;
|
2011-01-07 22:47:01 +03:00
|
|
|
terminal->saved_cs = terminal->cs;
|
|
|
|
terminal->saved_g0 = terminal->g0;
|
|
|
|
terminal->saved_g1 = terminal->g1;
|
2011-01-07 22:46:59 +03:00
|
|
|
break;
|
|
|
|
case '8': /* DECRC */
|
|
|
|
terminal->row = terminal->saved_row;
|
|
|
|
terminal->column = terminal->saved_column;
|
|
|
|
terminal->curr_attr = terminal->saved_attr;
|
|
|
|
terminal->origin_mode = terminal->saved_origin_mode;
|
2011-01-07 22:47:01 +03:00
|
|
|
terminal->cs = terminal->saved_cs;
|
|
|
|
terminal->g0 = terminal->saved_g0;
|
|
|
|
terminal->g1 = terminal->saved_g1;
|
2011-01-07 22:46:59 +03:00
|
|
|
break;
|
2011-01-07 22:47:02 +03:00
|
|
|
case '=': /* DECPAM */
|
|
|
|
terminal->key_mode = KM_APPLICATION;
|
|
|
|
break;
|
|
|
|
case '>': /* DECPNM */
|
|
|
|
terminal->key_mode = KM_NORMAL;
|
|
|
|
break;
|
2011-01-07 22:46:58 +03:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown escape code: %c\n", code);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-07 22:46:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_special_escape(struct terminal *terminal, char special, char code)
|
|
|
|
{
|
2011-01-07 22:46:59 +03:00
|
|
|
int i, numChars;
|
|
|
|
|
|
|
|
if (special == '#') {
|
|
|
|
switch(code) {
|
|
|
|
case '8':
|
|
|
|
/* fill with 'E', no cheap way to do this */
|
|
|
|
memset(terminal->data, 0, terminal->data_pitch * terminal->height);
|
|
|
|
numChars = terminal->width * terminal->height;
|
|
|
|
for(i = 0; i < numChars; i++) {
|
|
|
|
terminal->data[i].byte[0] = 'E';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown HASH escape #%c\n", code);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-07 22:47:01 +03:00
|
|
|
} else if (special == '(' || special == ')') {
|
|
|
|
switch(code) {
|
|
|
|
case '0':
|
|
|
|
if (special == '(')
|
|
|
|
terminal->g0 = CS_SPECIAL;
|
|
|
|
else
|
|
|
|
terminal->g1 = CS_SPECIAL;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
if (special == '(')
|
|
|
|
terminal->g0 = CS_UK;
|
|
|
|
else
|
|
|
|
terminal->g1 = CS_UK;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
if (special == '(')
|
|
|
|
terminal->g0 = CS_US;
|
|
|
|
else
|
|
|
|
terminal->g1 = CS_US;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown character set %c\n", code);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-07 22:46:59 +03:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown special escape %c%c\n", special, code);
|
|
|
|
}
|
2011-01-07 22:46:57 +03:00
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:55 +03:00
|
|
|
static void
|
|
|
|
handle_sgr(struct terminal *terminal, int code)
|
|
|
|
{
|
|
|
|
switch(code) {
|
|
|
|
case 0:
|
|
|
|
terminal->curr_attr = terminal->color_scheme->default_attr;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
terminal->curr_attr.a |= ATTRMASK_BOLD;
|
|
|
|
if (terminal->curr_attr.fg < 8)
|
|
|
|
terminal->curr_attr.fg += 8;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
terminal->curr_attr.a |= ATTRMASK_BLINK;
|
|
|
|
break;
|
2011-01-10 02:14:01 +03:00
|
|
|
case 8:
|
|
|
|
terminal->curr_attr.a |= ATTRMASK_CONCEALED;
|
|
|
|
break;
|
2011-01-07 22:46:55 +03:00
|
|
|
case 2:
|
|
|
|
case 21:
|
|
|
|
case 22:
|
|
|
|
terminal->curr_attr.a &= ~ATTRMASK_BOLD;
|
|
|
|
if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
|
|
|
|
terminal->curr_attr.fg -= 8;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
|
|
|
|
break;
|
|
|
|
case 25:
|
|
|
|
terminal->curr_attr.a &= ~ATTRMASK_BLINK;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
case 26:
|
|
|
|
terminal->curr_attr.a |= ATTRMASK_INVERSE;
|
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
|
|
|
|
break;
|
2011-01-10 02:14:01 +03:00
|
|
|
case 28:
|
|
|
|
terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
|
|
|
|
break;
|
2011-01-07 22:46:55 +03:00
|
|
|
case 39:
|
|
|
|
terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
|
|
|
|
break;
|
|
|
|
case 49:
|
|
|
|
terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(code >= 30 && code <= 37) {
|
|
|
|
terminal->curr_attr.fg = code - 30;
|
|
|
|
if (terminal->curr_attr.a & ATTRMASK_BOLD)
|
|
|
|
terminal->curr_attr.fg += 8;
|
|
|
|
} else if(code >= 40 && code <= 47) {
|
|
|
|
terminal->curr_attr.bg = code - 40;
|
2011-01-10 02:14:01 +03:00
|
|
|
} else if (code >= 90 && code <= 97) {
|
|
|
|
terminal->curr_attr.fg = code - 90 + 8;
|
|
|
|
} else if (code >= 100 && code <= 107) {
|
|
|
|
terminal->curr_attr.bg = code - 100 + 8;
|
2011-01-07 22:46:55 +03:00
|
|
|
} else if(code >= 256 && code < 512) {
|
|
|
|
terminal->curr_attr.fg = code - 256;
|
|
|
|
} else if(code >= 512 && code < 768) {
|
|
|
|
terminal->curr_attr.bg = code - 512;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown SGR code: %d\n", code);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
/* Returns 1 if c was special, otherwise 0 */
|
|
|
|
static int
|
|
|
|
handle_special_char(struct terminal *terminal, char c)
|
|
|
|
{
|
2012-07-02 05:31:46 +04:00
|
|
|
union utf8_char *row;
|
|
|
|
struct attr *attr_row;
|
|
|
|
|
|
|
|
row = terminal_get_row(terminal, terminal->row);
|
|
|
|
attr_row = terminal_get_attr_row(terminal, terminal->row);
|
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
switch(c) {
|
|
|
|
case '\r':
|
|
|
|
terminal->column = 0;
|
|
|
|
break;
|
|
|
|
case '\n':
|
2011-01-07 22:46:59 +03:00
|
|
|
if (terminal->mode & MODE_LF_NEWLINE) {
|
|
|
|
terminal->column = 0;
|
|
|
|
}
|
2011-01-07 22:46:57 +03:00
|
|
|
/* fallthrough */
|
|
|
|
case '\v':
|
|
|
|
case '\f':
|
2011-01-07 22:46:58 +03:00
|
|
|
terminal->row++;
|
2013-10-24 03:29:14 +04:00
|
|
|
if (terminal->row + terminal->start > terminal->end)
|
|
|
|
terminal->end = terminal->row + terminal->start;
|
|
|
|
if (terminal->end == terminal->buffer_height)
|
|
|
|
terminal->log_size = terminal->buffer_height;
|
|
|
|
else if (terminal->log_size < terminal->buffer_height)
|
|
|
|
terminal->log_size = terminal->end;
|
|
|
|
|
|
|
|
if (terminal->row > terminal->margin_bottom) {
|
2011-01-07 22:46:58 +03:00
|
|
|
terminal->row = terminal->margin_bottom;
|
|
|
|
terminal_scroll(terminal, +1);
|
2011-01-07 22:46:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case '\t':
|
2011-01-07 22:46:59 +03:00
|
|
|
while (terminal->column < terminal->width) {
|
2011-01-07 22:47:00 +03:00
|
|
|
if (terminal->mode & MODE_IRM)
|
|
|
|
terminal_shift_line(terminal, +1);
|
2012-07-02 05:31:46 +04:00
|
|
|
|
|
|
|
if (row[terminal->column].byte[0] == '\0') {
|
|
|
|
row[terminal->column].byte[0] = ' ';
|
|
|
|
row[terminal->column].byte[1] = '\0';
|
|
|
|
attr_row[terminal->column] = terminal->curr_attr;
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:59 +03:00
|
|
|
terminal->column++;
|
2012-06-20 23:56:13 +04:00
|
|
|
if (terminal->tab_ruler[terminal->column]) break;
|
2011-01-07 22:46:59 +03:00
|
|
|
}
|
|
|
|
if (terminal->column >= terminal->width) {
|
|
|
|
terminal->column = terminal->width - 1;
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
break;
|
|
|
|
case '\b':
|
2011-01-07 22:46:59 +03:00
|
|
|
if (terminal->column >= terminal->width) {
|
|
|
|
terminal->column = terminal->width - 2;
|
|
|
|
} else if (terminal->column > 0) {
|
2011-01-07 22:46:57 +03:00
|
|
|
terminal->column--;
|
2011-01-07 22:46:59 +03:00
|
|
|
} else if (terminal->mode & MODE_AUTOWRAP) {
|
|
|
|
terminal->column = terminal->width - 1;
|
|
|
|
terminal->row -= 1;
|
|
|
|
if (terminal->row < terminal->margin_top) {
|
|
|
|
terminal->row = terminal->margin_top;
|
|
|
|
terminal_scroll(terminal, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
break;
|
|
|
|
case '\a':
|
|
|
|
/* Bell */
|
|
|
|
break;
|
2011-01-07 22:47:01 +03:00
|
|
|
case '\x0E': /* SO */
|
|
|
|
terminal->cs = terminal->g1;
|
|
|
|
break;
|
|
|
|
case '\x0F': /* SI */
|
|
|
|
terminal->cs = terminal->g0;
|
|
|
|
break;
|
2012-08-03 17:37:05 +04:00
|
|
|
case '\0':
|
|
|
|
break;
|
2011-01-07 22:46:57 +03:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_char(struct terminal *terminal, union utf8_char utf8)
|
|
|
|
{
|
|
|
|
union utf8_char *row;
|
|
|
|
struct attr *attr_row;
|
|
|
|
|
|
|
|
if (handle_special_char(terminal, utf8.byte[0])) return;
|
2011-01-07 22:47:01 +03:00
|
|
|
|
|
|
|
apply_char_set(terminal->cs, &utf8);
|
2011-01-07 22:46:57 +03:00
|
|
|
|
|
|
|
/* There are a whole lot of non-characters, control codes,
|
|
|
|
* and formatting codes that should probably be ignored,
|
|
|
|
* for example: */
|
|
|
|
if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
|
|
|
|
/* BOM, ignore */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some of these non-characters should be translated, e.g.: */
|
|
|
|
if (utf8.byte[0] < 32) {
|
|
|
|
utf8.byte[0] = utf8.byte[0] + 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle right margin effects */
|
|
|
|
if (terminal->column >= terminal->width) {
|
2011-01-07 22:46:59 +03:00
|
|
|
if (terminal->mode & MODE_AUTOWRAP) {
|
|
|
|
terminal->column = 0;
|
|
|
|
terminal->row += 1;
|
|
|
|
if (terminal->row > terminal->margin_bottom) {
|
|
|
|
terminal->row = terminal->margin_bottom;
|
|
|
|
terminal_scroll(terminal, +1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
terminal->column--;
|
|
|
|
}
|
|
|
|
}
|
2011-01-07 22:46:57 +03:00
|
|
|
|
|
|
|
row = terminal_get_row(terminal, terminal->row);
|
|
|
|
attr_row = terminal_get_attr_row(terminal, terminal->row);
|
|
|
|
|
2011-01-07 22:47:00 +03:00
|
|
|
if (terminal->mode & MODE_IRM)
|
|
|
|
terminal_shift_line(terminal, +1);
|
2011-01-07 22:46:57 +03:00
|
|
|
row[terminal->column] = utf8;
|
|
|
|
attr_row[terminal->column++] = terminal->curr_attr;
|
|
|
|
|
2013-08-19 06:59:21 +04:00
|
|
|
/* cursor jump for wide character. */
|
|
|
|
if (is_wide(utf8))
|
|
|
|
row[terminal->column++].ch = 0x200B; /* space glyph */
|
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
if (utf8.ch != terminal->last_char.ch)
|
|
|
|
terminal->last_char = utf8;
|
|
|
|
}
|
|
|
|
|
2011-01-12 09:23:41 +03:00
|
|
|
static void
|
|
|
|
escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
|
|
|
|
{
|
|
|
|
int len, i;
|
|
|
|
|
|
|
|
if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
|
|
|
|
else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
|
|
|
|
else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
|
|
|
|
else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
|
|
|
|
else len = 1; /* Invalid, cannot happen */
|
|
|
|
|
|
|
|
if (terminal->escape_length + len <= MAX_ESCAPE) {
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
terminal->escape[terminal->escape_length + i] = utf8.byte[i];
|
|
|
|
terminal->escape_length += len;
|
|
|
|
} else if (terminal->escape_length < MAX_ESCAPE) {
|
|
|
|
terminal->escape[terminal->escape_length++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-08 07:17:31 +03:00
|
|
|
static void
|
|
|
|
terminal_data(struct terminal *terminal, const char *data, size_t length)
|
|
|
|
{
|
2012-03-30 19:52:39 +04:00
|
|
|
unsigned int i;
|
2011-01-07 22:46:54 +03:00
|
|
|
union utf8_char utf8;
|
|
|
|
enum utf8_state parser_state;
|
2008-12-08 07:17:31 +03:00
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
2011-01-07 22:46:54 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
/* assume escape codes never use non-ASCII characters */
|
2011-01-12 09:23:41 +03:00
|
|
|
switch (terminal->state) {
|
|
|
|
case escape_state_escape:
|
|
|
|
escape_append_utf8(terminal, utf8);
|
|
|
|
switch (utf8.byte[0]) {
|
|
|
|
case 'P': /* DCS */
|
|
|
|
terminal->state = escape_state_dcs;
|
|
|
|
break;
|
|
|
|
case '[': /* CSI */
|
|
|
|
terminal->state = escape_state_csi;
|
|
|
|
break;
|
|
|
|
case ']': /* OSC */
|
|
|
|
terminal->state = escape_state_osc;
|
|
|
|
break;
|
|
|
|
case '#':
|
|
|
|
case '(':
|
|
|
|
case ')': /* special */
|
|
|
|
terminal->state = escape_state_special;
|
|
|
|
break;
|
|
|
|
case '^': /* PM (not implemented) */
|
|
|
|
case '_': /* APC (not implemented) */
|
|
|
|
terminal->state = escape_state_ignore;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
terminal->state = escape_state_normal;
|
2011-01-07 22:46:57 +03:00
|
|
|
handle_non_csi_escape(terminal, utf8.byte[0]);
|
2011-01-12 09:23:41 +03:00
|
|
|
break;
|
2011-01-07 22:46:57 +03:00
|
|
|
}
|
2011-01-12 09:23:41 +03:00
|
|
|
continue;
|
|
|
|
case escape_state_csi:
|
2011-01-07 22:46:57 +03:00
|
|
|
if (handle_special_char(terminal, utf8.byte[0]) != 0) {
|
|
|
|
/* do nothing */
|
|
|
|
} else if (utf8.byte[0] == '?') {
|
2011-01-12 09:23:41 +03:00
|
|
|
terminal->escape_flags |= ESC_FLAG_WHAT;
|
|
|
|
} else if (utf8.byte[0] == '>') {
|
|
|
|
terminal->escape_flags |= ESC_FLAG_GT;
|
|
|
|
} else if (utf8.byte[0] == '!') {
|
|
|
|
terminal->escape_flags |= ESC_FLAG_BANG;
|
|
|
|
} else if (utf8.byte[0] == '$') {
|
|
|
|
terminal->escape_flags |= ESC_FLAG_CASH;
|
|
|
|
} else if (utf8.byte[0] == '\'') {
|
|
|
|
terminal->escape_flags |= ESC_FLAG_SQUOTE;
|
|
|
|
} else if (utf8.byte[0] == '"') {
|
|
|
|
terminal->escape_flags |= ESC_FLAG_DQUOTE;
|
|
|
|
} else if (utf8.byte[0] == ' ') {
|
|
|
|
terminal->escape_flags |= ESC_FLAG_SPACE;
|
2011-01-07 22:46:57 +03:00
|
|
|
} else {
|
2011-01-12 09:23:41 +03:00
|
|
|
escape_append_utf8(terminal, utf8);
|
2011-01-07 22:46:57 +03:00
|
|
|
if (terminal->escape_length >= MAX_ESCAPE)
|
2011-01-12 09:23:41 +03:00
|
|
|
terminal->state = escape_state_normal;
|
2011-01-07 22:46:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
|
|
|
|
utf8.byte[0] == '`')
|
|
|
|
{
|
2011-01-12 09:23:41 +03:00
|
|
|
terminal->state = escape_state_normal;
|
2008-12-08 20:20:40 +03:00
|
|
|
handle_escape(terminal);
|
|
|
|
} else {
|
2008-12-08 08:16:39 +03:00
|
|
|
}
|
2011-01-12 09:23:41 +03:00
|
|
|
continue;
|
|
|
|
case escape_state_inner_escape:
|
|
|
|
if (utf8.byte[0] == '\\') {
|
|
|
|
terminal->state = escape_state_normal;
|
|
|
|
if (terminal->outer_state == escape_state_dcs) {
|
|
|
|
handle_dcs(terminal);
|
|
|
|
} else if (terminal->outer_state == escape_state_osc) {
|
|
|
|
handle_osc(terminal);
|
|
|
|
}
|
|
|
|
} else if (utf8.byte[0] == '\e') {
|
|
|
|
terminal->state = terminal->outer_state;
|
|
|
|
escape_append_utf8(terminal, utf8);
|
|
|
|
if (terminal->escape_length >= MAX_ESCAPE)
|
|
|
|
terminal->state = escape_state_normal;
|
|
|
|
} else {
|
|
|
|
terminal->state = terminal->outer_state;
|
|
|
|
if (terminal->escape_length < MAX_ESCAPE)
|
|
|
|
terminal->escape[terminal->escape_length++] = '\e';
|
|
|
|
escape_append_utf8(terminal, utf8);
|
|
|
|
if (terminal->escape_length >= MAX_ESCAPE)
|
|
|
|
terminal->state = escape_state_normal;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case escape_state_dcs:
|
|
|
|
case escape_state_osc:
|
|
|
|
case escape_state_ignore:
|
|
|
|
if (utf8.byte[0] == '\e') {
|
|
|
|
terminal->outer_state = terminal->state;
|
|
|
|
terminal->state = escape_state_inner_escape;
|
|
|
|
} else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
|
|
|
|
terminal->state = escape_state_normal;
|
|
|
|
handle_osc(terminal);
|
|
|
|
} else {
|
|
|
|
escape_append_utf8(terminal, utf8);
|
|
|
|
if (terminal->escape_length >= MAX_ESCAPE)
|
|
|
|
terminal->state = escape_state_normal;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case escape_state_special:
|
|
|
|
escape_append_utf8(terminal, utf8);
|
|
|
|
terminal->state = escape_state_normal;
|
|
|
|
if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
|
|
|
|
handle_special_escape(terminal, terminal->escape[1],
|
|
|
|
utf8.byte[0]);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
2011-01-07 22:46:57 +03:00
|
|
|
}
|
2008-12-08 20:20:40 +03:00
|
|
|
|
2011-01-07 22:46:57 +03:00
|
|
|
/* this is valid, because ASCII characters are never used to
|
|
|
|
* introduce a multibyte sequence in UTF-8 */
|
|
|
|
if (utf8.byte[0] == '\e') {
|
2011-01-12 09:23:41 +03:00
|
|
|
terminal->state = escape_state_escape;
|
|
|
|
terminal->outer_state = escape_state_normal;
|
2008-12-08 20:20:40 +03:00
|
|
|
terminal->escape[0] = '\e';
|
|
|
|
terminal->escape_length = 1;
|
2011-01-12 09:23:41 +03:00
|
|
|
terminal->escape_flags = 0;
|
2011-01-07 22:46:57 +03:00
|
|
|
} else {
|
|
|
|
handle_char(terminal, utf8);
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
2008-12-08 19:13:26 +03:00
|
|
|
|
2010-06-15 07:52:50 +04:00
|
|
|
window_schedule_redraw(terminal->window);
|
2008-12-08 08:01:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-28 20:26:06 +04:00
|
|
|
data_source_target(void *data,
|
|
|
|
struct wl_data_source *source, const char *mime_type)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "data_source_target, %s\n", mime_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
data_source_send(void *data,
|
|
|
|
struct wl_data_source *source,
|
|
|
|
const char *mime_type, int32_t fd)
|
2011-01-19 22:27:42 +03:00
|
|
|
{
|
2011-01-21 23:18:55 +03:00
|
|
|
struct terminal *terminal = data;
|
2011-01-19 22:27:42 +03:00
|
|
|
|
2011-01-21 23:18:55 +03:00
|
|
|
terminal_send_selection(terminal, fd);
|
2011-01-19 22:27:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-28 20:26:06 +04:00
|
|
|
data_source_cancelled(void *data, struct wl_data_source *source)
|
2011-01-19 22:27:42 +03:00
|
|
|
{
|
2011-10-28 20:26:06 +04:00
|
|
|
wl_data_source_destroy(source);
|
2011-01-19 22:27:42 +03:00
|
|
|
}
|
|
|
|
|
2011-10-28 20:26:06 +04:00
|
|
|
static const struct wl_data_source_listener data_source_listener = {
|
|
|
|
data_source_target,
|
|
|
|
data_source_send,
|
|
|
|
data_source_cancelled
|
2011-01-19 22:27:42 +03:00
|
|
|
};
|
|
|
|
|
2013-09-05 07:41:06 +04:00
|
|
|
static const char text_mime_type[] = "text/plain;charset=utf-8";
|
|
|
|
|
|
|
|
static void
|
|
|
|
data_handler(struct window *window,
|
|
|
|
struct input *input,
|
|
|
|
float x, float y, const char **types, void *data)
|
|
|
|
{
|
|
|
|
int i, has_text = 0;
|
|
|
|
|
|
|
|
if (!types)
|
|
|
|
return;
|
|
|
|
for (i = 0; types[i]; i++)
|
|
|
|
if (strcmp(types[i], text_mime_type) == 0)
|
|
|
|
has_text = 1;
|
|
|
|
|
|
|
|
if (!has_text) {
|
|
|
|
input_accept(input, NULL);
|
|
|
|
} else {
|
|
|
|
input_accept(input, text_mime_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drop_handler(struct window *window, struct input *input,
|
|
|
|
int32_t x, int32_t y, void *data)
|
|
|
|
{
|
|
|
|
struct terminal *terminal = data;
|
|
|
|
|
|
|
|
input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
|
|
|
|
}
|
|
|
|
|
2012-07-24 05:56:31 +04:00
|
|
|
static void
|
|
|
|
fullscreen_handler(struct window *window, void *data)
|
|
|
|
{
|
|
|
|
struct terminal *terminal = data;
|
|
|
|
|
2012-10-10 19:37:46 +04:00
|
|
|
window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
|
2012-07-24 05:56:31 +04:00
|
|
|
}
|
|
|
|
|
2013-01-28 13:11:06 +04:00
|
|
|
static void
|
|
|
|
close_handler(struct window *window, void *data)
|
|
|
|
{
|
|
|
|
struct terminal *terminal = data;
|
|
|
|
|
|
|
|
terminal_destroy(terminal);
|
|
|
|
}
|
|
|
|
|
2013-10-24 03:52:05 +04:00
|
|
|
static void
|
|
|
|
terminal_copy(struct terminal *terminal, struct input *input)
|
|
|
|
{
|
|
|
|
terminal->selection =
|
|
|
|
display_create_data_source(terminal->display);
|
|
|
|
wl_data_source_offer(terminal->selection,
|
|
|
|
"text/plain;charset=utf-8");
|
|
|
|
wl_data_source_add_listener(terminal->selection,
|
|
|
|
&data_source_listener, terminal);
|
|
|
|
input_set_selection(input, terminal->selection,
|
|
|
|
display_get_serial(terminal->display));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
terminal_paste(struct terminal *terminal, struct input *input)
|
|
|
|
{
|
|
|
|
input_receive_selection_data_to_fd(input,
|
|
|
|
"text/plain;charset=utf-8",
|
|
|
|
terminal->master);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
terminal_new_instance(struct terminal *terminal)
|
|
|
|
{
|
|
|
|
struct terminal *new_terminal;
|
|
|
|
|
|
|
|
new_terminal = terminal_create(terminal->display);
|
|
|
|
if (terminal_run(new_terminal, option_shell))
|
|
|
|
terminal_destroy(new_terminal);
|
|
|
|
}
|
|
|
|
|
2011-01-19 22:27:42 +03:00
|
|
|
static int
|
|
|
|
handle_bound_key(struct terminal *terminal,
|
|
|
|
struct input *input, uint32_t sym, uint32_t time)
|
|
|
|
{
|
|
|
|
switch (sym) {
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_X:
|
2011-10-28 20:26:06 +04:00
|
|
|
/* Cut selection; terminal doesn't do cut, fall
|
|
|
|
* through to copy. */
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_C:
|
2013-10-24 03:52:05 +04:00
|
|
|
terminal_copy(terminal, input);
|
2011-01-19 22:27:42 +03:00
|
|
|
return 1;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_V:
|
2013-10-24 03:52:05 +04:00
|
|
|
terminal_paste(terminal, input);
|
2011-01-19 22:27:42 +03:00
|
|
|
return 1;
|
2012-06-21 06:44:03 +04:00
|
|
|
case XKB_KEY_N:
|
2013-10-24 03:52:05 +04:00
|
|
|
terminal_new_instance(terminal);
|
2012-06-21 06:44:03 +04:00
|
|
|
return 1;
|
|
|
|
|
2013-10-24 03:29:14 +04:00
|
|
|
case XKB_KEY_Up:
|
|
|
|
if (!terminal->scrolling)
|
|
|
|
terminal->saved_start = terminal->start;
|
|
|
|
if (terminal->start == terminal->end - terminal->log_size)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
terminal->scrolling = 1;
|
|
|
|
terminal->start--;
|
|
|
|
terminal->row++;
|
|
|
|
terminal->selection_start_row++;
|
|
|
|
terminal->selection_end_row++;
|
|
|
|
widget_schedule_redraw(terminal->widget);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case XKB_KEY_Down:
|
|
|
|
if (!terminal->scrolling)
|
|
|
|
terminal->saved_start = terminal->start;
|
|
|
|
|
|
|
|
if (terminal->start == terminal->saved_start)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
terminal->scrolling = 1;
|
|
|
|
terminal->start++;
|
|
|
|
terminal->row--;
|
|
|
|
terminal->selection_start_row--;
|
|
|
|
terminal->selection_end_row--;
|
|
|
|
widget_schedule_redraw(terminal->widget);
|
|
|
|
return 1;
|
|
|
|
|
2011-01-19 22:27:42 +03:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-19 22:20:33 +03:00
|
|
|
static void
|
|
|
|
key_handler(struct window *window, struct input *input, uint32_t time,
|
2012-05-30 19:31:52 +04:00
|
|
|
uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
|
|
|
|
void *data)
|
2008-12-08 08:01:36 +03:00
|
|
|
{
|
|
|
|
struct terminal *terminal = data;
|
2011-01-07 22:46:59 +03:00
|
|
|
char ch[MAX_RESPONSE];
|
2012-06-21 23:55:03 +04:00
|
|
|
uint32_t modifiers, serial;
|
2013-10-24 03:29:14 +04:00
|
|
|
int ret, len = 0, d;
|
2012-08-18 15:38:38 +04:00
|
|
|
bool convert_utf8 = true;
|
2008-12-08 08:01:36 +03:00
|
|
|
|
2011-01-19 22:20:33 +03:00
|
|
|
modifiers = input_get_modifiers(input);
|
2012-05-08 23:55:39 +04:00
|
|
|
if ((modifiers & MOD_CONTROL_MASK) &&
|
|
|
|
(modifiers & MOD_SHIFT_MASK) &&
|
2012-05-30 19:31:52 +04:00
|
|
|
state == WL_KEYBOARD_KEY_STATE_PRESSED &&
|
|
|
|
handle_bound_key(terminal, input, sym, time))
|
2011-01-19 22:20:33 +03:00
|
|
|
return;
|
|
|
|
|
2012-11-07 10:51:35 +04:00
|
|
|
/* Map keypad symbols to 'normal' equivalents before processing */
|
|
|
|
switch (sym) {
|
|
|
|
case XKB_KEY_KP_Space:
|
|
|
|
sym = XKB_KEY_space;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Tab:
|
|
|
|
sym = XKB_KEY_Tab;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Enter:
|
|
|
|
sym = XKB_KEY_Return;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Left:
|
|
|
|
sym = XKB_KEY_Left;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Up:
|
|
|
|
sym = XKB_KEY_Up;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Right:
|
|
|
|
sym = XKB_KEY_Right;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Down:
|
|
|
|
sym = XKB_KEY_Down;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Equal:
|
|
|
|
sym = XKB_KEY_equal;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Multiply:
|
|
|
|
sym = XKB_KEY_asterisk;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Add:
|
|
|
|
sym = XKB_KEY_plus;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Separator:
|
|
|
|
/* Note this is actually locale-dependent and should mostly be
|
|
|
|
* a comma. But leave it as period until we one day start
|
|
|
|
* doing the right thing. */
|
|
|
|
sym = XKB_KEY_period;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Subtract:
|
|
|
|
sym = XKB_KEY_minus;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Decimal:
|
|
|
|
sym = XKB_KEY_period;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Divide:
|
|
|
|
sym = XKB_KEY_slash;
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_0:
|
|
|
|
case XKB_KEY_KP_1:
|
|
|
|
case XKB_KEY_KP_2:
|
|
|
|
case XKB_KEY_KP_3:
|
|
|
|
case XKB_KEY_KP_4:
|
|
|
|
case XKB_KEY_KP_5:
|
|
|
|
case XKB_KEY_KP_6:
|
|
|
|
case XKB_KEY_KP_7:
|
|
|
|
case XKB_KEY_KP_8:
|
|
|
|
case XKB_KEY_KP_9:
|
|
|
|
sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-06-26 00:50:05 +04:00
|
|
|
switch (sym) {
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_BackSpace:
|
2012-06-21 20:29:36 +04:00
|
|
|
if (modifiers & MOD_ALT_MASK)
|
|
|
|
ch[len++] = 0x1b;
|
2012-06-21 01:57:56 +04:00
|
|
|
ch[len++] = 0x7f;
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Tab:
|
|
|
|
case XKB_KEY_Linefeed:
|
|
|
|
case XKB_KEY_Clear:
|
|
|
|
case XKB_KEY_Pause:
|
|
|
|
case XKB_KEY_Scroll_Lock:
|
|
|
|
case XKB_KEY_Sys_Req:
|
|
|
|
case XKB_KEY_Escape:
|
2010-06-26 00:50:05 +04:00
|
|
|
ch[len++] = sym & 0x7f;
|
|
|
|
break;
|
|
|
|
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Return:
|
2011-01-07 22:46:59 +03:00
|
|
|
if (terminal->mode & MODE_LF_NEWLINE) {
|
|
|
|
ch[len++] = 0x0D;
|
|
|
|
ch[len++] = 0x0A;
|
|
|
|
} else {
|
|
|
|
ch[len++] = 0x0D;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Shift_L:
|
|
|
|
case XKB_KEY_Shift_R:
|
|
|
|
case XKB_KEY_Control_L:
|
|
|
|
case XKB_KEY_Control_R:
|
|
|
|
case XKB_KEY_Alt_L:
|
|
|
|
case XKB_KEY_Alt_R:
|
2012-06-22 20:18:56 +04:00
|
|
|
case XKB_KEY_Meta_L:
|
|
|
|
case XKB_KEY_Meta_R:
|
|
|
|
case XKB_KEY_Super_L:
|
|
|
|
case XKB_KEY_Super_R:
|
|
|
|
case XKB_KEY_Hyper_L:
|
|
|
|
case XKB_KEY_Hyper_R:
|
2010-06-26 00:50:05 +04:00
|
|
|
break;
|
|
|
|
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Insert:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 2, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Delete:
|
2011-01-07 22:47:02 +03:00
|
|
|
if (terminal->mode & MODE_DELETE_SENDS_DEL) {
|
|
|
|
ch[len++] = '\x04';
|
|
|
|
} else {
|
|
|
|
len = function_key_response('[', 3, modifiers, '~', ch);
|
|
|
|
}
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Page_Up:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 5, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Page_Down:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 6, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F1:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('O', 1, modifiers, 'P', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F2:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('O', 1, modifiers, 'Q', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F3:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('O', 1, modifiers, 'R', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F4:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('O', 1, modifiers, 'S', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F5:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 15, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F6:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 17, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F7:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 18, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F8:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 19, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F9:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 20, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F10:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 21, modifiers, '~', ch);
|
|
|
|
break;
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_F12:
|
2011-01-07 22:47:02 +03:00
|
|
|
len = function_key_response('[', 24, modifiers, '~', ch);
|
|
|
|
break;
|
2008-12-08 08:01:36 +03:00
|
|
|
default:
|
2011-01-07 22:47:02 +03:00
|
|
|
/* Handle special keys with alternate mappings */
|
|
|
|
len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
|
|
|
|
if (len != 0) break;
|
|
|
|
|
2012-05-08 23:55:39 +04:00
|
|
|
if (modifiers & MOD_CONTROL_MASK) {
|
2011-01-07 22:47:02 +03:00
|
|
|
if (sym >= '3' && sym <= '7')
|
|
|
|
sym = (sym & 0x1f) + 8;
|
|
|
|
|
|
|
|
if (!((sym >= '!' && sym <= '/') ||
|
|
|
|
(sym >= '8' && sym <= '?') ||
|
|
|
|
(sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
|
|
|
|
else if (sym == '2') sym = 0x00;
|
|
|
|
else if (sym == '/') sym = 0x1F;
|
|
|
|
else if (sym == '8' || sym == '?') sym = 0x7F;
|
2012-06-21 20:30:15 +04:00
|
|
|
}
|
2012-08-18 15:38:38 +04:00
|
|
|
if (modifiers & MOD_ALT_MASK) {
|
|
|
|
if (terminal->mode & MODE_ALT_SENDS_ESC) {
|
|
|
|
ch[len++] = 0x1b;
|
|
|
|
} else {
|
|
|
|
sym = sym | 0x80;
|
|
|
|
convert_utf8 = false;
|
|
|
|
}
|
2011-01-07 22:47:02 +03:00
|
|
|
}
|
|
|
|
|
2012-08-18 15:38:38 +04:00
|
|
|
if ((sym < 128) ||
|
|
|
|
(!convert_utf8 && sym < 256)) {
|
2010-06-26 00:50:05 +04:00
|
|
|
ch[len++] = sym;
|
2012-08-18 15:38:38 +04:00
|
|
|
} else {
|
|
|
|
ret = xkb_keysym_to_utf8(sym, ch + len,
|
|
|
|
MAX_RESPONSE - len);
|
|
|
|
if (ret < 0)
|
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: buffer too small to encode "
|
|
|
|
"UTF8 character\n");
|
|
|
|
else
|
|
|
|
len += ret;
|
|
|
|
}
|
|
|
|
|
2008-12-08 08:01:36 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-06-26 00:50:05 +04:00
|
|
|
|
2012-06-22 20:17:17 +04:00
|
|
|
if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
|
2013-10-24 03:29:14 +04:00
|
|
|
if (terminal->scrolling) {
|
|
|
|
d = terminal->saved_start - terminal->start;
|
|
|
|
terminal->row -= d;
|
|
|
|
terminal->selection_start_row -= d;
|
|
|
|
terminal->selection_end_row -= d;
|
|
|
|
terminal->start = terminal->saved_start;
|
|
|
|
terminal->scrolling = 0;
|
|
|
|
widget_schedule_redraw(terminal->widget);
|
|
|
|
}
|
|
|
|
|
2011-08-24 19:30:21 +04:00
|
|
|
terminal_write(terminal, ch, len);
|
2012-06-22 20:17:17 +04:00
|
|
|
|
|
|
|
/* Hide cursor, except if this was coming from a
|
|
|
|
* repeating key press. */
|
|
|
|
serial = display_get_serial(terminal->display);
|
|
|
|
if (terminal->hide_cursor_serial != serial) {
|
|
|
|
input_set_pointer_image(input, CURSOR_BLANK);
|
|
|
|
terminal->hide_cursor_serial = serial;
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 08:01:36 +03:00
|
|
|
}
|
|
|
|
|
2009-02-23 07:01:35 +03:00
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
2010-07-29 07:50:12 +04:00
|
|
|
struct input *device, void *data)
|
2009-02-23 07:01:35 +03:00
|
|
|
{
|
|
|
|
struct terminal *terminal = data;
|
|
|
|
|
2010-06-15 07:52:50 +04:00
|
|
|
window_schedule_redraw(terminal->window);
|
2009-02-23 07:01:35 +03:00
|
|
|
}
|
|
|
|
|
2012-06-28 01:43:10 +04:00
|
|
|
static int wordsep(int ch)
|
|
|
|
{
|
|
|
|
const char extra[] = "-,./?%&#:_=+@~";
|
2012-08-02 22:59:43 +04:00
|
|
|
|
|
|
|
if (ch > 127)
|
|
|
|
return 1;
|
2012-06-28 01:43:10 +04:00
|
|
|
|
|
|
|
return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
recompute_selection(struct terminal *terminal)
|
|
|
|
{
|
|
|
|
struct rectangle allocation;
|
|
|
|
int col, x, width, height;
|
|
|
|
int start_row, end_row;
|
2012-07-02 05:31:46 +04:00
|
|
|
int word_start, eol;
|
2012-06-28 01:43:10 +04:00
|
|
|
int side_margin, top_margin;
|
|
|
|
int start_x, end_x;
|
|
|
|
int cw, ch;
|
|
|
|
union utf8_char *data;
|
|
|
|
|
2013-06-06 11:32:41 +04:00
|
|
|
cw = terminal->average_width;
|
2012-06-28 01:43:10 +04:00
|
|
|
ch = terminal->extents.height;
|
|
|
|
widget_get_allocation(terminal->widget, &allocation);
|
|
|
|
width = terminal->width * cw;
|
|
|
|
height = terminal->height * ch;
|
|
|
|
side_margin = allocation.x + (allocation.width - width) / 2;
|
|
|
|
top_margin = allocation.y + (allocation.height - height) / 2;
|
|
|
|
|
2012-06-29 19:35:24 +04:00
|
|
|
start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
|
|
|
|
end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
|
|
|
|
|
2012-06-28 01:43:10 +04:00
|
|
|
if (start_row < end_row ||
|
|
|
|
(start_row == end_row &&
|
|
|
|
terminal->selection_start_x < terminal->selection_end_x)) {
|
|
|
|
terminal->selection_start_row = start_row;
|
|
|
|
terminal->selection_end_row = end_row;
|
|
|
|
start_x = terminal->selection_start_x;
|
|
|
|
end_x = terminal->selection_end_x;
|
|
|
|
} else {
|
|
|
|
terminal->selection_start_row = end_row;
|
|
|
|
terminal->selection_end_row = start_row;
|
|
|
|
start_x = terminal->selection_end_x;
|
|
|
|
end_x = terminal->selection_start_x;
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:31:46 +04:00
|
|
|
eol = 0;
|
2012-06-29 19:35:24 +04:00
|
|
|
if (terminal->selection_start_row < 0) {
|
|
|
|
terminal->selection_start_row = 0;
|
2012-06-28 01:43:10 +04:00
|
|
|
terminal->selection_start_col = 0;
|
2012-06-29 19:35:24 +04:00
|
|
|
} else {
|
|
|
|
x = side_margin + cw / 2;
|
|
|
|
data = terminal_get_row(terminal,
|
|
|
|
terminal->selection_start_row);
|
|
|
|
word_start = 0;
|
|
|
|
for (col = 0; col < terminal->width; col++, x += cw) {
|
|
|
|
if (col == 0 || wordsep(data[col - 1].ch))
|
|
|
|
word_start = col;
|
2012-07-02 05:31:46 +04:00
|
|
|
if (data[col].ch != 0)
|
|
|
|
eol = col + 1;
|
2012-06-29 19:35:24 +04:00
|
|
|
if (start_x < x)
|
|
|
|
break;
|
|
|
|
}
|
2012-06-28 01:43:10 +04:00
|
|
|
|
2012-06-29 19:35:24 +04:00
|
|
|
switch (terminal->dragging) {
|
|
|
|
case SELECT_LINE:
|
|
|
|
terminal->selection_start_col = 0;
|
|
|
|
break;
|
|
|
|
case SELECT_WORD:
|
|
|
|
terminal->selection_start_col = word_start;
|
2012-06-28 01:43:10 +04:00
|
|
|
break;
|
2012-06-29 19:35:24 +04:00
|
|
|
case SELECT_CHAR:
|
|
|
|
terminal->selection_start_col = col;
|
2012-06-28 01:43:10 +04:00
|
|
|
break;
|
2012-06-29 19:35:24 +04:00
|
|
|
}
|
2012-06-28 01:43:10 +04:00
|
|
|
}
|
|
|
|
|
2012-06-29 19:35:24 +04:00
|
|
|
if (terminal->selection_end_row >= terminal->height) {
|
|
|
|
terminal->selection_end_row = terminal->height;
|
|
|
|
terminal->selection_end_col = 0;
|
|
|
|
} else {
|
|
|
|
x = side_margin + cw / 2;
|
|
|
|
data = terminal_get_row(terminal, terminal->selection_end_row);
|
|
|
|
for (col = 0; col < terminal->width; col++, x += cw) {
|
|
|
|
if (terminal->dragging == SELECT_CHAR && end_x < x)
|
|
|
|
break;
|
|
|
|
if (terminal->dragging == SELECT_WORD &&
|
|
|
|
end_x < x && wordsep(data[col].ch))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
terminal->selection_end_col = col;
|
|
|
|
}
|
2012-06-28 01:43:10 +04:00
|
|
|
|
2012-07-02 05:31:46 +04:00
|
|
|
if (terminal->selection_end_col != terminal->selection_start_col ||
|
|
|
|
terminal->selection_start_row != terminal->selection_end_row) {
|
|
|
|
col = terminal->selection_end_col;
|
|
|
|
if (col > 0 && data[col - 1].ch == 0)
|
|
|
|
terminal->selection_end_col = terminal->width;
|
|
|
|
data = terminal_get_row(terminal, terminal->selection_start_row);
|
|
|
|
if (data[terminal->selection_start_col].ch == 0)
|
|
|
|
terminal->selection_start_col = eol;
|
|
|
|
}
|
|
|
|
|
2012-06-28 01:43:10 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-24 03:52:05 +04:00
|
|
|
static void
|
|
|
|
menu_func(struct window *window, struct input *input, int index, void *data)
|
|
|
|
{
|
|
|
|
struct terminal *terminal = data;
|
|
|
|
|
|
|
|
fprintf(stderr, "picked entry %d\n", index);
|
|
|
|
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
terminal_new_instance(terminal);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
terminal_copy(terminal, input);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
terminal_paste(terminal, input);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_menu(struct terminal *terminal, struct input *input, uint32_t time)
|
|
|
|
{
|
|
|
|
int32_t x, y;
|
|
|
|
static const char *entries[] = {
|
|
|
|
"Open Terminal", "Copy", "Paste"
|
|
|
|
};
|
|
|
|
|
|
|
|
input_get_position(input, &x, &y);
|
|
|
|
window_show_menu(terminal->display, input, time, terminal->window,
|
|
|
|
x - 10, y - 10, menu_func,
|
|
|
|
entries, ARRAY_LENGTH(entries));
|
|
|
|
}
|
|
|
|
|
2011-01-20 19:56:57 +03:00
|
|
|
static void
|
2012-01-09 20:12:05 +04:00
|
|
|
button_handler(struct widget *widget,
|
2011-01-20 19:56:57 +03:00
|
|
|
struct input *input, uint32_t time,
|
2012-05-30 19:31:51 +04:00
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data)
|
2011-01-20 19:56:57 +03:00
|
|
|
{
|
2012-01-10 18:43:58 +04:00
|
|
|
struct terminal *terminal = data;
|
2011-01-20 19:56:57 +03:00
|
|
|
|
|
|
|
switch (button) {
|
2013-10-24 03:52:05 +04:00
|
|
|
case BTN_LEFT:
|
2012-05-30 19:31:51 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
2012-06-28 01:43:10 +04:00
|
|
|
|
|
|
|
if (time - terminal->button_time < 500)
|
|
|
|
terminal->click_count++;
|
|
|
|
else
|
|
|
|
terminal->click_count = 1;
|
|
|
|
|
|
|
|
terminal->button_time = time;
|
|
|
|
terminal->dragging =
|
|
|
|
(terminal->click_count - 1) % 3 + SELECT_CHAR;
|
|
|
|
|
2011-01-20 19:56:57 +03:00
|
|
|
input_get_position(input,
|
|
|
|
&terminal->selection_start_x,
|
|
|
|
&terminal->selection_start_y);
|
|
|
|
terminal->selection_end_x = terminal->selection_start_x;
|
|
|
|
terminal->selection_end_y = terminal->selection_start_y;
|
2012-06-28 01:43:10 +04:00
|
|
|
if (recompute_selection(terminal))
|
|
|
|
widget_schedule_redraw(widget);
|
2011-01-20 19:56:57 +03:00
|
|
|
} else {
|
2012-06-28 01:43:10 +04:00
|
|
|
terminal->dragging = SELECT_NONE;
|
2011-01-20 19:56:57 +03:00
|
|
|
}
|
|
|
|
break;
|
2013-10-24 03:52:05 +04:00
|
|
|
|
|
|
|
case BTN_RIGHT:
|
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
show_menu(terminal, input, time);
|
|
|
|
break;
|
2011-01-20 19:56:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 22:27:02 +04:00
|
|
|
static int
|
|
|
|
enter_handler(struct widget *widget,
|
|
|
|
struct input *input, float x, float y, void *data)
|
|
|
|
{
|
2013-10-24 07:47:35 +04:00
|
|
|
struct terminal *terminal = data;
|
|
|
|
|
|
|
|
/* Reset title to get rid of resizing '[WxH]' in titlebar */
|
|
|
|
window_set_title(terminal->window, terminal->title);
|
|
|
|
|
2012-06-28 22:27:02 +04:00
|
|
|
return CURSOR_IBEAM;
|
|
|
|
}
|
|
|
|
|
2011-01-20 19:56:57 +03:00
|
|
|
static int
|
2012-01-09 18:44:45 +04:00
|
|
|
motion_handler(struct widget *widget,
|
2011-01-20 19:56:57 +03:00
|
|
|
struct input *input, uint32_t time,
|
2012-05-10 20:21:37 +04:00
|
|
|
float x, float y, void *data)
|
2011-01-20 19:56:57 +03:00
|
|
|
{
|
2012-01-10 18:43:58 +04:00
|
|
|
struct terminal *terminal = data;
|
2011-01-20 19:56:57 +03:00
|
|
|
|
|
|
|
if (terminal->dragging) {
|
|
|
|
input_get_position(input,
|
|
|
|
&terminal->selection_end_x,
|
|
|
|
&terminal->selection_end_y);
|
2012-06-28 01:43:10 +04:00
|
|
|
|
|
|
|
if (recompute_selection(terminal))
|
|
|
|
widget_schedule_redraw(widget);
|
2011-01-20 19:56:57 +03:00
|
|
|
}
|
|
|
|
|
2012-05-25 17:01:41 +04:00
|
|
|
return CURSOR_IBEAM;
|
2011-01-20 19:56:57 +03:00
|
|
|
}
|
|
|
|
|
2013-05-22 16:41:34 +04:00
|
|
|
static void
|
|
|
|
output_handler(struct window *window, struct output *output, int enter,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
if (enter)
|
|
|
|
window_set_buffer_transform(window, output_get_transform(output));
|
|
|
|
window_set_buffer_scale(window, window_get_output_scale(window));
|
|
|
|
window_schedule_redraw(window);
|
|
|
|
}
|
|
|
|
|
2013-06-06 11:32:41 +04:00
|
|
|
#ifndef howmany
|
|
|
|
#define howmany(x, y) (((x) + ((y) - 1)) / (y))
|
|
|
|
#endif
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
static struct terminal *
|
2012-10-10 19:37:46 +04:00
|
|
|
terminal_create(struct display *display)
|
2008-12-08 03:59:11 +03:00
|
|
|
{
|
|
|
|
struct terminal *terminal;
|
2010-06-15 07:22:15 +04:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2013-06-06 11:32:41 +04:00
|
|
|
cairo_text_extents_t text_extents;
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2013-08-08 05:57:05 +04:00
|
|
|
terminal = xzalloc(sizeof *terminal);
|
2011-01-07 22:46:55 +03:00
|
|
|
terminal->color_scheme = &DEFAULT_COLORS;
|
|
|
|
terminal_init(terminal);
|
2011-01-07 22:46:58 +03:00
|
|
|
terminal->margin_top = 0;
|
2011-01-07 22:47:03 +03:00
|
|
|
terminal->margin_bottom = -1;
|
2012-02-01 00:24:48 +04:00
|
|
|
terminal->window = window_create(display);
|
2013-10-14 04:08:38 +04:00
|
|
|
terminal->widget = window_frame_create(terminal->window, terminal);
|
2013-10-24 07:47:35 +04:00
|
|
|
terminal->title = strdup("Wayland Terminal");
|
|
|
|
window_set_title(terminal->window, terminal->title);
|
2012-02-24 02:30:45 +04:00
|
|
|
widget_set_transparent(terminal->widget, 0);
|
2011-01-07 22:46:54 +03:00
|
|
|
|
|
|
|
init_state_machine(&terminal->state_machine);
|
2011-01-07 22:46:55 +03:00
|
|
|
init_color_table(terminal);
|
2011-01-07 22:46:54 +03:00
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
terminal->display = display;
|
2008-12-08 20:59:37 +03:00
|
|
|
terminal->margin = 5;
|
2013-10-24 03:29:14 +04:00
|
|
|
terminal->buffer_height = 1024;
|
|
|
|
terminal->end = 1;
|
2008-12-08 05:51:58 +03:00
|
|
|
|
2010-06-25 19:19:22 +04:00
|
|
|
window_set_user_data(terminal->window, terminal);
|
|
|
|
window_set_key_handler(terminal->window, key_handler);
|
2009-02-23 07:01:35 +03:00
|
|
|
window_set_keyboard_focus_handler(terminal->window,
|
2010-06-25 19:19:22 +04:00
|
|
|
keyboard_focus_handler);
|
2012-07-24 05:56:31 +04:00
|
|
|
window_set_fullscreen_handler(terminal->window, fullscreen_handler);
|
2013-05-22 16:41:34 +04:00
|
|
|
window_set_output_handler(terminal->window, output_handler);
|
2013-01-28 13:11:06 +04:00
|
|
|
window_set_close_handler(terminal->window, close_handler);
|
2012-07-24 05:56:31 +04:00
|
|
|
|
2013-09-05 07:41:06 +04:00
|
|
|
window_set_data_handler(terminal->window, data_handler);
|
|
|
|
window_set_drop_handler(terminal->window, drop_handler);
|
|
|
|
|
2012-01-10 21:23:19 +04:00
|
|
|
widget_set_redraw_handler(terminal->widget, redraw_handler);
|
|
|
|
widget_set_resize_handler(terminal->widget, resize_handler);
|
2012-01-10 18:43:58 +04:00
|
|
|
widget_set_button_handler(terminal->widget, button_handler);
|
2012-06-28 22:27:02 +04:00
|
|
|
widget_set_enter_handler(terminal->widget, enter_handler);
|
2012-01-10 18:43:58 +04:00
|
|
|
widget_set_motion_handler(terminal->widget, motion_handler);
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2010-06-15 07:22:15 +04:00
|
|
|
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
|
|
|
|
cr = cairo_create(surface);
|
2012-06-28 01:52:23 +04:00
|
|
|
cairo_set_font_size(cr, option_font_size);
|
2012-06-20 23:29:07 +04:00
|
|
|
cairo_select_font_face (cr, option_font,
|
2010-12-26 00:58:31 +03:00
|
|
|
CAIRO_FONT_SLANT_NORMAL,
|
|
|
|
CAIRO_FONT_WEIGHT_BOLD);
|
|
|
|
terminal->font_bold = cairo_get_scaled_font (cr);
|
|
|
|
cairo_scaled_font_reference(terminal->font_bold);
|
|
|
|
|
2012-06-20 23:29:07 +04:00
|
|
|
cairo_select_font_face (cr, option_font,
|
2010-12-26 00:58:31 +03:00
|
|
|
CAIRO_FONT_SLANT_NORMAL,
|
|
|
|
CAIRO_FONT_WEIGHT_NORMAL);
|
|
|
|
terminal->font_normal = cairo_get_scaled_font (cr);
|
|
|
|
cairo_scaled_font_reference(terminal->font_normal);
|
|
|
|
|
2010-06-15 07:22:15 +04:00
|
|
|
cairo_font_extents(cr, &terminal->extents);
|
2013-06-06 11:32:41 +04:00
|
|
|
|
|
|
|
/* Compute the average ascii glyph width */
|
|
|
|
cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
|
|
|
|
&text_extents);
|
|
|
|
terminal->average_width = howmany
|
|
|
|
(text_extents.width,
|
|
|
|
strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
|
|
|
|
terminal->average_width = ceil(terminal->average_width);
|
|
|
|
|
2010-06-15 07:22:15 +04:00
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
2012-07-20 19:32:51 +04:00
|
|
|
terminal_resize(terminal, 20, 5); /* Set minimum size first */
|
2012-06-21 01:30:03 +04:00
|
|
|
terminal_resize(terminal, 80, 25);
|
2012-02-01 00:24:48 +04:00
|
|
|
|
2012-06-21 06:44:03 +04:00
|
|
|
wl_list_insert(terminal_list.prev, &terminal->link);
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
return terminal;
|
|
|
|
}
|
|
|
|
|
2012-06-21 06:44:03 +04:00
|
|
|
static void
|
|
|
|
terminal_destroy(struct terminal *terminal)
|
|
|
|
{
|
2012-11-29 12:27:09 +04:00
|
|
|
display_unwatch_fd(terminal->display, terminal->master);
|
2012-06-21 06:44:03 +04:00
|
|
|
window_destroy(terminal->window);
|
|
|
|
close(terminal->master);
|
|
|
|
wl_list_remove(&terminal->link);
|
|
|
|
|
|
|
|
if (wl_list_empty(&terminal_list))
|
2012-11-29 12:27:09 +04:00
|
|
|
display_exit(terminal->display);
|
|
|
|
|
2013-10-24 07:47:35 +04:00
|
|
|
free(terminal->title);
|
2012-11-29 12:27:09 +04:00
|
|
|
free(terminal);
|
2012-06-21 06:44:03 +04:00
|
|
|
}
|
|
|
|
|
2011-09-15 01:33:48 +04:00
|
|
|
static void
|
|
|
|
io_handler(struct task *task, uint32_t events)
|
2008-12-08 07:17:31 +03:00
|
|
|
{
|
2011-09-15 01:33:48 +04:00
|
|
|
struct terminal *terminal =
|
|
|
|
container_of(task, struct terminal, io_task);
|
|
|
|
char buffer[256];
|
|
|
|
int len;
|
2011-01-20 01:14:33 +03:00
|
|
|
|
2012-06-21 06:44:03 +04:00
|
|
|
if (events & EPOLLHUP) {
|
|
|
|
terminal_destroy(terminal);
|
|
|
|
return;
|
|
|
|
}
|
2008-12-08 07:17:31 +03:00
|
|
|
|
2011-09-15 01:33:48 +04:00
|
|
|
len = read(terminal->master, buffer, sizeof buffer);
|
|
|
|
if (len < 0)
|
2012-06-21 06:44:03 +04:00
|
|
|
terminal_destroy(terminal);
|
|
|
|
else
|
|
|
|
terminal_data(terminal, buffer, len);
|
2008-12-08 07:17:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
terminal_run(struct terminal *terminal, const char *path)
|
|
|
|
{
|
2008-12-12 21:39:03 +03:00
|
|
|
int master;
|
2008-12-08 07:17:31 +03:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
pid = forkpty(&master, NULL, NULL, NULL);
|
|
|
|
if (pid == 0) {
|
2012-06-21 06:14:31 +04:00
|
|
|
setenv("TERM", option_term, 1);
|
|
|
|
setenv("COLORTERM", option_term, 1);
|
2008-12-08 07:17:31 +03:00
|
|
|
if (execl(path, path, NULL)) {
|
|
|
|
printf("exec failed: %m\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2008-12-12 21:39:03 +03:00
|
|
|
} else if (pid < 0) {
|
|
|
|
fprintf(stderr, "failed to fork and create pty (%m).\n");
|
|
|
|
return -1;
|
2008-12-08 07:17:31 +03:00
|
|
|
}
|
|
|
|
|
2008-12-08 08:01:36 +03:00
|
|
|
terminal->master = master;
|
2008-12-08 07:17:31 +03:00
|
|
|
fcntl(master, F_SETFL, O_NONBLOCK);
|
2011-09-15 01:33:48 +04:00
|
|
|
terminal->io_task.run = io_handler;
|
|
|
|
display_watch_fd(terminal->display, terminal->master,
|
|
|
|
EPOLLIN | EPOLLHUP, &terminal->io_task);
|
2008-12-08 07:17:31 +03:00
|
|
|
|
2012-10-10 19:37:46 +04:00
|
|
|
window_set_fullscreen(terminal->window, option_fullscreen);
|
|
|
|
if (!window_is_fullscreen(terminal->window))
|
2011-09-14 04:58:00 +04:00
|
|
|
terminal_resize(terminal, 80, 24);
|
|
|
|
|
2008-12-08 07:17:31 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-12 05:05:57 +04:00
|
|
|
static const struct weston_option terminal_options[] = {
|
|
|
|
{ WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
|
2012-06-20 23:29:07 +04:00
|
|
|
{ WESTON_OPTION_STRING, "font", 0, &option_font },
|
2012-06-21 06:44:03 +04:00
|
|
|
{ WESTON_OPTION_STRING, "shell", 0, &option_shell },
|
2008-12-22 20:14:50 +03:00
|
|
|
};
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *d;
|
2008-12-08 03:59:11 +03:00
|
|
|
struct terminal *terminal;
|
2013-09-22 09:23:08 +04:00
|
|
|
struct weston_config *config;
|
|
|
|
struct weston_config_section *s;
|
2012-06-20 23:29:07 +04:00
|
|
|
|
2013-08-19 06:59:21 +04:00
|
|
|
/* as wcwidth is locale-dependent,
|
|
|
|
wcwidth needs setlocale call to function properly. */
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2012-06-21 06:44:03 +04:00
|
|
|
option_shell = getenv("SHELL");
|
|
|
|
if (!option_shell)
|
|
|
|
option_shell = "/bin/bash";
|
|
|
|
|
2013-09-22 10:02:31 +04:00
|
|
|
config = weston_config_parse("weston.ini");
|
2013-09-22 09:23:08 +04:00
|
|
|
s = weston_config_get_section(config, "terminal", NULL, NULL);
|
|
|
|
weston_config_section_get_string(s, "font", &option_font, "mono");
|
|
|
|
weston_config_section_get_int(s, "font-size", &option_font_size, 14);
|
|
|
|
weston_config_section_get_string(s, "term", &option_term, "xterm");
|
|
|
|
weston_config_destroy(config);
|
2012-03-12 05:05:57 +04:00
|
|
|
|
2013-02-21 00:27:49 +04:00
|
|
|
d = display_create(&argc, argv);
|
2010-11-22 22:34:19 +03:00
|
|
|
if (d == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2012-06-21 06:44:03 +04:00
|
|
|
wl_list_init(&terminal_list);
|
2012-10-10 19:37:46 +04:00
|
|
|
terminal = terminal_create(d);
|
2012-06-21 06:44:03 +04:00
|
|
|
if (terminal_run(terminal, option_shell))
|
2008-12-12 21:39:03 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-08 03:59:11 +03:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
display_run(d);
|
2008-12-08 03:59:11 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|