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)
175 st=XGetTextProperty(ioncore_g.dpy, win, &prop, a);
178 *nret=(!st ? 0 : -1);
183 #ifdef CF_XFREE86_TEXTPROP_BUG_WORKAROUND
184 while(prop.nitems>0){
185 if(prop.value[prop.nitems-1]=='\0')
192 if(!ioncore_g.use_mb){
193 Status st=XTextPropertyToStringList(&prop, &list, &n);
196 int st=XmbTextPropertyToTextList(ioncore_g.dpy, &prop, &list, &n);
202 if(!ok || n==0 || list==NULL)
212 void xwindow_set_text_property(Window win, Atom a, const char **ptr, int n)
217 if(!ioncore_g.use_mb){
218 Status st=XStringListToTextProperty((char **)ptr, n, &prop);
221 int st=XmbTextListToTextProperty(ioncore_g.dpy, (char **)ptr, n,
229 XSetTextProperty(ioncore_g.dpy, win, &prop, a);
241 * Create a new atom. See \code{XInternAtom}(3) manual page for details.
244 int ioncore_x_intern_atom(const char *name, bool only_if_exists)
246 return XInternAtom(ioncore_g.dpy, name, only_if_exists);
251 * Get the name of an atom. See \code{XGetAtomName}(3) manual page for
255 char *ioncore_x_get_atom_name(int atom)
257 return XGetAtomName(ioncore_g.dpy, atom);
264 for(i=0; i<n; i++) extl_table_seti_i(tab, i+1, d[i]); \
269 * Get a property \var{atom} of type \var{atom_type} for window \var{win}.
270 * The \var{n32expected} parameter indicates the expected number of 32bit
271 * words, and \var{more} indicates whether all or just this amount of data
272 * should be fetched. Each 8, 16 or 32bit element of the property, as
273 * deciphered from \var{atom_type} is a field in the returned table.
274 * See \code{XGetWindowProperty}(3) manual page for more information.
278 ExtlTab ioncore_x_get_window_property(int win, int atom, int atom_type,
279 int n32expected, bool more)
286 n=xwindow_get_property_(win, atom, atom_type, n32expected, more, &p,
290 return extl_table_none();
292 if(n<=0 || (format!=8 && format!=16 && format!=32)){
294 return extl_table_none();
297 tab=extl_create_table();
300 case 8: CP(char); break;
301 case 16: CP(short); break;
302 case 32: CP(long); break;
311 TYPE *d=ALLOC_N(TYPE, n); \
312 if(d==NULL) return; \
313 for(i=0; i<n; i++) { \
314 if(!extl_table_geti_i(tab, i+1, &tmp)) return; \
321 static bool get_mode(const char *mode, int *m)
323 if(strcmp(mode, "replace")==0)
325 else if(strcmp(mode, "prepend")==0)
327 else if(strcmp(mode, "append")==0)
337 * Modify a window property. The \var{mode} is one of
338 * \codestr{replace}, \codestr{prepend} or \codestr{append}, and format
339 * is either 8, 16 or 32. Also see \fnref{ioncore.x_get_window_property}
340 * and the \code{XChangeProperty}(3) manual page.
343 void ioncore_x_change_property(int win, int atom, int atom_type,
344 int format, const char *mode, ExtlTab tab)
346 int tmp, m, i, n=extl_table_get_n(tab);
349 if(n<0 || !get_mode(mode, &m)){
350 warn(TR("Invalid arguments."));
355 case 8: GET(char); break;
356 case 16: GET(short); break;
357 case 32: GET(long); break;
359 warn(TR("Invalid arguments."));
363 XChangeProperty(ioncore_g.dpy, win, atom, atom_type, format, m, p, n);
370 * Delete a window property.
373 void ioncore_x_delete_property(int win, int atom)
375 XDeleteProperty(ioncore_g.dpy, win, atom);
380 * Get a text property for a window. The fields in the returned
381 * table (starting from 1) are the null-separated parts of the property.
382 * See the \code{XGetTextProperty}(3) manual page for more information.
386 ExtlTab ioncore_x_get_text_property(int win, int atom)
390 ExtlTab tab=extl_table_none();
392 list=xwindow_get_text_property(win, atom, &n);
396 tab=extl_create_table();
398 extl_table_seti_s(tab, i+1, list[i]);
400 XFreeStringList(list);
408 * Set a text property for a window. The fields of \var{tab} starting from
409 * 1 should be the different null-separated parts of the property.
410 * See the \code{XSetTextProperty}(3) manual page for more information.
413 void ioncore_x_set_text_property(int win, int atom, ExtlTab tab)
416 int i, n=extl_table_get_n(tab);
418 list=ALLOC_N(char*, n);
425 extl_table_geti_s(tab, i+1, &(list[i]));
428 xwindow_set_text_property(win, atom, (const char **)list, n);
430 XFreeStringList(list);