Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_x / xcb / ecore_xcb_atoms.c
1 #include "ecore_xcb_private.h"
2 #include "ecore_x_atoms_decl.h"
3
4 /* NB: Increment if you add new atoms */
5 #define ECORE_X_ATOMS_COUNT 199
6
7 /* local function prototypes */
8
9 /* local variables */
10 static xcb_intern_atom_cookie_t cookies[ECORE_X_ATOMS_COUNT];
11
12 void
13 _ecore_xcb_atoms_init(void)
14 {
15    int i = 0, num = 0;
16
17    LOGFN(__FILE__, __LINE__, __FUNCTION__);
18    CHECK_XCB_CONN;
19
20    num = (sizeof(atom_items) / sizeof(Atom_Item));
21    for (i = 0; i < num; i++)
22      {
23         cookies[i] =
24           xcb_intern_atom_unchecked(_ecore_xcb_conn, 0,
25                                     strlen(atom_items[i].name), atom_items[i].name);
26      }
27 }
28
29 void
30 _ecore_xcb_atoms_finalize(void)
31 {
32    int i = 0, num = 0;
33
34    LOGFN(__FILE__, __LINE__, __FUNCTION__);
35    CHECK_XCB_CONN;
36
37    num = (sizeof(atom_items) / sizeof(Atom_Item));
38    for (i = 0; i < num; i++)
39      {
40         xcb_intern_atom_reply_t *reply = NULL;
41
42         if (!(reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookies[i], 0)))
43           continue;
44         *(atom_items[i].atom) = reply->atom;
45         free(reply);
46      }
47 }
48
49 /**
50  * @defgroup Ecore_X_Atom_Group XCB Atom Functions
51  *
52  * Functions that operate on atoms
53  */
54
55 /**
56  * Retrieves the atom value associated to a name.
57  *
58  * @param  name Unused.
59  * @return      Associated atom value.
60  *
61  * Retrieves the atom value associated to a name. The reply is the
62  * returned value of the function ecore_xcb_intern_atom_reply(). If
63  * @p reply is @c NULL, the NULL atom is returned. Otherwise, the atom
64  * associated to the name is returned.
65  *
66  * @ingroup Ecore_X_Atom_Group
67  */
68 EAPI Ecore_X_Atom
69 ecore_x_atom_get(const char *name)
70 {
71    xcb_intern_atom_cookie_t cookie;
72    xcb_intern_atom_reply_t *reply;
73    Ecore_X_Atom a;
74
75    LOGFN(__FILE__, __LINE__, __FUNCTION__);
76    CHECK_XCB_CONN;
77
78    cookie = xcb_intern_atom_unchecked(_ecore_xcb_conn, 0, strlen(name), name);
79    reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookie, NULL);
80    if (!reply) return XCB_ATOM_NONE;
81    a = reply->atom;
82    free(reply);
83    return a;
84 }
85
86 /**
87  * Retrieves the name of the given atom.
88  *
89  * @param  atom
90  * @return      The name of the atom.
91  *
92  * @ingroup Ecore_X_Atom_Group
93  */
94 EAPI char *
95 ecore_x_atom_name_get(Ecore_X_Atom atom)
96 {
97    xcb_get_atom_name_cookie_t cookie;
98    xcb_get_atom_name_reply_t *reply;
99    char *name;
100    int len = 0;
101
102    LOGFN(__FILE__, __LINE__, __FUNCTION__);
103    CHECK_XCB_CONN;
104
105    cookie = xcb_get_atom_name_unchecked(_ecore_xcb_conn, atom);
106    reply = xcb_get_atom_name_reply(_ecore_xcb_conn, cookie, NULL);
107    if (!reply) return NULL;
108    len = xcb_get_atom_name_name_length(reply);
109    name = (char *)malloc(sizeof(char) * (len + 1));
110    if (!name)
111      {
112         free(reply);
113         return NULL;
114      }
115    memcpy(name, xcb_get_atom_name_name(reply), len);
116    name[len] = '\0';
117
118    free(reply);
119    return name;
120 }
121
122 EAPI void
123 ecore_x_atoms_get(const char  **names,
124                   int           num,
125                   Ecore_X_Atom *atoms)
126 {
127    xcb_intern_atom_cookie_t cookies[num];
128    int i = 0;
129
130    LOGFN(__FILE__, __LINE__, __FUNCTION__);
131    CHECK_XCB_CONN;
132
133    for (i = 0; i < num; i++)
134      {
135         cookies[i] =
136           xcb_intern_atom_unchecked(_ecore_xcb_conn, 0,
137                                     strlen(names[i]), names[i]);
138      }
139    for (i = 0; i < num; i++)
140      {
141         xcb_intern_atom_reply_t *reply = NULL;
142
143         if (!(reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookies[i], 0)))
144           continue;
145         atoms[i] = reply->atom;
146         free(reply);
147      }
148 }
149