1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
|
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
#include "ui_display.h"
#include <stdio.h> /* printf */
#include <unistd.h> /* STDIN_FILENO */
#include <fcntl.h> /* fcntl */
#include <sys/ioctl.h> /* ioctl */
#include <string.h> /* memset/memcpy */
#include <stdlib.h> /* getenv */
#include <termios.h>
#include <signal.h>
#include <time.h> /* time */
#include <sys/time.h> /* gettimeofday */
#include <errno.h>
#include <sys/select.h> /* select */
#include <pobl/bl_def.h> /* HAVE_GETTIMEOFDAY */
#include <pobl/bl_debug.h>
#include <pobl/bl_privilege.h> /* bl_priv_change_e(u|g)id */
#include <pobl/bl_unistd.h> /* bl_getuid */
#include <pobl/bl_file.h>
#include <pobl/bl_mem.h> /* alloca */
#include <pobl/bl_conf_io.h>
#include <pobl/bl_net.h>
#include <pobl/bl_util.h>
#include <vt_color.h>
#ifdef __linux__
#include <linux/keyboard.h>
#endif
#ifdef USE_LIBSIXEL
#include <sixel.h>
#endif
#include "../ui_window.h"
#include "../ui_picture.h"
/* --- static variables --- */
static ui_display_t **displays;
static u_int num_displays;
static struct termios orig_tm;
static vt_char_encoding_t encoding = VT_UTF8;
static u_int default_col_width = 8;
static u_int default_line_height = 16;
#ifdef USE_LIBSIXEL
static int dither_id = BUILTIN_XTERM256;
static int check_xtcolreg;
#endif
/* --- static functions --- */
static void set_blocking(int fd, int block) {
fcntl(fd, F_SETFL,
block ? (fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK) : (fcntl(fd, F_GETFL, 0) | O_NONBLOCK));
}
static int set_winsize(ui_display_t *disp, char *seq) {
struct winsize ws;
memset(&ws, 0, sizeof(ws));
if (seq) {
int col;
int row;
int x;
int y;
if (sscanf(seq, "8;%d;%d;4;%d;%dt", &row, &col, &y, &x) != 4) {
return 0;
}
ws.ws_col = col;
ws.ws_row = row;
disp->width = x;
disp->height = y;
} else {
if (ioctl(fileno(disp->display->fp), TIOCGWINSZ, &ws) == 0) {
disp->width = ws.ws_xpixel;
disp->height = ws.ws_ypixel;
}
}
if (ws.ws_col == 0) {
bl_error_printf("winsize.ws_col is 0\n");
ws.ws_col = 80;
}
if (ws.ws_row == 0) {
bl_error_printf("winsize.ws_row is 0\n");
ws.ws_row = 24;
}
if (disp->width == 0) {
disp->width = ws.ws_col * default_col_width;
disp->display->col_width = default_col_width;
} else {
disp->display->col_width = disp->width / ws.ws_col;
disp->width = disp->display->col_width * ws.ws_col;
}
if (disp->height == 0) {
disp->height = ws.ws_row * default_line_height;
disp->display->line_height = default_line_height;
} else {
disp->display->line_height = disp->height / ws.ws_row;
disp->height = disp->display->line_height * ws.ws_row;
}
bl_msg_printf("Screen is %dx%d (Cell %dx%d)\n", disp->width / disp->display->col_width,
disp->height / disp->display->line_height, disp->display->col_width,
disp->display->line_height);
return 1;
}
/* XXX */
int ui_font_cache_unload_all(void);
static void sig_winch(int sig) {
u_int count;
set_winsize(displays[0], NULL);
/* XXX */
ui_font_cache_unload_all();
for (count = 0; count < displays[0]->num_roots; count++) {
ui_window_resize_with_margin(displays[0]->roots[count], displays[0]->width, displays[0]->height,
NOTIFY_TO_MYSELF);
}
signal(SIGWINCH, sig_winch);
}
static void init_console(int fd) {
#ifdef USE_LIBSIXEL
/*
* The response is parsed in receive_stdin_event() or ui_display_output_picture().
* (To reflect it to wall picture, ui_display_output_picture() parses it.)
*/
write(fd, "\x1b[?1;1;0S", 9);
check_xtcolreg = -1;
#endif
/* The response is parsed in receive_stdin_event(). */
write(fd, "\x1b[>c", 4);
write(fd, "\x1b[?25l", 6);
write(fd, "\x1b[>4;2m", 7);
write(fd, "\x1b[?1002h\x1b[?1006h\x1b[?8452h", 24);
}
static ui_display_t *open_display_socket(int fd) {
void *p;
if (!(p = realloc(displays, sizeof(ui_display_t *) * (num_displays + 1)))) {
return NULL;
}
displays = p;
if (!(displays[num_displays] = calloc(1, sizeof(ui_display_t))) ||
!(displays[num_displays]->display = calloc(1, sizeof(Display)))) {
free(displays[num_displays]);
return NULL;
}
if (!(displays[num_displays]->display->fp = fdopen(fd, "w"))) {
free(displays[num_displays]->display);
free(displays[num_displays]);
return NULL;
}
/*
* Set the close-on-exec flag.
* If this flag off, this fd remained open until the child process forked in
* open_screen_intern()(vt_term_open_pty()) close it.
*/
bl_file_set_cloexec(fd);
set_blocking(fd, 1);
init_console(fd);
displays[num_displays]->display->conv = vt_char_encoding_conv_new(encoding);
vt_char_encoding_conv_set_use_loose_rule(displays[num_displays]->display->conv, encoding, 1);
set_winsize(displays[num_displays], "8;24;80;4;384;640t");
return displays[num_displays++];
}
static ui_display_t *open_display_console(void) {
void *p;
struct termios tio;
int fd;
if (num_displays > 0 || !isatty(STDIN_FILENO) ||
!(p = realloc(displays, sizeof(ui_display_t *)))) {
return NULL;
}
displays = p;
if (!(displays[0] = calloc(1, sizeof(ui_display_t))) ||
!(displays[0]->display = calloc(1, sizeof(Display)))) {
free(displays[0]);
return NULL;
}
tcgetattr(STDIN_FILENO, &orig_tm);
if (!(displays[0]->display->fp = fopen(ttyname(STDIN_FILENO), "r+"))) {
free(displays[0]->display);
free(displays[0]);
return NULL;
}
fd = fileno(displays[0]->display->fp);
bl_file_set_cloexec(fd);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
init_console(fd);
tio = orig_tm;
#ifdef IMAXBEL
tio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP);
#else
/* IMAXBEL is not defined on HaikuOS */
tio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | ISTRIP);
#endif
tio.c_iflag |= IGNBRK;
tio.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONLRET);
#ifdef ECHOPRT
tio.c_lflag &=
~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ECHOCTL | ISIG);
#else
/* ECHOPRT is not defined on cygwin. */
tio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOKE | ECHOCTL | ISIG);
#endif
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &tio);
displays[0]->display->conv = vt_char_encoding_conv_new(encoding);
vt_char_encoding_conv_set_use_loose_rule(displays[0]->display->conv, encoding, 1);
set_winsize(displays[0], NULL);
signal(SIGWINCH, sig_winch);
return displays[num_displays++];
}
#ifdef __linux__
static int get_key_state(void) {
int ret;
char state;
state = 6;
ret = ioctl(STDIN_FILENO, TIOCLINUX, &state);
if (ret == -1) {
return 0;
} else {
/* ShiftMask and ControlMask is the same. */
return state | ((state & (1 << KG_ALT)) ? Mod1Mask : 0);
}
}
#else
#define get_key_state() (0)
#endif
static inline ui_window_t *get_window_intern(ui_window_t *win, int x, int y) {
u_int count;
for (count = 0; count < win->num_children; count++) {
ui_window_t *child;
if ((child = win->children[count])->is_mapped) {
if (child->x <= x && x < child->x + ACTUAL_WIDTH(child) && child->y <= y &&
y < child->y + ACTUAL_HEIGHT(child)) {
return get_window_intern(child, x - child->x, y - child->y);
}
}
}
return win;
}
/*
* disp->roots[1] is ignored.
*/
static inline ui_window_t *get_window(ui_display_t *disp, int x, /* X in display */
int y /* Y in display */
) {
return get_window_intern(disp->roots[0], x, y);
}
/* XXX defined in console/ui_window.c */
void ui_window_clear_margin_area(ui_window_t *win);
static void expose_window(ui_window_t *win, int x, int y, u_int width, u_int height) {
if (x + width <= win->x || win->x + ACTUAL_WIDTH(win) < x || y + height <= win->y ||
win->y + ACTUAL_HEIGHT(win) < y) {
return;
}
if (x < win->x + win->hmargin || y < win->y + win->vmargin ||
x - win->x + width > win->hmargin + win->width ||
y - win->y + height > win->vmargin + win->height) {
ui_window_clear_margin_area(win);
}
if (win->window_exposed) {
if (x < win->x + win->hmargin) {
width -= (win->x + win->hmargin - x);
x = 0;
} else {
x -= (win->x + win->hmargin);
}
if (y < win->y + win->vmargin) {
height -= (win->y + win->vmargin - y);
y = 0;
} else {
y -= (win->y + win->vmargin);
}
(*win->window_exposed)(win, x, y, width, height);
}
}
static void expose_display(ui_display_t *disp, int x, int y, u_int width, u_int height) {
u_int count;
/*
* XXX
* ui_im_{status|candidate}_screen can exceed display width or height,
* because ui_im_{status|candidate}_screen_new() shows screen at
* non-adjusted position.
*/
if (x + width > disp->width) {
width = disp->width - x;
}
if (y + height > disp->height) {
height = disp->height - y;
}
expose_window(disp->roots[0], x, y, width, height);
for (count = 0; count < disp->roots[0]->num_children; count++) {
expose_window(disp->roots[0]->children[count], x, y, width, height);
}
}
static int check_visibility_of_im_window(ui_display_t *disp) {
static struct {
int saved;
int x;
int y;
u_int width;
u_int height;
} im_region;
int redraw_im_win = 0;
#ifdef DEBUG
if (disp->num_roots > 2) {
bl_debug_printf(BL_DEBUG_TAG" Multiple IM Windows (%d) are activated.\n",
disp->num_roots - 1);
}
#endif
if (IM_WINDOW_IS_ACTIVATED(disp)) {
if (im_region.saved) {
if (im_region.x == disp->roots[1]->x && im_region.y == disp->roots[1]->y &&
im_region.width == ACTUAL_WIDTH(disp->roots[1]) &&
im_region.height == ACTUAL_HEIGHT(disp->roots[1])) {
return 0;
}
if (im_region.x < disp->roots[1]->x || im_region.y < disp->roots[1]->y ||
im_region.x + im_region.width > disp->roots[1]->x + ACTUAL_WIDTH(disp->roots[1]) ||
im_region.y + im_region.height > disp->roots[1]->y + ACTUAL_HEIGHT(disp->roots[1])) {
expose_display(disp, im_region.x, im_region.y, im_region.width, im_region.height);
redraw_im_win = 1;
}
}
im_region.saved = 1;
im_region.x = disp->roots[1]->x;
im_region.y = disp->roots[1]->y;
im_region.width = ACTUAL_WIDTH(disp->roots[1]);
im_region.height = ACTUAL_HEIGHT(disp->roots[1]);
} else {
if (im_region.saved) {
expose_display(disp, im_region.x, im_region.y, im_region.width, im_region.height);
im_region.saved = 0;
}
}
return redraw_im_win;
}
static void receive_event_for_multi_roots(ui_display_t *disp, XEvent *xev) {
int redraw_im_win;
if ((redraw_im_win = check_visibility_of_im_window(disp))) {
/* Stop drawing input method window */
disp->roots[1]->is_mapped = 0;
}
ui_window_receive_event(disp->roots[0], xev);
if (redraw_im_win && disp->num_roots == 2) {
/* Restart drawing input method window */
disp->roots[1]->is_mapped = 1;
} else if (!check_visibility_of_im_window(disp)) {
return;
}
expose_window(disp->roots[1], disp->roots[1]->x, disp->roots[1]->y, ACTUAL_WIDTH(disp->roots[1]),
ACTUAL_HEIGHT(disp->roots[1]));
}
static int receive_stdin(Display *display) {
ssize_t len;
if ((len = read(fileno(display->fp), display->buf + display->buf_len,
sizeof(display->buf) - display->buf_len - 1)) > 0) {
display->buf_len += len;
display->buf[display->buf_len] = '\0';
return 1;
} else {
return 0;
}
}
static u_char *skip_range(u_char *seq, int beg, int end) {
while (beg <= *seq && *seq <= end) {
seq++;
}
return seq;
}
static int parse(u_char **param, u_char **intermed, u_char **ft, u_char *seq) {
*param = seq;
if ((*intermed = skip_range(*param, 0x30, 0x3f)) == *param) {
*param = NULL;
}
if ((*ft = skip_range(*intermed, 0x20, 0x2f)) == *intermed) {
*intermed = NULL;
}
if (0x40 <= **ft && **ft <= 0x7e) {
return 1;
} else {
return 0;
}
}
static int parse_modify_other_keys(XKeyEvent *kev, const char *param, const char *format,
int key_mod_order) {
int key;
int modcode;
if (sscanf(param, format, &key, &modcode) == 2) {
if (!key_mod_order) {
int tmp;
tmp = key;
key = modcode;
modcode = tmp;
}
kev->ksym = key;
modcode--;
if (modcode & 1) {
kev->state |= ShiftMask;
}
if (modcode & (2 | 8)) {
kev->state |= Mod1Mask;
}
if (modcode & 4) {
kev->state |= ControlMask;
}
return 1;
} else {
return 0;
}
}
#ifdef USE_LIBSIXEL
static void parse_xtcolreg(ui_display_t *disp, u_char *param) {
int pi;
int pa;
int pv;
if (sscanf(param, "%d;%d;%dS", &pi, &pa, &pv) == 3) {
check_xtcolreg = 1;
if (pv == 16) {
ui_display_set_sixel_colors(disp, "16");
}
}
}
#endif
/* Same as fb/ui_display */
static int receive_stdin_event(ui_display_t *disp) {
u_char *p;
if (!receive_stdin(disp->display)) {
u_int count;
for (count = disp->num_roots; count > 0; count--) {
if (disp->roots[count - 1]->window_destroyed) {
(*disp->roots[count - 1]->window_destroyed)(disp->roots[count - 1]);
}
}
return 0;
}
p = disp->display->buf;
while (p - disp->display->buf < disp->display->buf_len) {
XKeyEvent kev;
u_char *param;
u_char *intermed;
u_char *ft;
kev.type = KeyPress;
kev.state = get_key_state();
kev.ksym = 0;
kev.keycode = 0;
if (*p == '\x1b' && p[1] == '\x0') {
fd_set fds;
struct timeval tv;
FD_ZERO(&fds);
FD_SET(fileno(disp->display->fp), &fds);
tv.tv_usec = 50000; /* 0.05 sec */
tv.tv_sec = 0;
if (select(fileno(disp->display->fp) + 1, &fds, NULL, NULL, &tv) == 1) {
receive_stdin(disp->display);
}
}
if (*p == '\x1b' && (p[1] == '[' || p[1] == 'O')) {
if (!parse(¶m, &intermed, &ft, p + 2)) {
set_blocking(fileno(disp->display->fp), 0);
if (!receive_stdin(disp->display)) {
break;
}
continue;
}
if (p[1] == '[') {
p = ft + 1;
if (*ft == '~') {
if (!param || intermed) {
continue;
} else if (!parse_modify_other_keys(&kev, param, "27;%d;%d~", 0)) {
switch (atoi(param)) {
case 2:
kev.ksym = XK_Insert;
break;
case 3:
kev.ksym = XK_Delete;
break;
case 5:
kev.ksym = XK_Prior;
break;
case 6:
kev.ksym = XK_Next;
break;
case 7:
kev.ksym = XK_Home;
break;
case 8:
kev.ksym = XK_End;
break;
case 17:
kev.ksym = XK_F6;
break;
case 18:
kev.ksym = XK_F7;
break;
case 19:
kev.ksym = XK_F8;
break;
case 20:
kev.ksym = XK_F9;
break;
case 21:
kev.ksym = XK_F10;
break;
case 23:
kev.ksym = XK_F11;
break;
case 24:
kev.ksym = XK_F12;
break;
default:
continue;
}
}
} else if (*ft == 'M' || *ft == 'm') {
XButtonEvent bev;
int state;
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
#endif
ui_window_t *win;
if (*ft == 'M') {
if (disp->display->is_pressing) {
bev.type = MotionNotify;
} else {
bev.type = ButtonPress;
disp->display->is_pressing = 1;
}
if (!param) {
state = *(p++) - 0x20;
bev.x = *(p++) - 0x20;
bev.y = *(p++) - 0x20;
goto make_event;
}
} else {
bev.type = ButtonRelease;
disp->display->is_pressing = 0;
}
*ft = '\0';
if (!param || sscanf(param, "<%d;%d;%d", &state, &bev.x, &bev.y) != 3) {
continue;
}
make_event:
bev.button = (state & 0x2) + 1;
if (bev.type == MotionNotify) {
bev.state = Button1Mask << (bev.button - 1);
} else {
bev.state = 0;
}
bev.x--;
bev.x *= disp->display->col_width;
bev.y--;
bev.y *= disp->display->line_height;
win = get_window(disp, bev.x, bev.y);
bev.x -= win->x;
bev.y -= win->y;
#ifdef HAVE_GETTIMEOFDAY
gettimeofday(&tv, NULL);
bev.time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#else
bev.time = time(NULL) * 1000;
#endif
set_blocking(fileno(disp->display->fp), 1);
ui_window_receive_event(win, (XEvent*)&bev);
set_blocking(fileno(disp->display->fp), 0);
continue;
} else if (param && set_winsize(disp, param)) {
u_int count;
/* XXX */
ui_font_cache_unload_all();
for (count = 0; count < disp->num_roots; count++) {
ui_window_resize_with_margin(disp->roots[count], disp->width, disp->height,
NOTIFY_TO_MYSELF);
}
continue;
} else if (param && *ft == 'u') {
if (!parse_modify_other_keys(&kev, param, "%d;%du", 1)) {
continue;
}
} else if (param && /* (*param == '>' || *param == '?') && */ *ft == 'c') {
int pp;
int pv;
int pc;
/*
* iTerm2 2.0.0.20141103: CSI>0;95;c
* MacOSX Terminal 2.1.2: CSI?1;2c
* mlterm: CSI>24;279;0c
*/
if (sscanf(param + 1, "%d;%d;%dc", &pp, &pv, &pc) >= 2 &&
(pp >= 41 /* VT420 or later */ ||
/*
* xterm 279 or later supports DECSLRM/DECLRMM
* but mlterm 3.7.1 or before which supports it
* responses 277.
*/
pv >= 277)) {
disp->display->support_hmargin = 1;
} else {
bl_msg_printf("Slow scrolling on split screens\n");
disp->display->support_hmargin = 0;
}
continue;
}
#ifdef USE_LIBSIXEL
else if (param && *param == '?' && *ft == 'S') {
parse_xtcolreg(disp, param + 1);
continue;
}
#endif
else if ('P' <= *ft && *ft <= 'S') {
kev.ksym = XK_F1 + (*ft - 'P');
}
#ifdef __FreeBSD__
else if ('Y' <= *ft && *ft <= 'Z') {
kev.ksym = XK_F1 + (*ft - 'Y');
kev.state = ShiftMask;
} else if ('a' <= *ft && *ft <= 'j') {
kev.ksym = XK_F3 + (*ft - 'a');
kev.state = ShiftMask;
} else if ('k' <= *ft && *ft <= 'v') {
kev.ksym = XK_F1 + (*ft - 'k');
kev.state = ControlMask;
} else if ('w' <= *ft && *ft <= 'z') {
kev.ksym = XK_F1 + (*ft - 'w');
kev.state = ControlMask | ShiftMask;
} else if (*ft == '@') {
kev.ksym = XK_F5;
kev.state = ControlMask | ShiftMask;
} else if ('[' <= *ft && *ft <= '\`') {
kev.ksym = XK_F6 + (*ft - '[');
kev.state = ControlMask | ShiftMask;
} else if (*ft == '{') {
kev.ksym = XK_F12;
kev.state = ControlMask | ShiftMask;
}
#endif
else {
switch (*ft) {
case 'A':
kev.ksym = XK_Up;
break;
case 'B':
kev.ksym = XK_Down;
break;
case 'C':
kev.ksym = XK_Right;
break;
case 'D':
kev.ksym = XK_Left;
break;
case 'F':
kev.ksym = XK_End;
break;
case 'H':
kev.ksym = XK_Home;
break;
default:
continue;
}
}
if (param) {
u_char *tmp;
if ((tmp = strchr(param, ';'))) {
param = tmp + 1;
}
}
} else /* if( p[1] == 'O') */ {
p = ft + 1;
switch (*ft) {
case 'P':
kev.ksym = XK_F1;
break;
case 'Q':
kev.ksym = XK_F2;
break;
case 'R':
kev.ksym = XK_F3;
break;
case 'S':
kev.ksym = XK_F4;
break;
default:
continue;
}
}
if (param && '1' <= *param && *param <= '9') {
int state;
state = atoi(param) - 1;
if (state & 0x1) {
kev.state |= ShiftMask;
}
if (state & 0x2) {
kev.state |= Mod1Mask;
}
if (state & 0x4) {
kev.state |= ControlMask;
}
}
} else {
kev.ksym = *(p++);
/* XXX */
if (kev.ksym == 0x7f && orig_tm.c_cc[VERASE] == 0x7f) {
/* Convert to BackSpace */
kev.ksym = 0x08;
}
if ((u_int)kev.ksym <= 0x1f) {
if (kev.ksym == '\0') {
/* CTL+' ' instead of CTL+@ */
kev.ksym = ' ';
} else if (0x01 <= kev.ksym && kev.ksym <= 0x1a) {
/* Lower case alphabets instead of upper ones. */
kev.ksym = kev.ksym + 0x60;
} else {
kev.ksym = kev.ksym + 0x40;
}
kev.state = ControlMask;
} else if ('A' <= kev.ksym && kev.ksym <= 'Z') {
kev.state = ShiftMask;
}
}
set_blocking(fileno(disp->display->fp), 1);
receive_event_for_multi_roots(disp, (XEvent*)&kev);
set_blocking(fileno(disp->display->fp), 0);
}
if ((disp->display->buf_len = disp->display->buf + disp->display->buf_len - p) > 0) {
memcpy(disp->display->buf, p, disp->display->buf_len + 1);
}
set_blocking(fileno(disp->display->fp), 1);
return 1;
}
/* --- global functions --- */
ui_display_t *ui_display_open(char *disp_name, u_int depth) {
ui_display_t *disp;
if (disp_name && strncmp(disp_name, "client:", 7) == 0) {
disp = open_display_socket(atoi(disp_name + 7));
} else if (!displays) {
disp = open_display_console();
} else {
return displays[0];
}
if (disp) {
if (!(disp->name = getenv("DISPLAY"))) {
disp->name = ":0.0";
}
}
return disp;
}
void ui_display_close(ui_display_t *disp) {
u_int count;
/* inline pictures are alive until vt_term_t is destroyed. */
#if 0
ui_picture_display_closed(disp->display);
#endif
if (isatty(fileno(disp->display->fp))) {
tcsetattr(fileno(disp->display->fp), TCSAFLUSH, &orig_tm);
signal(SIGWINCH, SIG_IGN);
}
write(fileno(disp->display->fp), "\x1b[?25h", 6);
write(fileno(disp->display->fp), "\x1b[>4;0m", 7);
write(fileno(disp->display->fp), "\x1b[?1002l\x1b[?1006l\x1b[?8452l", 24);
fclose(disp->display->fp);
(*disp->display->conv->destroy)(disp->display->conv);
for (count = 0; count < num_displays; count++) {
if (displays[count] == disp) {
memcpy(displays + count, displays + count + 1,
sizeof(ui_display_t *) * (num_displays - count - 1));
num_displays--;
break;
}
}
}
void ui_display_close_all(void) {
u_int count;
for (count = num_displays; count > 0; count--) {
ui_display_close(displays[count - 1]);
}
free(displays);
displays = NULL;
}
ui_display_t **ui_get_opened_displays(u_int *num) {
*num = num_displays;
return displays;
}
int ui_display_fd(ui_display_t *disp) { return fileno(disp->display->fp); }
int ui_display_show_root(ui_display_t *disp, ui_window_t *root, int x, int y, int hint,
char *app_name, char *wm_role, Window parent_window /* Ignored */
) {
void *p;
if ((p = realloc(disp->roots, sizeof(ui_window_t *) * (disp->num_roots + 1))) == NULL) {
#ifdef DEBUG
bl_warn_printf(BL_DEBUG_TAG " realloc failed.\n");
#endif
return 0;
}
disp->roots = p;
root->disp = disp;
root->parent = NULL;
root->parent_window = disp->my_window;
root->gc = disp->gc;
root->x = x;
root->y = y;
if (app_name) {
root->app_name = app_name;
}
disp->roots[disp->num_roots++] = root;
/* Cursor is drawn internally by calling ui_display_put_image(). */
if (!ui_window_show(root, hint)) {
return 0;
}
return 1;
}
int ui_display_remove_root(ui_display_t *disp, ui_window_t *root) {
u_int count;
for (count = 0; count < disp->num_roots; count++) {
if (disp->roots[count] == root) {
/* XXX ui_window_unmap resizes all windows internally. */
#if 0
ui_window_unmap(root);
#endif
ui_window_final(root);
disp->num_roots--;
if (count == disp->num_roots) {
disp->roots[count] = NULL;
} else {
disp->roots[count] = disp->roots[disp->num_roots];
}
return 1;
}
}
return 0;
}
void ui_display_idling(ui_display_t *disp) {
u_int count;
for (count = 0; count < disp->num_roots; count++) {
ui_window_idling(disp->roots[count]);
}
}
int ui_display_receive_next_event(ui_display_t *disp) { return receive_stdin_event(disp); }
/*
* Folloing functions called from ui_window.c
*/
int ui_display_own_selection(ui_display_t *disp, ui_window_t *win) {
if (disp->selection_owner) {
ui_display_clear_selection(NULL, NULL);
}
disp->selection_owner = win;
return 1;
}
int ui_display_clear_selection(ui_display_t *disp, /* NULL means all selection owner windows. */
ui_window_t *win) {
if (disp == NULL) {
u_int count;
for (count = 0; count < num_displays; count++) {
ui_display_clear_selection(displays[count], displays[count]->selection_owner);
}
return 1;
}
if (disp->selection_owner == NULL || disp->selection_owner != win) {
return 0;
}
if (disp->selection_owner->selection_cleared) {
(*disp->selection_owner->selection_cleared)(disp->selection_owner);
}
disp->selection_owner = NULL;
return 1;
}
XModifierKeymap *ui_display_get_modifier_mapping(ui_display_t *disp) { return disp->modmap.map; }
void ui_display_update_modifier_mapping(ui_display_t *disp, u_int serial) { /* dummy */ }
XID ui_display_get_group_leader(ui_display_t *disp) { return None; }
/* XXX for input method window */
void ui_display_reset_input_method_window(void) {
#if 0
if (IM_WINDOW_IS_ACTIVATED(displays[0]))
#endif
{
check_visibility_of_im_window(displays[0]);
ui_window_clear_margin_area(displays[0]->roots[1]);
}
}
void ui_display_set_char_encoding(ui_display_t *disp, vt_char_encoding_t e) {
encoding = e;
if (disp) {
(*disp->display->conv->destroy)(disp->display->conv);
disp->display->conv = vt_char_encoding_conv_new(encoding);
vt_char_encoding_conv_set_use_loose_rule(disp->display->conv, encoding, 1);
}
}
void ui_display_set_default_cell_size(u_int width, u_int height) {
default_col_width = width;
default_line_height = height;
}
#ifdef USE_LIBSIXEL
void ui_display_set_sixel_colors(ui_display_t *disp, const char *colors) {
int old_dither_id = dither_id;
if (strcmp(colors, "16") == 0) {
dither_id = BUILTIN_XTERM16;
} else if (strcmp(colors, "full") == 0) {
dither_id = -1;
} else {
dither_id = BUILTIN_XTERM256;
}
if (disp) {
if (disp->display->sixel_dither) {
if (old_dither_id == dither_id) {
return;
}
sixel_dither_destroy(disp->display->sixel_dither);
}
if (dither_id == -1) {
disp->display->sixel_dither = sixel_dither_create(-1);
} else {
disp->display->sixel_dither = sixel_dither_get(dither_id);
}
/* See modify_pixmap() (which reorder ARGB bytes by RGBA) in ui_imagelib.c */
sixel_dither_set_pixelformat(disp->display->sixel_dither, PIXELFORMAT_RGBA8888);
}
}
static int callback(char *data, int size, void *out) { return fwrite(data, 1, size, out); }
void ui_display_output_picture(ui_display_t *disp, u_char *picture, u_int width, u_int height) {
if (!disp->display->sixel_output) {
disp->display->sixel_output = sixel_output_create(callback, disp->display->fp);
}
if (check_xtcolreg == -1) {
/*
* Don't call receive_stdin_event() which may have called this function itself.
* (receive_stdin_event() -> set_winsize() -> ui_display_output_picture().
*/
if (disp->display->buf_len > 0 || receive_stdin(disp->display)) {
char *p;
for (p = disp->display->buf; (p = strchr(p, '\x1b')); p ++) {
if (p[1] == '[' && p[2] == '?' && strchr(p, 'S')) {
parse_xtcolreg(disp, p + 3);
break;
}
}
}
}
if (!disp->display->sixel_dither) {
if (dither_id == -1) {
disp->display->sixel_dither = sixel_dither_create(-1);
} else {
disp->display->sixel_dither = sixel_dither_get(dither_id);
}
}
sixel_dither_set_pixelformat(disp->display->sixel_dither, PIXELFORMAT_RGBA8888);
sixel_encode(picture, width, height, 4, disp->display->sixel_dither, disp->display->sixel_output);
}
#endif
|