set _NET_WM_USER_TIME on the window
[dana/urxvt.git] / src / rxvttoolkit.C
index 666b0e2..c52c954 100644 (file)
@@ -61,6 +61,8 @@ 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",
@@ -298,6 +300,7 @@ rxvt_screen::clear ()
 rxvt_display::rxvt_display (const char *id)
 : refcounted (id)
 , 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);
@@ -427,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
@@ -464,7 +467,7 @@ bool rxvt_display::ref_init ()
 
   int fd = XConnectionNumber (dpy);
 
-  // try to detect wether we have a local connection.
+  // 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;
@@ -488,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));
@@ -503,6 +506,7 @@ rxvt_display::~rxvt_display ()
   XFreeCursor (dpy, blank_cursor);
 #endif
   x_ev.stop ();
+  flush_ev.stop ();
 #ifdef USE_XIM
   xims.clear ();
 #endif
@@ -544,43 +548,42 @@ void rxvt_display::im_change_check ()
 
 void rxvt_display::x_cb (ev::io &w, int revents)
 {
-  while (XEventsQueued (dpy, QueuedAfterReading))
-    {
-      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
-          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);
-            }
+            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));
 
-void rxvt_display::flush ()
-{
-  XFlush (dpy);
-}
-
-void rxvt_display::flush_cb (ev::prepare &w, int revents)
-{
-  flush ();
+  w.stop ();
 }
 
 void rxvt_display::reg (xevent_watcher *w)
@@ -601,12 +604,18 @@ void rxvt_display::unreg (xevent_watcher *w)
     }
 }
 
-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
@@ -629,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';
@@ -637,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;
 }
 
@@ -673,6 +680,9 @@ insert_component (unsigned int value, unsigned int mask, unsigned int 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;
 
@@ -686,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 = 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 (color.a, format->direct.alphaMask, format->direct.alpha);
+              | insert_component (alpha  , format->direct.alphaMask, format->direct.alpha);
 
       return true;
     }
@@ -702,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);
     }
@@ -713,9 +723,9 @@ rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
 
   if (screen->visual->c_class == TrueColor)
     {
-      c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask  )) << ctz (screen->visual->red_mask  ))
-              | (color.g >> (16 - popcount (screen->visual->green_mask)) << ctz (screen->visual->green_mask))
-              | (color.b >> (16 - popcount (screen->visual->blue_mask )) << 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;
     }
@@ -737,29 +747,17 @@ 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 "#aarrggbb" format
-  if (name[0] == '#' && strlen (name) == 1+2+2+2+2 && 4 == sscanf (name+1, "%2hx%2hx%2hx%2hx%c", &c.a, &c.r, &c.g, &c.b, &eos))
-    {
-      if (c.r)
-        c.r = (c.r << 8) | 0x0ff;
-      if (c.g)
-        c.g = (c.g << 8) | 0x0ff;
-      if (c.b)
-        c.b = (c.b << 8) | 0x0ff;
-      if (c.a)
-        c.a = (c.a << 8) | 0x0ff;
-    }
   // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
-  else if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
+  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;