Code to display sysinfo using GTK

Start
#include <gtk/gtk.h>
#include <sys/sysinfo.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
    // Initialize GTK
    gtk_init (&argc, &argv);
    
    // Create the main window
    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Computer Network, Process and Memory Information");
    gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
    gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
    
    // Create a vertical box as the main container
    GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    
    // Create a label to display the network information
    GtkWidget *network_label = gtk_label_new (NULL);
    gtk_label_set_markup (GTK_LABEL (network_label), "<b>Network Information</b>");
    gtk_container_add (GTK_CONTAINER (vbox), network_label);
    
    // Create a table to display the network information
    GtkWidget *network_table = gtk_grid_new ();
    gtk_grid_insert_column (GTK_GRID (network_table), 0);
    gtk_grid_insert_column (GTK_GRID (network_table), 1);
    gtk_grid_insert_row (GTK_GRID (network_table), 0);
    gtk_grid_insert_row (GTK_GRID (network_table), 1);
    gtk_container_add (GTK_CONTAINER (vbox), network_table);
    
    // Insert network information labels into the table
    GtkWidget *network_label_0 = gtk_label_new ("IP Address:");
    GtkWidget *network_label_1 = gtk_label_new ("Subnet Mask:");
    gtk_grid_attach (GTK_GRID (network_table), network_label_0, 0, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (network_table), network_label_1, 0, 1, 1, 1);
    
    // Insert network information values into the table
    GtkWidget *network_value_0 = gtk_label_new (NULL);
    GtkWidget *network_value_1 = gtk_label_new (NULL);
    gtk_grid_attach (GTK_GRID (network_table), network_value_0, 1, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (network_table), network_value_1, 1, 1, 1, 1);
    
    // Create a label to display the process information
    GtkWidget *process_label = gtk_label_new (NULL);
    gtk_label_set_markup (GTK_LABEL (process_label), "<b>Process Information</b>");
    gtk_container_add (GTK_CONTAINER (vbox), process_label);
    
    // Create a table to display the process information
    GtkWidget *process_table = gtk_grid_new ();
    gtk_grid_insert_column (GTK_GRID (process_table), 0);
    gtk_grid_insert_column (GTK_GRID (process_table), 1);
    gtk_grid_insert_row (GTK_GRID (process_table), 0);
    gtk_grid_insert_row (GTK_GRID (process_table), 1);
    gtk_container_add (GTK_CONTAINER (vbox), process_table);
    
    // Insert process information labels into the table
    GtkWidget *process_label_0 = gtk_label_new ("Number of Processes:");
    GtkWidget *process_label_1 = gtk_label_new ("Number of Threads:");
    gtk_grid_attach (GTK_GRID (process_table), process_label_0, 0, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (process_table), process_label_1, 0, 1, 1, 1);
    
    // Insert process information values into the table
    GtkWidget *process_value_0 = gtk_label_new (NULL);
    GtkWidget *process_value_1 = gtk_label_new (NULL);
    gtk_grid_attach (GTK_GRID (process_table), process_value_0, 1, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (process_table), process_value_1, 1, 1, 1, 1);
    
    // Create a label to display the memory information
    GtkWidget *memory_label = gtk_label_new (NULL);
    gtk_label_set_markup (GTK_LABEL (memory_label), "<b>Memory Information</b>");
    gtk_container_add (GTK_CONTAINER (vbox), memory_label);
    
    // Create a table to display the memory information
    GtkWidget *memory_table = gtk_grid_new ();
    gtk_grid_insert_column (GTK_GRID (memory_table), 0);
    gtk_grid_insert_column (GTK_GRID (memory_table), 1);
    gtk_grid_insert_row (GTK_GRID (memory_table), 0);
    gtk_grid_insert_row (GTK_GRID (memory_table), 1);
    gtk_container_add (GTK_CONTAINER (vbox), memory_table);
    
    // Insert memory information labels into the table
    GtkWidget *memory_label_0 = gtk_label_new ("Total Memory:");
    GtkWidget *memory_label_1 = gtk_label_new ("Available Memory:");
    gtk_grid_attach (GTK_GRID (memory_table), memory_label_0, 0, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (memory_table), memory_label_1, 0, 1, 1, 1);
    
    // Insert memory information values into the table
    GtkWidget *memory_value_0 = gtk_label_new (NULL);
    GtkWidget *memory_value_1 = gtk_label_new (NULL);
    gtk_grid_attach (GTK_GRID (memory_table), memory_value_0, 1, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (memory_table), memory_value_1, 1, 1, 1, 1);
    
    // Get the network information and update the labels
    // TODO: fill in the network information here
    
    // Get the process information and update the labels
    struct sysinfo sys_info;
    if (sysinfo(&sys_info) == 0) {
        char buffer[32];
        sprintf(buffer, "%ld", sys_info.procs);
        gtk_label_set_text (GTK_LABEL (process_value_0), buffer);
        sprintf(buffer, "%ld", sys_info.threads);
        gtk_label_set_text (GTK_LABEL (process_value_1), buffer);
    }
    
    // Get the memory information and update the labels
    // TODO: fill in the memory information here
    
    // Connect the 'destroy' event to the gtk_main_quit () function
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    
    // Show the window
    gtk_widget_show_all (window);
    
    // Start the main loop
    gtk_main ();
    
    return 0;
}
Previous Story

Write code to greet you everyday at noon

Next Story

S3 Ep120: When dud crypto simply won’t let go [Audio + Text]