GApplication signals

I am investigating the various GApplication signals.

So this is related to

One strange observation is that startup signal seems to be inactive when we start the app from command line with none blocking

./hello-world-gtk &

Took the code from https://www.gtk.org/docs/getting-started/hello-world/ for testing:

// https://www.gtk.org/docs/getting-started/hello-world/
// gcc `pkg-config --cflags gtk+-3.0` -o hello-world-gtk hello-world-gtk.c `pkg-config --libs gtk+-3.0`

#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
startup (GtkApplication *app,
          gpointer        user_data)
{
  g_print("startup\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button_box;

  g_print("activate\n");
  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_container_add (GTK_CONTAINER (window), button_box);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (button_box), button);

  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}
$ gcc `pkg-config --cflags gtk+-3.0` -o hello-world-gtk hello-world-gtk.c `pkg-config --libs gtk+-3.0`
salewski@nuc /tmp/hhh $ ./hello-world-gtk
startup
activate
salewski@nuc /tmp/hhh $ ./hello-world-gtk &
[1] 21189
salewski@nuc /tmp/hhh $ startup
activate

[1]+  Done                    ./hello-world-gtk
salewski@nuc /tmp/hhh $ 

GApplication defaults to single instances unless otherwise indicated. This means that, when there is an instance running with the same application id, the newly launched instance will send a message to the running instance, and terminate immediately; the running instance will emit the “activate” signal.

If you print out the instance pointer and PID, you’ll notice that the first instance you launched will emit “startup” and “activate”; and when you launch the second instance, the “activate” signal will be emitted by the first instance. The second instance will not emit any signal, and terminate immediately.

This is documented by the GApplication base class.

1 Like

The actual answer is much simpler: nothing was wrong, the startup signal was emitted!
“non-blocking” means that the shell was allowed to print its prompt while your program was beginning. That prompt doesn’t print a newline, therefore your program’s output started on the same line:

1 Like

Indeed. I feel stupid for today.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.