/*
 * gcc -o togglefullscreen -g -O0 togglefullscreen.c `pkg-config --libs --cflags gtk+-3.0`
 *
 */

#include <gtk/gtk.h>

static gboolean fullscreen = FALSE;

static gboolean
window_state_cb (GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
  GdkEventWindowState *event_state = (GdkEventWindowState *) event;

  fullscreen = event_state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;

  return FALSE;
}

static void
toggle_fullscreen_cb (GtkWidget *widget, gpointer user_data)
{
  GdkWindow  *window;

  window = gtk_widget_get_parent_window (widget);
  if (fullscreen)
    gdk_window_unfullscreen (window);
  else
    gdk_window_fullscreen (window);
  g_usleep (10 * 1000);
}

int
main (int argc, char *argv[])
{
  GtkWidget *window, *button;
  GtkAccelGroup *accel_group;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "window-state-event", G_CALLBACK (window_state_cb), NULL);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  accel_group = gtk_accel_group_new ();
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

  button = gtk_button_new_with_label ("Toggle fullscreen");
  g_signal_connect (button, "clicked", G_CALLBACK (toggle_fullscreen_cb), NULL);
  gtk_widget_add_accelerator (button, "activate", accel_group, GDK_KEY_F11, 0, GTK_ACCEL_VISIBLE);
  gtk_container_add (GTK_CONTAINER (window), button);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
