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

#include <gtk/gtk.h>

static gboolean fullscreen;
static guint timeout;


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 gboolean
timed_resize_cb (GtkWidget *toplevel)
{
  gint width, height;

  timeout = 0;

  gtk_window_get_size (GTK_WINDOW (toplevel), &width, &height);
  gtk_window_resize (GTK_WINDOW (toplevel), width * 2, height * 2);
  gtk_widget_show (toplevel);

  return FALSE;
}

static void
set_timed_resize_cb (GtkWidget *widget, gpointer user_data)
{
  GtkWidget *toplevel;

  g_return_if_fail (timeout == 0);

  toplevel = gtk_widget_get_toplevel (widget);
  g_return_if_fail (gtk_widget_is_toplevel (toplevel));

  gtk_widget_hide (toplevel);
  timeout = g_timeout_add_seconds (2, (GSourceFunc) timed_resize_cb, toplevel);
}

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);
}

int
main (int argc, char *argv[])
{
  GtkWidget *window, *vbox, *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);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_widget_set_valign (vbox, GTK_ALIGN_CENTER);
  gtk_widget_set_halign (vbox, GTK_ALIGN_CENTER);
  gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  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 (vbox), button);

  button = gtk_button_new_with_label ("Hidden resize");
  g_signal_connect (button, "clicked", G_CALLBACK (set_timed_resize_cb), NULL);
  gtk_container_add (GTK_CONTAINER (vbox), button);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
