This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository e16-keyedit.

View the commit online.

commit 95ca821c14c7ef0d0fd862b3038d25166708eaae
Author: Kim Woelders <k...@woelders.dk>
AuthorDate: Fri Dec 8 09:53:19 2023 +0100

    Simplifications, cleanups
---
 ipc.c | 203 ++++++++++++++++++++++++++++++++----------------------------------
 1 file changed, 97 insertions(+), 106 deletions(-)

diff --git a/ipc.c b/ipc.c
index fc2c6b4..e348fb0 100644
--- a/ipc.c
+++ b/ipc.c
@@ -12,10 +12,11 @@
 #define gdk_window_foreign_new(xwin) \
     gdk_x11_window_foreign_new_for_display(gdk_display_get_default(), xwin)
 
-typedef struct _client {
-    char           *name;
+typedef struct {
     Window          win;
     char           *msg;
+#if 0
+    char           *name;
     char           *clientname;
     char           *version;
     char           *author;
@@ -23,10 +24,12 @@ typedef struct _client {
     char           *web;
     char           *address;
     char           *info;
+#endif
 } Client;
 
-Client         *e_client = NULL;
+static Client  *e_client = NULL;
 
+static Display *dpy = NULL;
 static Window   root_win = None;
 static Window   comms_win = None;
 static Window   my_win = None;
@@ -38,27 +41,24 @@ static void     CommsSetup(void);
 static GdkFilterReturn CommsFilter(GdkXEvent * gdk_xevent, GdkEvent * event,
                                    void *data);
 static Window   CommsFindCommsWindow(void);
-static char    *CommsGet(Client ** c, XEvent * ev);
+static char    *CommsGet(XEvent * ev);
 static Client  *MakeClient(Window win);
-static void     ListFreeClient(void *ptr);
+static void     ListFreeClient(Client * cl);
 static char     in_init = 0;
+static unsigned int Atom_ENL_MSG;
 
 int
 CommsInit(void  (*msg_receive_func)(char *msg))
 {
     Window          win;
-    char            st[32];
-    Client         *cl;
 
     CommsSetup();
     comms_win = win = CommsFindCommsWindow();
     if (win == None)
         return 1;
 
-    cl = MakeClient(win);
-    g_snprintf(st, sizeof(st), "%8x", (int)win);
-    cl->name = g_strdup(st);
-    e_client = cl;
+    e_client = MakeClient(win);
+
     gdkwin = gdk_window_foreign_new(win);
     gdk_window_add_filter(gdkwin, CommsFilter, NULL);
     gdkwin2 = gdk_window_foreign_new(my_win);
@@ -75,27 +75,23 @@ CommsSend(const char *s)
     char            ss[21];
     int             i, j, k, len;
     XEvent          ev;
-    static Atom     a = 0;
-    Client         *c;
+    Client         *cl = e_client;
 
-    c = e_client;
-    if ((!s) || (!c))
+    if (!s || !cl)
         return;
-    len = strlen(s);
 
-    if (!a)
-        a = gdk_x11_get_xatom_by_name("ENL_MSG");
+    len = strlen(s);
 
     ev.xclient.type = ClientMessage;
     ev.xclient.serial = 0;
     ev.xclient.send_event = True;
-    ev.xclient.window = c->win;
-    ev.xclient.message_type = a;
+    ev.xclient.window = cl->win;
+    ev.xclient.message_type = Atom_ENL_MSG;
     ev.xclient.format = 8;
 
     for (i = 0; i < len + 1; i += 12)
     {
-        g_snprintf(ss, sizeof(ss), "%8x", (int)my_win);
+        snprintf(ss, sizeof(ss), "%8x", (int)my_win);
         for (j = 0; j < 12; j++)
         {
             ss[8 + j] = s[i + j];
@@ -105,10 +101,9 @@ CommsSend(const char *s)
         ss[20] = 0;
         for (k = 0; k < 20; k++)
             ev.xclient.data.b[k] = ss[k];
-        XSendEvent(gdk_x11_get_default_xdisplay(), c->win, False, 0,
-                   (XEvent *) & ev);
+        XSendEvent(dpy, cl->win, False, 0, &ev);
     }
-    XFlush(gdk_x11_get_default_xdisplay());
+    XFlush(dpy);
 }
 
 static          GdkFilterReturn
@@ -116,14 +111,7 @@ CommsFilter(GdkXEvent *gdk_xevent, GdkEvent *event __UNUSED__,
             void *data __UNUSED__)
 {
     XEvent         *xevent;
-    char           *msg = NULL;
-    Client         *c = NULL;
-    static Atom     a;
-
-    data = ""
-
-    if (!a)
-        a = gdk_x11_get_xatom_by_name("ENL_MSG");
+    char           *msg;
 
     xevent = (XEvent *) gdk_xevent;
     switch (xevent->type)
@@ -140,12 +128,8 @@ CommsFilter(GdkXEvent *gdk_xevent, GdkEvent *event __UNUSED__,
                 {
                     if ((comms_win = CommsFindCommsWindow()))
                     {
-                        char            st[256];
-
                         ListFreeClient(e_client);
                         e_client = MakeClient(comms_win);
-                        g_snprintf(st, sizeof(st), "%8x", (int)comms_win);
-                        e_client->name = g_strdup(st);
                         if (gdkwin)
                             g_object_unref(gdkwin);
                         gdkwin = gdk_window_foreign_new(comms_win);
@@ -196,14 +180,15 @@ CommsFilter(GdkXEvent *gdk_xevent, GdkEvent *event __UNUSED__,
         break;
 
     case ClientMessage:
-        if (xevent->xclient.message_type != a)
+        if (xevent->xclient.message_type != Atom_ENL_MSG)
             return GDK_FILTER_CONTINUE;
-        msg = CommsGet(&c, xevent);
+
+        msg = CommsGet(xevent);
         if (msg)
         {
             if (msg_receive_callback)
                 (*msg_receive_callback) (msg);
-            g_free(msg);
+            free(msg);
         }
         break;
 
@@ -218,18 +203,21 @@ CommsSetup(void)
 {
     char           *str;
 
+    dpy = gdk_x11_get_default_xdisplay();
+
     str = getenv("ENL_WM_ROOT");
-    root_win = (str) ? strtoul(str, NULL, 0) : GDK_ROOT_WINDOW();
+    root_win = (str) ? strtoul(str, NULL, 0) : DefaultRootWindow(dpy);
 
-    my_win = XCreateSimpleWindow(gdk_x11_get_default_xdisplay(), root_win,
-                                 -100, -100, 5, 5, 0, 0, 0);
+    my_win = XCreateSimpleWindow(dpy, root_win, -10, -10, 1, 1, 0, 0, 0);
+
+    Atom_ENL_MSG = XInternAtom(dpy, "ENL_MSG", False);
 }
 
 static          Window
 CommsFindCommsWindow(void)
 {
     unsigned char  *s;
-    Atom            a, ar;
+    Atom            Atom_ENL_COMMS, ar;
     unsigned long   num, after;
     int             format;
     Window          win = None;
@@ -237,13 +225,13 @@ CommsFindCommsWindow(void)
     int             dint;
     unsigned int    duint;
 
-    a = gdk_x11_get_xatom_by_name("ENLIGHTENMENT_COMMS");
-    if (a == None)
+    Atom_ENL_COMMS = XInternAtom(dpy, "ENLIGHTENMENT_COMMS", True);
+    if (Atom_ENL_COMMS == None)
         return None;
 
     s = NULL;
-    XGetWindowProperty(gdk_x11_get_default_xdisplay(), root_win, a, 0, 14,
-                       False, AnyPropertyType, &ar, &format, &num, &after, &s);
+    XGetWindowProperty(dpy, root_win, Atom_ENL_COMMS, 0, 14, False,
+                       AnyPropertyType, &ar, &format, &num, &after, &s);
     if (s)
     {
         sscanf((char *)s, "%*s %x", (unsigned int *)&win);
@@ -253,15 +241,13 @@ CommsFindCommsWindow(void)
     if (win == None)
         return None;
 
-    if (!XGetGeometry
-        (gdk_x11_get_default_xdisplay(), win, &rt, &dint, &dint,
-         &duint, &duint, &duint, &duint))
+    if (!XGetGeometry(dpy, win, &rt,
+                      &dint, &dint, &duint, &duint, &duint, &duint))
         return None;
 
     s = NULL;
-    XGetWindowProperty(gdk_x11_get_default_xdisplay(), win, a, 0,
-                       14, False, AnyPropertyType, &ar, &format,
-                       &num, &after, &s);
+    XGetWindowProperty(dpy, win, Atom_ENL_COMMS, 0, 14, False,
+                       AnyPropertyType, &ar, &format, &num, &after, &s);
     if (!s)
         return None;
 
@@ -271,86 +257,91 @@ CommsFindCommsWindow(void)
 }
 
 static char    *
-CommsGet(Client **c, XEvent *ev)
+CommsGet(XEvent *ev)
 {
-    char            s[13], s2[9], *msg;
-    int             i;
-    Window          win;
-    Client         *cl;
-    static Atom     a;
+    char            buf[13], *msg;
+    unsigned int    win;
+    Client         *cl = e_client;
 
-    if (!a)
-        a = gdk_x11_get_xatom_by_name("ENL_MSG");
+    if (!cl)
+        return NULL;
 
-    if ((!ev) || (!c))
-        return (NULL);
+    if (!ev)
+        return NULL;
     if (ev->type != ClientMessage)
-        return (NULL);
-    if (ev->xclient.message_type != a)
-        return (NULL);
-    s[12] = 0;
-    s2[8] = 0;
-    msg = NULL;
-    for (i = 0; i < 8; i++)
-        s2[i] = ev->xclient.data.b[i];
-    for (i = 0; i < 12; i++)
-        s[i] = ev->xclient.data.b[i + 8];
-    sscanf(s2, "%x", (int *)&win);
-    cl = e_client;
-    if (!cl)
-        return (NULL);
+        return NULL;
+    if (ev->xclient.message_type != Atom_ENL_MSG)
+        return NULL;
+
+    /* Message should start with comms_win but we don't check */
+    memcpy(buf, ev->xclient.data.b, 8);
+    buf[8] = 0;
+    sscanf(buf, "%x", &win);
+
+    memcpy(buf, ev->xclient.data.b + 8, 12);
+    buf[12] = 0;
+
     if (cl->msg)
     {
         /* append text to end of msg */
-        cl->msg = g_realloc(cl->msg, strlen(cl->msg) + strlen(s) + 1);
+        cl->msg = realloc(cl->msg, strlen(cl->msg) + strlen(buf) + 1);
         if (!cl->msg)
-            return (NULL);
-        strcat(cl->msg, s);
+            return NULL;
+        strcat(cl->msg, buf);
     }
     else
     {
         /* new msg */
-        cl->msg = g_malloc(strlen(s) + 1);
+        cl->msg = malloc(strlen(buf) + 1);
         if (!cl->msg)
-            return (NULL);
-        strcpy(cl->msg, s);
+            return NULL;
+        strcpy(cl->msg, buf);
     }
-    if (strlen(s) < 12)
+
+    msg = NULL;
+    if (strlen(buf) < 12)
     {
         msg = cl->msg;
         cl->msg = NULL;
-        *c = cl;
     }
-    return (msg);
+    return msg;
 }
 
 static Client  *
 MakeClient(Window win)
 {
-    Client         *c;
+    Client         *cl;
 
-    c = g_malloc0(sizeof(Client));
-    if (c)
-        c->win = win;
-    return c;
+    cl = calloc(1, sizeof(Client));
+    if (!cl)
+        return cl;
+
+    cl->win = win;
+
+#if 0
+    char            st[32];
+    snprintf(st, sizeof(st), "%8x", (int)win);
+    cl->name = strdup(st);
+#endif
+
+    return cl;
 }
 
 static void
-ListFreeClient(void *ptr)
+ListFreeClient(Client *cl)
 {
-    Client         *c;
-
-    c = (Client *) ptr;
-    if (!c)
+    if (!cl)
         return;
-    g_free(c->name);
-    g_free(c->msg);
-    g_free(c->clientname);
-    g_free(c->version);
-    g_free(c->author);
-    g_free(c->email);
-    g_free(c->web);
-    g_free(c->address);
-    g_free(c->info);
-    g_free(c);
+    free(cl->msg);
+#if 0
+    free(cl->name);
+    free(cl->clientname);
+    free(cl->version);
+    free(cl->author);
+    free(cl->email);
+    free(cl->web);
+    free(cl->address);
+    free(cl->info);
+#endif
+    free(cl);
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to