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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * emulateur de terminal
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <sys/select.h>
  9. #include <ncurses.h>
  10. #include "../serial/serial.h"
  11. #include "terminal.h"
  12. extern int verbosity;
  13. /* ---------------------------------------------------------------- */
  14. static void bordure(WINDOW * w, char *texte, int type)
  15. {
  16. if (type)
  17. box(w, 0, 0);
  18. else
  19. wborder(w, '|', '|', '-', '-', '+', '+', '+', '+');
  20. wstandout(w); mvwaddstr(w, 0, 3, texte); wstandend(w);
  21. wrefresh(w);
  22. }
  23. /* ---------------------------------------------------------------- */
  24. static int interactive(WINDOW *glass, int fd_local, int fd_remote)
  25. {
  26. int flag_exit = 0;
  27. long tstart, tcur;
  28. char ligne[100];
  29. /* --- variables for select */
  30. struct timeval tv;
  31. fd_set rfds;
  32. int retval;
  33. #if DEBUG_LEVEL
  34. fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__,
  35. glass, fd_local, fd_remote);
  36. #endif
  37. tstart = time(NULL);
  38. wclear(glass); wrefresh(glass);
  39. do {
  40. tcur = time(NULL);
  41. FD_ZERO(&rfds);
  42. FD_SET(fd_local, &rfds); /* stdin */
  43. FD_SET(fd_remote, &rfds); /* teletype */
  44. tv.tv_sec = tv.tv_sec = 0; /* no timeout */
  45. retval = select(2, &rfds, NULL, NULL, &tv);
  46. wrefresh(glass); usleep(155*1000);
  47. } while (! flag_exit);
  48. sleep(1);
  49. return -1;
  50. }
  51. /* ---------------------------------------------------------------- */
  52. int run_the_terminal(int fd_serial, char *title, WINDOW *win)
  53. {
  54. WINDOW *terminal, *ecran;
  55. int wid_term, hei_term, lin_term, col_term;
  56. int foo;
  57. char ligne[100];
  58. unsigned char byte;
  59. int fd_stdin;
  60. lin_term = col_term = 4; /* position */
  61. wid_term = 40;
  62. hei_term = 25;
  63. fd_stdin = fileno(stdin); /* for select or pool */
  64. terminal = newwin(hei_term, wid_term, lin_term, col_term);
  65. if ( NULL==terminal ) return -1;
  66. bordure(terminal, title, 1);
  67. ecran = subwin(terminal, hei_term-2, wid_term-2, lin_term+1, col_term+1);
  68. if ( NULL==ecran ) return -2;
  69. scrollok(ecran, 1);
  70. if (verbosity) {
  71. foo = mvwaddstr(ecran, 2, 14, "SCREEN READY\n");
  72. #if DEBUG_LEVEL > 1
  73. fprintf(stderr, "in '%s', mvwaddstr -> %d\n", __func__, foo);
  74. #endif
  75. wrefresh(ecran); sleep(2);
  76. }
  77. foo = interactive(ecran, fd_stdin, fd_serial);
  78. sleep(1);
  79. delwin(terminal);
  80. touchwin(stdscr); wrefresh(stdscr);
  81. return 0;
  82. }
  83. /* ---------------------------------------------------------------- */
  84. int essai_terminal(int fd, char *txt, int k)
  85. {
  86. int lig, col;
  87. int foo, retv;
  88. #if DEBUG_LEVEL
  89. fprintf(stderr, ">>> %s ( %d '%s' %d )\n", __func__,
  90. fd, txt, k );
  91. #endif
  92. /*** initialisation de curses */
  93. initscr();
  94. nonl(); cbreak(); noecho();
  95. for (lig=0; lig<LINES; lig++) {
  96. for (col=0; col<COLS; col++) {
  97. mvaddch(lig, col, "o+OX"[rand()%4]);
  98. }
  99. }
  100. wrefresh(stdscr);
  101. /*** passons a la partie active */
  102. retv = run_the_terminal(fd, txt, stdscr);
  103. /*** terminaison de curses */
  104. endwin();
  105. fprintf(stderr, "terminal return %d\n", retv);
  106. return 0;
  107. }
  108. /* ---------------------------------------------------------------- */