// Gtk-OpenGL-Binding for Linux (x11)                   2012-01-30 Franz Reiter

// test gtk + glx direct (without gtkgl, gthglext, sdl).

// Gtk2 + OpenGL:
// gcc -c `pkg-config --cflags gtk+-2.0` -DGTK2 gtk_gl_x11.c


// Gtk3 + OpenGL:
// gcc -c `pkg-config --cflags gtk+-3.0` -DGTK3 gtk_gl_x11.c


// -lGL glXChooseVisual



#include <stdio.h>
#include <stdlib.h>

#include <GL/gl.h>
#include <GL/glx.h>

#include <gtk/gtk.h>
#include <gdk/gdkx.h>        // gdk_x11_get_default_xdisplay GDK_WINDOW_XID

#include "GLB.h"             // GLB_Create

// 
  GLXContext  GLB_x_context;
  Display     *GLB_display;
  int         GLB_x_id = 0;




//================================================================
  GtkWidget*  GLB_Create () {
//================================================================
 
  GtkWidget   *area;
  int         xscreen, i1;
  GdkScreen   *screen;
  XVisualInfo *xvisual;
  GdkVisual   *visual;
  Window      root;
  Colormap    xcolormap;
  int         attributes[] = {
                GLX_RGBA,
                GLX_RED_SIZE, 1,
                GLX_GREEN_SIZE, 1,
                GLX_BLUE_SIZE, 1,
                GLX_DOUBLEBUFFER, True,
                GLX_DEPTH_SIZE, 24,
                None };


  area = gtk_drawing_area_new ();
  gtk_widget_set_double_buffered (area, FALSE);

  GLB_display = gdk_x11_get_default_xdisplay ();
  xscreen = DefaultScreen (GLB_display);
  screen = gdk_screen_get_default ();
    // printf(" screenNr = %d\n",gdk_screen_get_number(screen));
  xvisual = glXChooseVisual (GLB_display, xscreen, attributes);
    // printf(" xvisualid=%d\n",xvisual->visualid);
  visual = gdk_x11_screen_lookup_visual (screen, xvisual->visualid);

  root = RootWindow (GLB_display, xscreen);
  xcolormap = XCreateColormap (GLB_display, root, xvisual->visual, AllocNone);

  glXGetConfig (GLB_display, xvisual, GLX_RED_SIZE, &i1);
    printf(" GLX_RED_SIZE=%d\n",i1);
  glXGetConfig (GLB_display, xvisual, GLX_DEPTH_SIZE, &i1);
    printf(" GLX_DEPTH_SIZE=%d\n",i1);

// Gtk2 only:
  //colormap = gdk_x11_colormap_foreign_new (visual, xcolormap);
  //gtk_widget_set_colormap (area, colormap);

  GLB_x_context = glXCreateContext (GLB_display, xvisual, NULL, TRUE);

  free (xvisual);


  gtk_widget_set_events (GTK_WIDGET (area),
                         GDK_EXPOSURE_MASK |
                         GDK_BUTTON_PRESS_MASK |
                         GDK_BUTTON_RELEASE_MASK |
                         GDK_POINTER_MOTION_MASK |
                         GDK_POINTER_MOTION_HINT_MASK |
                         GDK_KEY_PRESS_MASK |
                         GDK_KEY_RELEASE_MASK);

  return area;

}


//================================================================
  int GLB_DrawInit (GtkWidget *widget) {
//================================================================

  GdkWindow     *window;

  if(!GLB_x_id) {

    window = gtk_widget_get_window (widget);
    GLB_x_id = GDK_WINDOW_XID(window);                // causes expose!
      printf("  GLB_x_id=%d\n",GLB_x_id);
  }


  return glXMakeCurrent (GLB_display, GLB_x_id, GLB_x_context);

}


//================================================================
  void GLB_DrawExit () {
//================================================================

  glXSwapBuffers (GLB_display, GLB_x_id);

  return;

}


//================================================================
  int GLB_Query () {
//================================================================

  char  const  *p1, *p2;


  p1 = glXGetClientString (GLB_display, GLX_VENDOR);
  if(!p1) return -1;

  p2 = glXGetClientString(GLB_display, GLX_VERSION);
    printf("OpenGL Vendor %s  Version %s\n",p1,p2);

  // p1 = glXGetClientString(GLB_display, GLX_EXTENSIONS);
    // printf("Extensions: %s\n",p1);

  return 0;

}

/*
//================================================================
  int GLB_Config () {
//================================================================

  XVisualInfo *vi;
  int i1;

  vi = get_xvisualinfo(visual);

  glXGetConfig (GLB_display, vi, GLX_RED_SIZE, &i1);
    printf(" GDK_GL_RED_SIZE=%d\n",i1);

  return 0;

}
*/

// EOF
