wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

x11.c (4387B)


      1 /* Copyright ©2007 Kris Maglione <maglione.k at Gmail>
      2  * See LICENSE file for license details.
      3  */
      4 #include <assert.h>
      5 #include "x11.h"
      6 
      7 /* Misc */
      8 static Atom
      9 xatom(Display *display, char *name) {
     10 	/* Blech. I don't trust Xlib's cacheing.
     11 	MapEnt *e;
     12 
     13 	e = hash_get(&amap, name, 1);
     14 	if(e->val == nil)
     15 		e->val = (void*)XInternAtom(display, name, False);
     16 	return (Atom)e->val;
     17 	*/
     18 	return XInternAtom(display, name, False);
     19 }
     20 /* Properties */
     21 #if 0
     22 static void
     23 delproperty(Display *display, Window w, char *prop) {
     24 	XDeleteProperty(display, w, xatom(display, prop));
     25 }
     26 #endif
     27 
     28 static void
     29 changeproperty(Display *display, Window w, char *prop, char *type, int width, uchar data[], int n) {
     30 	XChangeProperty(display, w, xatom(display, prop), xatom(display, type), width, PropModeReplace, data, n);
     31 }
     32 
     33 static void
     34 changeprop_string(Display *display, Window w, char *prop, char *string) {
     35 	changeprop_char(display, w, prop, "UTF8_STRING", string, strlen(string));
     36 }
     37 
     38 static void
     39 changeprop_char(Display *display, Window w, char *prop, char *type, char data[], int len) {
     40 	changeproperty(display, w, prop, type, 8, (uchar*)data, len);
     41 }
     42 
     43 #if 0
     44 static void
     45 changeprop_short(Display *display, Window w, char *prop, char *type, short data[], int len) {
     46 	changeproperty(display, w, prop, type, 16, (uchar*)data, len);
     47 }
     48 #endif
     49 
     50 static void
     51 changeprop_long(Display *display, Window w, char *prop, char *type, long data[], int len) {
     52 	changeproperty(display, w, prop, type, 32, (uchar*)data, len);
     53 }
     54 
     55 static void
     56 changeprop_textlist(Display *display, Window w, char *prop, char *type, char *data[]) {
     57 	char **p, *s, *t;
     58 	int len, n;
     59 
     60 	len = 0;
     61 	for(p=data; *p; p++)
     62 		len += strlen(*p) + 1;
     63 	s = malloc(len);
     64 	if(s == nil)
     65 		return;
     66 	t = s;
     67 	for(p=data; *p; p++) {
     68 		n = strlen(*p) + 1;
     69 		memcpy(t, *p, n);
     70 		t += n;
     71 	}
     72 	changeprop_char(display, w, prop, type, s, len);
     73 	free(s);
     74 }
     75 
     76 #if 0
     77 static void
     78 freestringlist(char *list[]) {
     79 	XFreeStringList(list);
     80 }
     81 #endif
     82 
     83 static ulong
     84 getprop(Display *display, Window w, char *prop, char *type, Atom *actual, int *format, ulong offset, uchar **ret, ulong length) {
     85 	Atom typea;
     86 	ulong n, extra;
     87 	int status;
     88 
     89 	typea = (type ? xatom(display, type) : 0L);
     90 
     91 	status = XGetWindowProperty(display, w,
     92 		xatom(display, prop), offset, length, False /* delete */,
     93 		typea, actual, format, &n, &extra, ret);
     94 
     95 	if(status != Success) {
     96 		*ret = nil;
     97 		return 0;
     98 	}
     99 	if(n == 0) {
    100 		free(*ret);
    101 		*ret = nil;
    102 	}
    103 	return n;
    104 }
    105 
    106 #if 0
    107 static ulong
    108 getproperty(Display *display, Window w, char *prop, char *type, Atom *actual, ulong offset, uchar **ret, ulong length) {
    109 	int format;
    110 
    111 	return getprop(display, w, prop, type, actual, &format, offset, ret, length);
    112 }
    113 #endif
    114 
    115 static ulong
    116 getprop_long(Display *display, Window w, char *prop, char *type, ulong offset, long **ret, ulong length) {
    117 	Atom actual;
    118 	ulong n;
    119 	int format;
    120 
    121 	n = getprop(display, w, prop, type, &actual, &format, offset, (uchar**)ret, length);
    122 	if(n == 0 || format == 32 && xatom(display, type) == actual)
    123 		return n;
    124 	free(*ret);
    125 	*ret = 0;
    126 	return 0;
    127 }
    128 
    129 #ifdef notdef
    130 static char**
    131 strlistdup(char *list[], int n) {
    132 	char **p, *q;
    133 	int i, m;
    134 
    135 	for(i=0, m=0; i < n; i++)
    136 		m += strlen(list[i])+1;
    137 
    138 	p = malloc((n+1)*sizeof(char*) + m);
    139 	if(p == nil)
    140 		return nil;
    141 	q = (char*)&p[n+1];
    142 
    143 	for(i=0; i < n; i++) {
    144 		p[i] = q;
    145 		m = strlen(list[i])+1;
    146 		memcpy(q, list[i], m);
    147 		q += m;
    148 	}
    149 	p[n] = nil;
    150 	return p;
    151 }
    152 #endif
    153 
    154 static char**
    155 strlistdup(char *list[], int n) {
    156 	char **p, *q;
    157 	int i, m;
    158 
    159 	m = 0;
    160 	for(i=0; i < n; i++)
    161 		m += strlen(list[i]) + 1;
    162 
    163 	p = malloc((n+1) * sizeof(*p) + m);
    164 	q = (char*)&p[n+1];
    165 
    166 	for(i=0; i < n; i++) {
    167 		p[i] = q;
    168 		m = strlen(list[i]) + 1;
    169 		memcpy(q, list[i], m);
    170 		q += m;
    171 	}
    172 	p[n] = nil;
    173 	return p;
    174 }
    175 
    176 #if 0
    177 static int
    178 getprop_textlist(Display *display, Window w, char *name, char **ret[]) {
    179 	XTextProperty prop;
    180 	char **list;
    181 	int n;
    182 
    183 	n = 0;
    184 
    185 	XGetTextProperty(display, w, &prop, xatom(display, name));
    186 	if(prop.nitems > 0) {
    187 		if(Xutf8TextPropertyToTextList(display, &prop, &list, &n) == Success) {
    188 			*ret = strlistdup(list, n);
    189 			XFreeStringList(list);
    190 		}
    191 		XFree(prop.value);
    192 	}
    193 	return n;
    194 }
    195 #endif
    196 
    197 #if 0
    198 static char*
    199 getprop_string(Display *display, Window w, char *name) {
    200 	char **list, *str;
    201 	int n;
    202 
    203 	str = nil;
    204 
    205 	n = getprop_textlist(display, w, name, &list);
    206 	if(n > 0)
    207 		str = strdup(*list);
    208 	freestringlist(list);
    209 
    210 	return str;
    211 }
    212 #endif
    213