JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
cursor is hid when unfocused.
[st.git] / st.c
diff --git a/st.c b/st.c
index 6dcded5..73121ef 100644 (file)
--- a/st.c
+++ b/st.c
 #include <X11/keysym.h>
 #include <X11/Xutil.h>
 
-#if   defined(LINUX)
+#if   defined(__linux)
  #include <pty.h>
-#elif defined(OPENBSD)
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
  #include <util.h>
-#elif defined(FREEBSD)
+#elif defined(__FreeBSD__) || defined(__DragonFly__)
  #include <libutil.h>
 #endif
 
@@ -108,6 +108,7 @@ typedef struct {
        int bufh; /* pixmap height */
        int ch; /* char height */
        int cw; /* char width  */
+       int hasfocus;
 } XWindow; 
 
 typedef struct {
@@ -161,23 +162,27 @@ static void ttyread(void);
 static void ttyresize(int, int);
 static void ttywrite(const char *, size_t);
 
-static void xbell(void);
 static void xdraws(char *, Glyph, int, int, int);
 static void xhints(void);
 static void xclear(int, int, int, int);
-static void xcursor(void);
+static void xdrawcursor(void);
 static void xinit(void);
 static void xloadcols(void);
+static void xseturgency(int);
 
 static void expose(XEvent *);
 static char* kmap(KeySym);
 static void kpress(XEvent *);
 static void resize(XEvent *);
+static void focus(XEvent *);
+
 
 static void (*handler[LASTEvent])(XEvent *) = {
        [KeyPress] = kpress,
        [Expose] = expose,
-       [ConfigureNotify] = resize
+       [ConfigureNotify] = resize,
+       [FocusIn] = focus,
+       [FocusOut] = focus,
 };
 
 /* Globals */
@@ -187,7 +192,6 @@ static Term term;
 static CSIEscape escseq;
 static int cmdfd;
 static pid_t pid;
-static int running;
 
 #ifdef DEBUG
 void
@@ -227,15 +231,6 @@ execsh(void) {
        execvp(args[0], args);
 }
 
-void
-xbell(void) {
-       XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
-       XFillRectangle(xw.dis, xw.win, dc.gc, BORDER, BORDER, xw.bufw, xw.bufh);
-       XFlush(xw.dis);
-       usleep(BellTime);
-       draw(SCREEN_REDRAW);
-}
-
 void 
 sigchld(int a) {
        int stat = 0;
@@ -930,7 +925,8 @@ tputc(char c) {
                        tnewline();
                        break;
                case '\a':
-                       xbell();
+                       if(!xw.hasfocus)
+                               xseturgency(1);
                        break;
                case '\033':
                        csireset();
@@ -1071,9 +1067,6 @@ xinit(void) {
        /* colors */
        xloadcols();
 
-       term.c.attr.fg = DefaultFG;
-       term.c.attr.bg = DefaultBG;
-       term.c.attr.mode = ATTR_NULL;
        /* windows */
        xw.h = term.row * xw.ch + 2*BORDER;
        xw.w = term.col * xw.cw + 2*BORDER;
@@ -1118,7 +1111,7 @@ xdraws(char *s, Glyph base, int x, int y, int len) {
 }
 
 void
-xcursor(void) {
+xdrawcursor(void) {
        static int oldx = 0;
        static int oldy = 0;
        Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
@@ -1136,7 +1129,7 @@ xcursor(void) {
                xclear(oldx, oldy, oldx, oldy);
        
        /* draw the new one */
-       if(!(term.c.state & CURSOR_HIDE)) {
+       if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
                xdraws(&g.c, g, term.c.x, term.c.y, 1);
                oldx = term.c.x, oldy = term.c.y;
        }
@@ -1163,7 +1156,7 @@ draw(int dummy) {
                        if(term.line[y][x].state & GLYPH_SET)
                                xdrawc(x, y, term.line[y][x]);
 
-       xcursor();
+       xdrawcursor();
        XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
        XFlush(xw.dis);
 }
@@ -1199,7 +1192,7 @@ draw(int redraw_all) {
                if(i > 0)
                        xdraws(buf, base, ox, y, i);
        }
-       xcursor();
+       xdrawcursor();
        XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
        XFlush(xw.dis);
 }
@@ -1211,6 +1204,21 @@ expose(XEvent *ev) {
        draw(SCREEN_REDRAW);
 }
 
+void
+xseturgency(int add) {
+       XWMHints *h = XGetWMHints(xw.dis, xw.win);
+       h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
+       XSetWMHints(xw.dis, xw.win, h);
+       XFree(h);
+}
+
+void
+focus(XEvent *ev) {
+       if((xw.hasfocus = ev->type == FocusIn))
+               xseturgency(0);
+       draw(SCREEN_UPDATE);
+}
+
 char*
 kmap(KeySym k) {
        int i;
@@ -1285,12 +1293,12 @@ run(void) {
        XEvent ev;
        fd_set rfd;
        int xfd = XConnectionNumber(xw.dis);
+       long mask = ExposureMask | KeyPressMask | StructureNotifyMask | FocusChangeMask;
 
-       running = 1;
-       XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
+       XSelectInput(xw.dis, xw.win, mask);
        XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
 
-       while(running) {
+       while(1) {
                FD_ZERO(&rfd);
                FD_SET(cmdfd, &rfd);
                FD_SET(xfd, &rfd);