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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. waddch(glass, received);
  70. if ('\n' == received) waddch(glass, '\r');
  71. }
  72. /*** est-ce le yuser avec son clavier ? */
  73. if (FD_ISSET(fd_local, &rfds)) {
  74. received = getch();
  75. if (verbosity) {
  76. sprintf(ligne, " $%02x from yuser\n", received);
  77. waddstr(glass, ligne);
  78. }
  79. putbyte(fd_remote, received);
  80. }
  81. wrefresh(glass);
  82. } while (! flag_exit);
  83. sleep(1);
  84. return -1;
  85. }
  86. /* ---------------------------------------------------------------- */
  87. int run_the_terminal(int fd_serial, char *title, WINDOW *win)
  88. {
  89. WINDOW *terminal, *ecran;
  90. int wid_term, hei_term, lin_term, col_term;
  91. int foo;
  92. // char ligne[100];
  93. // unsigned char byte;
  94. int fd_stdin;
  95. lin_term = col_term = 4; /* position */
  96. wid_term = 60;
  97. hei_term = 25;
  98. fd_stdin = fileno(stdin); /* for select or pool */
  99. terminal = newwin(hei_term, wid_term, lin_term, col_term);
  100. if ( NULL==terminal ) return -1;
  101. bordure(terminal, title, 1);
  102. ecran = subwin(terminal, hei_term-2, wid_term-2, lin_term+1, col_term+1);
  103. if ( NULL==ecran ) return -2;
  104. scrollok(ecran, 1);
  105. if (verbosity) {
  106. foo = mvwaddstr(ecran, 2, 14, "SCREEN READY\n");
  107. #if DEBUG_LEVEL > 1
  108. fprintf(stderr, "in '%s', mvwaddstr -> %d\n", __func__, foo);
  109. #endif
  110. wrefresh(ecran); sleep(1);
  111. }
  112. foo = interactive(ecran, fd_stdin, fd_serial);
  113. if (foo) {
  114. fprintf(stderr, "interactive -> %d\n", foo);
  115. sleep(1);
  116. }
  117. delwin(terminal);
  118. touchwin(stdscr); wrefresh(stdscr);
  119. return 0;
  120. }
  121. /* ---------------------------------------------------------------- */
  122. int essai_terminal(int fd, char *txt, int k)
  123. {
  124. int lig, col;
  125. int foo, retv;
  126. #if DEBUG_LEVEL
  127. fprintf(stderr, ">>> %s ( %d '%s' %d )\n", __func__,
  128. fd, txt, k );
  129. #endif
  130. /*** initialisation de curses */
  131. initscr();
  132. nonl(); cbreak(); noecho();
  133. for (lig=0; lig<LINES; lig++) {
  134. for (col=0; col<COLS; col++) {
  135. mvaddch(lig, col, "o+OX"[rand()%4]);
  136. }
  137. }
  138. wrefresh(stdscr);
  139. /*** passons a la partie active */
  140. retv = run_the_terminal(fd, txt, stdscr);
  141. /*** terminaison de curses */
  142. endwin();
  143. fprintf(stderr, "terminal return %d\n", retv);
  144. return 0;
  145. }
  146. /* ---------------------------------------------------------------- */