(*========================================================================
 glfw version
 ========================================================================*)

let version_major  =   2;;
let version_minor  =   4;;
let version_revision =  0;;


(*========================================================================
 Input handling definitions
 ========================================================================*)

(* Key and button state/action definitions *)
let release         =   0;;
let press           =   1;;

(* Keyboard key definitions: 8-bit ISO-8859-1 (Latin 1) encoding is used 
 for printable keys (such as A-Z, 0-9 etc), and values above 256
 represent special (non-printable) keys (e.g. F1, Page Up etc). *)
let key_unknown      = -1;;
let key_space        = 32;;
let key_special      = 256;;
let key_esc          = (key_special+1);;
let key_f1           = (key_special+2);;
let key_f2           = (key_special+3);;
let key_f3           = (key_special+4);;
let key_f4           = (key_special+5);;
let key_f5           = (key_special+6);;
let key_f6           = (key_special+7);;
let key_f7           = (key_special+8);;
let key_f8           = (key_special+9);;
let key_f9           = (key_special+10);;
let key_f10          = (key_special+11);;
let key_f11          = (key_special+12);;
let key_f12          = (key_special+13);;
let key_f13          = (key_special+14);;
let key_f14          = (key_special+15);;
let key_f15          = (key_special+16);;
let key_f16          = (key_special+17);;
let key_f17          = (key_special+18);;
let key_f18          = (key_special+19);;
let key_f19          = (key_special+20);;
let key_f20          = (key_special+21);;
let key_f21          = (key_special+22);;
let key_f22          = (key_special+23);;
let key_f23          = (key_special+24);;
let key_f24          = (key_special+25);;
let key_f25          = (key_special+26);;
let key_up           = (key_special+27);;
let key_down         = (key_special+28);;
let key_left         = (key_special+29);;
let key_right        = (key_special+30);;
let key_lshift       = (key_special+31);;
let key_rshift       = (key_special+32);;
let key_lctrl        = (key_special+33);;
let key_rctrl        = (key_special+34);;
let key_lalt         = (key_special+35);;
let key_ralt         = (key_special+36);;
let key_tab          = (key_special+37);;
let key_enter        = (key_special+38);;
let key_backspace    = (key_special+39);;
let key_insert       = (key_special+40);;
let key_del          = (key_special+41);;
let key_pageup       = (key_special+42);;
let key_pagedown     = (key_special+43);;
let key_home         = (key_special+44);;
let key_end          = (key_special+45);;
let key_kp_0         = (key_special+46);;
let key_kp_1         = (key_special+47);;
let key_kp_2         = (key_special+48);;
let key_kp_3         = (key_special+49);;
let key_kp_4         = (key_special+50);;
let key_kp_5         = (key_special+51);;
let key_kp_6         = (key_special+52);;
let key_kp_7         = (key_special+53);;
let key_kp_8         = (key_special+54);;
let key_kp_9         = (key_special+55);;
let key_kp_divide    = (key_special+56);;
let key_kp_multiply  = (key_special+57);;
let key_kp_subtract  = (key_special+58);;
let key_kp_add       = (key_special+59);;
let key_kp_decimal   = (key_special+60);;
let key_kp_equal     = (key_special+61);;
let key_kp_enter     = (key_special+62);;
let key_last         = key_kp_enter;;

(* Mouse button definitions *)
let mouse_button_left    = 0;;
let mouse_button_right   = 1;;
let mouse_button_middle  = 2;;
let mouse_button_last    = mouse_button_middle;;

(* Joystick identifiers *)
let joystick_1           = 0;;
let joystick_2           = 1;;
let joystick_3           = 2;;
let joystick_4           = 3;;
let joystick_5           = 4;;
let joystick_6           = 5;;
let joystick_7           = 6;;
let joystick_8           = 7;;
let joystick_9           = 8;;
let joystick_10          = 9;;
let joystick_11          = 10;;
let joystick_12          = 11;;
let joystick_13          = 12;;
let joystick_14          = 13;;
let joystick_15          = 14;;
let joystick_16          = 15;;
let joystick_last        = joystick_16;;


(*========================================================================
 Other definitions
  ========================================================================*)

(* glfwOpenWindow modes *)
let window               = 0x00010001;;
let fullscreen           = 0x00010002;;

(* glfwGetWindowParam tokens *)
let opened               = 0x00020001;;
let active               = 0x00020002;;
let iconified            = 0x00020003;;
let accelerated          = 0x00020004;;
let red_bits             = 0x00020005;;
let green_bits           = 0x00020006;;
let blue_bits            = 0x00020007;;
let alpha_bits           = 0x00020008;;
let depth_bits           = 0x00020009;;
let stencil_bits         = 0x0002000a;;

(* the following constants are used for both glfwgetwindowparam
 and glfwopenwindowhint *)
let refresh_rate         = 0x0002000b;;
let accum_red_bits       = 0x0002000c;;
let accum_green_bits     = 0x0002000d;;
let accum_blue_bits      = 0x0002000e;;
let accum_alpha_bits     = 0x0002000f;;
let aux_buffers          = 0x00020010;;
let stereo               = 0x00020011;;

(* glfwenable/glfwdisable tokens *)
let mouse_cursor         = 0x00030001;;
let sticky_keys          = 0x00030002;;
let sticky_mouse_buttons = 0x00030003;;
let system_keys          = 0x00030004;;
let key_repeat           = 0x00030005;;
let auto_poll_events     = 0x00030006;;

(* glfwwaitthread wait modes *)
let wait                 = 0x00040001;;
let nowait               = 0x00040002;;

(* glfwgetjoystickparam tokens *)
let present              = 0x00050001;;
let axes                 = 0x00050002;;
let buttons              = 0x00050003;;

(* glfwreadimage/glfwloadtexture2d flags *)
let no_rescale_bit       = 0x00000001;; (* only for glfwreadimage *)
let origin_ul_bit        = 0x00000002;;
let build_mipmaps_bit    = 0x00000004;; (* only for glfwloadtexture2d *)

(* time spans longer than this (seconds) are considered to be infinity *)
let infinity = 100000.0;;

(* File generated from glfw.idl *)

type vidmode = {
  vidmode_width: int;
  vidmode_height: int;
  vidmode_red_bits: int;
  vidmode_blue_bits: int;
  vidmode_green_bits: int;
}
and image = {
  image_width: int;
  image_height: int;
  image_format: int;
  image_bytes_per_pixel: int;
  image_data: char option;
}

external init : unit -> int
	= "camlidl_glfw_glfwInit"

external terminate : unit -> unit
	= "camlidl_glfw_glfwTerminate"

external get_version : unit -> int * int * int
	= "camlidl_glfw_glfwGetVersion"

external open_window : int -> int -> int -> int -> int -> int -> int -> int -> int -> int
	= "camlidl_glfw_glfwOpenWindow_bytecode" "camlidl_glfw_glfwOpenWindow"

external open_window_hint : int -> int -> unit
	= "camlidl_glfw_glfwOpenWindowHint"

external close_window : unit -> unit
	= "camlidl_glfw_glfwCloseWindow"

external set_window_title : string -> unit
	= "camlidl_glfw_glfwSetWindowTitle"

external get_window_size : unit -> int * int
	= "camlidl_glfw_glfwGetWindowSize"

external set_window_size : int -> int -> unit
	= "camlidl_glfw_glfwSetWindowSize"

external set_window_pos : int -> int -> unit
	= "camlidl_glfw_glfwSetWindowPos"

external iconify_window : unit -> unit
	= "camlidl_glfw_glfwIconifyWindow"

external restore_window : unit -> unit
	= "camlidl_glfw_glfwRestoreWindow"

external swap_buffers : unit -> unit
	= "camlidl_glfw_glfwSwapBuffers"

external swap_interval : int -> unit
	= "camlidl_glfw_glfwSwapInterval"

external get_window_param : int -> int
	= "camlidl_glfw_glfwGetWindowParam"

external get_video_modes : int -> int * vidmode array
	= "camlidl_glfw_glfwGetVideoModes"

external get_desktop_mode : unit -> vidmode
	= "camlidl_glfw_glfwGetDesktopMode"

external poll_events : unit -> unit
	= "camlidl_glfw_glfwPollEvents"

external get_key : int -> int
	= "camlidl_glfw_glfwGetKey"

external get_mouse_button : int -> int
	= "camlidl_glfw_glfwGetMouseButton"

external get_mouse_pos : unit -> int * int
	= "camlidl_glfw_glfwGetMousePos"

external set_mouse_pos : int -> int -> unit
	= "camlidl_glfw_glfwSetMousePos"

external get_mouse_wheel : unit -> int
	= "camlidl_glfw_glfwGetMouseWheel"

external set_mouse_wheel : int -> unit
	= "camlidl_glfw_glfwSetMouseWheel"

external get_joystick_param : int -> int -> int
	= "camlidl_glfw_glfwGetJoystickParam"

external get_joystick_pos : int -> int -> int * float
	= "camlidl_glfw_glfwGetJoystickPos"

external get_joystick_buttons : int -> int -> int * char
	= "camlidl_glfw_glfwGetJoystickButtons"

external get_time : unit -> float
	= "camlidl_glfw_glfwGetTime"

external set_time : float -> unit
	= "camlidl_glfw_glfwSetTime"

external sleep : float -> unit
	= "camlidl_glfw_glfwSleep"

external extension_supported : string -> int
	= "camlidl_glfw_glfwExtensionSupported"

external get_gl_version : unit -> int * int * int
	= "camlidl_glfw_glfwGetGLVersion"

external get_number_of_processors : unit -> int
	= "camlidl_glfw_glfwGetNumberOfProcessors"

external enable : int -> unit
	= "camlidl_glfw_glfwEnable"

external disable : int -> unit
	= "camlidl_glfw_glfwDisable"

external read_image : string -> int -> int * image
	= "camlidl_glfw_glfwReadImage"

external free_image : image option -> unit
	= "camlidl_glfw_glfwFreeImage"

external load_texture_2d : string -> int -> int
	= "camlidl_glfw_glfwLoadTexture2D"

external proto_swsc: unit -> unit = "ml_glfwProtoswsc"
let set_window_size_callback (f:int -> int -> unit) =
    Callback.register "ml_setwindowsizecallback" f;
    proto_swsc ()

external proto_skc: unit -> unit = "ml_glfwProtoskc"
let set_key_callback (f: int -> bool -> unit) =
    Callback.register "ml_setkeycallback" f;
    proto_skc ()

external proto_scc: unit -> unit = "ml_glfwProtoscc"
let set_char_callback (f: int -> bool -> unit) =
    Callback.register "ml_setcharcallback" f;
    proto_scc ()
    
    
external proto_mpc: unit -> unit = "ml_glfwProtompc"
let set_mouse_pos_callback (f: int -> int -> unit) =
    Callback.register "ml_setmouseposcallback" f;
    proto_mpc ()

external proto_mbc: unit -> unit = "ml_glfwProtombc"
let set_mouse_button_callback (f: int -> bool -> unit) =
    Callback.register "ml_setmousebuttoncallback" f;
    proto_mbc ()

external proto_mwc: unit -> unit = "ml_glfwProtomwc"
let set_mouse_wheel_callback (f: int -> unit) =
    Callback.register "ml_setmousewheelcallback" f;
    proto_mwc ()
