
#include "Jtk.h"

#ifdef JTK_X

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

static Bool WaitForNotify(Display * display, XEvent * event, XPointer arg)
{
    return (event->type == MapNotify) && (event->xmap.window == (Window)arg);
}

void * Jtk_CreateWindow()
{
    XEvent event;
    Window window = XCreateWindow(g_display,
            RootWindow(g_display, g_vi->screen),
            0, 0, 800, 800, 0, g_vi->depth, InputOutput, g_vi->visual,
            CWBorderPixel | CWColormap | CWEventMask, &g_swa);
    XMapWindow(g_display, window);
    XIfEvent(g_display, &event, WaitForNotify, (XPointer)window);
    if (glXMakeCurrent(g_display, window, g_context) == False)
    {
        fprintf(stderr, "glXMakeCurrent() failure\n");
        XDestroyWindow(g_display, window);
        return nullptr;
    }

    return (void *)window;
}

void Jtk_SwapBuffers(void * window)
{
    glXSwapBuffers(g_display, (Window)window);
}

void Jtk_CloseWindow(void * window)
{
    XDestroyWindow(g_display, (Window)window);
}

#endif
