]> Dogcows Code - chaz/openbox/blob - cwmcc/prop.c
watch out for not loading the timed_menu plugin
[chaz/openbox] / cwmcc / prop.c
1 #include "cwmcc_internal.h"
2 #include "atom.h"
3
4 #include <X11/Xutil.h>
5 #include <glib.h>
6 #include <string.h>
7
8 /* this just isn't used...
9 static gboolean get(Window win, Atom prop, Atom type, int size,
10 guchar **data, gulong num)
11 {
12 gboolean ret = FALSE;
13 int res;
14 guchar *xdata = NULL;
15 Atom ret_type;
16 int ret_size;
17 gulong ret_items, bytes_left;
18 long num32 = 32 / size * num; /\* num in 32-bit elements *\/
19
20 res = XGetWindowProperty(cwmcc_display, win, prop, 0l, num32,
21 FALSE, type, &ret_type, &ret_size,
22 &ret_items, &bytes_left, &xdata);
23 if (res == Success && ret_items && xdata) {
24 if (ret_size == size && ret_items >= num) {
25 *data = g_memdup(xdata, num * (size / 8));
26 ret = TRUE;
27 }
28 XFree(xdata);
29 }
30 return ret;
31 }
32 */
33
34 static gboolean get_prealloc(Window win, Atom prop, Atom type, int size,
35 guchar *data, gulong num)
36 {
37 gboolean ret = FALSE;
38 int res;
39 guchar *xdata = NULL;
40 Atom ret_type;
41 int ret_size;
42 gulong ret_items, bytes_left;
43 long num32 = 32 / size * num; /* num in 32-bit elements */
44
45 res = XGetWindowProperty(cwmcc_display, win, prop, 0l, num32,
46 FALSE, type, &ret_type, &ret_size,
47 &ret_items, &bytes_left, &xdata);
48 if (res == Success && ret_items && xdata) {
49 if (ret_size == size && ret_items >= num) {
50 gulong i;
51 for (i = 0; i < num; ++i)
52 switch (size) {
53 case 8:
54 data[i] = xdata[i];
55 break;
56 case 16:
57 ((guint16*)data)[i] = ((guint16*)xdata)[i];
58 break;
59 case 32:
60 ((guint32*)data)[i] = ((guint32*)xdata)[i];
61 break;
62 default:
63 g_assert_not_reached(); /* unhandled size */
64 }
65 ret = TRUE;
66 }
67 XFree(xdata);
68 }
69 return ret;
70 }
71
72 static gboolean get_all(Window win, Atom prop, Atom type, int size,
73 guchar **data, gulong *num)
74 {
75 gboolean ret = FALSE;
76 int res;
77 guchar *xdata = NULL;
78 Atom ret_type;
79 int ret_size;
80 gulong ret_items, bytes_left;
81
82 res = XGetWindowProperty(cwmcc_display, win, prop, 0l, G_MAXLONG,
83 FALSE, type, &ret_type, &ret_size,
84 &ret_items, &bytes_left, &xdata);
85 if (res == Success) {
86 if (ret_size == size && ret_items > 0) {
87 *data = g_memdup(xdata, ret_items * (size / 8));
88 *num = ret_items;
89 ret = TRUE;
90 }
91 XFree(xdata);
92 }
93 return ret;
94 }
95
96 static gboolean get_stringlist(Window win, Atom prop, char ***list, int *nstr)
97 {
98 XTextProperty tprop;
99 gboolean ret = FALSE;
100
101 if (XGetTextProperty(cwmcc_display, win, &tprop, prop) && tprop.nitems) {
102 if (XTextPropertyToStringList(&tprop, list, nstr))
103 ret = TRUE;
104 XFree(tprop.value);
105 }
106 return ret;
107 }
108
109 gboolean cwmcc_prop_get32(Window win, Atom prop, Atom type, gulong *ret)
110 {
111 return get_prealloc(win, prop, type, 32, (guchar*)ret, 1);
112 }
113
114 gboolean cwmcc_prop_get_array32(Window win, Atom prop, Atom type, gulong **ret,
115 gulong *nret)
116 {
117 return get_all(win, prop, type, 32, (guchar**)ret, nret);
118 }
119
120 gboolean cwmcc_prop_get_string_locale(Window win, Atom prop, char **data)
121 {
122 char **list;
123 int nstr;
124
125 if (get_stringlist(win, prop, &list, &nstr) && nstr) {
126 *data = g_locale_to_utf8(list[0], -1, NULL, NULL, NULL);
127 XFreeStringList(list);
128 if (data) return TRUE;
129 }
130 return FALSE;
131 }
132
133 gboolean cwmcc_prop_get_string_utf8(Window win, Atom prop, char **ret)
134 {
135 char *raw;
136 gulong num;
137
138 if (get_all(win, prop, CWMCC_ATOM(type, utf8), 8, (guchar**)&raw, &num)) {
139 *ret = g_strdup(raw); /* grab the first string from the list */
140 g_free(raw);
141 return TRUE;
142 }
143 return FALSE;
144 }
145
146 gboolean cwmcc_prop_get_strings_utf8(Window win, Atom prop, char ***ret)
147 {
148 char *raw, *p;
149 gulong num, i;
150
151 if (get_all(win, prop, CWMCC_ATOM(type, utf8), 8, (guchar**)&raw, &num)) {
152 *ret = g_new(char*, num + 1);
153 (*ret)[num] = NULL; /* null terminated list */
154
155 p = raw;
156 for (i = 0; i < num; ++i) {
157 (*ret)[i] = g_strdup(p);
158 p = strchr(p, '\0');
159 }
160 g_free(raw);
161 return TRUE;
162 }
163 return FALSE;
164 }
165
166 gboolean cwmcc_prop_get_strings_locale(Window win, Atom prop, char ***ret)
167 {
168 char *raw, *p;
169 gulong num, i;
170
171 if (get_all(win, prop, CWMCC_ATOM(type, string), 8, (guchar**)&raw, &num)){
172 *ret = g_new(char*, num + 1);
173 (*ret)[num] = NULL; /* null terminated list */
174
175 p = raw;
176 for (i = 0; i < num; ++i) {
177 (*ret)[i] = g_locale_to_utf8(p, -1, NULL, NULL, NULL);
178 /* make sure translation did not fail */
179 if (!(*ret)[i]) {
180 g_strfreev(*ret); /* free what we did so far */
181 break; /* the force is not strong with us */
182 }
183 p = strchr(p, '\0');
184 }
185 g_free(raw);
186 if (i == num)
187 return TRUE;
188 }
189 return FALSE;
190 }
191
192 void cwmcc_prop_set32(Window win, Atom prop, Atom type, gulong val)
193 {
194 XChangeProperty(cwmcc_display, win, prop, type, 32, PropModeReplace,
195 (guchar*)&val, 1);
196 }
197
198 void cwmcc_prop_set_array32(Window win, Atom prop, Atom type,
199 gulong *val, gulong num)
200 {
201 XChangeProperty(cwmcc_display, win, prop, type, 32, PropModeReplace,
202 (guchar*)val, num);
203 }
204
205 void cwmcc_prop_set_string_utf8(Window win, Atom prop, char *val)
206 {
207 XChangeProperty(cwmcc_display, win, prop, CWMCC_ATOM(type, utf8), 8,
208 PropModeReplace, (guchar*)val, strlen(val));
209 }
210
211 void cwmcc_prop_set_strings_utf8(Window win, Atom prop, char **strs)
212 {
213 GString *str;
214 guint i;
215
216 str = g_string_sized_new(0);
217 for (i = 0; strs[i]; ++i) {
218 str = g_string_append(str, strs[i]);
219 str = g_string_append_c(str, '\0');
220 }
221 XChangeProperty(cwmcc_display, win, prop, CWMCC_ATOM(type, utf8), 8,
222 PropModeReplace, (guchar*)str->str, str->len);
223 }
224
225 void cwmcc_prop_erase(Window win, Atom prop)
226 {
227 XDeleteProperty(cwmcc_display, win, prop);
228 }
229
This page took 0.042015 seconds and 4 git commands to generate.