/*---------------------------------------------------------------------------- How to run the GTK-server with the C programming language using STDIN. Compile like this: gcc -o demo demo.c The bidirectional pipe theory was retrieved from: http://www.unixwiz.net/techtips/remap-pipe-fds.html Tested with GCC-3.3.4 on Slackware Linux 10.1 with GTK-server 2.0.5. Does *not* work with Windows since this demo needs a fork(). September 10, 2005 - PvE. ------------------------------------------------------------------------------*/ /* Header stuff */ #include #include #include #include #include #define MAX_LEN 256 char *answer; int writepipe[2], /* parent -> child */ readpipe [2]; /* child -> parent */ /*--------------------------------------------------*/ /* Communication function */ char* gtk(int no, ...) { char *command; int len, i; va_list args; va_start(args, no); /* Put the var_args into 1 string */ command = (char*)malloc(sizeof(char)*MAX_LEN); strcpy(command, ""); 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 memory */ free(command); va_end(args); return answer; } /*--------------------------------------------------*/ /* Main routine */ int main(int argc, char* argv[]) { /* Declare pointers for widgets we want to remember */ char *win, *table, *button1, *button2, *entry; int result; answer = (char*)malloc(MAX_LEN*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"); win = strdup(gtk(1, "gtk_window_new 0")); 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"); table = strdup(gtk(1, "gtk_table_new 30 30 1")); gtk(3, "gtk_container_add", win, table); button1 = strdup(gtk(1, "gtk_button_new_with_label Exit")); gtk(4, "gtk_table_attach_defaults", table, button1, "17 28 20 25"); button2 = strdup(gtk(1, "gtk_button_new_with_label \"Print text\"")); gtk(4, "gtk_table_attach_defaults", table, button2, "2 13 20 25"); entry = strdup(gtk(1, "gtk_entry_new")); gtk(4, "gtk_table_attach_defaults", table, entry, "2 28 5 15"); gtk(2, "gtk_widget_show_all", win); /* Mainloop */ do { gtk(1, "gtk_server_callback wait"); if (!strcmp(answer, button2)){ gtk(2, "gtk_entry_get_text", entry); printf("This is the contents: %s", answer); } } while (strcmp(answer, win) && strcmp(answer, button1)); /* Exit GTK-server */ gtk(1, "gtk_server_exit"); } return 0; }