#include #include #include #include #include #include // Demonstrate running a command from a button - erco 03/03/09 // (Response on fltk.general..) Fl_Window *G_win = 0; Fl_Multi_Browser *G_out = 0; Fl_Light_Button *G_but = 0; FILE *G_fp = 0; const char *G_cmd = "ping -c 10 -i 3 localhost"; // or "make -j4 .." void HandleFD(int fd, void *) { char results[1024]; if ( fgets(results, 1023, G_fp) == NULL ) { Fl::remove_fd(fileno(G_fp)); // remove fd pclose(G_fp); G_fp = 0; // close up G_win->cursor(FL_CURSOR_DEFAULT); // normal cursor G_but->value(0); // button light off G_but->activate(); // reactivate return; // back to FLTK } char *crlf = strchr(results, '\n'); if ( crlf ) *crlf = 0; G_out->add(results); } void Run_CB(Fl_Widget*,void*) { if ( ( G_fp = popen(G_cmd, "r") ) == NULL ) { fl_message("popen failed"); return; } // COMMAND RUNNING: DEACTIVATE BUTTON, WAIT CURSOR, ADD_FD G_but->deactivate(); // gray out button G_win->cursor(FL_CURSOR_WAIT); // timer cursor G_out->clear(); // clear output Fl::add_fd(fileno(G_fp), HandleFD, 0); // tell fltk to watch popen() for data // NOTE: let FLTK handle everything from here } int main() { G_win = new Fl_Window(720,450); G_out = new Fl_Multi_Browser(10,10,700,400); G_but = new Fl_Light_Button(600,420,100,20,"Run"); G_but->tooltip("Runs 10 iterations of ping,\n" "then stops automatically.\n"); G_but->callback(Run_CB); G_win->end(); G_win->show(); return(Fl::run()); }