summaryrefslogtreecommitdiff
path: root/src/conn.c
blob: 39833c890fa463579e066e71cb3389dbc6dc78a5 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#include "module.h"
#include "except.h"
#include "cookie.h"
#include "response.h"
#include "event.h"
#include "error.h"
#include "extkey.h"
#include "ext.h"
#include "conn.h"

/*
 * Helpers
 */
int
xpybConn_invalid(xpybConn *self)
{
    if (self->conn == NULL) {
	PyErr_SetString(xpybExcept_base, "Invalid connection.");
	return 1;
    }
    if (xcb_connection_has_error(self->conn)) {
	PyErr_SetString(xpybExcept_base, "An error has occurred on the connection.");
	return 1;
    }
    return 0;
}

static int
xpyb_parse_auth(const char *authstr, int authlen, xcb_auth_info_t *auth)
{
    int i = 0;

    while (i < authlen && authstr[i] != ':')
	i++;

    if (i >= authlen) {
	PyErr_SetString(xpybExcept_base, "Auth string must take the form '<name>:<data>'.");
	return -1;
    }

    auth->name = (char *)authstr;
    auth->namelen = i++;
    auth->data = (char *)authstr + i;
    auth->datalen = authlen - i;
    return 0;
}

int
xpybConn_init_struct(xpybConn *self, PyObject *core_type)
{
    self->core = PyObject_CallFunctionObjArgs(core_type, self, NULL);
    if (self->core == NULL)
        return -1;

    self->dict = PyDict_New();
    if (self->dict == NULL)
        return -1;

   self->extcache = PyDict_New();
    if (self->extcache == NULL)
        return -1;

    self->wrapped = 0;
    self->setup = NULL;
    self->events = NULL;
    self->events_len = 0;
    self->errors = NULL;
    self->errors_len = 0;
    return 0;
}

int
xpybConn_init(xpybConn *self, PyObject *args, PyObject *kw)
{
    static char *kwlist[] = { "display", "fd", "auth", NULL };
    const char *displayname = NULL, *authstr = NULL;
    xcb_auth_info_t auth, *authptr = NULL;
    int authlen, fd = -1;

    /* Make sure core was set. */
    if (xpybModule_core == NULL) {
	PyErr_SetString(xpybExcept_base, "No core protocol object has been set.  Did you import xcb.xproto?");
        return -1;
    }

    /* Parse arguments and allocate new connection object */
    if (!PyArg_ParseTupleAndKeywords(args, kw, "|ziz#", kwlist, &displayname,
				     &fd, &authstr, &authlen))
        return -1;

    /* Set up authorization */
    if (authstr != NULL) {
	if (xpyb_parse_auth(authstr, authlen, &auth) < 0)
            return -1;
	authptr = &auth;
    }

    /* Connect to display */
    if (fd >= 0)
	self->conn = xcb_connect_to_fd(fd, authptr);
    else if (authptr)
	self->conn = xcb_connect_to_display_with_auth_info(displayname, authptr, &self->pref_screen);
    else
	self->conn = xcb_connect(displayname, &self->pref_screen);

    if (xcb_connection_has_error(self->conn)) {
	PyErr_SetString(xpybExcept_conn, "Failed to connect to X server.");
        return -1;
    }

    xpybConn_init_struct(self, (PyObject *)xpybModule_core);

    /* Load extensions */
    if (xpybConn_setup(self) < 0)
	return -1;

    return 0;
}

static xpybExt *
xpybConn_load_ext(xpybConn *self, PyObject *key)
{
    PyObject *type;
    xpybExt *ext;
    const xcb_query_extension_reply_t *reply;

    ext = (xpybExt *)PyDict_GetItem(self->extcache, key);
    Py_XINCREF(ext);

    if (ext == NULL) {
	/* Look up the extension type in the global dictionary. */
	type = PyDict_GetItem(xpybModule_extdict, key);
	if (type == NULL) {
	    PyErr_SetString(xpybExcept_ext, "No extension found for that key.");
	    return NULL;
	}

	/* Call the type object to get a new xcb.Extension object. */
	ext = (xpybExt *)PyObject_CallFunctionObjArgs(type, self, key, NULL);
	if (ext == NULL)
	    return NULL;

	/* Get the opcode and base numbers. */
	reply = xcb_get_extension_data(self->conn, &((xpybExtkey *)key)->key);
	ext->present = reply->present;
	ext->major_opcode = reply->major_opcode;
	ext->first_event = reply->first_event;
	ext->first_error = reply->first_error;

	if (PyDict_SetItem(self->extcache, key, (PyObject *)ext) < 0)
	    return NULL;
    }

    return ext;
}

static int
xpybConn_setup_helper(xpybConn *self, xpybExt *ext, PyObject *events, PyObject *errors)
{
    Py_ssize_t j = 0;
    unsigned char opcode, newlen;
    PyObject *num, *type, **newmem;

    while (PyDict_Next(events, &j, &num, &type)) {
	opcode = ext->first_event + PyInt_AS_LONG(num);
	if (opcode >= self->events_len) {
	    newlen = opcode + 1;
	    newmem = realloc(self->events, newlen * sizeof(PyObject *));
	    if (newmem == NULL)
		return -1;
	    memset(newmem + self->events_len, 0, (newlen - self->events_len) * sizeof(PyObject *));
	    self->events = newmem;
	    self->events_len = newlen;
	}
	Py_INCREF(self->events[opcode] = type);
    }

    j = 0;
    while (PyDict_Next(errors, &j, &num, &type)) {
	opcode = ext->first_error + PyInt_AS_LONG(num);
	if (opcode >= self->errors_len) {
	    newlen = opcode + 1;
	    newmem = realloc(self->errors, newlen * sizeof(PyObject *));
	    if (newmem == NULL)
		return -1;
	    memset(newmem + self->errors_len, 0, (newlen - self->errors_len) * sizeof(PyObject *));
	    self->errors = newmem;
	    self->errors_len = newlen;
	}
	Py_INCREF(self->errors[opcode] = type);
    }

    return 0;
}

int
xpybConn_setup(xpybConn *self)
{
    PyObject *key, *events, *errors;
    xpybExt *ext;
    Py_ssize_t i = 0;
    int rc = -1;

    ext = (xpybExt *)self->core;
    events = xpybModule_core_events;
    errors = xpybModule_core_errors;
    if (xpybConn_setup_helper(self, ext, events, errors) < 0)
	return -1;

    ext = NULL;
    while (PyDict_Next(xpybModule_ext_events, &i, &key, &events)) {
	errors = PyDict_GetItem(xpybModule_ext_errors, key);
	if (errors == NULL)
	    goto out;

	Py_XDECREF(ext);
	ext = xpybConn_load_ext(self, key);
	if (ext == NULL)
	    goto out;
	if (ext->present)
	    if (xpybConn_setup_helper(self, ext, events, errors) < 0)
		goto out;
    }

    rc = 0;
out:
    Py_XDECREF(ext);
    return rc;
}

/*
 * Infrastructure
 */

static PyObject *
xpybConn_new(PyTypeObject *self, PyObject *args, PyObject *kw)
{
    return PyType_GenericNew(self, args, kw);
}

static void
xpybConn_dealloc(xpybConn *self)
{
    int i;

    Py_CLEAR(self->dict);
    Py_CLEAR(self->core);
    Py_CLEAR(self->setup);
    Py_CLEAR(self->extcache);

    if (self->conn && !self->wrapped)
	xcb_disconnect(self->conn);

    for (i = 0; i < self->events_len; i++)
	Py_XDECREF(self->events[i]);
    for (i = 0; i < self->errors_len; i++)
	Py_XDECREF(self->errors[i]);

    free(self->events);
    free(self->errors);
    self->ob_type->tp_free((PyObject *)self);
}

static PyObject *
xpybConn_getattro(xpybConn *self, PyObject *obj)
{
    const char *name = PyString_AS_STRING(obj);
    PyMethodDef *mptr = xpybConn_type.tp_methods;
    PyMemberDef *sptr = xpybConn_type.tp_members;
    PyObject *result;

    while (mptr && mptr->ml_name)
	if (strcmp(name, (mptr++)->ml_name) == 0)
	    goto out2;
    while (sptr && sptr->name)
	if (strcmp(name, (sptr++)->name) == 0)
	    goto out2;
	
    Py_XINCREF(result = PyDict_GetItem(self->dict, obj));
    if (result != NULL || PyErr_Occurred())
	return result;

    return xpybConn_type.tp_base->tp_getattro((PyObject *)self, obj);
out2:
    return PyObject_GenericGetAttr((PyObject *)self, obj);
}

static int
xpybConn_setattro(xpybConn *self, PyObject *obj, PyObject *val)
{
    const char *name = PyString_AS_STRING(obj);
    PyMethodDef *mptr = xpybConn_type.tp_methods;
    PyMemberDef *sptr = xpybConn_type.tp_members;

    while (mptr && mptr->ml_name)
	if (strcmp(name, (mptr++)->ml_name) == 0)
	    goto out2;
    while (sptr && sptr->name)
	if (strcmp(name, (sptr++)->name) == 0)
	    goto out2;

    return val ? PyDict_SetItem(self->dict, obj, val) : PyDict_DelItem(self->dict, obj);
out2:
    return PyObject_GenericSetAttr((PyObject *)self, obj, val);
}

static PyObject *
xpybConn_call(xpybConn *self, PyObject *args, PyObject *kw)
{
    static char *kwlist[] = { "key", NULL };
    PyObject *key;
    xpybExt *ext;

    /* Parse the extension key argument and check connection. */
    if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist, &xpybExtkey_type, &key))
	return NULL;
    if (xpybConn_invalid(self))
	return NULL;

    /* Check our dictionary of cached values */
    ext = xpybConn_load_ext(self, key);
    if (!ext->present) {
	PyErr_SetString(xpybExcept_ext, "Extension not present on server.");
	Py_DECREF(ext);
	return NULL;
    }

    return (PyObject *)ext;
}


/*
 * Members
 */

static PyMemberDef xpybConn_members[] = {
    { "pref_screen",
      T_INT,
      offsetof(xpybConn, pref_screen),
      READONLY,
      "Preferred display screen" },

    { "core",
      T_OBJECT,
      offsetof(xpybConn, core),
      READONLY,
      "Core protocol object" },

    { "__dict__",
      T_OBJECT,
      offsetof(xpybConn, dict),
      READONLY,
      "Instance dictionary object" },

    { NULL } /* terminator */
};


/*
 * Methods
 */

static PyObject *
xpybConn_has_error(xpybConn *self, PyObject *args)
{
    if (self->conn == NULL) {
	PyErr_SetString(xpybExcept_base, "Invalid connection.");
	return NULL;
    }

    return Py_BuildValue("i", xcb_connection_has_error(self->conn));
}

static PyObject *
xpybConn_get_file_descriptor(xpybConn *self, PyObject *args)
{
    if (xpybConn_invalid(self))
	return NULL;

    return Py_BuildValue("i", xcb_get_file_descriptor(self->conn));
}

static PyObject *
xpybConn_get_maximum_request_length(xpybConn *self, PyObject *args)
{
    if (xpybConn_invalid(self))
	return NULL;

    return Py_BuildValue("I", xcb_get_maximum_request_length(self->conn));
}

static PyObject *
xpybConn_prefetch_maximum_request_length(xpybConn *self, PyObject *args)
{
    if (xpybConn_invalid(self))
	return NULL;

    xcb_prefetch_maximum_request_length(self->conn);
    Py_RETURN_NONE;
}

static PyObject *
xpybConn_get_setup(xpybConn *self, PyObject *args)
{
    const xcb_setup_t *s;
    PyObject *shim, *type;

    if (xpybConn_invalid(self))
	return NULL;

    if (self->setup == NULL) {
	s = xcb_get_setup(self->conn);
	shim = PyBuffer_FromMemory((void *)s, 8 + s->length * 4);
	if (shim == NULL)
	    return NULL;
	type = (PyObject *)xpybModule_setup;
	self->setup = PyObject_CallFunctionObjArgs(type, shim, Py_False, NULL);
	Py_DECREF(shim);
    }

    Py_XINCREF(self->setup);
    return self->setup;
}

static PyObject *
xpybConn_wait_for_event(xpybConn *self, PyObject *args)
{
    xcb_generic_event_t *data;

    if (xpybConn_invalid(self))
	return NULL;

    data = xcb_wait_for_event(self->conn);

    if (data == NULL) {
	PyErr_SetString(PyExc_IOError, "I/O error on X server connection.");
	return NULL;
    }

    if (data->response_type == 0) {
	xpybError_set(self, (xcb_generic_error_t *)data);
	return NULL;
    }

    return xpybEvent_create(self, data);
}

static PyObject *
xpybConn_poll_for_event(xpybConn *self, PyObject *args)
{
    xcb_generic_event_t *data;

    if (xpybConn_invalid(self))
	return NULL;

    data = xcb_poll_for_event(self->conn);

    if (data == NULL) {
        if (xpybConn_invalid(self))
            return NULL;
        else
            Py_RETURN_NONE;
    }

    if (data->response_type == 0) {
	xpybError_set(self, (xcb_generic_error_t *)data);
	return NULL;
    }

    return xpybEvent_create(self, data);
}

static PyObject *
xpybConn_flush(xpybConn *self, PyObject *args)
{
    if (xpybConn_invalid(self))
	return NULL;

    xcb_flush(self->conn);
    Py_RETURN_NONE;
}

static PyObject *
xpybConn_generate_id(xpybConn *self)
{
    unsigned int xid;

    if (xpybConn_invalid(self))
	return NULL;

    xid = xcb_generate_id(self->conn);
    if (xid == (unsigned int)-1) {
	PyErr_SetString(xpybExcept_base, "No more free XID's available.");
	return NULL;
    }

    return Py_BuildValue("I", xid);
}

static PyObject *
xpybConn_disconnect(xpybConn *self, PyObject *args)
{
    if (self->conn)
	xcb_disconnect(self->conn);
    self->conn = NULL;
    Py_RETURN_NONE;
}

static PyMethodDef xpybConn_methods[] = {
    { "has_error",
      (PyCFunction)xpybConn_has_error,
      METH_NOARGS,
      "Test whether the connection has shut down due to a fatal error." },

    { "get_file_descriptor",
      (PyCFunction)xpybConn_get_file_descriptor,
      METH_NOARGS,
      "Access the file descriptor of the connection." },

    { "get_maximum_request_length",
      (PyCFunction)xpybConn_get_maximum_request_length,
      METH_NOARGS,
      "Returns the maximum request length that this server accepts." },

    { "prefetch_maximum_request_length",
      (PyCFunction)xpybConn_prefetch_maximum_request_length,
      METH_NOARGS,
      "Prefetch the maximum request length without blocking." },

    { "get_setup",
      (PyCFunction)xpybConn_get_setup,
      METH_NOARGS,
      "Accessor for the connection information returned by the server." },

    { "wait_for_event",
      (PyCFunction)xpybConn_wait_for_event,
      METH_NOARGS,
      "Returns the next event or raises the next error from the server." },

    { "poll_for_event",
      (PyCFunction)xpybConn_poll_for_event,
      METH_NOARGS,
      "Returns the next event or raises the next error from the server." },

    { "flush",
      (PyCFunction)xpybConn_flush,
      METH_NOARGS,
      "Forces any buffered output to be written to the server." },

    { "generate_id",
      (PyCFunction)xpybConn_generate_id,
      METH_NOARGS,
      "Allocates an XID for a new object." },

    { "disconnect",
      (PyCFunction)xpybConn_disconnect,
      METH_NOARGS,
      "Disconnects from the X server." },

    { NULL } /* terminator */
};


/*
 * Definition
 */

PyTypeObject xpybConn_type = {
    PyObject_HEAD_INIT(NULL)
    .tp_name = "xcb.Connection",
    .tp_basicsize = sizeof(xpybConn),
    .tp_new = xpybConn_new,
    .tp_dealloc = (destructor)xpybConn_dealloc,
    .tp_init = (initproc)xpybConn_init,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_doc = "XCB connection object",
    .tp_methods = xpybConn_methods,
    .tp_members = xpybConn_members,
    .tp_call = (ternaryfunc)xpybConn_call,
    .tp_getattro = (getattrofunc)xpybConn_getattro,
    .tp_setattro = (setattrofunc)xpybConn_setattro
};


/*
 * Module init
 */
int xpybConn_modinit(PyObject *m)
{
    if (PyType_Ready(&xpybConn_type) < 0)
        return -1;
    Py_INCREF(&xpybConn_type);
    if (PyModule_AddObject(m, "Connection", (PyObject *)&xpybConn_type) < 0)
	return -1;

    return 0;
}