set _NET_WM_USER_TIME on the window
[dana/urxvt.git] / src / rxvttoolkit.C
index 4db0eb1..c52c954 100644 (file)
@@ -1,9 +1,9 @@
-/*--------------------------------*-C-*---------------------------------*
+/*----------------------------------------------------------------------*
  * File:       rxvttoolkit.C
  *----------------------------------------------------------------------*
  *
  * All portions of code are copyright by their respective author/s.
- * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com>
+ * Copyright (c) 2003-2007 Marc Lehmann <pcg@goof.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -61,12 +61,14 @@ const char *const xa_names[] =
   "_NET_WM_NAME",
   "_NET_WM_ICON_NAME",
   "_NET_WM_PING",
+  "_NET_WM_ICON",
+  "_NET_WM_USER_TIME",
 #endif
 #if USE_XIM
   "WM_LOCALE_NAME",
   "XIM_SERVERS",
 #endif
-#ifdef TRANSPARENT
+#ifdef ENABLE_TRANSPARENCY
   "_XROOTPMAP_ID",
   "ESETROOT_PMAP_ID",
 #endif
@@ -185,7 +187,7 @@ rxvt_xim::ref_init ()
   ximcallback.client_data = (XPointer)this;
   ximcallback.callback = im_destroy_cb;
 
-  XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0);
+  XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0);
 
   return true;
 }
@@ -264,10 +266,8 @@ rxvt_screen::set (rxvt_display *disp)
 }
 
 void
-rxvt_screen::set (rxvt_display *disp, int bitdepth)
+rxvt_screen::select_visual (int bitdepth)
 {
-  set (disp);
-
 #if XFT
   XVisualInfo vinfo;
 
@@ -275,7 +275,7 @@ rxvt_screen::set (rxvt_display *disp, int bitdepth)
     {
       depth  = bitdepth;
       visual = vinfo.visual;
-      cmap   = XCreateColormap (dpy, disp->root, visual, AllocNone);
+      cmap   = XCreateColormap (dpy, display->root, visual, AllocNone);
     }
 #endif
 }
@@ -299,15 +299,17 @@ rxvt_screen::clear ()
 
 rxvt_display::rxvt_display (const char *id)
 : refcounted (id)
-, x_ev (this, &rxvt_display::x_cb)
 , selection_owner (0)
+, clipboard_owner (0)
 {
+  x_ev    .set<rxvt_display, &rxvt_display::x_cb    > (this);
+  flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this);
 }
 
 XrmDatabase
 rxvt_display::get_resources (bool refresh)
 {
-  char *homedir = (char *)getenv ("HOME");
+  char *homedir = getenv ("HOME");
   char fname[1024];
 
   /*
@@ -317,11 +319,12 @@ rxvt_display::get_resources (bool refresh)
   XrmDatabase rdb1, database = 0;
 
   // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
+  // as opposed to "standard practise", we always read in ~/.Xdefaults
 
   // 6. System wide per application default file.
 
   /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
-  if ((xe = (char *)getenv ("XAPPLRESDIR")))
+  if ((xe = getenv ("XAPPLRESDIR")))
     {
       snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
 
@@ -333,6 +336,14 @@ rxvt_display::get_resources (bool refresh)
   // none
 
   // 4. User's defaults file.
+  if (homedir)
+    {
+      snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
+
+      if ((rdb1 = XrmGetFileDatabase (fname)))
+        XrmMergeDatabases (rdb1, &database);
+    }
+
   /* Get any Xserver defaults */
   if (refresh)
     {
@@ -358,6 +369,7 @@ rxvt_display::get_resources (bool refresh)
        else
          {
            displayResource = 0;
+
            if (val)
              XFree(val);
          }
@@ -374,13 +386,6 @@ rxvt_display::get_resources (bool refresh)
       if ((rdb1 = XrmGetStringDatabase (displayResource)))
         XrmMergeDatabases (rdb1, &database);
     }
-  else if (homedir)
-    {
-      snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
-
-      if ((rdb1 = XrmGetFileDatabase (fname)))
-        XrmMergeDatabases (rdb1, &database);
-    }
 
 #if !XLIB_ILLEGAL_ACCESS
   if (refresh && displayResource)
@@ -401,7 +406,7 @@ rxvt_display::get_resources (bool refresh)
 
   // 3. User's per host defaults file
   /* Add in XENVIRONMENT file */
-  if ((xe = (char *)getenv ("XENVIRONMENT"))
+  if ((xe = getenv ("XENVIRONMENT"))
       && (rdb1 = XrmGetFileDatabase (xe)))
     XrmMergeDatabases (rdb1, &database);
   else if (homedir)
@@ -425,11 +430,11 @@ bool rxvt_display::ref_init ()
 #ifdef LOCAL_X_IS_UNIX
   if (id[0] == ':')
     {
-      val = rxvt_malloc (5 + strlen (id) + 1);
+      if (!(val = rxvt_temp_buf<char> (5 + strlen (id) + 1)))
+        return false;
       strcpy (val, "unix/");
       strcat (val, id);
       dpy = XOpenDisplay (val);
-      free (val);
     }
   else
 #endif
@@ -462,17 +467,18 @@ bool rxvt_display::ref_init ()
 
   int fd = XConnectionNumber (dpy);
 
-  // try to detect wether we have a local connection.
-  // assume unix domains socket == local, everything else not
+  // try to detect whether we have a local connection.
+  // assume unix domain socket == local, everything else not
   // TODO: might want to check for inet/127.0.0.1
   is_local = 0;
   sockaddr_un sa;
   socklen_t sl = sizeof (sa);
 
   if (!getsockname (fd, (sockaddr *)&sa, &sl))
-    is_local = sa.sun_family == AF_LOCAL;
+    is_local = sa.sun_family == AF_UNIX;
 
-  x_ev.start (fd, EVENT_READ);
+  flush_ev.start ();
+  x_ev.start (fd, ev::READ);
   fcntl (fd, F_SETFD, FD_CLOEXEC);
 
   XSelectInput (dpy, root, PropertyChangeMask);
@@ -485,7 +491,7 @@ bool rxvt_display::ref_init ()
 void
 rxvt_display::ref_next ()
 {
-  // TODO: somehow check wether the database files/resources changed
+  // TODO: somehow check whether the database files/resources changed
   // before affording re-loading/parsing
   XrmDestroyDatabase (XrmGetDatabase (dpy));
   XrmSetDatabase (dpy, get_resources (true));
@@ -500,6 +506,7 @@ rxvt_display::~rxvt_display ()
   XFreeCursor (dpy, blank_cursor);
 #endif
   x_ev.stop ();
+  flush_ev.stop ();
 #ifdef USE_XIM
   xims.clear ();
 #endif
@@ -539,63 +546,76 @@ void rxvt_display::im_change_check ()
 }
 #endif
 
-void rxvt_display::x_cb (io_watcher &w, short revents)
+void rxvt_display::x_cb (ev::io &w, int revents)
 {
-  do
-    {
-      XEvent xev;
-      XNextEvent (dpy, &xev);
+  flush_ev.start ();
+}
+
+void rxvt_display::flush_cb (ev::prepare &w, int revents)
+{
+  while (XEventsQueued (dpy, QueuedAfterFlush))
+    do
+      {
+        XEvent xev;
+        XNextEvent (dpy, &xev);
 
 #ifdef USE_XIM
-      if (!XFilterEvent (&xev, None))
-        {
-          if (xev.type == PropertyNotify
-              && xev.xany.window == root
-              && xev.xproperty.atom == xa[XA_XIM_SERVERS])
-            im_change_check ();
+        if (!XFilterEvent (&xev, None))
+          {
+            if (xev.type == PropertyNotify
+                && xev.xany.window == root
+                && xev.xproperty.atom == xa[XA_XIM_SERVERS])
+              im_change_check ();
 #endif
-          for (int i = xw.size (); i--; )
-            {
-              if (!xw[i])
-                xw.erase_unordered (i);
-              else if (xw[i]->window == xev.xany.window)
-                xw[i]->call (xev);
-            }
+            if (xev.type == MappingNotify)
+              XRefreshKeyboardMapping (&xev.xmapping);
+
+            for (int i = xw.size (); i--; )
+              {
+                if (!xw[i])
+                  xw.erase_unordered (i);
+                else if (xw[i]->window == xev.xany.window)
+                  xw[i]->call (xev);
+              }
 #ifdef USE_XIM
-        }
+          }
 #endif
-    }
-  while (XEventsQueued (dpy, QueuedAlready));
-
-  XFlush (dpy);
-}
+      }
+    while (XEventsQueued (dpy, QueuedAlready));
 
-void rxvt_display::flush ()
-{
-  if (XEventsQueued (dpy, QueuedAlready))
-    x_cb (x_ev, EVENT_READ);
-
-  XFlush (dpy);
+  w.stop ();
 }
 
 void rxvt_display::reg (xevent_watcher *w)
 {
-  xw.push_back (w);
-  w->active = xw.size ();
+  if (!w->active)
+    {
+      xw.push_back (w);
+      w->active = xw.size ();
+    }
 }
 
 void rxvt_display::unreg (xevent_watcher *w)
 {
   if (w->active)
-    xw[w->active - 1] = 0;
+    {
+      xw[w->active - 1] = 0;
+      w->active = 0;
+    }
 }
 
-void rxvt_display::set_selection_owner (rxvt_term *owner)
+void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard)
 {
-  if (selection_owner && selection_owner != owner)
-    selection_owner->selection_clear ();
+  rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner;
+
+  if (cur_owner && cur_owner != owner)
+    {
+      rxvt_term *term = cur_owner;
+      term->selection_clear (clipboard);
+      term->flush ();
+    }
 
-  selection_owner = owner;
+  cur_owner = owner;
 }
 
 #ifdef USE_XIM
@@ -618,7 +638,7 @@ rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
   l = strlen (locale);
   m = strlen (modifiers);
 
-  if (!(id = (char *)malloc (l + m + 2)))
+  if (!(id = rxvt_temp_buf<char> (l + m + 2)))
     return 0;
 
   memcpy (id, locale, l); id[l] = '\n';
@@ -626,8 +646,6 @@ rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
 
   rxvt_xim *xim = xims.get (id);
 
-  free (id);
-
   return xim;
 }
 
@@ -651,10 +669,20 @@ template class refcache<rxvt_display>;
 refcache<rxvt_display> displays;
 
 /////////////////////////////////////////////////////////////////////////////
+//
+
+static unsigned int
+insert_component (unsigned int value, unsigned int mask, unsigned int shift)
+{
+  return (value * (mask + 1) >> 16) << shift;
+}
+
 bool
 rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
 {
+  //TODO: only supports 24 bit
+  int alpha = color.a >= 0xff00 ? 0xffff : color.a;
+
 #if XFT
   XRenderPictFormat *format;
 
@@ -668,12 +696,12 @@ rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
       c.color.red   = color.r;
       c.color.green = color.g;
       c.color.blue  = color.b;
-      c.color.alpha = color.a;
+      c.color.alpha = alpha;
 
-      c.pixel = ((color.r * format->direct.redMask   / rgba::MAX_CC) << format->direct.red  )
-              | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green)
-              | ((color.b * format->direct.blueMask  / rgba::MAX_CC) << format->direct.blue )
-              | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha);
+      c.pixel = insert_component (color.r, format->direct.redMask  , format->direct.red  )
+              | insert_component (color.g, format->direct.greenMask, format->direct.green)
+              | insert_component (color.b, format->direct.blueMask , format->direct.blue )
+              | insert_component (alpha  , format->direct.alphaMask, format->direct.alpha);
 
       return true;
     }
@@ -684,7 +712,7 @@ rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
       d.red   = color.r;
       d.green = color.g;
       d.blue  = color.b;
-      d.alpha = color.a;
+      d.alpha = alpha;
 
       return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c);
     }
@@ -695,12 +723,9 @@ rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
 
   if (screen->visual->c_class == TrueColor)
     {
-      c.pixel = (color.r * (screen->visual->red_mask   >> ctz (screen->visual->red_mask  ))
-                         / rgba::MAX_CC) << ctz (screen->visual->red_mask  )
-              | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
-                         / rgba::MAX_CC) << ctz (screen->visual->green_mask)
-              | (color.b * (screen->visual->blue_mask  >> ctz (screen->visual->blue_mask ))
-                         / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
+      c.pixel = (color.r >> (16 - rxvt_popcount (screen->visual->red_mask  )) << rxvt_ctz (screen->visual->red_mask  ))
+              | (color.g >> (16 - rxvt_popcount (screen->visual->green_mask)) << rxvt_ctz (screen->visual->green_mask))
+              | (color.b >> (16 - rxvt_popcount (screen->visual->blue_mask )) << rxvt_ctz (screen->visual->blue_mask ));
 
       return true;
     }
@@ -722,19 +747,19 @@ rxvt_color::set (rxvt_screen *screen, const char *name)
   char eos;
   int skip;
 
+  c.a = rgba::MAX_CC;
+
   // parse the nonstandard "[alphapercent]" prefix
   if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
     {
       c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
       name += skip;
     }
-  else
-    c.a = rgba::MAX_CC;
 
   // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
   if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
     {
-      XColor xc, xc_exact;
+      XColor xc;
 
       if (XParseColor (screen->dpy, screen->cmap, name, &xc))
         {
@@ -771,7 +796,7 @@ rxvt_color::set (rxvt_screen *screen, const rgba &color)
 
       for (int i = 0; i < cmap_size; i++)
         colors [i].pixel = i;
+
       // many kilobytes transfer per colour, but pseudocolor isn't worth
       // many extra optimisations.
       XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
@@ -794,10 +819,10 @@ rxvt_color::set (rxvt_screen *screen, const rgba &color)
 
       //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n",
       //    color.r, color.g, color.b, best->red, best->green, best->blue, diff);
-          
+
       got = alloc (screen, rgba (best->red, best->green, best->blue));
 
-      delete colors;
+      delete [] colors;
     }
 #endif
 
@@ -832,7 +857,7 @@ rxvt_color::get (XColor &color)
   color.pixel = (Pixel)*this;
 }
 
-void 
+void
 rxvt_color::free (rxvt_screen *screen)
 {
   if (screen->visual->c_class == TrueColor)
@@ -862,3 +887,19 @@ rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rg
   );
 }
 
+#if TRACE_PIXMAPS
+# undef XCreatePixmap
+# undef XFreePixmap
+Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d)
+{
+  Pixmap res = XCreatePixmap (dpy, r, w, h, d);
+  fprintf (stderr, "%s:%d: XCreatePixmap (%p,%lX,%u,%u,%u) returned %lX\n", file, line, dpy, r, w, h, d, res);
+  return res;
+}
+
+void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p)
+{
+  fprintf (stderr, "%s:%d: XFreePixmap (%p,%lX)\n", file, line, dpy, p);
+  XFreePixmap (dpy,p);
+}
+#endif