fltk-rundialog/main.cc
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Multi_Browser.H>
#include <FL/Fl_PNG_Image.H>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <signal.h>
FILE *G_fp = NULL;
int cline=1;
int autoclose=0;
char wtitle[100]="RunBox";
int winWidth=600;
int winHeight=200;
char iconFilename[256];
Fl_Window win(winWidth,winHeight,wtitle);
Fl_Button btOk(10, 30, 100, 25, "Ok");
Fl_Multi_Browser brow(10,10,winWidth-20,winHeight-50);
void HandleFD(int fd, void *data) {
Fl_Multi_Browser *brow = (Fl_Multi_Browser*)data;
char s[1024];
if ( fgets(s, 1023, G_fp) == NULL ) {
Fl::remove_fd(fileno(G_fp));
pclose(G_fp);
cline++;
brow->add(" ");
brow ->bottomline(cline) ;
if ((fileno(G_fp)==-1) && autoclose==1)exit(0);
if ((fileno(G_fp)==-1) && autoclose==0) btOk.activate();
return;
}
brow->add(s);
brow ->bottomline(cline) ;
cline++;
}
void quit(Fl_Widget *w, void *data)
{
pid_t mypid=getpid();
kill(mypid+1, 9);
exit(0);
}
void printHelp()
{
printf("usage :\n runbox <options> \n\n");
printf("--width= or -W for window weight\n\
--height= or h for window height\n\
--title= or -t for window title\n\
--cmd= or -c command to run\n\
--icon= or -i icon for the window\n\
--autoclose= or -h (0/1) closing after the end of the process\n\
--help or -h for this help");
printf("\n\n To get stderr use runbox --cmd=' ls myfolder &>mystderr_file.txt' and \
then read mystderr_file.txt for reading errors \n");
printf("\nIf you get also stderr append '2>&1' at your commandline\n\n");
}
int main(int argc, char** argv) {
int c;
char cmd[1024]="ping -c 4 localhost";
while( c != -1 )
{
static struct option long_options[] =
{
{"width", required_argument, 0, 'W'},
{"height", required_argument, 0, 'H'},
{"title", required_argument, 0, 't'},
{"cmd", required_argument, 0, 'c'},
{"autoclose", required_argument, 0, 'a'},
{"icon", required_argument, 0, 'i'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "W:H:t:c:i:h:",long_options, &option_index);
switch (c)
{
case 'h':
printHelp();
exit(0);
break;
case 'c':
sprintf(cmd,"%s",optarg);
break;
case 'W':
winWidth=atoi(optarg);
break;
case 'H':
winHeight=atoi(optarg);
break;
case 't':
sprintf(wtitle,"%s",optarg);
break;
case 'i':
sprintf(iconFilename,"%s",optarg);
break;
case 'a':
autoclose=atoi(optarg);
break;
}
}
if ( ( G_fp = popen(cmd, "r") ) == NULL ) {
perror("popen failed");
return(1);
}
Fl::add_fd(fileno(G_fp), HandleFD, (void*)&brow);
btOk.deactivate();
win.size(winWidth,winHeight);
btOk.resize(winWidth-350, winHeight-30, 100, 25);
btOk.callback(quit);
win.callback(quit);
brow.size(winWidth-20,winHeight-50);
win.resizable(brow);
if (autoclose==1)
{
brow.resize(10,10,winWidth-20,winHeight-20);
btOk.hide();
}
if (strlen(iconFilename)>1 )
{
Fl_PNG_Image *icon = new Fl_PNG_Image(iconFilename);
win.icon(icon);
}
win.show();
return(Fl::run());
}