Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 10 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 22 | */ |
| 23 | |
Kristian Høgsberg | 0b36d97 | 2013-08-21 22:13:17 -0700 | [diff] [blame] | 24 | #include <config.h> |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 25 | |
Philipp Brüschweiler | fdb4b02 | 2012-08-18 13:38:38 +0200 | [diff] [blame] | 26 | #include <stdbool.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <unistd.h> |
| 33 | #include <math.h> |
| 34 | #include <time.h> |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 35 | #include <pty.h> |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 36 | #include <ctype.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 37 | #include <cairo.h> |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 38 | #include <sys/epoll.h> |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 39 | #include <wchar.h> |
| 40 | #include <locale.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 41 | |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 42 | #include <linux/input.h> |
| 43 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 44 | #include <wayland-client.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 45 | |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 46 | #include "shared/config-parser.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame^] | 47 | #include "shared/helpers.h" |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 48 | #include "window.h" |
| 49 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 50 | static int option_fullscreen; |
Kristian Høgsberg | 9c3dee1 | 2013-09-21 22:23:08 -0700 | [diff] [blame] | 51 | static char *option_font; |
| 52 | static int option_font_size; |
| 53 | static char *option_term; |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 54 | static char *option_shell; |
| 55 | |
| 56 | static struct wl_list terminal_list; |
| 57 | |
| 58 | static struct terminal * |
Kristian Høgsberg | b7ed4cb | 2012-10-10 11:37:46 -0400 | [diff] [blame] | 59 | terminal_create(struct display *display); |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 60 | static void |
| 61 | terminal_destroy(struct terminal *terminal); |
| 62 | static int |
| 63 | terminal_run(struct terminal *terminal, const char *path); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 64 | |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 65 | #define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \ |
| 66 | " !\"#$%&'()*+,-./" \ |
| 67 | "0123456789" \ |
| 68 | ":;<=>?@" \ |
| 69 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ |
| 70 | "[\\]^_`" \ |
| 71 | "abcdefghijklmnopqrstuvwxyz" \ |
| 72 | "{|}~" \ |
| 73 | "" |
| 74 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 75 | #define MOD_SHIFT 0x01 |
| 76 | #define MOD_ALT 0x02 |
| 77 | #define MOD_CTRL 0x04 |
| 78 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 79 | #define ATTRMASK_BOLD 0x01 |
| 80 | #define ATTRMASK_UNDERLINE 0x02 |
| 81 | #define ATTRMASK_BLINK 0x04 |
| 82 | #define ATTRMASK_INVERSE 0x08 |
Callum Lowcay | 81179db | 2011-01-10 12:14:01 +1300 | [diff] [blame] | 83 | #define ATTRMASK_CONCEALED 0x10 |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 84 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 85 | /* Buffer sizes */ |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 86 | #define MAX_RESPONSE 256 |
| 87 | #define MAX_ESCAPE 255 |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 88 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 89 | /* Terminal modes */ |
| 90 | #define MODE_SHOW_CURSOR 0x00000001 |
| 91 | #define MODE_INVERSE 0x00000002 |
| 92 | #define MODE_AUTOWRAP 0x00000004 |
| 93 | #define MODE_AUTOREPEAT 0x00000008 |
| 94 | #define MODE_LF_NEWLINE 0x00000010 |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 95 | #define MODE_IRM 0x00000020 |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 96 | #define MODE_DELETE_SENDS_DEL 0x00000040 |
| 97 | #define MODE_ALT_SENDS_ESC 0x00000080 |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 98 | |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 99 | union utf8_char { |
| 100 | unsigned char byte[4]; |
| 101 | uint32_t ch; |
| 102 | }; |
| 103 | |
| 104 | enum utf8_state { |
| 105 | utf8state_start, |
| 106 | utf8state_accept, |
| 107 | utf8state_reject, |
| 108 | utf8state_expect3, |
| 109 | utf8state_expect2, |
| 110 | utf8state_expect1 |
| 111 | }; |
| 112 | |
| 113 | struct utf8_state_machine { |
| 114 | enum utf8_state state; |
| 115 | int len; |
| 116 | union utf8_char s; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 117 | uint32_t unicode; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static void |
| 121 | init_state_machine(struct utf8_state_machine *machine) |
| 122 | { |
| 123 | machine->state = utf8state_start; |
| 124 | machine->len = 0; |
| 125 | machine->s.ch = 0; |
| 126 | } |
| 127 | |
| 128 | static enum utf8_state |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 129 | utf8_next_char(struct utf8_state_machine *machine, unsigned char c) |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 130 | { |
| 131 | switch(machine->state) { |
| 132 | case utf8state_start: |
| 133 | case utf8state_accept: |
| 134 | case utf8state_reject: |
| 135 | machine->s.ch = 0; |
| 136 | machine->len = 0; |
| 137 | if(c == 0xC0 || c == 0xC1) { |
| 138 | /* overlong encoding, reject */ |
| 139 | machine->state = utf8state_reject; |
| 140 | } else if((c & 0x80) == 0) { |
| 141 | /* single byte, accept */ |
| 142 | machine->s.byte[machine->len++] = c; |
| 143 | machine->state = utf8state_accept; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 144 | machine->unicode = c; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 145 | } else if((c & 0xC0) == 0x80) { |
| 146 | /* parser out of sync, ignore byte */ |
| 147 | machine->state = utf8state_start; |
| 148 | } else if((c & 0xE0) == 0xC0) { |
| 149 | /* start of two byte sequence */ |
| 150 | machine->s.byte[machine->len++] = c; |
| 151 | machine->state = utf8state_expect1; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 152 | machine->unicode = c & 0x1f; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 153 | } else if((c & 0xF0) == 0xE0) { |
| 154 | /* start of three byte sequence */ |
| 155 | machine->s.byte[machine->len++] = c; |
| 156 | machine->state = utf8state_expect2; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 157 | machine->unicode = c & 0x0f; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 158 | } else if((c & 0xF8) == 0xF0) { |
| 159 | /* start of four byte sequence */ |
| 160 | machine->s.byte[machine->len++] = c; |
| 161 | machine->state = utf8state_expect3; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 162 | machine->unicode = c & 0x07; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 163 | } else { |
| 164 | /* overlong encoding, reject */ |
| 165 | machine->state = utf8state_reject; |
| 166 | } |
| 167 | break; |
| 168 | case utf8state_expect3: |
| 169 | machine->s.byte[machine->len++] = c; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 170 | machine->unicode = (machine->unicode << 6) | (c & 0x3f); |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 171 | if((c & 0xC0) == 0x80) { |
| 172 | /* all good, continue */ |
| 173 | machine->state = utf8state_expect2; |
| 174 | } else { |
| 175 | /* missing extra byte, reject */ |
| 176 | machine->state = utf8state_reject; |
| 177 | } |
| 178 | break; |
| 179 | case utf8state_expect2: |
| 180 | machine->s.byte[machine->len++] = c; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 181 | machine->unicode = (machine->unicode << 6) | (c & 0x3f); |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 182 | if((c & 0xC0) == 0x80) { |
| 183 | /* all good, continue */ |
| 184 | machine->state = utf8state_expect1; |
| 185 | } else { |
| 186 | /* missing extra byte, reject */ |
| 187 | machine->state = utf8state_reject; |
| 188 | } |
| 189 | break; |
| 190 | case utf8state_expect1: |
| 191 | machine->s.byte[machine->len++] = c; |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 192 | machine->unicode = (machine->unicode << 6) | (c & 0x3f); |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 193 | if((c & 0xC0) == 0x80) { |
| 194 | /* all good, accept */ |
| 195 | machine->state = utf8state_accept; |
| 196 | } else { |
| 197 | /* missing extra byte, reject */ |
| 198 | machine->state = utf8state_reject; |
| 199 | } |
| 200 | break; |
| 201 | default: |
| 202 | machine->state = utf8state_reject; |
| 203 | break; |
| 204 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 205 | |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 206 | return machine->state; |
| 207 | } |
| 208 | |
Kristian Høgsberg | 3e12583 | 2013-08-13 17:23:54 -0700 | [diff] [blame] | 209 | static uint32_t |
| 210 | get_unicode(union utf8_char utf8) |
| 211 | { |
| 212 | struct utf8_state_machine machine; |
| 213 | int i; |
| 214 | |
| 215 | init_state_machine(&machine); |
| 216 | for (i = 0; i < 4; i++) { |
| 217 | utf8_next_char(&machine, utf8.byte[i]); |
| 218 | if (machine.state == utf8state_accept || |
| 219 | machine.state == utf8state_reject) |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | if (machine.state == utf8state_reject) |
| 224 | return 0xfffd; |
| 225 | |
| 226 | return machine.unicode; |
| 227 | } |
| 228 | |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 229 | static bool |
| 230 | is_wide(union utf8_char utf8) |
| 231 | { |
| 232 | uint32_t unichar = get_unicode(utf8); |
| 233 | return wcwidth(unichar) > 1; |
| 234 | } |
| 235 | |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 236 | struct char_sub { |
| 237 | union utf8_char match; |
| 238 | union utf8_char replace; |
| 239 | }; |
| 240 | /* Set last char_sub match to NULL char */ |
| 241 | typedef struct char_sub *character_set; |
| 242 | |
| 243 | struct char_sub CS_US[] = { |
| 244 | {{{0, }}, {{0, }}} |
| 245 | }; |
| 246 | static struct char_sub CS_UK[] = { |
David Herrmann | a6128d6 | 2012-05-29 09:37:02 +0200 | [diff] [blame] | 247 | {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */ |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 248 | {{{0, }}, {{0, }}} |
| 249 | }; |
| 250 | static struct char_sub CS_SPECIAL[] = { |
David Herrmann | a6128d6 | 2012-05-29 09:37:02 +0200 | [diff] [blame] | 251 | {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */ |
| 252 | {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */ |
| 253 | {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */ |
| 254 | {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */ |
| 255 | {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */ |
| 256 | {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */ |
| 257 | {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */ |
| 258 | {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */ |
| 259 | {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL:  */ |
| 260 | {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */ |
| 261 | {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */ |
| 262 | {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */ |
| 263 | {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */ |
| 264 | {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */ |
| 265 | {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */ |
| 266 | {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */ |
| 267 | {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */ |
| 268 | {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */ |
| 269 | {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */ |
| 270 | {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */ |
| 271 | {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */ |
| 272 | {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */ |
| 273 | {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */ |
| 274 | {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */ |
| 275 | {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */ |
| 276 | {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */ |
| 277 | {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */ |
| 278 | {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */ |
| 279 | {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */ |
| 280 | {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */ |
| 281 | {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */ |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 282 | {{{0, }}, {{0, }}} |
| 283 | }; |
| 284 | |
| 285 | static void |
| 286 | apply_char_set(character_set cs, union utf8_char *utf8) |
| 287 | { |
| 288 | int i = 0; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 289 | |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 290 | while (cs[i].match.byte[0]) { |
| 291 | if ((*utf8).ch == cs[i].match.ch) { |
| 292 | *utf8 = cs[i].replace; |
| 293 | break; |
| 294 | } |
| 295 | i++; |
| 296 | } |
| 297 | } |
| 298 | |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 299 | struct key_map { |
| 300 | int sym; |
| 301 | int num; |
| 302 | char escape; |
| 303 | char code; |
| 304 | }; |
| 305 | /* Set last key_sub sym to NULL */ |
| 306 | typedef struct key_map *keyboard_mode; |
| 307 | |
| 308 | static struct key_map KM_NORMAL[] = { |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 309 | { XKB_KEY_Left, 1, '[', 'D' }, |
| 310 | { XKB_KEY_Right, 1, '[', 'C' }, |
| 311 | { XKB_KEY_Up, 1, '[', 'A' }, |
| 312 | { XKB_KEY_Down, 1, '[', 'B' }, |
| 313 | { XKB_KEY_Home, 1, '[', 'H' }, |
| 314 | { XKB_KEY_End, 1, '[', 'F' }, |
| 315 | { 0, 0, 0, 0 } |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 316 | }; |
| 317 | static struct key_map KM_APPLICATION[] = { |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 318 | { XKB_KEY_Left, 1, 'O', 'D' }, |
| 319 | { XKB_KEY_Right, 1, 'O', 'C' }, |
| 320 | { XKB_KEY_Up, 1, 'O', 'A' }, |
| 321 | { XKB_KEY_Down, 1, 'O', 'B' }, |
| 322 | { XKB_KEY_Home, 1, 'O', 'H' }, |
| 323 | { XKB_KEY_End, 1, 'O', 'F' }, |
| 324 | { XKB_KEY_KP_Enter, 1, 'O', 'M' }, |
| 325 | { XKB_KEY_KP_Multiply, 1, 'O', 'j' }, |
| 326 | { XKB_KEY_KP_Add, 1, 'O', 'k' }, |
| 327 | { XKB_KEY_KP_Separator, 1, 'O', 'l' }, |
| 328 | { XKB_KEY_KP_Subtract, 1, 'O', 'm' }, |
| 329 | { XKB_KEY_KP_Divide, 1, 'O', 'o' }, |
| 330 | { 0, 0, 0, 0 } |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | static int |
| 334 | function_key_response(char escape, int num, uint32_t modifiers, |
| 335 | char code, char *response) |
| 336 | { |
| 337 | int mod_num = 0; |
| 338 | int len; |
| 339 | |
Kristian Høgsberg | 7016313 | 2012-05-08 15:55:39 -0400 | [diff] [blame] | 340 | if (modifiers & MOD_SHIFT_MASK) mod_num |= 1; |
| 341 | if (modifiers & MOD_ALT_MASK) mod_num |= 2; |
| 342 | if (modifiers & MOD_CONTROL_MASK) mod_num |= 4; |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 343 | |
| 344 | if (mod_num != 0) |
| 345 | len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c", |
| 346 | num, mod_num + 1, code); |
| 347 | else if (code != '~') |
| 348 | len = snprintf(response, MAX_RESPONSE, "\e%c%c", |
| 349 | escape, code); |
| 350 | else |
| 351 | len = snprintf(response, MAX_RESPONSE, "\e%c%d%c", |
| 352 | escape, num, code); |
| 353 | |
| 354 | if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1; |
| 355 | else return len; |
| 356 | } |
| 357 | |
| 358 | /* returns the number of bytes written into response, |
| 359 | * which must have room for MAX_RESPONSE bytes */ |
| 360 | static int |
| 361 | apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response) |
| 362 | { |
| 363 | struct key_map map; |
| 364 | int len = 0; |
| 365 | int i = 0; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 366 | |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 367 | while (mode[i].sym) { |
| 368 | map = mode[i++]; |
| 369 | if (sym == map.sym) { |
| 370 | len = function_key_response(map.escape, map.num, |
| 371 | modifiers, map.code, |
| 372 | response); |
| 373 | break; |
| 374 | } |
| 375 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 376 | |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 377 | return len; |
| 378 | } |
| 379 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 380 | struct terminal_color { double r, g, b, a; }; |
| 381 | struct attr { |
| 382 | unsigned char fg, bg; |
| 383 | char a; /* attributes format: |
| 384 | * 76543210 |
Callum Lowcay | 81179db | 2011-01-10 12:14:01 +1300 | [diff] [blame] | 385 | * cilub */ |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 386 | char s; /* in selection */ |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 387 | }; |
| 388 | struct color_scheme { |
| 389 | struct terminal_color palette[16]; |
Kristian Høgsberg | 71eca89 | 2011-01-11 10:13:00 -0500 | [diff] [blame] | 390 | char border; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 391 | struct attr default_attr; |
| 392 | }; |
| 393 | |
| 394 | static void |
| 395 | attr_init(struct attr *data_attr, struct attr attr, int n) |
| 396 | { |
| 397 | int i; |
| 398 | for (i = 0; i < n; i++) { |
| 399 | data_attr[i] = attr; |
| 400 | } |
| 401 | } |
| 402 | |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 403 | enum escape_state { |
| 404 | escape_state_normal = 0, |
| 405 | escape_state_escape, |
| 406 | escape_state_dcs, |
| 407 | escape_state_csi, |
| 408 | escape_state_osc, |
| 409 | escape_state_inner_escape, |
| 410 | escape_state_ignore, |
| 411 | escape_state_special |
| 412 | }; |
| 413 | |
| 414 | #define ESC_FLAG_WHAT 0x01 |
| 415 | #define ESC_FLAG_GT 0x02 |
| 416 | #define ESC_FLAG_BANG 0x04 |
| 417 | #define ESC_FLAG_CASH 0x08 |
| 418 | #define ESC_FLAG_SQUOTE 0x10 |
| 419 | #define ESC_FLAG_DQUOTE 0x20 |
| 420 | #define ESC_FLAG_SPACE 0x40 |
| 421 | |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 422 | enum { |
| 423 | SELECT_NONE, |
| 424 | SELECT_CHAR, |
| 425 | SELECT_WORD, |
| 426 | SELECT_LINE |
| 427 | }; |
| 428 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 429 | struct terminal { |
| 430 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 431 | struct widget *widget; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 432 | struct display *display; |
Kristian Høgsberg | a83be20 | 2013-10-23 20:47:35 -0700 | [diff] [blame] | 433 | char *title; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 434 | union utf8_char *data; |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 435 | struct task io_task; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 436 | char *tab_ruler; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 437 | struct attr *data_attr; |
| 438 | struct attr curr_attr; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 439 | uint32_t mode; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 440 | char origin_mode; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 441 | char saved_origin_mode; |
| 442 | struct attr saved_attr; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 443 | union utf8_char last_char; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 444 | int margin_top, margin_bottom; |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 445 | character_set cs, g0, g1; |
| 446 | character_set saved_cs, saved_g0, saved_g1; |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 447 | keyboard_mode key_mode; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 448 | int data_pitch, attr_pitch; /* The width in bytes of a line */ |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 449 | int width, height, row, column, max_width; |
| 450 | uint32_t buffer_height; |
| 451 | uint32_t start, end, saved_start, log_size; |
Magnus Hoff | 1046f12 | 2014-08-05 15:05:59 +0200 | [diff] [blame] | 452 | wl_fixed_t smooth_scroll; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 453 | int saved_row, saved_column; |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 454 | int scrolling; |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 455 | int send_cursor_position; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 456 | int fd, master; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 457 | uint32_t modifiers; |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 458 | char escape[MAX_ESCAPE+1]; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 459 | int escape_length; |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 460 | enum escape_state state; |
| 461 | enum escape_state outer_state; |
| 462 | int escape_flags; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 463 | struct utf8_state_machine state_machine; |
Kristian Høgsberg | 1584c57 | 2008-12-08 12:59:37 -0500 | [diff] [blame] | 464 | int margin; |
Kristian Høgsberg | 12308a4 | 2009-09-28 13:08:50 -0400 | [diff] [blame] | 465 | struct color_scheme *color_scheme; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 466 | struct terminal_color color_table[256]; |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 467 | cairo_font_extents_t extents; |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 468 | double average_width; |
Kristian Høgsberg | 7ae6b1a | 2010-12-25 16:58:31 -0500 | [diff] [blame] | 469 | cairo_scaled_font_t *font_normal, *font_bold; |
Kristian Høgsberg | 88fd408 | 2012-06-21 15:55:03 -0400 | [diff] [blame] | 470 | uint32_t hide_cursor_serial; |
Kristian Høgsberg | b405a80 | 2014-02-05 14:44:04 -0800 | [diff] [blame] | 471 | int size_in_title; |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 472 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 473 | struct wl_data_source *selection; |
Xiong Zhang | 49c6aee | 2013-11-25 18:42:52 +0800 | [diff] [blame] | 474 | uint32_t click_time; |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 475 | int dragging, click_count; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 476 | int selection_start_x, selection_start_y; |
| 477 | int selection_end_x, selection_end_y; |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 478 | int selection_start_row, selection_start_col; |
| 479 | int selection_end_row, selection_end_col; |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 480 | struct wl_list link; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 481 | }; |
| 482 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 483 | /* Create default tab stops, every 8 characters */ |
| 484 | static void |
| 485 | terminal_init_tabs(struct terminal *terminal) |
| 486 | { |
| 487 | int i = 0; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 488 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 489 | while (i < terminal->width) { |
| 490 | if (i % 8 == 0) |
| 491 | terminal->tab_ruler[i] = 1; |
| 492 | else |
| 493 | terminal->tab_ruler[i] = 0; |
| 494 | i++; |
| 495 | } |
| 496 | } |
| 497 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 498 | static void |
| 499 | terminal_init(struct terminal *terminal) |
| 500 | { |
| 501 | terminal->curr_attr = terminal->color_scheme->default_attr; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 502 | terminal->origin_mode = 0; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 503 | terminal->mode = MODE_SHOW_CURSOR | |
| 504 | MODE_AUTOREPEAT | |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 505 | MODE_ALT_SENDS_ESC | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 506 | MODE_AUTOWRAP; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 507 | |
| 508 | terminal->row = 0; |
| 509 | terminal->column = 0; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 510 | |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 511 | terminal->g0 = CS_US; |
| 512 | terminal->g1 = CS_US; |
| 513 | terminal->cs = terminal->g0; |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 514 | terminal->key_mode = KM_NORMAL; |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 515 | |
| 516 | terminal->saved_g0 = terminal->g0; |
| 517 | terminal->saved_g1 = terminal->g1; |
| 518 | terminal->saved_cs = terminal->cs; |
| 519 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 520 | terminal->saved_attr = terminal->curr_attr; |
| 521 | terminal->saved_origin_mode = terminal->origin_mode; |
| 522 | terminal->saved_row = terminal->row; |
| 523 | terminal->saved_column = terminal->column; |
| 524 | |
| 525 | if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static void |
| 529 | init_color_table(struct terminal *terminal) |
| 530 | { |
| 531 | int c, r; |
| 532 | struct terminal_color *color_table = terminal->color_table; |
| 533 | |
| 534 | for (c = 0; c < 256; c ++) { |
| 535 | if (c < 16) { |
| 536 | color_table[c] = terminal->color_scheme->palette[c]; |
| 537 | } else if (c < 232) { |
| 538 | r = c - 16; |
| 539 | color_table[c].b = ((double)(r % 6) / 6.0); r /= 6; |
| 540 | color_table[c].g = ((double)(r % 6) / 6.0); r /= 6; |
| 541 | color_table[c].r = ((double)(r % 6) / 6.0); |
| 542 | color_table[c].a = 1.0; |
| 543 | } else { |
| 544 | r = (c - 232) * 10 + 8; |
| 545 | color_table[c].r = ((double) r) / 256.0; |
| 546 | color_table[c].g = color_table[c].r; |
| 547 | color_table[c].b = color_table[c].r; |
| 548 | color_table[c].a = 1.0; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 553 | static union utf8_char * |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 554 | terminal_get_row(struct terminal *terminal, int row) |
| 555 | { |
| 556 | int index; |
| 557 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 558 | index = (row + terminal->start) & (terminal->buffer_height - 1); |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 559 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 560 | return (void *) terminal->data + index * terminal->data_pitch; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 561 | } |
| 562 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 563 | static struct attr* |
Kristian Høgsberg | 8c25420 | 2010-12-25 08:58:46 -0500 | [diff] [blame] | 564 | terminal_get_attr_row(struct terminal *terminal, int row) |
| 565 | { |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 566 | int index; |
| 567 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 568 | index = (row + terminal->start) & (terminal->buffer_height - 1); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 569 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 570 | return (void *) terminal->data_attr + index * terminal->attr_pitch; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 573 | union decoded_attr { |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 574 | struct attr attr; |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 575 | uint32_t key; |
Kristian Høgsberg | 01994a5 | 2011-01-11 10:26:04 -0500 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | static void |
| 579 | terminal_decode_attr(struct terminal *terminal, int row, int col, |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 580 | union decoded_attr *decoded) |
Kristian Høgsberg | 8c25420 | 2010-12-25 08:58:46 -0500 | [diff] [blame] | 581 | { |
Kristian Høgsberg | 01994a5 | 2011-01-11 10:26:04 -0500 | [diff] [blame] | 582 | struct attr attr; |
| 583 | int foreground, background, tmp; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 584 | |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 585 | decoded->attr.s = 0; |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 586 | if (((row == terminal->selection_start_row && |
| 587 | col >= terminal->selection_start_col) || |
| 588 | row > terminal->selection_start_row) && |
| 589 | ((row == terminal->selection_end_row && |
| 590 | col < terminal->selection_end_col) || |
| 591 | row < terminal->selection_end_row)) |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 592 | decoded->attr.s = 1; |
Kristian Høgsberg | 01994a5 | 2011-01-11 10:26:04 -0500 | [diff] [blame] | 593 | |
| 594 | /* get the attributes for this character cell */ |
| 595 | attr = terminal_get_attr_row(terminal, row)[col]; |
| 596 | if ((attr.a & ATTRMASK_INVERSE) || |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 597 | decoded->attr.s || |
Kristian Høgsberg | 01994a5 | 2011-01-11 10:26:04 -0500 | [diff] [blame] | 598 | ((terminal->mode & MODE_SHOW_CURSOR) && |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 599 | window_has_focus(terminal->window) && terminal->row == row && |
Kristian Høgsberg | 01994a5 | 2011-01-11 10:26:04 -0500 | [diff] [blame] | 600 | terminal->column == col)) { |
| 601 | foreground = attr.bg; |
| 602 | background = attr.fg; |
| 603 | if (attr.a & ATTRMASK_BOLD) { |
| 604 | if (foreground <= 16) foreground |= 0x08; |
| 605 | if (background <= 16) background &= 0x07; |
| 606 | } |
| 607 | } else { |
| 608 | foreground = attr.fg; |
| 609 | background = attr.bg; |
| 610 | } |
| 611 | |
| 612 | if (terminal->mode & MODE_INVERSE) { |
| 613 | tmp = foreground; |
| 614 | foreground = background; |
| 615 | background = tmp; |
| 616 | if (attr.a & ATTRMASK_BOLD) { |
| 617 | if (foreground <= 16) foreground |= 0x08; |
| 618 | if (background <= 16) background &= 0x07; |
| 619 | } |
| 620 | } |
| 621 | |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 622 | decoded->attr.fg = foreground; |
| 623 | decoded->attr.bg = background; |
| 624 | decoded->attr.a = attr.a; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 627 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 628 | static void |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 629 | terminal_scroll_buffer(struct terminal *terminal, int d) |
| 630 | { |
| 631 | int i; |
| 632 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 633 | terminal->start += d; |
| 634 | if (d < 0) { |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 635 | d = 0 - d; |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 636 | for (i = 0; i < d; i++) { |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 637 | memset(terminal_get_row(terminal, i), 0, terminal->data_pitch); |
| 638 | attr_init(terminal_get_attr_row(terminal, i), |
| 639 | terminal->curr_attr, terminal->width); |
| 640 | } |
| 641 | } else { |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 642 | for (i = terminal->height - d; i < terminal->height; i++) { |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 643 | memset(terminal_get_row(terminal, i), 0, terminal->data_pitch); |
| 644 | attr_init(terminal_get_attr_row(terminal, i), |
| 645 | terminal->curr_attr, terminal->width); |
| 646 | } |
| 647 | } |
Kristian Høgsberg | 18e928d | 2012-06-27 19:29:41 -0400 | [diff] [blame] | 648 | |
| 649 | terminal->selection_start_row -= d; |
| 650 | terminal->selection_end_row -= d; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static void |
| 654 | terminal_scroll_window(struct terminal *terminal, int d) |
| 655 | { |
| 656 | int i; |
| 657 | int window_height; |
| 658 | int from_row, to_row; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 659 | |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 660 | // scrolling range is inclusive |
| 661 | window_height = terminal->margin_bottom - terminal->margin_top + 1; |
| 662 | d = d % (window_height + 1); |
| 663 | if(d < 0) { |
| 664 | d = 0 - d; |
| 665 | to_row = terminal->margin_bottom; |
| 666 | from_row = terminal->margin_bottom - d; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 667 | |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 668 | for (i = 0; i < (window_height - d); i++) { |
| 669 | memcpy(terminal_get_row(terminal, to_row - i), |
| 670 | terminal_get_row(terminal, from_row - i), |
| 671 | terminal->data_pitch); |
| 672 | memcpy(terminal_get_attr_row(terminal, to_row - i), |
| 673 | terminal_get_attr_row(terminal, from_row - i), |
| 674 | terminal->attr_pitch); |
| 675 | } |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 676 | for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) { |
| 677 | memset(terminal_get_row(terminal, i), 0, terminal->data_pitch); |
Callum Lowcay | 86653ed | 2011-01-07 19:47:03 +0000 | [diff] [blame] | 678 | attr_init(terminal_get_attr_row(terminal, i), |
| 679 | terminal->curr_attr, terminal->width); |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 680 | } |
| 681 | } else { |
| 682 | to_row = terminal->margin_top; |
| 683 | from_row = terminal->margin_top + d; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 684 | |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 685 | for (i = 0; i < (window_height - d); i++) { |
| 686 | memcpy(terminal_get_row(terminal, to_row + i), |
| 687 | terminal_get_row(terminal, from_row + i), |
| 688 | terminal->data_pitch); |
| 689 | memcpy(terminal_get_attr_row(terminal, to_row + i), |
| 690 | terminal_get_attr_row(terminal, from_row + i), |
| 691 | terminal->attr_pitch); |
| 692 | } |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 693 | for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) { |
| 694 | memset(terminal_get_row(terminal, i), 0, terminal->data_pitch); |
Callum Lowcay | 86653ed | 2011-01-07 19:47:03 +0000 | [diff] [blame] | 695 | attr_init(terminal_get_attr_row(terminal, i), |
| 696 | terminal->curr_attr, terminal->width); |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | static void |
| 702 | terminal_scroll(struct terminal *terminal, int d) |
| 703 | { |
| 704 | if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1) |
| 705 | terminal_scroll_buffer(terminal, d); |
| 706 | else |
| 707 | terminal_scroll_window(terminal, d); |
| 708 | } |
| 709 | |
| 710 | static void |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 711 | terminal_shift_line(struct terminal *terminal, int d) |
| 712 | { |
| 713 | union utf8_char *row; |
Kristian Høgsberg | 0043961 | 2011-01-25 15:16:01 -0500 | [diff] [blame] | 714 | struct attr *attr_row; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 715 | |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 716 | row = terminal_get_row(terminal, terminal->row); |
| 717 | attr_row = terminal_get_attr_row(terminal, terminal->row); |
| 718 | |
| 719 | if ((terminal->width + d) <= terminal->column) |
| 720 | d = terminal->column + 1 - terminal->width; |
| 721 | if ((terminal->column + d) >= terminal->width) |
| 722 | d = terminal->width - terminal->column - 1; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 723 | |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 724 | if (d < 0) { |
| 725 | d = 0 - d; |
| 726 | memmove(&row[terminal->column], |
| 727 | &row[terminal->column + d], |
| 728 | (terminal->width - terminal->column - d) * sizeof(union utf8_char)); |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 729 | memmove(&attr_row[terminal->column], &attr_row[terminal->column + d], |
| 730 | (terminal->width - terminal->column - d) * sizeof(struct attr)); |
| 731 | memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char)); |
| 732 | attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d); |
| 733 | } else { |
| 734 | memmove(&row[terminal->column + d], &row[terminal->column], |
| 735 | (terminal->width - terminal->column - d) * sizeof(union utf8_char)); |
| 736 | memmove(&attr_row[terminal->column + d], &attr_row[terminal->column], |
| 737 | (terminal->width - terminal->column - d) * sizeof(struct attr)); |
| 738 | memset(&row[terminal->column], 0, d * sizeof(union utf8_char)); |
| 739 | attr_init(&attr_row[terminal->column], terminal->curr_attr, d); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static void |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 744 | terminal_resize_cells(struct terminal *terminal, |
| 745 | int width, int height) |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 746 | { |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 747 | union utf8_char *data; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 748 | struct attr *data_attr; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 749 | char *tab_ruler; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 750 | int data_pitch, attr_pitch; |
Kristian Høgsberg | 0043961 | 2011-01-25 15:16:01 -0500 | [diff] [blame] | 751 | int i, l, total_rows; |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 752 | uint32_t d, uheight = height; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 753 | struct rectangle allocation; |
Callum Lowcay | a0ee21c | 2011-01-07 19:46:56 +0000 | [diff] [blame] | 754 | struct winsize ws; |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 755 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 756 | if (uheight > terminal->buffer_height) |
| 757 | height = terminal->buffer_height; |
| 758 | |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 759 | if (terminal->width == width && terminal->height == height) |
| 760 | return; |
| 761 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 762 | if (terminal->data && width <= terminal->max_width) { |
| 763 | d = 0; |
| 764 | if (height < terminal->height && height <= terminal->row) |
| 765 | d = terminal->height - height; |
| 766 | else if (height > terminal->height && |
| 767 | terminal->height - 1 == terminal->row) { |
| 768 | d = terminal->height - height; |
| 769 | if (terminal->log_size < uheight) |
| 770 | d = -terminal->start; |
| 771 | } |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 772 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 773 | terminal->start += d; |
| 774 | terminal->row -= d; |
| 775 | } else { |
| 776 | terminal->max_width = width; |
| 777 | data_pitch = width * sizeof(union utf8_char); |
Bryce W. Harrington | 66cd2c1 | 2014-04-28 18:44:08 +0000 | [diff] [blame] | 778 | data = xzalloc(data_pitch * terminal->buffer_height); |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 779 | attr_pitch = width * sizeof(struct attr); |
Bryce W. Harrington | 66cd2c1 | 2014-04-28 18:44:08 +0000 | [diff] [blame] | 780 | data_attr = xmalloc(attr_pitch * terminal->buffer_height); |
| 781 | tab_ruler = xzalloc(width); |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 782 | attr_init(data_attr, terminal->curr_attr, |
| 783 | width * terminal->buffer_height); |
| 784 | |
| 785 | if (terminal->data && terminal->data_attr) { |
| 786 | if (width > terminal->width) |
| 787 | l = terminal->width; |
| 788 | else |
| 789 | l = width; |
| 790 | |
| 791 | if (terminal->height > height) { |
| 792 | total_rows = height; |
| 793 | i = 1 + terminal->row - height; |
| 794 | if (i > 0) { |
| 795 | terminal->start += i; |
| 796 | terminal->row = terminal->row - i; |
| 797 | } |
| 798 | } else { |
| 799 | total_rows = terminal->height; |
José Bollo | 4a4704a | 2013-09-13 11:28:51 +0200 | [diff] [blame] | 800 | } |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 801 | |
| 802 | for (i = 0; i < total_rows; i++) { |
| 803 | memcpy(&data[width * i], |
| 804 | terminal_get_row(terminal, i), |
| 805 | l * sizeof(union utf8_char)); |
| 806 | memcpy(&data_attr[width * i], |
| 807 | terminal_get_attr_row(terminal, i), |
| 808 | l * sizeof(struct attr)); |
| 809 | } |
| 810 | |
| 811 | free(terminal->data); |
| 812 | free(terminal->data_attr); |
| 813 | free(terminal->tab_ruler); |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 814 | } |
| 815 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 816 | terminal->data_pitch = data_pitch; |
| 817 | terminal->attr_pitch = attr_pitch; |
| 818 | terminal->data = data; |
| 819 | terminal->data_attr = data_attr; |
| 820 | terminal->tab_ruler = tab_ruler; |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 821 | terminal->start = 0; |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 822 | } |
| 823 | |
Callum Lowcay | 86653ed | 2011-01-07 19:47:03 +0000 | [diff] [blame] | 824 | terminal->margin_bottom = |
| 825 | height - (terminal->height - terminal->margin_bottom); |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 826 | terminal->width = width; |
| 827 | terminal->height = height; |
Kristian Høgsberg | dcfff55 | 2013-11-22 22:43:20 -0800 | [diff] [blame] | 828 | terminal_init_tabs(terminal); |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 829 | |
Callum Lowcay | a0ee21c | 2011-01-07 19:46:56 +0000 | [diff] [blame] | 830 | /* Update the window size */ |
| 831 | ws.ws_row = terminal->height; |
| 832 | ws.ws_col = terminal->width; |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 833 | widget_get_allocation(terminal->widget, &allocation); |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 834 | ws.ws_xpixel = allocation.width; |
| 835 | ws.ws_ypixel = allocation.height; |
Callum Lowcay | a0ee21c | 2011-01-07 19:46:56 +0000 | [diff] [blame] | 836 | ioctl(terminal->master, TIOCSWINSZ, &ws); |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 837 | } |
| 838 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 839 | static void |
Jasper St. Pierre | de68099 | 2014-04-10 17:23:49 -0700 | [diff] [blame] | 840 | update_title(struct terminal *terminal) |
| 841 | { |
| 842 | if (window_is_resizing(terminal->window)) { |
| 843 | char *p; |
| 844 | if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) { |
| 845 | window_set_title(terminal->window, p); |
| 846 | free(p); |
| 847 | } |
| 848 | } else { |
| 849 | window_set_title(terminal->window, terminal->title); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static void |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 854 | resize_handler(struct widget *widget, |
| 855 | int32_t width, int32_t height, void *data) |
| 856 | { |
| 857 | struct terminal *terminal = data; |
| 858 | int32_t columns, rows, m; |
Jasper St. Pierre | de68099 | 2014-04-10 17:23:49 -0700 | [diff] [blame] | 859 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 860 | m = 2 * terminal->margin; |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 861 | columns = (width - m) / (int32_t) terminal->average_width; |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 862 | rows = (height - m) / (int32_t) terminal->extents.height; |
| 863 | |
Kristian Høgsberg | b36f7ef | 2012-10-10 11:41:21 -0400 | [diff] [blame] | 864 | if (!window_is_fullscreen(terminal->window) && |
| 865 | !window_is_maximized(terminal->window)) { |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 866 | width = columns * terminal->average_width + m; |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 867 | height = rows * terminal->extents.height + m; |
| 868 | widget_set_size(terminal->widget, width, height); |
| 869 | } |
| 870 | |
| 871 | terminal_resize_cells(terminal, columns, rows); |
Jasper St. Pierre | de68099 | 2014-04-10 17:23:49 -0700 | [diff] [blame] | 872 | update_title(terminal); |
| 873 | } |
| 874 | |
| 875 | static void |
| 876 | state_changed_handler(struct window *window, void *data) |
| 877 | { |
| 878 | struct terminal *terminal = data; |
| 879 | update_title(terminal); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static void |
| 883 | terminal_resize(struct terminal *terminal, int columns, int rows) |
| 884 | { |
| 885 | int32_t width, height, m; |
| 886 | |
Kristian Høgsberg | b36f7ef | 2012-10-10 11:41:21 -0400 | [diff] [blame] | 887 | if (window_is_fullscreen(terminal->window) || |
| 888 | window_is_maximized(terminal->window)) |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 889 | return; |
| 890 | |
| 891 | m = 2 * terminal->margin; |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 892 | width = columns * terminal->average_width + m; |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 893 | height = rows * terminal->extents.height + m; |
Kristian Høgsberg | a162792 | 2012-06-20 17:30:03 -0400 | [diff] [blame] | 894 | |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 895 | window_frame_set_child_size(terminal->widget, width, height); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 896 | } |
| 897 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 898 | struct color_scheme DEFAULT_COLORS = { |
| 899 | { |
| 900 | {0, 0, 0, 1}, /* black */ |
| 901 | {0.66, 0, 0, 1}, /* red */ |
| 902 | {0 , 0.66, 0, 1}, /* green */ |
| 903 | {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */ |
| 904 | {0 , 0 , 0.66, 1}, /* blue */ |
| 905 | {0.66, 0 , 0.66, 1}, /* magenta */ |
| 906 | {0, 0.66, 0.66, 1}, /* cyan */ |
| 907 | {0.66, 0.66, 0.66, 1}, /* light grey */ |
| 908 | {0.22, 0.33, 0.33, 1}, /* dark grey */ |
| 909 | {1, 0.33, 0.33, 1}, /* high red */ |
| 910 | {0.33, 1, 0.33, 1}, /* high green */ |
| 911 | {1, 1, 0.33, 1}, /* high yellow */ |
| 912 | {0.33, 0.33, 1, 1}, /* high blue */ |
| 913 | {1, 0.33, 1, 1}, /* high magenta */ |
| 914 | {0.33, 1, 1, 1}, /* high cyan */ |
| 915 | {1, 1, 1, 1} /* white */ |
| 916 | }, |
Kristian Høgsberg | 71eca89 | 2011-01-11 10:13:00 -0500 | [diff] [blame] | 917 | 0, /* black border */ |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 918 | {7, 0, 0, } /* bg:black (0), fg:light gray (7) */ |
| 919 | }; |
Kristian Høgsberg | 12308a4 | 2009-09-28 13:08:50 -0400 | [diff] [blame] | 920 | |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 921 | static void |
Kristian Høgsberg | f106fd5 | 2011-01-11 10:11:39 -0500 | [diff] [blame] | 922 | terminal_set_color(struct terminal *terminal, cairo_t *cr, int index) |
| 923 | { |
| 924 | cairo_set_source_rgba(cr, |
| 925 | terminal->color_table[index].r, |
| 926 | terminal->color_table[index].g, |
| 927 | terminal->color_table[index].b, |
| 928 | terminal->color_table[index].a); |
| 929 | } |
| 930 | |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 931 | static void |
| 932 | terminal_send_selection(struct terminal *terminal, int fd) |
| 933 | { |
| 934 | int row, col; |
| 935 | union utf8_char *p_row; |
| 936 | union decoded_attr attr; |
| 937 | FILE *fp; |
| 938 | int len; |
| 939 | |
| 940 | fp = fdopen(fd, "w"); |
Brian Lovin | 1bf1481 | 2013-08-08 16:12:55 -0700 | [diff] [blame] | 941 | if (fp == NULL){ |
| 942 | close(fd); |
| 943 | return; |
| 944 | } |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 945 | for (row = 0; row < terminal->height; row++) { |
| 946 | p_row = terminal_get_row(terminal, row); |
| 947 | for (col = 0; col < terminal->width; col++) { |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 948 | if (p_row[col].ch == 0x200B) /* space glyph */ |
| 949 | continue; |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 950 | /* get the attributes for this character cell */ |
| 951 | terminal_decode_attr(terminal, row, col, &attr); |
| 952 | if (!attr.attr.s) |
| 953 | continue; |
| 954 | len = strnlen((char *) p_row[col].byte, 4); |
Kristian Høgsberg | 0dee647 | 2012-07-01 21:25:41 -0400 | [diff] [blame] | 955 | if (len > 0) |
| 956 | fwrite(p_row[col].byte, 1, len, fp); |
| 957 | if (len == 0 || col == terminal->width - 1) { |
| 958 | fwrite("\n", 1, 1, fp); |
| 959 | break; |
| 960 | } |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | fclose(fp); |
| 964 | } |
| 965 | |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 966 | struct glyph_run { |
| 967 | struct terminal *terminal; |
| 968 | cairo_t *cr; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 969 | unsigned int count; |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 970 | union decoded_attr attr; |
| 971 | cairo_glyph_t glyphs[256], *g; |
| 972 | }; |
| 973 | |
| 974 | static void |
| 975 | glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr) |
| 976 | { |
| 977 | run->terminal = terminal; |
| 978 | run->cr = cr; |
| 979 | run->g = run->glyphs; |
| 980 | run->count = 0; |
| 981 | run->attr.key = 0; |
| 982 | } |
| 983 | |
| 984 | static void |
| 985 | glyph_run_flush(struct glyph_run *run, union decoded_attr attr) |
| 986 | { |
| 987 | cairo_scaled_font_t *font; |
| 988 | |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 989 | if (run->count > ARRAY_LENGTH(run->glyphs) - 10 || |
| 990 | (attr.key != run->attr.key)) { |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 991 | if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK)) |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 992 | font = run->terminal->font_bold; |
| 993 | else |
| 994 | font = run->terminal->font_normal; |
| 995 | cairo_set_scaled_font(run->cr, font); |
| 996 | terminal_set_color(run->terminal, run->cr, |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 997 | run->attr.attr.fg); |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 998 | |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 999 | if (!(run->attr.attr.a & ATTRMASK_CONCEALED)) |
| 1000 | cairo_show_glyphs (run->cr, run->glyphs, run->count); |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1001 | run->g = run->glyphs; |
| 1002 | run->count = 0; |
| 1003 | } |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 1004 | run->attr = attr; |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static void |
| 1008 | glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c) |
| 1009 | { |
| 1010 | int num_glyphs; |
| 1011 | cairo_scaled_font_t *font; |
| 1012 | |
| 1013 | num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count; |
| 1014 | |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 1015 | if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK)) |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1016 | font = run->terminal->font_bold; |
| 1017 | else |
| 1018 | font = run->terminal->font_normal; |
| 1019 | |
| 1020 | cairo_move_to(run->cr, x, y); |
| 1021 | cairo_scaled_font_text_to_glyphs (font, x, y, |
| 1022 | (char *) c->byte, 4, |
| 1023 | &run->g, &num_glyphs, |
| 1024 | NULL, NULL, NULL); |
| 1025 | run->g += num_glyphs; |
| 1026 | run->count += num_glyphs; |
| 1027 | } |
| 1028 | |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 1029 | |
Kristian Høgsberg | f106fd5 | 2011-01-11 10:11:39 -0500 | [diff] [blame] | 1030 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 1031 | redraw_handler(struct widget *widget, void *data) |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1032 | { |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 1033 | struct terminal *terminal = data; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 1034 | struct rectangle allocation; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1035 | cairo_t *cr; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1036 | int top_margin, side_margin; |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 1037 | int row, col, cursor_x, cursor_y; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1038 | union utf8_char *p_row; |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1039 | union decoded_attr attr; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1040 | int text_x, text_y; |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 1041 | cairo_surface_t *surface; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 1042 | double d; |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1043 | struct glyph_run run; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 1044 | cairo_font_extents_t extents; |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1045 | double average_width; |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 1046 | double unichar_width; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1047 | |
Kristian Høgsberg | f39a9cc | 2011-01-20 12:37:33 -0500 | [diff] [blame] | 1048 | surface = window_get_surface(terminal->window); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 1049 | widget_get_allocation(terminal->widget, &allocation); |
Alexander Larsson | de79dd0 | 2013-05-22 14:41:34 +0200 | [diff] [blame] | 1050 | cr = widget_cairo_create(terminal->widget); |
Kristian Høgsberg | f39a9cc | 2011-01-20 12:37:33 -0500 | [diff] [blame] | 1051 | cairo_rectangle(cr, allocation.x, allocation.y, |
| 1052 | allocation.width, allocation.height); |
| 1053 | cairo_clip(cr); |
| 1054 | cairo_push_group(cr); |
| 1055 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1056 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Kristian Høgsberg | 71eca89 | 2011-01-11 10:13:00 -0500 | [diff] [blame] | 1057 | terminal_set_color(terminal, cr, terminal->color_scheme->border); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1058 | cairo_paint(cr); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1059 | |
Kristian Høgsberg | 7ae6b1a | 2010-12-25 16:58:31 -0500 | [diff] [blame] | 1060 | cairo_set_scaled_font(cr, terminal->font_normal); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1061 | |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 1062 | extents = terminal->extents; |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1063 | average_width = terminal->average_width; |
| 1064 | side_margin = (allocation.width - terminal->width * average_width) / 2; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 1065 | top_margin = (allocation.height - terminal->height * extents.height) / 2; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 1066 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1067 | cairo_set_line_width(cr, 1.0); |
Kristian Høgsberg | f39a9cc | 2011-01-20 12:37:33 -0500 | [diff] [blame] | 1068 | cairo_translate(cr, allocation.x + side_margin, |
| 1069 | allocation.y + top_margin); |
Kristian Høgsberg | 8c25420 | 2010-12-25 08:58:46 -0500 | [diff] [blame] | 1070 | /* paint the background */ |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1071 | for (row = 0; row < terminal->height; row++) { |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 1072 | p_row = terminal_get_row(terminal, row); |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1073 | for (col = 0; col < terminal->width; col++) { |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1074 | /* get the attributes for this character cell */ |
Kristian Høgsberg | 01994a5 | 2011-01-11 10:26:04 -0500 | [diff] [blame] | 1075 | terminal_decode_attr(terminal, row, col, &attr); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1076 | |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 1077 | if (attr.attr.bg == terminal->color_scheme->border) |
Kristian Høgsberg | fb266a3 | 2011-01-11 10:15:21 -0500 | [diff] [blame] | 1078 | continue; |
| 1079 | |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 1080 | if (is_wide(p_row[col])) |
| 1081 | unichar_width = 2 * average_width; |
| 1082 | else |
| 1083 | unichar_width = average_width; |
| 1084 | |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 1085 | terminal_set_color(terminal, cr, attr.attr.bg); |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1086 | cairo_move_to(cr, col * average_width, |
Kristian Høgsberg | f39a9cc | 2011-01-20 12:37:33 -0500 | [diff] [blame] | 1087 | row * extents.height); |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 1088 | cairo_rel_line_to(cr, unichar_width, 0); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1089 | cairo_rel_line_to(cr, 0, extents.height); |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 1090 | cairo_rel_line_to(cr, -unichar_width, 0); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1091 | cairo_close_path(cr); |
| 1092 | cairo_fill(cr); |
Kristian Høgsberg | 8c25420 | 2010-12-25 08:58:46 -0500 | [diff] [blame] | 1093 | } |
| 1094 | } |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1095 | |
Kristian Høgsberg | 8c25420 | 2010-12-25 08:58:46 -0500 | [diff] [blame] | 1096 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 1097 | |
| 1098 | /* paint the foreground */ |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1099 | glyph_run_init(&run, terminal, cr); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 1100 | for (row = 0; row < terminal->height; row++) { |
| 1101 | p_row = terminal_get_row(terminal, row); |
| 1102 | for (col = 0; col < terminal->width; col++) { |
| 1103 | /* get the attributes for this character cell */ |
Kristian Høgsberg | 01994a5 | 2011-01-11 10:26:04 -0500 | [diff] [blame] | 1104 | terminal_decode_attr(terminal, row, col, &attr); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1105 | |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1106 | glyph_run_flush(&run, attr); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1107 | |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1108 | text_x = col * average_width; |
Kristian Høgsberg | f39a9cc | 2011-01-20 12:37:33 -0500 | [diff] [blame] | 1109 | text_y = extents.ascent + row * extents.height; |
Callum Lowcay | 9d708b0 | 2011-01-12 20:06:17 +1300 | [diff] [blame] | 1110 | if (attr.attr.a & ATTRMASK_UNDERLINE) { |
| 1111 | terminal_set_color(terminal, cr, attr.attr.fg); |
Callum Lowcay | 86653ed | 2011-01-07 19:47:03 +0000 | [diff] [blame] | 1112 | cairo_move_to(cr, text_x, (double)text_y + 1.5); |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1113 | cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1114 | cairo_stroke(cr); |
| 1115 | } |
Kristian Høgsberg | 4f50670 | 2010-12-25 16:14:23 -0500 | [diff] [blame] | 1116 | |
Daiki Ueno | 56d8a7a | 2014-04-08 18:46:18 +0900 | [diff] [blame] | 1117 | /* skip space glyph (RLE) we use as a placeholder of |
| 1118 | the right half of a double-width character, |
| 1119 | because RLE is not available in every font. */ |
| 1120 | if (p_row[col].ch == 0x200B) |
| 1121 | continue; |
| 1122 | |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1123 | glyph_run_add(&run, text_x, text_y, &p_row[col]); |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1124 | } |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1125 | } |
Kristian Høgsberg | b0b82e2 | 2009-02-21 15:42:25 -0500 | [diff] [blame] | 1126 | |
Kristian Høgsberg | 1d3e939 | 2011-01-11 11:06:49 -0500 | [diff] [blame] | 1127 | attr.key = ~0; |
| 1128 | glyph_run_flush(&run, attr); |
| 1129 | |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 1130 | if ((terminal->mode & MODE_SHOW_CURSOR) && |
| 1131 | !window_has_focus(terminal->window)) { |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1132 | d = 0.5; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 1133 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1134 | cairo_set_line_width(cr, 1); |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1135 | cairo_move_to(cr, terminal->column * average_width + d, |
Kristian Høgsberg | f39a9cc | 2011-01-20 12:37:33 -0500 | [diff] [blame] | 1136 | terminal->row * extents.height + d); |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1137 | cairo_rel_line_to(cr, average_width - 2 * d, 0); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1138 | cairo_rel_line_to(cr, 0, extents.height - 2 * d); |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1139 | cairo_rel_line_to(cr, -average_width + 2 * d, 0); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1140 | cairo_close_path(cr); |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 1141 | |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 1142 | cairo_stroke(cr); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1143 | } |
Kristian Høgsberg | b0b82e2 | 2009-02-21 15:42:25 -0500 | [diff] [blame] | 1144 | |
Kristian Høgsberg | f39a9cc | 2011-01-20 12:37:33 -0500 | [diff] [blame] | 1145 | cairo_pop_group_to_source(cr); |
| 1146 | cairo_paint(cr); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1147 | cairo_destroy(cr); |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 1148 | cairo_surface_destroy(surface); |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 1149 | |
| 1150 | if (terminal->send_cursor_position) { |
| 1151 | cursor_x = side_margin + allocation.x + |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 1152 | terminal->column * average_width; |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 1153 | cursor_y = top_margin + allocation.y + |
| 1154 | terminal->row * extents.height; |
| 1155 | window_set_text_cursor_position(terminal->window, |
| 1156 | cursor_x, cursor_y); |
| 1157 | terminal->send_cursor_position = 0; |
| 1158 | } |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | static void |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1162 | terminal_write(struct terminal *terminal, const char *data, size_t length) |
| 1163 | { |
| 1164 | if (write(terminal->master, data, length) < 0) |
| 1165 | abort(); |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 1166 | terminal->send_cursor_position = 1; |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static void |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 1170 | terminal_data(struct terminal *terminal, const char *data, size_t length); |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 1171 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 1172 | static void |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1173 | handle_char(struct terminal *terminal, union utf8_char utf8); |
| 1174 | |
| 1175 | static void |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1176 | handle_sgr(struct terminal *terminal, int code); |
| 1177 | |
| 1178 | static void |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1179 | handle_term_parameter(struct terminal *terminal, int code, int sr) |
| 1180 | { |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1181 | int i; |
| 1182 | |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 1183 | if (terminal->escape_flags & ESC_FLAG_WHAT) { |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1184 | switch(code) { |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 1185 | case 1: /* DECCKM */ |
| 1186 | if (sr) terminal->key_mode = KM_APPLICATION; |
| 1187 | else terminal->key_mode = KM_NORMAL; |
| 1188 | break; |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 1189 | case 2: /* DECANM */ |
| 1190 | /* No VT52 support yet */ |
| 1191 | terminal->g0 = CS_US; |
| 1192 | terminal->g1 = CS_US; |
| 1193 | terminal->cs = terminal->g0; |
| 1194 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1195 | case 3: /* DECCOLM */ |
| 1196 | if (sr) |
| 1197 | terminal_resize(terminal, 132, 24); |
| 1198 | else |
| 1199 | terminal_resize(terminal, 80, 24); |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1200 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1201 | /* set columns, but also home cursor and clear screen */ |
| 1202 | terminal->row = 0; terminal->column = 0; |
| 1203 | for (i = 0; i < terminal->height; i++) { |
| 1204 | memset(terminal_get_row(terminal, i), |
| 1205 | 0, terminal->data_pitch); |
| 1206 | attr_init(terminal_get_attr_row(terminal, i), |
| 1207 | terminal->curr_attr, terminal->width); |
| 1208 | } |
| 1209 | break; |
| 1210 | case 5: /* DECSCNM */ |
| 1211 | if (sr) terminal->mode |= MODE_INVERSE; |
| 1212 | else terminal->mode &= ~MODE_INVERSE; |
| 1213 | break; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1214 | case 6: /* DECOM */ |
| 1215 | terminal->origin_mode = sr; |
| 1216 | if (terminal->origin_mode) |
| 1217 | terminal->row = terminal->margin_top; |
| 1218 | else |
| 1219 | terminal->row = 0; |
| 1220 | terminal->column = 0; |
| 1221 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1222 | case 7: /* DECAWM */ |
| 1223 | if (sr) terminal->mode |= MODE_AUTOWRAP; |
| 1224 | else terminal->mode &= ~MODE_AUTOWRAP; |
| 1225 | break; |
| 1226 | case 8: /* DECARM */ |
| 1227 | if (sr) terminal->mode |= MODE_AUTOREPEAT; |
| 1228 | else terminal->mode &= ~MODE_AUTOREPEAT; |
| 1229 | break; |
Kristian Høgsberg | e828e90 | 2012-06-20 16:59:17 -0400 | [diff] [blame] | 1230 | case 12: /* Very visible cursor (CVVIS) */ |
| 1231 | /* FIXME: What do we do here. */ |
| 1232 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1233 | case 25: |
| 1234 | if (sr) terminal->mode |= MODE_SHOW_CURSOR; |
| 1235 | else terminal->mode &= ~MODE_SHOW_CURSOR; |
| 1236 | break; |
Kristian Høgsberg | e828e90 | 2012-06-20 16:59:17 -0400 | [diff] [blame] | 1237 | case 1034: /* smm/rmm, meta mode on/off */ |
| 1238 | /* ignore */ |
| 1239 | break; |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 1240 | case 1037: /* deleteSendsDel */ |
| 1241 | if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL; |
| 1242 | else terminal->mode &= ~MODE_DELETE_SENDS_DEL; |
| 1243 | break; |
| 1244 | case 1039: /* altSendsEscape */ |
| 1245 | if (sr) terminal->mode |= MODE_ALT_SENDS_ESC; |
| 1246 | else terminal->mode &= ~MODE_ALT_SENDS_ESC; |
| 1247 | break; |
Kristian Høgsberg | e828e90 | 2012-06-20 16:59:17 -0400 | [diff] [blame] | 1248 | case 1049: /* rmcup/smcup, alternate screen */ |
| 1249 | /* Ignore. Should be possible to implement, |
| 1250 | * but it's kind of annoying. */ |
| 1251 | break; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1252 | default: |
| 1253 | fprintf(stderr, "Unknown parameter: ?%d\n", code); |
| 1254 | break; |
| 1255 | } |
| 1256 | } else { |
| 1257 | switch(code) { |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 1258 | case 4: /* IRM */ |
| 1259 | if (sr) terminal->mode |= MODE_IRM; |
| 1260 | else terminal->mode &= ~MODE_IRM; |
| 1261 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1262 | case 20: /* LNM */ |
| 1263 | if (sr) terminal->mode |= MODE_LF_NEWLINE; |
| 1264 | else terminal->mode &= ~MODE_LF_NEWLINE; |
| 1265 | break; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1266 | default: |
| 1267 | fprintf(stderr, "Unknown parameter: %d\n", code); |
| 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | static void |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 1274 | handle_dcs(struct terminal *terminal) |
| 1275 | { |
| 1276 | } |
| 1277 | |
| 1278 | static void |
| 1279 | handle_osc(struct terminal *terminal) |
| 1280 | { |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1281 | char *p; |
| 1282 | int code; |
| 1283 | |
| 1284 | terminal->escape[terminal->escape_length++] = '\0'; |
| 1285 | p = &terminal->escape[2]; |
| 1286 | code = strtol(p, &p, 10); |
| 1287 | if (*p == ';') p++; |
| 1288 | |
| 1289 | switch (code) { |
| 1290 | case 0: /* Icon name and window title */ |
| 1291 | case 1: /* Icon label */ |
| 1292 | case 2: /* Window title*/ |
Kristian Høgsberg | a83be20 | 2013-10-23 20:47:35 -0700 | [diff] [blame] | 1293 | free(terminal->title); |
| 1294 | terminal->title = strdup(p); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1295 | window_set_title(terminal->window, p); |
| 1296 | break; |
Kristian Høgsberg | 8a20500 | 2013-09-11 11:52:56 -0700 | [diff] [blame] | 1297 | case 7: /* shell cwd as uri */ |
| 1298 | break; |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1299 | default: |
Kristian Høgsberg | 8a20500 | 2013-09-11 11:52:56 -0700 | [diff] [blame] | 1300 | fprintf(stderr, "Unknown OSC escape code %d, text %s\n", |
| 1301 | code, p); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1302 | break; |
| 1303 | } |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | static void |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 1307 | handle_escape(struct terminal *terminal) |
| 1308 | { |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1309 | union utf8_char *row; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1310 | struct attr *attr_row; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 1311 | char *p; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1312 | int i, count, x, y, top, bottom; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1313 | int args[10], set[10] = { 0, }; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1314 | char response[MAX_RESPONSE] = {0, }; |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1315 | struct rectangle allocation; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 1316 | |
| 1317 | terminal->escape[terminal->escape_length++] = '\0'; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1318 | i = 0; |
| 1319 | p = &terminal->escape[2]; |
| 1320 | while ((isdigit(*p) || *p == ';') && i < 10) { |
| 1321 | if (*p == ';') { |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1322 | if (!set[i]) { |
| 1323 | args[i] = 0; |
| 1324 | set[i] = 1; |
| 1325 | } |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1326 | p++; |
| 1327 | i++; |
| 1328 | } else { |
| 1329 | args[i] = strtol(p, &p, 10); |
| 1330 | set[i] = 1; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 1331 | } |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 1332 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1333 | |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1334 | switch (*p) { |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 1335 | case '@': /* ICH */ |
| 1336 | count = set[0] ? args[0] : 1; |
| 1337 | if (count == 0) count = 1; |
| 1338 | terminal_shift_line(terminal, count); |
| 1339 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1340 | case 'A': /* CUU */ |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1341 | count = set[0] ? args[0] : 1; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1342 | if (count == 0) count = 1; |
| 1343 | if (terminal->row - count >= terminal->margin_top) |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1344 | terminal->row -= count; |
| 1345 | else |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1346 | terminal->row = terminal->margin_top; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1347 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1348 | case 'B': /* CUD */ |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1349 | count = set[0] ? args[0] : 1; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1350 | if (count == 0) count = 1; |
| 1351 | if (terminal->row + count <= terminal->margin_bottom) |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1352 | terminal->row += count; |
| 1353 | else |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1354 | terminal->row = terminal->margin_bottom; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1355 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1356 | case 'C': /* CUF */ |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1357 | count = set[0] ? args[0] : 1; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1358 | if (count == 0) count = 1; |
| 1359 | if ((terminal->column + count) < terminal->width) |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1360 | terminal->column += count; |
| 1361 | else |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1362 | terminal->column = terminal->width - 1; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1363 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1364 | case 'D': /* CUB */ |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1365 | count = set[0] ? args[0] : 1; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1366 | if (count == 0) count = 1; |
| 1367 | if ((terminal->column - count) >= 0) |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1368 | terminal->column -= count; |
| 1369 | else |
| 1370 | terminal->column = 0; |
| 1371 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1372 | case 'E': /* CNL */ |
| 1373 | count = set[0] ? args[0] : 1; |
| 1374 | if (terminal->row + count <= terminal->margin_bottom) |
| 1375 | terminal->row += count; |
| 1376 | else |
| 1377 | terminal->row = terminal->margin_bottom; |
| 1378 | terminal->column = 0; |
| 1379 | break; |
| 1380 | case 'F': /* CPL */ |
| 1381 | count = set[0] ? args[0] : 1; |
| 1382 | if (terminal->row - count >= terminal->margin_top) |
| 1383 | terminal->row -= count; |
| 1384 | else |
| 1385 | terminal->row = terminal->margin_top; |
| 1386 | terminal->column = 0; |
| 1387 | break; |
| 1388 | case 'G': /* CHA */ |
| 1389 | y = set[0] ? args[0] : 1; |
| 1390 | y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1391 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1392 | terminal->column = y - 1; |
| 1393 | break; |
| 1394 | case 'f': /* HVP */ |
| 1395 | case 'H': /* CUP */ |
| 1396 | x = (set[1] ? args[1] : 1) - 1; |
| 1397 | x = x < 0 ? 0 : |
| 1398 | (x >= terminal->width ? terminal->width - 1 : x); |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1399 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1400 | y = (set[0] ? args[0] : 1) - 1; |
| 1401 | if (terminal->origin_mode) { |
| 1402 | y += terminal->margin_top; |
| 1403 | y = y < terminal->margin_top ? terminal->margin_top : |
| 1404 | (y > terminal->margin_bottom ? terminal->margin_bottom : y); |
| 1405 | } else { |
| 1406 | y = y < 0 ? 0 : |
| 1407 | (y >= terminal->height ? terminal->height - 1 : y); |
| 1408 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1409 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1410 | terminal->row = y; |
| 1411 | terminal->column = x; |
| 1412 | break; |
| 1413 | case 'I': /* CHT */ |
| 1414 | count = set[0] ? args[0] : 1; |
| 1415 | if (count == 0) count = 1; |
| 1416 | while (count > 0 && terminal->column < terminal->width) { |
| 1417 | if (terminal->tab_ruler[terminal->column]) count--; |
| 1418 | terminal->column++; |
| 1419 | } |
| 1420 | terminal->column--; |
| 1421 | break; |
| 1422 | case 'J': /* ED */ |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1423 | row = terminal_get_row(terminal, terminal->row); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1424 | attr_row = terminal_get_attr_row(terminal, terminal->row); |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1425 | if (!set[0] || args[0] == 0 || args[0] > 2) { |
| 1426 | memset(&row[terminal->column], |
| 1427 | 0, (terminal->width - terminal->column) * sizeof(union utf8_char)); |
| 1428 | attr_init(&attr_row[terminal->column], |
| 1429 | terminal->curr_attr, terminal->width - terminal->column); |
| 1430 | for (i = terminal->row + 1; i < terminal->height; i++) { |
| 1431 | memset(terminal_get_row(terminal, i), |
| 1432 | 0, terminal->data_pitch); |
| 1433 | attr_init(terminal_get_attr_row(terminal, i), |
| 1434 | terminal->curr_attr, terminal->width); |
| 1435 | } |
| 1436 | } else if (args[0] == 1) { |
| 1437 | memset(row, 0, (terminal->column+1) * sizeof(union utf8_char)); |
| 1438 | attr_init(attr_row, terminal->curr_attr, terminal->column+1); |
| 1439 | for (i = 0; i < terminal->row; i++) { |
| 1440 | memset(terminal_get_row(terminal, i), |
| 1441 | 0, terminal->data_pitch); |
| 1442 | attr_init(terminal_get_attr_row(terminal, i), |
| 1443 | terminal->curr_attr, terminal->width); |
| 1444 | } |
| 1445 | } else if (args[0] == 2) { |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 1446 | /* Clear screen by scrolling contents out */ |
| 1447 | terminal_scroll_buffer(terminal, |
| 1448 | terminal->end - terminal->start); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1449 | } |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1450 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1451 | case 'K': /* EL */ |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1452 | row = terminal_get_row(terminal, terminal->row); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1453 | attr_row = terminal_get_attr_row(terminal, terminal->row); |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1454 | if (!set[0] || args[0] == 0 || args[0] > 2) { |
| 1455 | memset(&row[terminal->column], 0, |
| 1456 | (terminal->width - terminal->column) * sizeof(union utf8_char)); |
| 1457 | attr_init(&attr_row[terminal->column], terminal->curr_attr, |
| 1458 | terminal->width - terminal->column); |
| 1459 | } else if (args[0] == 1) { |
| 1460 | memset(row, 0, (terminal->column+1) * sizeof(union utf8_char)); |
| 1461 | attr_init(attr_row, terminal->curr_attr, terminal->column+1); |
| 1462 | } else if (args[0] == 2) { |
| 1463 | memset(row, 0, terminal->data_pitch); |
| 1464 | attr_init(attr_row, terminal->curr_attr, terminal->width); |
| 1465 | } |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1466 | break; |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 1467 | case 'L': /* IL */ |
| 1468 | count = set[0] ? args[0] : 1; |
| 1469 | if (count == 0) count = 1; |
| 1470 | if (terminal->row >= terminal->margin_top && |
| 1471 | terminal->row < terminal->margin_bottom) |
| 1472 | { |
| 1473 | top = terminal->margin_top; |
| 1474 | terminal->margin_top = terminal->row; |
| 1475 | terminal_scroll(terminal, 0 - count); |
| 1476 | terminal->margin_top = top; |
| 1477 | } else if (terminal->row == terminal->margin_bottom) { |
| 1478 | memset(terminal_get_row(terminal, terminal->row), |
| 1479 | 0, terminal->data_pitch); |
| 1480 | attr_init(terminal_get_attr_row(terminal, terminal->row), |
| 1481 | terminal->curr_attr, terminal->width); |
| 1482 | } |
| 1483 | break; |
| 1484 | case 'M': /* DL */ |
| 1485 | count = set[0] ? args[0] : 1; |
| 1486 | if (count == 0) count = 1; |
| 1487 | if (terminal->row >= terminal->margin_top && |
| 1488 | terminal->row < terminal->margin_bottom) |
| 1489 | { |
| 1490 | top = terminal->margin_top; |
| 1491 | terminal->margin_top = terminal->row; |
| 1492 | terminal_scroll(terminal, count); |
| 1493 | terminal->margin_top = top; |
| 1494 | } else if (terminal->row == terminal->margin_bottom) { |
| 1495 | memset(terminal_get_row(terminal, terminal->row), |
| 1496 | 0, terminal->data_pitch); |
| 1497 | } |
| 1498 | break; |
| 1499 | case 'P': /* DCH */ |
| 1500 | count = set[0] ? args[0] : 1; |
| 1501 | if (count == 0) count = 1; |
| 1502 | terminal_shift_line(terminal, 0 - count); |
| 1503 | break; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1504 | case 'S': /* SU */ |
| 1505 | terminal_scroll(terminal, set[0] ? args[0] : 1); |
| 1506 | break; |
| 1507 | case 'T': /* SD */ |
| 1508 | terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1)); |
| 1509 | break; |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 1510 | case 'X': /* ECH */ |
| 1511 | count = set[0] ? args[0] : 1; |
| 1512 | if (count == 0) count = 1; |
| 1513 | if ((terminal->column + count) > terminal->width) |
| 1514 | count = terminal->width - terminal->column; |
| 1515 | row = terminal_get_row(terminal, terminal->row); |
| 1516 | attr_row = terminal_get_attr_row(terminal, terminal->row); |
| 1517 | memset(&row[terminal->column], 0, count * sizeof(union utf8_char)); |
| 1518 | attr_init(&attr_row[terminal->column], terminal->curr_attr, count); |
| 1519 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1520 | case 'Z': /* CBT */ |
| 1521 | count = set[0] ? args[0] : 1; |
| 1522 | if (count == 0) count = 1; |
| 1523 | while (count > 0 && terminal->column >= 0) { |
| 1524 | if (terminal->tab_ruler[terminal->column]) count--; |
| 1525 | terminal->column--; |
| 1526 | } |
| 1527 | terminal->column++; |
| 1528 | break; |
| 1529 | case '`': /* HPA */ |
| 1530 | y = set[0] ? args[0] : 1; |
| 1531 | y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1532 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1533 | terminal->column = y - 1; |
| 1534 | break; |
| 1535 | case 'b': /* REP */ |
| 1536 | count = set[0] ? args[0] : 1; |
| 1537 | if (count == 0) count = 1; |
| 1538 | if (terminal->last_char.byte[0]) |
| 1539 | for (i = 0; i < count; i++) |
| 1540 | handle_char(terminal, terminal->last_char); |
| 1541 | terminal->last_char.byte[0] = 0; |
| 1542 | break; |
| 1543 | case 'c': /* Primary DA */ |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1544 | terminal_write(terminal, "\e[?6c", 5); |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1545 | break; |
| 1546 | case 'd': /* VPA */ |
| 1547 | x = set[0] ? args[0] : 1; |
| 1548 | x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1549 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1550 | terminal->row = x - 1; |
| 1551 | break; |
| 1552 | case 'g': /* TBC */ |
| 1553 | if (!set[0] || args[0] == 0) { |
| 1554 | terminal->tab_ruler[terminal->column] = 0; |
| 1555 | } else if (args[0] == 3) { |
| 1556 | memset(terminal->tab_ruler, 0, terminal->width); |
| 1557 | } |
| 1558 | break; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1559 | case 'h': /* SM */ |
| 1560 | for(i = 0; i < 10 && set[i]; i++) { |
| 1561 | handle_term_parameter(terminal, args[i], 1); |
| 1562 | } |
| 1563 | break; |
| 1564 | case 'l': /* RM */ |
| 1565 | for(i = 0; i < 10 && set[i]; i++) { |
| 1566 | handle_term_parameter(terminal, args[i], 0); |
| 1567 | } |
| 1568 | break; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1569 | case 'm': /* SGR */ |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1570 | for(i = 0; i < 10; i++) { |
Callum Lowcay | 81179db | 2011-01-10 12:14:01 +1300 | [diff] [blame] | 1571 | if (i <= 7 && set[i] && set[i + 1] && |
| 1572 | set[i + 2] && args[i + 1] == 5) |
| 1573 | { |
| 1574 | if (args[i] == 38) { |
| 1575 | handle_sgr(terminal, args[i + 2] + 256); |
| 1576 | break; |
| 1577 | } else if (args[i] == 48) { |
| 1578 | handle_sgr(terminal, args[i + 2] + 512); |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1582 | if(set[i]) { |
| 1583 | handle_sgr(terminal, args[i]); |
| 1584 | } else if(i == 0) { |
| 1585 | handle_sgr(terminal, 0); |
| 1586 | break; |
| 1587 | } else { |
| 1588 | break; |
| 1589 | } |
| 1590 | } |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1591 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1592 | case 'n': /* DSR */ |
| 1593 | i = set[0] ? args[0] : 0; |
| 1594 | if (i == 0 || i == 5) { |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1595 | terminal_write(terminal, "\e[0n", 4); |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1596 | } else if (i == 6) { |
| 1597 | snprintf(response, MAX_RESPONSE, "\e[%d;%dR", |
| 1598 | terminal->origin_mode ? |
| 1599 | terminal->row+terminal->margin_top : terminal->row+1, |
| 1600 | terminal->column+1); |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1601 | terminal_write(terminal, response, strlen(response)); |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1602 | } |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1603 | break; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1604 | case 'r': |
| 1605 | if(!set[0]) { |
| 1606 | terminal->margin_top = 0; |
| 1607 | terminal->margin_bottom = terminal->height-1; |
| 1608 | terminal->row = 0; |
| 1609 | terminal->column = 0; |
| 1610 | } else { |
| 1611 | top = (set[0] ? args[0] : 1) - 1; |
| 1612 | top = top < 0 ? 0 : |
| 1613 | (top >= terminal->height ? terminal->height - 1 : top); |
| 1614 | bottom = (set[1] ? args[1] : 1) - 1; |
| 1615 | bottom = bottom < 0 ? 0 : |
| 1616 | (bottom >= terminal->height ? terminal->height - 1 : bottom); |
| 1617 | if(bottom > top) { |
| 1618 | terminal->margin_top = top; |
| 1619 | terminal->margin_bottom = bottom; |
| 1620 | } else { |
| 1621 | terminal->margin_top = 0; |
| 1622 | terminal->margin_bottom = terminal->height-1; |
| 1623 | } |
| 1624 | if(terminal->origin_mode) |
| 1625 | terminal->row = terminal->margin_top; |
| 1626 | else |
| 1627 | terminal->row = 0; |
| 1628 | terminal->column = 0; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1629 | } |
| 1630 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1631 | case 's': |
| 1632 | terminal->saved_row = terminal->row; |
| 1633 | terminal->saved_column = terminal->column; |
| 1634 | break; |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1635 | case 't': /* windowOps */ |
| 1636 | if (!set[0]) break; |
| 1637 | switch (args[0]) { |
| 1638 | case 4: /* resize px */ |
| 1639 | if (set[1] && set[2]) { |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 1640 | widget_schedule_resize(terminal->widget, |
| 1641 | args[2], args[1]); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1642 | } |
| 1643 | break; |
| 1644 | case 8: /* resize ch */ |
| 1645 | if (set[1] && set[2]) { |
| 1646 | terminal_resize(terminal, args[2], args[1]); |
| 1647 | } |
| 1648 | break; |
| 1649 | case 13: /* report position */ |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 1650 | widget_get_allocation(terminal->widget, &allocation); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1651 | snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt", |
| 1652 | allocation.x, allocation.y); |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1653 | terminal_write(terminal, response, strlen(response)); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1654 | break; |
| 1655 | case 14: /* report px */ |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 1656 | widget_get_allocation(terminal->widget, &allocation); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1657 | snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt", |
| 1658 | allocation.height, allocation.width); |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1659 | terminal_write(terminal, response, strlen(response)); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1660 | break; |
| 1661 | case 18: /* report ch */ |
| 1662 | snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt", |
| 1663 | terminal->height, terminal->width); |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1664 | terminal_write(terminal, response, strlen(response)); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1665 | break; |
| 1666 | case 21: /* report title */ |
| 1667 | snprintf(response, MAX_RESPONSE, "\e]l%s\e\\", |
| 1668 | window_get_title(terminal->window)); |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 1669 | terminal_write(terminal, response, strlen(response)); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 1670 | break; |
| 1671 | default: |
| 1672 | if (args[0] >= 24) |
| 1673 | terminal_resize(terminal, terminal->width, args[0]); |
| 1674 | else |
| 1675 | fprintf(stderr, "Unimplemented windowOp %d\n", args[0]); |
| 1676 | break; |
| 1677 | } |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1678 | case 'u': |
| 1679 | terminal->row = terminal->saved_row; |
| 1680 | terminal->column = terminal->saved_column; |
| 1681 | break; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1682 | default: |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1683 | fprintf(stderr, "Unknown CSI escape: %c\n", *p); |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 1684 | break; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1685 | } |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | static void |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1689 | handle_non_csi_escape(struct terminal *terminal, char code) |
| 1690 | { |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1691 | switch(code) { |
| 1692 | case 'M': /* RI */ |
| 1693 | terminal->row -= 1; |
| 1694 | if(terminal->row < terminal->margin_top) { |
| 1695 | terminal->row = terminal->margin_top; |
| 1696 | terminal_scroll(terminal, -1); |
| 1697 | } |
| 1698 | break; |
| 1699 | case 'E': /* NEL */ |
| 1700 | terminal->column = 0; |
| 1701 | // fallthrough |
| 1702 | case 'D': /* IND */ |
| 1703 | terminal->row += 1; |
| 1704 | if(terminal->row > terminal->margin_bottom) { |
| 1705 | terminal->row = terminal->margin_bottom; |
| 1706 | terminal_scroll(terminal, +1); |
| 1707 | } |
| 1708 | break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1709 | case 'c': /* RIS */ |
| 1710 | terminal_init(terminal); |
| 1711 | break; |
| 1712 | case 'H': /* HTS */ |
| 1713 | terminal->tab_ruler[terminal->column] = 1; |
| 1714 | break; |
| 1715 | case '7': /* DECSC */ |
| 1716 | terminal->saved_row = terminal->row; |
| 1717 | terminal->saved_column = terminal->column; |
| 1718 | terminal->saved_attr = terminal->curr_attr; |
| 1719 | terminal->saved_origin_mode = terminal->origin_mode; |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 1720 | terminal->saved_cs = terminal->cs; |
| 1721 | terminal->saved_g0 = terminal->g0; |
| 1722 | terminal->saved_g1 = terminal->g1; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1723 | break; |
| 1724 | case '8': /* DECRC */ |
| 1725 | terminal->row = terminal->saved_row; |
| 1726 | terminal->column = terminal->saved_column; |
| 1727 | terminal->curr_attr = terminal->saved_attr; |
| 1728 | terminal->origin_mode = terminal->saved_origin_mode; |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 1729 | terminal->cs = terminal->saved_cs; |
| 1730 | terminal->g0 = terminal->saved_g0; |
| 1731 | terminal->g1 = terminal->saved_g1; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1732 | break; |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 1733 | case '=': /* DECPAM */ |
| 1734 | terminal->key_mode = KM_APPLICATION; |
| 1735 | break; |
| 1736 | case '>': /* DECPNM */ |
| 1737 | terminal->key_mode = KM_NORMAL; |
| 1738 | break; |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1739 | default: |
| 1740 | fprintf(stderr, "Unknown escape code: %c\n", code); |
| 1741 | break; |
| 1742 | } |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | static void |
| 1746 | handle_special_escape(struct terminal *terminal, char special, char code) |
| 1747 | { |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1748 | int i, numChars; |
| 1749 | |
| 1750 | if (special == '#') { |
| 1751 | switch(code) { |
| 1752 | case '8': |
| 1753 | /* fill with 'E', no cheap way to do this */ |
| 1754 | memset(terminal->data, 0, terminal->data_pitch * terminal->height); |
| 1755 | numChars = terminal->width * terminal->height; |
| 1756 | for(i = 0; i < numChars; i++) { |
| 1757 | terminal->data[i].byte[0] = 'E'; |
| 1758 | } |
| 1759 | break; |
| 1760 | default: |
| 1761 | fprintf(stderr, "Unknown HASH escape #%c\n", code); |
| 1762 | break; |
| 1763 | } |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 1764 | } else if (special == '(' || special == ')') { |
| 1765 | switch(code) { |
| 1766 | case '0': |
| 1767 | if (special == '(') |
| 1768 | terminal->g0 = CS_SPECIAL; |
| 1769 | else |
| 1770 | terminal->g1 = CS_SPECIAL; |
| 1771 | break; |
| 1772 | case 'A': |
| 1773 | if (special == '(') |
| 1774 | terminal->g0 = CS_UK; |
| 1775 | else |
| 1776 | terminal->g1 = CS_UK; |
| 1777 | break; |
| 1778 | case 'B': |
| 1779 | if (special == '(') |
| 1780 | terminal->g0 = CS_US; |
| 1781 | else |
| 1782 | terminal->g1 = CS_US; |
| 1783 | break; |
| 1784 | default: |
| 1785 | fprintf(stderr, "Unknown character set %c\n", code); |
| 1786 | break; |
| 1787 | } |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1788 | } else { |
| 1789 | fprintf(stderr, "Unknown special escape %c%c\n", special, code); |
| 1790 | } |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | static void |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1794 | handle_sgr(struct terminal *terminal, int code) |
| 1795 | { |
| 1796 | switch(code) { |
| 1797 | case 0: |
| 1798 | terminal->curr_attr = terminal->color_scheme->default_attr; |
| 1799 | break; |
| 1800 | case 1: |
| 1801 | terminal->curr_attr.a |= ATTRMASK_BOLD; |
| 1802 | if (terminal->curr_attr.fg < 8) |
| 1803 | terminal->curr_attr.fg += 8; |
| 1804 | break; |
| 1805 | case 4: |
| 1806 | terminal->curr_attr.a |= ATTRMASK_UNDERLINE; |
| 1807 | break; |
| 1808 | case 5: |
| 1809 | terminal->curr_attr.a |= ATTRMASK_BLINK; |
| 1810 | break; |
Callum Lowcay | 81179db | 2011-01-10 12:14:01 +1300 | [diff] [blame] | 1811 | case 8: |
| 1812 | terminal->curr_attr.a |= ATTRMASK_CONCEALED; |
| 1813 | break; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1814 | case 2: |
| 1815 | case 21: |
| 1816 | case 22: |
| 1817 | terminal->curr_attr.a &= ~ATTRMASK_BOLD; |
| 1818 | if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8) |
| 1819 | terminal->curr_attr.fg -= 8; |
| 1820 | break; |
| 1821 | case 24: |
| 1822 | terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE; |
| 1823 | break; |
| 1824 | case 25: |
| 1825 | terminal->curr_attr.a &= ~ATTRMASK_BLINK; |
| 1826 | break; |
| 1827 | case 7: |
| 1828 | case 26: |
| 1829 | terminal->curr_attr.a |= ATTRMASK_INVERSE; |
| 1830 | break; |
| 1831 | case 27: |
| 1832 | terminal->curr_attr.a &= ~ATTRMASK_INVERSE; |
| 1833 | break; |
Callum Lowcay | 81179db | 2011-01-10 12:14:01 +1300 | [diff] [blame] | 1834 | case 28: |
| 1835 | terminal->curr_attr.a &= ~ATTRMASK_CONCEALED; |
| 1836 | break; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1837 | case 39: |
| 1838 | terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg; |
| 1839 | break; |
| 1840 | case 49: |
| 1841 | terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg; |
| 1842 | break; |
| 1843 | default: |
| 1844 | if(code >= 30 && code <= 37) { |
| 1845 | terminal->curr_attr.fg = code - 30; |
| 1846 | if (terminal->curr_attr.a & ATTRMASK_BOLD) |
| 1847 | terminal->curr_attr.fg += 8; |
| 1848 | } else if(code >= 40 && code <= 47) { |
| 1849 | terminal->curr_attr.bg = code - 40; |
Callum Lowcay | 81179db | 2011-01-10 12:14:01 +1300 | [diff] [blame] | 1850 | } else if (code >= 90 && code <= 97) { |
| 1851 | terminal->curr_attr.fg = code - 90 + 8; |
| 1852 | } else if (code >= 100 && code <= 107) { |
| 1853 | terminal->curr_attr.bg = code - 100 + 8; |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 1854 | } else if(code >= 256 && code < 512) { |
| 1855 | terminal->curr_attr.fg = code - 256; |
| 1856 | } else if(code >= 512 && code < 768) { |
| 1857 | terminal->curr_attr.bg = code - 512; |
| 1858 | } else { |
| 1859 | fprintf(stderr, "Unknown SGR code: %d\n", code); |
| 1860 | } |
| 1861 | break; |
| 1862 | } |
| 1863 | } |
| 1864 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1865 | /* Returns 1 if c was special, otherwise 0 */ |
| 1866 | static int |
| 1867 | handle_special_char(struct terminal *terminal, char c) |
| 1868 | { |
Kristian Høgsberg | 0fe782b | 2012-07-01 21:31:46 -0400 | [diff] [blame] | 1869 | union utf8_char *row; |
| 1870 | struct attr *attr_row; |
| 1871 | |
| 1872 | row = terminal_get_row(terminal, terminal->row); |
| 1873 | attr_row = terminal_get_attr_row(terminal, terminal->row); |
| 1874 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1875 | switch(c) { |
| 1876 | case '\r': |
| 1877 | terminal->column = 0; |
| 1878 | break; |
| 1879 | case '\n': |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1880 | if (terminal->mode & MODE_LF_NEWLINE) { |
| 1881 | terminal->column = 0; |
| 1882 | } |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1883 | /* fallthrough */ |
| 1884 | case '\v': |
| 1885 | case '\f': |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1886 | terminal->row++; |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 1887 | if (terminal->row > terminal->margin_bottom) { |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 1888 | terminal->row = terminal->margin_bottom; |
| 1889 | terminal_scroll(terminal, +1); |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | break; |
| 1893 | case '\t': |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1894 | while (terminal->column < terminal->width) { |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 1895 | if (terminal->mode & MODE_IRM) |
| 1896 | terminal_shift_line(terminal, +1); |
Kristian Høgsberg | 0fe782b | 2012-07-01 21:31:46 -0400 | [diff] [blame] | 1897 | |
| 1898 | if (row[terminal->column].byte[0] == '\0') { |
| 1899 | row[terminal->column].byte[0] = ' '; |
| 1900 | row[terminal->column].byte[1] = '\0'; |
| 1901 | attr_row[terminal->column] = terminal->curr_attr; |
| 1902 | } |
| 1903 | |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1904 | terminal->column++; |
Kristian Høgsberg | cca3c2f | 2012-06-20 15:56:13 -0400 | [diff] [blame] | 1905 | if (terminal->tab_ruler[terminal->column]) break; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1906 | } |
| 1907 | if (terminal->column >= terminal->width) { |
| 1908 | terminal->column = terminal->width - 1; |
| 1909 | } |
| 1910 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1911 | break; |
| 1912 | case '\b': |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1913 | if (terminal->column >= terminal->width) { |
| 1914 | terminal->column = terminal->width - 2; |
| 1915 | } else if (terminal->column > 0) { |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1916 | terminal->column--; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1917 | } else if (terminal->mode & MODE_AUTOWRAP) { |
| 1918 | terminal->column = terminal->width - 1; |
| 1919 | terminal->row -= 1; |
| 1920 | if (terminal->row < terminal->margin_top) { |
| 1921 | terminal->row = terminal->margin_top; |
| 1922 | terminal_scroll(terminal, -1); |
| 1923 | } |
| 1924 | } |
| 1925 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1926 | break; |
| 1927 | case '\a': |
| 1928 | /* Bell */ |
| 1929 | break; |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 1930 | case '\x0E': /* SO */ |
| 1931 | terminal->cs = terminal->g1; |
| 1932 | break; |
| 1933 | case '\x0F': /* SI */ |
| 1934 | terminal->cs = terminal->g0; |
| 1935 | break; |
Kristian Høgsberg | 2a1aa4e | 2012-08-03 09:37:05 -0400 | [diff] [blame] | 1936 | case '\0': |
| 1937 | break; |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1938 | default: |
| 1939 | return 0; |
| 1940 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1941 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1942 | return 1; |
| 1943 | } |
| 1944 | |
| 1945 | static void |
| 1946 | handle_char(struct terminal *terminal, union utf8_char utf8) |
| 1947 | { |
| 1948 | union utf8_char *row; |
| 1949 | struct attr *attr_row; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1950 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1951 | if (handle_special_char(terminal, utf8.byte[0])) return; |
Callum Lowcay | 256e72f | 2011-01-07 19:47:01 +0000 | [diff] [blame] | 1952 | |
| 1953 | apply_char_set(terminal->cs, &utf8); |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1954 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1955 | /* There are a whole lot of non-characters, control codes, |
| 1956 | * and formatting codes that should probably be ignored, |
| 1957 | * for example: */ |
| 1958 | if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) { |
| 1959 | /* BOM, ignore */ |
| 1960 | return; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1961 | } |
| 1962 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1963 | /* Some of these non-characters should be translated, e.g.: */ |
| 1964 | if (utf8.byte[0] < 32) { |
| 1965 | utf8.byte[0] = utf8.byte[0] + 64; |
| 1966 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1967 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1968 | /* handle right margin effects */ |
| 1969 | if (terminal->column >= terminal->width) { |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 1970 | if (terminal->mode & MODE_AUTOWRAP) { |
| 1971 | terminal->column = 0; |
| 1972 | terminal->row += 1; |
| 1973 | if (terminal->row > terminal->margin_bottom) { |
| 1974 | terminal->row = terminal->margin_bottom; |
| 1975 | terminal_scroll(terminal, +1); |
| 1976 | } |
| 1977 | } else { |
| 1978 | terminal->column--; |
| 1979 | } |
| 1980 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1981 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1982 | row = terminal_get_row(terminal, terminal->row); |
| 1983 | attr_row = terminal_get_attr_row(terminal, terminal->row); |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 1984 | |
Callum Lowcay | 69e9658 | 2011-01-07 19:47:00 +0000 | [diff] [blame] | 1985 | if (terminal->mode & MODE_IRM) |
| 1986 | terminal_shift_line(terminal, +1); |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 1987 | row[terminal->column] = utf8; |
| 1988 | attr_row[terminal->column++] = terminal->curr_attr; |
| 1989 | |
Kristian Høgsberg | 1d781ee | 2013-11-24 16:54:12 -0800 | [diff] [blame] | 1990 | if (terminal->row + terminal->start + 1 > terminal->end) |
| 1991 | terminal->end = terminal->row + terminal->start + 1; |
| 1992 | if (terminal->end == terminal->buffer_height) |
| 1993 | terminal->log_size = terminal->buffer_height; |
| 1994 | else if (terminal->log_size < terminal->buffer_height) |
| 1995 | terminal->log_size = terminal->end; |
| 1996 | |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 1997 | /* cursor jump for wide character. */ |
| 1998 | if (is_wide(utf8)) |
| 1999 | row[terminal->column++].ch = 0x200B; /* space glyph */ |
| 2000 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2001 | if (utf8.ch != terminal->last_char.ch) |
| 2002 | terminal->last_char = utf8; |
| 2003 | } |
| 2004 | |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 2005 | static void |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2006 | escape_append_utf8(struct terminal *terminal, union utf8_char utf8) |
| 2007 | { |
| 2008 | int len, i; |
| 2009 | |
| 2010 | if ((utf8.byte[0] & 0x80) == 0x00) len = 1; |
| 2011 | else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2; |
| 2012 | else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3; |
| 2013 | else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4; |
| 2014 | else len = 1; /* Invalid, cannot happen */ |
| 2015 | |
| 2016 | if (terminal->escape_length + len <= MAX_ESCAPE) { |
| 2017 | for (i = 0; i < len; i++) |
| 2018 | terminal->escape[terminal->escape_length + i] = utf8.byte[i]; |
| 2019 | terminal->escape_length += len; |
| 2020 | } else if (terminal->escape_length < MAX_ESCAPE) { |
| 2021 | terminal->escape[terminal->escape_length++] = 0; |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | static void |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 2026 | terminal_data(struct terminal *terminal, const char *data, size_t length) |
| 2027 | { |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 2028 | unsigned int i; |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 2029 | union utf8_char utf8; |
| 2030 | enum utf8_state parser_state; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 2031 | |
| 2032 | for (i = 0; i < length; i++) { |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 2033 | parser_state = |
| 2034 | utf8_next_char(&terminal->state_machine, data[i]); |
| 2035 | switch(parser_state) { |
| 2036 | case utf8state_accept: |
| 2037 | utf8.ch = terminal->state_machine.s.ch; |
| 2038 | break; |
| 2039 | case utf8state_reject: |
| 2040 | /* the unicode replacement character */ |
| 2041 | utf8.byte[0] = 0xEF; |
| 2042 | utf8.byte[1] = 0xBF; |
| 2043 | utf8.byte[2] = 0xBD; |
| 2044 | utf8.byte[3] = 0x00; |
| 2045 | break; |
| 2046 | default: |
| 2047 | continue; |
| 2048 | } |
| 2049 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2050 | /* assume escape codes never use non-ASCII characters */ |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2051 | switch (terminal->state) { |
| 2052 | case escape_state_escape: |
| 2053 | escape_append_utf8(terminal, utf8); |
| 2054 | switch (utf8.byte[0]) { |
| 2055 | case 'P': /* DCS */ |
| 2056 | terminal->state = escape_state_dcs; |
| 2057 | break; |
| 2058 | case '[': /* CSI */ |
| 2059 | terminal->state = escape_state_csi; |
| 2060 | break; |
| 2061 | case ']': /* OSC */ |
| 2062 | terminal->state = escape_state_osc; |
| 2063 | break; |
| 2064 | case '#': |
| 2065 | case '(': |
| 2066 | case ')': /* special */ |
| 2067 | terminal->state = escape_state_special; |
| 2068 | break; |
| 2069 | case '^': /* PM (not implemented) */ |
| 2070 | case '_': /* APC (not implemented) */ |
| 2071 | terminal->state = escape_state_ignore; |
| 2072 | break; |
| 2073 | default: |
| 2074 | terminal->state = escape_state_normal; |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2075 | handle_non_csi_escape(terminal, utf8.byte[0]); |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2076 | break; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 2077 | } |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2078 | continue; |
| 2079 | case escape_state_csi: |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2080 | if (handle_special_char(terminal, utf8.byte[0]) != 0) { |
| 2081 | /* do nothing */ |
| 2082 | } else if (utf8.byte[0] == '?') { |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2083 | terminal->escape_flags |= ESC_FLAG_WHAT; |
| 2084 | } else if (utf8.byte[0] == '>') { |
| 2085 | terminal->escape_flags |= ESC_FLAG_GT; |
| 2086 | } else if (utf8.byte[0] == '!') { |
| 2087 | terminal->escape_flags |= ESC_FLAG_BANG; |
| 2088 | } else if (utf8.byte[0] == '$') { |
| 2089 | terminal->escape_flags |= ESC_FLAG_CASH; |
| 2090 | } else if (utf8.byte[0] == '\'') { |
| 2091 | terminal->escape_flags |= ESC_FLAG_SQUOTE; |
| 2092 | } else if (utf8.byte[0] == '"') { |
| 2093 | terminal->escape_flags |= ESC_FLAG_DQUOTE; |
| 2094 | } else if (utf8.byte[0] == ' ') { |
| 2095 | terminal->escape_flags |= ESC_FLAG_SPACE; |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2096 | } else { |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2097 | escape_append_utf8(terminal, utf8); |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2098 | if (terminal->escape_length >= MAX_ESCAPE) |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2099 | terminal->state = escape_state_normal; |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2100 | } |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 2101 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2102 | if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' || |
| 2103 | utf8.byte[0] == '`') |
| 2104 | { |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2105 | terminal->state = escape_state_normal; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 2106 | handle_escape(terminal); |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2107 | } else { |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2108 | } |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2109 | continue; |
| 2110 | case escape_state_inner_escape: |
| 2111 | if (utf8.byte[0] == '\\') { |
| 2112 | terminal->state = escape_state_normal; |
| 2113 | if (terminal->outer_state == escape_state_dcs) { |
| 2114 | handle_dcs(terminal); |
| 2115 | } else if (terminal->outer_state == escape_state_osc) { |
| 2116 | handle_osc(terminal); |
| 2117 | } |
| 2118 | } else if (utf8.byte[0] == '\e') { |
| 2119 | terminal->state = terminal->outer_state; |
| 2120 | escape_append_utf8(terminal, utf8); |
| 2121 | if (terminal->escape_length >= MAX_ESCAPE) |
| 2122 | terminal->state = escape_state_normal; |
| 2123 | } else { |
| 2124 | terminal->state = terminal->outer_state; |
| 2125 | if (terminal->escape_length < MAX_ESCAPE) |
| 2126 | terminal->escape[terminal->escape_length++] = '\e'; |
| 2127 | escape_append_utf8(terminal, utf8); |
| 2128 | if (terminal->escape_length >= MAX_ESCAPE) |
| 2129 | terminal->state = escape_state_normal; |
| 2130 | } |
| 2131 | continue; |
| 2132 | case escape_state_dcs: |
| 2133 | case escape_state_osc: |
| 2134 | case escape_state_ignore: |
| 2135 | if (utf8.byte[0] == '\e') { |
| 2136 | terminal->outer_state = terminal->state; |
| 2137 | terminal->state = escape_state_inner_escape; |
| 2138 | } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) { |
| 2139 | terminal->state = escape_state_normal; |
| 2140 | handle_osc(terminal); |
| 2141 | } else { |
| 2142 | escape_append_utf8(terminal, utf8); |
| 2143 | if (terminal->escape_length >= MAX_ESCAPE) |
| 2144 | terminal->state = escape_state_normal; |
| 2145 | } |
| 2146 | continue; |
| 2147 | case escape_state_special: |
| 2148 | escape_append_utf8(terminal, utf8); |
| 2149 | terminal->state = escape_state_normal; |
| 2150 | if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) { |
| 2151 | handle_special_escape(terminal, terminal->escape[1], |
| 2152 | utf8.byte[0]); |
| 2153 | } |
| 2154 | continue; |
| 2155 | default: |
| 2156 | break; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 2157 | } |
| 2158 | |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2159 | /* this is valid, because ASCII characters are never used to |
| 2160 | * introduce a multibyte sequence in UTF-8 */ |
| 2161 | if (utf8.byte[0] == '\e') { |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2162 | terminal->state = escape_state_escape; |
| 2163 | terminal->outer_state = escape_state_normal; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 2164 | terminal->escape[0] = '\e'; |
| 2165 | terminal->escape_length = 1; |
Callum Lowcay | 67a201d | 2011-01-12 19:23:41 +1300 | [diff] [blame] | 2166 | terminal->escape_flags = 0; |
Callum Lowcay | b8609ad | 2011-01-07 19:46:57 +0000 | [diff] [blame] | 2167 | } else { |
| 2168 | handle_char(terminal, utf8); |
| 2169 | } /* if */ |
| 2170 | } /* for */ |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 2171 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 2172 | window_schedule_redraw(terminal->window); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | static void |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 2176 | data_source_target(void *data, |
| 2177 | struct wl_data_source *source, const char *mime_type) |
| 2178 | { |
| 2179 | fprintf(stderr, "data_source_target, %s\n", mime_type); |
| 2180 | } |
| 2181 | |
| 2182 | static void |
| 2183 | data_source_send(void *data, |
| 2184 | struct wl_data_source *source, |
| 2185 | const char *mime_type, int32_t fd) |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2186 | { |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 2187 | struct terminal *terminal = data; |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2188 | |
Kristian Høgsberg | 31cce05 | 2011-01-21 15:18:55 -0500 | [diff] [blame] | 2189 | terminal_send_selection(terminal, fd); |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | static void |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 2193 | data_source_cancelled(void *data, struct wl_data_source *source) |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2194 | { |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 2195 | wl_data_source_destroy(source); |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2196 | } |
| 2197 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 2198 | static const struct wl_data_source_listener data_source_listener = { |
| 2199 | data_source_target, |
| 2200 | data_source_send, |
| 2201 | data_source_cancelled |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2202 | }; |
| 2203 | |
Kristian Høgsberg | 0749e3f | 2013-09-04 20:41:06 -0700 | [diff] [blame] | 2204 | static const char text_mime_type[] = "text/plain;charset=utf-8"; |
| 2205 | |
| 2206 | static void |
| 2207 | data_handler(struct window *window, |
| 2208 | struct input *input, |
| 2209 | float x, float y, const char **types, void *data) |
| 2210 | { |
| 2211 | int i, has_text = 0; |
| 2212 | |
| 2213 | if (!types) |
| 2214 | return; |
| 2215 | for (i = 0; types[i]; i++) |
| 2216 | if (strcmp(types[i], text_mime_type) == 0) |
| 2217 | has_text = 1; |
| 2218 | |
| 2219 | if (!has_text) { |
| 2220 | input_accept(input, NULL); |
| 2221 | } else { |
| 2222 | input_accept(input, text_mime_type); |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | static void |
| 2227 | drop_handler(struct window *window, struct input *input, |
| 2228 | int32_t x, int32_t y, void *data) |
| 2229 | { |
| 2230 | struct terminal *terminal = data; |
| 2231 | |
| 2232 | input_receive_drag_data_to_fd(input, text_mime_type, terminal->master); |
| 2233 | } |
| 2234 | |
Kristian Høgsberg | 67ace20 | 2012-07-23 21:56:31 -0400 | [diff] [blame] | 2235 | static void |
| 2236 | fullscreen_handler(struct window *window, void *data) |
| 2237 | { |
| 2238 | struct terminal *terminal = data; |
| 2239 | |
Kristian Høgsberg | b7ed4cb | 2012-10-10 11:37:46 -0400 | [diff] [blame] | 2240 | window_set_fullscreen(window, !window_is_fullscreen(terminal->window)); |
Kristian Høgsberg | 67ace20 | 2012-07-23 21:56:31 -0400 | [diff] [blame] | 2241 | } |
| 2242 | |
Dima Ryazanov | d20fd4d | 2013-01-28 01:11:06 -0800 | [diff] [blame] | 2243 | static void |
Jasper St. Pierre | bf17590 | 2013-11-12 20:19:58 -0500 | [diff] [blame] | 2244 | close_handler(void *data) |
Dima Ryazanov | d20fd4d | 2013-01-28 01:11:06 -0800 | [diff] [blame] | 2245 | { |
| 2246 | struct terminal *terminal = data; |
| 2247 | |
| 2248 | terminal_destroy(terminal); |
| 2249 | } |
| 2250 | |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2251 | static void |
| 2252 | terminal_copy(struct terminal *terminal, struct input *input) |
| 2253 | { |
| 2254 | terminal->selection = |
| 2255 | display_create_data_source(terminal->display); |
| 2256 | wl_data_source_offer(terminal->selection, |
| 2257 | "text/plain;charset=utf-8"); |
| 2258 | wl_data_source_add_listener(terminal->selection, |
| 2259 | &data_source_listener, terminal); |
| 2260 | input_set_selection(input, terminal->selection, |
| 2261 | display_get_serial(terminal->display)); |
| 2262 | } |
| 2263 | |
| 2264 | static void |
| 2265 | terminal_paste(struct terminal *terminal, struct input *input) |
| 2266 | { |
| 2267 | input_receive_selection_data_to_fd(input, |
| 2268 | "text/plain;charset=utf-8", |
| 2269 | terminal->master); |
| 2270 | |
| 2271 | } |
| 2272 | |
| 2273 | static void |
| 2274 | terminal_new_instance(struct terminal *terminal) |
| 2275 | { |
| 2276 | struct terminal *new_terminal; |
| 2277 | |
| 2278 | new_terminal = terminal_create(terminal->display); |
| 2279 | if (terminal_run(new_terminal, option_shell)) |
| 2280 | terminal_destroy(new_terminal); |
| 2281 | } |
| 2282 | |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2283 | static int |
| 2284 | handle_bound_key(struct terminal *terminal, |
| 2285 | struct input *input, uint32_t sym, uint32_t time) |
| 2286 | { |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2287 | switch (sym) { |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2288 | case XKB_KEY_X: |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 2289 | /* Cut selection; terminal doesn't do cut, fall |
| 2290 | * through to copy. */ |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2291 | case XKB_KEY_C: |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2292 | terminal_copy(terminal, input); |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2293 | return 1; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2294 | case XKB_KEY_V: |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2295 | terminal_paste(terminal, input); |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2296 | return 1; |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 2297 | case XKB_KEY_N: |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2298 | terminal_new_instance(terminal); |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 2299 | return 1; |
| 2300 | |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 2301 | case XKB_KEY_Up: |
| 2302 | if (!terminal->scrolling) |
| 2303 | terminal->saved_start = terminal->start; |
| 2304 | if (terminal->start == terminal->end - terminal->log_size) |
| 2305 | return 1; |
| 2306 | |
| 2307 | terminal->scrolling = 1; |
| 2308 | terminal->start--; |
| 2309 | terminal->row++; |
| 2310 | terminal->selection_start_row++; |
| 2311 | terminal->selection_end_row++; |
| 2312 | widget_schedule_redraw(terminal->widget); |
| 2313 | return 1; |
| 2314 | |
| 2315 | case XKB_KEY_Down: |
| 2316 | if (!terminal->scrolling) |
| 2317 | terminal->saved_start = terminal->start; |
| 2318 | |
| 2319 | if (terminal->start == terminal->saved_start) |
| 2320 | return 1; |
| 2321 | |
| 2322 | terminal->scrolling = 1; |
| 2323 | terminal->start++; |
| 2324 | terminal->row--; |
| 2325 | terminal->selection_start_row--; |
| 2326 | terminal->selection_end_row--; |
| 2327 | widget_schedule_redraw(terminal->widget); |
| 2328 | return 1; |
| 2329 | |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 2330 | default: |
| 2331 | return 0; |
| 2332 | } |
| 2333 | } |
| 2334 | |
Kristian Høgsberg | 67cac8a | 2011-01-19 14:20:33 -0500 | [diff] [blame] | 2335 | static void |
| 2336 | key_handler(struct window *window, struct input *input, uint32_t time, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 2337 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 2338 | void *data) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 2339 | { |
| 2340 | struct terminal *terminal = data; |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 2341 | char ch[MAX_RESPONSE]; |
Kristian Høgsberg | 88fd408 | 2012-06-21 15:55:03 -0400 | [diff] [blame] | 2342 | uint32_t modifiers, serial; |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 2343 | int ret, len = 0, d; |
Philipp Brüschweiler | fdb4b02 | 2012-08-18 13:38:38 +0200 | [diff] [blame] | 2344 | bool convert_utf8 = true; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 2345 | |
Kristian Høgsberg | 67cac8a | 2011-01-19 14:20:33 -0500 | [diff] [blame] | 2346 | modifiers = input_get_modifiers(input); |
Kristian Høgsberg | 7016313 | 2012-05-08 15:55:39 -0400 | [diff] [blame] | 2347 | if ((modifiers & MOD_CONTROL_MASK) && |
| 2348 | (modifiers & MOD_SHIFT_MASK) && |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 2349 | state == WL_KEYBOARD_KEY_STATE_PRESSED && |
| 2350 | handle_bound_key(terminal, input, sym, time)) |
Kristian Høgsberg | 67cac8a | 2011-01-19 14:20:33 -0500 | [diff] [blame] | 2351 | return; |
| 2352 | |
Daniel Stone | a846871 | 2012-11-07 17:51:35 +1100 | [diff] [blame] | 2353 | /* Map keypad symbols to 'normal' equivalents before processing */ |
| 2354 | switch (sym) { |
| 2355 | case XKB_KEY_KP_Space: |
| 2356 | sym = XKB_KEY_space; |
| 2357 | break; |
| 2358 | case XKB_KEY_KP_Tab: |
| 2359 | sym = XKB_KEY_Tab; |
| 2360 | break; |
| 2361 | case XKB_KEY_KP_Enter: |
| 2362 | sym = XKB_KEY_Return; |
| 2363 | break; |
| 2364 | case XKB_KEY_KP_Left: |
| 2365 | sym = XKB_KEY_Left; |
| 2366 | break; |
| 2367 | case XKB_KEY_KP_Up: |
| 2368 | sym = XKB_KEY_Up; |
| 2369 | break; |
| 2370 | case XKB_KEY_KP_Right: |
| 2371 | sym = XKB_KEY_Right; |
| 2372 | break; |
| 2373 | case XKB_KEY_KP_Down: |
| 2374 | sym = XKB_KEY_Down; |
| 2375 | break; |
| 2376 | case XKB_KEY_KP_Equal: |
| 2377 | sym = XKB_KEY_equal; |
| 2378 | break; |
| 2379 | case XKB_KEY_KP_Multiply: |
| 2380 | sym = XKB_KEY_asterisk; |
| 2381 | break; |
| 2382 | case XKB_KEY_KP_Add: |
| 2383 | sym = XKB_KEY_plus; |
| 2384 | break; |
| 2385 | case XKB_KEY_KP_Separator: |
| 2386 | /* Note this is actually locale-dependent and should mostly be |
| 2387 | * a comma. But leave it as period until we one day start |
| 2388 | * doing the right thing. */ |
| 2389 | sym = XKB_KEY_period; |
| 2390 | break; |
| 2391 | case XKB_KEY_KP_Subtract: |
| 2392 | sym = XKB_KEY_minus; |
| 2393 | break; |
| 2394 | case XKB_KEY_KP_Decimal: |
| 2395 | sym = XKB_KEY_period; |
| 2396 | break; |
| 2397 | case XKB_KEY_KP_Divide: |
| 2398 | sym = XKB_KEY_slash; |
| 2399 | break; |
| 2400 | case XKB_KEY_KP_0: |
| 2401 | case XKB_KEY_KP_1: |
| 2402 | case XKB_KEY_KP_2: |
| 2403 | case XKB_KEY_KP_3: |
| 2404 | case XKB_KEY_KP_4: |
| 2405 | case XKB_KEY_KP_5: |
| 2406 | case XKB_KEY_KP_6: |
| 2407 | case XKB_KEY_KP_7: |
| 2408 | case XKB_KEY_KP_8: |
| 2409 | case XKB_KEY_KP_9: |
| 2410 | sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0; |
| 2411 | break; |
| 2412 | default: |
| 2413 | break; |
| 2414 | } |
| 2415 | |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 2416 | switch (sym) { |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2417 | case XKB_KEY_BackSpace: |
Kristian Høgsberg | b7f94bf | 2012-06-21 12:29:36 -0400 | [diff] [blame] | 2418 | if (modifiers & MOD_ALT_MASK) |
| 2419 | ch[len++] = 0x1b; |
Kristian Høgsberg | 71a4cf4 | 2012-06-20 17:57:56 -0400 | [diff] [blame] | 2420 | ch[len++] = 0x7f; |
| 2421 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2422 | case XKB_KEY_Tab: |
| 2423 | case XKB_KEY_Linefeed: |
| 2424 | case XKB_KEY_Clear: |
| 2425 | case XKB_KEY_Pause: |
| 2426 | case XKB_KEY_Scroll_Lock: |
| 2427 | case XKB_KEY_Sys_Req: |
| 2428 | case XKB_KEY_Escape: |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 2429 | ch[len++] = sym & 0x7f; |
| 2430 | break; |
| 2431 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2432 | case XKB_KEY_Return: |
Callum Lowcay | 8e57dd5 | 2011-01-07 19:46:59 +0000 | [diff] [blame] | 2433 | if (terminal->mode & MODE_LF_NEWLINE) { |
| 2434 | ch[len++] = 0x0D; |
| 2435 | ch[len++] = 0x0A; |
| 2436 | } else { |
| 2437 | ch[len++] = 0x0D; |
| 2438 | } |
| 2439 | break; |
| 2440 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2441 | case XKB_KEY_Shift_L: |
| 2442 | case XKB_KEY_Shift_R: |
| 2443 | case XKB_KEY_Control_L: |
| 2444 | case XKB_KEY_Control_R: |
| 2445 | case XKB_KEY_Alt_L: |
| 2446 | case XKB_KEY_Alt_R: |
Kristian Høgsberg | 22fbcf7 | 2012-06-22 12:18:56 -0400 | [diff] [blame] | 2447 | case XKB_KEY_Meta_L: |
| 2448 | case XKB_KEY_Meta_R: |
| 2449 | case XKB_KEY_Super_L: |
| 2450 | case XKB_KEY_Super_R: |
| 2451 | case XKB_KEY_Hyper_L: |
| 2452 | case XKB_KEY_Hyper_R: |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 2453 | break; |
| 2454 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2455 | case XKB_KEY_Insert: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2456 | len = function_key_response('[', 2, modifiers, '~', ch); |
| 2457 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2458 | case XKB_KEY_Delete: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2459 | if (terminal->mode & MODE_DELETE_SENDS_DEL) { |
| 2460 | ch[len++] = '\x04'; |
| 2461 | } else { |
| 2462 | len = function_key_response('[', 3, modifiers, '~', ch); |
| 2463 | } |
| 2464 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2465 | case XKB_KEY_Page_Up: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2466 | len = function_key_response('[', 5, modifiers, '~', ch); |
| 2467 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2468 | case XKB_KEY_Page_Down: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2469 | len = function_key_response('[', 6, modifiers, '~', ch); |
| 2470 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2471 | case XKB_KEY_F1: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2472 | len = function_key_response('O', 1, modifiers, 'P', ch); |
| 2473 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2474 | case XKB_KEY_F2: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2475 | len = function_key_response('O', 1, modifiers, 'Q', ch); |
| 2476 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2477 | case XKB_KEY_F3: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2478 | len = function_key_response('O', 1, modifiers, 'R', ch); |
| 2479 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2480 | case XKB_KEY_F4: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2481 | len = function_key_response('O', 1, modifiers, 'S', ch); |
| 2482 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2483 | case XKB_KEY_F5: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2484 | len = function_key_response('[', 15, modifiers, '~', ch); |
| 2485 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2486 | case XKB_KEY_F6: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2487 | len = function_key_response('[', 17, modifiers, '~', ch); |
| 2488 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2489 | case XKB_KEY_F7: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2490 | len = function_key_response('[', 18, modifiers, '~', ch); |
| 2491 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2492 | case XKB_KEY_F8: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2493 | len = function_key_response('[', 19, modifiers, '~', ch); |
| 2494 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2495 | case XKB_KEY_F9: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2496 | len = function_key_response('[', 20, modifiers, '~', ch); |
| 2497 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2498 | case XKB_KEY_F10: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2499 | len = function_key_response('[', 21, modifiers, '~', ch); |
| 2500 | break; |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 2501 | case XKB_KEY_F12: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2502 | len = function_key_response('[', 24, modifiers, '~', ch); |
| 2503 | break; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 2504 | default: |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2505 | /* Handle special keys with alternate mappings */ |
| 2506 | len = apply_key_map(terminal->key_mode, sym, modifiers, ch); |
| 2507 | if (len != 0) break; |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 2508 | |
Kristian Høgsberg | 7016313 | 2012-05-08 15:55:39 -0400 | [diff] [blame] | 2509 | if (modifiers & MOD_CONTROL_MASK) { |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2510 | if (sym >= '3' && sym <= '7') |
| 2511 | sym = (sym & 0x1f) + 8; |
| 2512 | |
| 2513 | if (!((sym >= '!' && sym <= '/') || |
| 2514 | (sym >= '8' && sym <= '?') || |
| 2515 | (sym >= '0' && sym <= '2'))) sym = sym & 0x1f; |
| 2516 | else if (sym == '2') sym = 0x00; |
| 2517 | else if (sym == '/') sym = 0x1F; |
| 2518 | else if (sym == '8' || sym == '?') sym = 0x7F; |
Kristian Høgsberg | ae9e073 | 2012-06-21 12:30:15 -0400 | [diff] [blame] | 2519 | } |
Philipp Brüschweiler | fdb4b02 | 2012-08-18 13:38:38 +0200 | [diff] [blame] | 2520 | if (modifiers & MOD_ALT_MASK) { |
| 2521 | if (terminal->mode & MODE_ALT_SENDS_ESC) { |
| 2522 | ch[len++] = 0x1b; |
| 2523 | } else { |
| 2524 | sym = sym | 0x80; |
| 2525 | convert_utf8 = false; |
| 2526 | } |
Callum Lowcay | 7e08e90 | 2011-01-07 19:47:02 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Philipp Brüschweiler | fdb4b02 | 2012-08-18 13:38:38 +0200 | [diff] [blame] | 2529 | if ((sym < 128) || |
| 2530 | (!convert_utf8 && sym < 256)) { |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 2531 | ch[len++] = sym; |
Philipp Brüschweiler | fdb4b02 | 2012-08-18 13:38:38 +0200 | [diff] [blame] | 2532 | } else { |
| 2533 | ret = xkb_keysym_to_utf8(sym, ch + len, |
| 2534 | MAX_RESPONSE - len); |
| 2535 | if (ret < 0) |
| 2536 | fprintf(stderr, |
| 2537 | "Warning: buffer too small to encode " |
| 2538 | "UTF8 character\n"); |
| 2539 | else |
| 2540 | len += ret; |
| 2541 | } |
| 2542 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 2543 | break; |
| 2544 | } |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 2545 | |
Kristian Høgsberg | b24ab80 | 2012-06-22 12:17:17 -0400 | [diff] [blame] | 2546 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) { |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 2547 | if (terminal->scrolling) { |
| 2548 | d = terminal->saved_start - terminal->start; |
| 2549 | terminal->row -= d; |
| 2550 | terminal->selection_start_row -= d; |
| 2551 | terminal->selection_end_row -= d; |
| 2552 | terminal->start = terminal->saved_start; |
| 2553 | terminal->scrolling = 0; |
| 2554 | widget_schedule_redraw(terminal->widget); |
| 2555 | } |
| 2556 | |
Kristian Høgsberg | 2613086 | 2011-08-24 11:30:21 -0400 | [diff] [blame] | 2557 | terminal_write(terminal, ch, len); |
Kristian Høgsberg | b24ab80 | 2012-06-22 12:17:17 -0400 | [diff] [blame] | 2558 | |
| 2559 | /* Hide cursor, except if this was coming from a |
| 2560 | * repeating key press. */ |
| 2561 | serial = display_get_serial(terminal->display); |
| 2562 | if (terminal->hide_cursor_serial != serial) { |
| 2563 | input_set_pointer_image(input, CURSOR_BLANK); |
| 2564 | terminal->hide_cursor_serial = serial; |
| 2565 | } |
| 2566 | } |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 2567 | } |
| 2568 | |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 2569 | static void |
| 2570 | keyboard_focus_handler(struct window *window, |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame] | 2571 | struct input *device, void *data) |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 2572 | { |
| 2573 | struct terminal *terminal = data; |
| 2574 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 2575 | window_schedule_redraw(terminal->window); |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 2576 | } |
| 2577 | |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2578 | static int wordsep(int ch) |
| 2579 | { |
| 2580 | const char extra[] = "-,./?%&#:_=+@~"; |
| 2581 | |
Andre Heider | 552d12b | 2012-08-02 20:59:43 +0200 | [diff] [blame] | 2582 | if (ch > 127) |
| 2583 | return 1; |
| 2584 | |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2585 | return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch)); |
| 2586 | } |
| 2587 | |
| 2588 | static int |
| 2589 | recompute_selection(struct terminal *terminal) |
| 2590 | { |
| 2591 | struct rectangle allocation; |
| 2592 | int col, x, width, height; |
| 2593 | int start_row, end_row; |
Kristian Høgsberg | 0fe782b | 2012-07-01 21:31:46 -0400 | [diff] [blame] | 2594 | int word_start, eol; |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2595 | int side_margin, top_margin; |
| 2596 | int start_x, end_x; |
| 2597 | int cw, ch; |
| 2598 | union utf8_char *data; |
| 2599 | |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 2600 | cw = terminal->average_width; |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2601 | ch = terminal->extents.height; |
| 2602 | widget_get_allocation(terminal->widget, &allocation); |
| 2603 | width = terminal->width * cw; |
| 2604 | height = terminal->height * ch; |
| 2605 | side_margin = allocation.x + (allocation.width - width) / 2; |
| 2606 | top_margin = allocation.y + (allocation.height - height) / 2; |
| 2607 | |
Kristian Høgsberg | 8268d41 | 2012-06-29 11:35:24 -0400 | [diff] [blame] | 2608 | start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1; |
| 2609 | end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1; |
| 2610 | |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2611 | if (start_row < end_row || |
| 2612 | (start_row == end_row && |
| 2613 | terminal->selection_start_x < terminal->selection_end_x)) { |
| 2614 | terminal->selection_start_row = start_row; |
| 2615 | terminal->selection_end_row = end_row; |
| 2616 | start_x = terminal->selection_start_x; |
| 2617 | end_x = terminal->selection_end_x; |
| 2618 | } else { |
| 2619 | terminal->selection_start_row = end_row; |
| 2620 | terminal->selection_end_row = start_row; |
| 2621 | start_x = terminal->selection_end_x; |
| 2622 | end_x = terminal->selection_start_x; |
| 2623 | } |
| 2624 | |
Kristian Høgsberg | 0fe782b | 2012-07-01 21:31:46 -0400 | [diff] [blame] | 2625 | eol = 0; |
Kristian Høgsberg | 8268d41 | 2012-06-29 11:35:24 -0400 | [diff] [blame] | 2626 | if (terminal->selection_start_row < 0) { |
| 2627 | terminal->selection_start_row = 0; |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2628 | terminal->selection_start_col = 0; |
Kristian Høgsberg | 8268d41 | 2012-06-29 11:35:24 -0400 | [diff] [blame] | 2629 | } else { |
| 2630 | x = side_margin + cw / 2; |
| 2631 | data = terminal_get_row(terminal, |
| 2632 | terminal->selection_start_row); |
| 2633 | word_start = 0; |
| 2634 | for (col = 0; col < terminal->width; col++, x += cw) { |
| 2635 | if (col == 0 || wordsep(data[col - 1].ch)) |
| 2636 | word_start = col; |
Kristian Høgsberg | 0fe782b | 2012-07-01 21:31:46 -0400 | [diff] [blame] | 2637 | if (data[col].ch != 0) |
| 2638 | eol = col + 1; |
Kristian Høgsberg | 8268d41 | 2012-06-29 11:35:24 -0400 | [diff] [blame] | 2639 | if (start_x < x) |
| 2640 | break; |
| 2641 | } |
| 2642 | |
| 2643 | switch (terminal->dragging) { |
| 2644 | case SELECT_LINE: |
| 2645 | terminal->selection_start_col = 0; |
| 2646 | break; |
| 2647 | case SELECT_WORD: |
| 2648 | terminal->selection_start_col = word_start; |
| 2649 | break; |
| 2650 | case SELECT_CHAR: |
| 2651 | terminal->selection_start_col = col; |
| 2652 | break; |
| 2653 | } |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2654 | } |
| 2655 | |
Kristian Høgsberg | 8268d41 | 2012-06-29 11:35:24 -0400 | [diff] [blame] | 2656 | if (terminal->selection_end_row >= terminal->height) { |
| 2657 | terminal->selection_end_row = terminal->height; |
| 2658 | terminal->selection_end_col = 0; |
| 2659 | } else { |
| 2660 | x = side_margin + cw / 2; |
| 2661 | data = terminal_get_row(terminal, terminal->selection_end_row); |
| 2662 | for (col = 0; col < terminal->width; col++, x += cw) { |
| 2663 | if (terminal->dragging == SELECT_CHAR && end_x < x) |
| 2664 | break; |
| 2665 | if (terminal->dragging == SELECT_WORD && |
| 2666 | end_x < x && wordsep(data[col].ch)) |
| 2667 | break; |
| 2668 | } |
Kristian Høgsberg | 8268d41 | 2012-06-29 11:35:24 -0400 | [diff] [blame] | 2669 | terminal->selection_end_col = col; |
| 2670 | } |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2671 | |
Kristian Høgsberg | 0fe782b | 2012-07-01 21:31:46 -0400 | [diff] [blame] | 2672 | if (terminal->selection_end_col != terminal->selection_start_col || |
| 2673 | terminal->selection_start_row != terminal->selection_end_row) { |
| 2674 | col = terminal->selection_end_col; |
| 2675 | if (col > 0 && data[col - 1].ch == 0) |
| 2676 | terminal->selection_end_col = terminal->width; |
| 2677 | data = terminal_get_row(terminal, terminal->selection_start_row); |
| 2678 | if (data[terminal->selection_start_col].ch == 0) |
| 2679 | terminal->selection_start_col = eol; |
| 2680 | } |
| 2681 | |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2682 | return 1; |
| 2683 | } |
| 2684 | |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2685 | static void |
Pekka Paalanen | 807f09c | 2015-03-27 15:48:54 +0200 | [diff] [blame] | 2686 | terminal_minimize(struct terminal *terminal) |
| 2687 | { |
| 2688 | window_set_minimized(terminal->window); |
| 2689 | } |
| 2690 | |
| 2691 | static void |
Jasper St. Pierre | dda9313 | 2014-03-13 12:06:00 -0400 | [diff] [blame] | 2692 | menu_func(void *data, struct input *input, int index) |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2693 | { |
Jasper St. Pierre | dda9313 | 2014-03-13 12:06:00 -0400 | [diff] [blame] | 2694 | struct window *window = data; |
| 2695 | struct terminal *terminal = window_get_user_data(window); |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2696 | |
| 2697 | fprintf(stderr, "picked entry %d\n", index); |
| 2698 | |
| 2699 | switch (index) { |
| 2700 | case 0: |
| 2701 | terminal_new_instance(terminal); |
| 2702 | break; |
| 2703 | case 1: |
| 2704 | terminal_copy(terminal, input); |
| 2705 | break; |
| 2706 | case 2: |
| 2707 | terminal_paste(terminal, input); |
| 2708 | break; |
Pekka Paalanen | 807f09c | 2015-03-27 15:48:54 +0200 | [diff] [blame] | 2709 | case 3: |
| 2710 | terminal_minimize(terminal); |
| 2711 | break; |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | static void |
| 2716 | show_menu(struct terminal *terminal, struct input *input, uint32_t time) |
| 2717 | { |
| 2718 | int32_t x, y; |
| 2719 | static const char *entries[] = { |
Pekka Paalanen | 807f09c | 2015-03-27 15:48:54 +0200 | [diff] [blame] | 2720 | "Open Terminal", "Copy", "Paste", "Minimize" |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2721 | }; |
| 2722 | |
| 2723 | input_get_position(input, &x, &y); |
| 2724 | window_show_menu(terminal->display, input, time, terminal->window, |
| 2725 | x - 10, y - 10, menu_func, |
| 2726 | entries, ARRAY_LENGTH(entries)); |
| 2727 | } |
| 2728 | |
| 2729 | static void |
Xiong Zhang | 49c6aee | 2013-11-25 18:42:52 +0800 | [diff] [blame] | 2730 | click_handler(struct widget *widget, struct terminal *terminal, |
| 2731 | struct input *input, int32_t x, int32_t y, |
| 2732 | uint32_t time) |
| 2733 | { |
| 2734 | if (time - terminal->click_time < 500) |
| 2735 | terminal->click_count++; |
| 2736 | else |
| 2737 | terminal->click_count = 1; |
| 2738 | |
| 2739 | terminal->click_time = time; |
| 2740 | terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR; |
| 2741 | |
| 2742 | terminal->selection_end_x = terminal->selection_start_x = x; |
| 2743 | terminal->selection_end_y = terminal->selection_start_y = y; |
| 2744 | if (recompute_selection(terminal)) |
| 2745 | widget_schedule_redraw(widget); |
| 2746 | } |
| 2747 | |
| 2748 | static void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 2749 | button_handler(struct widget *widget, |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2750 | struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 2751 | uint32_t button, |
| 2752 | enum wl_pointer_button_state state, void *data) |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2753 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 2754 | struct terminal *terminal = data; |
Xiong Zhang | 49c6aee | 2013-11-25 18:42:52 +0800 | [diff] [blame] | 2755 | int32_t x, y; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2756 | |
| 2757 | switch (button) { |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2758 | case BTN_LEFT: |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 2759 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
Xiong Zhang | 49c6aee | 2013-11-25 18:42:52 +0800 | [diff] [blame] | 2760 | input_get_position(input, &x, &y); |
| 2761 | click_handler(widget, terminal, input, x, y, time); |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2762 | } else { |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2763 | terminal->dragging = SELECT_NONE; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2764 | } |
| 2765 | break; |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2766 | |
| 2767 | case BTN_RIGHT: |
| 2768 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 2769 | show_menu(terminal, input, time); |
| 2770 | break; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | static int |
Kristian Høgsberg | 2978440 | 2012-06-28 14:27:02 -0400 | [diff] [blame] | 2775 | enter_handler(struct widget *widget, |
| 2776 | struct input *input, float x, float y, void *data) |
| 2777 | { |
| 2778 | return CURSOR_IBEAM; |
| 2779 | } |
| 2780 | |
| 2781 | static int |
Kristian Høgsberg | 5f190ef | 2012-01-09 09:44:45 -0500 | [diff] [blame] | 2782 | motion_handler(struct widget *widget, |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2783 | struct input *input, uint32_t time, |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 2784 | float x, float y, void *data) |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2785 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 2786 | struct terminal *terminal = data; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2787 | |
| 2788 | if (terminal->dragging) { |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2789 | input_get_position(input, |
| 2790 | &terminal->selection_end_x, |
| 2791 | &terminal->selection_end_y); |
Kristian Høgsberg | 333db0a | 2012-06-27 17:43:10 -0400 | [diff] [blame] | 2792 | |
| 2793 | if (recompute_selection(terminal)) |
| 2794 | widget_schedule_redraw(widget); |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2795 | } |
| 2796 | |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2797 | return CURSOR_IBEAM; |
Kristian Høgsberg | 5982658 | 2011-01-20 11:56:57 -0500 | [diff] [blame] | 2798 | } |
| 2799 | |
Magnus Hoff | 1046f12 | 2014-08-05 15:05:59 +0200 | [diff] [blame] | 2800 | /* This magnitude is chosen rather arbitrarily. Really, the scrolling |
| 2801 | * should happen on a (fractional) pixel basis, not a line basis. */ |
| 2802 | #define AXIS_UNITS_PER_LINE 256 |
| 2803 | |
| 2804 | static void |
| 2805 | axis_handler(struct widget *widget, |
| 2806 | struct input *input, uint32_t time, |
| 2807 | uint32_t axis, |
| 2808 | wl_fixed_t value, |
| 2809 | void *data) |
| 2810 | { |
| 2811 | struct terminal *terminal = data; |
| 2812 | int lines; |
| 2813 | |
| 2814 | if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL) |
| 2815 | return; |
| 2816 | |
| 2817 | terminal->smooth_scroll += value; |
| 2818 | lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE; |
| 2819 | terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE; |
| 2820 | |
| 2821 | if (lines > 0) { |
| 2822 | if (terminal->scrolling) { |
| 2823 | if ((uint32_t)lines > terminal->saved_start - terminal->start) |
| 2824 | lines = terminal->saved_start - terminal->start; |
| 2825 | } else { |
| 2826 | lines = 0; |
| 2827 | } |
| 2828 | } else if (lines < 0) { |
| 2829 | uint32_t neg_lines = -lines; |
| 2830 | |
| 2831 | if (neg_lines > terminal->log_size + terminal->start - terminal->end) |
| 2832 | lines = terminal->end - terminal->log_size - terminal->start; |
| 2833 | } |
| 2834 | |
| 2835 | if (lines) { |
| 2836 | if (!terminal->scrolling) |
| 2837 | terminal->saved_start = terminal->start; |
| 2838 | terminal->scrolling = 1; |
| 2839 | |
| 2840 | terminal->start += lines; |
| 2841 | terminal->row -= lines; |
| 2842 | terminal->selection_start_row -= lines; |
| 2843 | terminal->selection_end_row -= lines; |
| 2844 | |
| 2845 | widget_schedule_redraw(widget); |
| 2846 | } |
| 2847 | } |
| 2848 | |
Alexander Larsson | de79dd0 | 2013-05-22 14:41:34 +0200 | [diff] [blame] | 2849 | static void |
| 2850 | output_handler(struct window *window, struct output *output, int enter, |
| 2851 | void *data) |
| 2852 | { |
| 2853 | if (enter) |
| 2854 | window_set_buffer_transform(window, output_get_transform(output)); |
| 2855 | window_set_buffer_scale(window, window_get_output_scale(window)); |
| 2856 | window_schedule_redraw(window); |
| 2857 | } |
| 2858 | |
Xiong Zhang | 49c6aee | 2013-11-25 18:42:52 +0800 | [diff] [blame] | 2859 | static void |
| 2860 | touch_down_handler(struct widget *widget, struct input *input, |
| 2861 | uint32_t serial, uint32_t time, int32_t id, |
| 2862 | float x, float y, void *data) |
| 2863 | { |
| 2864 | struct terminal *terminal = data; |
| 2865 | |
| 2866 | if (id == 0) |
| 2867 | click_handler(widget, terminal, input, x, y, time); |
| 2868 | } |
| 2869 | |
| 2870 | static void |
| 2871 | touch_up_handler(struct widget *widget, struct input *input, |
| 2872 | uint32_t serial, uint32_t time, int32_t id, void *data) |
| 2873 | { |
| 2874 | struct terminal *terminal = data; |
| 2875 | |
| 2876 | if (id == 0) |
| 2877 | terminal->dragging = SELECT_NONE; |
| 2878 | } |
| 2879 | |
| 2880 | static void |
| 2881 | touch_motion_handler(struct widget *widget, struct input *input, |
| 2882 | uint32_t time, int32_t id, float x, float y, void *data) |
| 2883 | { |
| 2884 | struct terminal *terminal = data; |
| 2885 | |
| 2886 | if (terminal->dragging && |
| 2887 | id == 0) { |
| 2888 | terminal->selection_end_x = (int)x; |
| 2889 | terminal->selection_end_y = (int)y; |
| 2890 | |
| 2891 | if (recompute_selection(terminal)) |
| 2892 | widget_schedule_redraw(widget); |
| 2893 | } |
| 2894 | } |
| 2895 | |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 2896 | #ifndef howmany |
| 2897 | #define howmany(x, y) (((x) + ((y) - 1)) / (y)) |
| 2898 | #endif |
| 2899 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 2900 | static struct terminal * |
Kristian Høgsberg | b7ed4cb | 2012-10-10 11:37:46 -0400 | [diff] [blame] | 2901 | terminal_create(struct display *display) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 2902 | { |
| 2903 | struct terminal *terminal; |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 2904 | cairo_surface_t *surface; |
| 2905 | cairo_t *cr; |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 2906 | cairo_text_extents_t text_extents; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 2907 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 2908 | terminal = xzalloc(sizeof *terminal); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 2909 | terminal->color_scheme = &DEFAULT_COLORS; |
| 2910 | terminal_init(terminal); |
Callum Lowcay | bbeac60 | 2011-01-07 19:46:58 +0000 | [diff] [blame] | 2911 | terminal->margin_top = 0; |
Callum Lowcay | 86653ed | 2011-01-07 19:47:03 +0000 | [diff] [blame] | 2912 | terminal->margin_bottom = -1; |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 2913 | terminal->window = window_create(display); |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 2914 | terminal->widget = window_frame_create(terminal->window, terminal); |
U. Artie Eoff | 09827e2 | 2014-01-15 11:40:37 -0800 | [diff] [blame] | 2915 | terminal->title = xstrdup("Wayland Terminal"); |
Kristian Høgsberg | a83be20 | 2013-10-23 20:47:35 -0700 | [diff] [blame] | 2916 | window_set_title(terminal->window, terminal->title); |
Kristian Høgsberg | 010f98b | 2012-02-23 17:30:45 -0500 | [diff] [blame] | 2917 | widget_set_transparent(terminal->widget, 0); |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 2918 | |
| 2919 | init_state_machine(&terminal->state_machine); |
Callum Lowcay | 30eeae5 | 2011-01-07 19:46:55 +0000 | [diff] [blame] | 2920 | init_color_table(terminal); |
Callum Lowcay | 15bdc5d | 2011-01-07 19:46:54 +0000 | [diff] [blame] | 2921 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 2922 | terminal->display = display; |
Kristian Høgsberg | 1584c57 | 2008-12-08 12:59:37 -0500 | [diff] [blame] | 2923 | terminal->margin = 5; |
Kristian Høgsberg | 14f39b2 | 2013-10-23 16:29:14 -0700 | [diff] [blame] | 2924 | terminal->buffer_height = 1024; |
| 2925 | terminal->end = 1; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 2926 | |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 2927 | window_set_user_data(terminal->window, terminal); |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 2928 | window_set_key_handler(terminal->window, key_handler); |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 2929 | window_set_keyboard_focus_handler(terminal->window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 2930 | keyboard_focus_handler); |
Kristian Høgsberg | 67ace20 | 2012-07-23 21:56:31 -0400 | [diff] [blame] | 2931 | window_set_fullscreen_handler(terminal->window, fullscreen_handler); |
Alexander Larsson | de79dd0 | 2013-05-22 14:41:34 +0200 | [diff] [blame] | 2932 | window_set_output_handler(terminal->window, output_handler); |
Dima Ryazanov | d20fd4d | 2013-01-28 01:11:06 -0800 | [diff] [blame] | 2933 | window_set_close_handler(terminal->window, close_handler); |
Jasper St. Pierre | de68099 | 2014-04-10 17:23:49 -0700 | [diff] [blame] | 2934 | window_set_state_changed_handler(terminal->window, state_changed_handler); |
Kristian Høgsberg | 67ace20 | 2012-07-23 21:56:31 -0400 | [diff] [blame] | 2935 | |
Kristian Høgsberg | 0749e3f | 2013-09-04 20:41:06 -0700 | [diff] [blame] | 2936 | window_set_data_handler(terminal->window, data_handler); |
| 2937 | window_set_drop_handler(terminal->window, drop_handler); |
| 2938 | |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 2939 | widget_set_redraw_handler(terminal->widget, redraw_handler); |
| 2940 | widget_set_resize_handler(terminal->widget, resize_handler); |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 2941 | widget_set_button_handler(terminal->widget, button_handler); |
Kristian Høgsberg | 2978440 | 2012-06-28 14:27:02 -0400 | [diff] [blame] | 2942 | widget_set_enter_handler(terminal->widget, enter_handler); |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 2943 | widget_set_motion_handler(terminal->widget, motion_handler); |
Magnus Hoff | 1046f12 | 2014-08-05 15:05:59 +0200 | [diff] [blame] | 2944 | widget_set_axis_handler(terminal->widget, axis_handler); |
Xiong Zhang | 49c6aee | 2013-11-25 18:42:52 +0800 | [diff] [blame] | 2945 | widget_set_touch_up_handler(terminal->widget, touch_up_handler); |
| 2946 | widget_set_touch_down_handler(terminal->widget, touch_down_handler); |
| 2947 | widget_set_touch_motion_handler(terminal->widget, touch_motion_handler); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 2948 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 2949 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); |
| 2950 | cr = cairo_create(surface); |
Kristian Høgsberg | 38912df | 2012-06-27 17:52:23 -0400 | [diff] [blame] | 2951 | cairo_set_font_size(cr, option_font_size); |
Kristian Høgsberg | 82cd36b | 2012-06-20 15:29:07 -0400 | [diff] [blame] | 2952 | cairo_select_font_face (cr, option_font, |
Kristian Høgsberg | 7ae6b1a | 2010-12-25 16:58:31 -0500 | [diff] [blame] | 2953 | CAIRO_FONT_SLANT_NORMAL, |
| 2954 | CAIRO_FONT_WEIGHT_BOLD); |
| 2955 | terminal->font_bold = cairo_get_scaled_font (cr); |
| 2956 | cairo_scaled_font_reference(terminal->font_bold); |
| 2957 | |
Kristian Høgsberg | 82cd36b | 2012-06-20 15:29:07 -0400 | [diff] [blame] | 2958 | cairo_select_font_face (cr, option_font, |
Kristian Høgsberg | 7ae6b1a | 2010-12-25 16:58:31 -0500 | [diff] [blame] | 2959 | CAIRO_FONT_SLANT_NORMAL, |
| 2960 | CAIRO_FONT_WEIGHT_NORMAL); |
| 2961 | terminal->font_normal = cairo_get_scaled_font (cr); |
| 2962 | cairo_scaled_font_reference(terminal->font_normal); |
| 2963 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 2964 | cairo_font_extents(cr, &terminal->extents); |
Peng Wu | f291f20 | 2013-06-06 15:32:41 +0800 | [diff] [blame] | 2965 | |
| 2966 | /* Compute the average ascii glyph width */ |
| 2967 | cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS, |
| 2968 | &text_extents); |
| 2969 | terminal->average_width = howmany |
| 2970 | (text_extents.width, |
| 2971 | strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS)); |
| 2972 | terminal->average_width = ceil(terminal->average_width); |
| 2973 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 2974 | cairo_destroy(cr); |
| 2975 | cairo_surface_destroy(surface); |
| 2976 | |
Kristian Høgsberg | d3a1965 | 2012-07-20 11:32:51 -0400 | [diff] [blame] | 2977 | terminal_resize(terminal, 20, 5); /* Set minimum size first */ |
Kristian Høgsberg | a162792 | 2012-06-20 17:30:03 -0400 | [diff] [blame] | 2978 | terminal_resize(terminal, 80, 25); |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 2979 | |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 2980 | wl_list_insert(terminal_list.prev, &terminal->link); |
| 2981 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 2982 | return terminal; |
| 2983 | } |
| 2984 | |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 2985 | static void |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 2986 | terminal_destroy(struct terminal *terminal) |
| 2987 | { |
Dima Ryazanov | a85292e | 2012-11-29 00:27:09 -0800 | [diff] [blame] | 2988 | display_unwatch_fd(terminal->display, terminal->master); |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 2989 | window_destroy(terminal->window); |
| 2990 | close(terminal->master); |
| 2991 | wl_list_remove(&terminal->link); |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 2992 | |
| 2993 | if (wl_list_empty(&terminal_list)) |
Dima Ryazanov | a85292e | 2012-11-29 00:27:09 -0800 | [diff] [blame] | 2994 | display_exit(terminal->display); |
| 2995 | |
Kristian Høgsberg | a83be20 | 2013-10-23 20:47:35 -0700 | [diff] [blame] | 2996 | free(terminal->title); |
Dima Ryazanov | a85292e | 2012-11-29 00:27:09 -0800 | [diff] [blame] | 2997 | free(terminal); |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | static void |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 3001 | io_handler(struct task *task, uint32_t events) |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3002 | { |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 3003 | struct terminal *terminal = |
| 3004 | container_of(task, struct terminal, io_task); |
| 3005 | char buffer[256]; |
| 3006 | int len; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3007 | |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 3008 | if (events & EPOLLHUP) { |
| 3009 | terminal_destroy(terminal); |
| 3010 | return; |
| 3011 | } |
Tim Wiederhake | f71accc | 2011-01-19 23:14:33 +0100 | [diff] [blame] | 3012 | |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 3013 | len = read(terminal->master, buffer, sizeof buffer); |
| 3014 | if (len < 0) |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 3015 | terminal_destroy(terminal); |
| 3016 | else |
| 3017 | terminal_data(terminal, buffer, len); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | static int |
| 3021 | terminal_run(struct terminal *terminal, const char *path) |
| 3022 | { |
Kristian Høgsberg | f0c7b20 | 2008-12-12 13:39:03 -0500 | [diff] [blame] | 3023 | int master; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3024 | pid_t pid; |
| 3025 | |
| 3026 | pid = forkpty(&master, NULL, NULL, NULL); |
| 3027 | if (pid == 0) { |
Kristian Høgsberg | de845cf | 2012-06-20 22:14:31 -0400 | [diff] [blame] | 3028 | setenv("TERM", option_term, 1); |
| 3029 | setenv("COLORTERM", option_term, 1); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3030 | if (execl(path, path, NULL)) { |
| 3031 | printf("exec failed: %m\n"); |
| 3032 | exit(EXIT_FAILURE); |
| 3033 | } |
Kristian Høgsberg | f0c7b20 | 2008-12-12 13:39:03 -0500 | [diff] [blame] | 3034 | } else if (pid < 0) { |
| 3035 | fprintf(stderr, "failed to fork and create pty (%m).\n"); |
| 3036 | return -1; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3037 | } |
| 3038 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 3039 | terminal->master = master; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3040 | fcntl(master, F_SETFL, O_NONBLOCK); |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 3041 | terminal->io_task.run = io_handler; |
| 3042 | display_watch_fd(terminal->display, terminal->master, |
| 3043 | EPOLLIN | EPOLLHUP, &terminal->io_task); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3044 | |
Kristian Høgsberg | cdbbae2 | 2014-04-07 11:28:05 -0700 | [diff] [blame] | 3045 | if (option_fullscreen) |
| 3046 | window_set_fullscreen(terminal->window, 1); |
| 3047 | else |
Kristian Høgsberg | 547da5a | 2011-09-13 20:58:00 -0400 | [diff] [blame] | 3048 | terminal_resize(terminal, 80, 24); |
| 3049 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 3050 | return 0; |
| 3051 | } |
| 3052 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 3053 | static const struct weston_option terminal_options[] = { |
| 3054 | { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen }, |
Kristian Høgsberg | 82cd36b | 2012-06-20 15:29:07 -0400 | [diff] [blame] | 3055 | { WESTON_OPTION_STRING, "font", 0, &option_font }, |
Bill Spitzak | 5cad843 | 2014-08-08 12:59:55 -0700 | [diff] [blame] | 3056 | { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size }, |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 3057 | { WESTON_OPTION_STRING, "shell", 0, &option_shell }, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 3058 | }; |
| 3059 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 3060 | int main(int argc, char *argv[]) |
| 3061 | { |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 3062 | struct display *d; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 3063 | struct terminal *terminal; |
Pekka Paalanen | 6c71aae | 2015-03-24 15:56:19 +0200 | [diff] [blame] | 3064 | const char *config_file; |
Kristian Høgsberg | 9c3dee1 | 2013-09-21 22:23:08 -0700 | [diff] [blame] | 3065 | struct weston_config *config; |
| 3066 | struct weston_config_section *s; |
Kristian Høgsberg | 82cd36b | 2012-06-20 15:29:07 -0400 | [diff] [blame] | 3067 | |
Peng Wu | cfcc111 | 2013-08-19 10:59:21 +0800 | [diff] [blame] | 3068 | /* as wcwidth is locale-dependent, |
| 3069 | wcwidth needs setlocale call to function properly. */ |
| 3070 | setlocale(LC_ALL, ""); |
| 3071 | |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 3072 | option_shell = getenv("SHELL"); |
| 3073 | if (!option_shell) |
| 3074 | option_shell = "/bin/bash"; |
| 3075 | |
Pekka Paalanen | 6c71aae | 2015-03-24 15:56:19 +0200 | [diff] [blame] | 3076 | config_file = weston_config_get_name_from_env(); |
| 3077 | config = weston_config_parse(config_file); |
Kristian Høgsberg | 9c3dee1 | 2013-09-21 22:23:08 -0700 | [diff] [blame] | 3078 | s = weston_config_get_section(config, "terminal", NULL, NULL); |
| 3079 | weston_config_section_get_string(s, "font", &option_font, "mono"); |
| 3080 | weston_config_section_get_int(s, "font-size", &option_font_size, 14); |
| 3081 | weston_config_section_get_string(s, "term", &option_term, "xterm"); |
| 3082 | weston_config_destroy(config); |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 3083 | |
Bill Spitzak | 5cad843 | 2014-08-08 12:59:55 -0700 | [diff] [blame] | 3084 | if (parse_options(terminal_options, |
| 3085 | ARRAY_LENGTH(terminal_options), &argc, argv) > 1) { |
| 3086 | printf("Usage: %s [OPTIONS]\n" |
| 3087 | " --fullscreen or -f\n" |
| 3088 | " --font=NAME\n" |
| 3089 | " --font-size=SIZE\n" |
| 3090 | " --shell=NAME\n", argv[0]); |
| 3091 | return 1; |
| 3092 | } |
| 3093 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 3094 | d = display_create(&argc, argv); |
Yuval Fledel | e9f5e36 | 2010-11-22 21:34:19 +0200 | [diff] [blame] | 3095 | if (d == NULL) { |
| 3096 | fprintf(stderr, "failed to create display: %m\n"); |
| 3097 | return -1; |
| 3098 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 3099 | |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 3100 | wl_list_init(&terminal_list); |
Kristian Høgsberg | b7ed4cb | 2012-10-10 11:37:46 -0400 | [diff] [blame] | 3101 | terminal = terminal_create(d); |
Kristian Høgsberg | b21fb9f | 2012-06-20 22:44:03 -0400 | [diff] [blame] | 3102 | if (terminal_run(terminal, option_shell)) |
Kristian Høgsberg | f0c7b20 | 2008-12-12 13:39:03 -0500 | [diff] [blame] | 3103 | exit(EXIT_FAILURE); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 3104 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 3105 | display_run(d); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 3106 | |
| 3107 | return 0; |
| 3108 | } |