Monitoring du Phytotron
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.

terminal.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * emulateur de terminal
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include <sys/select.h>
  13. #include <ncurses.h>
  14. #include "../serial/serial.h"
  15. #include "../viz/curses/ecran.h"
  16. #include "terminal.h"
  17. extern int verbosity;
  18. int special_dumper(FILE *fp, unsigned char octet);
  19. /* ---------------------------------------------------------------- */
  20. static int interactive(WINDOW *glass, int fd_local, int fd_remote)
  21. {
  22. int flag_exit = 0;
  23. long tstart, tcur;
  24. char ligne[100];
  25. int received;
  26. /* --- variables for select */
  27. struct timeval tv;
  28. fd_set rfds;
  29. int retval,mfd;
  30. #if DEBUG_LEVEL
  31. fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__,
  32. glass, fd_local, fd_remote);
  33. #endif
  34. tstart = time(NULL);
  35. wclear(glass); wrefresh(glass);
  36. mfd = (fd_local>fd_remote ? fd_local : fd_remote) + 1; /* XXX */
  37. #if DEBUG_LEVEL > 1
  38. sprintf(ligne, "%s : mfd is %d\n", __func__, mfd);
  39. waddstr(glass, ligne); wrefresh(glass);
  40. #endif
  41. do {
  42. tcur = time(NULL);
  43. FD_ZERO(&rfds);
  44. FD_SET(fd_local, &rfds); /* stdin */
  45. FD_SET(fd_remote, &rfds); /* teletype */
  46. tv.tv_sec = tv.tv_usec = 0; /* no timeout */
  47. retval = select(mfd, &rfds, NULL, NULL, &tv);
  48. if (-1 == retval) {
  49. sprintf(ligne, "err select %s\n", strerror(errno));
  50. waddstr(glass, ligne); wrefresh(glass);
  51. continue;
  52. }
  53. /*** est-ce la liaison serie ? */
  54. if (FD_ISSET(fd_remote, &rfds)) {
  55. /* get the incoming byte */
  56. received = getbyte(fd_remote);
  57. if (verbosity) {
  58. special_dumper(stderr, received);
  59. fflush(stderr);
  60. }
  61. if ( -1 == received) { /* anti ddos */
  62. sleep(1);
  63. waddstr(glass, " lost connection ?\n");
  64. }
  65. if ('\r' != received) waddch(glass, received);
  66. }
  67. /*** est-ce le yuser avec son clavier ? */
  68. if (FD_ISSET(fd_local, &rfds)) {
  69. noecho();
  70. received = wgetch(glass);
  71. echo();
  72. #if DEBUG_LEVEL > 1
  73. sprintf(ligne, " got $%X '%c'\n", received,
  74. isprint(received) ? received : '?');
  75. waddstr(glass, ligne); wrefresh(glass);
  76. sleep(1);
  77. #endif
  78. /* que doit-on faire avec ce qu'on
  79. vient de recevoir ? */
  80. if (0x04 == received) { /* EOT (end of transmission) */
  81. waddstr(glass, "--- EOT ---\n"); wrefresh(glass);
  82. flag_exit = 1;
  83. }
  84. putbyte(fd_remote, received);
  85. }
  86. wrefresh(glass);
  87. } while (! flag_exit);
  88. sleep(1);
  89. return -1;
  90. }
  91. /* ---------------------------------------------------------------- */
  92. int run_the_terminal(int fd_serial, char *title, WINDOW *win)
  93. {
  94. WINDOW *terminal, *ecran;
  95. int wid_term, hei_term, lin_term, col_term;
  96. int foo;
  97. // char ligne[100];
  98. // unsigned char byte;
  99. int fd_stdin;
  100. lin_term = 4; col_term = 9; /* position */
  101. wid_term = 60; hei_term = 25; /* dimensions */
  102. fd_stdin = fileno(stdin); /* for select or pool */
  103. terminal = newwin(hei_term, wid_term, lin_term, col_term);
  104. if ( NULL==terminal ) return -1;
  105. bordure(terminal, title, 1);
  106. ecran = subwin(terminal, hei_term-2, wid_term-2, lin_term+1, col_term+1);
  107. if ( NULL==ecran ) return -2;
  108. scrollok(ecran, 1);
  109. if (verbosity) {
  110. foo = mvwaddstr(ecran, 2, 14, "SCREEN READY\n");
  111. #if DEBUG_LEVEL > 1
  112. fprintf(stderr, "in '%s', mvwaddstr -> %d\n", __func__, foo);
  113. #endif
  114. wrefresh(ecran); sleep(1);
  115. }
  116. foo = interactive(ecran, fd_stdin, fd_serial);
  117. if (foo) {
  118. fprintf(stderr, "interactive -> %d\n", foo);
  119. sleep(1);
  120. }
  121. delwin(terminal);
  122. touchwin(stdscr); wrefresh(stdscr);
  123. return 0;
  124. }
  125. /* ---------------------------------------------------------------- */
  126. int essai_terminal(int fd, char *txt, int k)
  127. {
  128. int lig, col;
  129. int foo, retv;
  130. #if DEBUG_LEVEL
  131. fprintf(stderr, ">>> %s ( %d '%s' %d )\n", __func__,
  132. fd, txt, k );
  133. #endif
  134. /*** initialisation de curses */
  135. initscr();
  136. nonl(); cbreak(); noecho();
  137. for (lig=0; lig<LINES; lig++) {
  138. for (col=0; col<COLS; col++) {
  139. mvaddch(lig, col, "o+OX"[rand()%4]);
  140. }
  141. }
  142. wrefresh(stdscr);
  143. /*** passons a la partie active */
  144. retv = run_the_terminal(fd, txt, stdscr);
  145. /*** terminaison de curses */
  146. endwin();
  147. fprintf(stderr, "terminal return %d\n", retv);
  148. return 0;
  149. }
  150. /* ---------------------------------------------------------------- */