JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
tweak focus to support XEMBED client message and remove focus on EnterNotify. (thx...
[st.git] / st.c
diff --git a/st.c b/st.c
index 6ba5c4c..6f292ba 100644 (file)
--- a/st.c
+++ b/st.c
 
 #define USAGE \
        "st-" VERSION ", (c) 2010-2011 st engineers\n" \
-       "usage: st [-t title] [-c class] [-v] [-e command...]\n"
+       "usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n"
+
+/* XEMBED messages */
+#define XEMBED_FOCUS_IN  4
+#define XEMBED_FOCUS_OUT 5
 
 /* Arbitrary sizes */
 #define ESC_TITLE_SIZ 256
@@ -217,6 +221,7 @@ static void visibility(XEvent *);
 static void unmap(XEvent *);
 static char* kmap(KeySym, unsigned int state);
 static void kpress(XEvent *);
+static void cmessage(XEvent *);
 static void resize(XEvent *);
 static void focus(XEvent *);
 static void brelease(XEvent *);
@@ -237,6 +242,7 @@ static int isfullutf8(char *, int);
 
 static void (*handler[LASTEvent])(XEvent *) = {
        [KeyPress] = kpress,
+       [ClientMessage] = cmessage,
        [ConfigureNotify] = resize,
        [VisibilityNotify] = visibility,
        [UnmapNotify] = unmap,
@@ -260,7 +266,9 @@ static pid_t pid;
 static Selection sel;
 static char **opt_cmd  = NULL;
 static char *opt_title = NULL;
+static char *opt_embed = NULL;
 static char *opt_class = NULL;
+static Atom xembedatom;
 
 int
 utf8decode(char *s, long *u) {
@@ -614,7 +622,7 @@ bmotion(XEvent *e) {
                if(oldey != sel.ey || oldex != sel.ex) {
                        int starty = MIN(oldey, sel.ey);
                        int endy = MAX(oldey, sel.ey);
-                       drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row));
+                       drawregion(0, (starty > 0 ? starty : 0), term.col, (endy < term.row ? endy+1 : term.row));
                }
        }
 }
@@ -1606,6 +1614,7 @@ void
 xinit(void) {
        XSetWindowAttributes attrs;
        Cursor cursor;
+       Window parent;
 
        if(!(xw.dpy = XOpenDisplay(NULL)))
                die("Can't open display\n");
@@ -1623,8 +1632,8 @@ xinit(void) {
        xloadcols();
 
        /* window - default size */
-       xw.bufh = 24 * xw.ch;
-       xw.bufw = 80 * xw.cw;
+       xw.bufh = term.row * xw.ch;
+       xw.bufw = term.col * xw.cw;
        xw.h = xw.bufh + 2*BORDER;
        xw.w = xw.bufw + 2*BORDER;
 
@@ -1633,10 +1642,12 @@ xinit(void) {
        attrs.bit_gravity = NorthWestGravity;
        attrs.event_mask = FocusChangeMask | KeyPressMask
                | ExposureMask | VisibilityChangeMask | StructureNotifyMask
-               | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
+               | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask
+               | EnterWindowMask | LeaveWindowMask;
        attrs.colormap = xw.cmap;
 
-       xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0,
+       parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
+       xw.win = XCreateWindow(xw.dpy, parent, 0, 0,
                        xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
                        XDefaultVisual(xw.dpy, xw.scr),
                        CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
@@ -1660,6 +1671,8 @@ xinit(void) {
                &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
                &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
 
+       xembedatom = XInternAtom(xw.dpy, "_XEMBED", False);
+
        XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
        XMapWindow(xw.dpy, xw.win);
        xhints();
@@ -1884,6 +1897,19 @@ kpress(XEvent *ev) {
 }
 
 void
+cmessage(XEvent *e) {
+       if (e->xclient.message_type == xembedatom && e->xclient.format == 32) {
+               if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
+                       xw.state |= WIN_FOCUSED;
+                       xseturgency(0);
+               } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
+                       xw.state &= ~WIN_FOCUSED;
+               }
+               draw();
+       }
+}
+
+void
 resize(XEvent *e) {
        int col, row;
        
@@ -1943,6 +1969,9 @@ main(int argc, char *argv[]) {
                case 'c':
                        if(++i < argc) opt_class = argv[i];
                        break;
+               case 'w':
+                       if(++i < argc) opt_embed = argv[i];
+                       break;
                case 'e': 
                        /* eat every remaining arguments */
                        if(++i < argc) opt_cmd = &argv[i];