Skip to content

Instantly share code, notes, and snippets.

@david-swift
Last active October 16, 2023 11:35
Show Gist options
  • Save david-swift/2b5ecde6bdd87a2a8de7fbbb819f2e45 to your computer and use it in GitHub Desktop.
Save david-swift/2b5ecde6bdd87a2a8de7fbbb819f2e45 to your computer and use it in GitHub Desktop.
This app crashes when not modifying the keyboard shortcuts to be invalid.
#include <gtk-4.0/gtk/gtk.h>
static void
handler (void)
{
printf("Hello, world!");
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Hello GNOME");
gtk_window_present (GTK_WINDOW (window));
const char *action_id = "action";
GSimpleAction *action = g_simple_action_new (action_id, NULL);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
g_signal_connect (action, "activate", G_CALLBACK (handler), NULL);
// Remove "Ctrl" ({ "<>n" })
// or add an invalid shortcut ({ "<Ctrl>n", "<>f" })
// and the app doesn't crash.
//
// SOLUTION: Add NULL to the end ({ "<Ctrl>n", NULL })
const char *keyboard_shortcuts[] = { "<Ctrl>n" };
const char *full_id = "app.action";
gtk_application_set_accels_for_action (GTK_APPLICATION (app), full_id, keyboard_shortcuts);
}
int
main (int argc,
char *argv[])
{
GtkApplication *app = gtk_application_new ("test.test.TestyTest", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
int status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment