]> git.mdlowis.com Git - projs/tide.git/commitdiff
reworked event loop to allow x11.h to be used for both daemons and graphical apps
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 10 Dec 2018 21:18:47 +0000 (16:18 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 10 Dec 2018 21:18:47 +0000 (16:18 -0500)
inc/x11.h
src/pick.c
src/registrar.c

index 9ea4835ea6a7a31d04dc219d4def32299c48efb4..b9c6571e62219e05346b00a7bc75c560d1a06fc5 100644 (file)
--- a/inc/x11.h
+++ b/inc/x11.h
@@ -82,12 +82,14 @@ static void x11_show(XConf* x) {
     XMapWindow(x->display, x->self);
 }
 
-static void x11_event_loop(XConf* x) {
+static void x11_event_loop(XConf* x, void (*redraw)(XConf* x)) {
+    if (redraw) redraw(x);
     for (XEvent e;;) {
         XNextEvent(x->display, &e);
         if (x->eventfns[e.type])
             x->eventfns[e.type](x, &e);
         for (int status; waitpid(-1, &status, WNOHANG) > 0;);
+        if (redraw) redraw(x);
     }
 }
 
index 70517a19771d909ea13391f7ada0a13fef3dc0ba..b0edf245859be3df5533a346584d8f0bc99d45dd 100644 (file)
@@ -36,7 +36,7 @@ static char* rdline(FILE* fin) {
     return str;
 }
 
-int by_score(const void* a, const void* b) {
+static int by_score(const void* a, const void* b) {
     Choice* ca = ((Choice*)a);
     Choice* cb = ((Choice*)b);
     if (ca->score < cb->score)
@@ -47,7 +47,7 @@ int by_score(const void* a, const void* b) {
         return strcmp(ca->string, cb->string);
 }
 
-void load_choices(void) {
+static void load_choices(void) {
     char* choice_text;
     Choice choice = {0};
     vec_init(&Choices, sizeof(Choice));
@@ -63,14 +63,14 @@ void load_choices(void) {
     vec_sort(&Choices, by_score);
 }
 
-char* find_match_start(char *str, int ch) {
+static char* find_match_start(char *str, int ch) {
     for (; *str; str++)
         if (tolower(*str) == tolower(ch))
             return str;
     return NULL;
 }
 
-bool match(char *string, size_t offset, size_t *start, size_t *end) {
+static bool match(char *string, size_t offset, size_t *start, size_t *end) {
     char* q = Query;
     char* s = find_match_start(&string[offset], *q);
     char* e = s;
@@ -91,7 +91,7 @@ bool match(char *string, size_t offset, size_t *start, size_t *end) {
     return true;
 }
 
-void score(void) {
+static void score(void) {
     for (unsigned int i = 0; i < vec_size(&Choices); i++) {
         Choice* choice = (Choice*)vec_at(&Choices, i);
         float qlen = (float)QueryIdx;
@@ -109,6 +109,7 @@ void score(void) {
 
 static void xkeypress(XConf* x, XEvent* e) {
     (void)x, (void)e;
+    score();
 }
 
 static void xbtnpress(XConf* x, XEvent* e) {
@@ -141,26 +142,26 @@ static void draw_rect(XConf* x, int color, int px, int py, int width, int height
 }
 
 static void redraw(XConf* x) {
-    draw_rect(x, 0x0, 0, 0, x->width, x->height);
+    draw_rect(x, 0x00, 0, 0, x->width, x->height);
     XCopyArea(x->display, x->pixmap, x->self, x->gc, 0, 0, x->width, x->height, 0, 0);
     XFlush(x->display);
-
 }
 
-void filter(void) {
+static void filter(void) {
     XConf x = {0};
     x11_init(&x);
-    x11_mkwin(&x, 1, 1, 0);
+    x11_mkwin(&x, 1, 1, 0
+        | StructureNotifyMask
+        | KeyPressMask
+        | ButtonPressMask
+        | ExposureMask
+    );
     x11_init_gc(&x);
     x11_show(&x);
     x.eventfns[KeyPress] = xkeypress;
     x.eventfns[ButtonPress] = xbtnpress;
     x.eventfns[ConfigureNotify] = xresize;
-    redraw(&x);
-    while (true) {
-        x11_event_loop(&x);
-        redraw(&x);
-    }
+    x11_event_loop(&x, redraw);
 }
 
 int main(void) {
index c74f30ee87748749c16a955d8b1eb327e63caa1f..8b744b9f1a4ccc7135673ada00ba453b6e87b279 100644 (file)
@@ -133,7 +133,7 @@ int main(void) {
     if (None == XGetSelectionOwner(x.display, XA_REGISTRAR)) {
         XSetSelectionOwner(x.display, XA_REGISTRAR, x.self, CurrentTime);
         if (x.self == XGetSelectionOwner(x.display, XA_REGISTRAR)) {
-            x11_event_loop(&x);
+            x11_event_loop(&x, 0);
         }
     }
     return 1;