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

101 lines
2.6KB

  1. /* Copyright (C) 2018 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <termios.h>
  21. #include <unistd.h>
  22. #include "chat.h"
  23. static bool xterm;
  24. void termInit(void) {
  25. char *term = getenv("TERM");
  26. xterm = term && !strncmp(term, "xterm", 5);
  27. }
  28. void termNoFlow(void) {
  29. struct termios attr;
  30. int error = tcgetattr(STDIN_FILENO, &attr);
  31. if (error) return;
  32. attr.c_iflag &= ~IXON;
  33. attr.c_cc[VDISCARD] = _POSIX_VDISABLE;
  34. tcsetattr(STDIN_FILENO, TCSANOW, &attr);
  35. }
  36. void termTitle(const char *title) {
  37. if (!xterm) return;
  38. printf("\33]0;%s\33\\", title);
  39. fflush(stdout);
  40. }
  41. static void privateMode(const char *mode, bool set) {
  42. printf("\33[?%s%c", mode, (set ? 'h' : 'l'));
  43. fflush(stdout);
  44. }
  45. void termMode(enum TermMode mode, bool set) {
  46. switch (mode) {
  47. break; case TermFocus: privateMode("1004", set);
  48. break; case TermPaste: privateMode("2004", set);
  49. }
  50. }
  51. #define T(s, i) ((s) << 8 | (i))
  52. enum { Esc = '\33' };
  53. enum TermEvent termEvent(char ch) {
  54. static uint state = 0;
  55. switch (T(state, ch)) {
  56. case T(0, Esc): state = 1; return 0;
  57. case T(1, '['): state = 2; return 0;
  58. case T(2, 'I'): state = 0; return TermFocusIn;
  59. case T(2, 'O'): state = 0; return TermFocusOut;
  60. case T(2, '2'): state = 3; return 0;
  61. case T(3, '0'): state = 4; return 0;
  62. case T(4, '0'): state = 5; return 0;
  63. case T(5, '~'): state = 0; return TermPasteStart;
  64. case T(4, '1'): state = 6; return 0;
  65. case T(6, '~'): state = 0; return TermPasteEnd;
  66. default: state = 0; return 0;
  67. }
  68. }
  69. #ifdef TEST
  70. #include <assert.h>
  71. static bool testEvent(const char *str, enum TermEvent event) {
  72. enum TermEvent e = TermNone;
  73. for (size_t i = 0; i < strlen(str); ++i) {
  74. if (e) return false;
  75. e = termEvent(str[i]);
  76. }
  77. return (e == event);
  78. }
  79. int main() {
  80. assert(testEvent("\33[I", TermFocusIn));
  81. assert(testEvent("\33[O", TermFocusOut));
  82. assert(testEvent("\33[200~", TermPasteStart));
  83. assert(testEvent("\33[201~", TermPasteEnd));
  84. }
  85. #endif