#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <string.h>

static GtkWidget* window;

static gboolean fullscreen()
{
   gtk_window_fullscreen(GTK_WINDOW(window));
   return FALSE;
}

static gboolean unfullscreen()
{
   gtk_window_unfullscreen(GTK_WINDOW(window));
   return FALSE;
}

static gboolean switch_monitors(gpointer data)
{
   static const long monitors[4][4] = {
      {0, 0, 0, 1},
      {0, 0, 0, 0},
      {1, 1, 1, 1},
      {0, 0, 0, 1}
   };

   guint index = GPOINTER_TO_UINT(data);
   GdkDisplay *display = gdk_display_get_default();
   XClientMessageEvent xclient;

   memset(&xclient, 0, sizeof(xclient));
   xclient.type = ClientMessage;
   xclient.window = GDK_WINDOW_XID(window->window);
   xclient.message_type = gdk_x11_get_xatom_by_name_for_display(display, "_NET_WM_FULLSCREEN_MONITORS");
   xclient.format = 32;
   xclient.data.l[0] = monitors[index][0];
   xclient.data.l[1] = monitors[index][1];
   xclient.data.l[2] = monitors[index][2];
   xclient.data.l[3] = monitors[index][3];
   xclient.data.l[4] = 1;

   XSendEvent(GDK_WINDOW_XDISPLAY(window->window),
              GDK_WINDOW_XWINDOW(gdk_get_default_root_window()),
              False,
              SubstructureRedirectMask | SubstructureNotifyMask,
              (XEvent *) &xclient);
}

static gboolean quit()
{
   gtk_main_quit();
   return FALSE;
}

int main(int argc, char** argv)
{
   gtk_init(&argc, &argv);

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_widget_show(window);

   g_timeout_add(1000, (GSourceFunc) fullscreen, NULL);
   g_timeout_add(2000, (GSourceFunc) switch_monitors, GUINT_TO_POINTER(0));
   g_timeout_add(3000, (GSourceFunc) switch_monitors, GUINT_TO_POINTER(1));
   g_timeout_add(4000, (GSourceFunc) switch_monitors, GUINT_TO_POINTER(2));
   g_timeout_add(5000, (GSourceFunc) switch_monitors, GUINT_TO_POINTER(3));
   g_timeout_add(6000, (GSourceFunc) unfullscreen, NULL);
   g_timeout_add(8000, (GSourceFunc) quit, NULL);

   gtk_main();
}
