/* File generated from glfw.idl */

#include <stddef.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/callback.h>
#ifdef Custom_tag
#include <caml/custom.h>
#include <caml/bigarray.h>
#endif
#include <caml/camlidlruntime.h>


#include "glfw.h"

void camlidl_ml2c_glfw_struct_GLFWvidmode(value _v1, struct GLFWvidmode * _c2, camlidl_ctx _ctx)
{
  value _v3;
  value _v4;
  value _v5;
  value _v6;
  value _v7;
  _v3 = Field(_v1, 0);
  (*_c2).Width = Int_val(_v3);
  _v4 = Field(_v1, 1);
  (*_c2).Height = Int_val(_v4);
  _v5 = Field(_v1, 2);
  (*_c2).RedBits = Int_val(_v5);
  _v6 = Field(_v1, 3);
  (*_c2).BlueBits = Int_val(_v6);
  _v7 = Field(_v1, 4);
  (*_c2).GreenBits = Int_val(_v7);
}

value camlidl_c2ml_glfw_struct_GLFWvidmode(struct GLFWvidmode * _c1, camlidl_ctx _ctx)
{
  value _v2;
  value _v3[5];
  memset(_v3, 0, 5 * sizeof(value));
  Begin_roots_block(_v3, 5)
    _v3[0] = Val_int((*_c1).Width);
    _v3[1] = Val_int((*_c1).Height);
    _v3[2] = Val_int((*_c1).RedBits);
    _v3[3] = Val_int((*_c1).BlueBits);
    _v3[4] = Val_int((*_c1).GreenBits);
    _v2 = camlidl_alloc_small(5, 0);
    { mlsize_t _c4;
      for (_c4 = 0; _c4 < 5; _c4++) Field(_v2, _c4) = _v3[_c4];
    }
  End_roots()
  return _v2;
}

void camlidl_ml2c_glfw_struct_GLFWimage(value _v1, struct GLFWimage * _c2, camlidl_ctx _ctx)
{
  value _v3;
  value _v4;
  value _v5;
  value _v6;
  value _v7;
  value _v8;
  _v3 = Field(_v1, 0);
  (*_c2).Width = Int_val(_v3);
  _v4 = Field(_v1, 1);
  (*_c2).Height = Int_val(_v4);
  _v5 = Field(_v1, 2);
  (*_c2).Format = Int_val(_v5);
  _v6 = Field(_v1, 3);
  (*_c2).BytesPerPixel = Int_val(_v6);
  _v7 = Field(_v1, 4);
  if (_v7 == Val_int(0)) {
    (*_c2).Data = NULL;
  } else {
    _v8 = Field(_v7, 0);
    (*_c2).Data = (unsigned char  *) camlidl_malloc(sizeof(unsigned char ), _ctx);
    *(*_c2).Data = Int_val(_v8);
  }
}

value camlidl_c2ml_glfw_struct_GLFWimage(struct GLFWimage * _c1, camlidl_ctx _ctx)
{
  value _v2;
  value _v3[5];
  value _v4;
  memset(_v3, 0, 5 * sizeof(value));
  Begin_roots_block(_v3, 5)
    _v3[0] = Val_int((*_c1).Width);
    _v3[1] = Val_int((*_c1).Height);
    _v3[2] = Val_int((*_c1).Format);
    _v3[3] = Val_int((*_c1).BytesPerPixel);
    if ((*_c1).Data == NULL) {
      _v3[4] = Val_int(0);
    } else {
      _v4 = Val_int(*(*_c1).Data);
      Begin_root(_v4)
        _v3[4] = camlidl_alloc_small(1, 0);
        Field(_v3[4], 0) = _v4;
      End_roots();
    }
    _v2 = camlidl_alloc_small(5, 0);
    { mlsize_t _c5;
      for (_c5 = 0; _c5 < 5; _c5++) Field(_v2, _c5) = _v3[_c5];
    }
  End_roots()
  return _v2;
}

value camlidl_glfw_glfwInit(value _unit)
{
  int _res;
  value _vres;

  _res = glfwInit();
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwTerminate(value _unit)
{
  glfwTerminate();
  return Val_unit;
}

value camlidl_glfw_glfwGetVersion(value _unit)
{
  int *major; /*out*/
  int *minor; /*out*/
  int *rev; /*out*/
  int _c1;
  int _c2;
  int _c3;
  value _vresult;
  value _vres[3] = { 0, 0, 0, };

  major = &_c1;
  minor = &_c2;
  rev = &_c3;
  glfwGetVersion(major, minor, rev);
  Begin_roots_block(_vres, 3)
    _vres[0] = Val_int(*major);
    _vres[1] = Val_int(*minor);
    _vres[2] = Val_int(*rev);
    _vresult = camlidl_alloc_small(3, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
    Field(_vresult, 2) = _vres[2];
  End_roots()
  return _vresult;
}

value camlidl_glfw_glfwOpenWindow(
	value _v_width,
	value _v_height,
	value _v_redbits,
	value _v_greenbits,
	value _v_bluebits,
	value _v_alphabits,
	value _v_depthbits,
	value _v_stencilbits,
	value _v_mode)
{
  int width; /*in*/
  int height; /*in*/
  int redbits; /*in*/
  int greenbits; /*in*/
  int bluebits; /*in*/
  int alphabits; /*in*/
  int depthbits; /*in*/
  int stencilbits; /*in*/
  int mode; /*in*/
  int _res;
  value _vres;

  width = Int_val(_v_width);
  height = Int_val(_v_height);
  redbits = Int_val(_v_redbits);
  greenbits = Int_val(_v_greenbits);
  bluebits = Int_val(_v_bluebits);
  alphabits = Int_val(_v_alphabits);
  depthbits = Int_val(_v_depthbits);
  stencilbits = Int_val(_v_stencilbits);
  mode = Int_val(_v_mode);
  _res = glfwOpenWindow(width, height, redbits, greenbits, bluebits, alphabits, depthbits, stencilbits, mode);
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwOpenWindow_bytecode(value * argv, int argn)
{
  return camlidl_glfw_glfwOpenWindow(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
}

value camlidl_glfw_glfwOpenWindowHint(
	value _v_target,
	value _v_hint)
{
  int target; /*in*/
  int hint; /*in*/
  target = Int_val(_v_target);
  hint = Int_val(_v_hint);
  glfwOpenWindowHint(target, hint);
  return Val_unit;
}

value camlidl_glfw_glfwCloseWindow(value _unit)
{
  glfwCloseWindow();
  return Val_unit;
}

value camlidl_glfw_glfwSetWindowTitle(
	value _v_title)
{
  char const *title; /*in*/
  title = String_val(_v_title);
  glfwSetWindowTitle(title);
  return Val_unit;
}

value camlidl_glfw_glfwGetWindowSize(value _unit)
{
  int *width; /*out*/
  int *height; /*out*/
  int _c1;
  int _c2;
  value _vresult;
  value _vres[2] = { 0, 0, };

  width = &_c1;
  height = &_c2;
  glfwGetWindowSize(width, height);
  Begin_roots_block(_vres, 2)
    _vres[0] = Val_int(*width);
    _vres[1] = Val_int(*height);
    _vresult = camlidl_alloc_small(2, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
  End_roots()
  return _vresult;
}

value camlidl_glfw_glfwSetWindowSize(
	value _v_width,
	value _v_height)
{
  int width; /*in*/
  int height; /*in*/
  width = Int_val(_v_width);
  height = Int_val(_v_height);
  glfwSetWindowSize(width, height);
  return Val_unit;
}

value camlidl_glfw_glfwSetWindowPos(
	value _v_x,
	value _v_y)
{
  int x; /*in*/
  int y; /*in*/
  x = Int_val(_v_x);
  y = Int_val(_v_y);
  glfwSetWindowPos(x, y);
  return Val_unit;
}

value camlidl_glfw_glfwIconifyWindow(value _unit)
{
  glfwIconifyWindow();
  return Val_unit;
}

value camlidl_glfw_glfwRestoreWindow(value _unit)
{
  glfwRestoreWindow();
  return Val_unit;
}

value camlidl_glfw_glfwSwapBuffers(value _unit)
{
  glfwSwapBuffers();
  return Val_unit;
}

value camlidl_glfw_glfwSwapInterval(
	value _v_interval)
{
  int interval; /*in*/
  interval = Int_val(_v_interval);
  glfwSwapInterval(interval);
  return Val_unit;
}

value camlidl_glfw_glfwGetWindowParam(
	value _v_param)
{
  int param; /*in*/
  int _res;
  value _vres;

  param = Int_val(_v_param);
  _res = glfwGetWindowParam(param);
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwGetVideoModes(
	value _v_len)
{
  struct GLFWvidmode *list; /*out*/
  int len; /*in*/
  int _res;
  struct camlidl_ctx_struct _ctxs = { CAMLIDL_TRANSIENT, NULL };
  camlidl_ctx _ctx = &_ctxs;
  mlsize_t _c1;
  value _v2;
  value _vresult;
  value _vres[2] = { 0, 0, };

  len = Int_val(_v_len);
  list = camlidl_malloc(len * sizeof(struct GLFWvidmode ), _ctx);
  _res = glfwGetVideoModes(list, len);
  Begin_roots_block(_vres, 2)
    _vres[0] = Val_int(_res);
    _vres[1] = camlidl_alloc(len, 0);
    Begin_root(_vres[1])
      for (_c1 = 0; _c1 < len; _c1++) {
        _v2 = camlidl_c2ml_glfw_struct_GLFWvidmode(&list[_c1], _ctx);
        modify(&Field(_vres[1], _c1), _v2);
      }
    End_roots()
    _vresult = camlidl_alloc_small(2, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
  End_roots()
  camlidl_free(_ctx);
  return _vresult;
}

value camlidl_glfw_glfwGetDesktopMode(value _unit)
{
  struct GLFWvidmode *mode; /*out*/
  struct GLFWvidmode _c1;
  value _vres;

  struct camlidl_ctx_struct _ctxs = { CAMLIDL_TRANSIENT, NULL };
  camlidl_ctx _ctx = &_ctxs;
  mode = &_c1;
  glfwGetDesktopMode(mode);
  _vres = camlidl_c2ml_glfw_struct_GLFWvidmode(&*mode, _ctx);
  camlidl_free(_ctx);
  return _vres;
}

value camlidl_glfw_glfwPollEvents(value _unit)
{
  glfwPollEvents();
  return Val_unit;
}

value camlidl_glfw_glfwGetKey(
	value _v_key)
{
  int key; /*in*/
  int _res;
  value _vres;

  key = Int_val(_v_key);
  _res = glfwGetKey(key);
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwGetMouseButton(
	value _v_button)
{
  int button; /*in*/
  int _res;
  value _vres;

  button = Int_val(_v_button);
  _res = glfwGetMouseButton(button);
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwGetMousePos(value _unit)
{
  int *xpos; /*out*/
  int *ypos; /*out*/
  int _c1;
  int _c2;
  value _vresult;
  value _vres[2] = { 0, 0, };

  xpos = &_c1;
  ypos = &_c2;
  glfwGetMousePos(xpos, ypos);
  Begin_roots_block(_vres, 2)
    _vres[0] = Val_int(*xpos);
    _vres[1] = Val_int(*ypos);
    _vresult = camlidl_alloc_small(2, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
  End_roots()
  return _vresult;
}

value camlidl_glfw_glfwSetMousePos(
	value _v_xpos,
	value _v_ypos)
{
  int xpos; /*in*/
  int ypos; /*in*/
  xpos = Int_val(_v_xpos);
  ypos = Int_val(_v_ypos);
  glfwSetMousePos(xpos, ypos);
  return Val_unit;
}

value camlidl_glfw_glfwGetMouseWheel(value _unit)
{
  int _res;
  value _vres;

  _res = glfwGetMouseWheel();
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwSetMouseWheel(
	value _v_pos)
{
  int pos; /*in*/
  pos = Int_val(_v_pos);
  glfwSetMouseWheel(pos);
  return Val_unit;
}

value camlidl_glfw_glfwGetJoystickParam(
	value _v_joy,
	value _v_param)
{
  int joy; /*in*/
  int param; /*in*/
  int _res;
  value _vres;

  joy = Int_val(_v_joy);
  param = Int_val(_v_param);
  _res = glfwGetJoystickParam(joy, param);
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwGetJoystickPos(
	value _v_joy,
	value _v_numaxes)
{
  int joy; /*in*/
  float *pos; /*out*/
  int numaxes; /*in*/
  int _res;
  float _c1;
  value _vresult;
  value _vres[2] = { 0, 0, };

  joy = Int_val(_v_joy);
  numaxes = Int_val(_v_numaxes);
  pos = &_c1;
  _res = glfwGetJoystickPos(joy, pos, numaxes);
  Begin_roots_block(_vres, 2)
    _vres[0] = Val_int(_res);
    _vres[1] = copy_double(*pos);
    _vresult = camlidl_alloc_small(2, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
  End_roots()
  return _vresult;
}

value camlidl_glfw_glfwGetJoystickButtons(
	value _v_joy,
	value _v_numbuttons)
{
  int joy; /*in*/
  unsigned char *buttons; /*out*/
  int numbuttons; /*in*/
  int _res;
  unsigned char _c1;
  value _vresult;
  value _vres[2] = { 0, 0, };

  joy = Int_val(_v_joy);
  numbuttons = Int_val(_v_numbuttons);
  buttons = &_c1;
  _res = glfwGetJoystickButtons(joy, buttons, numbuttons);
  Begin_roots_block(_vres, 2)
    _vres[0] = Val_int(_res);
    _vres[1] = Val_int(*buttons);
    _vresult = camlidl_alloc_small(2, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
  End_roots()
  return _vresult;
}

value camlidl_glfw_glfwGetTime(value _unit)
{
  double _res;
  value _vres;

  _res = glfwGetTime();
  _vres = copy_double(_res);
  return _vres;
}

value camlidl_glfw_glfwSetTime(
	value _v_time)
{
  double time; /*in*/
  time = Double_val(_v_time);
  glfwSetTime(time);
  return Val_unit;
}

value camlidl_glfw_glfwSleep(
	value _v_time)
{
  double time; /*in*/
  time = Double_val(_v_time);
  glfwSleep(time);
  return Val_unit;
}

value camlidl_glfw_glfwExtensionSupported(
	value _v_extension)
{
  char const *extension; /*in*/
  int _res;
  value _vres;

  extension = String_val(_v_extension);
  _res = glfwExtensionSupported(extension);
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwGetGLVersion(value _unit)
{
  int *major; /*out*/
  int *minor; /*out*/
  int *rev; /*out*/
  int _c1;
  int _c2;
  int _c3;
  value _vresult;
  value _vres[3] = { 0, 0, 0, };

  major = &_c1;
  minor = &_c2;
  rev = &_c3;
  glfwGetGLVersion(major, minor, rev);
  Begin_roots_block(_vres, 3)
    _vres[0] = Val_int(*major);
    _vres[1] = Val_int(*minor);
    _vres[2] = Val_int(*rev);
    _vresult = camlidl_alloc_small(3, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
    Field(_vresult, 2) = _vres[2];
  End_roots()
  return _vresult;
}

value camlidl_glfw_glfwGetNumberOfProcessors(value _unit)
{
  int _res;
  value _vres;

  _res = glfwGetNumberOfProcessors();
  _vres = Val_int(_res);
  return _vres;
}

value camlidl_glfw_glfwEnable(
	value _v_token)
{
  int token; /*in*/
  token = Int_val(_v_token);
  glfwEnable(token);
  return Val_unit;
}

value camlidl_glfw_glfwDisable(
	value _v_token)
{
  int token; /*in*/
  token = Int_val(_v_token);
  glfwDisable(token);
  return Val_unit;
}

value camlidl_glfw_glfwReadImage(
	value _v_name,
	value _v_flags)
{
  char const *name; /*in*/
  struct GLFWimage *img; /*out*/
  int flags; /*in*/
  int _res;
  struct camlidl_ctx_struct _ctxs = { CAMLIDL_TRANSIENT, NULL };
  camlidl_ctx _ctx = &_ctxs;
  struct GLFWimage _c1;
  value _vresult;
  value _vres[2] = { 0, 0, };

  name = String_val(_v_name);
  flags = Int_val(_v_flags);
  img = &_c1;
  _res = glfwReadImage(name, img, flags);
  Begin_roots_block(_vres, 2)
    _vres[0] = Val_int(_res);
    _vres[1] = camlidl_c2ml_glfw_struct_GLFWimage(&*img, _ctx);
    _vresult = camlidl_alloc_small(2, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
  End_roots()
  camlidl_free(_ctx);
  return _vresult;
}

value camlidl_glfw_glfwFreeImage(
	value _v_img)
{
  struct GLFWimage *img; /*in*/
  value _v1;
  struct GLFWimage _c2;
  struct camlidl_ctx_struct _ctxs = { CAMLIDL_TRANSIENT, NULL };
  camlidl_ctx _ctx = &_ctxs;
  if (_v_img == Val_int(0)) {
    img = NULL;
  } else {
    _v1 = Field(_v_img, 0);
    img = &_c2;
    camlidl_ml2c_glfw_struct_GLFWimage(_v1, &_c2, _ctx);
  }
  glfwFreeImage(img);
  camlidl_free(_ctx);
  return Val_unit;
}

value camlidl_glfw_glfwLoadTexture2D(
	value _v_name,
	value _v_flags)
{
  char const *name; /*in*/
  int flags; /*in*/
  int _res;
  value _vres;

  name = String_val(_v_name);
  flags = Int_val(_v_flags);
  _res = glfwLoadTexture2D(name, flags);
  _vres = Val_int(_res);
  return _vres;
}



static void __protoswsc(int a,int b)
{
    callback2(*caml_named_value("ml_setwindowsizecallback"),Val_int(a),Val_int(b));
}

CAMLprim void ml_glfwProtoswsc(value unit)
{
    CAMLparam0();
    glfwSetWindowSizeCallback(__protoswsc);
    CAMLreturn0;
}

static void __protoskc(int a,int b)
{
    callback2(*caml_named_value("ml_setkeycallback"),Val_int(a),Val_bool(b));
}

CAMLprim void ml_glfwProtoskc(value unit)
{
    CAMLparam0();
    glfwSetKeyCallback(__protoskc);
    CAMLreturn0;
}

static void __protoscc(int a,int b)
{
    callback2(*caml_named_value("ml_setcharcallback"),Val_int(a),Val_bool(b));
}

CAMLprim void ml_glfwProtoscc(value unit)
{
    CAMLparam0();
    glfwSetCharCallback(__protoscc);
    CAMLreturn0;
}

static void __protombc(int a,int b)
{
    callback2(*caml_named_value("ml_setmousebuttoncallback"),Val_int(a),Val_bool(b));
}

CAMLprim void ml_glfwProtombc(value unit)
{
    CAMLparam0();
    glfwSetMouseButtonCallback(__protombc);
    CAMLreturn0;
}

static void __protompc(int a,int b)
{
    callback2(*caml_named_value("ml_setmouseposcallback"),Val_int(a),Val_int(b));
}

CAMLprim void ml_glfwProtompc(value unit)
{
    CAMLparam0();
    glfwSetMousePosCallback(__protompc);
    CAMLreturn0;
}

static void __protomwc(int a)
{
    callback(*caml_named_value("ml_setmousewheelcallback"),Val_int(a));
}

CAMLprim void ml_glfwProtomwc(value unit)
{
    CAMLparam0();
    glfwSetMouseWheelCallback(__protomwc);
    CAMLreturn0;
}
