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

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