Rearrange code, eliminating forward references

This commit is contained in:
Kim Woelders 2023-12-08 13:55:06 +01:00
parent 95ca821c14
commit 4e3f9d5955
1 changed files with 177 additions and 186 deletions

363
ipc.c
View File

@ -27,83 +27,163 @@ typedef struct {
#endif
} Client;
static Client *e_client = NULL;
static Display *dpy = NULL;
static Window root_win = None;
static Window comms_win = None;
static Window my_win = None;
static Client *e_client = NULL;
static GdkWindow *gdkwin = NULL;
static GdkWindow *gdkwin2 = NULL;
static void (*msg_receive_callback)(char *msg) = NULL;
static void CommsSetup(void);
static GdkFilterReturn CommsFilter(GdkXEvent * gdk_xevent, GdkEvent * event,
void *data);
static Window CommsFindCommsWindow(void);
static char *CommsGet(XEvent * ev);
static Client *MakeClient(Window win);
static void ListFreeClient(Client * cl);
static char in_init = 0;
static unsigned int Atom_ENL_MSG;
int
CommsInit(void (*msg_receive_func)(char *msg))
static void
CommsSetup(void)
{
Window win;
char *str;
CommsSetup();
comms_win = win = CommsFindCommsWindow();
if (win == None)
return 1;
dpy = gdk_x11_get_default_xdisplay();
e_client = MakeClient(win);
str = getenv("ENL_WM_ROOT");
root_win = (str) ? strtoul(str, NULL, 0) : DefaultRootWindow(dpy);
gdkwin = gdk_window_foreign_new(win);
gdk_window_add_filter(gdkwin, CommsFilter, NULL);
gdkwin2 = gdk_window_foreign_new(my_win);
gdk_window_add_filter(gdkwin2, CommsFilter, NULL);
gdk_window_set_events(gdkwin, GDK_STRUCTURE_MASK);
msg_receive_callback = msg_receive_func;
my_win = XCreateSimpleWindow(dpy, root_win, -10, -10, 1, 1, 0, 0, 0);
return 0;
Atom_ENL_MSG = XInternAtom(dpy, "ENL_MSG", False);
}
void
CommsSend(const char *s)
static Window
CommsFindCommsWindow(void)
{
char ss[21];
int i, j, k, len;
XEvent ev;
unsigned char *s;
Atom Atom_ENL_COMMS, ar;
unsigned long num, after;
int format;
Window win = None;
Window rt;
int dint;
unsigned int duint;
Atom_ENL_COMMS = XInternAtom(dpy, "ENLIGHTENMENT_COMMS", True);
if (Atom_ENL_COMMS == None)
return None;
s = NULL;
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);
XFree(s);
}
if (win == None)
return None;
if (!XGetGeometry(dpy, win, &rt,
&dint, &dint, &duint, &duint, &duint, &duint))
return None;
s = NULL;
XGetWindowProperty(dpy, win, Atom_ENL_COMMS, 0, 14, False,
AnyPropertyType, &ar, &format, &num, &after, &s);
if (!s)
return None;
XFree(s);
return win;
}
static Client *
MakeClient(Window win)
{
Client *cl;
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(Client *cl)
{
if (!cl)
return;
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);
}
static char *
CommsGet(XEvent *ev)
{
char buf[13], *msg;
unsigned int win;
Client *cl = e_client;
if (!s || !cl)
return;
if (!cl)
return NULL;
len = strlen(s);
if (!ev)
return NULL;
if (ev->type != ClientMessage)
return NULL;
if (ev->xclient.message_type != Atom_ENL_MSG)
return NULL;
ev.xclient.type = ClientMessage;
ev.xclient.serial = 0;
ev.xclient.send_event = True;
ev.xclient.window = cl->win;
ev.xclient.message_type = Atom_ENL_MSG;
ev.xclient.format = 8;
/* 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);
for (i = 0; i < len + 1; i += 12)
memcpy(buf, ev->xclient.data.b + 8, 12);
buf[12] = 0;
if (cl->msg)
{
snprintf(ss, sizeof(ss), "%8x", (int)my_win);
for (j = 0; j < 12; j++)
{
ss[8 + j] = s[i + j];
if (!s[i + j])
j = 12;
}
ss[20] = 0;
for (k = 0; k < 20; k++)
ev.xclient.data.b[k] = ss[k];
XSendEvent(dpy, cl->win, False, 0, &ev);
/* append text to end of msg */
cl->msg = realloc(cl->msg, strlen(cl->msg) + strlen(buf) + 1);
if (!cl->msg)
return NULL;
strcat(cl->msg, buf);
}
XFlush(dpy);
else
{
/* new msg */
cl->msg = malloc(strlen(buf) + 1);
if (!cl->msg)
return NULL;
strcpy(cl->msg, buf);
}
msg = NULL;
if (strlen(buf) < 12)
{
msg = cl->msg;
cl->msg = NULL;
}
return msg;
}
static GdkFilterReturn
@ -198,150 +278,61 @@ CommsFilter(GdkXEvent *gdk_xevent, GdkEvent *event __UNUSED__,
return GDK_FILTER_REMOVE;
}
static void
CommsSetup(void)
int
CommsInit(void (*msg_receive_func)(char *msg))
{
char *str;
dpy = gdk_x11_get_default_xdisplay();
str = getenv("ENL_WM_ROOT");
root_win = (str) ? strtoul(str, NULL, 0) : DefaultRootWindow(dpy);
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 Atom_ENL_COMMS, ar;
unsigned long num, after;
int format;
Window win = None;
Window rt;
int dint;
unsigned int duint;
Atom_ENL_COMMS = XInternAtom(dpy, "ENLIGHTENMENT_COMMS", True);
if (Atom_ENL_COMMS == None)
return None;
s = NULL;
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);
XFree(s);
}
Window win;
CommsSetup();
comms_win = win = CommsFindCommsWindow();
if (win == None)
return None;
return 1;
if (!XGetGeometry(dpy, win, &rt,
&dint, &dint, &duint, &duint, &duint, &duint))
return None;
e_client = MakeClient(win);
s = NULL;
XGetWindowProperty(dpy, win, Atom_ENL_COMMS, 0, 14, False,
AnyPropertyType, &ar, &format, &num, &after, &s);
if (!s)
return None;
gdkwin = gdk_window_foreign_new(win);
gdk_window_add_filter(gdkwin, CommsFilter, NULL);
gdkwin2 = gdk_window_foreign_new(my_win);
gdk_window_add_filter(gdkwin2, CommsFilter, NULL);
gdk_window_set_events(gdkwin, GDK_STRUCTURE_MASK);
msg_receive_callback = msg_receive_func;
XFree(s);
return win;
return 0;
}
static char *
CommsGet(XEvent *ev)
void
CommsSend(const char *s)
{
char buf[13], *msg;
unsigned int win;
char ss[21];
int i, j, k, len;
XEvent ev;
Client *cl = e_client;
if (!cl)
return NULL;
if (!ev)
return NULL;
if (ev->type != ClientMessage)
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 = realloc(cl->msg, strlen(cl->msg) + strlen(buf) + 1);
if (!cl->msg)
return NULL;
strcat(cl->msg, buf);
}
else
{
/* new msg */
cl->msg = malloc(strlen(buf) + 1);
if (!cl->msg)
return NULL;
strcpy(cl->msg, buf);
}
msg = NULL;
if (strlen(buf) < 12)
{
msg = cl->msg;
cl->msg = NULL;
}
return msg;
}
static Client *
MakeClient(Window win)
{
Client *cl;
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(Client *cl)
{
if (!cl)
if (!s || !cl)
return;
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);
len = strlen(s);
ev.xclient.type = ClientMessage;
ev.xclient.serial = 0;
ev.xclient.send_event = True;
ev.xclient.window = cl->win;
ev.xclient.message_type = Atom_ENL_MSG;
ev.xclient.format = 8;
for (i = 0; i < len + 1; i += 12)
{
snprintf(ss, sizeof(ss), "%8x", (int)my_win);
for (j = 0; j < 12; j++)
{
ss[8 + j] = s[i + j];
if (!s[i + j])
j = 12;
}
ss[20] = 0;
for (k = 0; k < 20; k++)
ev.xclient.data.b[k] = ss[k];
XSendEvent(dpy, cl->win, False, 0, &ev);
}
XFlush(dpy);
}