]> git.mdlowis.com Git - projs/tide.git/commitdiff
Updated xpick.c to use the new x11 API.
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 28 Nov 2016 01:06:08 +0000 (20:06 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 28 Nov 2016 01:06:08 +0000 (20:06 -0500)
Makefile
inc/x11.h
libx/x11.c
xpick.c

index e8e6a50beda74d195482ba2f9ad71d6b3348281a..12b1afb72b8ccb4ce1a9e7c3bb1e5998e9804eeb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ TEST_OBJS =     \
 
 include config.mk
 
-all: xedit
+all: xedit xpick
 
 clean:
        $(RM) *.o lib*/*.o test/*.o *.a xpick xedit unittests
@@ -29,7 +29,8 @@ test: unittests
 xedit: xedit.o libx.a libedit.a
        $(LD) -o $@ $^ $(LDFLAGS)
 
-#xpick: xpick.o libx.a libedit.a
+xpick: xpick.o libx.a libedit.a
+       $(LD) -o $@ $^ $(LDFLAGS)
 
 libedit.a: $(LIBEDIT_OBJS)
        $(AR) $(ARFLAGS) $@ $^
index aed8feef2f5763f736e98eb4608d00c03d3c3d29..a3ee126c4814cda3017c038e4d98218a653b8fd1 100644 (file)
--- a/inc/x11.h
+++ b/inc/x11.h
@@ -124,3 +124,4 @@ void x11_draw_utf8(XFont font, int fg, int bg, int x, int y, char* str);
 void x11_font_getglyph(XFont font, XGlyphSpec* spec, uint32_t rune);
 size_t x11_font_getglyphs(XGlyphSpec* specs, const XGlyph* glyphs, int len, XFont font, int x, int y);
 void x11_draw_glyphs(int fg, int bg, XGlyphSpec* glyphs, size_t nglyphs);
+void x11_draw_utf8(XFont font, int fg, int bg, int x, int y, char* str);
index ec77fc0a11867fd6411a7ce136399e1ebf9d723a..d700c7b5c191957e40917c140abb9ffa7eaa45c2 100644 (file)
@@ -375,6 +375,21 @@ void x11_draw_glyphs(int fg, int bg, XGlyphSpec* specs, size_t nspecs) {
     XftColorFree(X.display, X.visual, X.colormap, &fgc);
 }
 
+void x11_draw_utf8(XFont fnt, int fg, int bg, int x, int y, char* str) {
+    struct XFont* font = fnt;
+    static XftGlyphFontSpec specs[256];
+    size_t nspecs = 0;
+    while (*str && nspecs < 256) {
+        x11_font_getglyph(font, (XGlyphSpec*)&(specs[nspecs]), *str);
+        specs[nspecs].x = x;
+        specs[nspecs].y = y;
+        x += font->base.width;
+        nspecs++;
+        str++;
+    }
+    x11_draw_glyphs(fg, bg, (XGlyphSpec*)specs, nspecs);
+}
+
 //void x11_warp_mouse(int x, int y) {
 //    XWarpPointer(X.display, X.window, X.window, 0, 0, X.width, X.height, x, y);
 //}
diff --git a/xpick.c b/xpick.c
index 72e586666f03fa20c26b3d5e6bdf25338e628a0a..d77ec44abdf357c54b5e1dedf5afe54a6c9b6422 100644 (file)
--- a/xpick.c
+++ b/xpick.c
@@ -21,30 +21,12 @@ static unsigned Pos = 0;
 static Buf Query;
 static vec_t Choices = {0};
 static size_t ChoiceIdx = 0;
-static XFont Fonts;
+static XFont Font;
 static XConfig Config = {
     .redraw       = redraw,
     .handle_key   = keyboard_input,
     .handle_mouse = mouse_input,
-    .palette      = {
-        /* ARGB color values */
-        0xff002b36,
-        0xff073642,
-        0xff586e75,
-        0xff657b83,
-        0xff839496,
-        0xff93a1a1,
-        0xffeee8d5,
-        0xfffdf6e3,
-        0xffb58900,
-        0xffcb4b16,
-        0xffdc322f,
-        0xffd33682,
-        0xff6c71c4,
-        0xff268bd2,
-        0xff2aa198,
-        0xff859900
-    }
+    .palette      = COLOR_PALETTE
 };
 
 static char* rdline(FILE* fin) {
@@ -138,10 +120,10 @@ static void score(void) {
     vec_sort(&Choices, by_score);
 }
 
-static void draw_runes(unsigned x, unsigned y, int fg, int bg, XGlyph* glyphs, size_t rlen) {
-    XftGlyphFontSpec specs[rlen];
+static void draw_runes(size_t x, size_t y, int fg, int bg, UGlyph* glyphs, size_t rlen) {
+    XGlyphSpec specs[rlen];
     while (rlen) {
-        size_t nspecs = x11_font_getglyphs(specs, glyphs, rlen, &Fonts, x, y);
+        size_t nspecs = x11_font_getglyphs(specs, (XGlyph*)glyphs, rlen, Font, x, y);
         x11_draw_glyphs(fg, bg, specs, nspecs);
         rlen -= nspecs;
     }
@@ -150,26 +132,26 @@ static void draw_runes(unsigned x, unsigned y, int fg, int bg, XGlyph* glyphs, s
 static void redraw(int width, int height) {
     /* draw the background colors */
     x11_draw_rect(CLR_BASE03, 0, 0, width, height);
-    x11_draw_rect(CLR_BASE02, 0, 0, width, Fonts.base.height);
-    x11_draw_rect(CLR_BASE01, 0, Fonts.base.height, width, 1);
+    x11_draw_rect(CLR_BASE02, 0, 0, width, x11_font_height(Font));
+    x11_draw_rect(CLR_BASE01, 0, x11_font_height(Font), width, 1);
     /* create the array for the query glyphs */
-    int rows = height / Fonts.base.height - 1;
-    int cols = width  / Fonts.base.width;
+    int rows = height / x11_font_height(Font) - 1;
+    int cols = width  / x11_font_width(Font);
     XGlyph glyphs[cols], *text = glyphs;
     /* draw the query */
     unsigned start = 0, end = buf_end(&Query);
     while (start < end && start < cols)
         (text++)->rune = buf_get(&Query, start++);
-    draw_runes(0, 0, CLR_BASE3, CLR_BASE03, glyphs, text - glyphs);
+    draw_runes(0, 0, CLR_BASE3, CLR_BASE03, (UGlyph*)glyphs, text - glyphs);
     /* Draw the choices */
     size_t off = (ChoiceIdx >= rows ? (ChoiceIdx-rows+1) : 0);
     for (size_t i = 0; i < vec_size(&Choices) && i < rows; i++) {
         Choice* choice = vec_at(&Choices, i+off);
         if (i+off == ChoiceIdx) {
-            x11_draw_rect(CLR_BASE1, 0, ((i+1) * Fonts.base.height)+Fonts.base.descent, width, Fonts.base.height);
-            x11_draw_utf8(&Fonts, CLR_BASE03, CLR_BASE1, 0, (i+2) * Fonts.base.height, choice->string);
+            x11_draw_rect(CLR_BASE1, 0, ((i+1) * x11_font_height(Font))+x11_font_descent(Font), width, x11_font_height(Font));
+            x11_draw_utf8(Font, CLR_BASE03, CLR_BASE1, 0, (i+2) * x11_font_height(Font), choice->string);
         } else {
-            x11_draw_utf8(&Fonts, CLR_BASE1, CLR_BASE03, 0, (i+2) * Fonts.base.height, choice->string);
+            x11_draw_utf8(Font, CLR_BASE1, CLR_BASE03, 0, (i+2) * x11_font_height(Font), choice->string);
         }
     }
 }
@@ -219,7 +201,7 @@ int main(int argc, char** argv) {
     x11_init(&Config);
     x11_dialog("pick", Width, Height);
     x11_show();
-    x11_font_load(&Fonts, FONTNAME);
+    Font = x11_font_load(FONTNAME);
     x11_loop();
     /* print out the choice */
     if (vec_size(&Choices) && ChoiceIdx != SIZE_MAX) {