]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
added grouping logic to glyphs drawn with the same color
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 18 Oct 2017 18:24:47 +0000 (14:24 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 18 Oct 2017 18:24:47 +0000 (14:24 -0400)
edit.ml
lib/internals.h
lib/x11_prims.c

diff --git a/edit.ml b/edit.ml
index 455be09fe8df7f8ba2d0215a0765e9a88cbea8bd..a8e7f56259dffc8e829bbbdf93385b69b993ab1e 100644 (file)
--- a/edit.ml
+++ b/edit.ml
@@ -91,8 +91,7 @@ let onupdate width height =
   let pos = draw_status pos width "UNSI> *scratch*" in
   let pos = draw_tags pos width (height / font.height / 4) "Sample tags data" in
   let pos = draw_scroll pos height in
-  let _   = draw_edit pos width height in
-  flip ()
+  let _   = draw_edit pos width height in ()
 
 let onshutdown () =
   print_endline "onshutdown"
index 6b803aee8dc6b3ac4a0df6b64c97846df79b13e4..48b1237194e1fffa93df892f9a795db0bf9e7103 100644 (file)
@@ -1,4 +1,6 @@
 #include <stdbool.h>
+#include <stddef.h>
+#include <stdlib.h>
 #include <caml/mlvalues.h>
 #include <caml/fail.h>
 #include <caml/memory.h>
@@ -89,3 +91,11 @@ typedef struct XFont {
     int height;
     struct XFont* next;
 } XFont;
+
+typedef struct XText {
+    struct XText* next;
+    uint64_t argb;
+    XftColor color;
+    XftGlyphFontSpec* specs;
+    size_t nspecs;
+} XText;
index 3ce7701c3fbfa0113192e73c11f1bf72b23c4db7..c43193eeffef813206be19ccce31d7af7fd4f72a 100644 (file)
@@ -25,7 +25,7 @@ static value ev_clientmsg(XEvent*);
 static value ev_configure(XEvent*);
 
 static struct X X = {0};
-
+static XText* TextChunks = NULL;
 static value (*EventHandlers[LASTEvent]) (XEvent*) = {
     [FocusIn]          = ev_focus,
     [FocusOut]         = ev_focus,
@@ -97,12 +97,6 @@ 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);
     xftdrawrect(intfield(rect, 0), intfield(rect, 1), /* x,y */
@@ -130,14 +124,6 @@ CAMLprim value x11_event_loop(value ms, value cbfn) {
     while (X.running) {
         XPeekEvent(X.display, &e);
         uint64_t t = getmillis();
-
-//int nevents;
-//XTimeCoord* coords = XGetMotionEvents(X.display, X.self, CurrentTime, CurrentTime, &nevents);
-//if (coords) XFree(coords);
-
-Window xw; int x, ptrx, ptry; unsigned int ux;
-XQueryPointer(X.display, X.self, &xw, &xw, &x, &x, &ptrx, &ptry, &ux);
-
         while (XPending(X.display)) {
             XNextEvent(X.display, &e);
             printf("%d ", e.type);
@@ -148,12 +134,21 @@ XQueryPointer(X.display, X.self, &xw, &xw, &x, &x, &ptrx, &ptry, &ux);
             }
         }
         puts("");
-        printf("time %lu ", getmillis()-t);
+        printf("time %lu ", getmillis()-t);
         t = getmillis();
-        if (X.running)
+        if (X.running) {
             caml_callback(cbfn, mkvariant(TUpdate, 2, Val_int(X.width), Val_int(X.height)));
-        printf("%lu\n", getmillis()-t);
-
+            while (TextChunks) {
+                XText* chunk = TextChunks;
+                TextChunks = chunk->next;
+                XftDrawGlyphFontSpec(X.xft, &(chunk->color), chunk->specs, chunk->nspecs);
+                XftColorFree(X.display, X.visual, X.colormap, &(chunk->color));
+                free(chunk->specs);
+                free(chunk);
+            }
+            XCopyArea(X.display, X.pixmap, X.self, X.gc, 0, 0, X.width, X.height, 0, 0);
+        }
+        printf("\ntime 2 %lu\n", getmillis()-t);
         XFlush(X.display);
     }
     CAMLreturn(Val_unit);
@@ -262,19 +257,35 @@ CAMLprim value x11_font_glyph(value font, value rune) {
     CAMLreturn(glyph);
 }
 
+/* X11 Text Glyph Drawing
+ ******************************************************************************/
+
+static XText* glyphs_by_color(uint64_t color) {
+    XText* curr = TextChunks;
+    for (; curr && curr->argb != color; curr = curr->next);
+    if (curr == NULL) {
+        curr = calloc(1, sizeof(XText));
+        curr->next = TextChunks;
+        curr->argb = color;
+        xftcolor(&(curr->color), color);
+        TextChunks = curr;
+    }
+    return curr;
+}
+
 CAMLprim value x11_draw_glyph(value color, value glyph, value coord) {
     CAMLparam3(color, glyph, coord);
+    XText* textchunk = glyphs_by_color(Int_val(color));
+    textchunk->nspecs++;
+    textchunk->specs = realloc(textchunk->specs, textchunk->nspecs * sizeof(XftGlyphFontSpec));
     XftFont* font = (XftFont*)Field(glyph,0);
     XftGlyphFontSpec spec = {
-        .font  = font,
+        .font  = (XftFont*)Field(glyph,0),
         .glyph = intfield(glyph,1),
         .x     = intfield(coord,0),
         .y     = intfield(coord,1) + font->ascent
     };
-    XftColor fgc;
-    xftcolor(&fgc, Int_val(color));
-    XftDrawGlyphFontSpec(X.xft, &fgc, &spec, 1);
-    XftColorFree(X.display, X.visual, X.colormap, &fgc);
+    textchunk->specs[textchunk->nspecs-1] = spec;
     CAMLreturn(Field(glyph,6)); // Return xOff so we can chain operations
 }
 
@@ -322,7 +333,7 @@ static void create_window(int height, int width) {
           StructureNotifyMask
         | ButtonPressMask
         | ButtonReleaseMask
-//        | ButtonMotionMask
+        | ButtonMotionMask
         | KeyPressMask
         | FocusChangeMask
         | PropertyChangeMask