#include "Jtk.h"

#ifdef JTK_X

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

Display * g_display;
XVisualInfo * g_vi;
XSetWindowAttributes g_swa;
GLXContext g_context;

/**
 * Initialize the Jtk subsystem.
 *
 * @return true on success, false on failure
 */
bool Jtk_Init()
{
    static int glx_attribute_list[] = {
        GLX_RGBA,
        GLX_DOUBLEBUFFER,
        None,
    };

    g_display = XOpenDisplay(NULL);
    if (g_display == NULL)
    {
        fprintf(stderr, "XOpenDisplay() failure\n");
        return false;
    }

    g_vi = glXChooseVisual(g_display, DefaultScreen(g_display),
            glx_attribute_list);
    if (g_vi == NULL)
    {
        fprintf(stderr, "glXChooseVisual() failure\n");
        return false;
    }

    g_context = glXCreateContext(g_display, g_vi, NULL, True);
    if (g_context == NULL)
    {
        fprintf(stderr, "glXCreateContext() failure\n");
        return false;
    }

    Colormap colormap = XCreateColormap(g_display,
            RootWindow(g_display, g_vi->screen), g_vi->visual, AllocNone);
    g_swa.colormap = colormap;
    g_swa.border_pixel = 0;
    g_swa.event_mask = StructureNotifyMask |
        KeyPressMask | KeyReleaseMask |
        ButtonPressMask | ButtonReleaseMask;

    return true;
}

#endif
