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.

84 lines
2.9KB

  1. //
  2. // "$Id: howto-add_fd-and-popen.cxx 8194 2011-01-05 22:28:39Z AlbrechtS $"
  3. //
  4. // How to use popen() and Fl::add_fd() - erco 10/04/04
  5. // Originally from erco's cheat sheet, permission by author.
  6. //
  7. // Shows how the interface can remain "alive" while external
  8. // command is running and outputing occassional data. For instance,
  9. // while the command is running, keyboard navigation works,
  10. // text can be highlighted, and the interface can be resized.
  11. //
  12. // Copyright 2010 Greg Ercolano.
  13. // Copyright 1998-2010 by Bill Spitzak and others.
  14. //
  15. // This library is free software; you can redistribute it and/or
  16. // modify it under the terms of the GNU Library General Public
  17. // License as published by the Free Software Foundation; either
  18. // version 2 of the License, or (at your option) any later version.
  19. //
  20. // This library is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. // Library General Public License for more details.
  24. //
  25. // You should have received a copy of the GNU Library General Public
  26. // License along with this library; if not, write to the Free Software
  27. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  28. // USA.
  29. //
  30. // Please report all bugs and problems on the following page:
  31. //
  32. // http://www.fltk.org/str.php
  33. //
  34. #include <stdio.h>
  35. #include <FL/Fl.H>
  36. #include <FL/Fl_Window.H>
  37. #include <FL/Fl_Multi_Browser.H>
  38. #ifdef WIN32
  39. # define PING_CMD "ping -n 10 localhost" // 'slow command' under windows
  40. # ifdef _MSC_VER
  41. # define popen _popen
  42. # define pclose _pclose
  43. # else /*_MSC_VER*/
  44. # include <unistd.h> // non-MS win32 compilers (untested)
  45. # endif /*_MSC_VER*/
  46. #else
  47. # include <unistd.h>
  48. # define PING_CMD "ping -i 2 -c 10 localhost" // 'slow command' under unix
  49. #endif
  50. // GLOBALS
  51. FILE *G_fp = NULL;
  52. // Handler for add_fd() -- called whenever the ping command outputs a new line of data
  53. void HandleFD(int fd, void *data) {
  54. Fl_Multi_Browser *brow = (Fl_Multi_Browser*)data;
  55. char s[1024];
  56. if ( fgets(s, 1023, G_fp) == NULL ) { // read the line of data
  57. Fl::remove_fd(fileno(G_fp)); // command ended? disconnect callback
  58. pclose(G_fp); // close the descriptor
  59. brow->add(""); brow->add("<<DONE>>"); // append msg indicating command finished
  60. return;
  61. }
  62. brow->add(s); // line of data read? append to widget
  63. }
  64. int main(int argc, char *argv[]) {
  65. Fl_Window win(600,600);
  66. Fl_Multi_Browser brow(10,10,580,580);
  67. if ( ( G_fp = popen(PING_CMD, "r") ) == NULL ) { // start the external unix command
  68. perror("popen failed");
  69. return(1);
  70. }
  71. Fl::add_fd(fileno(G_fp), HandleFD, (void*)&brow); // setup a callback for the popen()ed descriptor
  72. win.resizable(brow);
  73. win.show(argc, argv);
  74. return(Fl::run());
  75. }
  76. //
  77. // End of "$Id: howto-add_fd-and-popen.cxx 8194 2011-01-05 22:28:39Z AlbrechtS $".
  78. //