wmii/cmd/tray/xembed.c

134 lines
2.7 KiB
C
Raw Normal View History

2010-05-27 11:58:02 +04:00
/* Copyright ©2010 Kris Maglione <maglione.k at Gmail>
* See LICENSE file for license details.
*/
#include "dat.h"
#include "fns.h"
#define DEAD ~0UL
enum { XEmbedVersion = 0 };
enum XEmbedMessage {
XEmbedEmbeddedNotify,
XEmbedWindowActivate,
XEmbedWindowDeactivate,
XEmbedRequestFocus,
XEmbedFocusIn,
XEmbedFocusOut,
XEmbedFocusNext,
XEmbedFocusPrev,
XEmbedModalityOn = 10,
XEmbedModalityOff,
XEmbedRegisterAccelerator,
XEmbedUnregisterAccelerator,
XEmbedActivateAccelerator,
};
enum XEmbedFocusDetail {
XEmbedFocusCurrent,
XEmbedFocusFirst,
XEmbedFocusLast,
};
static Handlers handlers;
static void xembed_updateinfo(XEmbed*);
static void xembed_sendmessage(XEmbed*, long, long, long, long);
XEmbed*
xembed_swallow(Window *parent, Window *client, void (*cleanup)(XEmbed*)) {
XEmbed *xembed;
xembed = emallocz(sizeof *xembed);
xembed->w = client;
xembed->owner = parent;
xembed->cleanup = cleanup;
selectinput(client, client->eventmask | PropertyChangeMask | StructureNotifyMask);
pushhandler(client, &handlers, xembed);
reparentwindow(client, parent, ZP);
xembed_updateinfo(xembed);
xembed_sendmessage(xembed, XEmbedEmbeddedNotify, 0, parent->xid, min(XEmbedVersion, xembed->version));
return xembed;
}
void
xembed_disown(XEmbed *xembed) {
pophandler(xembed->w, &handlers);
if(xembed->flags != DEAD) {
2010-05-27 11:58:02 +04:00
reparentwindow(xembed->w, &scr.root, ZP);
unmapwin(xembed->w);
}
2010-05-27 11:58:02 +04:00
if(xembed->cleanup)
xembed->cleanup(xembed);
free(xembed);
}
static void
xembed_updateinfo(XEmbed *xembed) {
ulong *res;
int n;
n = getprop_ulong(xembed->w, "_XEMBED_INFO", "_XEMBED_INFO", 0, &res, 2);
xembed->flags = 0UL;
if(n >= 2) {
xembed->version = res[0];
xembed->flags = res[1];
}
free(res);
if(xembed->flags & XEmbedMapped)
mapwin(xembed->w);
else
unmapwin(xembed->w);
}
static void
xembed_sendmessage(XEmbed *xembed, long message, long detail, long data1, long data2) {
traperrors(true);
sendmessage(xembed->w, "_XEMBED", event_xtime, message, detail, data1, data2);
traperrors(false);
}
2010-05-28 02:57:08 +04:00
static bool
2010-05-27 11:58:02 +04:00
destroy_event(Window *w, void *aux, XDestroyWindowEvent *ev) {
XEmbed *xembed;
xembed = aux;
xembed->flags = DEAD;
xembed_disown(xembed);
2010-05-28 02:57:08 +04:00
return false;
2010-05-27 11:58:02 +04:00
}
2010-05-28 02:57:08 +04:00
static bool
2010-05-27 11:58:02 +04:00
property_event(Window *w, void *aux, XPropertyEvent *ev) {
XEmbed *xembed;
2010-06-02 04:09:25 +04:00
Dprint("property_event(%W, %p, %A)\n",
w, aux, ev->atom);
2010-05-27 11:58:02 +04:00
xembed = aux;
if(ev->atom == xatom("_XEMBED_INFO"))
xembed_updateinfo(xembed);
2010-05-28 02:57:08 +04:00
return false;
2010-05-27 11:58:02 +04:00
}
2010-05-28 02:57:08 +04:00
static bool
2010-05-27 11:58:02 +04:00
reparent_event(Window *w, void *aux, XReparentEvent *ev) {
XEmbed *xembed;
xembed = aux;
if(ev->parent != xembed->owner->xid) {
xembed->flags = DEAD;
xembed_disown(xembed);
}
2010-05-28 02:57:08 +04:00
return false;
2010-05-27 11:58:02 +04:00
}
static Handlers handlers = {
.destroy = destroy_event,
.property = property_event,
.reparent = reparent_event,
};