Home > Blockchain >  Can't run shell command in VTE GTK4 Fake Terminal
Can't run shell command in VTE GTK4 Fake Terminal

Time:05-18

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 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.

  • Related