summaryrefslogtreecommitdiff
path: root/src/lib/ecore_x/xcb/ecore_xcb_atoms.c
blob: 3b4da1d5bc24d97f46818106241b8ee151403948 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "ecore_xcb_private.h"
#include "ecore_x_atoms_decl.h"

/* NB: Increment if you add new atoms */
#define ECORE_X_ATOMS_COUNT 199

/* local function prototypes */

/* local variables */
static xcb_intern_atom_cookie_t cookies[ECORE_X_ATOMS_COUNT];

void
_ecore_xcb_atoms_init(void)
{
   int i = 0, num = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   num = (sizeof(atom_items) / sizeof(Atom_Item));
   for (i = 0; i < num; i++)
     {
        cookies[i] =
          xcb_intern_atom_unchecked(_ecore_xcb_conn, 0,
                                    strlen(atom_items[i].name), atom_items[i].name);
     }
}

void
_ecore_xcb_atoms_finalize(void)
{
   int i = 0, num = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   num = (sizeof(atom_items) / sizeof(Atom_Item));
   for (i = 0; i < num; i++)
     {
        xcb_intern_atom_reply_t *reply = NULL;

        if (!(reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookies[i], 0)))
          continue;
        *(atom_items[i].atom) = reply->atom;
        free(reply);
     }
}

/**
 * @defgroup Ecore_X_Atom_Group X Atom Functions
 * @ingroup Ecore_X_Group
 *
 * Functions that operate on atoms
 */

/**
 * Retrieves the atom value associated to a name.
 *
 * @param  name Unused.
 * @return      Associated atom value.
 *
 * Retrieves the atom value associated to a name. The reply is the
 * returned value of the function ecore_xcb_intern_atom_reply(). If
 * @p reply is @c NULL, the NULL atom is returned. Otherwise, the atom
 * associated to the name is returned.
 *
 * @ingroup Ecore_X_Atom_Group
 */
EAPI Ecore_X_Atom
ecore_x_atom_get(const char *name)
{
   xcb_intern_atom_cookie_t cookie;
   xcb_intern_atom_reply_t *reply;
   Ecore_X_Atom a;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   cookie = xcb_intern_atom_unchecked(_ecore_xcb_conn, 0, strlen(name), name);
   reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return XCB_ATOM_NONE;
   a = reply->atom;
   free(reply);
   return a;
}

/**
 * Retrieves the name of the given atom.
 *
 * @param  atom
 * @return      The name of the atom.
 *
 * @ingroup Ecore_X_Atom_Group
 */
EAPI char *
ecore_x_atom_name_get(Ecore_X_Atom atom)
{
   xcb_get_atom_name_cookie_t cookie;
   xcb_get_atom_name_reply_t *reply;
   char *name;
   int len = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   cookie = xcb_get_atom_name_unchecked(_ecore_xcb_conn, atom);
   reply = xcb_get_atom_name_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return NULL;
   len = xcb_get_atom_name_name_length(reply);
   name = (char *)malloc(sizeof(char) * (len + 1));
   if (!name)
     {
        free(reply);
        return NULL;
     }
   memcpy(name, xcb_get_atom_name_name(reply), len);
   name[len] = '\0';

   free(reply);
   return name;
}

EAPI void
ecore_x_atoms_get(const char  **names,
                  int           num,
                  Ecore_X_Atom *atoms)
{
   xcb_intern_atom_cookie_t cookies[num];
   int i = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   for (i = 0; i < num; i++)
     {
        cookies[i] =
          xcb_intern_atom_unchecked(_ecore_xcb_conn, 0,
                                    strlen(names[i]), names[i]);
     }
   for (i = 0; i < num; i++)
     {
        xcb_intern_atom_reply_t *reply = NULL;

        if (!(reply = xcb_intern_atom_reply(_ecore_xcb_conn, cookies[i], 0)))
          continue;
        atoms[i] = reply->atom;
        free(reply);
     }
}