[PATCH 4/5] Cache various atoms

Julien Danjou julien at danjou.info
Mon May 24 10:01:35 PDT 2010


Signed-off-by: Julien Danjou <julien at danjou.info>
---
 libsn/sn-common.c    |   61 +++++++++++++++++++++++++++++++++++++++++++++++++-
 libsn/sn-internals.h |    8 ++++++
 libsn/sn-launchee.c  |    6 ++--
 libsn/sn-xmessages.c |   19 +++------------
 libsn/sn-xmessages.h |    4 +-
 libsn/sn-xutils.c    |   12 +++------
 libsn/sn-xutils.h    |    2 +-
 7 files changed, 82 insertions(+), 30 deletions(-)

diff --git a/libsn/sn-common.c b/libsn/sn-common.c
index 4d96924..eb2235b 100644
--- a/libsn/sn-common.c
+++ b/libsn/sn-common.c
@@ -39,6 +39,8 @@ struct SnDisplay
   Display *xdisplay;
   xcb_connection_t *xconnection;
   xcb_screen_t **screens;
+  xcb_atom_t UTF8_STRING, NET_STARTUP_ID,
+    NET_STARTUP_INFO, NET_STARTUP_INFO_BEGIN;
   SnDisplayErrorTrapPush push_trap_func;
   SnDisplayErrorTrapPop  pop_trap_func;
   SnXcbDisplayErrorTrapPush xcb_push_trap_func;
@@ -108,6 +110,24 @@ sn_xcb_display_new (xcb_connection_t          *xconnection,
   SnDisplay *display;
   int i;
 
+  /* Get all atoms we will need in the future */
+  xcb_intern_atom_cookie_t atom_utf8_string_c =
+    xcb_intern_atom(xconnection, FALSE,
+                    sizeof("UTF8_STRING") - 1, "UTF8_STRING");
+
+  xcb_intern_atom_cookie_t atom_net_startup_info_begin_c =
+    xcb_intern_atom(xconnection, FALSE,
+                    sizeof("_NET_STARTUP_INFO_BEGIN") - 1,
+                    "_NET_STARTUP_INFO_BEGIN");
+
+  xcb_intern_atom_cookie_t atom_net_startup_info_c =
+    xcb_intern_atom(xconnection, FALSE,
+                    sizeof("_NET_STARTUP_INFO") - 1, "_NET_STARTUP_INFO");
+
+  xcb_intern_atom_cookie_t atom_net_startup_id_c =
+    xcb_intern_atom(xconnection, FALSE,
+                    sizeof("_NET_STARTUP_ID") - 1, "_NET_STARTUP_ID");
+
   display = sn_new0 (SnDisplay, 1);
 
   display->xconnection = xconnection;
@@ -117,10 +137,26 @@ sn_xcb_display_new (xcb_connection_t          *xconnection,
 
   display->xcb_push_trap_func = push_trap_func;
   display->xcb_pop_trap_func = pop_trap_func;
-  
+
   for (i = 0; i < display->n_screens; ++i)
     display->screens[i] = xcb_aux_get_screen(xconnection, i);
 
+  display->UTF8_STRING = xcb_intern_atom_reply(display->xconnection,
+                                               atom_utf8_string_c,
+                                               NULL)->atom;
+
+  display->NET_STARTUP_INFO_BEGIN = xcb_intern_atom_reply(display->xconnection,
+                                                          atom_net_startup_info_begin_c,
+                                                          NULL)->atom;
+
+  display->NET_STARTUP_INFO = xcb_intern_atom_reply(display->xconnection,
+                                                    atom_net_startup_info_c,
+                                                    NULL)->atom;
+
+  display->NET_STARTUP_ID = xcb_intern_atom_reply(display->xconnection,
+                                                  atom_net_startup_id_c,
+                                                  NULL)->atom;
+
   return display;
 }
 
@@ -407,3 +443,26 @@ sn_internal_display_get_xmessage_data (SnDisplay              *display,
     *pending = display->pending_messages;
 }
 
+xcb_atom_t
+sn_internal_get_utf8_string_atom(SnDisplay *display)
+{
+  return display->UTF8_STRING;
+}
+
+xcb_atom_t
+sn_internal_get_net_startup_id_atom(SnDisplay *display)
+{
+  return display->NET_STARTUP_ID;
+}
+
+xcb_atom_t
+sn_internal_get_net_startup_info_atom(SnDisplay *display)
+{
+  return display->NET_STARTUP_INFO;
+}
+
+xcb_atom_t
+sn_internal_get_net_startup_info_begin_atom(SnDisplay *display)
+{
+  return display->NET_STARTUP_INFO_BEGIN;
+}
diff --git a/libsn/sn-internals.h b/libsn/sn-internals.h
index 3b37ec3..2876415 100644
--- a/libsn/sn-internals.h
+++ b/libsn/sn-internals.h
@@ -64,6 +64,14 @@ void       sn_internal_display_get_xmessage_data (SnDisplay              *displa
                                                   SnList                **funcs,
                                                   SnList                **pending);
 
+xcb_atom_t sn_internal_get_utf8_string_atom(SnDisplay *display);
+
+xcb_atom_t sn_internal_get_net_startup_id_atom(SnDisplay *display);
+
+xcb_atom_t sn_internal_get_net_startup_info_atom(SnDisplay *display);
+
+xcb_atom_t sn_internal_get_net_startup_info_begin_atom(SnDisplay *display);
+
 /* --- From sn-monitor.c --- */
 sn_bool_t sn_internal_monitor_process_event (SnDisplay *display);
 
diff --git a/libsn/sn-launchee.c b/libsn/sn-launchee.c
index fe32338..c7aae1f 100644
--- a/libsn/sn-launchee.c
+++ b/libsn/sn-launchee.c
@@ -196,8 +196,8 @@ sn_launchee_context_complete (SnLauncheeContext *context)
 
   sn_internal_broadcast_xmessage (context->display,
                                   context->screen,
-                                  "_NET_STARTUP_INFO",
-                                  "_NET_STARTUP_INFO_BEGIN",
+                                  sn_internal_get_net_startup_info_atom(context->display),
+                                  sn_internal_get_net_startup_info_begin_atom(context->display),
                                   message);
 
   sn_free (message);
@@ -221,6 +221,6 @@ sn_launchee_context_setup_window (SnLauncheeContext *context,
 {
   sn_internal_set_utf8_string (context->display,
                                xwindow,
-                               "_NET_STARTUP_ID",
+                               sn_internal_get_net_startup_id_atom(context->display),
                                context->startup_id);
 }
diff --git a/libsn/sn-xmessages.c b/libsn/sn-xmessages.c
index b1be972..59236e9 100644
--- a/libsn/sn-xmessages.c
+++ b/libsn/sn-xmessages.c
@@ -154,8 +154,8 @@ sn_internal_remove_xmessage_func (SnDisplay      *display,
 void
 sn_internal_broadcast_xmessage   (SnDisplay      *display,
                                   int             screen,
-                                  const char     *message_type,
-                                  const char     *message_type_begin,
+                                  xcb_atom_t      message_type,
+                                  xcb_atom_t      message_type_begin,
                                   const char     *message)
 {
   if (!sn_internal_utf8_validate (message, -1))
@@ -168,12 +168,6 @@ sn_internal_broadcast_xmessage   (SnDisplay      *display,
   
   xcb_connection_t *xconnection = sn_display_get_x_connection (display);
 
-  /* Send atom requests ASAP */
-  xcb_intern_atom_cookie_t message_type_c =
-    xcb_intern_atom(xconnection, FALSE, strlen(message_type), message_type);
-  xcb_intern_atom_cookie_t message_type_begin_c =
-    xcb_intern_atom(xconnection, FALSE, strlen(message_type_begin), message_type_begin);
-
   uint32_t attrs[] = { 1, XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY };
   xcb_screen_t *s = sn_internal_display_get_x_screen (display, screen);
 
@@ -193,16 +187,11 @@ sn_internal_broadcast_xmessage   (SnDisplay      *display,
       xevent.response_type = XCB_CLIENT_MESSAGE;
       xevent.window = xwindow;
       xevent.format = 8;
+      xevent.type = message_type_begin;
 
       src = message;
       src_end = message + strlen (message) + 1; /* +1 to include nul byte */
 
-      /* Get atom reply last */
-      xcb_atom_t type_atom = xcb_intern_atom_reply(xconnection,
-                                                   message_type_c, NULL)->atom;
-      xevent.type = xcb_intern_atom_reply(xconnection,
-                                          message_type_begin_c, NULL)->atom;
-
       while (src != src_end)
       {
           dest = &xevent.data.data8[0];
@@ -219,7 +208,7 @@ sn_internal_broadcast_xmessage   (SnDisplay      *display,
           xcb_send_event (xconnection, 0, s->root, XCB_EVENT_MASK_PROPERTY_CHANGE,
                           (char *) &xevent);
 
-          xevent.type = type_atom;
+          xevent.type = message_type;
       }
   }
 
diff --git a/libsn/sn-xmessages.h b/libsn/sn-xmessages.h
index c2174e6..24c61b9 100644
--- a/libsn/sn-xmessages.h
+++ b/libsn/sn-xmessages.h
@@ -48,8 +48,8 @@ void sn_internal_remove_xmessage_func (SnDisplay      *display,
                                        void           *func_data);
 void sn_internal_broadcast_xmessage   (SnDisplay      *display,
                                        int             screen,
-                                       const char     *message_type,
-                                       const char     *message_type_begin,
+                                       xcb_atom_t      message_type,
+                                       xcb_atom_t      message_type_begin,
                                        const char     *message);
 
 char*     sn_internal_serialize_message   (const char   *prefix,
diff --git a/libsn/sn-xutils.c b/libsn/sn-xutils.c
index 03ed74c..60c9ae1 100644
--- a/libsn/sn-xutils.c
+++ b/libsn/sn-xutils.c
@@ -30,23 +30,19 @@
 void
 sn_internal_set_utf8_string (SnDisplay  *display,
                              xcb_window_t xwindow,
-                             const char *property,
+                             xcb_atom_t  property,
                              const char *str)
 {
   sn_display_error_trap_push (display);
 
   xcb_connection_t *c = sn_display_get_x_connection (display);
-
-  xcb_intern_atom_cookie_t atom_property_c =
-    xcb_intern_atom(c, FALSE, strlen(property), property);
-  xcb_intern_atom_cookie_t atom_utf8_string_c =
-    xcb_intern_atom(c, FALSE, sizeof("UTF8_STRING") - 1, "UTF8_STRING");
+  xcb_atom_t UTF8_STRING = sn_internal_get_utf8_string_atom(display);
 
   xcb_change_property (c,
                        XCB_PROP_MODE_REPLACE,
                        xwindow,
-                       xcb_intern_atom_reply(c, atom_property_c, NULL)->atom,
-                       xcb_intern_atom_reply(c, atom_utf8_string_c, NULL)->atom,
+                       property,
+                       UTF8_STRING,
                        8, strlen (str), str);
 
   sn_display_error_trap_pop (display);
diff --git a/libsn/sn-xutils.h b/libsn/sn-xutils.h
index ce477f9..4ae0775 100644
--- a/libsn/sn-xutils.h
+++ b/libsn/sn-xutils.h
@@ -31,7 +31,7 @@ SN_BEGIN_DECLS
 
 void sn_internal_set_utf8_string (SnDisplay  *display,
                                   xcb_window_t xwindow,
-                                  const char *property,
+                                  xcb_atom_t   property,
                                   const char *str);
 
 
-- 
1.7.1



More information about the xdg mailing list