2 * ion/ioncore/property.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
20 static ulong xwindow_get_property_(Window win, Atom atom, Atom type,
21 ulong n32expected, bool more, uchar **p,
29 status=XGetWindowProperty(ioncore_g.dpy, win, atom, 0L, n32expected,
30 False, type, &real_type, format, &n,
33 if(status!=Success || *p==NULL)
40 n32expected+=(extra+4)/4;
54 ulong xwindow_get_property(Window win, Atom atom, Atom type,
55 ulong n32expected, bool more, uchar **p)
58 return xwindow_get_property_(win, atom, type, n32expected, more, p,
66 /*{{{ String property stuff */
69 char *xwindow_get_string_property(Window win, Atom a, int *nret)
74 n=xwindow_get_property(win, a, XA_STRING, 64L, TRUE, (uchar**)&p);
79 return (n<=0 ? NULL : p);
83 void xwindow_set_string_property(Window win, Atom a, const char *value)
86 XDeleteProperty(ioncore_g.dpy, win, a);
88 XChangeProperty(ioncore_g.dpy, win, a, XA_STRING,
89 8, PropModeReplace, (uchar*)value, strlen(value));
97 /*{{{ Integer property stuff */
100 bool xwindow_get_integer_property(Window win, Atom a, int *vret)
105 n=xwindow_get_property(win, a, XA_INTEGER, 1L, FALSE, (uchar**)&p);
117 void xwindow_set_integer_property(Window win, Atom a, int value)
123 XChangeProperty(ioncore_g.dpy, win, a, XA_INTEGER,
124 32, PropModeReplace, (uchar*)data, 1);
131 bool xwindow_get_state_property(Window win, int *state)
135 if(xwindow_get_property(win, ioncore_g.atom_wm_state,
136 ioncore_g.atom_wm_state,
137 2L, FALSE, (uchar**)&p)<=0)
148 void xwindow_set_state_property(Window win, int state)
155 XChangeProperty(ioncore_g.dpy, win,
156 ioncore_g.atom_wm_state, ioncore_g.atom_wm_state,
157 32, PropModeReplace, (uchar*)data, 2);
164 /*{{{ Text property stuff */
167 char **xwindow_get_text_property(Window win, Atom a, int *nret)
174 st=XGetTextProperty(ioncore_g.dpy, win, &prop, a);
177 *nret=(!st ? 0 : -1);
182 #ifdef CF_XFREE86_TEXTPROP_BUG_WORKAROUND
183 while(prop.nitems>0){
184 if(prop.value[prop.nitems-1]=='\0')
191 if(!ioncore_g.use_mb){
192 st=XTextPropertyToStringList(&prop, &list, &n);
194 st=XmbTextPropertyToTextList(ioncore_g.dpy, &prop, &list, &n);
200 if(!st || n==0 || list==NULL)
210 void xwindow_set_text_property(Window win, Atom a, const char **ptr, int n)
215 if(!ioncore_g.use_mb){
216 st=XStringListToTextProperty((char **)&ptr, n, &prop);
218 st=XmbTextListToTextProperty(ioncore_g.dpy, (char **)ptr, n,
226 XSetTextProperty(ioncore_g.dpy, win, &prop, a);
238 * Create a new atom. See \code{XInternAtom}(3) manual page for details.
241 int ioncore_x_intern_atom(const char *name, bool only_if_exists)
243 return XInternAtom(ioncore_g.dpy, name, only_if_exists);
248 * Get the name of an atom. See \code{XGetAtomName}(3) manual page for
252 char *ioncore_x_get_atom_name(int atom)
254 return XGetAtomName(ioncore_g.dpy, atom);
261 for(i=0; i<n; i++) extl_table_seti_i(tab, i+1, d[i]); \
266 * Get a property \var{atom} of type \var{atom_type} for window \var{win}.
267 * The \var{n32expected} parameter indicates the expected number of 32bit
268 * words, and \var{more} indicates whether all or just this amount of data
269 * should be fetched. Each 8, 16 or 32bit element of the property, as
270 * deciphered from \var{atom_type} is a field in the returned table.
271 * See \code{XGetWindowProperty}(3) manual page for more information.
275 ExtlTab ioncore_x_get_window_property(int win, int atom, int atom_type,
276 int n32expected, bool more)
283 n=xwindow_get_property_(win, atom, atom_type, n32expected, more, &p,
287 return extl_table_none();
289 if(n<=0 || (format!=8 && format!=16 && format!=32)){
291 return extl_table_none();
294 tab=extl_create_table();
297 case 8: CP(char); break;
298 case 16: CP(short); break;
299 case 32: CP(long); break;
308 TYPE *d=ALLOC_N(TYPE, n); \
309 if(d==NULL) return; \
310 for(i=0; i<n; i++) { \
311 if(!extl_table_geti_i(tab, i+1, &tmp)) return; \
318 static bool get_mode(const char *mode, int *m)
320 if(strcmp(mode, "replace")==0)
322 else if(strcmp(mode, "prepend")==0)
324 else if(strcmp(mode, "append")==0)
334 * Modify a window property. The \var{mode} is one of
335 * \codestr{replace}, \codestr{prepend} or \codestr{append}, and format
336 * is either 8, 16 or 32. Also see \fnref{ioncore.x_get_window_property}
337 * and the \code{XChangeProperty}(3) manual page.
340 void ioncore_x_change_property(int win, int atom, int atom_type,
341 int format, const char *mode, ExtlTab tab)
343 int tmp, m, i, n=extl_table_get_n(tab);
346 if(n<0 || !get_mode(mode, &m)){
347 warn(TR("Invalid arguments."));
352 case 8: GET(char); break;
353 case 16: GET(short); break;
354 case 32: GET(long); break;
356 warn(TR("Invalid arguments."));
360 XChangeProperty(ioncore_g.dpy, win, atom, atom_type, format, m, p, n);
367 * Delete a window property.
370 void ioncore_x_delete_property(int win, int atom)
372 XDeleteProperty(ioncore_g.dpy, win, atom);
377 * Get a text property for a window. The fields in the returned
378 * table (starting from 1) are the null-separated parts of the property.
379 * See the \code{XGetTextProperty}(3) manual page for more information.
383 ExtlTab ioncore_x_get_text_property(int win, int atom)
387 ExtlTab tab=extl_table_none();
389 list=xwindow_get_text_property(win, atom, &n);
393 tab=extl_create_table();
395 extl_table_seti_s(tab, i+1, list[i]);
397 XFreeStringList(list);
405 * Set a text property for a window. The fields of \var{tab} starting from
406 * 1 should be the different null-separated parts of the property.
407 * See the \code{XSetTextProperty}(3) manual page for more information.
410 void ioncore_x_set_text_property(int win, int atom, ExtlTab tab)
413 int i, n=extl_table_get_n(tab);
415 list=ALLOC_N(char*, n);
422 extl_table_geti_s(tab, i+1, &(list[i]));
425 xwindow_set_text_property(win, atom, (const char **)list, n);
427 XFreeStringList(list);