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.

term.c 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Copyright (C) 2018 Curtis 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 "chat.h"
  21. static bool xterm;
  22. void termInit(void) {
  23. char *term = getenv("TERM");
  24. xterm = term && !strncmp(term, "xterm", 5);
  25. }
  26. void termTitle(const char *title) {
  27. if (!xterm) return;
  28. printf("\33]0;%s\33\\", title);
  29. fflush(stdout);
  30. }
  31. static void privateMode(const char *mode, bool set) {
  32. printf("\33[?%s%c", mode, (set ? 'h' : 'l'));
  33. fflush(stdout);
  34. }
  35. void termMode(enum TermMode mode, bool set) {
  36. switch (mode) {
  37. break; case TermFocus: privateMode("1004", set);
  38. break; case TermPaste: privateMode("2004", set);
  39. }
  40. }
  41. #define T(s, i) ((s) << 8 | (i))
  42. enum { Esc = '\33' };
  43. enum TermEvent termEvent(char ch) {
  44. static uint state = 0;
  45. switch (T(state, ch)) {
  46. case T(0, Esc): state = 1; return 0;
  47. case T(1, '['): state = 2; return 0;
  48. case T(2, 'I'): state = 0; return TermFocusIn;
  49. case T(2, 'O'): state = 0; return TermFocusOut;
  50. case T(2, '2'): state = 3; return 0;
  51. case T(3, '0'): state = 4; return 0;
  52. case T(4, '0'): state = 5; return 0;
  53. case T(5, '~'): state = 0; return TermPasteStart;
  54. case T(4, '1'): state = 6; return 0;
  55. case T(6, '~'): state = 0; return TermPasteEnd;
  56. default: state = 0; return 0;
  57. }
  58. }
  59. #ifdef TEST
  60. #include <assert.h>
  61. static bool testEvent(const char *str, enum TermEvent event) {
  62. enum TermEvent e = TermNone;
  63. for (size_t i = 0; i < strlen(str); ++i) {
  64. if (e) return false;
  65. e = termEvent(str[i]);
  66. }
  67. return (e == event);
  68. }
  69. int main() {
  70. assert(testEvent("\33[I", TermFocusIn));
  71. assert(testEvent("\33[O", TermFocusOut));
  72. assert(testEvent("\33[200~", TermPasteStart));
  73. assert(testEvent("\33[201~", TermPasteEnd));
  74. }
  75. #endif