]> git.mdlowis.com Git - proto/aos.git/commitdiff
extracted x11 stuff into helper library
authorMike Lowis <mike.lowis@gentex.com>
Wed, 3 May 2023 18:11:36 +0000 (14:11 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Wed, 3 May 2023 18:11:36 +0000 (14:11 -0400)
13 files changed:
bin/editor/tide.h
bin/editor/x11_gc.c [deleted file]
bin/editor/x11_sel.c [deleted file]
bin/pick.c [moved from bin/pick/pick.c with 82% similarity]
bin/pick/config.h [deleted file]
bin/pick/x11.c [deleted file]
bin/pick/x11.h [deleted file]
bin/rules.mk
config.mk
inc/libx11.h [moved from bin/editor/x11.h with 100% similarity]
lib/x11/x11.c [moved from bin/editor/x11.c with 81% similarity]
lib/x11/x11_gc.c [moved from bin/pick/x11_gc.c with 99% similarity]
lib/x11/x11_sel.c [moved from bin/pick/x11_sel.c with 99% similarity]

index d7455e4e2f3f3fd47510d32ed818e6e16aeabea4..f6fa72bd37dbc750c1ceaf55b5c47ea463331c4b 100644 (file)
@@ -1,4 +1,4 @@
-#include "x11.h"
+#include <libx11.h>
 
 /* undo/redo list item */
 typedef struct Log {
diff --git a/bin/editor/x11_gc.c b/bin/editor/x11_gc.c
deleted file mode 100644 (file)
index c0cd469..0000000
+++ /dev/null
@@ -1,165 +0,0 @@
-#include <liba.h>
-#include <stdc.h>
-#include "x11.h"
-#include <X11/extensions/Xinerama.h>
-
-void x11_resize(XConf* x, XEvent* e)
-{
-    Telemetry_Send("XRESIZE(w: %d, h: %d)\n", e->xconfigure.width, e->xconfigure.height);
-    if (e->xconfigure.width != x->width || e->xconfigure.height != x->height)
-    {
-        x->width  = e->xconfigure.width;
-        x->height = e->xconfigure.height;
-        XFreePixmap(x->display, x->pixmap);
-        x->pixmap = XCreatePixmap(x->display, x->self, x->width, x->height, x->depth);
-        XftDrawChange(x->xft, x->pixmap);
-    }
-}
-
-void x11_mapnotify(XConf* x, XEvent* e)
-{
-    (void)x;
-    Telemetry_Send("XMAPNOTIFY(0x%x)\n", e->xmap.window);
-}
-
-void x11_enternotify(XConf* x, XEvent* e)
-{
-    Telemetry_Send("XENTERNOTIFY(0x%x)\n", e->xmap.window);
-    x11_seturgent(x, 0);
-}
-
-void x11_init_gc(XConf* x)
-{
-    /* set input methods */
-    if ((x->xim = XOpenIM(x->display, 0, 0, 0)))
-        x->xic = XCreateIC(x->xim, XNInputStyle, XIMPreeditNothing|XIMStatusNothing, XNClientWindow, x->self, XNFocusWindow, x->self, NULL);
-    /* initialize pixmap and drawing context */
-    x->pixmap = XCreatePixmap(x->display, x->self, x->width, x->height, x->depth);
-    x->xft    = XftDrawCreate(x->display, x->pixmap, x->visual, x->colormap);
-    /* initialize the graphics context */
-    XGCValues gcv;
-    gcv.foreground = WhitePixel(x->display, x->screen);
-    gcv.graphics_exposures = False;
-    x->gc = XCreateGC(x->display, x->self, GCGraphicsExposures, &gcv);
-    x->eventfns[ConfigureNotify] = x11_resize;
-    x->eventfns[MapNotify] = x11_mapnotify;
-    x->eventfns[EnterNotify] = x11_enternotify;
-}
-
-void x11_centerwin(XConf* x)
-{
-    int ptrx = 0, ptry = 0;
-    (void)x11_getptr(x, &ptrx, &ptry);
-    int nscreens = 0;
-    XineramaScreenInfo* p_screens = XineramaQueryScreens(x->display, &nscreens);
-    for (int i = 0; i < nscreens; i++)
-    {
-        int minx = p_screens[i].x_org,
-            maxx = p_screens[i].x_org + p_screens[i].width,
-            miny = p_screens[i].y_org,
-            maxy = p_screens[i].y_org + p_screens[i].height;
-        if (minx <= ptrx && ptrx <= maxx && miny <= ptry && ptry <= maxy)
-        {
-            XMoveWindow(x->display, x->self,
-                minx + p_screens[i].width/2 - x->width/2,
-                miny + p_screens[i].height/2 - x->height/2);
-            break;
-        }
-    }
-    if (p_screens)
-    {
-        XFree(p_screens);
-    }
-}
-
-void x11_show(XConf* x)
-{
-    /* simulate an initial resize and map the window */
-    XConfigureEvent ce;
-    ce.type   = ConfigureNotify;
-    ce.width  = x->width;
-    ce.height = x->height;
-    XSendEvent(x->display, x->self, False, StructureNotifyMask, (XEvent *)&ce);
-    XMapWindow(x->display, x->self);
-    XSync(x->display, False);
-
-    /* Waiting for window mapping */
-    XEvent ev;
-    do
-    {
-        XNextEvent(x->display, &ev);
-        if (XFilterEvent(&ev, None))
-        {
-            continue;
-        }
-        if (ev.type == ConfigureNotify)
-        {
-            x11_resize(x, &ev);
-        }
-    }
-    while (ev.type != MapNotify);
-//    XWarpPointer(x->display, None, x->self, 0, 0, x->width, x->height, x->width/2, x->height/2);
-}
-
-XftFont* x11_font_load(XConf* x, char* name)
-{
-    XftFont* font = NULL;
-    if (FcInit())
-    {
-        FcPattern* pattern = FcNameParse((FcChar8 *)name);
-        if (pattern)
-        {
-            /* load the base font */
-            FcResult result;
-            FcPattern* match = XftFontMatch(x->display, x->screen, pattern, &result);
-            if (match)
-            {
-                font = XftFontOpenPattern(x->display, match);
-            }
-            FcPatternDestroy(pattern);
-            FcPatternDestroy(match);
-        }
-    }
-//    printf("font: '%s'\n", name);
-    return font;
-}
-
-void xftcolor(XConf* x, XftColor* xc, unsigned int c)
-{
-    #define COLOR(c) ((c) | ((c) >> 8))
-    xc->color.alpha = 0xFFFF;
-    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);
-}
-
-void x11_draw_rect(XConf* x, int color, int px, int py, int width, int height)
-{
-    XftColor clr;
-    xftcolor(x, &clr, color);
-    XftDrawRect(x->xft, &clr, px, py, width, height);
-    XftColorFree(x->display, x->visual, x->colormap, &clr);
-}
-
-void x11_draw_glyphs(XConf* x, int color, XftFont* font, XftGlyphSpec* specs, long nspecs)
-{
-    XftColor clr;
-    xftcolor(x, &clr, color);
-    XftDrawGlyphSpec(x->xft, &clr, font, specs, nspecs);
-    XftColorFree(x->display, x->visual, x->colormap, &clr);
-}
-
-void x11_flip(XConf* x)
-{
-    XCopyArea(x->display, x->pixmap, x->self, x->gc, 0, 0, x->width, x->height, 0, 0);
-    XFlush(x->display);
-}
-
-void x11_draw_string(XConf* x, XftFont* font, int posx, int posy, int color, char* str)
-{
-    XftColor clr;
-    xftcolor(x, &clr, color);
-    XftDrawStringUtf8(x->xft, &clr, font, posx, posy, (const FcChar8*)str, strlen(str));
-    XftColorFree(x->display, x->visual, x->colormap, &clr);
-}
diff --git a/bin/editor/x11_sel.c b/bin/editor/x11_sel.c
deleted file mode 100644 (file)
index d026750..0000000
+++ /dev/null
@@ -1,183 +0,0 @@
-#include <stdc.h>
-#include "x11.h"
-
-struct XSel {
-    char* name;
-    Atom atom;
-    char* text;
-    void (*callback)(char*);
-};
-
-static Atom SelTarget;
-static struct XSel Selections[] = {
-    { .name = "PRIMARY" },
-    { .name = "CLIPBOARD" },
-};
-
-static struct XSel* selfetch(Atom atom)
-{
-    struct XSel* ret = NULL;
-    for (unsigned int i = 0; i < (sizeof(Selections) / sizeof(Selections[0])); i++)
-    {
-        if (atom == Selections[i].atom)
-        {
-            ret = &Selections[i];
-            break;
-        }
-    }
-    return ret;
-}
-
-static void xselclear(XConf* x, XEvent* e)
-{
-    (void)x;
-    struct XSel* sel = selfetch(e->xselectionclear.selection);
-    if (!sel) return;
-    free(sel->text);
-    sel->text = NULL;
-}
-
-static void xselnotify(XConf* x, XEvent* e)
-{
-    /* bail if the selection cannot be converted */
-    if (e->xselection.property == None)
-        return;
-    struct XSel* sel = selfetch( e->xselection.selection );
-    Atom rtype;
-    unsigned long format = 0, nitems = 0, nleft = 0;
-    unsigned char* propdata = NULL;
-    XGetWindowProperty(x->display, x->self, sel->atom, 0, -1, False, AnyPropertyType, &rtype,
-                       (int*)&format, &nitems, &nleft, &propdata);
-    if (e->xselection.target == SelTarget)
-    {
-        void(*cbfn)(char*) = sel->callback;
-        sel->callback = NULL;
-        cbfn((char*)propdata);
-    }
-    /* cleanup */
-    if (propdata) XFree(propdata);
-}
-
-static void xselrequest(XConf* x, XEvent* e)
-{
-    XEvent s;
-    struct XSel* sel = selfetch( e->xselectionrequest.selection );
-    s.xselection.type      = SelectionNotify;
-    s.xselection.property  = e->xselectionrequest.property;
-    s.xselection.requestor = e->xselectionrequest.requestor;
-    s.xselection.selection = e->xselectionrequest.selection;
-    s.xselection.target    = e->xselectionrequest.target;
-    s.xselection.time      = e->xselectionrequest.time;
-    Atom target    = e->xselectionrequest.target;
-    Atom xatargets = XInternAtom(x->display, "TARGETS", 0);
-    Atom xastring  = XInternAtom(x->display, "STRING", 0);
-    if (target == xatargets)
-    {
-        /* respond with the supported type */
-        XChangeProperty(
-            x->display,
-            s.xselection.requestor,
-            s.xselection.property,
-            XA_ATOM, 32, PropModeReplace,
-            (unsigned char*)&SelTarget, 1);
-    }
-    else if (target == SelTarget || target == xastring)
-    {
-        XChangeProperty(
-            x->display,
-            s.xselection.requestor,
-            s.xselection.property,
-            SelTarget, 8, PropModeReplace,
-            (unsigned char*)sel->text, strlen(sel->text));
-    }
-    XSendEvent(x->display, s.xselection.requestor, True, 0, &s);
-}
-
-void x11_sel_init(XConf* x)
-{
-    /* initialize selection atoms */
-    for (unsigned int i = 0; i < (sizeof(Selections) / sizeof(Selections[0])); i++)
-    {
-        Selections[i].atom = XInternAtom(x->display, Selections[i].name, 0);
-    }
-    SelTarget = XInternAtom(x->display, "UTF8_STRING", 0);
-    if (SelTarget == None)
-    {
-        SelTarget = XInternAtom(x->display, "STRING", 0);
-    }
-    /* setup event handlers */
-    x->eventfns[SelectionClear] = xselclear;
-    x->eventfns[SelectionNotify] = xselnotify;
-    x->eventfns[SelectionRequest] = xselrequest;
-}
-
-int x11_sel_get(XConf* x, int selid, void(*cbfn)(char*))
-{
-    int ret = 0;
-    struct XSel* sel = &(Selections[selid]);
-    if (!sel->callback)
-    {
-        Window owner = XGetSelectionOwner(x->display, sel->atom);
-        if (owner == x->self)
-        {
-            cbfn(sel->text);
-        }
-        else if (owner != None)
-        {
-            sel->callback = cbfn;
-            XConvertSelection(x->display, sel->atom, SelTarget, sel->atom, x->self, CurrentTime);
-        }
-        ret = 1;
-    }
-    return ret;
-}
-
-int x11_sel_set(XConf* x, int selid, char* str)
-{
-    int ret;
-    struct XSel* sel = &(Selections[selid]);
-    if (!sel || !str || !*str)
-    {
-        free(str);
-        ret = 0;
-    }
-    else
-    {
-        sel->text = str;
-        XSetSelectionOwner(x->display, sel->atom, x->self, CurrentTime);
-        ret = 1;
-    }
-    return ret;
-}
-
-void x11_sel_quit(XConf* x, XEvent* e)
-{
-    xselclear(x, e);
-    if (!Selections[PRIMARY].text && !Selections[CLIPBOARD].text)
-    {
-        x->state = QUITTING;
-    }
-}
-
-int x11_sel_ready(XConf* x)
-{
-    (void)x;
-    return (Selections[PRIMARY].text || Selections[CLIPBOARD].text);
-}
-
-void x11_sel_serve(XConf* x)
-{
-    X.eventfns[SelectionClear] = x11_sel_quit;
-    X.self = XCreateSimpleWindow(X.display, X.root, 0, 0, 1, 1, 0, 0, 0);
-    if (Selections[PRIMARY].text)
-    {
-        XSetSelectionOwner(x->display, Selections[PRIMARY].atom, x->self, CurrentTime);
-
-    }
-    if (Selections[CLIPBOARD].text)
-    {
-        XSetSelectionOwner(x->display, Selections[CLIPBOARD].atom, x->self, CurrentTime);
-    }
-    if (fork()) exit(0); /* fork into background */
-}
-
similarity index 82%
rename from bin/pick/pick.c
rename to bin/pick.c
index f6eb9202e6ebd7c6d5570dd8c2aeb13897c87991..eadb09e9fcd63d9c1c8ea4bb76e4f864f799d9dd 100644 (file)
@@ -1,12 +1,67 @@
+#include <liba.h>
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 #include <X11/Xft/Xft.h>
 #include <stdc.h>
 #include <vec.h>
-#include "x11.h"
+#include <libx11.h>
 
-#define INCLUDE_DEFS
-#include "config.h"
+enum { /* Color Names */
+    Red, Purple, Orange,
+    EditBg, EditFg, EditSel,
+    TagsBg, TagsFg, TagsSel,
+    ScrollBg, ScrollFg,
+    VerBdr, HorBdr, Point,
+    ClrCount
+};
+
+/* List of font patterns available to the editor */
+static char* Fonts[2] = {
+    "Verdana:size=12",
+    "Liberation Mono:size=12"
+};
+
+int ScrollWidth = 8;   /* width in pixels of the scrollbar */
+
+static int Palette[ClrCount] = {
+#if 0 /* Original Acme Colors */
+    [EditBg]   = 0xFFFFEA, /* Edit region background */
+    [EditFg]   = 0x000000, /* Edit region text */
+    [EditSel]  = 0xEEEE9E, /* Edit region selection */
+    [TagsBg]   = 0xEAFFFF, /* Tags region background */
+    [TagsFg]   = 0x000000, /* Tags region text */
+    [TagsSel]  = 0x9EEEEE, /* Tags region selection */
+    [ScrollBg] = 0x99994C, /* Scroll region background */
+    [ScrollFg] = 0xFFFFEA, /* Scroll region foreground */
+    [VerBdr]   = 0x99994C, /* Vertical border */
+    [HorBdr]   = 0x000000, /* Horizontal border */
+    [Point]    = 0xEFEFDA, /* Point background */
+    [Purple]   = 0x6666CC, /* Purple */
+    [Red]      = 0xCC0000, /* Red */
+    [Orange]   = 0xFF7700, /* Orange */
+#else
+    [EditBg]   = 0xF5F5F0, /* Edit region background */
+    [EditFg]   = 0x222222, /* Edit region text */
+    [EditSel]  = 0xAACCEE, /* Edit region selection */
+
+    [TagsBg]   = 0xF5F5F0, /* Tags region background */
+    [TagsFg]   = 0x222222, /* Tags region text */
+    [TagsSel]  = 0xAACCEE, /* Tags region selection */
+
+    [ScrollBg] = 0x959590, /* Scroll region background */
+    [ScrollFg] = 0xF5F5F0, /* Scroll region foreground */
+
+    [VerBdr]   = 0x959590, /* Vertical border */
+    [HorBdr]   = 0x222222, /* Horizontal border */
+
+    [Point]    = 0xD5D5D0, /* Point background */
+    [Purple]   = 0x6666CC, /* Purple */
+    [Red]      = 0xCC0000, /* Red */
+    [Orange]   = 0xFF7700, /* Orange */
+#endif
+};
+
+#pragma GCC diagnostic pop
 
 static void xkeypress(XConf* x, XEvent* e);
 static void xbtnpress(XConf* x, XEvent* e);
diff --git a/bin/pick/config.h b/bin/pick/config.h
deleted file mode 100644 (file)
index e1ab943..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-/** @file */
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wunused-variable"
-
-enum { Off = 0, On = 1 };
-
-enum { /* Color Names */
-    Red, Purple, Orange,
-    EditBg, EditFg, EditSel,
-    TagsBg, TagsFg, TagsSel,
-    ScrollBg, ScrollFg,
-    VerBdr, HorBdr, Point,
-    ClrCount
-};
-
-#define CMD_TIDE      "!tide"
-#define CMD_PICKFILE  "!pickfile ."
-#define CMD_COMPLETE  "picktag print tags"
-#define CMD_FCOMPLETE "fcomplete"
-#define CMD_GOTO_TAG  "!picktag fetch tags"
-
-/* Command used to open file in editor at a given line */
-static char* EditCmd[] = { "tide", "-l", 0, 0, 0 };
-
-/* The shell: Filled in with $SHELL. Used to execute commands */
-static char* ShellCmd[] = { 0, "-c", 0, 0 };
-
-/* Sed command used to execute commands marked with ':' sigil */
-static char* SedCmd[] = { "sed", "-Ee", 0, 0 };
-
-/* Command used to fetch some text based on a set of rules */
-static char* FetchCmd[] = { "fetch", 0, 0 };
-
-/* Default tag region text in editor windows */
-static char* TagString = "Del Put Get | Font Tabs Eol | x+ w+ !st !term | Find ";
-
-/* List of font patterns available to the editor */
-static char* Fonts[2] = {
-    "Verdana:size=12",
-    "Liberation Mono:size=12"
-};
-
-#ifdef INCLUDE_DEFS
-/* Integer config options */
-int WinWidth    = 640; /* default window width */
-int WinHeight   = 480; /* default window height */
-int ScrollWidth = 8;   /* width in pixels of the scrollbar */
-int Timeout     = 100; /* number of milliseconds to wait before poll() times out */
-int TabWidth    = 4;   /* number of spaces tab characters will be expanded to */
-int ScrollBy    = 1;   /* number of lines to scroll by on mouse wheel  events */
-int ClickTime   = 500; /* number of milliseconds after a click wehre multi-clicks will be recognized */
-int CopyIndent  = On;  /* whether indentation should be copied from previous line or not */
-int TrimOnSave  = On;  /* whether trailing whitespace should be removed on save or not */
-int ExpandTabs  = On;  /* whether tab characters should be expanded to spaces or not */
-int DosLineFeed = Off; /* use \r\n line endings by default */
-int Margin      = 50;
-#else
-extern int WinWidth, WinHeight, ScrollWidth, Timeout, TabWidth, ScrollBy,
-    ClickTime, CopyIndent, TrimOnSave, ExpandTabs, DosLineFeed, Margin;
-#endif
-
-static int Palette[ClrCount] = {
-#if 0 /* Original Acme Colors */
-    [EditBg]   = 0xFFFFEA, /* Edit region background */
-    [EditFg]   = 0x000000, /* Edit region text */
-    [EditSel]  = 0xEEEE9E, /* Edit region selection */
-    [TagsBg]   = 0xEAFFFF, /* Tags region background */
-    [TagsFg]   = 0x000000, /* Tags region text */
-    [TagsSel]  = 0x9EEEEE, /* Tags region selection */
-    [ScrollBg] = 0x99994C, /* Scroll region background */
-    [ScrollFg] = 0xFFFFEA, /* Scroll region foreground */
-    [VerBdr]   = 0x99994C, /* Vertical border */
-    [HorBdr]   = 0x000000, /* Horizontal border */
-    [Point]    = 0xEFEFDA, /* Point background */
-    [Purple]   = 0x6666CC, /* Purple */
-    [Red]      = 0xCC0000, /* Red */
-    [Orange]   = 0xFF7700, /* Orange */
-#else
-    [EditBg]   = 0xF5F5F0, /* Edit region background */
-    [EditFg]   = 0x222222, /* Edit region text */
-    [EditSel]  = 0xAACCEE, /* Edit region selection */
-
-    [TagsBg]   = 0xF5F5F0, /* Tags region background */
-    [TagsFg]   = 0x222222, /* Tags region text */
-    [TagsSel]  = 0xAACCEE, /* Tags region selection */
-
-    [ScrollBg] = 0x959590, /* Scroll region background */
-    [ScrollFg] = 0xF5F5F0, /* Scroll region foreground */
-
-    [VerBdr]   = 0x959590, /* Vertical border */
-    [HorBdr]   = 0x222222, /* Horizontal border */
-
-    [Point]    = 0xD5D5D0, /* Point background */
-    [Purple]   = 0x6666CC, /* Purple */
-    [Red]      = 0xCC0000, /* Red */
-    [Orange]   = 0xFF7700, /* Orange */
-#endif
-};
-
-#pragma GCC diagnostic pop
diff --git a/bin/pick/x11.c b/bin/pick/x11.c
deleted file mode 100644 (file)
index bb030cb..0000000
+++ /dev/null
@@ -1,256 +0,0 @@
-#include <liba.h>
-#include <stdc.h>
-#include "x11.h"
-#include <locale.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include "config.h"
-
-struct XConf X;
-static Bool Has_Error = False;
-static XErrorEvent Error = {0};
-
-static int onerror(Display* disp, XErrorEvent* ev)
-{
-    (void)disp;
-    Has_Error = True, Error = *ev;
-    return 0;
-}
-
-int x11_init(XConf* x)
-{
-    int ret = -1;
-    signal(SIGPIPE, SIG_IGN); // Ignore the SIGPIPE signal
-    setlocale(LC_CTYPE, "");
-    XSetLocaleModifiers("");
-    /* open the X display and get basic attributes */
-    if ( (x->display = XOpenDisplay(0)) )
-    {
-        x->root = DefaultRootWindow(x->display);
-        XWindowAttributes wa;
-        XGetWindowAttributes(x->display, x->root, &wa);
-        x->visual   = wa.visual;
-        x->colormap = wa.colormap;
-        x->screen   = DefaultScreen(x->display);
-        x->depth    = DefaultDepth(x->display, x->screen);
-        x->state    = RUNNING;
-        XSetErrorHandler(onerror);
-        ret = 0;
-    }
-    return ret;
-}
-
-void x11_error_clear(void)
-{
-    Has_Error = False;
-    memset(&Error, 0, sizeof(Error));
-}
-
-XErrorEvent* x11_error_get(void)
-{
-    return (Has_Error ? &Error : NULL);
-}
-
-void x11_mkwin(XConf* x, int width, int height, int evmask)
-{
-    /* create the main window */
-    x->width = width, x->height = height;
-    XSetWindowAttributes attr;
-    attr.background_pixel = Palette[EditBg];
-    attr.bit_gravity = NorthWestGravity;
-    attr.backing_store = WhenMapped;
-    attr.event_mask = evmask
-        | EnterWindowMask
-        | ExposureMask
-        | VisibilityChangeMask
-        | StructureNotifyMask
-    ;
-    x->self = XCreateWindow(
-        x->display, x->root, 0, 0, x->width, x->height, 0, x->depth, InputOutput, x->visual,
-        CWBackPixel | CWBitGravity | CWBackingStore | CWEventMask,
-        &attr);
-
-    /* register interest in the delete window message */
-    Atom wmDeleteMessage = XInternAtom(x->display, "WM_DELETE_WINDOW", False);
-    XSetWMProtocols(x->display, x->self, &wmDeleteMessage, 1);
-}
-
-void x11_mkdialog(XConf* x, int width, int height, int evmask)
-{
-    x11_mkwin(x, width, height, evmask);
-    Atom WindowType = XInternAtom(x->display, "_NET_WM_WINDOW_TYPE", False);
-    Atom DialogType = XInternAtom(x->display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
-    XChangeProperty(x->display, x->self, WindowType, XA_ATOM, 32, PropModeReplace, (unsigned char*)&DialogType, 1);
-}
-
-static void update_state(XConf* x, XEvent* e)
-{
-    if ((e->type == KeyPress) || (e->type == ButtonPress) ||
-        (e->type == ButtonRelease) || (e->type == MotionNotify))
-    {
-        x->now = e->xkey.time;
-        x->mods = e->xkey.state;
-    }
-}
-
-void x11_process_events(XConf* x)
-{
-    int nevents;
-    /* reap zombie background processes */
-    for (int status; waitpid(-1, &status, WNOHANG) > 0;);
-    /* process the entire event queue */
-    while (XEventsQueued(x->display, QueuedAfterReading))
-    {
-        Telemetry_Send("EV_READ_QUEUE(pending: %d)\n", XPending(x->display));
-        XGetMotionEvents(x->display, x->self, CurrentTime, CurrentTime, &nevents);
-
-        for (XEvent e; XPending(x->display);)
-        {
-            XNextEvent(x->display, &e);
-            update_state(x, &e);
-            if (!XFilterEvent(&e, None) && x->eventfns[e.type])
-            {
-                Telemetry_Send("EV_HANDLE(type: %d)\n", e.type);
-                (x->eventfns[e.type])(x, &e);
-            }
-            else
-            {
-                Telemetry_Send("EV_IGNORE(type: %d)\n", e.type);
-            }
-
-            /* log error here */
-            XErrorEvent* err = x11_error_get();
-            if (err)
-            {
-                char msg[8192];
-                XGetErrorText(x->display, err->error_code, msg, sizeof(msg));
-                Telemetry_Send("XERROR(%s)\n", msg);
-                x11_error_clear();
-            }
-        }
-    }
-}
-
-void x11_event_loop(XConf* x, void (*redraw)(XConf* x))
-{
-    if (redraw) redraw(x);
-    for (XEvent e; x->state != QUITTING;)
-    {
-        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);
-        }
-    }
-}
-
-int x11_getptr(XConf* x, int* ptrx, int* ptry)
-{
-    Window root = 0, child = 0;
-    int winx = 0, winy = 0, mask = 0;
-    return XQueryPointer(x->display, x->self, &root, &child, ptrx, ptry, &winx, &winy, (unsigned int*)&mask);
-}
-
-static uint32_t special_keys(uint32_t key)
-{
-    static uint32_t keymap[256] = {
-        /* Function keys */
-        [0xBE] = KEY_F1, [0xBF] = KEY_F2,  [0xC0] = KEY_F3,  [0xC1] = KEY_F4,
-        [0xC2] = KEY_F5, [0xC3] = KEY_F6,  [0xC4] = KEY_F7,  [0xC5] = KEY_F8,
-        [0xC6] = KEY_F9, [0xC7] = KEY_F10, [0xC8] = KEY_F11, [0xC9] = KEY_F12,
-        /* Navigation keys */
-        [0x50] = KEY_HOME,  [0x51] = KEY_LEFT, [0x52] = KEY_UP,
-        [0x53] = KEY_RIGHT, [0x54] = KEY_DOWN, [0x55] = KEY_PGUP,
-        [0x56] = KEY_PGDN,  [0x57] = KEY_END,
-        /* Control keys */
-        [0x08] = '\b', [0x09] = '\t', [0x0d] = '\n', [0x0a] = '\n',
-        /* Miscellaneous */
-        [0x63] = KEY_INSERT, [0x1B] = KEY_ESCAPE, [0xFF] = KEY_DELETE,
-    };
-    /* lookup the key by keysym */
-    key = ((key & 0xFF00) == 0xFF00 ? keymap[key & 0xFF] : key);
-    return (!key ? RUNE_ERR : key);
-}
-
-uint32_t x11_getkey(XConf* x, XEvent* e)
-{
-    uint32_t ret;
-    char buf[8];
-    KeySym key;
-    Status status;
-
-    /* Read the key string */
-    if (x->xic)
-    {
-        Xutf8LookupString(x->xic, &(e->xkey), buf, sizeof(buf), &key, &status);
-    }
-    else
-    {
-        XLookupString(&(e->xkey), buf, sizeof(buf), &key, 0);
-    }
-
-    /* if it's ascii, just return it */
-    if (key >= 0x20 && key <= 0x7F)
-    {
-        ret = (uint32_t)key;
-    }
-    else
-    {
-        ret = special_keys(key);
-    }
-    return ret;
-}
-
-uint32_t x11_process_key(XConf* x, XEvent* e, KeyBinding* keys)
-{
-    uint32_t key = x11_getkey(x, e);
-    if (key != RUNE_ERR)
-    {
-        int mods = e->xkey.state & (ModCtrl|ModShift|ModAlt);
-        int32_t mkey = tolower(key);
-        for (KeyBinding* bind = keys; bind && bind->key; bind++)
-        {
-            bool match   = (mkey == (int32_t)bind->key);
-            bool exact   = (bind->mods == mods);
-            bool any     = (bind->mods == ModAny);
-            bool oneplus = ((bind->mods == ModOneOrMore) && (mods & ModOneOrMore));
-            if (match && (exact || oneplus || any))
-            {
-                bind->fn(bind->arg);
-                key = RUNE_ERR;
-                break;
-            }
-        }
-    }
-    /* fallback to just inserting the rune if it doesn't fall in the private use area.
-     * the private use area is used to encode special keys */
-    return (key < 0xE000 || key > 0xF8FF ? key : RUNE_ERR);
-}
-
-int x11_seturgent(XConf* x, int urgent)
-{
-    int status = 0;
-    XWMHints hints = {0}, *prevhints = XGetWMHints(x->display, x->self);
-    if (prevhints)
-    {
-        hints = *prevhints;
-        XFree(prevhints);
-    }
-    if (urgent)
-    {
-        hints.flags |= XUrgencyHint;
-    }
-    else
-    {
-        hints.flags &= ~XUrgencyHint;
-    }
-    status = XSetWMHints(x->display, x->self, &hints);
-    Telemetry_Send("URGENT(%d %d)\n", urgent, status);
-    return status;
-}
diff --git a/bin/pick/x11.h b/bin/pick/x11.h
deleted file mode 100644 (file)
index 4878f97..0000000
+++ /dev/null
@@ -1,166 +0,0 @@
-/** @file */
-AUTOLIB(X11)
-AUTOLIB(Xinerama)
-AUTOLIB(Xft)
-AUTOLIB(fontconfig)
-
-#include <X11/Xlib.h>
-#include <X11/Xatom.h>
-#include <X11/Xft/Xft.h>
-
-enum {
-    RUNNING,
-    SERVE_SEL,
-    QUITTING
-};
-
-typedef struct XConf {
-    Bool error;
-    int state, fd, screen, width, height, mods;
-    Window root;
-    Display* display;
-    Visual* visual;
-    Colormap colormap;
-    unsigned depth;
-    Window self;
-    XftDraw* xft;
-    XftFont *tagfont, *font;
-    Pixmap pixmap;
-    XIC xic;
-    XIM xim;
-    GC gc;
-    void (*eventfns[LASTEvent])(struct XConf*, XEvent*);
-    Time now;
-} XConf;
-
-/* Selection identifiers */
-enum {
-    PRIMARY   = 0,
-    CLIPBOARD = 1
-};
-
-/* key definitions */
-enum Keys {
-    /* Define some runes in the private use area of unicode to represent
-     * special keys */
-    KEY_F1     = (0xE000+0),
-    KEY_F2     = (0xE000+1),
-    KEY_F3     = (0xE000+2),
-    KEY_F4     = (0xE000+3),
-    KEY_F5     = (0xE000+4),
-    KEY_F6     = (0xE000+5),
-    KEY_F7     = (0xE000+6),
-    KEY_F8     = (0xE000+7),
-    KEY_F9     = (0xE000+8),
-    KEY_F10    = (0xE000+9),
-    KEY_F11    = (0xE000+10),
-    KEY_F12    = (0xE000+11),
-    KEY_INSERT = (0xE000+12),
-    KEY_DELETE = (0xE000+13),
-    KEY_HOME   = (0xE000+14),
-    KEY_END    = (0xE000+15),
-    KEY_PGUP   = (0xE000+16),
-    KEY_PGDN   = (0xE000+17),
-    KEY_UP     = (0xE000+18),
-    KEY_DOWN   = (0xE000+19),
-    KEY_RIGHT  = (0xE000+20),
-    KEY_LEFT   = (0xE000+21),
-
-    /* ASCII Control Characters */
-    KEY_CTRL_TILDE       = 0x00,
-    KEY_CTRL_2           = 0x00,
-    KEY_CTRL_A           = 0x01,
-    KEY_CTRL_B           = 0x02,
-    KEY_CTRL_C           = 0x03,
-    KEY_CTRL_D           = 0x04,
-    KEY_CTRL_E           = 0x05,
-    KEY_CTRL_F           = 0x06,
-    KEY_CTRL_G           = 0x07,
-    KEY_BACKSPACE        = 0x08,
-    KEY_CTRL_H           = 0x08,
-    KEY_TAB              = 0x09,
-    KEY_CTRL_I           = 0x09,
-    KEY_CTRL_J           = 0x0A,
-    KEY_CTRL_K           = 0x0B,
-    KEY_CTRL_L           = 0x0C,
-    KEY_ENTER            = 0x0D,
-    KEY_CTRL_M           = 0x0D,
-    KEY_CTRL_N           = 0x0E,
-    KEY_CTRL_O           = 0x0F,
-    KEY_CTRL_P           = 0x10,
-    KEY_CTRL_Q           = 0x11,
-    KEY_CTRL_R           = 0x12,
-    KEY_CTRL_S           = 0x13,
-    KEY_CTRL_T           = 0x14,
-    KEY_CTRL_U           = 0x15,
-    KEY_CTRL_V           = 0x16,
-    KEY_CTRL_W           = 0x17,
-    KEY_CTRL_X           = 0x18,
-    KEY_CTRL_Y           = 0x19,
-    KEY_CTRL_Z           = 0x1A,
-    KEY_ESCAPE           = 0x1B,
-    KEY_CTRL_LSQ_BRACKET = 0x1B,
-    KEY_CTRL_3           = 0x1B,
-    KEY_CTRL_4           = 0x1C,
-    KEY_CTRL_BACKSLASH   = 0x1C,
-    KEY_CTRL_5           = 0x1D,
-    KEY_CTRL_RSQ_BRACKET = 0x1D,
-    KEY_CTRL_6           = 0x1E,
-    KEY_CTRL_7           = 0x1F,
-    KEY_CTRL_SLASH       = 0x1F,
-    KEY_CTRL_UNDERSCORE  = 0x1F,
-};
-
-/* Key modifier masks */
-enum {
-    ModNone       = 0,
-    ModShift      = (1 << 0),
-    ModCapsLock   = (1 << 1),
-    ModCtrl       = (1 << 2),
-    ModAlt        = (1 << 3),
-    ModNumLock    = (1 << 4),
-    ModScrollLock = (1 << 5),
-    ModWindows    = (1 << 6),
-    ModOneOrMore  = (ModCtrl|ModAlt),
-    ModAny        = -1
-};
-
-typedef struct {
-    int mods;
-    uint32_t key;
-    void (*fn)(char*);
-    char* arg;
-} KeyBinding;
-
-extern struct XConf X;
-
-int x11_init(XConf* x);
-void x11_error_clear(void);
-XErrorEvent* x11_error_get(void);
-void x11_resize(XConf* x, XEvent* e);
-
-void x11_mkwin(XConf* x, int width, int height, int evmask);
-void x11_mkdialog(XConf* x, int width, int height, int evmask);
-void x11_process_events(XConf* x);
-void x11_event_loop(XConf* x, void (*redraw)(XConf* x));
-int x11_getptr(XConf* x, int* ptrx, int* ptry);
-uint32_t x11_getkey(XConf* x, XEvent* e);
-uint32_t x11_process_key(XConf* x, XEvent* e, KeyBinding* keys);
-int x11_seturgent(XConf* x, int urgent);
-
-void x11_centerwin(XConf* x);
-void x11_init_gc(XConf* x);
-void x11_show(XConf* x);
-XftFont* x11_font_load(XConf* x, char* name);
-void xftcolor(XConf* x, XftColor* xc, unsigned int c);
-void x11_draw_rect(XConf* x, int color, int px, int py, int width, int height);
-void x11_draw_glyphs(XConf* x, int color, XftFont* font, XftGlyphSpec* specs, long nspecs);
-void x11_flip(XConf* x);
-void x11_draw_string(XConf* x, XftFont* font, int posx, int posy, int color, char* str);
-
-void x11_sel_init(XConf* x);
-int x11_sel_get(XConf* x, int selid, void(*cbfn)(char*));
-int x11_sel_set(XConf* x, int selid, char* str);
-void x11_sel_quit(XConf* x, XEvent* e);
-int x11_sel_ready(XConf* x);
-void x11_sel_serve(XConf* x);
index 7663b190f1fcd470925127785e8e84a25f20bdd1..e9dbda0986656ceee5bbf40297e949fc364d7317 100644 (file)
@@ -1,6 +1,6 @@
-$(BINDIR)/edit: LIBS += -lX11
-$(BINDIR)/editor: LIBS += -lX11 -lXft -lfontconfig -lXinerama
-$(BINDIR)/pick: LIBS += -lX11 -lXft -lfontconfig -lXinerama
+$(BINDIR)/edit:       LIBS += -lX11
+$(BINDIR)/editor:     LIBS += -lx11 -lX11 -lXft -lfontconfig -lXinerama
+$(BINDIR)/pick:       LIBS += -lx11 -lX11 -lXft -lfontconfig -lXinerama
 $(BINDIR)/screenlock: LIBS += -lui -lX11 -lXft -lfontconfig -lcrypt -lXrandr
-$(BINDIR)/terminal: LIBS += -lui -lX11 -lXft -lfontconfig
+$(BINDIR)/terminal:   LIBS += -lui -lX11 -lXft -lfontconfig
 $(BINDIR)/winmgr:     LIBS += -lX11 -lXft -lfontconfig -lXinerama
index 2eadbd5334c13f79e7579f684ac2921f53361c70..6ecb04eab71e39c2b6e7bc12db75258f85a5123d 100644 (file)
--- a/config.mk
+++ b/config.mk
@@ -9,8 +9,8 @@ CPPFLAGS = -Iinc/ -I/usr/include/freetype2
 AR = ar
 ARFLAGS = rs
 
-LDFLAGS = -flto -Lbuild/lib/ $(LIBS)
-LIBS = -la
+LDFLAGS = -flto -Lbuild/lib/ $(LIBS) -la
+LIBS =
 
 ARCHIVE = $(AR) $(ARFLAGS) $@ $^
 BINARY  = $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $^ $(LDFLAGS) && strip $@
similarity index 100%
rename from bin/editor/x11.h
rename to inc/libx11.h
similarity index 81%
rename from bin/editor/x11.c
rename to lib/x11/x11.c
index 4af3baf269dd816a5d16fbd75848e234809253a4..1acab81aa7a5451482c3a05ca94885bdb99e0546 100644 (file)
@@ -1,11 +1,57 @@
 #include <liba.h>
 #include <stdc.h>
-#include "x11.h"
+#include <libx11.h>
 #include <locale.h>
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include "config.h"
+
+enum { /* Color Names */
+    Red, Purple, Orange,
+    EditBg, EditFg, EditSel,
+    TagsBg, TagsFg, TagsSel,
+    ScrollBg, ScrollFg,
+    VerBdr, HorBdr, Point,
+    ClrCount
+};
+
+static int Palette[ClrCount] = {
+#if 0 /* Original Acme Colors */
+    [EditBg]   = 0xFFFFEA, /* Edit region background */
+    [EditFg]   = 0x000000, /* Edit region text */
+    [EditSel]  = 0xEEEE9E, /* Edit region selection */
+    [TagsBg]   = 0xEAFFFF, /* Tags region background */
+    [TagsFg]   = 0x000000, /* Tags region text */
+    [TagsSel]  = 0x9EEEEE, /* Tags region selection */
+    [ScrollBg] = 0x99994C, /* Scroll region background */
+    [ScrollFg] = 0xFFFFEA, /* Scroll region foreground */
+    [VerBdr]   = 0x99994C, /* Vertical border */
+    [HorBdr]   = 0x000000, /* Horizontal border */
+    [Point]    = 0xEFEFDA, /* Point background */
+    [Purple]   = 0x6666CC, /* Purple */
+    [Red]      = 0xCC0000, /* Red */
+    [Orange]   = 0xFF7700, /* Orange */
+#else
+    [EditBg]   = 0xF5F5F0, /* Edit region background */
+    [EditFg]   = 0x222222, /* Edit region text */
+    [EditSel]  = 0xAACCEE, /* Edit region selection */
+
+    [TagsBg]   = 0xF5F5F0, /* Tags region background */
+    [TagsFg]   = 0x222222, /* Tags region text */
+    [TagsSel]  = 0xAACCEE, /* Tags region selection */
+
+    [ScrollBg] = 0x959590, /* Scroll region background */
+    [ScrollFg] = 0xF5F5F0, /* Scroll region foreground */
+
+    [VerBdr]   = 0x959590, /* Vertical border */
+    [HorBdr]   = 0x222222, /* Horizontal border */
+
+    [Point]    = 0xD5D5D0, /* Point background */
+    [Purple]   = 0x6666CC, /* Purple */
+    [Red]      = 0xCC0000, /* Red */
+    [Orange]   = 0xFF7700, /* Orange */
+#endif
+};
 
 struct XConf X;
 static Bool Has_Error = False;
similarity index 99%
rename from bin/pick/x11_gc.c
rename to lib/x11/x11_gc.c
index c0cd4697c1b7d014dbbdb34c3dc266148d7365b3..9c4a6bb8e23c45002419d464e3e95918ee57c55e 100644 (file)
@@ -1,6 +1,6 @@
 #include <liba.h>
 #include <stdc.h>
-#include "x11.h"
+#include <libx11.h>
 #include <X11/extensions/Xinerama.h>
 
 void x11_resize(XConf* x, XEvent* e)
similarity index 99%
rename from bin/pick/x11_sel.c
rename to lib/x11/x11_sel.c
index d026750e7b90c140528cdc303463381f93a841ef..5253c61a5425efe185fa6d85f649b4434a70d873 100644 (file)
@@ -1,5 +1,5 @@
 #include <stdc.h>
-#include "x11.h"
+#include <libx11.h>
 
 struct XSel {
     char* name;