summaryrefslogtreecommitdiffstats
path: root/xutils.c
blob: 8eb9ee1acfe20faab6ff5de25b4832b21f37dcc1 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * xutils.c - a Python wrapper for common Xlib ops
 *
 * Michael Fulbright <msf@redhat.com>
 *
 * Copyright 2002 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include <Python.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <gdk/gdkx.h>

static PyObject * getRootResources(PyObject *s, PyObject *args);
static PyObject * setRootResource(PyObject * s, PyObject * args);
static PyObject * screenHeight (PyObject * s, PyObject * args);
static PyObject * screenWidth (PyObject * s, PyObject * args);
static PyObject * getXatom(PyObject *s, PyObject *args);


static PyMethodDef xutilsMethods[] = {
    { "getRootResources", getRootResources, 1, NULL },
    { "setRootResource", setRootResource, 1, NULL },
    { "screenHeight", screenHeight, 1, NULL },
    { "screenWidth", screenWidth, 1, NULL },
    { "getXatom", getXatom, 1, NULL },
    { NULL, NULL, 0, NULL }
};

typedef struct _Resource {
    char *key, *val;
} Resource;


static int
openDisplay(Display **dpy, Window *root) 
{
    int     scrn;

    *dpy=XOpenDisplay("");
    if (!*dpy)
	return -1;

    scrn=DefaultScreen(*dpy);
    *root = RootWindow(*dpy, scrn);
    return 0;
}

static void
closeDisplay(Display *dpy)
{
   XCloseDisplay(dpy);
}    

static Resource **
getCurrentResources(Display *dpy)
{
    char *resource_string, *p;
    Resource **rc;
    int  nrec;

    /* read through current resources, split on newlines */
    resource_string = XResourceManagerString(dpy);

    if (!resource_string)
	return NULL;

    rc = (Resource **)malloc(sizeof(Resource *));
    p = resource_string;
    nrec = 0;
    while (1) {
	char *eol;
	char *sep;
	int nleft;

	/* find next newline, defines end of current record */
	eol = strchr(p, '\n');

	if (!eol)
	    break;

	/* find colon separating key and value */
	/* if no colon skip this record        */
	sep = strchr(p, ':');
	if (sep) {
	    int  len;
	    Resource *newrec;

	    newrec = (Resource *) malloc(sizeof(Resource));

	    len = sep - p + 1;
	    newrec->key = (char *) malloc(len*sizeof(char));
	    memcpy(newrec->key, p, len);
	    newrec->key[len-1] = '\0';

	    len = eol - sep;
	    newrec->val = (char *) malloc(len*sizeof(char));
	    memcpy(newrec->val, sep+1, len);
	    newrec->val[len-1] = '\0';

	    rc = (Resource **) realloc(rc, (nrec+1) * sizeof(Resource *));
	    rc[nrec] = newrec;
	    nrec = nrec + 1;
	}

	nleft = strlen(resource_string) - (eol-resource_string);
	if (nleft <= 0)
	    break;

	p = eol + 1;
    }

    if (nrec > 0) {
	rc = (Resource **) realloc(rc, (nrec+1) * sizeof(Resource *));
	rc[nrec] = NULL;
    } else {
	rc = NULL;
    }

    return rc;
}

static void
freeResources(Resource **rc)
{
    int idx;

    if (!rc)
	return;

    idx = 0;
    while (rc[idx]) {
	free(rc[idx]->key);
	free(rc[idx]->val);
	free(rc[idx]);

	idx++;
    }

    free(rc);
}

/* return dictionary of resources on root display */
PyObject *
getRootResources(PyObject *s, PyObject *args) {
    Display *dpy;
    Window  root;
    Resource **resources, **p;
    PyObject *rc;

    if (openDisplay(&dpy, &root) < 0) {
	PyErr_SetString(PyExc_SystemError, "Could not open display.");
	return NULL;
    }
	
    resources = getCurrentResources(dpy);
    if (!resources) {
	closeDisplay(dpy);
	Py_INCREF(Py_None);
	return Py_None;
    }

    rc = PyDict_New();
    p = resources;
    while (*p) {
	PyDict_SetItemString(rc, (*p)->key,  Py_BuildValue("s", (*p)->val));
	p++;
    }

    freeResources(resources);
    closeDisplay(dpy);

    return rc;
}

static PyObject *
setRootResource(PyObject *s, PyObject *args)
{
    Display *dpy;
    Window  root;
    Resource **resources, **p;
    char *key, *val, *rstring;
    int fnd, nrec;

    if (!PyArg_ParseTuple(args, "ss", &key, &val)) {
	return NULL;
    }

    if (openDisplay(&dpy, &root) < 0) {
	PyErr_SetString(PyExc_SystemError, "Could not open display.");
	return NULL;
    }

    resources = getCurrentResources(dpy);
    fnd = 0;
    nrec = 0;
    if (resources) {
	p = resources;
	while (*p) {
	    if (!strcmp(key, (*p)->key)) {
		free((*p)->val);
		(*p)->val = strdup(val);
		fnd = 1;
		break;
	    }

	    p++;
	}

	p = resources;
	while (*p) {
	    nrec++;
	    p++;
	}
    }

    if (!fnd) {
	Resource *newrec;

	newrec = (Resource *) malloc(sizeof(Resource));
	newrec->key = strdup(key);
	newrec->val = strdup(val);

	if (nrec > 0)
	    resources = (Resource **) realloc(resources, (nrec+2) * sizeof(Resource *));
	else
	    resources = (Resource **) malloc(2*sizeof(Resource *));

	resources[nrec] = newrec;
	resources[nrec+1] = NULL;
    }

    rstring = NULL;
    p = resources;
    while (*p) {
	int len;
	char *tmpstr;

	len = strlen((*p)->key) + strlen((*p)->val) + 3;
	tmpstr = (char *) malloc(len*sizeof(char));
	strcpy(tmpstr, (*p)->key);
	strcat(tmpstr, ":");
	strcat(tmpstr, (*p)->val);
	strcat(tmpstr, "\n");

	if (rstring) {
	    rstring = (char *)realloc(rstring, (strlen(rstring)+len+1)*sizeof(char));
	    strcat(rstring, tmpstr);
	} else {
	    rstring = tmpstr;
	}

	p++;
    }

    XChangeProperty(dpy, root, XA_RESOURCE_MANAGER, XA_STRING, 
		    8, PropModeReplace, (unsigned char *)rstring,
		    strlen(rstring));

    free(rstring);
    freeResources(resources);

    closeDisplay(dpy);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
screenHeight(PyObject *s, PyObject *args)
{
    Display *dpy;
    Window  root;
    int     scrn;
    PyObject *rc;

    if (openDisplay(&dpy, &root) < 0) {
	PyErr_SetString(PyExc_SystemError, "Could not open display.");
	return NULL;
    }

    scrn=DefaultScreen(dpy);

    rc = Py_BuildValue("i", DisplayHeight(dpy, scrn));

    closeDisplay(dpy);
    return rc;
}

static PyObject *
screenWidth(PyObject *s, PyObject *args)
{
    Display *dpy;
    Window  root;
    int     scrn;
    PyObject *rc;

    if (openDisplay(&dpy, &root) < 0) {
	PyErr_SetString(PyExc_SystemError, "Could not open display.");
	return NULL;
    }

    scrn=DefaultScreen(dpy);

    rc = Py_BuildValue("i", DisplayWidth(dpy, scrn));

    closeDisplay(dpy);
    return rc;
}

/* this assumes you've already imported gtk and thus have a display */
static PyObject * 
getXatom(PyObject *s, PyObject *args)
{
    char *atomname;
    Atom theatom;

    if (!PyArg_ParseTuple(args, "s", &atomname)) {
	return NULL;
    }

    theatom = gdk_x11_get_xatom_by_name(atomname);
    if (XGetSelectionOwner (GDK_DISPLAY(), theatom) != None) {
        Py_INCREF(Py_True);
        return Py_True;
    }
    Py_INCREF(Py_False);
    return Py_False;
}

void 
initxutils ()
{
    PyObject * d;

    d = Py_InitModule ("xutils", xutilsMethods);
}