#!/usr/local/bin/octave -qf # # Example Octave program using the GTK-server # # Tested with Octave 2.1.72 and GTK-server 2.0.7 # on Slackware Linux 10.2. # # January 2, 2006 - PvE. # #------------------------------------------------ # Communication function with GTK-server function result = gtk (message) global pout; global pin; fputs(pout, message); result = fgetl(pin); endfunction #------------------------------------------------ # First, setup pipes [readpipe, err, msg] = pipe () if (err != 0) printf("Can not create pipe!\n"); exit(-1); endif [writepipe, err, msg] = pipe () if (err != 0) printf("Can not create pipe!\n"); exit(-1); endif # Then fork [pid, msg] = fork () if (pid < 0) printf("Can not FORK!\n"); exit(-1); endif # Child process if (pid == 0) # Close the parent pipes fclose(nth(writepipe,2)); fclose(nth(readpipe,1)); # Duplicate STDIN and STDOUT [fid, msg] = dup2 (nth(writepipe,1), stdin); fclose(nth(writepipe,1)); [fid, msg] = dup2 (nth(readpipe,2), stdout); fclose(nth(readpipe,2)); # Start GTK-server system("gtk-server -stdin"); # This is the parent process else # Close the child pipes first fclose(nth(writepipe,1)); fclose(nth(readpipe,2)); # Get the active filehandles global pout = nth(writepipe,2); global pin = nth(readpipe,1); # Setup the GUI gtk("gtk_init NULL NULL"); win = gtk("gtk_window_new 0"); gtk(["gtk_window_set_title ", win, " \"Octave GTK-server demo with STDIN\""]); gtk(["gtk_window_set_default_size ", win, " 400 200"]); gtk(["gtk_window_set_position ", win, " 1"]); tbl = gtk("gtk_table_new 10 10 1"); gtk(["gtk_container_add ", win, " ", tbl]); lbl = gtk("gtk_label_new \"Octave is compatible with all GTK versions now!\""); gtk(["gtk_table_attach_defaults ", tbl, " ", lbl, " 1 9 1 4"]); but = gtk("gtk_button_new_with_label \"Click to Quit\""); gtk(["gtk_table_attach_defaults ", tbl, " ", but, " 7 9 7 9"]); act = gtk("gtk_button_new_with_label \"Some action\""); gtk(["gtk_table_attach_defaults ", tbl, " ", act, " 2 4 7 9"]); gtk(["gtk_widget_show_all ", win]); event = ""; while (!strcmp(event, win) && !strcmp(event, but)) event = gtk("gtk_server_callback wait"); if strcmp(event, act) printf("Some action on the console!\n"); endif endwhile gtk("gtk_server_exit"); endif