PageRenderTime 102ms CodeModel.GetById 41ms app.highlight 4ms RepoModel.GetById 56ms app.codeStats 0ms

/error.ml

http://github.com/dmsh/ocaml-xlib
OCaml | 47 lines | 36 code | 8 blank | 3 comment | 3 complexity | a6568f63cd6737a2bc162362dbaac5a3 MD5 | raw file
 1(* error.ml, from: http://www.u-picardie.fr/~ferment/xwindow/erreur.htm *)
 2(* tests the error handler *)
 3open Xlib
 4
 5let errorHandler ~dpy ~event =
 6  Printf.printf "#### ERROR HANDLER ####\n%!";
 7;;
 8
 9let () =
10  let display = xOpenDisplay "" in
11  let screen = xDefaultScreen display in
12  let gc = xDefaultGC display screen in
13  let root = xRootWindow display screen in
14  let white_pixel = xWhitePixel display screen in
15  let black_pixel = xBlackPixel display screen in
16  let win = xCreateSimpleWindow display root 0 0 200 150 2
17                                black_pixel white_pixel in
18  xSelectInput display win [ExposureMask; ButtonPressMask];
19  xStoreName display win "erreur";
20  xMapWindow display win;
21
22  (* will produce an error which will be handled by the error handler *)
23  let wim = (Obj.magic 0 : Xlib.window) in
24
25  xSetErrorHandler errorHandler;
26
27  let ev = new_xEvent() in
28  while true do
29    xNextEvent display ev;
30    match xEventType ev with
31    | Expose ->
32        xDrawString display win gc 10 30 "Hello !";
33
34    | ButtonPress ->
35        let d = xButtonEvent_datas(to_xButtonEvent ev) in
36        if ((d.button_x + d.button_y) mod 2) = 0 then begin
37          xDrawString display win gc (d.button_x)     (d.button_y) "hi";
38          xDrawString display win gc (d.button_x +20) (d.button_y) "ho";
39        end else begin
40          xDrawString display win gc (d.button_x)     (d.button_y) "hi";
41          xDrawString display wim gc (d.button_x +20) (d.button_y) "ho";
42        end;
43
44    | _ -> ()
45  done;
46;;
47