terminal.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Igor van den Hoven 2006 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. #ifdef HAVE_SYS_IOCTL_H
  27. #include <sys/ioctl.h>
  28. #endif
  29. #include <termios.h>
  30. void init_terminal(struct session *ses)
  31. {
  32. struct termios io;
  33. if (tcgetattr(0, &gtd->old_terminal))
  34. {
  35. syserr_fatal(-1, "init_terminal: tcgetattr 1");
  36. }
  37. io = gtd->old_terminal;
  38. /*
  39. Canonical mode off
  40. */
  41. DEL_BIT(io.c_lflag, ICANON);
  42. io.c_cc[VMIN] = 1;
  43. io.c_cc[VTIME] = 0;
  44. io.c_cc[VSTART] = 255;
  45. io.c_cc[VSTOP] = 255;
  46. io.c_cc[VINTR] = 4; // ctrl-d
  47. /*
  48. Make the terminalal as raw as possible
  49. */
  50. /*
  51. DEL_BIT(io.c_iflag, IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  52. DEL_BIT(io.c_oflag, OPOST);
  53. DEL_BIT(io.c_cflag, CSIZE|PARENB);
  54. */
  55. DEL_BIT(io.c_lflag, ECHO|ECHONL|IEXTEN|ISIG);
  56. // DEL_BIT(io.c_lflag, ECHO|ECHONL|IEXTEN|ISIG);
  57. SET_BIT(io.c_cflag, CS8);
  58. if (tcsetattr(0, TCSANOW, &io))
  59. {
  60. syserr_printf(ses, "init_terminal: tcsetattr");
  61. }
  62. if (tcgetattr(0, &gts->cur_terminal))
  63. {
  64. syserr_fatal(-1, "init_terminal: tcgetattr 2");
  65. }
  66. }
  67. void reset_terminal(struct session *ses)
  68. {
  69. if (tcsetattr(0, TCSANOW, &gtd->old_terminal))
  70. {
  71. syserr_printf(ses, "reset_terminal: tcsetattr");
  72. }
  73. printf("\e[?1000l\e[?1002l\e[?1004l\e[?1006l");
  74. }
  75. void save_session_terminal(struct session *ses)
  76. {
  77. tcgetattr(0, &ses->cur_terminal);
  78. }
  79. void refresh_session_terminal(struct session *ses)
  80. {
  81. // tcsetattr(0, TCSANOW, &ses->cur_terminal);
  82. }
  83. void echo_off(struct session *ses)
  84. {
  85. struct termios io;
  86. tcgetattr(STDIN_FILENO, &io);
  87. DEL_BIT(io.c_lflag, ECHO|ECHONL);
  88. tcsetattr(STDIN_FILENO, TCSADRAIN, &io);
  89. }
  90. void echo_on(struct session *ses)
  91. {
  92. struct termios io;
  93. tcgetattr(STDIN_FILENO, &io);
  94. SET_BIT(io.c_lflag, ECHO|ECHONL);
  95. tcsetattr(STDIN_FILENO, TCSADRAIN, &io);
  96. }
  97. void init_terminal_size(struct session *ses)
  98. {
  99. struct winsize screen;
  100. push_call("init_terminal_size(%p)",ses);
  101. if (ses == gts)
  102. {
  103. if (ioctl(0, TIOCGWINSZ, &screen) == -1)
  104. {
  105. init_screen(SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT * 16, SCREEN_WIDTH * 10);
  106. }
  107. else
  108. {
  109. init_screen(screen.ws_row, screen.ws_col, screen.ws_ypixel, screen.ws_xpixel);
  110. }
  111. SET_BIT(gtd->flags, TINTIN_FLAG_RESETBUFFER);
  112. }
  113. if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  114. {
  115. init_split(ses, 1 + ses->top_split, gtd->screen->rows - 1 - ses->bot_split);
  116. }
  117. else
  118. {
  119. ses->top_row = 1;
  120. ses->bot_row = gtd->screen->rows;
  121. }
  122. check_all_events(ses, SUB_ARG, 0, 4, "SCREEN RESIZE", ntos(gtd->screen->rows), ntos(gtd->screen->cols), ntos(gtd->screen->height), ntos(gtd->screen->width));
  123. msdp_update_all("SCREEN_ROWS", "%d", gtd->screen->rows);
  124. msdp_update_all("SCREEN_COLS", "%d", gtd->screen->cols);
  125. msdp_update_all("SCREEN_HEIGHT", "%d", gtd->screen->height);
  126. msdp_update_all("SCREEN_WIDTH", "%d", gtd->screen->width);
  127. pop_call();
  128. return;
  129. }
  130. int get_scroll_size(struct session *ses)
  131. {
  132. return (ses->bot_row - ses->top_row);
  133. }