csegmentation-faultgtkgtk4vte

Can't run shell command in VTE GTK4 Fake Terminal


I made a small sample code in gtk4 + vte to run a fake terminal with a button in the bottom to run a simple command when the button is clicked.

main.c

#include <gtk/gtk.h>
#include <vte/vte.h>
#define WINDOW_HEIGHT 400
#define WINDOW_WIDTH 600
GtkApplication *app;
GtkWidget *window, *terminal, *grid, *scrollview1,*button;
int status;

void run_button(void){
    char **argv_test[2] = {
        "echo\0",
        "Hello!!\0"
    };
    // can't run this command in fake terminal!
    vte_terminal_spawn_async(VTE_TERMINAL(terminal),VTE_PTY_NO_HELPER,NULL,argv_test,NULL,G_SPAWN_SEARCH_PATH,NULL,NULL,NULL,2000,NULL,NULL,NULL);
}

void window_renderer(GtkApplication *app, gpointer user_data) {
    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window),"My terminal");
    gtk_window_set_default_size (GTK_WINDOW(window), WINDOW_WIDTH, WINDOW_HEIGHT);
    grid = gtk_grid_new();
    gtk_window_set_child(GTK_WINDOW(window), grid);
    gtk_widget_set_vexpand(grid,TRUE);
    gtk_widget_set_hexpand(grid,TRUE);
    scrollview1 = gtk_scrolled_window_new();
    gtk_grid_attach(GTK_GRID(grid), scrollview1, 0, 0, 1, 1);
    gtk_widget_set_size_request(GTK_WIDGET(scrollview1),WINDOW_WIDTH,WINDOW_HEIGHT);
    button = gtk_button_new_with_label("Run!");
    gtk_grid_attach(GTK_GRID(grid), button, 0, 1, 1, 1);
    g_signal_connect(button,"clicked", G_CALLBACK(run_button), NULL);
    terminal = vte_terminal_new();
    gtk_window_set_child(GTK_WINDOW(scrollview1), terminal);
    gtk_widget_show(window);
}

int main(int argc, char **argv)
{
    app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app,"activate", G_CALLBACK(window_renderer), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return status;
}

Makefile

CFLAGS += -Wall
CFLAGS += `pkg-config --cflags gtk4 vte-2.91-gtk4`
LIBS += `pkg-config --libs gtk4 vte-2.91-gtk4`

.PHONY: all clean

all: main

main:main.c
    gcc $(CFLAGS) main.c -o main $(LIBS)

clean:
    rm main

I have compiled VTE (for gtk4) and gtk4 developer package, building a gtk4 app is not an issue!

When running the built binary, the program crashes when I click the button with a segmentation fault, probably because of a pointer not properly initialized (according to tutorials point).

Can someone help me to find out what I have missed?


Solution

  • I tested out your code and I believe I found the main issue with your terminal command along with some warnings that were displayed when I tested. First off, from reviewing other sample code using the "vte_terminal_spawn_async" function, the character string set included an ending array element of "NULL". So instead of the following code snippet.

    char **argv_test[2] = {
        "echo\0",
        "Hello!!\0"
    };
    

    You would need to add in a third array element as in the following revised code snippet.

    char *argv_test[3] = {
        "echo\0", "Hello!!\0", NULL
    };
    

    If you notice one other minor revision to the above code snippet, I revised the definition of the character array from "char ** argv_test[3]" to "char *argv_test[3]". The compiler was listing a warning about incompatible pointer references. Also, when I was testing out this code I was receiving a warning and not able to view the terminal when the "Run" button was clicked.

    Gtk-CRITICAL **: 14:49:46.383: gtk_window_set_child: assertion 'GTK_IS_WINDOW (window)' failed
    

    From some previous troubleshooting with another user, we had determined that the child setup for the scrolled window needed to utilize the scrolled window child reference function along with the scrolled window macro. So I revised the scrolled window child assignment to be as follows.

    gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrollview1), terminal);
    

    Making those small revisions resulted in displaying the terminal with your "echo Hello" command (I clicked the button twice in my testing).

    Sample Terminal

    I believe that if you try out those minor revisions to your code, you will be able to view your terminal and progress with your coding.

    Regards.