eolian: rename is_ref API to is_ptr to match syntax
[platform/upstream/efl.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 #define MYMIN(X, Y) (((X) < (Y)) ? (X) : (Y))
13
14 void
15 _ecore_xcb_atoms_init(void)
16 {
17    int i = 0, num = 0;
18
19    LOGFN(__FILE__, __LINE__, __FUNCTION__);
20    CHECK_XCB_CONN;
21
22    num = (sizeof(atom_items) / sizeof(Atom_Item));
23    num = MYMIN(num, ECORE_X_ATOMS_COUNT);
24    for (i = 0; i < num; i++)
25      {
26         cookies[i] =
27           xcb_intern_atom_unchecked(_ecore_xcb_conn, 0,
28                                     strlen(atom_items[i].name), atom_items[i].name);
29      }
30 }
31
32 void
33 _ecore_xcb_atoms_finalize(void)
34 {
35    int i = 0, num = 0;
36
37    LOGFN(__FILE__, __LINE__, __FUNCTION__);
38    CHECK_XCB_CONN;
39
40    num = (sizeof(atom_items) / sizeof(Atom_Item));
41    num = MYMIN(num, ECORE_X_ATOMS_COUNT);
42    for (i = 0; i < num; i++)
43      {
44         xcb_intern_atom_reply_t *reply = NULL;
45
46         if (!(reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookies[i], 0)))
47           continue;
48         *(atom_items[i].atom) = reply->atom;
49         free(reply);
50      }
51 }
52
53 /**
54  * @defgroup Ecore_X_Atom_Group X Atom Functions
55  * @ingroup Ecore_X_Group
56  *
57  * Functions that operate on atoms
58  */
59
60 /**
61  * Retrieves the atom value associated to a name.
62  *
63  * @param  name Unused.
64  * @return      Associated atom value.
65  *
66  * Retrieves the atom value associated to a name. The reply is the
67  * returned value of the function ecore_xcb_intern_atom_reply(). If
68  * @p reply is @c NULL, the NULL atom is returned. Otherwise, the atom
69  * associated to the name is returned.
70  *
71  * @ingroup Ecore_X_Atom_Group
72  */
73 EAPI Ecore_X_Atom
74 ecore_x_atom_get(const char *name)
75 {
76    xcb_intern_atom_cookie_t cookie;
77    xcb_intern_atom_reply_t *reply;
78    Ecore_X_Atom a;
79
80    LOGFN(__FILE__, __LINE__, __FUNCTION__);
81    CHECK_XCB_CONN;
82
83    cookie = xcb_intern_atom_unchecked(_ecore_xcb_conn, 0, strlen(name), name);
84    reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookie, NULL);
85    if (!reply) return XCB_ATOM_NONE;
86    a = reply->atom;
87    free(reply);
88    return a;
89 }
90
91 /**
92  * Retrieves the name of the given atom.
93  *
94  * @param  atom
95  * @return      The name of the atom.
96  *
97  * @ingroup Ecore_X_Atom_Group
98  */
99 EAPI char *
100 ecore_x_atom_name_get(Ecore_X_Atom atom)
101 {
102    xcb_get_atom_name_cookie_t cookie;
103    xcb_get_atom_name_reply_t *reply;
104    char *name;
105    int len = 0;
106
107    LOGFN(__FILE__, __LINE__, __FUNCTION__);
108    CHECK_XCB_CONN;
109
110    cookie = xcb_get_atom_name_unchecked(_ecore_xcb_conn, atom);
111    reply = xcb_get_atom_name_reply(_ecore_xcb_conn, cookie, NULL);
112    if (!reply) return NULL;
113    len = xcb_get_atom_name_name_length(reply);
114    name = (char *)malloc(sizeof(char) * (len + 1));
115    if (!name)
116      {
117         free(reply);
118         return NULL;
119      }
120    memcpy(name, xcb_get_atom_name_name(reply), len);
121    name[len] = '\0';
122
123    free(reply);
124    return name;
125 }
126
127 EAPI void
128 ecore_x_atoms_get(const char  **names,
129                   int           num,
130                   Ecore_X_Atom *atoms)
131 {
132    xcb_intern_atom_cookie_t cookies[num];
133    int i = 0;
134
135    LOGFN(__FILE__, __LINE__, __FUNCTION__);
136    CHECK_XCB_CONN;
137
138    num = MYMIN(num, ECORE_X_ATOMS_COUNT);
139    for (i = 0; i < num; i++)
140      {
141         cookies[i] =
142           xcb_intern_atom_unchecked(_ecore_xcb_conn, 0,
143                                     strlen(names[i]), names[i]);
144      }
145    for (i = 0; i < num; i++)
146      {
147         xcb_intern_atom_reply_t *reply = NULL;
148
149         if (!(reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookies[i], 0)))
150           continue;
151         atoms[i] = reply->atom;
152         free(reply);
153      }
154 }
155