#! cint /*---------------------------------------------------------------------------- How to run the GTK-server with the C programming language, using the CINT interpreter. This demo was requested by several people. The CINT interpreter has some limitations; with real C code some things could have been implemented a little bit nicer. The CINT interpreter webpage: http://root.cern.ch/root/Cint.html Run like this: > export CINTSYSDIR= > cint demo.c The bidirectional pipe theory was retrieved from: http://www.unixwiz.net/techtips/remap-pipe-fds.html Tested with CINT 5.16 on Slackware Linux 10.1 with GTK-server 2.0.5. September 9, 2005 - PvE. ------------------------------------------------------------------------------*/ /* Header stuff */ #include #include #include #include #define MAX_LEN 256 char *answer = (char*)malloc(MAX_LEN*sizeof(char)); int writepipe[2], /* parent -> child */ readpipe [2]; /* child -> parent */ /*--------------------------------------------------*/ /* Communication function */ void gtk(int no, ...) { char *command; int i; int len; va_list args; va_start(args, no); command = (char*)malloc(sizeof(char)*MAX_LEN); strcpy(command, ""); /* Put the var_args into 1 string */ for (i = 0; i < no; i++) { strcat(command, va_arg(args, char*)); strcat(command, " "); } /* Write to pipe */ write(writepipe[1], command, MAX_LEN); len = read(readpipe[0], answer, MAX_LEN); /* Make sure we do not see trash of previous answers */ answer[len] = '\0'; free(command); va_end(args); } /*--------------------------------------------------*/ /* Main routine */ int main(int argc, char* argv[]) { int result; char *win = (char*)malloc(16*sizeof(char)); char *table = (char*)malloc(16*sizeof(char)); char *button1 = (char*)malloc(16*sizeof(char)); char *button2 = (char*)malloc(16*sizeof(char)); char *entry = (char*)malloc(16*sizeof(char)); /* First, setup pipes */ if (pipe(readpipe) < 0 || pipe(writepipe) < 0){ printf("Can not create pipes!\n"); exit(-1); } /* Then fork */ if((result = fork()) < 0){ printf("Can not FORK!\n"); exit(-1); } /* Child process */ else if (result == 0) { /* Close the parent pipes */ close(writepipe[1]); close(readpipe[0]); /* Duplicate STDIN and STDOUT */ dup2(writepipe[0], 0); close(writepipe[0]); dup2(readpipe[1], 1); close(readpipe[1]); /* Start GTK-server */ system("gtk-server -stdin"); } /* This is the parent process */ else { /* Close the child pipes first */ close(writepipe[0]); close(readpipe[1]); /* do parent stuff */ gtk(1, "gtk_init NULL NULL"); gtk(1, "gtk_window_new 0"); strcpy(win, answer); gtk(3, "gtk_window_set_title", win, "\"This is a title\""); gtk(3, "gtk_window_set_default_size", win, "100 100"); gtk(3, "gtk_window_set_position", win, "1"); gtk(1, "gtk_table_new 30 30 1"); strcpy(table, answer); gtk(3, "gtk_container_add", win, table); gtk(1, "gtk_button_new_with_label Exit"); strcpy(button1, answer); gtk(4, "gtk_table_attach_defaults", table, button1, "17 28 20 25"); gtk(1, "gtk_button_new_with_label \"Print text\""); strcpy(button2, answer); gtk(4, "gtk_table_attach_defaults", table, button2, "2 13 20 25"); gtk(1, "gtk_entry_new"); strcpy(entry, answer); gtk(4, "gtk_table_attach_defaults", table, entry, "2 28 5 15"); gtk(2, "gtk_widget_show_all", win); /* Mainloop - CINT seems to have problems with do/while and var_args */ LABEL: gtk(1, "gtk_server_callback wait"); if (!strcmp(answer, button2)){ gtk(2, "gtk_entry_get_text", entry); printf("This is the contents: %s", answer); } if (strcmp(answer, win) && strcmp(answer, button1)) goto LABEL; /* Exit GTK-server */ gtk(1, "gtk_server_exit"); } return 0; }