little vte tutorial (C/C++)

You use gtk for your progam and want a little terminal in it? For this is the vte (virtual terminal emulator) widget the right choice. But you won’t find a tutorial how to do this in C (or C++).  This is why I create this little tutorial.

The first thing you need to do is to include the right header (vte/vte.h) and choose in pkg-config the right vte version (gtk2/gtk3).
To create a terminal object use: vte_terminal_new().
This function will return a GtkWidget* which can be included normaly.
But now you have problem with the vte intern methods: they demand a terminal object.
This problem is solved via the VTE_TERMINAL( vteterm) wrapper which transforms the GtkWidget* back into the terminal.

The next thing to do is to start a shell  in the terminal. Don’t start programs directly (except you have special wishes); this can be done with vte_terminal_feed_child more comfortable.
For this create an char* array with two (or more fields if you want to add arguments for the shell)  and fill them with 0/NULL ({NULL,NULL})
Then replace the  [0] cell with a path to the shell you like to use in the terminal. Important is that the array ends with NULL/0.
Example:

char *startterm[2]={0,0};
startterm[0]=(char*)"/bin/sh";
or more elegant:
startterm[0]=vte_get_user_shell(); //returns default shell

This strange construct is needed for a function with the clumsy name:
vte_terminal_fork_command_full
Here an example to explain the arguments:

 vte_terminal_fork_command_full(VTE_TERMINAL(vteterm),
                                       VTE_PTY_DEFAULT, //how the shell is started
                                       NULL,  //working directory; set to NULL for default
                                       startterm, //here include the strange construct
                                       NULL, //NULL or another strange construct containing the environment variables
                                       (GSpawnFlags)(G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH),  //Spawnflags
                                       NULL, //setup function
                                       NULL, //pointer to child data
                                       &pidterm, //either NULL or reference to GPid object in this case (GPid pidterm;)
                                       &err); //either NULL or reference to GError* object; important GError* must be set on 0 before using

Please have a look into:
http://developer.gnome.org/vte/unstable/VteTerminal.html
to see what the arguments do exactly.

Let’s come to some working control commands for the terminal:

vte_terminal_feed (feeds terminal without interpretation by child (started shell)):

char *t="test"
 vte_terminal_feed (VTE_TERMINAL(vteterm),t,4);
 4 is the length of the char * array. This will print the message: "test" in the terminal.

vte_terminal_feed_child (interprete string with child):

char *t="echo \"test\""
vte_terminal_feed_child (VTE_TERMINAL(vteterm),t,11);
11 is the length of the char * array. The child will print the message: "test" in the terminal.

The child can also be fed with a binary (the syntax is the same). But I don’t know an example for this :
vte_terminal_feed_child_binary

http://developer.gnome.org/vte/unstable/
https://github.com/thestinger/termite

Advertisements

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s