]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
Added draw rectangle fucntion to x11
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 7 Sep 2017 16:54:52 +0000 (12:54 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 7 Sep 2017 16:54:52 +0000 (12:54 -0400)
edit.ml
lib/x11.ml
lib/x11_prims.c

diff --git a/edit.ml b/edit.ml
index e128c7d78c4acbcb16a6a99bd7b95f41f4ac311c..09d84b17379736a592773e91b177ecc655811da9 100644 (file)
--- 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"
index 0d4b19dab8e92619e1c88113d921c60af5c16db9..0ca8db8dd488c146b8593564f0472ab475670425 100644 (file)
@@ -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"
 
index 188f7e6b6aea81779a7975dc473e728dc3154dcd..1b7f7c3366f3cb462233ecb4fd51add55c33d2e3 100644 (file)
@@ -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);
+}