From: Michael D. Lowis Date: Thu, 7 Sep 2017 16:54:52 +0000 (-0400) Subject: Added draw rectangle fucntion to x11 X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ab594a75aeab5a2baa201fdf3923ef529e5abc4c;p=archive%2Ftide-ocaml.git Added draw rectangle fucntion to x11 --- diff --git a/edit.ml b/edit.ml index e128c7d..09d84b1 100644 --- a/edit.ml +++ b/edit.ml @@ -13,7 +13,9 @@ let onmousemove mods x y = print_endline "onmousemove" let onupdate width height = - print_endline "onupdate" + print_endline "onupdate"; + draw_rect { x = 0; y = 0; w = width; h = height; c = 0x55555555 }; + flip () let onshutdown () = print_endline "onshutdown" diff --git a/lib/x11.ml b/lib/x11.ml index 0d4b19d..0ca8db8 100644 --- a/lib/x11.ml +++ b/lib/x11.ml @@ -1,5 +1,8 @@ -type xatom -type xwin +type xatom (* X interned atom *) + +type xwin (* X window identifier *) + +(* X event definitions *) type xevent = | Focus of bool | KeyPress of { mods: int; rune: int } @@ -14,6 +17,9 @@ type xevent = | Update of { width: int; height: int } | Shutdown +(* rectangle description type *) +type xrect = { x: int; y: int; w: int; h: int; c: int; } + external connect : unit -> unit = "x11_connect" @@ -29,6 +35,12 @@ external make_dialog : int -> int -> xwin external show_window : xwin -> bool -> unit = "x11_show_window" +external flip : unit -> unit + = "x11_flip" + +external draw_rect : xrect -> unit + = "x11_draw_rect" + external event_loop : int -> (xevent -> unit) -> unit = "x11_event_loop" diff --git a/lib/x11_prims.c b/lib/x11_prims.c index 188f7e6..1b7f7c3 100644 --- a/lib/x11_prims.c +++ b/lib/x11_prims.c @@ -64,6 +64,7 @@ static char* readprop(Window win, Atom prop); static void create_window(int height, int width); static value mkrecord(int tag, int n, ...); static int32_t special_keys(int32_t key); +static void xftcolor(XftColor* xc, int c); static value ev_focus(XEvent*); static value ev_keypress(XEvent*); @@ -164,20 +165,40 @@ CAMLprim value x11_show_window(value win, value state) { CAMLreturn(Val_unit); } +CAMLprim value x11_flip(void) { + CAMLparam0(); + XCopyArea(X.display, X.pixmap, X.self, X.gc, 0, 0, X.width, X.height, 0, 0); + CAMLreturn(Val_unit); +} + +CAMLprim value x11_draw_rect(value rect) { + CAMLparam1(rect); + + XftColor clr; + xftcolor(&clr, Field(rect, 4)); + XftDrawRect(X.xft, &clr, Field(rect, 0), Field(rect, 1), /* x,y */ + Field(rect, 2), Field(rect, 3)); /* w,h */ + XftColorFree(X.display, X.visual, X.colormap, &clr); + + CAMLreturn(Val_unit); +} + 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); + //bool pending = false; //pollfds(Int_val(ms), cbfn); + int nevents = XEventsQueued(X.display, QueuedAfterFlush); /* Update the mouse posistion and simulate a mosuemove event for it */ - //Window xw; int _, x, y; unsigned int mods; - //XQueryPointer(X.display, X.self, &xw, &xw, &_, &_, &x, &y, &mods); - //caml_callback(cbfn, mkrecord(TMouseMove, 3, mods, x, y)); + Window xw; int _, x, y; unsigned int mods; + XQueryPointer(X.display, X.self, &xw, &xw, &_, &_, &x, &y, &mods); + caml_callback(cbfn, mkrecord(TMouseMove, 3, mods, x, y)); - if (pending || nevents) { + /* check if we have any pending xevents */ + if (nevents) { /* pare down irrelevant mouse drag events to just the latest */ XTimeCoord* coords = XGetMotionEvents( X.display, X.self, CurrentTime, CurrentTime, &nevents); @@ -192,12 +213,11 @@ CAMLprim value x11_event_loop(value ms, value cbfn) { if (event != Val_int(TNone)) caml_callback(cbfn, event); } - - if (X.running) { - caml_callback(cbfn, mkrecord(TUpdate, 2, X.width, X.height)); - XCopyArea(X.display, X.pixmap, X.self, X.gc, 0, 0, X.width, X.height, 0, 0); - } } + + /* generate an update event and flush any outgoing events */ + if (X.running) + caml_callback(cbfn, mkrecord(TUpdate, 2, X.width, X.height)); XFlush(X.display); } CAMLreturn(Val_unit); @@ -438,3 +458,12 @@ static int32_t special_keys(int32_t key) { return key; } } + +static void xftcolor(XftColor* xc, int c) { + #define COLOR(c) ((c) | ((c) >> 8)) + xc->color.alpha = COLOR((c & 0xFF000000) >> 16); + xc->color.red = COLOR((c & 0x00FF0000) >> 8); + xc->color.green = COLOR((c & 0x0000FF00)); + xc->color.blue = COLOR((c & 0x000000FF) << 8); + XftColorAllocValue(X.display, X.visual, X.colormap, &(xc->color), xc); +}