open Glfw

let r_run = ref true
let r_keyrepeat = ref false
let r_systemkeys = ref false

let key k p =
    let d s = Printf.printf "%s\n" s; flush stdout in
    if p then match k with
    | k when k = key_esc -> r_run := false; d "esc => quit program"
    | k when k = key_f1 -> d "f1"
    | k when k = key_f2 -> d "f2"
    | k when k = key_f3 -> d "f3"
    | k when k = key_f4 -> d "f4"
    | k when k = key_f5 -> d "f5"
    | k when k = key_f6 -> d "f6"
    | k when k = key_f7 -> d "f7"
    | k when k = key_f8 -> d "f8"
    | k when k = key_f9 -> d "f9"
    | k when k = key_f10 -> d "f10"
    | k when k = key_f11 -> d "f11"
    | k when k = key_f12 -> d "f12"
    | k when k = key_up -> d "up"
    | k when k = key_down -> d "down"
    | k when k = key_left -> d "left"
    | k when k = key_right -> d "right"
    | k when k = key_lshift -> d "lshift"
    | k when k = key_rshift -> d "rshift"
    | k when k = key_lalt -> d "lalt"
    | k when k = key_ralt -> d "ralt"
    | k when k = key_lctrl -> d "lctrl"
    | k when k = key_rctrl -> d "rctrl"
    | k when k = key_tab -> d "tab"
    | k when k = key_backspace -> d "backspace"
    | k when k = key_insert -> d "insert"
    | k when k = key_del -> d "del"
    | k when k = key_pageup -> d "pageup"
    | k when k = key_pagedown -> d "pagedown"
    | k when k = key_home -> d "home"
    | k when k = key_end -> d "end"
    | k when k = key_kp_0 -> d "keypad 0"
    | k when k = key_kp_1 -> d "keypad 1"
    | k when k = key_kp_2 -> d "keypad 2"
    | k when k = key_kp_3 -> d "keypad 3"
    | k when k = key_kp_4 -> d "keypad 4"
    | k when k = key_kp_5 -> d "keypad 5"
    | k when k = key_kp_6 -> d "keypad 6"
    | k when k = key_kp_7 -> d "keypad 7"
    | k when k = key_kp_8 -> d "keypad 8"
    | k when k = key_kp_9 -> d "keypad 9"
    | k when k = key_kp_divide -> d "keypad divide"
    | k when k = key_kp_multiply -> d "keypad multiply"
    | k when k = key_kp_subtract -> d "keypad subtract"
    | k when k = key_kp_add -> d "keypad add"
    | k when k = key_kp_decimal -> d "keypad decimal"
    | k when k = key_kp_enter -> d "keypad enter"
    | k when k = key_space -> d "space"
    | k when k = int_of_char 'R' -> 
	r_keyrepeat := not !r_keyrepeat;
	(if !r_keyrepeat then enable else disable) key_repeat;
	d ("R => Key repeat: "^string_of_bool !r_keyrepeat)
    | k when k = int_of_char 'S' -> 
	r_systemkeys := not !r_systemkeys;
	(if !r_systemkeys then enable else disable) system_keys;
	d ("S => System keys: "^string_of_bool !r_systemkeys)
    | k -> Printf.printf "%c\n" (char_of_int k); flush stdout
    

let _ =
    try
	if (init () = 0) then failwith "init failed";
	if ((open_window 250 100 0 0 0 0 0 0 window) = 0) then failwith "open_window failed";
	set_window_title "Press some keys!";
	set_key_callback key;
	let rec g () =
	    if not !r_run then () 
	    else begin
(*		
		Gl.glClearColor (0.5 +. 0.5 *. sin (3.0 *. Unix.gettimeofday ())) 0.0 0.0 0.0;
		Gl.glClear Gl.cgl_color_buffer_bit;
*)
		swap_buffers ();
		r_run := !r_run && (get_window_param opened <> 0);
		g ();
	    end
	in
	g ();
	terminate ()
    with Failure s -> Printf.eprintf "Failure: %s\n" s

