From: Michael D. Lowis Date: Wed, 6 Sep 2017 02:10:42 +0000 (-0400) Subject: added code to destructure events and call subfunctions for each X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=330ec80a16119726e7187b212638f01dd1fec1dd;p=archive%2Ftide-ocaml.git added code to destructure events and call subfunctions for each --- diff --git a/edit.ml b/edit.ml index 244938f..96f703a 100644 --- a/edit.ml +++ b/edit.ml @@ -1,22 +1,30 @@ open X11 + +let onfocus focused = () +let onkeypress mods rune = () +let onmousebtn mods btn x y pressed = () +let onmousedrag mods x y = () +let onupdate width height = () +let onshutdown = () + let () = - let win = X11.make_window 640 480 in - X11.show_window win true; - X11.event_loop 50 (function - | Focus _ -> print_endline "focus" - | KeyPress _ -> print_endline "keypress" - | MouseClick _ -> print_endline "mouseclick" - | MouseRelease _ -> print_endline "mouserelease" - | MouseDrag _ -> print_endline "mousedrag" - | Paste _ -> print_endline "paste" - | Resize _ -> print_endline "resize" - | Command _ -> print_endline "command" - | PipeClosed _ -> print_endline "pipeclosed" - | PipeWriteReady _ -> print_endline "pipewriteready" - | PipeReadReady _ -> print_endline "pipereadready" - | Update -> print_endline "update" - | Shutdown -> print_endline "shutdown" + let win = make_window 640 480 in + show_window win true; + event_loop 50 (function + | Focus state -> onfocus state + | KeyPress e -> onkeypress e.mods e.rune + | MouseClick e -> onmousebtn e.mods e.btn e.x e.y true + | MouseRelease e -> onmousebtn e.mods e.btn e.x e.y false + | MouseDrag e -> onmousedrag e.mods e.x e.y + | Paste e -> print_endline "paste" + | Command e -> print_endline "command" + | PipeClosed e -> print_endline "pipeclosed" + | PipeWriteReady e -> print_endline "pipewriteready" + | PipeReadReady e -> print_endline "pipereadready" + | Update e -> onupdate e.width e.height + | Shutdown -> onshutdown ) + (* let server = Tide.start_server () in let nargs = Array.length Sys.argv in diff --git a/lib/x11.ml b/lib/x11.ml index 7753f98..817d78c 100644 --- a/lib/x11.ml +++ b/lib/x11.ml @@ -1,13 +1,12 @@ type xatom type xwin type xevent = - | Focus of { focused: bool } + | Focus of bool | KeyPress of { mods: int; rune: int } | MouseClick of { mods: int; btn: int; x: int; y: int } | MouseRelease of { mods: int; btn: int; x: int; y: int } | MouseDrag of { mods: int; x: int; y: int } | Paste of { text: string } - (*| Resize of { height: int; width: int }*) | Command of { commands: string array } | PipeClosed of { fd: int } | PipeWriteReady of { fd: int } diff --git a/lib/x11_prims.c b/lib/x11_prims.c index afab8e7..7f0affd 100644 --- a/lib/x11_prims.c +++ b/lib/x11_prims.c @@ -162,6 +162,7 @@ CAMLprim value x11_show_window(value win, value state) { CAMLprim value x11_event_loop(value ms, value cbfn) { CAMLparam2(ms, cbfn); + CAMLlocal1( event ); while (X.running) { XEvent e; XPeekEvent(X.display, &e); bool pending = false; //pollfds(Int_val(ms), cbfn); @@ -176,11 +177,9 @@ CAMLprim value x11_event_loop(value ms, value cbfn) { XNextEvent(X.display, &e); if (XFilterEvent(&e, None)) continue; if (!EventHandlers[e.type]) continue; - value event = EventHandlers[e.type](&e); + event = EventHandlers[e.type](&e); if (event != Val_unit) caml_callback(cbfn, event); - else - puts("ignored"); } if (X.running) {